HDU~5015 233 Matrix(矩阵快速幂)

    xiaoxiao2021-04-17  37

    233 Matrix

    Problem Description  In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 … in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 2333, 23333… (it means a0,1 = 233,a0,2 = 2333,a0,3 = 23333…) Besides, in 233 matrix, we got ai,j = ai-1,j +ai,j-1( i,j ≠ 0). Now you have known a1,0,a2,0,…,an,0, could you tell me an,m in the 233 matrix?

    Input  There are multiple test cases. Please process till EOF.

    For each case, the first line contains two postive integers n,m(n ≤ 10,m ≤ 109). The second line contains n integers, a1,0,a2,0,…,an,0(0 ≤ ai,0 < 231).

    Output  For each case, output an,m mod 10000007.

    Sample Input  1 1  1  2 2  0 0  3 7  23 47 16

    Sample Output  234  2799  72937

    Hint

    Source  2014 ACM/ICPC Asia Regional Xi’an Online

    矩阵快速幂,求出其转换矩阵,然后那么第n行的第m个数就是转换矩阵的m次方*第一列,关键点在于如何求转换矩阵,这点要自己想办法;

    其转换矩阵为

    10 0 0 0 ….. 1  10 1 0 0 ….. 1  10 1 1 0 ….. 1  10 1 1 1 ….. 1  …….  0  0 0 0 …… 1

    初始矩阵

    23  a1  a2  a3  ….  3

    #include <iostream> #include<stdio.h> #include<string> #include<string.h> #include<algorithm> #include<math.h> #include<queue> #include<stdlib.h> #include<stdio.h> #include<map> #include<vector> #define mem(a,b) memset(a,b,sizeof(a)) #define inf 0x3f3f3f3f #define ll long long #define N 15 #define mod 10000007 using namespace std; struct Matrix { ll m[N][N]; }; int n,m; int s[15]; Matrix unit_; Matrix mul(Matrix a,Matrix b) { Matrix c; for(int i=0;i<=n+1;i++) { for(int j=0;j<=n+1;j++) { c.m[i][j]=0; for(int k=0;k<=n+1;k++) { c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j])%mod; } } } return c; } Matrix fast(Matrix a,int n) { Matrix res; res=unit_; while(n) { if(n&1) res=mul(res,a); n/=2; a=mul(a,a); } return res; } int main() { Matrix arr,p; while(~scanf("%d%d",&n,&m)) { //单位矩阵,在此处初始化很重要,如果定义在函数中,每次还得清零再初始化 //果断超时 for(int i=0;i<=n+1;i++) unit_.m[i][i]=1; for(int i=1;i<=n;i++) scanf("%d",&s[i]); s[0]=23,s[n+1]=3; mem(arr.m,0); //构造转换矩阵 for(int i=0;i<=n+1;i++) { arr.m[i][0]=10,arr.m[i][n+1]=1; for(int j=1;j<=i;j++) arr.m[i][j]=1; } for(int i=0; i<=n; ++i) arr.m[n+1][i]=0; p=fast(arr,m); int ans=0; for(int i=0;i<=n+1;i++) ans=(ans+s[i]*p.m[n][i]%mod)%mod; printf("%d\n",ans); } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-674244.html

    最新回复(0)