Pagini recente » Cod sursa (job #2771465) | Cod sursa (job #2102527) | Cod sursa (job #835565) | Cod sursa (job #2011205) | Cod sursa (job #1949427)
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#define MaxN 1000005
#define MaxP 20
using namespace std;
FILE*IN,*OUT;
int M,Prime[MaxN],Size=1,N=0;
long long A,D[MaxP],B,Sign[2]={1,-1};
bool Sieve[MaxN];
void Get_Sieve()
{
Prime[Size]=2;
for(int i=3;i<MaxN;i+=2)
{
if(!Sieve[i])
{
Prime[++Size]=i;
for(int j=3*i;j<MaxN;j+=2*i)
Sieve[i]=true;
}
}
}
void Decompose(long long X)
{
N=0;
for(int i=1;Prime[i]<=X;i++)
{
if(Prime[i]*Prime[i]>X)
{
D[++N]=X;
break;
}
if(X%Prime[i]==0)
{
D[++N]=Prime[i];
while(X%Prime[i]==0)
X/=Prime[i];
}
}
}
int main()
{
IN=fopen("pinex.in","r");
OUT=fopen("pinex.out","w");
Get_Sieve();
fscanf(IN,"%d",&M);
for(int i=1;i<=M;i++)
{
fscanf(IN,"%lld%lld",&A,&B);
int bits=0;
long long Out=A,Aux;
Decompose(B);
for(int i=1;i<(1<<N);i++)
{
bits=0;
Aux=1;
for(int j=1;j<=N;j++)
if(i&(1<<(j-1)))
Aux*=D[j],bits++;
Out+=Sign[bits%2]*A/Aux;
}
fprintf(OUT,"%lld\n",Out);
}
return 0;
}