快速幂 + 矩阵快速幂
生活随笔
收集整理的這篇文章主要介紹了
快速幂 + 矩阵快速幂
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
快速冪? ??
?
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #define LL long long 5 using namespace std; 6 7 LL Pow(LL a, LL b) 8 { 9 LL ans = 1; 10 while(b != 0){ 11 if(b&1) 12 ans *= a; 13 a *= a; 14 b >>= 1; 15 } 16 return ans; 17 } 18 int main() 19 { 20 LL a, b; 21 cin >> a >> b; 22 cout << Pow(a,b) << endl; 23 return 0; 24 }
?
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #define LL long long 5 using namespace std; 6 const LL mod = 1e9+7; 7 8 LL Pow(LL a, LL b) 9 { 10 LL ans = 1; 11 while(b != 0){ 12 if(b&1){ 13 ans *= a; 14 ans %= mod; 15 } 16 a *= a; 17 a %= mod; 18 b >>= 1; 19 } 20 return ans; 21 } 22 int main() 23 { 24 LL a, b; 25 cin >> a >> b; 26 cout << Pow(a,b) << endl; 27 return 0; 28 }
?
?
?
矩陣快速冪
? ?模板! (自己敲了一遍 只是感覺有些亂 就把ff的記上
?
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 typedef long long LL; 7 const int N = 105; 8 const LL mod = 1e9 + 7; 9 LL n, m; 10 struct mac { 11 LL c[N][N]; 12 void reset() { 13 memset(c, 0, sizeof(c)); 14 for (int i = 1; i <= n; i++) 15 c[i][i] = 1; 16 } 17 }; 18 mac multi(mac a, mac b) 19 { 20 mac ans; 21 for (int i = 1; i <= n; i++) 22 for (int j = 1; j <= n; j++) { 23 ans.c[i][j] = 0; 24 for (int k = 1; k <= n; k++) { 25 ans.c[i][j] += (a.c[i][k] * b.c[k][j]) % mod; 26 ans.c[i][j] %= mod; 27 } 28 } 29 return ans; 30 } 31 mac Pow(mac a, LL b) 32 { 33 mac ans; ans.reset(); 34 while (b) { 35 if (b & 1) 36 ans = multi(ans, a); 37 a = multi(a, a); 38 b >>= 1; 39 } 40 return ans; 41 } 42 int main() 43 { 44 ios::sync_with_stdio(false); 45 while (cin >> n >> m) { 46 mac a; 47 for (int i = 1; i <= n; i++) 48 for (int j = 1; j <= n; j++) 49 cin >> a.c[i][j]; 50 a = Pow(a, m); 51 for (int i = 1; i <= n; i++) { 52 for (int j = 1; j < n; j++) 53 cout << a.c[i][j] << " "; 54 cout << a.c[i][n] << endl; 55 } 56 } 57 return 0; 58 }
?
[應用](矩陣快速冪
?
4267: 二元數列
時間限制:?1 Sec??內存限制:?128 MB題目描述
已知:輸入x0,y0,a,b,c,d,n
輸出xn,yn對20181225取模的結果
輸入
輸入x0,y0,a,b,c,d,n輸出
輸出xn,yn對20181225取模的結果樣例輸入
7 6 5 4 3 2 1 樣例輸出
59 33 提示
0<=x0,y0,a,b,c,d<=1e3
0<=n<=8e8
?
解:雖然我此時此刻很想打死旁邊的豬 可是不得不承認這個題 ,是他教的
? ? ? 先推出 Xn+1 = (a2+bc)Xn-1 + (ab+bd)Yn-1;
? ? ? ? Yn+1 = (ac+cd)Xn-1 + (bc+d2)Yn-1;? ? ? ? 取系數為一個矩陣,你就會發現系數是由Xn Yn系數的平方求來的, 然后推推就知道? 題目求是一個矩陣的n次方? 由于n太大,所以采用矩陣快速冪來求出最后Xn Yn的系數,最后求得答案。
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #define LL long long 5 using namespace std; 6 const LL mod = 20181225; 7 struct node{ 8 LL m[2][2]; 9 void reset(){ 10 memset(m,0,sizeof(m)); 11 m[0][0] = 1; 12 m[1][1] = 1; 13 } 14 }; 15 LL xx, yy, a, b, c, d, n, x, y; 16 17 node multi(node a, node b) 18 { 19 node ans; ans.reset(); 20 ans.m[0][0] = 0; ans.m[1][1] = 0; 21 for(int i = 0; i < 2; i++) 22 for(int j = 0; j < 2; j++) 23 for(int k = 0; k < 2; k++){ 24 ans.m[i][j] += (a.m[i][k] * b.m[k][j]) % mod; 25 ans.m[i][j] %= mod; 26 } 27 return ans; 28 } 29 node Pow(node a, LL b) 30 { 31 node ans; ans.reset(); 32 while(b){ 33 if(b & 1) 34 ans = multi(ans, a); 35 a = multi(a, a); 36 b >>= 1; 37 } 38 return ans; 39 } 40 int main() 41 { 42 node may; 43 while(cin >> xx >> yy >> a >> b >> c >> d >> n){ 44 may.m[0][0] = a; may.m[0][1] = b; 45 may.m[1][0] = c; may.m[1][1] = d; 46 may = Pow(may,n); 47 x = (may.m[0][0] * xx + may.m[0][1] * yy) % mod; 48 y = (may.m[1][0] * xx + may.m[1][1] * yy) % mod; 49 cout << x << " " << y << endl; 50 } 51 return 0; 52 }?
?
[后記]? ?啦啦啦 這是我的第一篇博客,寫給自己看的博客, 以后要找些什么也會方便很多! 做個總結。希望這十分鐘不是浪費,加油鴨 小菜鳥 哈哈哈哈哈? 雖然我感覺8 這條道路上的大佬超級多,但是! 好像不是但是,我是一個比較容易放棄的人哈哈哈 也可以記錄一下生活呀 什么的 對伐! 耶? 悄咪咪的
轉載于:https://www.cnblogs.com/JiaaaaKe/p/9449880.html
總結
以上是生活随笔為你收集整理的快速幂 + 矩阵快速幂的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 硬七星香烟多少钱一包
- 下一篇: BZOJ4566: [Haoi2016]