HDU5015 233 Matrix(矩阵快速幂)

来源:互联网 发布:淘宝网充值 编辑:程序博客网 时间:2024/05/21 01:57

题目:

233 Matrix

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


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
 

Source
2014 ACM/ICPC Asia Regional Xi'an Online
 

Recommend
hujie   |   We have carefully selected several similar problems for you:  6022 6021 6020 6019 6018 
 

Statistic | Submit | Discuss | Note
思路:

这道题一看数据范围是10的九次方,那么普通方法肯定行不通,这时候我们用矩阵快速幂。

关于构造矩阵:http://blog.csdn.net/u011721440/article/details/39401515

代码:

#include <cstdio>#include <cstring>#include <cctype>#include <string>#include <set>#include <iostream>#include <stack>#include <cmath>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define mod 10000007#define N 3#define M 1000000+10#define ll long longusing namespace std;int n;struct Mat{    ll mat[12][12];    void init()    {        memset(mat,0,sizeof(mat));    }    void E()    {        init();        for(int i=0;i<n;i++) mat[i][i]=1;    }    void show()    {        printf("debug\n");        for(int i=0;i<=n+1;i++,puts(""))            for(int j=0;j<=n+1;j++)                printf("%lld ",mat[i][j]);        printf("Over\n");    }};Mat operator *(Mat a,Mat b){    Mat res;    res.init();    for(int i=0;i<=n+1;i++)        for(int j=0;j<=n+1;j++)            for(int k=0;k<=n+1;k++)                res.mat[i][j]+=a.mat[i][k]*b.mat[k][j],res.mat[i][j]%=mod;    return res;}Mat ksm(Mat a,int b){    Mat res;    res.E();    while(b>0)    {        if(b&1) res=a*res;        a=a*a;        b>>=1;    }    return res;}Mat getmat(){    Mat res;    res.init();    for(int i=0;i<n;i++)        for(int j=0;j<=i;j++) res.mat[i][j]=1;    for(int i=0;i<=n;i++) res.mat[n][i]=10;    for(int i=0;i<=n+1;i++) res.mat[n+1][i]=1;    return res;}int main(){    int m;    while(scanf("%d%d",&n,&m)!=EOF)    {        Mat ori;        ori.init();        ori.mat[0][n]=23;        ori.mat[0][n+1]=3;        for(int i=n-1;i>=0;i--) scanf("%lld",&ori.mat[0][i]);        Mat res=ori*ksm(getmat(),m);        printf("%lld\n",res.mat[0][0]);    }    return 0;}


0 0
原创粉丝点击