标签:des style blog http color io os java ar
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1562 Accepted Submission(s): 546
1 //#define LOCAL 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 using namespace std; 6 int mat[21][21]; 7 int ans[21][21]; 8 int sav[21][21]; 9 int n,m,t; 10 void init(int ans[][21]) 11 { 12 for(int i=0;i<n;i++) 13 { 14 for(int j=0;j<n;j++) 15 { 16 if(i==j) ans[i][j]=1; 17 else ans[i][j]=0; 18 } 19 } 20 } 21 22 void Matrix(int a[][21],int b[][21]) 23 { 24 int cc[21][21]={0}; 25 for(int i=0;i<n;i++) 26 { 27 for(int j=0;j<n;j++) 28 { 29 for(int k=0;k<n;k++) 30 { 31 cc[i][j]=(cc[i][j]+a[i][k]*b[k][j])%1000; 32 } 33 } 34 } 35 for(int i=0;i<n;i++) 36 { 37 for(int j=0;j<n;j++) 38 { 39 a[i][j]=cc[i][j]; 40 } 41 } 42 } 43 44 void pow(int w) 45 { 46 while(w>0) 47 { 48 if(w&1) Matrix(ans,mat); 49 w>>=1; 50 if(w==0)break; 51 Matrix(mat,mat); 52 } 53 } 54 55 void input() 56 { 57 int a,b,k; 58 memset(sav,0,sizeof(sav)); 59 while(m--){ 60 scanf("%d%d",&a,&b); 61 sav[a][b]=1; 62 } 63 scanf("%d",&t); 64 while(t--){ 65 scanf("%d%d%d",&a,&b,&k); 66 for(int i=0;i<n;i++) 67 { 68 for(int j=0;j<n;j++) 69 mat[i][j]=sav[i][j]; 70 } 71 init(ans); 72 pow(k); 73 printf("%d\n",ans[a][b]); 74 } 75 } 76 77 int main() 78 { 79 #ifdef LOCAL 80 freopen("test.in","r",stdin); 81 #endif 82 while(~scanf("%d%d",&n,&m)&&n+m!=0) 83 input(); 84 return 0; 85 }
HDU----(2157)How many ways??(快速矩阵幂)
标签:des style blog http color io os java ar
原文地址:http://www.cnblogs.com/gongxijun/p/3986164.html