LGP5136题解

配一个 \((\frac{1-\sqrt{5}}{2})^n\)。为什么不是 \(\frac{\sqrt{5}-1}{2}\)?因为根号难处理需要消掉:

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

考虑把两边的 GF 都扒出来,设 \(\alpha=\frac{1+\sqrt{5}}{2},\beta=\frac{1-\sqrt{5}}{2}\)

\[F(x)=\frac{1}{1-\alpha x}+\frac{1}{1-\beta x}=\frac{2-(\alpha+\beta)x}{1-(\alpha+\beta)x+\alpha\beta x^2} \]

代入后可以得到

\[F(x)=\frac{2-x}{1-x-x^2} \]

注意到 \(|\beta|<1\),也就是说答案相当于 \(F(x)\) 的某次项系数加上或减去 \(1\)

\(F(x)\) 显然是一个常系数齐次线性递推数列,于是能够做到 \(O(2^2T\log n)\)

#include<cstdio>
typedef unsigned ui;
typedef unsigned long long ull;
const ui mod=998244353;
ui T;ull n;
inline ui pow(ui a,ui b){
	ui ans(1);for(;b;b>>=1,a=1ull*a*a%mod)if(b&1)ans=1ull*ans*a%mod;return ans;
}
inline ui Solve(ull n){
    ui f0=2,f1=mod-1,g0=1,g1=mod-1,g2=mod-1;
    for(;n;n>>=1){
        if(n&1)f0=(1ull*g0*f1+1ull*(mod-g1)*f0)%mod,f1=1ull*g2*f1%mod;
        else f0=1ull*g0*f0%mod,f1=(1ull*g2*f0+1ull*(mod-g1)*f1)%mod;
        g1=(2ull*g0*g2+1ull*(mod-g1)*g1)%mod;g0=1ull*g0*g0%mod;g2=1ull*g2*g2%mod;
    }
    return 1ull*f0*pow(g0,mod-2)%mod;
}
signed main(){
	scanf("%u",&T);while(T--)scanf("%llu",&n),printf("%u\n",(Solve(n)+(n&1))%mod);
}
posted @ 2022-07-22 13:46  Prean  阅读(12)  评论(0编辑  收藏  举报
var canShowAdsense=function(){return !!0};