Cod sursa(job #1328201)

Utilizator chiriacandrei25Chiriac Andrei chiriacandrei25 Data 28 ianuarie 2015 08:48:13
Problema Principiul includerii si excluderii Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include <cstdio>

using namespace std;

int f[100005];
long long A,B;

inline void Descomp(long long x)
{
    long long d=3;
    int k=0; f[0]=0;
    while(x%2==0)
    {
        x/=2; ++k;
    }
    if(k) f[++f[0]]=2;
    while(d*d<=x && x>1)
    {
        k=0;
        while(x%d==0)
        {
            x/=d; ++k;
        }
        if(k) f[++f[0]]=d;
        d+=2;
    }
    if(x>1) f[++f[0]]=x;
}

inline long long Solve()
{
    long long sol=0,prod;
    int stare,i,cnt;
    for(stare=1;stare<(1<<f[0]);++stare)
    {
        for(i=cnt=0,prod=1;i<f[0];++i)
            if((1<<i)&stare)
            {
                prod*=f[i+1];
                ++cnt;
            }
        if(cnt%2) sol+=A/prod;
        else sol-=A/prod;
    }
    return sol;
}

int main()
{
    int T;
    freopen ("pinex.in","r",stdin);
    freopen ("pinex.out","w",stdout);
    scanf("%d", &T);
    while(T--)
    {
        scanf("%lld%lld", &A,&B);
        Descomp(B);
        printf("%lld\n", A-Solve());
    }
    return 0;
}