Cod sursa(job #2083298)

Utilizator amaliarebAmalia Rebegea amaliareb Data 7 decembrie 2017 14:56:03
Problema Curcubeu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.21 kb
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>

using namespace std;
ifstream f("curcubeu.in");

using namespace std;

class OutParser {
private:
    FILE *fout;
    char *buff;
    int sp;

    void write_ch(char ch) {
        if (sp == 50000) {
            fwrite(buff, 1, 50000, fout);
            sp = 0;
            buff[sp++] = ch;
        } else {
            buff[sp++] = ch;
        }
    }


public:
    OutParser(const char* name) {
        fout = fopen(name, "w");
        buff = new char[50000]();
        sp = 0;
    }
    ~OutParser() {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }

    OutParser& operator << (int vu32) {
        if (vu32 <= 9) {
            write_ch(vu32 + '0');
        } else {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (long long vu64) {
        if (vu64 <= 9) {
            write_ch(vu64 + '0');
        } else {
            (*this) << (vu64 / 10);
            write_ch(vu64 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (char ch) {
        write_ch(ch);
        return *this;
    }
    OutParser& operator << (const char *ch) {
        while (*ch) {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
};
OutParser g("curcubeu.out");
const int MaxN = 1000005;
int cols[MaxN], st[MaxN], dr[MaxN], v[MaxN], n, fth[MaxN];

inline int find_interval(int val)
{
    int aux, rez;
    aux = val;
    while (fth[val] != 0)
        val = fth[val];
    rez = val;
    val = aux;
    while (val != rez)
    {
        aux = fth[val];
        fth[val] = rez;
        val = aux;
    }
    return rez;
}

inline void join(int a, int b)
{
    if (a != b) fth[b] = a;
}

int main()
{
    f >> n >> st[1] >> dr[1] >> cols[1];
    for (int i = 2; i <= n; i++)
    {
        st[i] = (1LL * st[i - 1] * i) % n;
        dr[i] = (1LL * dr[i - 1] * i) % n;
        cols[i] = (1LL * cols[i - 1] * i) % n;
    }
    --n;
    for (int i = n; i >= 1; i--)
    {
        if (st[i] > dr[i]) swap(st[i], dr[i]);
        int poz = st[i];
        int auxst = st[i], auxdr = dr[i];
        while (poz <= dr[i])
        {
            while (v[poz] == 0 && poz <= dr[i])
                v[poz] = i, ++poz;
            if (poz <= dr[i] && v[poz])
            {
                int x = find_interval(v[poz]);
                poz = dr[x] + 1;
                join(i, x);
                auxst = min(st[x], auxst);
                auxdr = max(dr[x], auxdr);
            }
        }
        if (st[i] >= 2 && v[st[i] - 1] > 0)
        {
            int x = find_interval(v[st[i] - 1]);
            join(i, x);
            auxst = min(st[x], auxst);
            auxdr = max(dr[x], auxdr);
        }
        if (dr[i] < n && v[dr[i] + 1] > 0)
        {
            int x = find_interval(v[dr[i] + 1]);
            join(i, x);
            auxst = min(st[x], auxst);
            auxdr = max(dr[x], auxdr);
        }
        st[i] = auxst;
        dr[i] = auxdr;
    }
    for (int i = 1; i <= n; i++) g << cols[v[i]] << '\n';
    return 0;
}