Advertisement
Jakube

Untitled

Apr 23rd, 2020
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. long long gcd(long long x, long long y) {
  5.     if (y == 0)
  6.         return x;
  7.     else
  8.         return gcd(y, x%y);
  9. }
  10.  
  11. int main() {
  12.     ios_base::sync_with_stdio(false);
  13.     cin.tie(nullptr);
  14.  
  15.     long long N, K;
  16.     cin >> N >> K;
  17.     vector<long long> a;
  18.     for (int i = 0; i < K; i++) {
  19.         int x;
  20.         cin >> x;
  21.         if (x > 1)
  22.             a.push_back(x);
  23.     }
  24.    
  25.     constexpr int MAX = 1'000'000;
  26.     if (N <= MAX) {
  27.         bitset<MAX + 1> bs;
  28.         for (int i = 1; i <= N; i++)
  29.             bs[i] = true;
  30.         for (auto x : a) {
  31.             for (int idx = 1; idx <= N; idx += x)
  32.                 bs[idx] = false;
  33.         }
  34.         cout << bs.count() << '\n';
  35.     } else {
  36.         long long pigs = N;
  37.         K = a.size();
  38.         for (int mask = 1; mask < (1 << K); mask++) {
  39.             long long lcm = 1;
  40.             int bits = 0;
  41.             for (int i = 0; i < K; i++) {
  42.                 if (mask & (1 << i)) {
  43.                     long long g = gcd(lcm, a[i]);
  44.                     long long new_factor = a[i] / g;
  45.                     if (lcm > N / new_factor) {
  46.                         lcm = N + 1; // essentially to big to have any impact
  47.                     } else {
  48.                         lcm *= new_factor;
  49.                     }
  50.                     bits++;
  51.                 }
  52.             }
  53.             if (bits % 2)
  54.                 pigs -= (N + (lcm - 1)) / lcm;
  55.             else
  56.                 pigs += (N + (lcm - 1)) / lcm;
  57.         }
  58.         cout << pigs << '\n';
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement