1.快速幂
#include<iostream> using namespace std; int qpow(int x, int y) { int res = 1; while (y) { if (y & 1)res *= x; x = x * x; y >>= 1; } return res; } signed main() { int num,n; cin >> num>>n; int ans = qpow(num, n); cout << ans << endl; }
当指数n较大时,使用快速幂可以大大降低循环次数,对qpow()函数的实现,我的理解是,任何一个整数都可以写成二进制形式,如10可写成1010,自然7^10=7^8*7^2,即当指数的二进制表示下的位数为1时,进行乘积,以为是依据二进制完成的操作,因此底数要每次平方来呼应二进制表示的位数
2.矩阵快速幂
即为矩阵乘法+快速幂
矩阵乘法的实现
note mul(note a, note b)//note为结构体类型 { note res; memset(res.m, 0, sizeof (res.m)); for(int i=1;i<=2;i++) for(int j=1;j<=2;j++) for (int k = 1; k <= 2; k++) { res.m[i][j] += (a.m[i][k] * b.m[k][j])%N; } return res; }
例题
经典斐波那契
#include<iostream> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; const int N = 10000; struct note { ll m[3][3]; }; note mul(note a, note b) { note res; memset(res.m, 0, sizeof (res.m)); for(int i=1;i<=2;i++) for(int j=1;j<=2;j++) for (int k = 1; k <= 2; k++) { res.m[i][j] += (a.m[i][k] * b.m[k][j])%N; } return res; } note ksm(note a, ll b) { note ans; memset(ans.m, 0, sizeof(ans.m)); for (int i = 1; i <= 2; i++)ans.m[i][i] = 1; while (b) { if (b & 1)ans = mul(ans, a); a = mul(a, a); b >>= 1; } return ans; } signed main() { ll n; while (scanf("%lld", &n)!=EOF&&n!=-1) { note a, b; a.m[1][1] = 1; a.m[1][2] = 1; a.m[2][1] = 1; a.m[2][2] = 0; b = ksm(a, n); cout << (b.m[2][1]+N)%N << endl; } }
#include<iostream> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; const int N = 9973; ll n,k; struct note { ll m[11][11]; }; note mul(note a, note b) { note res; memset(res.m, 0, sizeof(res.m)); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) { res.m[i][j] += (a.m[i][k] * b.m[k][j]) % N; } return res; } note ksm(note a, ll b) { note ans; memset(ans.m, 0, sizeof(ans.m)); ans = a; b--; while (b) { if (b & 1)ans = mul(ans, a); a = mul(a, a); b >>= 1; } return ans; } signed main() { ll t; cin >> t; while (t--) { scanf("%lld%lld", &n,&k); note a, b; for(int i=1;i<=n;i++) for (int j = 1; j <= n; j++) { scanf("%lld", &a.m[i][j]); } b = ksm(a, k); ll ans = 0; for(int i=1;i<=n;i++) { //cout << b.m[i][i] << " "; ans = (ans + b.m[i][i] + N) % N; //cout << ans << " "; } cout << ans << endl; } return 0; }
#include<iostream> #include<algorithm> #include<cstring> using namespace std; typedef long long ll; struct note { ll m[11][11]; }; note mul(note a, note b,ll N) { note res; memset(res.m, 0, sizeof(res)); for (int i = 1; i <= 10; i++) for (int j = 1; j <= 10; j++) for (int k = 1; k <= 10; k++) res.m[i][j] +=( a.m[i][k] * b.m[k][j])%N; return res; } note ksm(note a, ll b,ll N) { note ans; memset(ans.m, 0, sizeof(ans.m)); for (int i = 1; i <= 10; i++)ans.m[1][i] = 10 - i; while (b) { if (b & 1)ans = mul(ans, a,N); a = mul(a, a, N); b >>= 1; } return ans; } signed main() { ll n, k; while (cin >> n >> k) { note a, b; memset(a.m, 0, sizeof(a.m)); for (int i = 1; i <= 10; i++) { cin >> a.m[i][1]; if (i !=10 )a.m[i][i +1 ] = 1; } b = ksm(a, n - 9,k); cout <<b.m[1][1] % k << endl; } return 0; }
#include<iostream> using namespace std; typedef long long ll; ll ans; ll modmul(int a, int n) { ll res = 1; for (int i = 1; i <= n; i++) { res = (res * a) % 10000000000; } return res; } signed main() { int t; cin >> t; while (t--) { int n; cin >> n; for (int i = 1; i <= n; i++) { ans = (ans + modmul(i,i)+ 10000000000) % 10000000000; } cout << ans << endl; ans = 0; } return 0; }