Fibonacci Sum HDU - 6755【2020 Multi-University Training Contest 1】斐波那契数列变形+二项式定理

【杭电多校2020】Distinct Sub-palindromes
分析:
Fibonacci Sum HDU - 6755【2020 Multi-University Training Contest 1】斐波那契数列变形+二项式定理_第1张图片

题目:

The Fibonacci numbers are defined as below:
在这里插入图片描述
Given three integers N, C and K, calculate the following summation:
在这里插入图片描述

Since the answer can be huge, output it modulo 1000000009 ( 1 0 9 10^9 109+9).

Input

The first line contains an integer T (1≤T≤200), denoting the number of test cases. Each test case contains three space separated integers in the order: N, C, K (1≤N,C≤ 1 0 18 10^{18} 1018,1≤K≤ 1 0 5 10^5 105).

Output

For each test case, output a single line containing the answer.

Sample Input

2
5 1 1
2 1 2

Sample Output

12
2
官方题解:Fibonacci Sum HDU - 6755【2020 Multi-University Training Contest 1】斐波那契数列变形+二项式定理_第2张图片

分析:Fibonacci Sum HDU - 6755【2020 Multi-University Training Contest 1】斐波那契数列变形+二项式定理_第3张图片

Fibonacci Sum HDU - 6755【2020 Multi-University Training Contest 1】斐波那契数列变形+二项式定理_第4张图片

AC代码:

#include
using namespace std;
typedef long long ll;
const int mod=1e9+9;
const int M=1e5;
int pow_mod(int x,int i)
{
    int y=1;
    while(i)
    {
        if(i&1)
            y=(ll)y*x%mod;
        x=(ll)x*x%mod;
        i>>=1;
    }
    return y;
}
ll N,C;
int K,a[M+5],b[M+5];
int comb(int n,int k)
{
    if(n<k)
        return 0;
    return (ll)a[n]*b[k]%mod*b[n-k]%mod;
}
int main()
{
    ios::sync_with_stdio(0);
    a[0]=1;
    for(int i=1; i<=M; ++i)
        a[i]=(ll)a[i-1]*i%mod;
    b[M]=pow_mod(a[M],mod-2);
    for(int i=M-1; i>=0; --i)
        b[i]=(ll)(i+1)*b[i+1]%mod;
    int T;
    cin>>T;
    while(T--)
    {
        cin>>N>>C>>K;
        int A=691504013,B=308495997;
        A=pow_mod(A,C%(mod-1));
        B=pow_mod(B,C%(mod-1));
        int a=1,b=pow_mod(B,K);
        int ib=pow_mod(B,mod-2);
        int ans=0;
        for(int j=0; j<=K; ++j)
        {
            int x=(ll)a*b%mod;
            if(x==1)
                x=(N+1)%mod;
            else
                x=(ll)(pow_mod(x,(N+1)%(mod-1))-1+mod)%mod * pow_mod((x-1+mod)%mod,mod-2) % mod;
            if((K-j)&1)
                x=(x==0?x:mod-x);
            ans=((ll)ans+(ll)comb(K,j)*x)%mod;
            a=(ll)a*A%mod;
            b=(ll)b*ib%mod;
        }
        int mul=276601605;
        mul=pow_mod(mul,K);
        ans=(ll)ans*mul%mod;
        cout<<ans<<endl;
    }
    return 0;
}

你可能感兴趣的:(数论的概念和方法)