Cod sursa(job #2641807)

Utilizator popashtefan10Popa Stefan popashtefan10 Data 12 august 2020 18:44:27
Problema Indep Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.53 kb
#include <iostream>
#include <cstdio>

using namespace std;

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


class Huge {
private:
  int cf[MAXCF];

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

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

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

    for(int i = cf[0] + 1; i <= ans.cf[0]; i++) cf[i] = 0;
    for(int i = dr.cf[0] + 1; i <= ans.cf[0]; i++) dr.cf[i] = 0;

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

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

    return ans;
  }

  void print() {
    for(int i = cf[0]; i >= 1; i--)
      printf("%d", 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++) {
      int aux = cmmdc(x, i);
      dp[aux] = dp[aux] + dp[i]; /// adaug x la toate subsirurile cu cmmdc-ul i
    }
    dp[x] = dp[x] + dp[0];
  }

  dp[1].print();

  return 0;
}