快速幂
举个例子:
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