Pagini recente » Cod sursa (job #1516736) | Cod sursa (job #898245) | Cod sursa (job #660016) | Cod sursa (job #1333872) | Cod sursa (job #1244109)
#include <cstdio>
#define nmp 1000050
#include <algorithm>
#define f_max 30
#include <cmath>
using namespace std;
bool prmc[nmp];
int prmll[80005];
long long a,b,fact[30],m;
void eratosthenes(){
int i,j;
//fill(prmc+2,prmc+nmp,1);
for(i=2;i<nmp;++i){
if(!prmc[i]){
for(j=i<<1;j<nmp;j+=i)
prmc[j]=true;
prmll[++prmll[0]]=i;
}
}
}
void search_prime(){
long long t=0,d=0;
while(b>1){
d++;
if(b%prmll[d]==0){
fact[++t]=prmll[d];
while(b%prmll[d]==0)
b/=prmll[d];
}
if(prmll[d]>sqrt(b) && b>1){
fact[++t]=b;
break;
}
}
long long r=a,no,pr;
int i,j;
for(i=1;i<(1<<t);i++){
no=0;
pr=1;
for(j=0;j<t;j++){
if((1<<j)&i){
pr=1LL*pr*fact[j+1];
no++;
}
}
if(no%2)no=-1;
else
no=1;
r=r+1LL *no* a / pr;
}
printf("%lld\n",r);
}
int main()
{
freopen("pinex.in","r",stdin);
freopen("pinex.out","w",stdout);
eratosthenes();
scanf("%d ",&m);
while(m--){
scanf("%lld %lld ",&a,&b);
search_prime();
}
return 0;
}