Cod sursa(job #2777687)

Utilizator MihneaCadar101Cadar Mihnea MihneaCadar101 Data 23 septembrie 2021 21:35:35
Problema Curcubeu Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fin("curcubeu.in");
ofstream fout("curcubeu.out");

const int nmax = 1e6;
int n, a[nmax + 5], b[nmax + 5], c[nmax + 5], ans[nmax + 5], t[nmax + 5];
int main()
{
    fin >> n >> a[1] >> b[1] >> c[1];
    t[1] = 1;
    for (int i = 2; i < n; ++i) {
        a[i] = (a[i - 1] * i) % n;
        b[i] = (b[i - 1] * i) % n;
        c[i] = (c[i - 1] * i) % n;
        t[i] = i;
    }

    for (int i = n - 1; i >= 1; --i) {
        int st = a[i], dr = b[i], x = c[i];
        if (st > dr) swap(st, dr);

        while (st <= dr) {
            if (st != t[st]) {
                st = t[++st];
                continue;
            }

            ans[st] = x;
            t[st] = dr + 1;
            if (st + 1 > dr) break;

            st = t[++st];
        }
    }

    for (int i = 1; i < n; ++i) fout << ans[i] << '\n';
    return 0;
}