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






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值