Cod sursa(job #1980105)

Utilizator GoogalAbabei Daniel Googal Data 12 mai 2017 12:36:45
Problema Curcubeu Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <fstream>

using namespace std;

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

const int NMAX = 1000000 + 20;
const int DIM = 100000;

int n, a[NMAX], b[NMAX], c[NMAX], k[NMAX], v[NMAX], poz;
char buff[DIM];

int read(){
  int x = 0;

  while(isdigit(buff[poz]) == 0)
    if(++poz == DIM){
      in.read(buff, DIM);
      poz = 0;
    }

  while(isdigit(buff[poz]) != 0){
    x = x * 10 + (buff[poz] - '0');
    if(++poz == DIM){
        in.read(buff, DIM);
        poz = 0;
      }
    }

  return x;
}

inline int src(int x){
  while(k[x] != 0)
    x = k[x];
  return x;
}

void reunion(int a, int b, int c){
  if(a > b)
    swap(a, b);
  while(a <= b){
    if(v[a] != 0)
      a = src(a);
    if(a <= b){
      v[a] = c;
      k[a] = b + 1;
      a++;
    }
  }
}

int main()
{
  n = read();
  a[1] = read();
  b[1] = read();
  c[1] = read();
  //in >> n >> a[1] >> b[1] >> c[1];
  in.close();

  for(int i = 2; i < n; i++){
    a[i] = ((long long)a[i - 1] * i) % n;
    b[i] = ((long long)b[i - 1] * i) % n;
    c[i] = ((long long)c[i - 1] * i) % n;
  }

  for(int i = n - 1; i >= 1; i--)
    reunion(a[i], b[i], c[i]);

  for(int i = 1; i < n; i++)
    out << v[i] << '\n';
  out.close();
  return 0;
}