代码:
#include <bits/stdc++.h>
#define N 110
#define MOD 2009
using namespace std;
typedef long long ll;
int g, n;
struct Matrix {
ll mat[N][N];
Matrix() {
memset(mat, 0, sizeof mat);
}
void I() {
for (int i = 1; i <= n; i++) mat[i][i] = 1;
}
} M;
Matrix operator *(const Matrix &l, const Matrix &r) {
Matrix res;
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
res.mat[i][j] += l.mat[i][k] * r.mat[k][j];
res.mat[i][j] %= MOD;
}
}
}
return res;
}
Matrix qpow(Matrix a, int b) {
Matrix res;
res.I();
while (b) {
if (b & 1) res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
int num(int x, int y) {
return x + y * g;
}
int main() {
int a;
cin>>g>>a;
n = 9 * g;
string s;
int t;
for (int i = 1; i <= g; i++) {
for (int j = 1; j <= 8; j++) {
M.mat[num(i, j)][num(i, j - 1)] = 1;
}
// cin>>s;
// for (int j = 0; j < g; j++) {
// int d = s[j] - '0';
// //cout<<d<<' ';
// if (d) M.mat[i][num(j, d - 1)] = 1;
// }
for (int j = 1; j <= g; j++) {
scanf("%1d", &t);
if (t) M.mat[i][num(j, t - 1)] = 1;
}
}
M = qpow(M, a);
cout<<M.mat[1][g] % MOD<<endl;
return 0;
}
我一开始想要用字符串读入然后一个一个分离数字的方法来处理输入,但是按照这种读入方式计算出来的结果一直有误……后面借鉴了一下第一篇题解的scanf读入方式才能AC。但是我看了半天没发现这两种读入方式的差别……有没有大佬能帮我解释一下为什么按字符串方式读会出错……