CF1743G Antifibonacci Cut
空间限制非常小。考虑对于每一个点 \(i\),找到所有 \(j\) 满足 \([i...j]\) 为斐波那契字符串的前缀。
斐波那契字符串的第 \(i\) 为是可以计算的,代码如下(fbnqsl 表示斐波那契数列)
int js(int w){for(int i=31;i>1;i--) if(w>fbnqsl[i]) w-=fbnqsl[i];return w==1;}
用 \(vector\) 维护所有满足条件的 \(j\),时间复杂度和空间复杂度均为未知,但出题人说祂用暴力验证了 \(vector\) 中的元素数量是 \(\log\) 级的。
代码
#include<bits/stdc++.h>
using namespace std;
namespace IO{
template<typename T>
inline void read(T&x){
x=0;char c=getchar();bool f=0;
while(!isdigit(c)) c=='-'?f=1:0,c=getchar();
while(isdigit(c)) x=x*10+c-'0',c=getchar();
f?x=-x:0;
}
template<typename T>
inline void write(T x){
if(x==0){putchar('0');return ;}
x<0?x=-x,putchar('-'):0;short st[50],top=0;
while(x) st[++top]=x%10,x/=10;
while(top) putchar(st[top--]+'0');
}
inline void read(char&c){c=getchar();while(isspace(c)) c=getchar();}
inline void write(char c){putchar(c);}
inline void read(string&s){s.clear();char c;read(c);while(!isspace(c)&&~c) s+=c,c=getchar();}
inline void write(string s){for(int i=0,len=s.size();i<len;i++) putchar(s[i]);}
template<typename T>inline void write(T*x){while(*x) putchar(*(x++));}
template<typename T,typename...T2> inline void read(T&x,T2&...y){read(x),read(y...);}
template<typename T,typename...T2> inline void write(const T x,const T2...y){write(x),putchar(' '),write(y...),sizeof...(y)==1?putchar('\n'):0;}
}using namespace IO;
template<int mod>struct Modint{
int z;
Modint(){z=0;}
template<typename T>Modint(T x){x%=mod;z=x<0?x+mod:x;}
template<typename T>operator T()const{return z;}
Modint operator+(const Modint t)const{Modint ans;ans.z=(z+t.z)%mod;return ans;}
Modint operator*(const Modint t)const{Modint ans;ans.z=1ll*z*t.z%mod;return ans;}
Modint operator-(const Modint t)const{Modint ans;ans.z=(z-t.z)%mod;return ans;}
Modint operator+(const int t)const{Modint ans;ans.z=(z+t)%mod;return ans;}
Modint operator*(const int t)const{Modint ans;ans.z=1ll*z*t%mod;return ans;}
Modint operator-(const int t)const{Modint ans;ans.z=(z-t)%mod;return ans;}
Modint operator+(const long long t)const{Modint ans;ans.z=(z+t)%mod;return ans;}
Modint operator*(const long long t)const{Modint ans;ans.z=1ll*z*t%mod;return ans;}
Modint operator-(const long long t)const{Modint ans;ans.z=(z-t)%mod;return ans;}
Modint operator<<(const int t)const{Modint ans;ans.z=(z<<t)%mod;return ans;}
Modint operator>>(const int t)const{Modint ans;ans.z=(z>>t)%mod;return ans;}
Modint&operator+=(const Modint t){z=(z+t.z)%mod;return *this;}
Modint&operator*=(const Modint t){z=1ll*z*t.z%mod;return *this;}
Modint&operator-=(const Modint t){z=(z-t.z)%mod;return *this;}
Modint&operator+=(const int t){z=(z+t)%mod;return *this;}
Modint&operator*=(const int t){z=1ll*z*t%mod;return *this;}
Modint&operator-=(const int t){z=(z-t)%mod;return *this;}
Modint&operator+=(const long long t){z=(z+t)%mod;return *this;}
Modint&operator*=(const long long t){z=1ll*z*t%mod;return *this;}
Modint&operator-=(const long long t){z=(z-t)%mod;return *this;}
Modint&operator<<=(const int t){z=(z<<t)%mod;return *this;}
Modint&operator>>=(const int t){z=(z>>t)%mod;return *this;}
friend void read(Modint&z){
int x=0;char c=getchar();bool f=0;
while(!isdigit(c)) c=='-'?f=1:0,c=getchar();
while(isdigit(c)) x=(x*10ll+c-'0')%mod,c=getchar();
f?x=-x:0;
z.z=x;
}
friend void write(Modint x){x.z<0?x.z+=mod:0;write(x.z);}
};
const int mod=998244353;
Modint<mod>h,lth,ans;
int n,fbnqsl[40];
string s;
int js(int w){for(int i=31;i>1;i--) if(w>fbnqsl[i]) w-=fbnqsl[i];return w==1;}
vector<pair<int,Modint<mod>>>vt,nt;
signed main(){
fbnqsl[0]=fbnqsl[1]=1;
for(int i=2;i<=31;i++) fbnqsl[i]=fbnqsl[i-1]+fbnqsl[i-2];
read(n);
vt.push_back({0,1});
h=1,ans=1;
for(int i=1;i<=n;i++){
read(s);
for(int j:s){
int w=j-'0';
Modint<mod>nw=h;
lth=ans;
for(auto k:vt){
k.first++;
if(js(k.first)!=w) continue;
if(k.first!=1&&*lower_bound(fbnqsl+1,fbnqsl+1+31,k.first)==k.first) nw-=k.second;
nt.push_back(k);
}
vt.swap(nt),nt.clear();
ans=(nw-=lth);vt.push_back({0,nw});
h+=nw;
}
write(ans);write("\n");
}
return 0;
}

浙公网安备 33010602011771号