首页 > 其他 > 详细

2014 Super Training #7 F Power of Fibonacci --数学+逆元+快速幂

时间:2014-07-16 20:45:12      阅读:326      评论:0      收藏:0      [点我收藏+]

原题:ZOJ 3774  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3774

----------------------------------------------------------------------------------------------------------------------

这题比较复杂,看这篇比较详细:http://blog.csdn.net/acdreamers/article/details/23039571

结论就是计算:

bubuko.com,布布扣

充分利用了快速幂及求逆元。

代码:

bubuko.com,布布扣
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define Mod 1000000009
#define ll long long
using namespace std;
#define N 100007

ll fac[N],A[N],B[N];

void init()
{
    int i;
    fac[0] = 1;
    for(i=1;i<N;i++)
        fac[i] = fac[i-1]*i%Mod;
    A[0] = B[0] = 1;
    for(i=1;i<N;i++)
    {
        A[i] = A[i-1]*691504013 % Mod;
        B[i] = B[i-1]*308495997 % Mod;
    }
}

ll fastm(ll n,ll k,ll MOD)
{
    ll res = 1LL;
    n %= MOD;
    while(k)
    {
        if(k&1LL)
            res = (res*n)%MOD;
        k >>= 1;
        n = n*n%MOD;
    }
    return res;
}

ll Inv(ll n,ll MOD)
{
    return fastm(n,MOD-2,MOD);
}

int main()
{
    int cs;
    ll n,k;
    init();
    ll ans,r;
    scanf("%d",&cs);
    while(cs--)
    {
        scanf("%lld%lld",&n,&k);
        ans = 0;
        for(r=0;r<=k;r++)
        {
            ll t = A[k-r]*B[r] % Mod;
            ll x = fac[k];            // k!
            ll y = fac[k-r]*fac[r] % Mod;   // (k-r)!*(r)!
            ll c = x*Inv(y,Mod) % Mod;      // c = C(k,r) = x/y = x*Inv(y)
            ll tmp = t*(fastm(t,n,Mod)-1)%Mod*Inv(t-1,Mod)%Mod; //t(t^n-1)/(t-1) = t(t^n-1)*Inv(t-1)
            if(t == 1)
                tmp = n%Mod;
            tmp = tmp*c%Mod;
            if(r&1LL)   // (-1)^r
                ans -= tmp;
            else
                ans += tmp;
            ans %= Mod;
        }
        //ans = ans*(1/sqrt(5))^k
        ll m = Inv(383008016,Mod)%Mod;
        ans = ans*fastm(m,k,Mod)%Mod;
        ans = (ans%Mod+Mod)%Mod;
        printf("%lld\n",ans);
    }
    return 0;
}
View Code

 

2014 Super Training #7 F Power of Fibonacci --数学+逆元+快速幂,布布扣,bubuko.com

2014 Super Training #7 F Power of Fibonacci --数学+逆元+快速幂

原文:http://www.cnblogs.com/whatbeg/p/3836551.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!