标签:span line type space output NPU a* sizeof string
#include <algorithm> #include <iostream> #include <cmath> #include <cstring> #include <map> #include <string> #include <vector> #include <queue> #include <stack> #include <cstdio> #include <cstdlib> using namespace std; typedef long long ll; inline int read() { register int p(1),a(0);register char ch=getchar(); while((ch<‘0‘||ch>‘9‘)&&ch!=‘-‘) ch=getchar(); if(ch==‘-‘) p=-1,ch=getchar(); while(ch>=‘0‘&&ch<=‘9‘) a=a*10+ch-48,ch=getchar(); return a*p; } const int N=22,mo=1000; struct matrix { int data[N][N]; matrix(){memset(data,0,sizeof(data));} }; int u,v,n,m,k; matrix chen(matrix a,matrix b) { matrix c; for(int i=0;i<n;i++) for(int j=0;j<n;j++) if(a.data[i][j]) for(int k=0;k<n;k++) c.data[i][k]=(c.data[i][k]+a.data[i][j]*b.data[j][k])%mo; return c; } matrix qs(matrix a,int b) { matrix ans; for(int i=0;i<n;i++) ans.data[i][i]=1; while(b) { if(b&1) ans=chen(ans,a); a=chen(a,a); b>>=1; } return ans; } int main() { // freopen("input","r",stdin); // freopen("output","w",stdout); while(true) { n=read(),m=read(); if(n+m==0) return 0; matrix a; while(m--) { u=read(),v=read(); a.data[u][v]=1; } m=read(); while(m--) { u=read(),v=read(),k=read(); printf("%d\n",qs(a,k).data[u][v]); } } return 0; } /* */
标签:span line type space output NPU a* sizeof string
原文地址:https://www.cnblogs.com/cold-cold/p/10090527.html