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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM