Cod sursa(job #2701457)

Utilizator sabinandreiBocan Sabin Andrei sabinandrei Data 31 ianuarie 2021 12:58:37
Problema Frac Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.57 kb
// frac.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <fstream>

using namespace std;

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

int prim[20];
long long n, p;

void desc() {
	long long i;
	for (i = 2; i * i <= n; i++)
		if (n % i == 0) {
			prim[++prim[0]] = i;
			while (n % i == 0)
				n /= i;
		}
	if (n > 1)
		prim[++prim[0]] = n;
}

long long fractii(long long x) {
	int i, j, lim = (1 << prim[0]) - 1;
	long long nr = 0, p;
	for (i = 0; i <= lim; i++) {
		p = 1;
		for (j = 1; j <= prim[0]; j++)
			if (i & (1 << j - 1))
				p = p * -1 * prim[j];
		nr = nr + x / p;
	}
	return nr;
}

int main() {
	in >> n >> p;
	desc();
	long long st = 0, dr = 1844674407370955161, m, sol = 0;
	while (st <= dr) {
		m = (st + dr) >> 1;
		if (fractii(m) >= p) {
			sol = m;
			dr = m - 1;
		}
		else
			st = m + 1;
	}
	out << sol << '\n';
	return 0;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file