神仙题orzorzorz
每个点只会被最后一次染色到,倒着做就行了
#include<bits/stdc++.h>
#define il inline
#define vd void
typedef long long ll;
il int gi(){
int x=0,f=1;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-')f=-1;
ch=getchar();
}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return x*f;
}
int fa[1000100];
int col[1000100];
int l[10000100],r[10000100];
il int hd(int x){return fa[x]==x?x:fa[x]=hd(fa[x]);}
il vd Union(int a,int b){fa[hd(a)]=hd(b);}
int main(){
#ifndef ONLINE_JUDGE
freopen("2054.in","r",stdin);
freopen("2054.out","w",stdout);
#endif
int n=gi(),m=gi(),p=gi(),q=gi();
for(int i=1;i<=m;++i){
l[i]=(i*p+q)%n+1,r[i]=(i*q+p)%n+1;
if(l[i]>r[i])std::swap(l[i],r[i]);
}
for(int i=1;i<=n+1;++i)fa[i]=i;
for(int i=m;i;--i)
for(int j=hd(l[i]);j<=r[i];j=hd(j))
col[j]=i,Union(j,j+1);
for(int i=1;i<=n;++i)printf("%d
",col[i]);
return 0;
}