BZOJ 1875: [SDOI2009]HH去散步

刷水严重啊。

 

  1 // {HEAD
  2 #define FILE_IN_OUT
  3 #define debug
  4 #include <cstdio>
  5 #include <cstring>
  6 #include <cstdlib>
  7 #include <cmath>
  8 #include <ctime>
  9 #include <algorithm>
 10 #include <iostream>
 11 #include <fstream>
 12 #include <vector>
 13 #include <stack>
 14 #include <queue>
 15 #include <deque>
 16 #include <map>
 17 #include <set>
 18 #include <bitset>
 19 #include <complex>
 20 #include <string>
 21 #define REP(i, j) for (int i = 0; i < j; ++i)
 22 #define REPI(i, j, k) for (int i = j; i <= k; ++i)
 23 #define REPD(i, j) for (int i = j; 0 < i; --i)
 24 #define STLR(i, con) for (int i = 0, sz = con.size(); i < sz; ++i)
 25 #define STLRD(i, con) for (int i = con.size() - 1; 0 <= i; --i)
 26 #define CLR(s) memset(s, 0, sizeof s)
 27 #define SET(s, v) memset(s, v, sizeof s)
 28 #define pb push_back
 29 #define PL(k, n) for (int i = 1; i <= n; ++i) { cout << k[i] << ' '; } cout << endl
 30 #define PS(k) STLR(i, k) { cout << k[i] << ' '; } cout << endl
 31 using namespace std;
 32 #ifdef debug
 33 #ifndef ONLINE_JUDGE
 34     const int OUT_PUT_DEBUG_INFO = 1;
 35 #endif
 36 #endif
 37 #ifdef ONLINE_JUDGE
 38     const int OUT_PUT_DEBUG_INFO = 0;
 39 #endif
 40 #define DG if(OUT_PUT_DEBUG_INFO)
 41 void FILE_INIT(string FILE_NAME) {
 42 #ifdef FILE_IN_OUT
 43 #ifndef ONLINE_JUDGE 
 44     freopen((FILE_NAME + ".in").c_str(), "r", stdin);
 45     freopen((FILE_NAME + ".out").c_str(), "w", stdout);
 46  
 47 #endif
 48 #endif
 49 }
 50 typedef long long LL;
 51 typedef double DB;
 52 typedef pair<int, int> i_pair;
 53 const int INF = 0x3f3f3f3f;
 54 //}
 55  
 56  
 57 const int maxn = 121;
 58 int n, m, t, start, end;
 59  
 60 int mod = 45989;
 61 struct Matrix {
 62     int d[maxn][maxn];
 63     int row, col;
 64     Matrix() {
 65         memset(d, 0, sizeof d);
 66     }
 67     Matrix operator * (const Matrix &rhs) {
 68         Matrix ret;
 69         ret.row = row;
 70         ret.col = rhs.col;
 71         for(int i = 1; i <= row; ++i) {
 72             for(int j = 1; j <= rhs.col; ++j) {
 73                 for(int k = 1; k <= col; ++k) {
 74                     (ret.d[i][j] += d[i][k] * rhs.d[k][j]) %= mod;
 75                 }
 76             }
 77         }
 78         return ret;
 79     }
 80     void print() {
 81         for(int i = 1; i <= row; ++i) {
 82             for(int j = 1; j < col; ++j) {
 83                 printf("%d ", d[i][j]);
 84             }
 85             printf("%d\n", d[i][col]);
 86         }
 87     }
 88 };
 89  
 90 Matrix fast_pow(Matrix base, int index) {
 91     Matrix ret;
 92     ret.col = ret.row = base.col;
 93     for(int i = 1; i <= ret.col; ++i) {
 94         ret.d[i][i] = 1;
 95     }
 96     for(; index; index >>= 1, base = base * base) {
 97         if(index & 1) {
 98             ret = ret * base;
 99         }
100     }
101     return ret;
102 }
103  
104 int E[maxn][3], cnt;
105  
106 int main() {
107     FILE_INIT("BZOJ1875");
108  
109     cin >> n >> m >> t >> start >> end;
110     ++start, ++end;
111     REP(i, m) {
112         int a, b;
113         cin >> a >> b;
114         ++a, ++b;
115         E[++cnt][0] = a;
116         E[cnt][1] = b;
117         E[cnt][2] = i;
118         E[++cnt][0] = b;
119         E[cnt][1] = a;
120         E[cnt][2] = i;
121     }
122     Matrix M;
123     M.col = M.row = m * 2;
124     for(int i = 1; i <= cnt; ++i) {
125         for(int j = 1; j <= cnt; ++j) {
126             if(E[i][0] == E[j][1] && E[i][2] != E[j][2]) {
127                 M.d[j][i] = 1;
128             }
129         }
130     }
131     M = fast_pow(M, t - 1);
132     int ans = 0;
133     for(int i = 1; i <= cnt; ++i) {
134         for(int j = 1; j <= cnt; ++j) {
135             if(E[i][0] == start && E[j][1] == end) {
136                 ans = (ans + M.d[i][j]) % mod;
137             }
138         }
139     }
140     printf("%d\n", ans);
141      
142     return 0;
143 }
View Code

 

posted @ 2014-08-02 23:31  sbit  阅读(289)  评论(0编辑  收藏  举报