hdu 5015 233 Matrix 矩阵快速幂

本文介绍了一个名为233Matrix的独特矩阵问题及其解决方法。该问题涉及递归矩阵元素的计算,并通过输入一系列整数来求解特定位置的值。文章提供了具体的实现代码,并解释了如何使用矩阵乘法和快速幂技术高效地解决这个问题。
233 Matrix Time 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 a 0,1 = 233,a 0,2 = 2333,a 0,3 = 23333...) Besides, in 233 matrix, we got a i,j = a i-1,j +a i,j-1( i,j ≠ 0). Now you have known a 1,0,a 2,0,...,a n,0, could you tell me a n,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 ≤ 10 9). The second line contains n integers, a 1,0,a 2,0,...,a n,0(0 ≤ a i,0 < 2 31).

Output
For each case, output a n,m mod 10000007.

Sample Input

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

Sample Output

   
234 2799 72937
Hint


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

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

  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.   

转载于:https://my.oschina.net/pangzhuzhu/blog/318104

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值