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

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 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
 


构造矩阵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 
#include 
#include 
#include 
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;
}




你可能感兴趣的:(ACM-区域赛,ACM-矩阵)