Pagini recente » Cod sursa (job #13183) | Cod sursa (job #2220154) | Cod sursa (job #1255216) | Cod sursa (job #2951230) | Cod sursa (job #2979818)
//https://infoarena.ro/problema/pinex
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("pinex.in");
ofstream fout("pinex.out");
vector <int> primes;
vector <int> bPrimeFactors;
vector <int> currentSubset;
vector <pair<int, int>> productSize;
bool notPrime[1000005];
void sieve(int n) {
notPrime[0] = notPrime[1] = true;
for (int i = 2; i <= n; ++i) {
if (notPrime[i] == false) {
primes.push_back(i);
for (int j = 2 * i; j <= n; j += i) {
notPrime[j] = true;
}
}
}
}
void primeFactoring (int B) {
for (int i = 0; i < primes.size(); ++i) {
if (primes * primes > B) break;
if (B % primes[i] == 0) {
bPrimeFactors.push_back(primes[i]);
while (B % primes[i] == 0) {
B /= primes[i];
}
}
}
if (B != 1) {
bPrimeFactors.push_back(B);
}
}
void subsets(int N, int lastValue) {
int product = 1;
if (!currentSubset.empty()) {
for (int i = 0; i < currentSubset.size(); ++i) {
product *= currentSubset[i];
}
productSize.push_back({product, currentSubset.size()});
}
for (int i = lastValue; i < N; ++i) {
currentSubset.push_back(bPrimeFactors[i]);
subsets(N, i + 1);
currentSubset.pop_back();
}
}
int main()
{
int numberTests;
sieve(1000000);
fin >> numberTests;
for (int i = 1; i <= numberTests; ++i) {
int A, B;
fin >> A >> B;
int ans = 0;
primeFactoring(B);
subsets(bPrimeFactors.size(), 0);
for (int j = 0; j < productSize.size(); ++j) {
int cnt = 0;
for (int i = 1; i <= A; ++i) {
if (i % productSize[j].first == 0) {
cnt += 1;
}
}
if (productSize[j].second % 2 == 0) {
ans -= cnt;
} else {
ans += cnt;
}
}
fout << A - ans << "\n";
bPrimeFactors.clear();
currentSubset.clear();
productSize.clear();
}
}