快速幂(整数+矩阵):hdu 2817+poj3233+poj1995+poj3070
2015-01-25 21:23
232 查看
快速幂的思想:二分
参考文献:http://blog.csdn.net/shiwei408/article/details/8818386
http://blog.csdn.net/hkdgjqr/article/details/5381292
hdu 2817(整数快速幂) A sequence of numbers
解题思路:
分别写出等差、等比数列通项an的表达式:
快速幂取模+基本的模运算就做了,附代码:
poj 3233(矩阵快速幂+二分)Matrix Power Series
题目链接:http://poj.org/problem?id=3233
解题思路:由于k比较大,如果每一项快速幂在相加,肯定会超时,需要二份求解,参考代码:
poj 1995(整数快速幂)Raising Modulo Numbers
题目链接:http://poj.org/problem?id=1995
解题思路:整数快速幂,依次相加取模即可。
参考代码:
poj 3070(矩阵快速幂)Fibonacci
题目链接:http://poj.org/problem?id=3070
解题思路:
求解菲波那切数列,f(n)=f(n-1)+f(n-2),如果我们一个个递推求解,当n特别大的时候复杂度就会变的很高,但我们我们利用如下的矩阵运算,可以大大减小时间复杂度,知道这个公式后我们就采用矩阵快速幂的方法可以求解f(n)。
参考代码:
参考文献:http://blog.csdn.net/shiwei408/article/details/8818386
http://blog.csdn.net/hkdgjqr/article/details/5381292
hdu 2817(整数快速幂) A sequence of numbers
解题思路:
分别写出等差、等比数列通项an的表达式:
快速幂取模+基本的模运算就做了,附代码:
#include #include #include #include #include #include #include using namespace std; typedef long long ll; const int maxn = 65535; const int mod=200907; ll a1,a2,a3,n,an; ll solve1() { ll d=(a2-a1)%mod; return (a1%mod+((n-1)%mod*d)%mod)%mod; } ll solve2() { ll ans=1,b=n-1,a=a2/a1; while(b){ if(b&1) ans=(ans*a)%mod; b>>=1; a=(a*a)%mod; } return (a1%mod*ans)%mod; } int main() { // freopen("input.txt","r",stdin); int T;cin>>T; while(T--){ cin>>a1>>a2>>a3>>n; if(a1+a3==2*a2) an=solve1(); else an=solve2(); cout<
poj 3233(矩阵快速幂+二分)Matrix Power Series
题目链接:http://poj.org/problem?id=3233
解题思路:由于k比较大,如果每一项快速幂在相加,肯定会超时,需要二份求解,参考代码:
#include #include #include #include #include #include #include using namespace std; typedef long long ll; const int maxn = 50; ll n,mod,b; struct matrix { ll m[maxn][maxn]; }a,p; matrix add(matrix a,matrix b) //矩阵加法 { matrix c; for(int i=0;i>=1; a=multi(a,a); } return ans; } matrix bsearch(int k) { if(k==1) return a; matrix temp=bsearch(k/2),b; if(k&1){ b=quickpow_mod(a,k/2+1); temp=add(temp,multi(temp,b)); temp=add(temp,b); } else{ b=quickpow_mod(a,k/2); temp=add(temp,multi(temp,b)); } return temp; } int main() { // freopen("input.txt","r",stdin); while(cin>>n>>b>>mod){ for(int i=0;i>a.m[i][j]; a.m[i][j]%=mod; //读入后先取模,避免溢出且减少取模次数 p.m[i][j]=(i==j); } matrix ans=bsearch(b); for(int i=0;i
poj 1995(整数快速幂)Raising Modulo Numbers
题目链接:http://poj.org/problem?id=1995
解题思路:整数快速幂,依次相加取模即可。
参考代码:
#include #include #include #include #include #include #include using namespace std; typedef long long ll; const int maxn = 50; ll mod,a,b; ll quickpow_mod(ll a,ll b) { ll ans=1; while(b){ if(b&1) ans=(ans*a)%mod; b>>=1; a=(a*a)%mod; } return ans; } int main() { // freopen("input.txt","r",stdin); int T;cin>>T; while(T--){ cin>>mod; int n;cin>>n; ll ans=0; while(n--){ cin>>a>>b; ans=(ans+quickpow_mod(a,b))%mod; } cout<
poj 3070(矩阵快速幂)Fibonacci
题目链接:http://poj.org/problem?id=3070
解题思路:
求解菲波那切数列,f(n)=f(n-1)+f(n-2),如果我们一个个递推求解,当n特别大的时候复杂度就会变的很高,但我们我们利用如下的矩阵运算,可以大大减小时间复杂度,知道这个公式后我们就采用矩阵快速幂的方法可以求解f(n)。
参考代码:
#include #include #include #include #include #include #include using namespace std; typedef long long ll; const int maxn = 50; const int mod=10000; ll n; struct matrix { ll m[2][2]; }a,p; matrix multi(matrix a,matrix b) { matrix c; for(int i=0;i<2;i++) for(int j=0;j<2;j++){ c.m[i][j]=0; for(int k=0;k<2;k++) c.m[i][j]+=a.m[i][k]*b.m[k][j]; c.m[i][j]%=mod; } return c; } matrix quickpow_mod(matrix a,ll b) { matrix ans=p; while(b){ if(b&1) ans=multi(ans,a); b>>=1; a=multi(a,a); } return ans; } int main() { // freopen("input.txt","r",stdin); a.m[0][0]=a.m[0][1]=a.m[1][0]=1;a.m[1][1]=0;//a、p矩阵初始化 p.m[0][0]=p.m[1][1]=1;p.m[0][1]=p.m[1][0]=0; matrix ans; while(cin>>n&&n!=-1){ ans=quickpow_mod(a,n); cout<
相关文章推荐
- HDU-1005(矩阵二分快速幂)
- C(HDU-1588矩阵二分快速幂)
- HDU 4686 Arc of Dream(构造矩阵 快速幂)
- hdu 4291 矩阵的快速幂(寻找循环节)
- HDU 2256 Problem of Precision (矩阵快速幂)(推算)
- 【HDU】2157 - How many ways??(矩阵快速幂)(从i到j经过k个点有多少种走法)
- hdu 4549(矩阵优化递推,数论,二分快速幂)
- HDU 1757 A Simple Math Problem (矩阵+快速幂)
- hdu 5015 233 Matrix(矩阵构造加快速幂)
- HDU 4549 M斐波那契数列(费马小定理,矩阵快速幂,快速幂)
- HDU 4565So Easy! (矩阵快速幂)(推公式)
- HDU(2855_矩阵的二分快速幂)
- hdu 4565 So Easy!(矩阵+快速幂)
- HDU 2157 How many ways??(矩阵的快速幂)
- HDU - 5950 Recursive sequence 矩阵快速幂(由公式推矩阵快速幂简单讲解)
- 【HDU】1757 - A Simple Math Problem(矩阵构造方法 & 快速幂)
- 矩阵快速幂之整数快速幂
- hdu 4565 So Easy!(矩阵+快速幂)
- HDU 4549 M斐波那契数列(矩阵快速幂)(费马小定理)
- 整数的快速幂和矩阵的快速幂