Cod sursa(job #2892178)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 21 aprilie 2022 00:24:20
Problema Light2 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("light2.in");
ofstream fout("light2.out");

const int kM = 1 << 5;
int64_t n, ans;
int m, a[kM];

int64_t lcm(int64_t x, int64_t y) {
  return x / __gcd(x, y) * y;
}

void solve(int pas, int64_t num, int cnt) {
  if (pas == m) {
    if (cnt % 2 == 1) {
      ans += (n / num) * (1 << (cnt - 1));
    } else if (cnt) {
      ans -= (n / num) * (1 << (cnt - 1));
    }
    return;
  }
  solve(pas + 1, num, cnt);
  num = lcm(num, a[pas]);
  if (num <= n) {
    solve(pas + 1, num, cnt + 1);
  }
}

void testCase() {
  fin >> n >> m;
  for (int i = 0; i < m; ++i) {
    fin >> a[i];
  }
  sort(a, a + m, greater<int>());
  solve(0, 1, 0);
  fout << ans << '\n';
}

int main() {
  int tests = 1;
  for (int tc = 1; tc <= tests; ++tc) {
    testCase();
  }
  fin.close();
  fout.close();
  return 0;
}