胡策题
感觉出了一道送分的
瑠璃色の物語
我的送分题
S
o
l
u
t
i
o
n
Solution
Solution
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define y1 shinkle
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
#define gc getchar
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res*10)+(ch^48),ch=gc();
return f?res:-res;
}
inline ll readll(){
char ch=gc();
ll res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res*10)+(ch^48),ch=gc();
return f?res:-res;
}
inline char readchar(){
char ch=gc();
while(isspace(ch))ch=gc();
return ch;
}
inline int readstring(char *s){
int top=0;char ch=gc();
while(isspace(ch))ch=gc();
while(!isspace(ch)&&ch!=EOF)s[++top]=ch,ch=gc();
s[top+1]='\0';return top;
}
template<typename tp>inline void chemx(tp &a,tp b){a=max(a,b);}
template<typename tp>inline void chemn(tp &a,tp b){a=min(a,b);}
cs int mod=998244353;
inline int add(int a,int b){return (a+b)>=mod?(a+b-mod):(a+b);}
inline int dec(int a,int b){return (a<b)?(a-b+mod):(a-b);}
inline int mul(int a,int b){static ll r;r=(ll)a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){a=(a+b)>=mod?(a+b-mod):(a+b);}
inline void Dec(int &a,int b){a=(a<b)?(a-b+mod):(a-b);}
inline void Mul(int &a,int b){static ll r;r=(ll)a*b;a=(r>=mod)?(r%mod):r;}
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);}
inline int fix(ll x){x%=mod;return (x<0)?x+mod:x;}
typedef vector<int> poly;
namespace Poly{
cs int C=19,M=(1<<C)|5;
int *w[C+1],rev[M],iv[M];
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 init_w(){
for(int i=1;i<=C;i++)w[i]=new int[(1<<(i-1))|1];
int wn=ksm(3,(mod-1)/(1<<C));w[C][0]=1;
for(int i=1,l=1<<(C-1);i<l;i++)w[C][i]=mul(w[C][i-1],wn);
for(int i=C-1;i;i--)
for(int j=0,l=1<<(i-1);j<l;j++)w[i][j]=w[i+1][j<<1];
iv[0]=iv[1]=1;
for(int i=2;i<M;i++)iv[i]=mul(mod-mod/i,iv[mod%i]);
}
inline void dft(int *f,int lim){
for(int i=0;i<lim;i++)if(i>rev[i])swap(f[i],f[rev[i]]);
for(int mid=1,l=1,a0,a1;mid<lim;mid<<=1,l++)
for(int i=0;i<lim;i+=mid<<1)
for(int j=0;j<mid;j++)
a0=f[i+j],a1=mul(w[l][j],f[i+j+mid]),f[i+j]=add(a0,a1),f[i+j+mid]=dec(a0,a1);
}
inline void ntt(poly &f,int lim,int kd){
dft(&f[0],lim);
if(kd==-1){
reverse(f.bg()+1,f.bg()+lim);
for(int i=0;i<lim;i++)Mul(f[i],iv[lim]);
}
}
inline poly operator +(poly a,poly b){
if(a.size()<b.size())a.resize(b.size());
for(int i=0;i<b.size();i++)Add(a[i],b[i]);
return a;
}
inline poly operator -(poly a,poly b){
if(a.size()<b.size())a.resize(b.size());
for(int i=0;i<b.size();i++)Dec(a[i],b[i]);
return a;
}
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,poly b){
if(!a.size()||!b.size())return poly(0);
int deg=a.size()+b.size()-1;
if(a.size()<=32||b.size()<=32){
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;
}int lim=1;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 b(1,::Inv(a[0])),c;
for(int lim=4;lim<(deg<<2);lim<<=1){
c.resize(lim>>1);init_rev(lim);
for(int i=0;i<(lim>>1);i++)c[i]=(i<a.size()?a[i]:0);
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 deriv(poly a){
for(int i=0;i+1<a.size();i++)a[i]=mul(a[i+1],i+1);
a.pop_back();return a;
}
inline poly integ(poly a){
a.pb(0);
for(int i=a.size()-1;i;i--)a[i]=mul(a[i-1],iv[i]);
a[0]=0;return a;
}
inline poly Ln(poly a,int deg){
a=integ(Inv(a,deg)*deriv(a)),a.resize(deg);return a;
}
inline poly Exp(poly a,int deg){
poly b(1,1),c;
for(int lim=2;lim<(deg<<1);lim<<=1){
c=Ln(b,lim);
for(int i=0;i<lim;i++)c[i]=dec(i<a.size()?a[i]:0,c[i]);
Add(c[0],1);
b=b*c,b.resize(lim);
}b.resize(deg);return b;
}
inline poly ksm(poly a,int b,int deg){
return Exp(Ln(a,deg)*b,deg);
}
inline poly Mulx(poly a){
a.pb(0);
for(int i=a.size()-1;i;i--)a[i]=a[i-1];
a[0]=0;return a;
}
inline poly Divx(poly a){
for(int i=0;i+1<a.size();i++)a[i]=a[i+1];
a.pop_back();return a;
}
}
using namespace Poly;
cs int N=200005;
int n,m;
int c[N],v[N],a[N];
vector<int> tp[N];
int g[N];
poly build(int l,int r){
if(l==r){
poly a(2);
a[0]=1,a[1]=dec(0,g[l]);
return a;
}int mid=(l+r)>>1;
return build(l,mid)*build(mid+1,r);
}
inline poly get(int ln,int lm){
poly f=build(1,ln),g(ln+1);
for(int i=0;i<=ln;i++)g[i]=mul(f[i],ln-i);
g=g*Inv(f,lm),g.resize(lm);return g;
//for(int i=0;i<now.size();i++)cout<<now[i]<<" ";puts("");
//return Inv(now,lm);
}
poly build(){
poly f(n+1);
for(int i=1;i<=n;i++)if(tp[i].size()){
int lt=n/i;
for(int j=0;j<tp[i].size();j++)g[j+1]=tp[i][j];
vector<int> vl=get(tp[i].size(),lt+1);
for(int j=1;j<=lt;j++){
Add(f[i*j],mul(iv[j],vl[j]));
}
}
f=Exp(f,n+1);
return f;
}
char rd[100],bt[100];
int main(){
init_w();
m=read(),n=read();
for(int i=1;i<=m;i++)c[i]=read(),assert(c[i]<=n&&c[i]!=0);
for(int i=1;i<=m;i++)v[i]=read(),assert(v[i]>0&&v[i]<mod);
for(int i=1;i<=m;i++)tp[c[i]].pb(v[i]);
poly f=build();
poly g=Mulx(Inv(f,n));
poly now=Inv(poly(1,1)-g,n+1);
now.resize(n+1);
now=now*ksm(f,n,n+1);
cout<<now[n]<<'\n';
return 0;
}
Naiive
f s y fsy fsy的神仙题
考虑串建成
S
a
Sa
Sa或者后缀树上的两点的贡献
只需要按照
l
c
p
lcp
lcp合并,这样需要的是找到在原数列中位置的前驱后继
询问就变成矩形取
m
a
x
max
max,单点询问,但发现矩形比较特殊可以做
强制在线写个主席树即可
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
#define bg begin
namespace IO{
cs int RLEN=1<<20|1;
char ibuf[RLEN],*ib,*ob;
inline char gc(){
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=0;
while(!isdigit(ch))f=(ch=='-'),ch=gc();
while(isdigit(ch))res=(res*10)+(ch^48),ch=gc();
return f?-res:res;
}
inline int readstring(char *s){
int top=0;char ch=gc();
while(isspace(ch))ch=gc();
while(!isspace(ch)&&ch!=EOF)s[++top]=ch,ch=gc();
s[top+1]='\0';return top;
}
}
using IO::read;
using IO::readstring;
template<typename tp>inline void chemx(tp &a,tp b){(a<b)?(a=b):0;}
template<typename tp>inline void chemn(tp &a,tp b){(a>b)?(a=b):0;}
cs int N=100005;
namespace Seg{
cs int N=::N*400;
#define mid ((l+r)>>1)
int lc[N],rc[N],tot,mx[N];
inline int copy(int r){
int u=++tot;lc[u]=lc[r],rc[u]=rc[r],mx[u]=mx[r];return u;
}
void update(int &u,int l,int r,int p,int k){
u=copy(u);chemx(mx[u],k);
if(l==r)return;
(p<=mid)?update(lc[u],l,mid,p,k):update(rc[u],mid+1,r,p,k);
}
int query(int &u,int l,int r,int st){
if(!u)return 0;
if(st<=l)return mx[u];
if(mid<st)return query(rc[u],mid+1,r,st);
return max(mx[rc[u]],query(lc[u],l,mid,st));
}
#undef mid
}
int rt[N];
vector<pii> upd[N];
namespace Sa{
int n,m,sa[N],sa2[N],rk[N],a[N],buc[N],ht[N];
set<int>st[N];
typedef set<int>::iterator It;
inline void Sort(){
for(int i=1;i<=n;i++)buc[rk[sa2[i]]]++;
for(int i=1;i<=m;i++)buc[i]+=buc[i-1];
for(int i=n;i>=1;i--)sa[buc[rk[sa2[i]]]--]=sa2[i];
for(int i=0;i<=m;i++)buc[i]=0;
}
int id[N],fa[N];
inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
void Ins(int l,int r,int vl){
if(l>r)swap(l,r);
upd[r].pb(pii(l,vl));
}
inline void build(char *s,int _n){
n=_n;m=26;
for(int i=1;i<=n;i++)a[i]=s[i]-'a'+1;
for(int i=1;i<=n;i++)rk[i]=a[i],sa2[i]=i;
Sort();
for(int i=1,pos=0;i<=n&&pos<n;i<<=1){
pos=0;
for(int j=n-i+1;j<=n;j++)sa2[++pos]=j;
for(int j=1;j<=n;j++)if(sa[j]>i)sa2[++pos]=sa[j]-i;
Sort(),swap(rk,sa2);
rk[sa[1]]=1,pos=1;
for(int j=2;j<=n;j++)
rk[sa[j]]=(sa2[sa[j]]==sa2[sa[j-1]]&&sa2[sa[j]+i]==sa2[sa[j-1]+i])?pos:++pos;
m=pos;
}
for(int i=1,k=0,j;i<=n;ht[rk[i++]]=k)
for(j=sa[rk[i]-1],k?k--:0;a[i+k]==a[j+k];k++);
for(int i=1;i<n;i++)id[i]=i+1;
for(int i=1;i<=n;i++)fa[i]=i,st[i].insert(sa[i]);
sort(id+1,id+n,[](cs int &i,cs int &j){return ht[i]>ht[j];});
for(int i=1;i<n;i++){
int u=id[i],v=u-1,h=ht[u];u=find(u),v=find(v);
if(st[u].size()>st[v].size())swap(u,v);fa[u]=v;
for(int x:st[u]){
It it=st[v].lower_bound(x);
if(it!=st[v].end())Ins(x,*it,h);
if(it!=st[v].bg())Ins(x,*--it,h);
}
for(int x:st[u])st[v].insert(x);st[u].clear();
}
}
}
int n,m;
char str[N];
int last;
int main(){
#ifdef Stargazer
freopen("lx.in","r",stdin);
freopen("my.out","w",stdout);
#endif
n=read(),m=read();
readstring(str);
Sa::build(str,n);
for(int i=0;i<=n;i++){
if(i)rt[i]=rt[i-1];
for(int j=0;j<upd[i].size();j++){
pii x=upd[i][j];
Seg::update(rt[i],1,n,x.fi,x.se);
}
}
for(int t=1;t<=m;t++){
int l=(read()+last-1)%n+1,r=(read()+last-1)%n+1;
if(l>r)swap(l,r);
cout<<(last=Seg::query(rt[r],1,n,l))<<'\n';
}return 0;
}
gcd卷积
l d x ldx ldx的神仙题
直接调和级数容斥做是
O
(
55
n
l
o
g
m
o
d
)
O(55nlogmod)
O(55nlogmod)
如果用质因数次数高维前缀和容斥并预处理幂次是
O
(
55
n
l
o
g
l
o
g
n
+
55
m
o
d
l
o
g
)
O(55nloglogn+55modlog)
O(55nloglogn+55modlog)的
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
#define bg begin
namespace IO{
cs int RLEN=1<<20|1;
char ibuf[RLEN],*ib,*ob;
inline char gc(){
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=0;
while(!isdigit(ch))f=(ch=='-'),ch=gc();
while(isdigit(ch))res=(res*10)+(ch^48),ch=gc();
return f?-res:res;
}
inline int readstring(char *s){
int top=0;char ch=gc();
while(isspace(ch))ch=gc();
while(!isspace(ch)&&ch!=EOF)s[++top]=ch,ch=gc();
s[top+1]='\0';return top;
}
}
using IO::read;
template<typename tp>inline void chemx(tp &a,tp b){(a<b)?(a=b):0;}
template<typename tp>inline void chemn(tp &a,tp b){(a>b)?(a=b):0;}
cs int mod=23333;
inline int add(int a,int b){return (a+b>=mod)?(a+b-mod):(a+b);}
inline int dec(int a,int b){return (a<b)?(a-b+mod):(a-b);}
inline int mul(int a,int b){static ll r;r=(ll)a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){a=(a+b>=mod)?(a+b-mod):(a+b);}
inline void Dec(int &a,int b){a=(a<b)?(a-b+mod):(a-b);}
inline void Mul(int &a,int b){static ll r;r=(ll)a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))if(b&1)Mul(res,a);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
cs int N=100005;
int n,a[N],s[N],pr[N],tot,pw[mod],lm[N];
bool vis[N];
inline void init_sieve(){
for(int i=2;i<=n;i++){
if(!vis[i])pr[++tot]=i,lm[tot]=n/i;
for(int j=1;j<=tot&&i*pr[j]<=n;j++){
vis[i*pr[j]]=1;
if(i%pr[j]==0)break;
}
}
}
inline void fgt(int *f){
for(int i=1;i<=tot;i++)
for(int j=lm[i];j;j--)Add(f[j],f[j*pr[i]]);
}
inline void ifgt(int *f){
for(int i=1;i<=tot;i++)
for(int j=1;j<=lm[i];j++)Dec(f[j],f[j*pr[i]]);
}
int main(){
#ifdef Stargazer
freopen("lx.in","r",stdin);
freopen("my.out","w",stdout);
#endif
n=read();init_sieve();
for(int i=1;i<=n;i++)s[i]=1;
for(int i=1;i<=55;i++){
for(int j=1;j<=n;j++)a[j]=read();
fgt(a);
int t=read();
for(int j=0;j<mod;j++)pw[j]=ksm(j,t);
for(int j=1;j<=n;j++)assert(a[j]<mod),Mul(s[j],pw[a[j]]);
}
ifgt(s);
for(int i=1;i<=n;i++)cout<<s[i]<<" ";puts("");
return 0;
}
构造题
z
x
y
zxy
zxy的神仙题
做不来QwQ
s
o
l
u
t
i
o
n
solution
solution