Cod sursa(job #2701353)

Utilizator VladMxPMihaila Vlad VladMxP Data 30 ianuarie 2021 16:02:50
Problema Frac Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <iostream>
#include <fstream>
#define ull unsigned long long

using namespace std;
ifstream fin("frac.in");
ofstream fout("frac.out");
ull v[2000002],k=0;

ull rez(ull x)
{
    ull nr=x,q,p;
    for(int i=1;i<(1LL<<k);i++)
    {
        q=0;
        p=1;
        for(int j=0;j<k;j++)
        {
            if(i&(1LL<<j))
            {
                q++;
                p=p*v[j+1];
            }
        }
        if(q%2==1)nr=nr-x/p;
        else nr=nr+x/p;
    }
    return nr;
}

int main()
{
    ull n,p;
    fin>>n>>p;
    if(n%2==0)
    {
        v[1]=2;
        k=1;
        while(n%2==0)n/=2;
    }
    ull d=3;
    while(n>1&&d*d<=n)
    {
        if(n%d==0)
        {
            v[++k]=d;
            while(n%d==0)n/=d;
        }
        d+=2;
    }
    if(n>1)v[++k]=n;

    ull st=1,dr=1000000000000;
    while(st<=dr)
    {
        ull m=(st+dr)/2;
        if(rez(m)<p)st=m+1;
        else dr=m-1;
    }
    fout<<st;
}