Pagini recente » Cod sursa (job #1683722) | Cod sursa (job #2683821) | Cod sursa (job #2145930) | Cod sursa (job #2526314) | Cod sursa (job #1288971)
#include <stdio.h>
#include <cmath>
int div[100000],pos;
bool schimb[100000];
int a,b,temp,tx=0;
FILE *fin,*fout;
void ver()
{
int ct=0;
int mult=1;
for(int i=1;i<pos;i++)
{
if(schimb[i]==1)
{
ct++;
mult*=div[i];
}
}
if(ct==0) return ;
if(ct%2==0) tx-=a/mult;
else if(ct%2!=0) tx+=a/mult;
}
void bcs(int pt)
{
if(pt==pos) ver();
else
{
for(int i=0;i<=1;i++)
{
schimb[pt]=i;
bcs(pt+1);
}
}
}
int main()
{
fin=fopen("pinex.in","r");
fout=fopen("pinex.out","w");
int n;
fscanf(fin,"%d",&n);
for(int i=1;i<=n;i++)
{
pos=1;
fscanf(fin,"%d %d",&a,&b);
temp=b;
if(temp%2==0)
{
div[pos]=2;
pos++;
while(temp%2==0) temp/=2;
}
for(int j=3;j<=temp;j+=2)
{
if(temp==1) break;
if(temp%j==0)
{
div[pos]=j;
pos++;
while(temp%j==0) temp/=j;
}
}
if(temp==b)
{
div[pos]=temp;
pos++;
}
tx=0;
bcs(1);
fprintf(fout,"%d\n",a-tx);
}
}