http://www.bnuoj.com/bnuoj/problem_show.php?pid=16493
【题解】:矩阵快速幂
【code】:
1 #include <cstdlib> 2 #include <cstring> 3 #include <cstdio> 4 #include <iostream> 5 using namespace std; 6 7 int N; 8 struct matrix 9 { 10 double a[100][100]; 11 }origin,res; 12 13 14 matrix multiply(matrix x,matrix y) 15 { 16 matrix temp; 17 for(int i=1;i<=N;i++) 18 { 19 for(int j=1;j<=N;j++) 20 { 21 double ans=0; 22 for(int k=1;k<=N;k++) 23 { 24 ans+=x.a[i][k]*y.a[k][j]; 25 } 26 temp.a[i][j]=ans; 27 } 28 } 29 30 return temp; 31 } 32 33 matrix calc(int n) 34 { 35 while(n) 36 { 37 if(n%2==1) 38 res=multiply(origin,res); 39 origin=multiply(origin,origin); 40 n/=2; 41 } 42 return res; 43 } 44 45 int main() 46 { 47 int t; 48 scanf("%d",&t); 49 while(t--) 50 { 51 int n; 52 scanf("%d",&n); 53 N=n; 54 double x[100]; 55 int i,j; 56 for(i=1;i<=n;i++) 57 { 58 scanf("%lf",&x[i]); 59 } 60 memset(origin.a,0,sizeof(origin.a)); 61 for(i=1;i<=n;i++) 62 { 63 int k; 64 scanf("%d",&k); 65 if(k==0) origin.a[i][i]=1.0; 66 for(j=1;j<=k;j++) 67 { 68 int p; 69 scanf("%d",&p); 70 origin.a[p][i]=1.0/k; 71 } 72 } 73 memset(res.a,0,sizeof(res.a)); 74 for(i=1;i<=n;i++) 75 { 76 res.a[i][i]=1; 77 } 78 int m; 79 scanf("%d",&m); 80 calc(m); 81 for(i=1;i<=n;i++) 82 { 83 double ans=0; 84 for(j=1;j<=n;j++) 85 { 86 ans+=res.a[i][j]*x[j]; 87 } 88 if(i==1) 89 { 90 printf("%.2lf",ans); 91 } 92 else 93 { 94 printf(" %.2lf",ans); 95 } 96 } 97 putchar(10); 98 99 } 100 return 0; 101 }