好题重温。
唯一的坑点是k可能小于0…
#include <cstdio>
#include <cstring>
#include <algorithm>
const int MAX = 101;
typedef long long ll;
struct mat {
ll a[MAX][MAX];
mat() {
memset(a, 0, sizeof(a));
}
mat(int x) {
memset(a, 0, sizeof(a));
for (int i = 0; i < MAX; ++i) {
a[i][i] = 1;
}
}
mat operator+(const mat& B)const {
mat res;
for (int i = 0; i < MAX; ++i) {
for (int j = 0; j < MAX; ++j) {
res.a[i][j] = a[i][j] + B.a[i][j];
}
}
return res;
}
mat operator*(const mat& B)const {
mat res;
for (int i = 0; i < MAX; ++i) {
for (int j = 0; j < MAX; ++j) {
if (a[i][j]) {
for (int k = 0; k < MAX; ++k) {
res.a[i][k] += a[i][j] * B.a[j][k];
}
}
}
}
return res;
}
} E(1);
mat fastpow(mat a, int m) {
mat res(1);
while (m) {
if (m & 1) res = res * a;
m >>= 1;
a = a * a;
}
return res;
}
int main() {
char op;
int c1, c2;
int n, m, k;
while (~scanf(" %d %d %d", &n, &m, &k)) {
if (n == 0 && m == 0 && k == 0) break;
mat cat, b(1);
cat.a[n][0] = 1;
while (k-- > 0) {
scanf(" %c %d", &op, &c1);
if (op == 'g') {
++b.a[c1-1][n];
} else if (op == 'e') {
memset(b.a[c1-1], 0, sizeof(b.a[c1-1]));
} else {
scanf(" %d", &c2);
std::swap(b.a[c1-1], b.a[c2-1]);
}
}
cat = fastpow(b, m) * cat;
printf("%lld", cat.a[0][0]);
for (int i = 1; i < n; ++i) {
printf(" %lld", cat.a[i][0]);
}
puts("");
}
return 0;
}