Cod sursa(job #1793877)

Utilizator TincaMateiTinca Matei TincaMatei Data 31 octombrie 2016 17:13:27
Problema Indep Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.19 kb
#include <cstdio>
#include <algorithm>

const int MAX_N = 500;
const int MAX_NR = 1000;
const int MAX_CIFRE = 150;

class Huge {
public:
  int n;
  char cf[MAX_CIFRE];
  Huge() {
    int i;
    for(i = 0; i < MAX_CIFRE; i++)
      cf[i] = 0;
    n = 1;
  }
  Huge(int x) {
    int i;
    while(x > 0) {
      cf[i] = x % 10;
      x = x / 10;
      i++;
    }
    n = i;
    while(i < MAX_CIFRE) {
      cf[i] = 0;
      i++;
    }
  }
  Huge operator+(int x) {
    Huge rez(0);
    int tr, i;
    i = 0;
    tr = 0;
    while(i < this->n || x > 0 || tr > 0) {
      tr = (tr + this->cf[i] + x % 10);
      rez.cf[i] = tr % 10;
      x = x / 10;
      tr = tr / 10;
      i++;
    }
    rez.n = i;
    return rez;
  }
  Huge operator+(Huge x) {
    Huge rez(0);
    int tr, i;
    i = 0;
    tr = 0;
    while(i < this->n || i < x.n || tr > 0) {
      tr = (tr + this->cf[i] + x.cf[i]);
      rez.cf[i] = tr % 10;
      tr = tr / 10;
      i++;
    }
    rez.n = i;
    return rez;
  }
  Huge operator*(int x) {
    Huge rez(0);
    int tr, i;
    tr = 0;
    while(i < this->n || tr > 0) {
      tr = (tr + this->cf[i] * x);
      rez.cf[i] = tr % 10;
      tr = tr / 10;
      i++;
    }
    rez.n = i;
    return rez;
  }
};

void putHuge(FILE *fout, Huge a) {
  int i;
  i = MAX_CIFRE - 1;
  while(i > 0 && a.cf[i] == 0)
    i--;
  while(i >= 0) {
    fprintf(fout, "%d", a.cf[i]);
    i--;
  }
}

Huge d[1+MAX_NR];
int gcd[1+MAX_NR][1+MAX_NR];
int v[MAX_N];

int getGcd(int a, int b) {
  if(gcd[a][b] == 0) {
    if(a % b == 0)
      gcd[a][b] = gcd[b][a] = b;
    else
      gcd[a][b] = gcd[b][a] = getGcd(b, a % b);
  }
  return gcd[a][b];
}

int main() {
  int n, x;

  for(int i = 1; i <= MAX_NR; ++i)
    for(int j = 1; j <= MAX_NR; ++j)
      getGcd(i, j);

  FILE *fin = fopen("indep.in", "r");
  fscanf(fin, "%d", &n);
  for(int i = 0; i < n; ++i)
    fscanf(fin, "%d", &v[i]);
  std::sort(v, v + n);
  for(int i = 0; i < n; ++i) {
    x = v[i];
    for(int g = 1; g <= x; ++g) {
      for(int j = g; j <= x; j = j + g) {
        if(gcd[j][x] == g)
          d[g] = d[g] + d[j];
      }
    }
    d[x] = d[x] + 1;
  }
  fclose(fin);
  FILE *fout = fopen("indep.out", "w");
  putHuge(fout, d[1]);
  fclose(fout);
  return 0;
}