#include <stdio.h> #include <iostream> #include <math.h> //b + c = (c^2 + 1) / (c - a) = (c-a) + (2 * a) + (a^2 + 1) / (c -a) //令c-a = t, f(t) = t + 2*a + (a^2+1)/ t //因为f(t)在sqrt(a^2+1)时取最小值,但是由于t只能取整数, //所以,必须从极值点往下和往上寻找最小的值,然后取2者中最小的 int main() { long long a; while (std::cin >> a) { long long nTemp = a * a + 1; long long nDown = sqrt(nTemp); long long nUp = nDown; long long one, two;
while (nTemp % nDown ) { nDown--; } one = 2 * a + nTemp / nDown + nDown;
while (nTemp % nUp ) { nUp++; } two = 2 * a + nTemp / nUp + nUp;
std::cout << (one < two ? one : two) << std::endl; } return 0; }
//a = (b*c-1)/(b+c) //令b = a + m, c = a + n, c >= b //-> a*(2*a+m+n) = (a+m)*(a+n)-1 //m*n = a^2 + 1 (n>=m) //所以,求出a^2+1所有因子对,取其中m+n最小的即可 int main() { long long a; while (std::cin >> a) { long long m, n; long long nTemp = a * a + 1; long long nMax = sqrt(nTemp); long long nRes = 1 + nTemp; for (m = 2; m <= nMax; ++m) { if (nTemp % m == 0) { n = nTemp / m; if (m + n < nRes) { nRes = m + n; } } }