Pagini recente » Welcome! :D | Cod sursa (job #454799) | Cod sursa (job #1085306) | Cod sursa (job #1528752) | Cod sursa (job #2157331)
#include <iostream>
#include<fstream>
using namespace std;
ifstream f("pinex.in");
ofstream g("pinex.out");
int M, pr[79000], nr, x[20], k;
long long p, rez, A, B,divs[20];
bool v[1000001];
void ciur(long long n)
{
int i, j;
v[0] = 1;
v[1] = 1;
for(i = 4; i <= n; i += 2)
v[i] = 1;
for(i = 3; i * i <= n; i += 2)
{
if(v[i] == 0)
for(j = i * i; j <= n; j += i)
v[j] = 1;
}
pr[1] = 2;
nr = 1;
for(int i = 3; i <= n; i += 2)
if(v[i] == 0)
pr[++nr] = i;
}
void afis(int n)
{
int semn = 1;
long long p = 1;
for(int i = 1; i <= n; i++)
if(x[i] == 1)
{
semn *= -1;
p *= divs[i];
}
rez += 1LL * semn * A / p;
}
void back1(int n)
{
k = 1;
x[1] = -1;
while(k > 0)
if(x[k] < 1)
{
x[k]++;
if(k == n)
afis(n);
else
{
k++;
x[k] = -1;
}
}
else
k--;
}
void solutie()
{
int poz = 0;
for(int ind = 1; ind <= nr && 1LL * pr[ind]*pr[ind] <= B; ind++)
if(B % pr[ind] == 0)
{
divs[++poz] = pr[ind];
do
{
B /= pr[ind];
}
while(B % pr[ind] == 0);
}
if(B > 1)
{
divs[++poz] = B;
}
rez = 0;
back1(poz);
}
int main()
{
f >> M;
ciur(1000000);
for(int i = 1; i <= M; i++)
{
f >> A >> B;
solutie();
g << rez << '\n';
}
return 0;
}