Cod sursa(job #764281)
#include <iostream>
#include <fstream>
using namespace std;
#define nmax 1000000
ifstream f("curcubeu.in");
ofstream g("curcubeu.out");
struct{int a,b,c;}a[nmax];
int n, next[nmax], rez[nmax];
void citeste(){
f >> n >> a[1].a >> a[1].b >> a[1].c;
for(int i=2; i<n; i++){
a[i].a = (a[i-1].a*i)%n;
a[i].b = (a[i-1].b*i)%n;
a[i].c = (a[i-1].c*i)%n;
}
}
void rezolva(){
for(int i=1; i<=n; i++) next[i]=i;
for(int i=n-1; i>=1; i--){
int st = min(a[i].a,a[i].b);
int dr = max(a[i].a,a[i].b);
for(int j=next[st]; j<=dr; j=next[j]+1){
rez[j] = a[i].c;
}
int x = st;
while(x <= dr){
int aux = next[x]+1;
next[x] = max(dr,next[x]);
x = aux;
}
}
for(int i=1; i<n; i++)g<<rez[i] << "\n";
}
int main(){
citeste();
rezolva();
}