hdu5015 233 Matrix 西安网络赛I题 构造矩阵

233 Matrix

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


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

  一直都不太会矩阵的题,部长说一般都是先构造一列,然后乘个矩阵可以得到第二列,以此类推。。。找出这样的矩阵再用矩阵快速幂就行了。

  这题可以发现每个数等于前一列行小于等于这一行的所有数相加,再加上233333那个东西。

  设矩阵B,n+2行:

  a1

  a2

  a3

  ..

  233

  3

矩阵A,n+2行:

  1 0 0  ...1 0

  1 1 0 0..1 0

  1 1 1 0 .1 0 

  1 1 1 1. 1 0

  0 0 0 0..10 0

  0 0 0 0 ..0 1

最后答案是A^M * B,乘完之后第N行的数。

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MAXN 15
#define MAXM 2000010
#define MAXNODE 105
#define MOD 10000007
#define SIGMA_SIZE 4
typedef long long LL;
using namespace std;
LL N,M,B[MAXN];
struct Mat{
    LL mat[MAXN][MAXN];
    Mat(){
        memset(mat,0,sizeof(mat));
    }
}A;
Mat operator * (Mat a,Mat b){
    int i,j,k;
    Mat ret;
    for(int k=0;k<=N+1;k++)
        for(int i=0;i<=N+1;i++){
            if(!a.mat[i][k]) continue;
            for(int j=0;j<=N+1;j++) ret.mat[i][j]=(ret.mat[i][j]+a.mat[i][k]*b.mat[k][j])%MOD;
        }
    return ret;
}
Mat operator ^ (Mat a,int n){
    Mat ret,t=a;
    for(int i=0;i<=N+1;i++)
        for(int j=0;j<=N+1;j++) ret.mat[i][j]=(i==j);
    while(n){
        if(n&1) ret=ret*t;
        t=t*t;
        n>>=1;
    }
    return ret;
}
void get_A(){
    memset(A.mat,0,sizeof(A.mat));
    for(int i=0;i<N;i++)
        for(int j=0;j<=i;j++) A.mat[i][j]=1;
    for(int i=0;i<N;i++) A.mat[i][N]=1;
    A.mat[N][N]=10;
    A.mat[N][N+1]=A.mat[N+1][N+1]=1;
}
int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%I64d%I64d",&N,&M)!=EOF){
        for(int i=0;i<N;i++) scanf("%I64d",&B[i]);
        B[N]=233;
        B[N+1]=3;
        get_A();
        A=A^M;
        LL ans=0;
        for(int i=0;i<=N+1;i++) ans=(ans+A.mat[N-1][i]*B[i])%MOD;
        printf("%I64d\n",ans);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值