P4451[国家集训队]整数的lqp拆分【生成函数,特征方程】

正题

题目链接:https://www.luogu.com.cn/problem/P4451


题目大意

给出\(n\),对于所有满足\(\sum_{i=1}^ma_i=n\)\(\forall a_i\in N^+\)的序列求

\[\sum_{m=1}^{\infty}\prod_{i=1}^mFbi_{a_i} \]

其中\(Fbi_x\)表示第\(x\)个斐波那契数

\(1\leq n\leq 10^{10^4}\)


解题思路

因为刚学特征方程所以推的都会写下来,比较冗长

首先考虑斐波那契的生成函数\(F(x)=\sum_{i=0}^nFbi_ix^i\)
那么有\(F(x)=x^2F(x)+xF(x)+x\),可以解得\(F(x)=\frac{x}{1-x-x^2}\)

然后答案就是

\[\sum_{i=0}^{\infty}F(x)^i=\frac{1}{1-F(x)}=\frac{1}{1-\frac{x}{1-x-x^2}}=\frac{1-x-x^2}{1-2x-x^2} \]

然后\(\frac{1}{1-2x-x^2}\)是一个特征方程为\(1-2x-x^2\)的递推式,也就是\(a_n=2a_{n-1}+a_{n-2}\)
然后\(G(x)=\sum_{i=0}^{\infty}a_ix^i\),那么答案就是

\[(1-x-x^2)G(x)=\sum_{i=0}^{\infty}(a_i-a_{i-1}-a_{i-2})x^i=\sum_{i=0}^\infty a_{i-1}x^i \]

所以我们要求的第\(n\)项就是\(a_{n-1}\)

用特征方程化前面那个递推式了,解出\(1-2x-x^2=0\)\(x_0=\sqrt 2+1,x_1=-\sqrt 2+1\)
然后设\(a_n=c_0x_0^n+c_1x_1^n\)带入\(a_0=1\)\(a_1=2\)有方程

\[\left\{\begin{matrix}c_0+c_1=1\\c_0(\sqrt2+1)+c_1(-\sqrt 2+1)=2\end{matrix}\right. \]

解出来就是

\[\left\{\begin{matrix}c_0=\frac{2+\sqrt 2}{4}\\c_1=\frac{2-\sqrt 2}{4}\end{matrix}\right. \]

然后我们就有

\[a_n=\frac{2+\sqrt 2}{4}(\sqrt 2+1)^n+\frac{2-\sqrt 2}{4}(-\sqrt 2+1)^n \]

然后二次剩余跑出来在模\(10^9+7\)\(\sqrt 2=59713600\)带进去做就好了。

时间复杂度\(O(\log n)\)


code

#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const ll g2=59713600,P=1e9+7;
char s[11000];ll n; 
ll power(ll x,ll b){
	ll ans=1;
	while(b){
		if(b&1)ans=ans*x%P;
		x=x*x%P;b>>=1;
	}
	return ans;
}
signed main()
{
	scanf("%s",s+1);n=strlen(s+1);
	ll p=0;
	for(ll i=1;i<=n;i++)
		p=(p*10+s[i]-'0')%(P-1);
	ll inv=(P+1)/4;
	ll c1=(2-g2+P)%P*inv%P,c2=(2+g2)*inv%P;
	ll t1=c1*power(P-g2+1,p)%P*power(P-g2+1,P-2)%P;
	ll t2=c2*power(g2+1,p)%P*power(g2+1,P-2)%P;
	printf("%lld\n",(t1+t2)%P);
	return 0;
}
原文地址:https://www.cnblogs.com/QuantAsk/p/14438069.html