0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 20:25:24 +00:00

1304. 佳佳的斐波那契

https://www.acwing.com/problem/content/submission/code_detail/7249810/
This commit is contained in:
Baoshuo Ren 2021-08-20 18:42:22 +08:00 committed by Baoshuo Ren
parent 5c4e71b2c0
commit e92d65cbe0
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

37
AcWing/1304/1304.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
void mul(int c[][4], int a[][4], int b[][4]) {
int t[4][4];
memset(t, 0x00, sizeof(t));
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
t[i][j] = (t[i][j] + 1ll * a[i][k] * b[k][j]) % m;
}
}
}
memcpy(c, t, sizeof(t));
}
int main() {
cin >> n >> m;
k = n - 1;
int f[4][4] = {1, 1, 1, 0};
int a[4][4] = {
{0, 1, 0, 0},
{1, 1, 1, 0},
{0, 0, 1, 1},
{0, 0, 0, 1},
};
while (k) {
if (k & 1) mul(f, f, a);
mul(a, a, a);
k >>= 1;
}
cout << ((1ll * n * f[0][2] - f[0][3]) % m + m) % m << endl;
return 0;
}