1005 Fibonacci Sum
Problem Description
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 (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≤10e18,1≤K≤10e5).
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
n,c的范围太大了只能用通项公式了:
通项公式带入二项式拆分之后可以得到k+1个形如(q ^ n)的等比数列,分别枚举这k+1个等比数列求和能直接求出来,但是式子中有根号5,这个没办法处理。可以根据二次剩余的概念求 x²≡5(mod p)可以解出来x=383008016或616991993。将x带入即可。
因为t*k已经2e7了,所以枚举等比数列时得尽量减少用快速幂否则会tle(比赛途中好像评测姬跑得快一些不会t),令a=(1+x)/ 2 , b =(1-x ) / 2 , 相邻项的公比的倍数是 a ^ -1 * b ^ 1 ,可以递推出来公比以及 q^n 减少快速幂的次数。
欧拉降幂需要mod的欧拉函数,mod是质数 欧拉函数就等于mod-1;
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100010,mod=1e9+9;
int t;
ll po[N],r[N],g[N],n,c,k,a,b,x;
void init()
{
po[0]=po[1]=g[0]=r[1]=g[1]=1;
for(int i=2;i<N;i++)
{
po[i]=po[i-1]*i%mod;
r[i]=1ll*(mod-mod/i)*r[mod%i]%mod;//逆元
g[i]=g[i-1]*r[i]%mod;//前i项逆元乘积
}
}
ll ksm(ll a,ll b)
{
ll res=1;
while(b)
{
if(b&1) res=res*a%mod;
a=a*a%mod