zoukankan      html  css  js  c++  java
  • 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
    ",ans.a[0][0]); 
    	return 0; 
    } 
    
    
  • 相关阅读:
    day 66
    day 66 作业
    day 65 作业
    day 55 Ajax
    day 56 forms组件
    day 59
    day 58 cookie与session 中间件
    day 54
    day 53
    day 52
  • 原文地址:https://www.cnblogs.com/sssy/p/9193818.html
Copyright © 2011-2022 走看看