Editorial
If , then . It is sufficient to find the solution for and, along with the pair , also print the pair . For these two pairs coincide.
Let be the smallest number for which . Then it is obvious that .
Choose such that and consider the function . Then for , the function is monotonically increasing. Therefore, you can solve the equation by binary search.
To solve the problem, one should iterate over the values of , and for each such , solve the equation relatively with binary search. The found value of must be an integer.
Example
Consider the equation . Given that and , it is sufficient to iterate over .
Let , consider the equation or , . By binary search in the interval , we find an integer solution . Since , we have two solutions: .
Algorithm realization
Store the required pairs in the vector of pairs .
vector<pair<long long,long long> > res;
The Cnk function computes the value of binomial coefficient .
long long Cnk(long long n, long long k) { long long i, Res = 1; if (k > n - k) k = n - k; for(i = 1; i <= k; i++) {
If at the next iteration the result exceeds (we are searching for a solution to the equation ), then stop computations. Exiting the function at this point avoids overflow.
if (1.0 * Res * (n - i + 1) / i > m) return m + 1; Res = Res * (n - i + 1) / i; } return Res; }
The main part of the program. Read the input data.
scanf("%d",&tests); while (tests--) { res.clear(); scanf("%lld",&m);
Iterate over the values of from until .
for(k = 0; Cnk(2*k,k) <= m; k++) {
Find the value of using binary search.
long long lo = 2*k, hi = m; while (lo < hi) { long long n = (lo + hi) / 2; if (Cnk(n,k) < m) lo = n + 1; else hi = n; }
If , then a solution is found. Include one or two pairs of solutions in the result.
if (Cnk(lo,k) == m) { res.push_back(make_pair(lo,k)); if (lo != 2*k) res.push_back(make_pair(lo,lo - k)); } }
Sort the pairs.
sort(res.begin(),res.end());
Print the answer — the number of found pairs and the pairs themselves.
printf("%d\n",res.size()); for(i = 0; i < res.size(); i++) printf("(%lld,%lld) ", res[i].first,res[i].second); printf("\n"); }
Python realization
The Cnk function computes the value of binomial coefficient .
def Cnk(n, k): Res = 1 if k > n - k: k = n – k for i in range(1, k + 1):
If at the next iteration the result exceeds (we are searching for a solution to the equation ), then stop computations. Exiting the function at this point avoids overflow.
if Res * (n - i + 1) // i > m: return m + 1 Res = Res * (n - i + 1) // i return Res
The main part of the program. Read the input data.
tests = int(input()) for _ in range(tests): res = [] m = int(input())
Iterate over the values of from until .
k = 0 while Cnk(2 * k, k) <= m:
Find the value of using binary search.
lo, hi = 2 * k, m while lo < hi: n = (lo + hi) // 2 if Cnk(n, k) < m: lo = n + 1 else: hi = n
If , then a solution is found. Include one or two pairs of solutions in the result.
if Cnk(lo, k) == m: res.append((lo, k)) if lo != 2 * k: res.append((lo, lo - k)) k += 1
Sort the pairs.
res.sort()
Print the answer — the number of found pairs and the pairs themselves.
print(len(res)) for item in res: print(f"({item[0]},{item[1]})", end=" ") print()