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
hdu5015 233 Matrix 西安网络赛I题 构造矩阵_第1张图片

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

  这题可以发现每个数等于前一列行小于等于这一行的所有数相加,再加上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;
}


你可能感兴趣的:(hdu5015 233 Matrix 西安网络赛I题 构造矩阵)