2019牛客第九场 A-The power of Fibonacci

2019牛客第九场 A-The power of Fibonacci_第1张图片
2019牛客第九场 A-The power of Fibonacci_第2张图片
题解:
2019牛客第九场 A-The power of Fibonacci_第3张图片

#include 
using namespace std;
const int maxn=1e7+5;
typedef long long ll;
const ll mod=1e9;

ll loop1=768,p1=512;
ll loop2=7812500,p2=1953125;
ll pre[maxn],fib[maxn];

ll qpow(ll a,ll b,ll c){
    ll ans=1; a=a%c;
    while(b){
        if(b&1) ans=ans*a%c;
        b>>=1; a=a*a%c;
    }
    return ans;
}

void exgcd(ll a,ll b,ll &x,ll &y){
    if(b==0){
        x=1; y=0; return;
    }
    exgcd(b,a%b,y,x);
    y-=(a/b)*x;
}

ll inv(ll a,ll b){
    ll x,y; exgcd(a,b,x,y);
    return (x+b)%b;
}

int main(){
    ll n,m; scanf("%lld %lld",&n,&m);
    fib[0]=pre[0]=0; fib[1]=pre[1]=1;
    for(int i=2;i<=loop2;i++){
        fib[i]=(fib[i-1]+fib[i-2])%mod;
        pre[i]=pre[i-1]+qpow(fib[i],m,mod);
    }
    ll num1=(pre[n%loop1]+n/loop1*pre[loop1])%p1;
    ll num2=(pre[n%loop2]+n/loop2*pre[loop2])%p2;
    ll ans1=num1*p2%mod*inv(p2,p1)%mod;
    ll ans2=num2*p1%mod*inv(p1,p2)%mod;
    ll ans=(ans1+ans2)%mod;
    printf("%lld\n",ans);
    return 0;
}

你可能感兴趣的:(线性递推数列)