Cod sursa(job #847614)

Utilizator repp4raduRadu-Andrei Szasz repp4radu Data 4 ianuarie 2013 12:07:48
Problema Frac Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <fstream>

#define ll long long
#define MAX 15

using namespace std;

ll N, P, K, rez, V[MAX];

bool ok(ll val)
{
    ll rez = 0;
    for(int conf = 0; conf < (1 << K); conf++)
    {
        ll sign = 1, x = 1;
        for(int i = 0; i < K; i++)
            if((1 << i) & conf)
            {
                sign *= -1;
                x *= V[i];
            }
        rez += sign * val / x;
    }
    return (rez >= P);
}

int main()
{
    ifstream in("frac.in"); in>>N>>P; in.close();
    for(int i = 2; i * i <= N; i++)
    {
        if(N % i) continue;
        V[K++] = i;
        while(!(N % i)) N /= i;
    }
    if(N) V[K++] = N;
    ll L = 1, R = (1LL<<61) + 1, m;
    while(L <= R)
    {
        m = (L + R) >> 1;
        if(ok(m)) rez = m, R = m - 1;
        else L = m + 1;
    }
    ofstream out("frac.out"); out<<rez; out.close();
    return 0;
}