Cod sursa(job #3032742)

Utilizator RolandPetreanPetrean Roland RolandPetrean Data 22 martie 2023 17:40:11
Problema Frac Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.76 kb
// https://www.infoarena.ro/problema/frac
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'

ifstream fin("frac.in");
ofstream fout("frac.out");

#define int long long

signed main() {
  int n, p;
  fin>>n>>p;

  vector<int> pi;
  for (int d=2; d*d<=n; ++d) {
    if (n%d==0) {
      while (n%d==0) n /= d;
      pi.push_back(d);
    }
  }
  if (n>1) pi.push_back(n);

  int l=0, r=1e18, res=0;
  while (l <= r) {
    int x=(l+r)/2;

    int ans=0;
    for (int i=1; i<(1<<(pi.size())); ++i) {
      int prod=(__builtin_popcount(i)&1 ? 1 : -1);
      for (int j=0; j<(int)pi.size(); ++j) {
        if (i&(1<<j)) prod *= pi[j];
      }
      ans += x/prod;
    }
    ans = x-ans;

    if (ans>=p) {
      res = x;
      r = x-1;
    } else l = x+1;
  }

  fout<<res;
}