题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<cmath> 6 #include<algorithm> 7 #define inf 0x7fffffff 8 using namespace std; 9 typedef long long ll; 10 const int maxn=12; 11 const ll mod=20140518; 12 13 ll n,k; 14 struct matrix 15 { 16 ll an[maxn][maxn]; 17 }A,B; 18 matrix multiply(matrix x,matrix y) 19 { 20 matrix sum; 21 memset(sum.an,0,sizeof(sum.an)); 22 for (int i=1 ;i<=k ;i++) 23 { 24 for (int j=1 ;j<=k ;j++) 25 { 26 for (int k2=1 ;k2<=k ;k2++) { 27 sum.an[i][j]=(sum.an[i][j]+x.an[i][k2]*y.an[k2][j]%mod); 28 if (sum.an[i][j]>=mod) sum.an[i][j] -= mod; 29 } 30 } 31 } 32 return sum; 33 } 34 matrix power(ll K,matrix q) 35 { 36 matrix temp; 37 for (int i=1 ;i<=k ;i++) 38 { 39 for (int j=1 ;j<=k ;j++) 40 temp.an[i][j]= i==j ; 41 } 42 while (K) 43 { 44 if (K&1) temp=multiply(temp,q); 45 q=multiply(q,q); 46 K >>= 1; 47 } 48 return temp; 49 } 50 int main() 51 { 52 int t,ncase=1; 53 scanf("%d",&t); 54 while (t--) 55 { 56 scanf("%lld%lld",&n,&k); 57 if (n==1) 58 { 59 printf("Case #%d: %d\n",ncase++,k+1);continue; 60 } 61 matrix q; 62 for (int i=1 ;i<=k ;i++) 63 { 64 for (int j=1 ;j<=k ;j++) 65 { 66 if (j>=i) q.an[i][j]=(ll)1; 67 else if (j==i-1) q.an[i][j]=(ll)(k+2-i); 68 else q.an[i][j]=(ll)0; 69 } 70 } 71 q=power(n-1,q); 72 // for (int i=1 ;i<=k ;i++) 73 // { 74 // for (int j=1 ;j<=k ;j++) 75 // cout<<q.an[i][j]<<" "; 76 // cout<<endl; 77 // } 78 ll ans=0; 79 for (int i=1 ;i<=k ;i++) 80 { 81 ans=(ans+(ll)q.an[i][1]*(ll)(k+1))%mod; 82 } 83 printf("Case #%d: %lld\n",ncase++,ans); 84 } 85 return 0; 86 }