Cod sursa(job #792600)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 27 septembrie 2012 21:23:51
Problema Frac Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <cstdio>
#include <vector>
#include <cassert>

using namespace std;

typedef long long LL;

const LL oo = 1LL<<62;

LL N, M, P;
vector<LL> F;

void BuildF() {
    for (LL D = 2; D*D <= N; ++D) {
        if (N%D == 0)
            F.push_back(D);
        for (; N%D == 0; N /= D);
    }
    if (N > 1)
        F.push_back(N);
}

inline LL Count(LL X) {
    LL C = 0;
    int NF = F.size();
    for (int Conf = 0; Conf < (1<<NF); ++Conf) {
        LL D = 1, Sign = 1;
        for (int i = 0; i < NF; ++i)
            if (Conf&(1<<i))
                D *= F[i], Sign = - Sign;
        C += Sign*X/D;
    }
    return C;
}

void Solve() {
    BuildF();
    LL L = 1, R = oo;
    while (L <= R) {
        LL Mid = (L+R)/2;
        if (Count(Mid) >= P)
            M = Mid, R = Mid-1;
        else
            L = Mid+1;
    }
}

void Read() {
    assert(freopen("frac.in", "r", stdin));
    assert(scanf("%lld %lld", &N, &P) == 2);
}

void Print() {
    assert(freopen("frac.out", "w", stdout));
    printf("%lld\n", M);
}

int main() {
    Read();
    Solve();
    Print();
    return 0;
}