hdu 5451 Best Solver

來源:互聯網
上載者:User

標籤:fast   case   struct   const   sizeof   結果   base   分享   height   

 

 

很顯然這是矩陣快速冪的題

 

但發現用矩陣快速冪,指數很大很大,沒辦法用歐拉降冪(傻乎乎的用歐拉降冪,歐拉降冪只是對底數為整數,做了3個小時),結果是由周期的,根據周期對指數進行降冪,然後再矩陣快速冪,最後答案減一,因為這是求的向上取整

 

AC code:

#include <bits/stdc++.h>using namespace std;const int N = 1e6 + 10;const int M = 2;typedef long long ll;int m=2;int MOD;struct Matrix{    ll matrix[M][M];};void init(Matrix &res){    for(int i=0;i<m;i++)    {        for(int j=0;j<m;j++)            res.matrix[i][j]=0;        res.matrix[i][i]=1;    }}Matrix multiplicative(Matrix a,Matrix b){    Matrix res;    memset(res.matrix,0,sizeof(res.matrix));    for(int i = 0 ; i <m; i++)        for(int j = 0 ; j < m ; j++)            for(int k = 0 ; k < m; k++)                res.matrix[i][j] = (res.matrix[i][j]+a.matrix[i][k]%MOD*b.matrix[k][j]%MOD+MOD)%MOD;    return res;}Matrix pow(Matrix mx,ll m){    Matrix res,base=mx;    init(res); //初始為單位矩陣,即除主對角線都是1外,其他都是0    while(m)    {        if(m&1)            res=multiplicative(res,base);        base=multiplicative(base,base);        m>>=1;    }    return res;}ll fast_pow(ll a,ll n,ll mod){    ll ans = 1;    while(n)    {        if(n&1) ans = ans * a % mod;        a = a*a % mod;        n >>= 1;    }    return ans;}ll ans[N];int looped[46337 + 10];int getloop(int mod){    if(looped[mod]) return looped[mod];    ans[0] = 2%mod;ans[1] = 10%mod;    for(int i = 2;;i++)    {        ans[i] = (ans[i - 1]*10ll%mod - ans[i - 2] + mod)%mod;     //   cout<<i<<endl;        if(ans[i-1] == ans[0] && ans[i] == ans[1])    return looped[mod] = i - 1;    }}int main(){    int t,kase = 0,x;    memset(looped,0,sizeof(looped));    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&x,&MOD);        int loop = getloop(MOD);        //cout<<loop<<endl;        int n = (fast_pow(2,x,loop) + 1 ) % loop;        ll a = 5;        ll b = 24;        //cout<<n<<endl;        if(n == 0)  printf("Case #%d: %lld\n",++kase,1%MOD);        else if(n == 1)  printf("Case #%d: %lld\n",++kase,(2*a-1)%MOD);        else{            Matrix base={                2*a,-(a*a-b),                1,0            };            base=pow(base,n-1);            printf("Case #%d: %lld\n",++kase,(2*a%MOD*base.matrix[0][0]%MOD+2*base.matrix[0][1]%MOD+MOD - 1)%MOD);        }    }}

 

hdu 5451 Best Solver

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.