编号 | 题目 | 状态 | 分数 | 总时间 | 内存 | 代码 / 答案文件 | 提交者 | 提交时间 |
---|---|---|---|---|---|---|---|---|
#1855 | #1029. 寒域爷的兔子 | Accepted | 100 | 250 ms | 388 K | C++ 11 / 1.3 K | 丁丁跑卡车 | 2019-06-25 15:33:49 |
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define MAX_INF 0x3f
#define MAX_INF_VAL 0x3f3f3f3f
#define pi 3.141592653589
#define eps 1e-10
//#define p 2173412051LL
#define sz 2
using namespace std;
ull p;
struct matrix {
ull a[sz][sz];
void init() {
memset(a, 0, sizeof(a));
for (int i = 0; i < sz; ++i) a[i][i] = 1;
}
void clear() { memset(a, 0, sizeof(a)); }
};
matrix matrix_pow(matrix, ull);
matrix matrix_mul(matrix, matrix);
int main() {
ull n;
scanf("%llu%llu", &n, &p);
matrix mt;
mt.clear();
mt.a[0][0] = mt.a[0][1] = mt.a[1][0] = 1;
if (n == 1) {
printf("%llu", 2 % p);
return 0;
}
mt = matrix_pow(mt, n - 2);
printf("%llu", 2 * (mt.a[0][0] + mt.a[0][1]) % p);
return 0;
}
matrix matrix_pow(matrix x, ull n) {
matrix tmp;
tmp.init();
while (n) {
if (n & 1)
tmp = matrix_mul(tmp, x);
x = matrix_mul(x, x);
n >>= 1;
}
return tmp;
}
matrix matrix_mul(matrix a, matrix b) {
matrix ans;
ans.clear();
for (int i = 0; i < sz; ++i)
for (int j = 0; j < sz; ++j) {
for (int k = 0; k < sz; ++k) {
ans.a[i][j] += a.a[i][k] * b.a[k][j];
ans.a[i][j] %= p;
}
}
return ans;
}
用户输出
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