Small Z dreams disjoint-set

Problem Description

Small Z had a dream, a dream he finally got the coveted German Shepherd, and there are just a lot of small Z counted a total of N only, but unfortunately all of Shepherds actually are white, most small Z I do not like white. So he let all of Shepherds in a row, they are ready to M times dyeing operation. Every time he dyed in a plurality of consecutive Shepherd particular certain color. A final color Collie is the last dyed colors. If a shepherd has not been dyed, its color is white.

Z decided small staining following method: the i-th dyeing operation, the first comprising ( i × A + B ) M O D N + . 1 and ( i × B + A ) M O D N + . 1

Shepherd between all dyed to i, where A, B are two positive integers specific. He wanted to know immediately the end of each color Collie.

Input Format

The first line four positive integers N, M, A, B

Output Format

A total of N output line, the i-th row represents the final color of the i-th Shepherd (color is white if the final outputs 0).

SAMPLE INPUT

4 3 2 4

Sample Output

2
2
3
0

Restrictions and conventions

1N10000001M10000000

1M×A+BM×A+B2311

Time limit: 5s

Space limitations: 128MB

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
const int maxn = 1000000 + 10 ;
int f[maxn],ans[maxn];
int find(int x)
{
    if( !f[x] || f[x]==x ) return f[x]=x;
    return f[x]=find(f[x]);
}
int main()
{
    int n,m,a,b;
    cin>>n>>m>>a>>b;
//    for(int i=1;i<=n;i++) f[n]=i;
    for(int i=m;i;i--)
    {
        int l=(i*a+b)%n+1;
        int r=(i*b+a)%n+1;
        if(l>r) swap(l,r);
        for(int j=find(l);j<=r;j=find(j))
        {
            ans[j]=i;
            f[j]=j+1;
        }
    }
    for(int i=1;i<=n;i++)
        printf("%d\n",ans[i]);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/hfang/p/11240006.html