标签:
1 #include <stdio.h> 2 #include <iostream> 3 #include <string.h> 4 #include <algorithm> 5 #include <math.h> 6 #define MAXX 100010 7 using namespace std; 8 9 struct matrix 10 { 11 int a[20][20]; 12 void init(int n) 13 { 14 for(int i = 0; i < n; i++) 15 for(int j = 0; j < n; j++) 16 { 17 a[i][j] = 0; 18 } 19 } 20 }s, w; 21 22 23 matrix mul(matrix x, matrix y, int n) 24 { 25 matrix c; 26 c.init(n); 27 int i , j; 28 for(i = 0; i < n; i++) 29 { 30 for(j = 0; j < n; j++) 31 { 32 for(int k = 0; k < n; k++) 33 { 34 c.a[i][j]+=x.a[i][k]*y.a[k][j]; 35 } 36 c.a[i][j] %= 1000; 37 } 38 } 39 return c; 40 } 41 42 matrix poww(matrix a, int k, int n) 43 { 44 matrix x; 45 x.init(n); 46 for(int i = 0; i < n; i++) 47 x.a[i][i] = 1; 48 while(k) 49 { 50 if(k & 1) 51 x = mul(x, a, n); 52 a = mul(a, a, n); 53 k >>= 1; 54 } 55 return x; 56 } 57 58 int main() 59 { 60 int n ,m, T; 61 int x, y; 62 int ss, ee, k; 63 while(~scanf("%d%d", &n, &m) && n+m) 64 { 65 s.init(n); 66 for(int i = 0; i < m; i++) 67 { 68 scanf("%d%d",&x, &y); 69 s.a[x][y] = 1; 70 } 71 72 scanf("%d", &T); 73 for(int i = 0; i < T; i++) 74 { 75 scanf("%d%d%d", &ss, &ee, &k); 76 w = poww(s, k, n); 77 printf("%d\n", w.a[ss][ee]); 78 } 79 } 80 81 }
HDU 2157 How many ways?? 关系矩阵+快速幂
标签:
原文地址:http://www.cnblogs.com/Yumesenya/p/5472323.html