hdu 5015 233 Matrix 矩阵快速幂

来源:互联网 发布:三端口光纤环形器 编辑:程序博客网 时间:2024/05/22 01:32
233 MatrixTime Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 98    Accepted Submission(s): 47


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 112 20 03 723 47 16

Sample Output
234279972937
Hint


构造矩阵就行了。具体看代码把,可以用print 把矩阵打出来看;

不知道为什么G++能过,C++数组越界,路过大神求指点。

[cpp] view plaincopyprint?
  1. #include<stdio.h> 
  2. #include<string.h> 
  3. #define Matr 15 //矩阵大小,注意能小就小   矩阵从1开始   所以Matr 要+1 
  4. #define ll __int64 
  5. struct mat//矩阵结构体,a表示内容,size大小 矩阵从1开始   但size不用加一 
  6.     ll a[Matr][Matr]; 
  7.     mat()//构造函数 
  8.     { 
  9.         memset(a,0,sizeof(a)); 
  10.     } 
  11. }; 
  12. int Size; 
  13. ll mod= 10000007; 
  14.  
  15. mat multi(mat m1,mat m2)//两个相等矩阵的乘法,对于稀疏矩阵,有0处不用运算的优化 
  16.     mat ans=mat();  
  17.     for(int i=1;i<=Size;i++) 
  18.         for(int j=1;j<=Size;j++) 
  19.             if(m1.a[i][j])//稀疏矩阵优化 
  20.                 for(int k=1;k<=Size;k++) 
  21.                     ans.a[i][k]=(ans.a[i][k]+m1.a[i][j]*m2.a[j][k])%mod;//i行k列第j项 
  22.     return ans; 
  23.  
  24. mat quickmulti(mat m,ll n)//二分快速幂  
  25.     mat ans=mat(); 
  26.     int i; 
  27.     for(i=1;i<=Size;i++)ans.a[i][i]=1; 
  28.     while(n) 
  29.     { 
  30.         if(n&1)ans=multi(m,ans); 
  31.         m=multi(m,m); 
  32.         n>>=1; 
  33.     } 
  34.     return ans; 
  35.  
  36. void print(mat m)//输出矩阵信息,debug用   
  37. {   
  38.     int i,j;   
  39.     printf("%d\n",Size);   
  40.     for(i=1;i<=Size;i++)   
  41.     {   
  42.         for(j=1;j<=Size;j++) 
  43. <span style="white-space:pre">            </span>printf("%I64d ",m.a[i][j]);   
  44.         printf("\n");   
  45.     }   
  46. }   
  47. int main() 
  48.     int i; 
  49.     mat gou,chu=mat();//构造矩阵  初始矩阵   
  50.     int n,m; 
  51.     __int64 ans; 
  52.     while(scanf("%I64d%I64d",&n,&m)!=EOF)//n+2*n+2 
  53.     { 
  54.         Size=n+2; 
  55.         chu=mat(); 
  56.         chu.a[1][n+1]=23; 
  57.         for(i=1;i<=n;i++) 
  58.         { 
  59.             scanf("%I64d",&chu.a[1][i]); 
  60.             chu.a[1][i]%=mod; 
  61.         } 
  62.         chu.a[1][n+2]=3; 
  63.         gou=mat(); 
  64.         for(int j=1;j<=n;j++) 
  65.         { 
  66.             if(j==1) 
  67.             { 
  68.                 gou.a[n+1][j]=10; 
  69.                 gou.a[n+2][j]=1; 
  70.             } 
  71.             gou.a[j][j]=1; 
  72.             for(int i=1;i<=n+2;i++) 
  73.             { 
  74.                 gou.a[i][j]+=gou.a[i][j-1]; 
  75.             } 
  76.         } 
  77.         gou.a[n+1][n+1]=10; 
  78.         gou.a[n+2][n+2]=1; 
  79.         gou.a[n+2][n+1]=1; 
  80.         ans=multi(chu,quickmulti(gou,m)).a[1][n]; 
  81.         printf("%I64d\n",ans%mod); 
  82.     } 
  83.     return 0; 
  84.   

0 0
原创粉丝点击