Cod sursa(job #176321)
#include<stdio.h>
long long n, nr, x[1302], m[1302];
typedef struct
{
long long a, b, c;
} culoare;
culoare v[1302];
inline long long min(long long x, long long y) { return x < y ? x : y;}
inline long long max(long long x, long long y) { return x > y ? x : y;}
int main()
{
freopen("curcubeu.in","r",stdin);
freopen("curcubeu.out","w",stdout);
scanf("%lld",&n);
scanf("%lld %lld %lld",&v[1].a, &v[1].b, &v[1].c);
long i, j, xx, yy;
nr = n - 1; m[1] = 1;
for (i = 2; i < n; i++)
{
v[i].a = ((v[i - 1].a % n) * (i % n)) % n;
v[i].b = ((v[i - 1].b % n) * (i % n)) % n;
v[i].c = ((v[i - 1].c % n) * (i % n)) % n;
m[i] = i;
}
for (i = n - 1; i >= 1; i--)
{
xx = min(v[i].a, v[i].b);
yy = max(v[i].a, v[i].b);
while (m[xx] != xx && m[xx] < n) xx = m[xx];
for (j = xx; j <= yy; j++)
{
while (m[j] != j && j < n) j = m[j];
if (!x[j] && j < n) { x[j] = v[i].c; m[j] = m[yy] + 1; nr--;}
if (!nr) break;
}
if (!nr) break;
}
for (i = 1; i < n; i++) printf("%lld\n",x[i]);
return 0;
}