快速冪總結
快速冪這個東西比較好理解,但實現起來到不老好辦,記了幾次老是忘,今天把它系統的總結一下防止忘記。
首先,快速冪的目的就是做到快速求冪,假設我們要求a^b,按照樸素算法就是把a連乘b次,這樣一來時間復雜度是O(b)也即是O(n)級別,快速冪能做到O(logn),快了好多好多。它的原理如下:
假設我們要求a^b,那么其實b是可以拆成二進制的,該二進制數第i位的權為2^(i-1),例如當b==11時
1 int poww(int a,int b){//求a的b次方2 int ans=1,base=a;3 while(b!=0){4 if(b&1!=0)5 ans*=base;6 base*=base;7 b>>=1;8 }9 return ans; 10 }
代碼很短,死記也可行,但最好還是理解一下吧,其實也很好理解,以b==11為例,b=>1011,二進制從右向左算,但乘出來的順序是?a^(2^0)*a^(2^1)*a^(2^3),是從左向右的。我們不斷的讓base*=base目的即是累乘,以便隨時對ans做出貢獻。
其中要理解base*=base這一步:因為 base*base==base2,下一步再乘,就是base2*base2==base4,然后同理 ?base4*base4=base8,由此可以做到base-->base2-->base4-->base8-->base16-->base32.......指數正是?2^i ,再看上面的例子,a11= a1*a2*a8,這三項就可以完美解決了,快速冪就是這樣。
順便啰嗦一句,由于指數函數是爆炸增長的函數,所以很有可能會爆掉int的范圍,根據題意選擇 long long還是mod某個數自己看著辦。
矩陣快速冪也是這個道理,下面放一個求斐波那契數列的矩陣快速冪模板
1 #include<iostream>2 #include<cstdio>3 #include<cstring>4 #include<cmath>5 #include<algorithm>6 using namespace std;7 const int mod = 10000;8 const int maxn = 35;9 int N; 10 struct Matrix { 11 int mat[maxn][maxn]; 12 int x, y; 13 Matrix() { 14 memset(mat, 0, sizeof(mat)); 15 for (int i = 1; i <= maxn - 5; i++) mat[i][i] = 1; 16 } 17 }; 18 inline void mat_mul(Matrix a, Matrix b, Matrix &c) { 19 memset(c.mat, 0, sizeof(c.mat)); 20 c.x = a.x; c.y = b.y; 21 for (int i = 1; i <= c.x; i++) { 22 for (int j = 1; j <= c.y; j++) { 23 for (int k = 1; k <= a.y; k++) { 24 c.mat[i][j] += (a.mat[i][k] * b.mat[k][j]) % mod; 25 c.mat[i][j] %= mod; 26 } 27 } 28 } 29 return; 30 } 31 inline void mat_pow(Matrix &a, int z) { 32 Matrix ans, base = a; 33 ans.x = a.x; ans.y = a.y; 34 while (z) { 35 if (z & 1 == 1) mat_mul(ans, base, ans); 36 mat_mul(base, base, base); 37 z >>= 1; 38 } 39 a = ans; 40 } 41 int main() { 42 while (cin >> N) { 43 switch (N){ 44 case -1: return 0; 45 case 0: cout << "0" << endl; continue; 46 case 1: cout << "1" << endl; continue; 47 case 2: cout << "1" << endl; continue; 48 } 49 Matrix A, B; 50 A.x = 2; A.y = 2; 51 A.mat[1][1] = 1; A.mat[1][2] = 1; 52 A.mat[2][1] = 1; A.mat[2][2] = 0; 53 B.x = 2; B.y = 1; 54 B.mat[1][1] = 1; B.mat[2][1] = 1; 55 56 mat_pow(A, N - 1); 57 mat_mul(A, B, B); 58 cout << B.mat[1][1] << endl; 59 60 } 61 return 0; 62 }
?
?
洛谷P1962 斐波那契數列
題目背景
大家都知道,斐波那契數列是滿足如下性質的一個數列:
? f(1) = 1
? f(2) = 1
? f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 為整數)
題目描述
請你求出 f(n) mod 1000000007 的值。
輸入輸出格式
輸入格式:
?
·第 1 行:一個整數 n
?
?輸出格式:
?
第 1 行: f(n) mod 1000000007 的值
?
輸入輸出樣例
5
5
10
55
說明
對于 60% 的數據: n ≤ 92
對于 100% 的數據: n在long long(INT64)范圍內。
首先看到數據范圍,longlong以內,故我們考慮矩陣加速動態規劃。
我們都知道斐波那契數列有這樣的一個動態轉移方程:f[i]=f[i-1]+f[i-2];
由此可以推出一個2×2的矩陣:1 1 1 0
然后就是套用矩陣快速冪的模板來加速。
以下是代碼:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
using namespace std;
typedef long long lol;
lol n;
lol mod=1e9+7;
lol f[3][3],ans[3]={0,1,1};
void lu() {
lol t[3]={0};
for (int i=1;i<=2;i++)
for (int j=1;j<=2;j++)
t[i]=(t[i]+(ans[j]*f[j][i])%mod)%mod;
memcpy(ans,t,sizeof(ans));
}
void ge() {
lol t[3][3]={0};
for (lol i=1;i<=2;i++)
for (lol j=1;j<=2;j++)
for (lol k=1;k<=2;k++)
t[i][j]=(t[i][j]+(f[i][k]*f[k][j])%mod)%mod;
memcpy(f,t,sizeof(f));
}
int main() {
cin>>n;
if (n==1||n==2||!n) {
cout<<'1'<<endl;
exit(0);
}
n--;
f[1][1]=1;f[1][2]=1;
f[2][1]=1;f[2][2]=0;
while (n) {
if (n&1) lu();
ge();n>>=1;
}
cout<<ans[2]<<endl;
return 0;
}
?