快速幂
举个例子:
2 ^ 10 = 2 ^ 5 * 2 ^ 5 = (2 ^ 2 * 2 ^ 2 * 2) * (2 ^ 2 * 2 ^ 2 * 2) = …
代码奉上
#include<bits/stdc++.h>
#define LL long long
#define PP pair<int, int>
using namespace std;
LL a, b, mod, ans = 1;
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> a >> b >> mod;
if (a == 0) {
cout << 0 << endl;
return 0;
}
cout << a << '^' << b << " mod " << mod << '=';
while (b) {
if (b % 2 == 1) ans = ans * a % mod;
a = a * a % mod;
b /= 2;
}
cout << ans << endl;
return 0;
}
矩阵快速幂
矩阵快速幂公式:

代码奉上
#include<bits/stdc++.h>
#define LL long long
#define PP pair<int, int>
using namespace std;
const LL N = 1e2 + 10;
const LL mod = 1e9 + 7;
LL a[N][N];
LL ans[N][N];
LL b[N][N];
LL n, k;
void work (LL u[N][N], LL v[N][N]) {
memset (b, 0, sizeof

本文介绍了C++中实现快速幂、矩阵快速幂的方法,并提供了相关例题,如斐波那契数列的矩阵快速幂解法。此外,还讲解了ST表的概念,并给出USACO07JAN Balanced Lineup G和LCA模版的例题应用。
最低0.47元/天 解锁文章
1654

被折叠的 条评论
为什么被折叠?



