1 条题解
-
0Guest LV 0 MOD
-
1
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <queue> using namespace std; long long int n, m; struct node { long long int g[5][5]; } f, res; void init(node &x) { //构造单位矩阵 for (int i = 1; i <= 3; i++) for (int j = 1; j <= 3; j++) if (i == j) x.g[i][j] = 1; else x.g[i][j] = 0; } void multiple(node &x, node &y, node &z) { //矩阵乘法运算 memset(z.g, 0, sizeof(z.g)); for (int i = 1; i <= 3; i++) for (int j = 1; j <= 3; j++) if (x.g[i][j]) { for (int k = 1; k <= 3; k++) { z.g[i][k] += x.g[i][j] * y.g[j][k]; if (z.g[i][k] >= m) z.g[i][k] %= m; } } } void quickpow(int k) { //快速幂 init(res); node temp = f, t; while (k) { if (k & 1) { multiple(res, temp, t); res = t; } multiple(temp, temp, t); temp = t; k >>= 1; } } long long int solve() { if (n <= 1) return 1; quickpow(n - 1); long long int ret = res.g[1][1] * 1 + res.g[1][2] * 1 + res.g[1][3] * 1; if (ret >= m) ret %= m; return ret; } int main() { scanf("%d%d", &n, &m); f.g[1][1] = 1; f.g[1][2] = 1; f.g[1][3] = 0; f.g[2][1] = 0; f.g[2][2] = 1; f.g[2][3] = 1; f.g[3][1] = 0; f.g[3][2] = 1; f.g[3][3] = 0; long long int ans = solve(); printf("%d\n", ans); return 0; }
- 1