hdoj 5015 233 Matrix

233 Matrix

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


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

很显然矩阵的第一列为:

0

a[1]

a[2]

a[3]

a[4]

我们转化一下,转化为

23

a[1]

a[2]

a[3]

a[4]

3

那么由第一列转移到第二列则为

23*10+3

a[1]+23*10+3

a[2]+a[1]+23*10+3

a[3]+a[2]+a[1]+23*10+3

a[4]+a[3]+a[2]+a[1]+23*10+3

3

很显然转移矩阵A就出来了:

10 0 0 0 0 1

10 1 0 0 0 1

10 1 1 0 0 1

10 1 1 1 0 1

10 1 1 1 1 1

 0  0 0 0 0 1

那么最后一列就是A的m次方*第一列。



#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
using namespace std;
typedef long long ll;
const int NI = 15;
const int mod = 10000007;
struct Mat {
    ll mat[NI][NI];
    Mat() {
        memset(mat, 0, sizeof(mat));
    }
};
int n, m;
Mat operator * (Mat a, Mat b) {
    Mat c;
    for(int i = 1; i <= n+2; i++) {
        for(int k = 1; k <= n+2; k++) {
            for(int j = 1; j <= n+2; j++) {
                c.mat[i][j] = (c.mat[i][j] + a.mat[i][k] * b.mat[k][j]) % mod;
            }
        }
    }
    return c;
}
Mat operator ^ (Mat a, int k) {
    Mat c;
    for(int i = 1; i <= n+2; ++i)
        for(int j = 1; j <= n+2; ++j)
            c.mat[i][j] = (i == j);    //初始化为单位矩阵

    while(k) {
        if(k&1) c = c * a;
        a = a * a;
        k >>= 1;
    }
    return c;
}/*
void print(Mat &A)
{
    cout<<"matrix"<<endl;
    for(int i=1;i<=n+2;i++)
    {
        for(int j=1;j<=n+2;j++)
        {
            cout<<A.mat[i][j]<<"\t";
        }
        cout<<endl;
    }
}*/
Mat a;
int main() {
    while(~scanf("%d%d",&n,&m)) {
        a.mat[1][1]=23;
        for(int i=1;i<=n;i++) {
            scanf("%I64d",&a.mat[i+1][1]);
        }
        a.mat[n+2][1]=3;
        Mat b; //b要在while循环内定义,保证其初始值全为0
        for(int i=1;i<=n+1;i++) b.mat[i][1]=10;
        for(int i=1;i<=n+2;i++) b.mat[i][n+2]=1;
        for(int i=2;i<=n+1;i++) {
            for(int j=2;j<=i;j++) b.mat[i][j]=1;
        }
        //print(b);
        b = b ^ m;
        a = b * a;
        print(a);
        printf("%I64d\n", a.mat[n+1][1]);
    }
    return 0;
}






猜你喜欢

转载自blog.csdn.net/zyf_2014/article/details/52793400
233
今日推荐