快速冪(整數+矩陣):hdu 2817+poj3233+poj1995+poj3070

來源:互聯網
上載者:User

快速冪的思想:二分

參考文獻: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 #includeusing 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 #includeusing 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 #includeusing 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 #includeusing 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<





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.