解释
code
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long LL;
int n;
LL c(int a,int b)
{
// c(a,b) = a! / b! / (a-b)!
// a > b
// a ... a-b 1 ... b
LL res = 1;
for(int i = a, j = 1;j <= b;i--,j ++)
{
res = res * i / j;
if (res > n)
return res;
}
return res;
}
bool check(int k)
{
int l = 2 * k; int r = max(n,2 * k);
while(l < r)
{
int mid = (l + r) >> 1;
if(c(mid,k) >= (LL)n) r = mid;
else l = mid + 1;
}
if(c(r,k) != n) return false;
else cout << 1ll * r * (r + 1) / 2 + k + 1 << endl;
return true;
}
int main()
{
cin >> n;
for(int k = 16; ; k--)
if(check(k)) break;
return 0;
}