Pagini recente » Cod sursa (job #1827633) | Cod sursa (job #374880) | Cod sursa (job #2594056) | Cod sursa (job #1299862) | Cod sursa (job #1949443)
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#define MaxN 1000005
#define MaxP 20
#define MaxS 800000
using namespace std;
FILE*IN,*OUT;
int M,Size=1,N=0,Prime[MaxS],Sign[2]={1,-1};
long long A,D[MaxP],B;
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;
int pos=0;
while(X>1)
{
pos++;
if(X%Prime[pos]==0)
{
D[++N]=Prime[pos];
while(X%Prime[pos]==0)
X/=Prime[pos];
}
if(X>1&&Prime[pos]>sqrt(X+0.0))
D[++N]=X,X=1;
}
}
int main()
{
IN=fopen("pinex.in","r");
OUT=fopen("pinex.out","w");
Get_Sieve();
long long Out,Aux;
fscanf(IN,"%d",&M);
for(int t=1;t<=M;t++)
{
fscanf(IN,"%lld%lld",&A,&B);
int bits=0;
Out=A;
Decompose(B);
for(int i=1;i<(1<<N);i++)
{
bits=0;
Aux=1;
for(int j=0;j<N;j++)
if(i&(1<<j))
Aux*=D[j+1],bits++;
Out+=Sign[bits%2]*A/Aux;
}
fprintf(OUT,"%lld\n",Out);
}
return 0;
}