hdu 5015 233 Matrix(矩阵快速幂)

233 Matrix

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 884    Accepted Submission(s): 531


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
 


把第一行的233数列全部左移一位就可以推出一个递推矩阵来

比如输入的是

n m

a1 a2 a3 a4 、... 、an


可以构造成

B[0][0]=3

B[1][0]=233

B[2][0]=a1

B[3][0]=a2

B[4][0]=a3

...

B[n+1][0]=an


递推矩阵A:

1 0 0 0 0 ... 0 0

1 10 0 0 0 ... 0 0

0 1 1 0 0 ... 0 0

0 1 1 1 0 ... 0 0

0 1 1 1 1 ... 0 0

...

0 1 1 1 1 ... 1 0

0 1 1 1 1 ... 1 1


然后就是矩阵(A^m)*B第n项就是答案


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define LL long long
using namespace std;
const LL mod = 10000007;
const int maxn=15;
struct node
{
    LL mat[maxn][maxn];
    int r,c;
}A,B,C,D;
int n,m,a[15];

void initial()
{
     A.c=A.r=D.r=D.c=n+2;
     B.r=n+2,B.c=1;
     for(int i=0;i<n+2;i++)
        for(int j=0;j<n+2;j++)
        {
            A.mat[i][j]=0;
            if(i==j)  D.mat[i][j]=1;
            else  D.mat[i][j]=0;
        }
     A.mat[0][0]=1;
     A.mat[1][0]=1,A.mat[1][1]=10;
     for(int i=2;i<n+2;i++)
        for(int j=1;j<i+1;j++)
            A.mat[i][j]=1;
     B.mat[0][0]=3,B.mat[1][0]=233;
     for(int i=2;i<n+2;i++)  B.mat[i][0]=a[i-1];
}

node mul(node now,node tmp)
{
     node ans;
     ans.r=now.r,ans.c=tmp.c;
     for(int i=0;i<ans.r;i++)
        for(int j=0;j<ans.c;j++)
           ans.mat[i][j]=0;
     for(int i=0;i<now.r;i++)
     {
         for(int j=0;j<now.c;j++)
         {
               for(int k=0;k<tmp.r;k++)
               {
                    if(now.mat[i][k] && tmp.mat[k][j])
                          ans.mat[i][j]=(ans.mat[i][j]+now.mat[i][k]*tmp.mat[k][j]%mod)%mod;
               }
         }
     }
     return ans;
}

node pow_mod(node w,int coun)
{
    int p=coun;
    node ret=D,q=w;
    while(p)
    {
         if(p & 1)   ret=mul(ret,q);
         q=mul(q,q);
         p >>= 1;
    }
    return ret;
}

int main()
{
     while(scanf("%d %d",&n,&m)!=EOF)
     {
         for(int i=1;i<=n;i++)  scanf("%d",&a[i]);
         initial();
         C=pow_mod(A,m);
         node ans=mul(C,B);
         printf("%I64d\n",ans.mat[n+1][0]);
     }
     return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值