首页 > 代码库 > Hdu2276Kiki & Little Kiki 2矩阵
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;}
Hdu2276Kiki & Little Kiki 2矩阵
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。