Pagini recente » Cod sursa (job #2447783) | Cod sursa (job #954141) | Cod sursa (job #2290761) | Cod sursa (job #977426) | Cod sursa (job #682894)
Cod sursa(job #682894)
#include <cstdio>
#include <bitset>
#define XMax 1000001
#define LL long long
using namespace std;
LL A, B, S;
bitset <XMax> V;
int Primes[XMax], F[XMax];
void Eratosthenes ()
{
Primes[++Primes[0]]=2;
for (int i=3; i<XMax; i+=2)
{
if (!V[i])
{
Primes[++Primes[0]]=i;
if (1LL*i*i>=XMax) continue;
for (int j=i*i; j<XMax; j+=(i+i))
{
V[j]=1;
}
}
}
}
void Decompose ()
{
F[0]=0;
for (int i=1; i<=Primes[0] and B>1; ++i)
{
if (B%Primes[i]==0)
{
F[++F[0]]=Primes[i];
for (; B%Primes[i]==0; B/=Primes[i]);
}
}
if (B>1)
{
F[++F[0]]=B;
}
}
void Pinex ()
{
S=0;
for (int Conf=1; Conf<(1<<F[0]); ++Conf)
{
int Sign=-1; LL X=1;
for (int i=0; i<F[0]; ++i)
{
if (Conf&(1<<i))
{
Sign=-Sign;
X*=F[i+1];
}
}
S+=(Sign*(A/X));
}
S=A-S;
}
int main()
{
freopen ("pinex.in", "r", stdin);
freopen ("pinex.out", "w", stdout);
Eratosthenes ();
int T;
scanf ("%d", &T);
for (; T>0; --T)
{
scanf ("%lld %lld", &A, &B);
Decompose ();
Pinex ();
printf ("%lld\n", S);
}
return 0;
}