标签:
题目链接:
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
/* 2157 218MS 1600K 1422 B G++ 2014300227*/ #include <bits/stdc++.h> using namespace std; const int N=1e4+5; typedef long long ll; const int mod=1e9+7; int n,m,t,u,v,number; struct matrix { ll a[22][22]; }; matrix A; matrix mul(matrix x,matrix y) { matrix ans; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { ans.a[i][j]=0; for(int k=0;k<n;k++) { ans.a[i][j]+=x.a[i][k]*y.a[k][j]; ans.a[i][j]%=1000; } } } return ans; } ll fast_mod(int num,int fx,int fy) { matrix ans,base; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(i!=j)ans.a[i][j]=0; else ans.a[i][j]=1; base.a[i][j]=A.a[i][j]; } } while(num) { if(num&1) { ans=mul(ans,base); } base=mul(base,base); num=(num>>1); } return ans.a[fx][fy]%1000; } int main() { while(1) { scanf("%d%d",&n,&m); if(n==0&&m==0)break; memset(A.a,0,sizeof(A.a)); for(int i=0;i<m;i++) { scanf("%d%d",&u,&v); A.a[u][v]=1; } scanf("%d",&t); for(int i=0;i<t;i++) { scanf("%d%d%d",&u,&v,&number); printf("%lld\n",fast_mod(number,u,v)); } } return 0; }
hdu-2157 How many ways??(矩阵快速幂)
标签:
原文地址:http://www.cnblogs.com/zhangchengc919/p/5402140.html