Cod sursa(job #532052)
// http://infoarena.ro/problema/curcubeu
#include <fstream>
#include <cstdio>
using namespace std;
#define maxSize 6
ifstream in("curcubeu.in");
// cu ofstream nu intra in timpul de executie
FILE *out=fopen("curcubeu.out","wt");
struct stuff {
int start,stop;
int color;
};
int nrHouses;
int following[maxSize]; // retine urmatoarea pozitie de vopsit
int house[maxSize]; // retine culorile
stuff toPaint[maxSize]; // retine operatiile de colorare
int main() {
in >> nrHouses >> toPaint[1].start >> toPaint[1].stop >> toPaint[1].color;
int aux;
aux = max(toPaint[1].start,toPaint[1].stop);
toPaint[1].start = min(toPaint[1].start,toPaint[1].stop);
toPaint[1].stop = aux;
for(int i=2;i<=nrHouses;i++) {
toPaint[i].start = ((long long)toPaint[i-1].start * i) % nrHouses;
toPaint[i].stop = ((long long)toPaint[i-1].stop * i) % nrHouses;
toPaint[i].color = ((long long)toPaint[i-1].color * i) % nrHouses;
aux = max(toPaint[i].start,toPaint[i].stop);
toPaint[i].start = min(toPaint[i].start,toPaint[i].stop);
toPaint[i].stop = aux;
}
// initial de la fiecare casa se trece la urmatoarea
for(int i=1;i<=nrHouses;i++)
following[i] = i + 1;
// se iau casele in ordine inversa
// (odata vopsita o casa nu va mai fi revopsita)
for(int i=nrHouses-1;i>=1;i--)
for(int k=toPaint[i].start;k<=toPaint[i].stop;) {
if(!house[k])
house[k] = toPaint[i].color;
int aux = k;
k = following[k]; // se va trece la urmatoarea pozitie
// fiind vopsita, se vrea evitarea vopsirii acesteia din nou
// (urmatoarea pozitie va indica spre sfarsitul intervalului de vopsit + 1)
following[aux] = toPaint[i].stop + 1;
}
for(int i=1;i<=nrHouses-1;i++)
fprintf(out,"%d\n",house[i]);
in.close();
return (0);
}