prime multiples
This commit is contained in:
34
CSES - CSES Problem Set/Mathematics/Prime_Multiples.cpp
Normal file
34
CSES - CSES Problem Set/Mathematics/Prime_Multiples.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
// Prime Multiples
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
using ll = long long;
|
||||
|
||||
int main() {
|
||||
ll n, k;
|
||||
cin >> n >> k;
|
||||
vector<ll> primes(k);
|
||||
for (auto &e: primes) cin >> e;
|
||||
|
||||
ll ans = 0;
|
||||
for (ll i = 1; i < (1 << k); i++) {
|
||||
ll prime_product = 1;
|
||||
for (ll j = 0; j < k; j++) {
|
||||
if ( i & (1 << j)) {
|
||||
if (prime_product > n / primes[j]) {
|
||||
prime_product = n + 1;
|
||||
break;
|
||||
}
|
||||
prime_product *= primes[j];
|
||||
}
|
||||
}
|
||||
if (__builtin_popcount(i) & 1) {
|
||||
ans += n / prime_product;
|
||||
} else {
|
||||
ans -= n / prime_product;
|
||||
}
|
||||
}
|
||||
cout << ans << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user