Pagini recente » Cod sursa (job #1933423) | Cod sursa (job #2265555) | Cod sursa (job #2779289) | Cod sursa (job #2589239) | Cod sursa (job #1821593)
#include <cstdio>
using namespace std;
int i,j,nr,nr1,nr2,nr3,b[1000004],c[104],n,x1;
long long x,p,nr4,y;
bool a[1000004];
int main()
{
freopen ("pinex.in","r",stdin);
freopen ("pinex.out","w",stdout);
scanf ("%d", &n);
b[++nr]=2;
for (i=3;i<=1000000;i+=2)
{
if (a[i]==false)
{
b[++nr]=i;
for (j=i;j<=(1000000/i);j+=2)
a[j*i]=true;
}
}
for (i=1;i<=n;i++)
{
scanf ("%lld %lld", &x, &y);
nr1=0;
nr4=0LL+x;
for (j=1;j<=nr;j++)
{
if ((1LL*y%b[j])==0)
c[++nr1]=b[j];
while ((1LL*y%b[j])==0)
y=1LL*y/b[j];
if (y<b[j])
break;
}
if (y>1)
c[++nr1]=y;
for (j=1;j<(1<<nr1);j++)
{
x1=j;
p=1LL;
nr2=1;
nr3=0;
while (x1>0)
{
if (x1%2==1)
{
nr3++;
p=1LL*p*c[nr2];
}
x1/=2;
nr2++;
}
if (nr3%2==1)
nr4=0LL+nr4-1LL*x/p;
else
nr4=0LL+nr4+1LL*x/p;
}
printf ("%lld\n", nr4);
}
return 0;
}