您的位置:首页 > 其它

快速幂(整数+矩阵):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的表达式:

                                                                             


快速幂取模+基本的模运算就做了,附代码:

#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<


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  快速幂