Cod sursa(job #763857)
Program curcubeu;
var a,b,c:array [1..1000001] of int64;
e,d:array [1..1000001] of longint;
b1:array [1..1 shl 17] of integer;
i,j,aux,n:longint;
fi,fo:text;
begin
assign(fi,'curcubeu.in');
assign(fo,'curcubeu.out');
settextbuf(fo,b1);
reset(fi); rewrite(fo); readln(fi,n,a[1],b[1],c[1]); d[1]:=1;
for i:=2 to n-1 do begin
aux:=i;
a[i]:=(int64(a[i-1]*aux)) mod n;
b[i]:=(int64(b[i-1]*aux)) mod n;
c[i]:=(int64(c[i-1]*aux)) mod n;
d[i]:=i;
if a[i]>b[i] then begin aux:=a[i]; a[i]:=b[i]; b[i]:=aux; end;
end;
for i:=n-1 downto 1 do begin
j:=a[i];
while j<=b[i] do
if e[j]=0 then begin e[j]:=c[i]; d[e[j]]:=j; inc(j); end
else j:=d[e[j]]+1;
end;
{for i:=1 to n-1 do writeln(fo,e[i]); }
close(fo);
end.