hdu 5451 Best Solver -廣義斐波那契+矩陣快速冪+共軛構造+循環節

http://acm.hdu.edu.cn/showproblem.php?pid=5451

方法類似http://blog.csdn.net/viphong/article/details/52980972

只不過此題指定了a=5,b=24

首先根據http://blog.csdn.net/viphong/article/details/52980972 中的方法,構造 Cn=An+Bn

推出 

C(n+1)=10*C(n)−C(n−1)

然後由於本題的次數是 1+2^x,  x非常大

可以證明循環節是不大的

這裏用暴力打表找循環節


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;

const int N = 2;
long long  mod=1e9+7;

long long n;
const long long  k=2;
int f[86000];
long long len;
const long long b=24-5*5;

int work()
{
    const int modd=mod;
    f[1]=10%modd;
    f[2]=98%modd;
    for (int i=3;  ; i++)
    {
        f[i]=(f[i-1]*10-f[i-2]+modd)%modd;
        //f[i]=(f[i]+modd)%modd;
         if (f[i-1]==f[1]&&f[i]==f[2] )
            return len=i-2;
    }
}
int powe_m(int  a,int  b,const int mod )
{
    long long ans=1;
    long long tmp=a;
    while(b!=0)
    {
        if (b&1)
            ans=ans*tmp%mod;
        tmp=tmp*tmp%mod;
        b=b>>1;
    }
    return ans;
}

int main()
{
    int t;
    scanf("%d",&t);
    int cnt=1;
    while(t--)
    {
        scanf("%lld%lld,",&n,&mod);
        work();
        long long id=powe_m(2,n,len);
          id=(id+1)%len;
        printf("Case #%d: %lld\n", cnt++,((  f[id]-1  )%mod+mod)%mod);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章