Cod sursa(job #587635)

Utilizator dushmiMihai-Alexandru Dusmanu dushmi Data 5 mai 2011 14:36:49
Problema Curcubeu Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <cstdio>
#include <algorithm>

using namespace std;

const int N = 1000005;

int n, v[N], next[N], cul[N], a[N], b[N], c[N];

void read() {
  scanf("%d%d%d%d", &n, &a[1], &b[1], &c[1]);
}

void init() {
  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;

    if (a[i] > b[i])
      swap(a[i], b[i]);
  }

  for (int i = 1; i < n; ++ i)
    next[i] = i;

  next[n] = n;
}

inline int find_next(int poz) {
  int nr = 0;
  v[++ nr] = poz;

  while (next[poz] != poz) {
    v[++ nr] = poz;
    poz = next[poz];
  }

  int pf = poz;

  for (int i = 1; i <= nr; ++ i)
    next[v[i]] = pf;

  return next[poz];
}

void solve() {
  for (int i = n - 1; i >= 1; -- i)
    for (int j = find_next(a[i]); j <= b[i]; j = next[j]) {
      cul[j] = c[i];
      next[j] = find_next(j + 1);
    }
}

void afis() {
  for (int i = 1; i < n; ++ i)
    printf("%d\n", cul[i]);
}

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

  read();
  init();
  solve();
  afis();

  return 0;
}