Cod sursa(job #3337762)

Utilizator mtcmtcmtc mtc mtcmtc Data 29 ianuarie 2026 20:38:40
Problema Frac Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1 kb
#include <fstream>
#include <cmath>
#define int long long
using namespace std;
ifstream cin("frac.in");
ofstream cout("frac.out");
int dv[3005];
int k=-1;
int calc(int nr){
    int p=1<<(k+1),ans=0;
    for(int i=1;i<p;i++){
        int elem=0,prod=1;
        int x=i;
        for(int j=0;j<=k;j++){
            if(x%2==1){
                elem++;
                prod*=dv[j];
            }
            x/=2;
        }
        if(elem%2==1){
            ans+=nr/prod;
        }
        else{
            ans-=nr/prod;
        }
    }
    return nr-ans;
}
int32_t main()
{
    int n,poz;
    cin>>n>>poz;
    int d=2;
    while(n>1){
        if(n%d==0){
            dv[++k]=d;
            while(n%d==0) n/=d;
        }
        if(d==2) d++;
        else d+=2;
        if(d*d>n&&n>1) dv[++k]=n,n=1;
    }
    int st=0,dr=pow(2,61);
    while(st<=dr){
        int mid=(st+dr)/2;
        if(calc(mid)>=poz) dr=mid-1;
        else st=mid+1;
    }
    cout<<st;
    return 0;
}