【洛谷P4451】整数的lqp拆分(生成函数)

传送门

考虑设 g ( x ) g(x) g(x) a n s ans ans的生成函数
那么有
g ( x ) = f ( x ) g ( x ) + 1 g(x)=f(x)g(x)+1 g(x)=f(x)g(x)+1
g ( x ) = 1 1 − f ( x ) g(x)=\frac{1}{1-f(x)} g(x)=1f(x)1
由于 f ( x ) = f ( x ) x + f ( x ) x 2 + x f(x)=f(x)x+f(x)x^2+x f(x)=f(x)x+f(x)x2+x
所以 f ( x ) = x 1 − x − x 2 f(x)=\frac{x}{1-x-x^2} f(x)=1xx2x
g ( x ) = 1 + x 1 − 2 x − x 2 g(x)=1+\frac{x}{1-2x-x^2} g(x)=1+12xx2x
那么有 g ( x ) = 2 x g ( x ) + x 2 g ( x ) + x g(x)=2xg(x)+x^2g(x)+x g(x)=2xg(x)+x2g(x)+x
于是 a n s [ i ] = 2 a n s [ i − 1 ] + a n s [ i − 2 ] ans[i]=2ans[i-1]+ans[i-2] ans[i]=2ans[i1]+ans[i2]

#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<20|1;
inline char gc(){
    static char ibuf[RLEN],*ib,*ob;
    (ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ob==ib)?EOF:*ib++;
}
#define gc getchar
inline int read(){
    char ch=gc();
    int res=0,f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
#define ll long long
#define re register
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define cs const
#define bg begin
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int mod=1e9+7;
inline int add(int a,int b){return (a+=b)>=mod?a-mod:a;}
inline void Add(int &a,int b){(a+=b)>=mod?a-=mod:0;}
inline int dec(int a,int b){return (a-=b)<0?a+mod:a;}
inline void Dec(int &a,int b){(a-=b)<0?a+=mod:0;}
inline int mul(int a,int b){return 1ll*a*b%mod;}
inline void Mul(int &a,int b){a=1ll*a*b%mod;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
int f[1000005],n;
int main(){
	n=read();
	f[1]=1;
	for(int i=2;i<=n;i++)f[i]=add(add(f[i-1],f[i-1]),f[i-2]);
	cout<<f[n];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值