# Codeforces Round #542 (Div. 1) 题解

### A

$O(n^2)$暴力贪心即可

#include<ctime>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#define rt register int
#define ll long long
using namespace std;
ll x=0;char zf=1;char ch=getchar();
while(ch!='-'&&!isdigit(ch))ch=getchar();
if(ch=='-')zf=-1,ch=getchar();
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();return x*zf;
}
void write(ll y){if(y<0)putchar('-'),y=-y;if(y>9)write(y/10);putchar(y%10+48);}
void writeln(const ll y){write(y);putchar('\n');}
int k,m,n,x,y,z,cnt,ans;
int sum[20010],far[20010];
int main(){
for(rt i=1;i<=m;i++){
sum[x]++;
if((y+n-x)%n<far[x]||sum[x]==1)far[x]=(y+n-x)%n;
}
for(rt i=1;i<=n;i++){
int ans=0;
for(rt j=1;j<=n;j++)ans=max(ans,(sum[j]-1)*n+far[j]+(j+n-i)%n);
write(ans),putchar(' ');
}
return 0;
}

### B

#include<ctime>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#define rt register int
#define ll long long
using namespace std;
ll x=0;char zf=1;char ch=getchar();
while(ch!='-'&&!isdigit(ch))ch=getchar();
if(ch=='-')zf=-1,ch=getchar();
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();return x*zf;
}
void write(ll y){if(y<0)putchar('-'),y=-y;if(y>9)write(y/10);putchar(y%10+48);}
void writeln(const ll y){write(y);putchar('\n');}
int k,m,n,x,y,z,cnt,ans;
void pt(int x,int y){
for(rt i=1;i<=y;i++)if(x<=1000000)cout<<x<<' ',x=0;
else cout<<1000000<<' ',x-=1000000;
}
int main(){
int S=k+1927,L=1926;
cout<<L+1<<endl;
cout<<-1<<' ';pt(S,L);
return 0;
}

### C

#include<ctime>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#define p 1000000007
#define rt register int
#define ll long long
using namespace std;
ll x=0;char zf=1;char ch=getchar();
while(ch!='-'&&!isdigit(ch))ch=getchar();
if(ch=='-')zf=-1,ch=getchar();
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();return x*zf;
}
void write(ll y){if(y<0)putchar('-'),y=-y;if(y>9)write(y/10);putchar(y%10+48);}
void writeln(const ll y){write(y);putchar('\n');}
int k,m,n,x,y,z,cnt,ans,Root;
struct trie{
int son[2],fa;
}a[9000010];
int v[3010],dp[9000010];
void insert(int &x,int pl){
if(!x)x=++cnt;if(pl==0)return;
bool fla=0;int val=0;
if(!a[x].son[v[pl]])a[x].son[v[pl]]=++cnt;a[a[x].son[v[pl]]].fa=x;
if(!dp[a[x].son[v[pl]]]){
int now=x,zhi=0;
for(rt i=0;i<4;i++){
if(!now)continue;
zhi|=(1<<i)*(v[pl+i]);
if((zhi==12||zhi==10||zhi==7||zhi==15)&&i==3)break;
(val+=dp[now])%=p;now=a[now].fa;
}
dp[a[x].son[v[pl]]]=val,(ans+=val)%=p;
}
insert(a[x].son[v[pl]],pl-1);
}
int main(){
for(rt i=1;i<=n;i++){
insert(Root,i);
writeln(ans);
}
return 0;
}

### D

$f_{x,y}$表示第$x$个块，所有$S$值不超过$y$的$dp$值之和

#include<ctime>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#define p 998244353
#define rt register int
#define ll long long
using namespace std;
ll x=0;char zf=1;char ch=getchar();
while(ch!='-'&&!isdigit(ch))ch=getchar();
if(ch=='-')zf=-1,ch=getchar();
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();return x*zf;
}
void write(ll y){if(y<0)putchar('-'),y=-y;if(y>9)write(y/10);putchar(y%10+48);}
void writeln(const ll y){write(y);putchar('\n');}
int k,m,n,x,y,z,cnt,ans;
int la[100010],now[100010],v[100010],s[100010],dp[100010];
int f[400][100010],tag[350],blo,maxs[350],id[100010];
void build(int x){
int Min=1000000000,Max=-1000000000;
for(rt i=x*blo;i<(x+1)*blo;i++){
if(s[i]<Min)Min=s[i];
if(s[i]>Max)Max=s[i];
}
maxs[x]=Max-Min;
for(rt i=0;i<=maxs[x];i++)f[x][i]=0;
tag[x]+=Min;
for(rt i=x*blo;i<(x+1)*blo;i++)s[i]-=Min,(f[x][s[i]]+=dp[i])%=p;
for(rt i=1;i<=Max-Min;i++)(f[x][i]+=f[x][i-1])%=p;

}
void change(int L,int val,int R){
if(val==v[L])return;
int upd=val-v[L];v[L]=val;
if(id[L]==id[R]){
for(rt i=L;i<=R;i++)s[i]+=upd;
return;
}
for(rt i=L;i<(id[L]+1)*blo;i++)s[i]+=upd;build(id[L]);
for(rt i=id[L]+1;;i++){
if(i==id[R]){
for(rt j=i*blo;j<=R;j++)s[j]+=upd;
return;
}
tag[i]+=upd;
}
}

int query(int x,int val){
//块x中大于等于val的值
val-=tag[x];
if(val>maxs[x])return 0;
if(val<=0)return f[x][maxs[x]];
return (f[x][maxs[x]]-f[x][val-1])%p;
}
int main(){
for(rt i=0;i<n;i++)id[i]=i/blo;
for(rt i=1;i<=n;i++)now[i]=la[i]=-1;
for(rt i=0;i<n;i++){
if(now[x]!=-1)change(now[x],-1,i);
if(la[x]!=-1)change(la[x],0,i);
la[x]=now[x];now[x]=i;v[i]=1;s[i]++;
for(rt j=0;j<=i;j+=blo)(dp[i]+=query(id[j],s[i]+tag[id[i]]-k))%=p;
for(rt j=i/blo*blo;j<i;j++)if(s[j]+tag[id[j]]>=s[i]+tag[id[i]]-k)(dp[i]+=dp[j])%=p;
if(s[i]<=k)dp[i]++;
if((i+1)%blo==0)build(id[i]);
}
cout<<(dp[n-1]%p+p)%p;
return 0;
}

### E

#include<ctime>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#define flush fflush(stdout)
#define rt register int
#define ll long long
using namespace std;
ll x=0;char zf=1;char ch=getchar();
while(ch!='-'&&!isdigit(ch))ch=getchar();
if(ch=='-')zf=-1,ch=getchar();
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();return x*zf;
}
void write(ll y){if(y<0)putchar('-'),y=-y;if(y>9)write(y/10);putchar(y%10+48);}
void writeln(const ll y){write(y);putchar('\n');}
int k,m,n,x,y,z,cnt,ans;
int size[505];
struct node{
int x,size;
bool operator <(const node s)const{
return size<s.size;
}
}a[505];
int q[505],t;
set<int>s;
int erase[505],fa[505],top;
bool chk(int L,int R,int nd){
writeln(1);writeln(1);
writeln(R-L+1);
for(rt i=L;i<=R;i++)write(q[i]),putchar(' ');putchar('\n');
writeln(nd);flush;
}
int main(){
for(rt i=2;i<=n;i++){
writeln(1);
writeln(1);
writeln(n-1);
for(rt j=2;j<=n;j++)write(j),putchar(' ');putchar('\n');
writeln(i);flush;
}
for(rt i=1;i<=n;i++)a[i]={i,size[i]};
sort(a+1,a+n+1);
for(rt i=1;i<=n;i++){
t=0;
for(auto i:s)q[++t]=i;
for(rt lef=1;lef<=t;){
if(!chk(lef,t,a[i].x))break;
int L=lef,R=t,ans=0;
while(L<=R){
const int mid=L+R>>1;
if(chk(L,mid,a[i].x))ans=mid,R=mid-1;
else L=mid+1;
}
lef=ans+1;fa[q[ans]]=a[i].x;erase[++top]=q[ans];
}
for(rt i=top;i>=1;i--)s.erase(erase[i]);top=0;
s.insert(a[i].x);
}
}