HDU 5015 233Matrix(矩阵快速幂+递推)

来源:互联网 发布:mysql golang 编辑:程序博客网 时间:2024/05/22 05:19

233 Matrix

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

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

  • 错误总结,忘记开long long,,,,,
  • 做法:对于这些在矩阵中的项是前面某些项的和的情况,就可以考虑构造幂矩阵来做了,可以先写出前几项来找规律,上一列对应元素的系数就是构造矩阵的行,对于上一列中没有的因子,我们可以在构造矩阵中添列,在原向量中添行。
#include <iostream>#include <cmath>#include <cstring>#include <cstdio>#include <algorithm>#define maxn 1000#define kuma 20#define mod 10000007typedef long long ll;ll n,k,m;using namespace std;struct matrix{    int a[kuma][kuma];    void init()    {        memset(a,0,sizeof(a));        for(int i=0;i<=kuma;i++)        {            a[i][i]=1;        }    }};matrix E;matrix mul(matrix x,matrix y){    matrix ans;    for(int i=0;i<=n+1;i++)    {        for(int j=0;j<=n+1;j++)        {            ans.a[i][j]=0;            for(int k=0;k<=n+1;k++)            {                ans.a[i][j]=(ans.a[i][j]+(x.a[i][k]*y.a[k][j])%mod)%mod;            }        }    }    return ans;}matrix add(matrix x,matrix y){    matrix ans;    for(int i=0;i<=n+1;i++)    {        for(int j=0;j<=n+1;j++)        {            ans.a[i][j]=(x.a[i][j]+y.a[i][j])%mod;        }    }    return ans;}matrix quickpow(matrix a,ll p){    matrix ans;    ans.init();    matrix t;    t=a;    while(p)    {        if(p&1)ans=mul(ans,t);        p>>=1;        t=mul(t,t);    }    return ans;}int main(){    matrix nico;    while(scanf("%I64d%I64d",&n,&m)!=EOF)    {        nico.init();        for(int i=1;i<=n;i++)        {            scanf("%I64d",&nico.a[i][0]);            nico.a[i][0]=nico.a[i][0]%mod;        }        nico.a[0][0]=23;        nico.a[n+1][0]=3;        matrix tmp;        tmp.init();        for(int i=1;i<=n;i++)        {            tmp.a[i][0]=10;            tmp.a[i][n+1]=1;            for(int j=1;j<=n;j++)            {                if(i>=j)tmp.a[i][j]=1;            }        }        tmp.a[0][0]=10;        tmp.a[0][n+1]=1;//        for(int i=0;i<=n+1;i++)//            printf("%d\n",nico.a[i][0]);//        for(int i=0;i<=n+1;i++)//        {//            for(int j=0;j<=n+1;j++)//                printf("%d ",tmp.a[i][j]);//            printf("\n");//        }        matrix ans=quickpow(tmp,m);        ans=mul(ans,nico);//        for(int i=0;i<=n+1;i++)//        {//            for(int j=0;j<=n+1;j++)//                printf("%d ",ans.a[i][j]);//            printf("\n");//        }        printf("%I64d\n",ans.a[n][0]);    }}
原创粉丝点击