编号 | 题目 | 状态 | 分数 | 总时间 | 内存 | 代码 / 答案文件 | 提交者 | 提交时间 |
---|---|---|---|---|---|---|---|---|
#241 | #1029. 寒域爷的兔子 | Accepted | 100 | 255 ms | 360 K | C++ / 1.7 K | JM233333 | 2019-06-22 17:46:34 |
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef unsigned long long ull;
const int MAX = 3;
class Matrix {
private:
ull data[MAX][MAX];
public:
int row;
int column;
static ull mod;
public:
Matrix(int r, int c) : row(r), column(c) { memset(data, 0, sizeof(data)); }
ull *operator[](const int x) { return data[x]; }
const ull *operator[](const int x) const { return data[x]; }
Matrix operator^(ull n) {
Matrix A(*this);
Matrix R(A.row, A.row);
for (int i = 0; i < A.row; i++) {
R[i][i] = 1;
}
while (n > 0) {
if (n % 2 == 1)
R = R * A;
A = A * A;
n /= 2;
}
return R;
}
friend Matrix operator*(const Matrix &A, const Matrix &B) {
Matrix C(A.row, B.column);
int cen = A.column; // or = B.row;
for (int i = 0; i < C.row; i++) {
for (int k = 0; k < cen; k++) {
for (int j = 0; j < C.column; j++) {
C[i][j] += ((A[i][k] % mod) * (B[k][j] % mod)) % mod;
C[i][j] %= mod;
}
}
}
return C;
}
};
ull Matrix::mod;
ull solve(ull n);
int main() {
// freopen("test.txt", "r", stdin);
ull M, K;
while (scanf("%llu %llu", &M, &K) != EOF) {
// 求解
Matrix::mod = K;
ull res = solve(M);
// 输出
printf("%llu\n", res);
}
return 0;
}
// 解决
ull solve(ull n) {
// 特判
if (n <= 2) {
return 2;
}
// 初始化
// A = [x, y; 1, 0];
Matrix A(2, 2);
A[0][0] = 1;
A[0][1] = 1;
A[1][0] = 1;
A[1][1] = 0;
// F = [f(2); f(1); 0];
Matrix F(2, 1);
F[0][0] = 2;
F[1][0] = 2;
// 矩阵快速幂
// f(n) = x*f(n-1) + y*f(n-2)
// [f(n); f(n-1)] = A * [f(n-1); f(n-2)] = ... = A^(n-2) * [f(2); f(1)]
Matrix X = A ^ (n - 2);
F = X * F;
// 返回
return F[0][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