HDU 4549
来源:互联网 发布:windows xp 下载 编辑:程序博客网 时间:2024/06/06 13:10
水题: 费马小定理+快速幂+矩阵快速幂
(第一次用到费马小定理)
#include<bits/stdc++.h>using namespace std;typedef long long LL;const LL MOD = 1000000006;const LL MOD1 = 1000000007;struct Matrix{ LL NUM[2][2]; Matrix operator + (const Matrix a) const { Matrix c; for(int i = 0; i < 2; ++i) { for(int j = 0; j < 2; ++j) { c.NUM[i][j] = NUM[i][j] + a.NUM[i][j]; } } return c; } Matrix operator * (const Matrix a) const { Matrix c; for(int i = 0; i < 2; ++i) { for(int j = 0; j < 2; ++j) { c.NUM[i][j] = 0; for(int k = 0; k < 2; ++k) c.NUM[i][j] = (c.NUM[i][j] + NUM[i][k] * a.NUM[k][j] % MOD) % MOD; } } return c; }};Matrix ppow(Matrix a, LL n){ Matrix ret; for(int i =0 ; i< 2; ++i) { for(int j = 0; j < 2; ++j) ret.NUM[i][j] = i==j ? 1 : 0; } while(n) { if(n & 1) ret = ret * a; a = a * a; n >>= 1; } return ret;}LL Pow(LL a, LL n){ LL ret = 1; while(n) { if(n & 1) ret =ret * a % MOD1; a = a * a % MOD1; n >>= 1; } return ret;}int main(){ LL a, b, n; Matrix E; E.NUM[0][0] = 1; E.NUM[0][1] = 1; E.NUM[1][0] = 1; E.NUM[1][1] = 0; while(cin >> a >> b >> n) { if(n == 0) cout << a << endl; else if(n == 1) cout << b << endl; else { n -= 1; Matrix tmp = ppow(E,n); LL na = tmp.NUM[0][1] , nb = tmp.NUM[0][0]; LL ans = (Pow(a,na) * Pow(b,nb))%MOD1; cout << ans << endl; } } return 0;}
0 0
- HDU 4549
- HDU 4549
- hdu 4549
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- UVA 1395 MST
- UVA 1151
- Java的poi技术读取和导入Excel
- CF 2B The least round way DP+Math
- POJ 1659
- HDU 4549
- CF 573B
- 在Eclipse中导入SVN库里的Maven项目
- 在网上看到好代码 ,记得发表到网上
- LA 4108 (线段树)
- java抽象类与接口的区别(谈谈自己的理解)
- iOS开发:创建真机调试证书
- CF 189A Cut Ribbon
- opencv 轮廓查找 findContours