HDU 2276 Kiki & Little Kiki 2 矩阵
构造出类似这样的矩阵
1 0 0 1
1 1 0 0
0 1 1 0
0 0 1 1
通过对2取模就可以模拟出开关灯的状态了。然后直接快速幂
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <stack> #include <map> #include <set> #include <climits> #include <iostream> #include <string> using namespace std; #define MP make_pair #define PB push_back typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef pair<int, int> PII; typedef pair<double, double> PDD; const int INF = INT_MAX / 3; const double eps = 1e-8; const LL LINF = 1e17; const double DINF = 1e60; const int maxn = 105; struct Matrix { int data[maxn][maxn], n, m; Matrix(int n = 0, int m = 0): n(n), m(m) { memset(data, 0, sizeof(data)); } }; Matrix operator * (Matrix a, Matrix b) { Matrix ret(a.n, b.m); for(int i = 1; i <= a.n; i++) { for(int j = 1; j <= b.m; j++) { for(int k = 1; k <= a.m; k++) { ret.data[i][j] += a.data[i][k] * b.data[k][j]; ret.data[i][j] &= 1; } } } return ret; } Matrix pow(Matrix mat, int p) { Matrix ret(mat.n, mat.m); for(int i = 1; i <= mat.n; i++) ret.data[i][i] = 1; while(p) { if(p & 1) ret = ret * mat; mat = mat * mat; p >>= 1; } return ret; } char buf[128]; int n; int main() { while(scanf("%d%s", &n, buf + 1) != EOF) { int len = strlen(buf + 1); Matrix A0(len, 1), A(len, len); for(int i = 1; i <= len; i++) A0.data[i][1] = buf[i] - '0'; A.data[1][len] = A.data[1][1] = 1; for(int i = 2; i <= len; i++) A.data[i][i - 1] = A.data[i][i] = 1; A0 = pow(A, n) * A0; for(int i = 1; i <= len; i++) printf("%d", A0.data[i][1]); puts(""); } return 0; }