Cod sursa(job #2224832)

Utilizator linia_intaiConstantinescu Iordache Ciobanu Noi cei din linia intai linia_intai Data 25 iulie 2018 12:08:01
Problema Curcubeu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.34 kb
#include <cstdio>
#include <algorithm>

using namespace std;

class OutputWriter {
public:
    OutputWriter(const char *output_file) {
        out = fopen(output_file, "w");
        cursor = 0;
    }

    ~OutputWriter() {
        flush();
    }


    OutputWriter& operator<<(int nr) {
        char digits[10];
        int cnt = 0;

        do {
            digits[cnt ++] = (nr % 10 + '0');
            nr /= 10;
        } while (nr);

        for (int i = cnt - 1; i >= 0; -- i)
            (*this) << digits[i];
        return (*this);
    }

    OutputWriter& operator<<(const char &ch) {
        if (cursor == SIZE)
            flush();
        buffer[cursor ++] = ch;
        return (*this);
    }

    void flush() {
        fwrite(buffer, 1, cursor, out);
        cursor = 0;
    }
private:
    FILE *out;
    static const int SIZE = (1 << 17);
    char buffer[SIZE];
    int cursor;
};

int a[1000002], b[1000002], c[1000002];
int t[1000002], nxt[1000002], h[1000002];
int ans[1000002];

inline int f(int x) {
  int y = x;
  while (x != t[x])
    x = t[x];
  while (y != t[y]) {
    int aux = t[y];
    t[y] = x;
    y = aux;
  }
  return x;
}

inline void u(int x, int y) {
  x = f(x);
  y = f(y);
  if (x == y)
    return ;
  if (h[y] > h[x])
    swap(x, y);
  t[y] = x;
  if (h[x] == h[y]) ++h[x];
  nxt[x] = max(nxt[x], nxt[y]);
}

int get(int x) {
  if (ans[x + 1] == 0)
    return x + 1;
  return nxt[f(x)];
}

OutputWriter cout("curcubeu.out");

int main() {
  freopen("curcubeu.in", "r", stdin);
  //freopen("curcubeu.out", "w", stdout);

  int n, x, y;
  scanf("%d", &n);
  scanf("%d%d%d", &a[1], &b[1], &c[1]);
  t[1] = 1;
  nxt[1] = 2;
  for (register int i = 2; i < n; ++i) {
    a[i] = 1LL * a[i - 1] * i % n;
    b[i] = 1LL * b[i - 1] * i % n;
    c[i] = 1LL * c[i - 1] * i % n;
    t[i] = i;
    nxt[i] = i + 1;
  }
  for (register int i = n - 1; i >= 1; --i) {
    x = min(a[i], b[i]);
    y = a[i] ^ b[i] ^ x;
    if (ans[x] != 0)
      x = get(x);
    for (register int j = x; j <= y; j = get(j)) {
      ans[j] = c[i];
      if (ans[j - 1] != 0)
        u(j - 1, j);
      if (ans[j + 1] != 0)
        u(j + 1, j);
    }
  }

  for (register int i = 1; i < n; ++i) {
    //printf("%d\n", ans[i]);
    cout << ans[i] << '\n';
  }
  return 0;
}