【CSP2019模拟】题解
T1:
本场送分题
建个图求最长路即可
由于花了太久并没有来的及写
被踩爆了
#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 pic pair<int,char>
#define fi first
#define se second
#define pb push_back
#define cs const
#define bg begin
#define poly vector<int>
#define chemx(a,b) ((a)<(b)?(a)=(b):0)
#define chemn(a,b) ((a)>(b)?(a)=(b):0)
cs int N=1e5,M=31;
int n,m,tot,in[N];
vector<int> e[N],E[N];
inline void addedge(int u,int v){
e[u].pb(v);
}
inline void add(int u,int v){
E[u].pb(v),in[v]++;
}
int pos[M][M][M][M];
struct node{
int a,b,c,d;
node(int _1=0,int _2=0,int _3=0,int _4=0):a(_1),b(_2),c(_3),d(_4){}
friend inline bool operator <(cs node &a,cs node &b){
return a.a<=b.a&&a.b<=b.b&&a.c<=b.c&&a.d<=b.d;
}
friend inline node operator +(cs node &a,cs node &b){
return node(a.a+b.a,a.b+b.b,a.c+b.c,a.d+b.d);
}
friend inline node operator -(cs node &a,cs node &b){
return node(a.a-b.a,a.b-b.b,a.c-b.c,a.d-b.d);
}
}s[55],t[55],idx[N];
char ss[N];
ll val[N],w[N],mxv[N];
__int128 tval;
int dfn[N],low[N],tim,vis[N],bel[N],belnum;
stack<int> stk;
inline ll calc(int i,int j,int k,int p){
__int128 res=tval;
for(int x=1;x<=i;x++)res/=x;
for(int x=1;x<=j;x++)res/=x;
for(int x=1;x<=k;x++)res/=x;
for(int x=1;x<=p;x++)res/=x;
return res;
}
void dfs(int u){
dfn[u]=low[u]=++tim;
stk.push(u),vis[u]=1;
for(int &v:e[u]){
if(!dfn[v])dfs(v),chemn(low[u],low[v]);
else if(vis[v])chemn(low[u],dfn[v]);
}
if(low[u]==dfn[u]){
belnum++;
int tmp;
do{
tmp=stk.top();
stk.pop();
vis[tmp]=0;
bel[tmp]=belnum;
w[belnum]+=val[tmp];
}while(tmp!=u);
}
}
int pp[4];
int main(){
n=read(),m=read();
for(int i=1;i<=m;i++){
scanf("%s",ss+1);
memset(pp,0,sizeof(pp));
for(int j=1,l=strlen(ss+1);j<=l;j++)
pp[ss[j]-'A']++;
s[i]=node(pp[0],pp[1],pp[2],pp[3]);
memset(pp,0,sizeof(pp));
scanf("%s",ss+1);
for(int j=1,l=strlen(ss+1);j<=l;j++)
pp[ss[j]-'A']++;
t[i]=node(pp[0],pp[1],pp[2],pp[3]);
}
tval=1;
for(int i=2;i<=n;i++)tval*=i;
for(int i=0;i<=n;i++)
for(int j=0;i+j<=n;j++)
for(int k=0;i+j+k<=n;k++)
pos[i][j][k][n-i-j-k]=++tot,val[tot]=calc(i,j,k,n-i-j-k),idx[tot]=node(i,j,k,n-i-j-k);
for(int i=1;i<=tot;i++){
node to;for(int j=1;j<=m;j++)
if(s[j]<idx[i]){
to=idx[i]-s[j]+t[j];
addedge(i,pos[to.a][to.b][to.c][to.d]);
}
}
for(int i=1;i<=tot;i++)if(!dfn[i])dfs(i);
for(int i=1;i<=tot;i++){
for(int &v:e[i]){
if(bel[v]!=bel[i])
add(bel[i],bel[v]);
}
}
queue<int> q;
for(int i=1;i<=belnum;i++)
if(!in[i])q.push(i);
ll ans=0;
while(!q.empty()){
int u=q.front();q.pop();
chemx(ans,mxv[u]+w[u]);
for(int &v:E[u]){
in[v]--,chemx(mxv[v],mxv[u]+w[u]);
if(!in[v])q.push(v);
}
}
cout<<ans;
}
T2:
以前考过的一道原题
不过当时并没有写就是了
结果为了保险写了双取模哈希光荣了,白丢分
#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 pic pair<int,char>
#define fi first
#define se second
#define pb push_back
#define cs const
#define bg begin
#define poly vector<int>
#define chemx(a,b) ((a)<(b)?(a)=(b):0)
#define chemn(a,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);}
cs int N=2005;
int n,m;
char a[2][N],s[N];
int f[2][N][N];
int pw1[N],pw2[N];
cs int bas1=1331,mod1=1920643713,bas2=233,mod2=771939933;
inline void init(){
pw1[0]=pw2[0]=1;
for(int i=1;i<N;i++)pw1[i]=1ll*pw1[i-1]*bas1%mod1,pw2[i]=1ll*pw2[i-1]*bas2%mod2;
}
struct Hash{
int has1[N],has2[N];
inline void init(char *s){
for(int i=1,len=strlen(s+1);i<=len;i++)
has1[i]=(1ll*has1[i-1]*bas1+s[i]-'a'+1)%mod1,has2[i]=(1ll*has2[i-1]*bas2+s[i]-'a'+1)%mod2;
}
inline pii query(int l,int r){
return pii(((has1[r]-1ll*has1[l-1]*pw1[r-l+1])%mod1+mod1)%mod1,((has2[r]-1ll*has2[l-1]*pw2[r-l+1])%mod2+mod2)%mod2);
}
}S,pre[2],suf[2];
inline bool operator ==(cs pii &a,cs pii &b){
return a.fi==b.fi&&a.se==b.se;
}
inline int calc(char *s,int dir){
memset(f,0,sizeof(f));
int res=0;
for(int i=0;i<=n;i++){
for(int k=2;k*2<=m&&k<=i;k++)if(!dir||k*2!=m){
if(suf[0].query(n-i+1,n-i+k)==S.query(1,k)&&pre[1].query(i-k+1,i)==S.query(k+1,2*k))Add(f[1][i][2*k],1);
if(suf[1].query(n-i+1,n-i+k)==S.query(1,k)&&pre[0].query(i-k+1,i)==S.query(k+1,2*k))Add(f[0][i][2*k],1);
}
f[0][i][0]=1,f[1][i][0]=1;
for(int k=2;2*k<=m&&i+k<=n;k++)if(!dir||k*2!=m){
if(pre[0].query(i+1,i+k)==S.query(m-2*k+1,m-k)&&suf[1].query(n-i-k+1,n-i)==S.query(m-k+1,m))Add(res,f[0][i][m-2*k]);
if(pre[1].query(i+1,i+k)==S.query(m-2*k+1,m-k)&&suf[0].query(n-i-k+1,n-i)==S.query(m-k+1,m))Add(res,f[1][i][m-2*k]);
}
for(int j=0;j<m;j++){
if(a[0][i+1]==s[j+1])Add(f[0][i+1][j+1],f[0][i][j]);
if(a[1][i+1]==s[j+1])Add(f[1][i+1][j+1],f[1][i][j]);
if(j+2<=m){
if(a[1][i+1]==s[j+1]&&a[0][i+1]==s[j+2])Add(f[0][i+1][j+2],f[1][i][j]);
if(a[0][i+1]==s[j+1]&&a[1][i+1]==s[j+2])Add(f[1][i+1][j+2],f[0][i][j]);
}
}
Add(res,f[0][i][m]),Add(res,f[1][i][m]);
}
return res;
}
int ans;
int main(){
init();
scanf("%s",a[0]+1);
scanf("%s",a[1]+1);
n=strlen(a[0]+1);
pre[0].init(a[0]);
reverse(a[0]+1,a[0]+n+1);
suf[0].init(a[0]);
reverse(a[0]+1,a[0]+n+1);
pre[1].init(a[1]);
reverse(a[1]+1,a[1]+n+1);
suf[1].init(a[1]);
reverse(a[1]+1,a[1]+n+1);
scanf("%s",s+1);
m=strlen(s+1);
S.init(s);
Add(ans,calc(s,0));
if(m==1){cout<<ans;return 0;}
reverse(s+1,s+m+1);
S.init(s);
Add(ans,calc(s,1));
if(m==2){
for(int i=1;i<=n;i++){
if(a[0][i]==s[1]&&a[1][i]==s[2])Dec(ans,1);
if(a[1][i]==s[1]&&a[0][i]==s[2])Dec(ans,1);
}
}
cout<<ans<<'\n';
}
T3:
卷一个
就是相当于要求这样一个东西
先快速插值后多点求值完杜教筛即可
本机跑了交上只跑了
什么鬼
#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 pic pair<int,char>
#define fi first
#define se second
#define pb push_back
#define cs const
#define bg begin
#define poly vector<int>
#define chemx(a,b) ((a)<(b)?(a)=(b):0)
#define chemn(a,b) ((a)>(b)?(a)=(b):0)
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;}
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);}
cs int C=19;
poly w[C+1];
int rev[(1<<20)|5];
inline void init_w(){
for(int i=1;i<=C;i++)w[i].resize(1<<(i-1));
w[C][0]=1;
int wn=ksm(G,(mod-1)/(1<<C));
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];
}
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,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);
if(kd==-1){
reverse(f.bg()+1,f.bg()+lim);
for(int i=0,iv=Inv(lim);i<lim;i++)Mul(f[i],iv);
}
}
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,poly b){
int deg=a.size()+b.size()-1,lim=1;
if(deg<=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;
}
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=a,c.resize(lim>>1);
init_rev(lim);
b.resize(lim),ntt(b,lim,1);
c.resize(lim),ntt(c,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 deg=a.size()-b.size()+1;
reverse(a.bg(),a.end());
reverse(b.bg(),b.end());
a=a*Inv(b,deg),a.resize(deg);
reverse(a.bg(),a.end());
return a;
}
inline poly operator %(poly a,poly b){
if(a.size()<b.size())return a;
a=a-(a/b)*b;
a.resize(b.size()-1);return a;
}
inline poly integ(poly a){
for(int i=0;i<(int)a.size()-1;i++)a[i]=mul(a[i+1],i+1);
a.pop_back();return a;
}
cs int N=1000005;
namespace fastcalc{
poly f[N<<2];
#define lc (u<<1)
#define rc ((u<<1)|1)
#define mid ((l+r)>>1)
void build(int u,int l,int r,int *v){
if(l==r){f[u].clear(),f[u].pb(dec(0,v[l]%mod)),f[u].pb(1);return;}
build(lc,l,mid,v),build(rc,mid+1,r,v);
f[u]=f[lc]*f[rc];
}
void calc(int u,int l,int r,poly g,int *v){
if(l==r){v[l]=g[0];return;}
calc(lc,l,mid,g%f[lc],v),calc(rc,mid+1,r,g%f[rc],v);
}
inline void calc(poly coef,int n,int *v){
build(1,1,n,v);
calc(1,1,n,coef,v);
}
#undef lc
#undef rc
#undef mid
}
namespace interpolation{
poly f[N<<2];
int val[N];
#define lc (u<<1)
#define rc ((u<<1)|1)
#define mid ((l+r)>>1)
void build(int u,int l,int r){
if(l==r){f[u].clear(),f[u].pb(dec(0,l)),f[u].pb(1);return;}
build(lc,l,mid),build(rc,mid+1,r);
f[u]=f[lc]*f[rc];
}
poly calc(int u,int l,int r,int *v){
if(l==r){return poly(1,v[l]);}
return calc(lc,l,mid,v)*f[rc]+f[lc]*calc(rc,mid+1,r,v);
}
inline poly calc(int n,int *v){
build(1,1,n);
for(int i=1;i<=n;i++)val[i]=i;
fastcalc::calc(integ(f[1]),n,val);
for(int i=1;i<=n;i++)val[i]=mul(Inv(val[i]),v[i]);
return calc(1,1,n,val);
}
#undef lc
#undef rc
#undef mid
}
int n,m,tot;
int pr[N],s1[N],s2[N],g1[N],g2[N],mu[N];
int all[N],num,idx[N];
bitset<N> vis;
inline int get(int x){
return x<=N-5?g1[x]:g2[n/x];
}
int du(int x){
if(x<=N-5)return s1[x];
if(vis[n/x])return s2[n/x];
vis[n/x]=1;
int res=1;
for(int i=2,nxt;i<=x;i=nxt+1){
nxt=x/(x/i);
Dec(res,mul(dec(get(nxt),get(i-1)),du(x/i)));
}
return s2[n/x]=res;
}
inline void init(){
cs int len=N-5;
s1[1]=g1[1]=mu[1]=1;
for(int i=2;i<=len;i++){
if(!vis[i])pr[++tot]=i,mu[i]=mod-1,g1[i]=ksm(i,m);
s1[i]=mul(g1[i],mu[i]);
for(int j=1;i*pr[j]<=len&&j<=tot;j++){
vis[i*pr[j]]=1,g1[i*pr[j]]=mul(g1[i],g1[pr[j]]);
if(i%pr[j]==0)break;
mu[i*pr[j]]=mod-mu[i];
}
}
for(int i=2;i<=len;i++)Add(s1[i],s1[i-1]),Add(g1[i],g1[i-1]);
vis.reset();
}
int main(){
init_w();
n=read(),m=read();
init();
if(n>N-5){
for(int i=1,nxt;i<=n;i=nxt+1){
nxt=n/(n/i);
if(nxt>N-5)all[++num]=nxt,idx[num]=nxt;
}
poly res=interpolation::calc(m+2,g1);
fastcalc::calc(res,num,all);
for(int i=1;i<=num;i++)g2[n/idx[i]]=all[i];
}
cout<<du(n);
}