可以用来求解数列的最短常系数线性递推式
即一个长度为的数列满足对于
满足
算法复杂度是的
首先有个东西叫做常系数齐次线性递推
就是对于一个长度为的递推式
可以在的时间求出第项
首先定义为第次修改的递推式
当对于最新的一个不满足时
就会对递推式修改
定义表示第次修改
表示第次修改前和用当时的递推式算出来的值之差
考虑当在出错时,设修改次数为
考虑对修改为,使其对同样成立
如果当,直接填入个,这样就相当于作为定义式,不用考虑当前这个了
否则设
我们只需要求出一个{}$
使其满足
并且
然后只需要即可
我们发现只需要令{}
将个0,一个,再把乘上接在后面(注意是的)
可以发现这样是满足的(可以手推一下,不想具体写了)
这样最坏情况每次都会修改一次,修改次
复杂度就是的
代码:
// luogu-judger-enable-o2
// luogu-judger-enable-o2
#include<bits/stdc++.h>
using namespace std;
#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 re register
#define pb push_back
#define cs const
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
#define poly vector<int>
#define bg begin
cs int mod=998244353,G=3;
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?1ll*a*b%mod:a*b;}
inline void Mul(int &a,int b){a=mul(a,b);}
inline int ksm(int a,int b,int res=1){
for(;b;b>>=1,a=mul(a,a))(b&1)&&(res=mul(res,a));return res;
}
inline void chemx(ll &a,ll b){a<b?a=b:0;}
inline void chemn(int &a,int b){a>b?a=b:0;}
cs int N=(1<<16)|7,C=16;
poly w[C+1];
inline void init_w(){
for(int i=1;i<=C;i++)w[i].resize(1<<(i-1));
int wn=ksm(G,(mod-1)/(1<<C));
w[C][0]=1;
for(int i=1;i<(1<<(C-1));i++)w[C][i]=mul(w[C][i-1],wn);
for(int i=C-1;i;i--)
for(int j=0;j<(1<<(i-1));j++)
w[i][j]=w[i+1][j<<1];
}
int rev[N<<2];
inline void init_rev(int lim){
for(int i=0;i<lim;i++)rev[i]=(rev[i>>1]>>1)|((i&1)*(lim>>1));
}
inline void ntt(poly &f,int lim,int kd){
for(int i=0;i<lim;i++)if(i>rev[i])swap(f[i],f[rev[i]]);
for(int mid=1,l=1;mid<lim;mid<<=1,l++)
for(int i=0;i<lim;i+=(mid<<1))
for(int j=0,a0,a1;j<mid;j++)
a0=f[i+j],a1=mul(f[i+j+mid],w[l][j]),
f[i+j]=add(a0,a1),f[i+j+mid]=dec(a0,a1);
if(kd==-1&&(reverse(f.bg()+1,f.bg()+lim),1))
for(int inv=ksm(lim,mod-2),i=0;i<lim;i++)Mul(f[i],inv);
}
inline poly operator +(poly a,poly b){
poly c;int lim=max(a.size(),b.size());c.resize(lim);
a.resize(lim),b.resize(lim);
for(int i=0;i<lim;i++)c[i]=add(a[i],b[i]);return c;
}
inline poly operator -(poly a,poly b){
poly c;int lim=max(a.size(),b.size());c.resize(lim);
a.resize(lim),b.resize(lim);
for(int i=0;i<lim;i++)c[i]=dec(a[i],b[i]);return c;
}
inline poly operator *(poly a,int b){
for(int i=0;i<a.size();i++)Mul(a[i],b);return a;
}
inline poly operator /(poly a,int b){
for(int i=0,inv=ksm(b,mod-2);i<a.size();i++)Mul(a[i],inv);
return a;
}
inline poly operator *(poly a,poly b){
int deg=a.size()+b.size()-1,lim=1;
if(deg<=128){
poly c(deg,0);
for(int i=0;i<a.size();i++)
for(int j=0;j<b.size();j++)
Add(c[i+j],mul(a[i],b[j]));
return c;
}
while(lim<deg)lim<<=1;init_rev(lim);
a.resize(lim),ntt(a,lim,1);
b.resize(lim),ntt(b,lim,1);
for(int i=0;i<lim;i++)Mul(a[i],b[i]);
ntt(a,lim,-1),a.resize(deg);
return a;
}
inline poly Inv(poly a,int deg){
poly c,b(1,ksm(a[0],mod-2));
for(int lim=4;lim<(deg<<2);lim<<=1){
init_rev(lim);
c=a,c.resize(lim>>1);
c.resize(lim),ntt(c,lim,1);
b.resize(lim),ntt(b,lim,1);
for(int i=0;i<lim;i++)Mul(b[i],dec(2,mul(b[i],c[i])));
ntt(b,lim,-1),b.resize(lim>>1);
}b.resize(deg);return b;
}
inline poly operator /(poly a,poly b){
int lim=1,deg=(int)a.size()-(int)b.size()+1;
reverse(a.bg(),a.end());
reverse(b.bg(),b.end());
while(lim<=deg)lim<<=1;
b=Inv(b,lim),b.resize(deg);
a=a*b,a.resize(deg);
reverse(a.bg(),a.end());
return a;
}
inline poly operator %(poly a,poly b){
int deg=(int)a.size()-(int)b.size()+1;
if(deg<0)return a;
poly c=a-(a/b)*b;
c.resize(b.size()-1);
return c;
}
inline poly ksm(poly a,int b,poly res,cs poly &mod){
for(;b;b>>=1,a=a*a%mod)if(b&1)res=res*a%mod;
return res;
}
namespace Cayley_Hamilton{
int n;
inline int solve(poly coef,int *a,ll k){
n=coef.size(),init_w();
poly f(n),g(2),res(1,0);g[1]=res[0]=1;
for(int i=1; i<n;i++)f[n-i-1]=dec(0,coef[i]);
f[n-1]=1;
res=ksm(g,k,res,f);
int anc=0;
for(int i=0;i<res.size();i++)Add(anc,mul(res[i],a[i+1]));
return anc;
}
}
namespace Berlekamp_Massey{
poly r[N];
int n,cnt,m,a[N],fail[N],del[N];
inline void update(int i){
++cnt;
int MUL=mul(dec(a[i],del[i]),ksm(dec(a[fail[cnt-2]],del[fail[cnt-2]]),mod-2));
r[cnt].resize(i-fail[cnt-2],0);
r[cnt].pb(MUL);
for(int j=1;j<r[cnt-2].size();j++){
r[cnt].pb(mul(mod-r[cnt-2][j],MUL));
}
r[cnt]=r[cnt]+r[cnt-1];
}
inline void BM(){
for(int i=1;i<=n;i++){
for(int j=1;j<r[cnt].size();j++){
Add(del[i],mul(a[i-j],r[cnt][j]));
}
if(a[i]!=del[i]){
fail[cnt]=i;
if(!cnt)r[++cnt].resize(i+1);
else update(i);
}
}
}
inline void solve(){
n=read(),m=read();
for(int i=1;i<=n;i++)
a[i]=read();
BM();
for(int i=1;i<r[cnt].size();i++)
cout<<r[cnt][i]<<" ";puts("");
cout<<Cayley_Hamilton::solve(r[cnt],a,m)<<'
';
}
}
int main(){
Berlekamp_Massey::solve();
}
例题: