Cod sursa(job #2224830)

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

using namespace std;

class OutputWriter {
public:
    OutputWriter(const char *out_file) {
        file = fopen(out_file, "w");
    }
    inline void operator<<(const int &number) {
        char p[15];
        int copy = number, digits = 0;
        do {
            p[digits ++] = '0' + copy % 10;
            copy /= 10;
        }while(copy);
        for(int i = digits - 1; i >= 0; -- i) {
            (*this) << p[i];
        }
    }
    inline void operator<<(const char &ch) {
        buffer[cursor ++] = ch;
        if(cursor == size) {
            cursor = 0;
            fwrite(buffer, size, 1, file);
        }
    }
    inline void flush() {
        fwrite(buffer, cursor, 1, file);
        cursor = 0;
    }
private:
    static const int MAX_BUFFER_SIZE = 1 << 17;
    FILE *file;
    int cursor, size;
    char buffer[MAX_BUFFER_SIZE];
};

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)];
}

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

  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];
    cout << '\n';
  }
  cout.flush();
  return 0;
}