'commit'
This commit is contained in:
48
TangDou/AcWing_TiGao/T5/JuZhengChengFa/YBT_1642.cpp
Normal file
48
TangDou/AcWing_TiGao/T5/JuZhengChengFa/YBT_1642.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
const int INF = 0x3f3f3f3f;
|
||||
#define int long long
|
||||
#define endl "\n"
|
||||
/*
|
||||
http://ybt.ssoier.cn:8088/problem_show.php?pid=1642
|
||||
测试用例:
|
||||
5 1000
|
||||
|
||||
答案:
|
||||
5
|
||||
*/
|
||||
const int N = 2;
|
||||
|
||||
int n, mod;
|
||||
|
||||
// 矩阵乘法
|
||||
void mul(int a[][N], int b[][N], int c[][N]) {
|
||||
int t[N][N] = {0};
|
||||
for (int i = 0; i < N; i++) {
|
||||
for (int j = 0; j < N; j++)
|
||||
for (int k = 0; k < N; k++)
|
||||
t[i][j] = (t[i][j] + (a[i][k] * b[k][j]) % mod) % mod;
|
||||
}
|
||||
memcpy(c, t, sizeof t);
|
||||
}
|
||||
|
||||
signed main() {
|
||||
cin >> n >> mod;
|
||||
|
||||
int b[N][N] = {1, 1}; // 结果矩阵(初始化fib[2]=1,fib[1]=1)
|
||||
|
||||
// 构造的向量数组
|
||||
int m[N][N] = {
|
||||
{1, 1},
|
||||
{1, 0}};
|
||||
|
||||
// 矩阵快速幂
|
||||
for (int i = n - 1; i; i >>= 1) {
|
||||
if (i & 1) mul(b, m, b);
|
||||
mul(m, m, m);
|
||||
}
|
||||
|
||||
printf("%lld\n", b[0][1]);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user