vijos Warcraft III 守望者的烦恼

题解

转移方程好写吧
一个一维递推式
然后我们可以构造矩阵优化
嗯,最近学一下递推优化

代码

#include<cstdio>
#include<cstring> 
#include<algorithm> 
#define mod 7777777 
int K,n; 
#define LL long long 
const int maxn = 11; 
struct matrix {
    int a[maxn][maxn];int c,r; 
    void in(int x,int y) { 
        r = x,c = y; 
    } 
    matrix() { 
        c = 0,r = 0; 
        memset(a,0,sizeof a); n = 0 ;
    }   
} ans,tmp; 
matrix operator * (const matrix A,const matrix B) { 
    matrix ret; ret.in(A.r,B.c); 
    for(int i = 0; i < ret.r;++ i) 
        for(int j = 0;j < ret.c;++ j)  
            for(int k = 0;k < A.c;++ k) 
                ret.a[i][j] = (ret.a[i][j] + ((long long )A.a[i][k] * B.a[k][j] % mod) )% mod; 
    return ret; 
} 
int dp[maxn]; 
matrix pow(matrix a,int k) {
    matrix ret; 
    ret.in(K,K); 
    for(int i = 0;i < K;++ i) ret.a[i][i] = 1;
    for(;k;k >>= 1,a = a * a)  
        if(k & 1) ret = ret * a; 
    return ret; 
} 
int main() { 
    scanf("%d%d",&K,&n); 
    tmp.in(K,K); 
    for(int i = 0;i < K;++ i)  tmp.a[i][0] = 1; 
    for(int i = 1;i < K;++ i)  tmp.a[i - 1][i] = 1; 
    tmp = pow(tmp,n); 
    ans.in(1,K); 
    ans.a[0][0] = 1;
    ans = ans * tmp; 
    printf("%lld\n",ans.a[0][0]); 
    return 0; 
} 

猜你喜欢

转载自www.cnblogs.com/sssy/p/9193818.html
今日推荐