Cod sursa(job #682894)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 19 februarie 2012 17:59:26
Problema Principiul includerii si excluderii Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.4 kb
#include <cstdio>
#include <bitset>

#define XMax 1000001
#define LL long long

using namespace std;

LL A, B, S;
bitset <XMax> V;
int Primes[XMax], F[XMax];

void Eratosthenes ()
{
    Primes[++Primes[0]]=2;
    for (int i=3; i<XMax; i+=2)
    {
        if (!V[i])
        {
            Primes[++Primes[0]]=i;
            if (1LL*i*i>=XMax) continue;
            for (int j=i*i; j<XMax; j+=(i+i))
            {
                V[j]=1;
            }
        }
    }
}

void Decompose ()
{
    F[0]=0;
    for (int i=1; i<=Primes[0] and B>1; ++i)
    {
        if (B%Primes[i]==0)
        {
            F[++F[0]]=Primes[i];
            for (; B%Primes[i]==0; B/=Primes[i]);
        }
    }
    if (B>1)
    {
        F[++F[0]]=B;
    }
}

void Pinex ()
{
    S=0;
    for (int Conf=1; Conf<(1<<F[0]); ++Conf)
    {
        int Sign=-1; LL X=1;
        for (int i=0; i<F[0]; ++i)
        {
            if (Conf&(1<<i))
            {
                Sign=-Sign;
                X*=F[i+1];
            }
        }
        S+=(Sign*(A/X));
    }
    S=A-S;
}

int main()
{
    freopen ("pinex.in", "r", stdin);
    freopen ("pinex.out", "w", stdout);
    Eratosthenes ();
    int T;
    scanf ("%d", &T);
    for (; T>0; --T)
    {
        scanf ("%lld %lld", &A, &B);
        Decompose ();
        Pinex ();
        printf ("%lld\n", S);
    }
    return 0;
}