Hdu2276Kiki & Little Kiki 2矩阵
ai = (ai-1 + ai) %2 然后构造个 n*n的矩阵A, 时间K之后的状态就是 A^k * B(给出的字符串)
#include <cstdio> #include <algorithm> #include <iostream> #include <string.h> typedef long long LL; using namespace std; int n; struct Matrix { int m[104][105]; }; Matrix Mul(Matrix a, Matrix b) { Matrix ans; for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++){ ans.m[i][j] = 0; for (int k = 0; k < n; k++){ ans.m[i][j] += a.m[i][k] * b.m[k][j]; ans.m[i][j] %= 2; } } } return ans; } Matrix quick(Matrix a, int b) { Matrix ans; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) ans.m[i][j] = (i == j); while (b){ if (b & 1){ ans = Mul(ans, a); } a = Mul(a, a); b >>= 1; } return ans; } Matrix init() { Matrix ans; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) ans.m[i][j] = 0; ans.m[n - 1][0] = 1; for (int i = 0; i<n; i++) ans.m[i][i] = 1; for (int i = 0; i<n - 1; i++) ans.m[i][i + 1] = 1; return ans; } int main() { int k; char str[1000]; int a[1000]; while (cin >> k){ scanf("%s", str); n = strlen(str); Matrix ans = init(); Matrix t = quick(ans, k); for (int i = 0; i < n; i++) a[i] = str[i] - '0'; for (int j = 0; j<n; j++){ int sum = 0; for (int k = 0; k<n; k++){ sum += a[k] * t.m[k][j]; } sum %= 2; cout << sum; } cout << endl; } return 0; }