HDU - 5015 Matrix(矩阵快速幂)

233 Matrix

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


 

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

 

 

Source

2014 ACM/ICPC Asia Regional Xi'an Online

 

 

Recommend

hujie

 

 

 

这个题需要构建一个矩阵a:(大小是(n+2)*(n+2))(比如n=5)

10    0      0     0    0     0      1  

10    1      0     0    0     0      1

10    1      1     0    0     0      1

10    1      1     1    0     0      1

10    1      1     1    1     0      1

10    1      1     1    1     1      1

0      0      0     0    0     0      1

然后a的(m-1)次幂与矩阵b相乘得矩阵c;

矩阵b大小为((n+1)*1);(比如n=5)

233

233+b[1][0]

233+b[1][0]+b[2][0]

233+b[1][0]+b[2][0]+b[3][0]

233+b[1][0]+b[2][0]+b[3][0]+b[4][0]

233+b[1][0]+b[2][0]+b[3][0]+b[4][0]+b[5][0]

3

 

得到的结果矩阵的c[n][0]就是所求的值

 

 


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <list>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

const int N = 15;
//矩阵快速幂模板不变,只需要给N(矩阵大小),Mod(模大小),msize,nsize,(矩阵的行列大小)赋
//值,就可以用了
int  Mod = 1e7+7;
int n,m,nsize,msize;

struct Mat{
	ll mat[N][N];
};

Mat operator *(Mat a,Mat b)
{
	Mat c;
	memset(c.mat,0,sizeof(c.mat));
	int k,i,j;
	for(k = 0;k < msize;k++)
		for(i = 0;i < msize;i++)
			if(a.mat[i][k])
				for(j = 0;j < nsize;j++)
					if(b.mat[k][j])
						c.mat[i][j] = (c.mat[i][j] + a.mat[i][k]*b.mat[k][j]) % Mod;
	return c;
}

Mat operator ^ (Mat a,ll k)
{
	Mat c;
	memset(c.mat,0,sizeof(c.mat));
	for(int i = 0;i < msize;i++)
		c.mat[i][i] = 1;
	for(;k;k >>= 1){
		if(k & 1)  c = c*a;
		a = a*a;
	}
	return c;
}

int main()
{
    while(cin >> n >> m){
        msize = nsize = n+2;
        Mat a;
        memset(a.mat,0,sizeof(a.mat));
        int i,j;
        for(i = 0;i <= n;i++)
            for(j = 0;j <= n+1;j++){
                a.mat[i][0] = 10;
                a.mat[i][n+1] = 1;
                if(i>=j) a.mat[i][j] = 1;
            }
        a.mat[n+1][n+1] = 1;
        Mat b = a^(m-1);
        Mat c;
        c.mat[0][0] = 233;
        for(i = 1;i <= n;i++){
            cin >> c.mat[i][0];
            c.mat[i][0] += c.mat[i-1][0];
        }
        c.mat[n+1][0] = 3;
        msize = n+2,nsize=1;
        Mat d = b*c;
        cout << d.mat[n][0]%Mod << endl;
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值