Determinant【矩阵的行列式的求法】

题意

给出一个整数 \(x\) 和两个数组:\(a_1,a_2,\cdots,a_n,b_1,b_2,\cdots ,b_n\)。生成一个 \(n\times n\) 的矩阵 \(M\),定义如下:

\[M_{i,j}=
\begin{cases}
x+a_ib_j &, i=j\\
\\
a_ib_j &,\text{othe}r
\end{cases}
\]

求出矩阵 \(M\) 的行列式,模 \(10^9+7\) ,即 \(\det(M)\mod 10^9+7\)

题目链接:https://ac.nowcoder.com/acm/contest/7502/D

分析参考:https://zhuanlan.zhihu.com/p/34685081

分析

由题意得:

\[M=\left[
\begin{matrix}
a_1b_1+x & a_1b_2 & a_1b_3 &\cdots & a_1b_n\\
a_2b_1 & a_2b_2+x & a_2b_3 &\cdots &a_2b_n\\
a_3b_1 & a_3b_2 & a_3b_3+x &\cdots &a_3b_n\\
\vdots &\vdots & \vdots &\ddots &\vdots\\
a_nb_1 & a_nb_2 &a_nb_3 &\cdots &a_nb_n+x
\end{matrix}
\right]
\]

\(M\) 进行加边升阶,有:

\[M'=\left[
\begin{matrix}
1 & b_1 & b_2 & b_3 & \cdots & b_n\\
0 & a_1b_1+x & a_1b_2 & a_1b_3 &\cdots & a_1b_n\\
0 & a_2b_1 & a_2b_2+x & a_2b_3 &\cdots &a_2b_n\\
0 & a_3b_1 & a_3b_2 & a_3b_3+x &\cdots &a_3b_n\\
\vdots & \vdots &\vdots & \vdots &\ddots &\vdots\\
0 & a_nb_1 & a_nb_2 &a_nb_3 &\cdots &a_nb_n+x
\end{matrix}
\right]
\]

对于 \(M'\)\(2\)\(n+1\) 行,每行减去 \(a_i\) 乘第 \(1\) 行的值,得:

\[M''=\left[
\begin{matrix}
1 & b_1 & b_2 & b_3 & \cdots & b_n\\
-a_1 & x & 0 & 0 &\cdots & 0\\
-a_2 & 0 & x & 0 &\cdots &0\\
-a_3 & 0 & 0 & x &\cdots &0\\
\vdots & \vdots &\vdots & \vdots &\ddots &\vdots\\
-a_n & 0 & 0 &0 &\cdots &x
\end{matrix}
\right]
\]

从第二列开始,每列乘上 \(\frac{a_i}{x}\) 后加到第 \(1\) 列上,得:

\[M'''=\left[
\begin{matrix}
1+\frac{1}{x}\sum_{i=1}^{n}{a_ib_i} & b_1 & b_2 & b_3 & \cdots & b_n\\
0 & x & 0 & 0 &\cdots & 0\\
0 & 0 & x & 0 &\cdots &0\\
0 & 0 & 0 & x &\cdots &0\\
\vdots & \vdots &\vdots & \vdots &\ddots &\vdots\\
0 & 0 & 0 &0 &\cdots &x
\end{matrix}
\right]
\]

即转化为一个上三角形式,最终答案为主对角线上得元素的乘积:

\[ANS=x^n+x^{n-1}·\sum_{i=1}^{n}{a_ib_i}
\]

代码

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=1e5+5;
int a[N],b[N];
ll power(ll x,ll y)
{
    ll res=1;
    x%=mod;
    while(y)
    {
        if(y&1)
            res=res*x%mod;
        y>>=1;
        x=x*x%mod;
    }
    return res;
}
int main()
{
    int x,n;
    while(scanf("%d%d",&n,&x)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)
            scanf("%d",&b[i]);
        ll ans=0;
        for(int i=1;i<=n;i++)
            ans=(ans+1LL*a[i]*b[i]%mod)%mod;
        ans=(power(1LL*x,1LL*n)+power(1LL*x,1LL*n-1)*ans%mod+mod)%mod;
        printf("%lld\n",ans);
    }
    return 0;
}

原文链接: https://www.cnblogs.com/1024-xzx/p/13974609.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    Determinant【矩阵的行列式的求法】

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/205099

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 下午10:05
下一篇 2023年2月12日 下午10:06

相关推荐