1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| #include <cstdio> using namespace std; const int N = 130, mod = 45989; template <class Type> void read(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; flag && (x = ~x + 1); } template <class Type> void write(Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar(x % 10 + '0'); } struct Matrix { int n, m, w[N][N]; Matrix() { } Matrix(int x, int y) { n = x, m = y; } Matrix(int x, int y, int k) { n = x, m = y; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) w[i][j] = i == j ? k : 0; } void output () { printf("%d %d\n", n, m); for (int i = 0; i < n; i++, puts("")) for (int j = 0; j < m; j++, putchar(' ')) write(w[i][j]); } friend Matrix operator*(Matrix x, Matrix y) { Matrix res(x.n, y.m, 0); for (int i = 0; i < res.n; i++) for (int j = 0; j < res.m; j++) for (int k = 0; k < x.m; k++) (res.w[i][j] += x.w[i][k] * y.w[k][j] % mod) %= mod; return res; } friend Matrix operator^(Matrix b, int k) { Matrix res(b.n, b.m, 1); while (k) { k & 1 && (res = res * b, 0); b = b * b; k >>= 1; } return res; } }; struct Edge { int u, v; } e[N]; int main () { int n, m, d, st, ed; read(n), read(m), read(d), read(st), read(ed); static Matrix mat(m << 1, m << 1); for (int i = 0, a, b; i < m; i++) { read(a), read(b); e[i << 1] = (Edge){a, b}; e[i << 1 | 1] = (Edge){b, a}; } for (int i = 0; i < m << 1; i++) for (int j = 0; j < m << 1; j++) if (i ^ j ^ 1 && e[i].v == e[j].u) mat.w[i][j] = 1; mat = mat ^ d - 1; int res = 0; for (int i = 0; i < m << 1; i++) for (int j = 0; j < m << 1; j++) e[i].u == st && e[j].v == ed && ((res += mat.w[i][j]) %= mod); write(res); return 0; }
|