编号 | 题目 | 状态 | 分数 | 总时间 | 内存 | 代码 / 答案文件 | 提交者 | 提交时间 |
---|---|---|---|---|---|---|---|---|
#16785 | #1029. 寒域爷的兔子 | Accepted | 100 | 339 ms | 348 K | C++ / 986 B | zz_ylolita | 2019-07-11 2:19:08 |
//矩阵快速幂
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef long long LL;
LL P;
unsigned long long m;
struct Matrix {
LL a[2][2];
void init() {
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++) a[i][j] = 0;
}
} a, e, ans;
Matrix mul(Matrix a, Matrix b) {
Matrix c;
c.init();
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++) c.a[i][j] = (c.a[i][j] + a.a[i][k] * b.a[k][j] % P) % P;
return c;
}
Matrix qpow(Matrix a, unsigned long long b) {
Matrix res = e;
while (b) {
if (b & 1)
res = mul(res, a);
a = mul(a, a);
b >>= 1;
}
return res;
}
int main() {
scanf("%llu%lld", &m, &P);
if (m == 1) {
printf("%lld\n", 2 % P);
return 0;
}
a.a[0][0] = a.a[0][1] = a.a[1][0] = 1;
a.a[1][1] = 0;
e.a[0][0] = e.a[1][1] = 1;
ans = qpow(a, m - 2);
printf("%lld\n", (ans.a[0][0] * 2 % P + ans.a[0][1] * 2 % P) % P);
return 0;
}
用户输出
5770
系统信息
Exited with return code 0
用户输出
27662
系统信息
Exited with return code 0
用户输出
8791
系统信息
Exited with return code 0
用户输出
6732
系统信息
Exited with return code 0
用户输出
1454
系统信息
Exited with return code 0
用户输出
3790
系统信息
Exited with return code 0
用户输出
11690
系统信息
Exited with return code 0
用户输出
2151
系统信息
Exited with return code 0
用户输出
1617
系统信息
Exited with return code 0
用户输出
10509
系统信息
Exited with return code 0
用户输出
4380
系统信息
Exited with return code 0
用户输出
2560
系统信息
Exited with return code 0
用户输出
1938
系统信息
Exited with return code 0
用户输出
10705
系统信息
Exited with return code 0
用户输出
9350
系统信息
Exited with return code 0
用户输出
8076
系统信息
Exited with return code 0
用户输出
7210
系统信息
Exited with return code 0
用户输出
666
系统信息
Exited with return code 0
用户输出
16089
系统信息
Exited with return code 0
用户输出
5065
系统信息
Exited with return code 0
用户输出
9362
系统信息
Exited with return code 0
用户输出
9370
系统信息
Exited with return code 0
用户输出
4198
系统信息
Exited with return code 0
用户输出
4041
系统信息
Exited with return code 0
用户输出
4361
系统信息
Exited with return code 0
用户输出
11702
系统信息
Exited with return code 0
用户输出
13980
系统信息
Exited with return code 0
用户输出
1442
系统信息
Exited with return code 0
用户输出
24628
系统信息
Exited with return code 0
用户输出
6954
系统信息
Exited with return code 0
用户输出
5562
系统信息
Exited with return code 0