【杭电多校2020】Fibonacci Sum【斐波拉契通项】【推式子】

题意:设 F i F_i Fi为斐波拉契数列,求

∑ i = 0 N ( F i C ) k \sum_{i=0}^N(F_{iC})^k i=0N(FiC)k

1 0 9 + 9 10^9+9 109+9

N , C ≤ 1 0 18 , k ≤ 1 0 5 N,C\leq10^{18},k\leq10^5 N,C1018,k105

把斐波拉契暴力拆开

F i C = 1 5 [ ( 1 + 5 2 ) i C − ( 1 − 5 2 ) i C ] F_{iC}=\frac 1 {\sqrt{5}}[(\frac {1+\sqrt{5}}2)^{iC}-(\frac {1-\sqrt{5}}2)^{iC}] FiC=5 1[(21+5 )iC(215 )iC]

为了方便,忽略常数写成

F i c = A i − B i F_{ic}=A^i-B^i Fic=AiBi

所以

a n s = ∑ i = 0 N ( A i − B i ) k ans=\sum_{i=0}^N(A^i-B^i)^k ans=i=0N(AiBi)k

= ∑ i = 0 N ∑ j = 0 k ( k j ) ( − 1 ) j A ( k − j ) i B j =\sum_{i=0}^N\sum_{j=0}^k\binom kj(-1)^jA^{(k-j)i}B^j =i=0Nj=0k(jk)(1)jA(kj)iBj

= ∑ i = 0 k ( − 1 ) i ( k j ) ∑ j = 0 N ( A k − j B j ) i =\sum_{i=0}^k(-1)^i\binom kj\sum_{j=0}^N(A^{k-j}B^j)^i =i=0k(1)i(jk)j=0N(AkjBj)i

等比数列求和即可

复杂度 O ( n log ⁡ M ) O(n\log M) O(nlogM)

推式子先从简单的入手

#include 
#include 
#include 
#include 
using namespace std;
#define MAXN 100005
typedef long long ll;
const int MOD=1e9+9,N=1e5,p=383008016,A=(p+1ll)*(MOD+1)/2%MOD,B=(1ll+MOD-p)*(MOD+1)/2%MOD;
inline int qpow(int a,int p)
{
	int ans=1;
	while (p)
	{
		if (p&1) ans=(ll)ans*a%MOD;
		a=(ll)a*a%MOD;p>>=1;
	}
	return ans;
}
int fac[MAXN],finv[MAXN],px[MAXN],py[MAXN];
int main()
{
	fac[0]=1;
	for (int i=1;i<=N;i++) fac[i]=(ll)fac[i-1]*i%MOD;
	finv[N]=qpow(fac[N],MOD-2);
	for (int i=N-1;i>=0;i--) finv[i]=(ll)finv[i+1]*(i+1)%MOD;
	int T;
	scanf("%d",&T);
	while (T--)
	{
		ll n,c;
		int k;
		scanf("%lld%lld%d",&n,&c,&k);
		c%=MOD-1;
		int ans=0;
		int x=qpow(A,c),y=qpow(B,c);
		px[0]=py[0]=1;
		for (int i=1;i<=k;i++) px[i]=(ll)px[i-1]*x%MOD,py[i]=(ll)py[i-1]*y%MOD;
		for (int i=0;i<=k;i++) 
		{
			int t=((i&1)? MOD-1ll:1ll)*fac[k]%MOD*finv[i]%MOD*finv[k-i]%MOD;
			int a=(ll)px[k-i]*py[i]%MOD;
			if (a==1) ans=(ans+(n+1ll)%MOD*t)%MOD;
			else ans=(ans+t*(qpow(a,(n+1ll)%(MOD-1))-1ll)%MOD*qpow(a-1,MOD-2))%MOD;
		}
		ans=(ll)ans*qpow(p,MOD-1-k)%MOD;
		printf("%d\n",ans);
	}
	return 0;
}

你可能感兴趣的:(【杭电多校2020】Fibonacci Sum【斐波拉契通项】【推式子】)