Pagini recente » Cod sursa (job #2004346) | Cod sursa (job #1986554) | Cod sursa (job #2040557) | Cod sursa (job #2044845) | Cod sursa (job #1323914)
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
bool c[1000005];
int cc[1000005];
int f[35];
void ciur() {
int n = 1000000, lim = 1000;
c[0] = c[1] = 1;
cc[0] = 1;
cc[1] = 2;
for(int i = 4; i <= n; i += 2)
c[i] = 1;
for(int i = 3; i <= lim; i += 2)
if(!c[i]) {
++ cc[0];
cc[cc[0]] = i;
for(int j = i * i; j <= n; j += 2 * i)
c[j] = 1;
}
}
void desc(long long n) {
int d, e;
//lim = (int) sqrt((double) n);
d = 1;
f[0] = 0;
while(1LL * cc[d] * cc[d] <= n && n > 1) {
e = 0;
while(n % cc[d] == 0) {
++ e;
n /= cc[d];
}
if(e != 0) {
++ f[0];
f[f[0]] = cc[d];
}
++ d;
}
if(n > 1) {
++ f[0];
f[f[0]] = n;
}
}
long long sol(long long a, long long b) {
long long n = 0, cate, prod, m;
desc(b);
m = 1 << f[0];
for(long long i = 1; i < m; ++ i) {
cate = 0;
prod = 1;
for(long long j = 1; j <= f[0]; ++ j) {
if(i & (1 << (j - 1))) {
++ cate;
prod *= f[j];
}
}
if(cate & 1) {
n += a / prod;
} else {
n -= a / prod;
}
}
return a - n;
}
int main() {
freopen("pinex.in", "r", stdin);
freopen("pinex.out", "w", stdout);
int n;
long long a, b;
scanf("%d", &n);
ciur();
for(int i = 1; i <= n; ++ i) {
scanf("%lld%lld", &a, &b);
printf("%lld\n", sol(a, b));
}
return 0;
}