标签:std led void icp bit long rom init button
1 998 1007
11
360
#include "bits/stdc++.h" using namespace std; typedef long long ll; int n, mod, k; struct matrix { ll a[35][35]; }; struct mm { matrix a[5][5]; }; matrix zero, one, A; matrix add(matrix a, matrix b) { matrix c; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { c.a[i][j] = (a.a[i][j] + b.a[i][j]) % mod; } } return c; } matrix mul(matrix a, matrix b) { matrix c; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { c.a[i][j] = 0; for (int k = 0; k < n; k++) { c.a[i][j] = (c.a[i][j] + a.a[i][k] * b.a[k][j]) % mod; } } } return c; } mm smul(mm a, mm b) { mm c; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { c.a[i][j] = zero; for (int k = 0; k < 2; k++) { c.a[i][j] = add(c.a[i][j], mul(a.a[i][k], b.a[k][j])); } } } return c; } void init() { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { zero.a[i][j] = 0; one.a[i][j] = 0; } } //one.a[0][0] = 1; //one.a[1][1] = 1; for(int i=0;i<n;i++){ //cout<<i<<endl; one.a[i][i] = 1; } } mm powmod(mm a, int b) { mm ret; ret.a[0][0] = one; ret.a[1][1] = one; ret.a[0][1] = zero; ret.a[1][0] = zero; while (b) { if (b & 1) ret = smul(ret, a); a = smul(a, a); b >>= 1; } return ret; } int main() { //freopen("input.txt", "r", stdin); cin >> n >> k >> mod; init(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> A.a[i][j]; } } mm f, h; f.a[0][0] = A; f.a[1][0] = zero; f.a[0][1] = one; f.a[1][1] = one; f = powmod(f, k); h.a[0][0] = A; h.a[0][1] = zero; h = smul(h, f); matrix ans = h.a[0][1]; int flag = 0; if (k != 0) { for (int i = 0; i < n; i++) { flag = 0; for (int j = 0; j < n; j++) { if (flag) cout << " "; cout << (ans.a[i][j] + mod) % mod; flag = 1; } cout << endl; } } else { for (int i = 0; i < n; i++) { flag = 0; for (int j = 0; j < n; j++) { if (flag) cout << " "; cout << (one.a[i][j] + mod) % mod; flag = 1; } cout << endl; } } return 0; }
标签:std led void icp bit long rom init button
原文地址:https://www.cnblogs.com/albert-biu/p/10294214.html