hdu 5015 233 Matrix(西安网络赛 1009)

来源:互联网 发布:排油减肥产品知乎 编辑:程序博客网 时间:2024/06/04 01:31

233 Matrix

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


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
 


构造矩阵b:

b[0]=233

b[1]=a[1]

b[2]=a[2]

b[3]=a[3]

......

b[n+1]=3

例如 样例3

b[0]=233

b[1]=23

b[2]=47

b[3]=16

b[4]=3

递推矩阵A,样例3

10 0 0 0 1

1   1 0 0 0

1   1 1 0 0

1   1 1 1 0

0   0 0 0 1

n+2阶方阵

A^m*b的第n项就是结果

代码:

//1046ms#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int mod=10000007;struct matrix{    long long ma[13][13];}a;int n,m;long long b[13];matrix multi(matrix x,matrix y)//矩阵相乘{    matrix ans;    memset(ans.ma,0,sizeof(ans.ma));    for(int i=0;i<=n+1;i++)    {        for(int j=0;j<=n+1;j++)        {            for(int k=0;k<=n+1;k++)            {                ans.ma[i][j]=(ans.ma[i][j]+x.ma[i][k]*y.ma[k][j])%mod;            }        }    }    return ans;}int main(){    while(~scanf("%d%d",&n,&m))    {        memset(a.ma,0,sizeof(a.ma));        b[0]=233;        for(int i=1;i<=n;i++)        {            scanf("%I64d",&b[i]);        }        b[n+1]=3;        a.ma[0][0]=10;//构造a矩阵        a.ma[0][n+1]=1;        a.ma[n+1][n+1]=1;        for(int i=1;i<=n;i++)        {            for(int j=0;j<=i;j++)            {                a.ma[i][j]=1;            }        }        matrix ans;        memset(ans.ma,0,sizeof(ans.ma));        for(int i=0;i<=n+1;i++)//单位矩阵        {            for(int j=0;j<=n+1;j++)            {                if(i==j)                ans.ma[i][j]=1;            }        }        while(m)//矩阵快速幂        {            if(m&1)            {                ans=multi(ans,a);            }            a=multi(a,a);            m=(m>>1);        }        matrix mp;        memset(mp.ma,0,sizeof(mp.ma));        for(int i=0;i<=n+1;i++)//a的m次方与b矩阵相乘        {                for(int k=0;k<=n+1;k++)                {                    mp.ma[i][0]=(mp.ma[i][0]+ans.ma[i][k]*b[k])%mod;                }        }        printf("%I64d\n",mp.ma[n][0]);    }    return 0;}




0 0
原创粉丝点击