zoukankan      html  css  js  c++  java
  • Determinant【矩阵的行列式的求法】

    题意

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

    [M_{i,j}= egin{cases} x+a_ib_j &, i=j\ \ a_ib_j &, ext{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[ egin{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} ight] ]

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

    [M'=left[ egin{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} ight] ]

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

    [M''=left[ egin{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} ight] ]

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

    [M'''=left[ egin{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} ight] ]

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

    [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
    ",ans);
        }
        return 0;
    }
    
  • 相关阅读:
    python管理包(模块和包的应用)
    简单运行 Jupyter Notebook
    Linux出现“FirewallD is not running”解决办法
    Mindjet MindManager2020切换中文界面的教程
    idea 快捷键汇总
    南怀瑾经典语录
    CentOS 7 安装SonarQube 8.3版本
    Jenkins插件开发完全示例
    Jenkins在Pod中实现Docker in Docker并用kubectl进行部署
    Jenkins的kubernetes-plugin使用方法
  • 原文地址:https://www.cnblogs.com/1024-xzx/p/13974609.html
Copyright © 2011-2022 走看看