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
| #include <cstdio> using namespace std; const int N = 110, mod = 2e3 + 9; 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 = 1; i <= n; i++) for (int j = 1; j <= m; j++) w[i][j] = i == j ? k : 0; } friend Matrix operator*(Matrix x, Matrix y) { Matrix res(x.n, y.m, 0); for (int i = 1; i <= res.n; i++) for (int j = 1; j <= res.m; j++) for (int k = 1; 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; } }; int num (int a, int b) { return (a - 1) * 9 + b; } int main () { int n, m; scanf("%d%d", &n, &m); static Matrix mat(n * 9, n * 9); for (int i = 1; i <= n; i++) for (int j = 1, a; j <= n; j++) { scanf("%1d", &a); a && (mat.w[num(i, 1)][num(j, a)]++); } for (int i = 1; i <= n ;i++) for (int j = 2; j <= 9; j++) mat.w[num(i, j)][num(i, j - 1)]++; printf("%d", (mat ^ m).w[num(1, 1)][num(n, 1)]); return 0; }
|