Cod sursa(job #2641869)

Utilizator popashtefan10Popa Stefan popashtefan10 Data 12 august 2020 22:36:42
Problema Indep Scor 25
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <iostream>
#include <cstdio>

using namespace std;

const int MAXCF = 350;
const int MAX = 1000;
const int BASE = 10000;


class Huge {
private:
  int cf[MAXCF];

public:
  Huge(int x = 0) {
    cf[0] = 0;
    if(!x)
      cf[0] = 1;
    while(x) {
      cf[0]++;
      cf[cf[0]] = x % BASE;
      x /= BASE;
    }
  }

  const Huge operator = (const Huge &dr) {
    for(int i = 0; i <= dr.cf[0]; i++)
      cf[i] = dr.cf[i];
    return *this;
  }

  bool is0() {
    return !cf[0];
  }

  const Huge operator += (const Huge &dr) {
    Huge ans = Huge();
    cf[0] = max(cf[0], dr.cf[0]);

    int r = 0;
    for(int i = 1; i <= cf[0]; i++) {
      cf[i] = cf[i] + dr.cf[i] + r;
      r = cf[i] / BASE;
      cf[i] %= BASE;
    }

    if(r)
      cf[++cf[0]] = r;

    return ans;
  }

  void print() {
    for(int i = cf[0]; i >= 1; i--) {
      int aux = cf[i];
      while(aux)
        aux /= BASE;

      const char *format = (i == 1) ? "%d" : "%04d";
      printf(format, cf[i]);
    }
  }
};


Huge dp[MAX + 5];

int cmmdc(int a, int b) {
  int r = 0;

  while(b) {
    r = a % b;
    a = b;
    b = r;
  }

  return a;
}

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

  scanf("%d", &n);
  dp[0] = 1;

  for(int i = 1; i <= n; i++) {
    scanf("%d", &x);
    for(int i = 1; i <= MAX; i++) {
      if(dp[i].is0())
        continue;
      int aux = cmmdc(x, i);
      dp[aux] += dp[i]; /// adaug x la toate subsirurile cu cmmdc-ul i
    }
    dp[x] += dp[0];
  }

  dp[1].print();

  return 0;
}