社区讨论
这题不能用矩阵快速幂吗?
P4461[CQOI2018] 九连环参与者 2已保存回复 5
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 5 条
- 当前快照
- 1 份
- 快照标识符
- @mhj100uw
- 此快照首次捕获于
- 2025/11/03 18:57 4 个月前
- 此快照最后确认于
- 2025/11/03 18:57 4 个月前
CPP
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define int unsigned long long
const int N = 4;
struct Matrix {
int a[N][N];
Matrix() {
memset(a, 0, sizeof a);
}
Matrix operator * (const Matrix &b) const {
Matrix res;
for (int i = 1; i <= N - 1; i++)
for (int j = 1; j <= N - 1; j++)
for (int k = 1; k <= N - 1; k++)
res.a[i][j] = (res.a[i][j] + a[i][k] * b.a[k][j]);
return res;
}
} ans, base;
void qpow(long long b) {
while (b) {
if (b & 1) ans = ans * base;
base = base * base;
b >>= 1;
}
}
signed main() {
long long n;
int uu;
cin>>uu;
while (uu--) {
cin>>n;
if (n == 1) {
cout << "1\n";
continue;
}
if (n == 2) {
cout << "2\n";
continue;
}
memset(base.a, 0, sizeof base.a);
base.a[1][1] = base.a[1][3] = base.a[3][2] = base.a[3][3] = 1;
base.a[2][3] = 2;
ans.a[1][1] = ans.a[1][2] = 1, ans.a[1][3] = 2;
qpow(n - 2);
cout << ans.a[1][3] << '\n';
}
return 0;
}
30 pts WA,记录,没用高精度。
换成 Python 3 就 50 pts RE 了,记录。
是矩阵快速幂这方法不对,还是代码写错?
求助!
回复
共 5 条回复,欢迎继续交流。
正在加载回复...