编号 | 题目 | 状态 | 分数 | 总时间 | 内存 | 代码 / 答案文件 | 提交者 | 提交时间 |
---|---|---|---|---|---|---|---|---|
#81628 | #1029. 寒域爷的兔子 | Accepted | 100 | 148 ms | 496 K | C++ / 1.7 K | Cmr | 2022-12-31 14:58:51 |
#include <cstdio>
#include <cstring>
#define LL __int128
using namespace std;
const int MAXN = 25;
LL M;
LL N;
struct Matrix {
LL Nums[MAXN][MAXN];
int N, M;
void ms() {
memset(Nums, 0, sizeof Nums);
N = M = 0;
}
} Speed, A;
inline Matrix Mul(Matrix A, Matrix B) {
Matrix res;
res.ms();
res.N = A.N, res.M = B.M;
for (int i = 1; i <= A.N; i++) {
for (int j = 1; j <= B.M; j++) {
for (int k = 1; k <= A.M; k++) {
res.Nums[i][j] = ((1ll * res.Nums[i][j]) + ((A.Nums[i][k] % M) * (B.Nums[k][j] % M)) % M) % M;
}
}
}
return res;
}
inline LL Read() {
LL res = 0, f = 0;
char sign = getchar();
for (; sign < '0' || sign > '9'; sign = getchar()) f |= (sign == '-');
for (; sign >= '0' && sign <= '9'; sign = getchar()) res = (res << 1) + (res << 3) + sign - '0';
return f ? -res : res;
}
inline void Write(LL x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
Write(x / 10);
putchar('0' + x % 10);
return;
}
LL qpow(LL times) {
Matrix al;
al.ms();
al.N = al.M = 2;
for (int i = 1; i <= al.N; i++) {
al.Nums[i][i] = 1;
}
// A = Mul(A , al);
// for(int i = 1 ; i <= 2 ; i++){
// for(int j = 1 ; j <= 2 ; j++){
// printf("%d " , A.Nums[i][j]);
// }
// }
// printf("%lld" , times);
while (times) {
if (times & 1) {
al = Mul(al, Speed);
}
times >>= 1;
Speed = Mul(Speed, Speed);
}
A = Mul(A, al);
return (A.Nums[1][1] + A.Nums[1][2]) % M;
}
int main() {
N = Read(), M = Read();
// Write(N);
Speed.ms(), A.ms();
Speed.N = 2, Speed.M = 2;
Speed.Nums[1][1] = 1, Speed.Nums[1][2] = 1, Speed.Nums[2][1] = 1, Speed.Nums[2][2] = 0;
A.N = 1, A.M = 2;
A.Nums[1][1] = 0, A.Nums[1][2] = 2;
Write(qpow(N - 1));
}
用户输出
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