Cod sursa(job #2017812)

Utilizator cella.florescuCella Florescu cella.florescu Data 2 septembrie 2017 17:22:48
Problema Indep Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.05 kb
#include <bits/stdc++.h>

using namespace std;

const int BASE = 1e1;
const int MAXN = 500;
const int DIG = 500;
const int MAXVAL = 1e3;

struct Huge {
  short v[DIG + 1];
  Huge () {
    memset(v, 0, sizeof v);
    v[0] = 1;
  }
  void operator *= (int other) {
    int t = 0, i;
    for (i = 1; i <= v[0] || t > 0; ++i, t /= BASE)
      v[i] = (t += other * v[i]) % BASE;
    v[0] = i - 1;
  }
  void operator += (int other) {
    int i, t = other;
    for (i = 1; i <= v[0] || t > 0; ++i, t /= BASE)
      v[i] = (t += v[i]) % BASE;
    v[0] = i - 1;
  }
  void operator += (Huge &other) {
    int lim = max(v[0], other.v[0]), t = 0, i;
    for (i = 1; i <= lim || t > 0; ++i, t /= BASE)
      v[i] = (t += v[i] + other.v[i]) % BASE;
    v[0] = i - 1;
  }
  void operator -= (Huge &other) {
    int i, t = 0;
    for (i = 1; i <= v[0]; ++i) {
      v[i] -= (i <= other.v[0] ? other.v[i] : 0) + t;
      v[i] += (t = (v[i] < 0)) * 10;
    }
    while (v[v[0]] == 0 && v[0] > 1)
      --v[0];
  }
} p2[MAXN + 1], ans;


int v[MAXN], pinex[MAXVAL + 1];

int main()
{
    ifstream fin("indep.in");
    ofstream fout("indep.out");
    int n;
    fin >> n;
    for (int i = 0; i < n; ++i)
      fin >> v[i];
    p2[1].v[0] = p2[1].v[1] = 1;
    for (int i = 2; i <= n; ++i) {
      p2[i] = p2[i - 1];
      p2[i] *= 2;
      p2[i] += 1;
    }
    ans = p2[n];
    for (int i = 2; i <= MAXVAL; ++i)
      if (pinex[i] == 0)
        for (int j = i; j <= MAXVAL; j += i)
          ++pinex[j];
    for (int i = 2; i * i <= MAXVAL; ++i)
      for (int j = 1; j * i * i <= MAXVAL; ++j)
        pinex[i * i * j] = 0;
    for (int i = 2; i <= MAXVAL; ++i)
      if (pinex[i]) {
        int sum = 0;
        for (int j = 0; j < n; ++j)
          sum += (v[j] % i == 0);
        if (sum) {
          if (pinex[i] & 1)
            ans -= p2[sum];
          else
            ans += p2[sum];
        }
      }
    for (int i = ans.v[0]; i > 0; --i)
      fout << ans.v[i];
    fin.close();
    fout.close();
    return 0;
}