Binomial coefficient A combination of n elements taken k at a time is a set of k elements chosen from the given n elements. In this context, sets that differ only in the order of the elements (but not in their composition) are considered identical. It is precisely this property of combinations that distinguishes them from permutations.
For example, consider a set of 5 elements: \{1, 2, 3, 4, 5\} . The sets \{2, 1, 3\} and \{3, 2, 1\} are identical as combinations (though different as permutations) because they consist of the same elements \{1, 2, 3\} .
The formula for calculating the number of combinations of n elements taken k at a time is
C_n^{k} = {n! \over k! \cdot (n - k)!}
The numbers C_n^{k} are called binomial coefficients .
When we choose zero elements from a set of n elements, we are essentially making an “empty” choice. The empty set, in this context, is considered a valid combination. Imagine you have a box with n balls, and you want to choose 0 of them. Even if you don’t choose anything, it is still a valid choice. Thus, C_n^{0} is the number of ways to choose 0 elements from n , which is equal to 1 .
We can think of this as the one and only way to not choose any elements from the set, which gives the result of 1 .
C_n^{0} = {n! \over 0! \cdot (n - 0)!} = {n! \over 1! \cdot n!} = 1
When we choose one element from a set of n elements, we have n options to choose from: we can choose any one of the n elements. For example, if we have a set of numbers from 1 to 5 ( \{1, 2, 3, 4, 5\} ), we can choose one number from this set, and we have 5 options for this choice.
The value C_n^{1} equals n because there are n ways to choose one element from a set of n elements.
C_n^{1} = {n! \over 1! \cdot (n - 1)!} = n
When choosing 2 elements from a set of n elements, we form combinations of pairs. To build such a pair, we first choose one element, and then the second.
The first element can be chosen from n elements.
After choosing the first element, we have n –1 elements left to choose the second element (since we cannot choose the same element again).
Thus, the total number of combinations of pairs of elements that can be chosen from a set of n elements is the product of the number of ways to choose the first and second elements: n \cdot (n - 1) .
However, each pair is counted twice, as the order of elements in the pair does not matter. For example, ( p , q ) and ( q , p ) are considered the same pair. To account for this, the total number of combinations should be divided by 2 . Therefore, the value C_n^{2} equals n \cdot (n - 1) / 2 .
C_n^{2} = {n! \over 2! \cdot (n - 2)!} = {n \cdot (n - 1) \over 2}
Let's calculate the values of the following binomial coefficients:
C_4^{2} = {4! \over 2! \cdot 2!} = {3 \cdot 4 \over 2} = 6,
C_6^{3} = {6! \over 3! \cdot 3!} = {4 \cdot 5 \cdot 6 \over 6} = 20,
C_7^{3} = {7! \over 3! \cdot 4!} = {5 \cdot 6 \cdot 7 \over 6} = 35
Symmetry rule: C_n^{k} = C_n^{n - k} .
The number of ways to choose k elements from n is equal to the number of ways to not choose ( n – k ) elements from n . There are C_n^{n - k} ways to not choose ( n – k ) elements from n . Therefore C_n^{k} = C_n^{n - k} .
Let’s calculate the values of the following binomial coefficients:
C_5^{4} = C_5^{5 - 4} = C_5^{1} = 5,
C_6^{4} = C_6^{6 - 4} = C_6^{2} = {6 \cdot 5 \over 2} = 15
Exercise. Calculate the values of the following binomial coefficients:
1) C_5^{2}
3) C_5^{4}
5) C_8^{1}
2) C_8^{3}
4) C_7^{5}
6) C_8^{5}
Summation formula: C_n^{k} = C_{n-1}^{k-1} + C_{n-1}^{k} .
Proof. Let’s calculate the value of the right-hand side of the equation:
C_{n-1}^{k-1} + C_{n-1}^{k} = {(n - 1)! \over (k - 1)! \cdot (n - k)!} + {(n - 1)! \over k! \cdot (n - k - 1)!} =
{(n - 1)! \cdot k + (n - 1)! \cdot (n - k) \over k! \cdot (n - k)!} = {(n - 1)! \cdot (k + n - k)\over k! \cdot (n - k)!} = {n! \over k! \cdot (n - k)!} = C_n^{k}
Preparing for the calculus exam, Petya spread out n different cheat sheets in front of himself. They were his salvation, as throughout the entire semester Petya had never bothered to properly study the material. There were so many cheat sheets that they didn’t fit into any pocket. Therefore, Petya decided to calculate the maximum number of cheat sheets he could take with him to the exam. And then the question arose: how many ways are there in total to choose the required number of cheat sheets?
Input. Contains the total number of cheat sheets n~(1 \le n \le 12) and the number of cheat sheets k~(0 \le k \le n) that Peter can take with him.
Output. Print the number of ways to choose k cheat sheets from n .
Open problem
To compute the binomial coefficient, you can use the following formula:
C_n^{k} = {n! \over k! \cdot (n - k)!} = {n \cdot (n - 1) \cdot (n - 2) \cdot ... \cdot (n - k + 1) \over 1 \cdot 2 \cdot 3 \cdot ... \cdot k}
Declare a variable r es and initialize it with a value of 1 . Then multiply r es by n and divide the result by 1 . After that multiply r es by n –1 and divide by 2 . Continue this process of multiplication and division k times (the numerator and denominator of C_n^{k} after simplification by ( n – k )! contain k factors).
The Cnk function returns the binomial coefficient using an iterative method.
int Cnk(int n, int k)
{
int res = 1;
for(int i = 1; i <= k; i++)
res = res * (n - i + 1) / i;
return res;
}The main part of the program. Read the input data.
Compute and print the answer.
res = Cnk(n,k);
printf("%d\n",res);For the recursive implementation of the binomial coefficient, use the following formula:
C_n^{k} = \begin{cases}
C_{n - 1}^{k - 1} + C_{n - 1}^{k}, n > 0 \\
1, k = n~or~k = 0
\end{cases}
The Cnk function returns the binomial coefficient using a recursive method.
int Cnk(int n, int k)
{
if (n == k) return 1;
if (k == 0) return 1;
return Cnk(n - 1, k - 1) + Cnk(n - 1, k);
}The main part of the program. Read the input data.
Compute and print the answer.
res = Cnk(n,k);
printf("%d\n",res);How many ways are there to choose k out of n participants in the summer math camp, each of whom will receive kefir? Print the answer modulo 9929 .
Input. Two integers n and k~(0 \le k \le n \le 500) .
Output. Print the number of ways modulo 9929 .
Open problem
The answer to the problem will be the value of C_n^{k}~mod~9929 . Since we need to find the binomial coefficient by modulo, we’ll try to avoid division during calculations. To achieve this, use the relation: C_n^{k} = C_{n - 1}^{k} + C_{n - 1}^{k - 1}, C_n^{0} = 1 .
Since k \le n \le 500 , we use the memoization technique.
Declare the constants and array c nk , where cnk[n][k] = C_n^{k}~mod~9929 .
#define MAX 510
#define MOD 9929
int cnk[MAX][MAX];The c function computes the value of the binomial coefficient C_n^{k}~mod~9929 .
int c(int n, int k)
{
if (cnk[n][k] > 0) return cnk[n][k];
if (n - k < k) return c(n,n-k);
if (!k) return cnk[n][k] = 1;
return cnk[n][k] = (c(n-1,k) + c(n-1,k-1)) % MOD;
}The main part of the program. Initialize the c nk array with zeros. Read the input data.
memset(cnk,0,sizeof(cnk));
scanf("%d %d",&n,&k);Compute and print the answer.
Binomial coefficients appear in Newton’s binomial:
(x + y)^n = \sum_{k=0}^n C_n^{k}x^{k}y^{n-k}
Let’s look at examples:
(x + y)^2 = C_2^{0}x^{2}y^{0} + C_2^{1}x^{1}y^{1} + C_2^{2}x^{0}y^{2} = x^2 + 2xy + y^2,
(x + y)^3 = C_3^{0}x^{3}y^{0} + C_3^{1}x^{2}y^{1} + C_3^{2}x^{1}y^{2} + C_3^{3}x^{0}y^{3} = x^3 + 3x^2y + 3xy^2 + y^3
Given a non-negative integer n , find the sum of binomial coefficients
C_n^{0} + C_n^{1} + ... + C_n^{n}
Input. One non-negative integer n~(n \le 60) .
Output. Print the value of the sum.
Open problem
The formula of the binomial theorem is as follows:
(a + b)^n = \sum_{i = 0}^n{C_n^ia^ib^{n-i}}
If we set a = b = 1 , then this relation takes the following form:
(1 + 1)^n = \sum_{i = 0}^n{C_n^i1^i1^{n-i}}
or
2^n = \sum_{i = 0}^n{C_n^i} = C_n^0 + C_n^1 + ... + C_n^n
Thus, the indicated sum equals 2^n .
Example
If n = 1 , then C_1^0 + C_1^1 = 1 + 1 = 2
If n = 2 , then C_2^0 + C_2^1 + C_2^2 = 1 + 2 + 1 = 4
If n = 3 , then C_3^0 + C_3^1 + C_3^2 + C_3^3 = 1 + 3 + 3 + 1 = 8
Given a non-negative integer n , find the sum of binomial coefficients
(C_n^{0})^2 + (C_n^{1})^2 + (C_n^{2})^2 + ... + (C_n^{n})^2
Input. One non-negative integer n~(n \le 30) .
Output. Print the value of the sum.
Open problem
Let’s consider 2 n objects a_1, ..., a_{2n} and find the number of ways to select n objects out of 2 n .
We’ll divide the set in half: \{a_1, a_2, ..., a_n, a_{n+1}, ..., a_{2n}\} . To choose n objects from 2 n , we’ll select k~(k \le n) objects from the left half (from the set \{a_1, a_2, ..., a_n \}) and n – k objects from the right half (from the set \{a_{n+1}, a_{n+2}, ..., a_{2n}\}) . The number of ways to make such a selection, according to the rule of multiplication, is equal to C_n^k \cdot C_n^{n-k} = (C_n^k)^2 . Since k can take values from 0 to n , то \sum_{k=0}^n{(C_n^k)^2} is equal to the number of ways to choose n objects from 2 n , which is equal to C_{2n}^n . Thus,
(C_n^0)^2 + (C_n^1)^2 + (C_n^2)^2 + ... + (C_n^n)^2 = C_{2n}^n
Example
For n = 3 the answer is (C_3^0)^2 + (C_3^1)^2 + (C_3^2)^2 + (C_3^3)^2 = 1^2 + 3^2 + 3^2 + 1^2 = 20
At the same time C_6^3 = {6! \over 3! \cdot 3!} = {4 \cdot 5 \cdot 6 \over 6} = 20 .
The Cnk function computes the value of the binomial coefficient C_n^k .
long long Cnk(long long n, long long k)
{
long long res = 1;
if (k > n - k) k = n - k;
for (long long i = 1; i <= k; i++)
res = res * (n - i + 1) / i;
return res;
}The main part of the program. Read the value of n .
Compute and print the answer.
res = Cnk(2*n, n);
printf("%lld\n", res);Given the values of k and n , find the number of positive integral solutions for the equation
x_1 + x_2 + ... + x_k = n
Input. Two positive integers k and n~(k \le n \le 100) .
Output. Print the number of positive integral solutions for the given equation. It is known that the answer is no more than 10^{18} .
Open problem
Consider a sequence of n ones: 111...11 . You can insert a ‘ + ’ sign between any two ones. For example, 11+111+1 . Such notation denotes the sum 2 + 3 + 1 , where each term represents the number of ones adjacent to each other. The number of positions where a ‘ + ’ sign can be inserted is n –1 . Since the sum must consist of k terms, k –1 ‘ + ’ signs should be inserted.
We should insert k –1 pluses in n –1 places. This can be done in C_{n-1}^{k-1} ways.
Example
Consider the equation x_1 + x_2 + x_3 = 4 . It has 3 positive integer solutions:
For example, n = 4 ones can be divided into k = 3 terms in C_2^3 = 3 ways:
The Cnk function computes the value of the binomial coefficient C_n^k .
long long Cnk(long long k, long long n)
{
long long res = 1;
if (k > n - k) k = n - k;
for (long long i = 1; i <= k; i++)
res = res * (n - i + 1) / i;
return res;
}The main part of the program. Read the input data.
scanf("%lld %lld", &k, &n);Compute and print the answer — the value of C_{n-1}^{k-1} .
res = Cnk(k - 1, n - 1);
printf("%lld\n", res);Given values of k and n , find the number of nonnegative integral solutions for the equation
x_1 + x_2 + ... + x_k = n
Input. Two positive integers k and n~(k \le n \le 100) .
Output. Print the number of nonnegative integral solutions for the given equation. It is known that the answer is no more than 10^{18} .
Open problem
Consider a sequence of n + k – 1 positions. In n positions we should place 1 . In k –1 positions we should place the ' + ' symbol (to obtain k terms).
Any arrangement of 1 s and ' + ' signs among these positions will correspond to some solution of the given equation. For example, consider some solutions of the equation x_1 + x_2 + x_3 = 4 (4 ones and 2 plus signs):
We should insert k –1 plus signs into n + k – 1 positions. This can be done in C_{n+k-1}^{k-1} ways.
Example
Consider the equation x_1 + x_2 + x_3 = 4 . Its solutions are:
(4, 0, 0) and its 3 permutations;
(3, 1, 0) and its 6 permutations;
(2, 2, 0) and its 3 permutations;
(2, 1, 1) and its 3 permutations;
There are 3 + 6 + 3 + 3 = 15 solutions.
The Cnk function computes the value of the binomial coefficient C_n^k .
long long Cnk(long long k, long long n)
{
long long res = 1;
if (k > n - k) k = n - k;
for (long long i = 1; i <= k; i++)
res = res * (n - i + 1) / i;
return res;
}The main part of the program. Read the input data.
scanf("%lld %lld", &k, &n);Compute and print the answer — the value of C_{n+k-1}^{k-1} .
res = Cnk(k - 1, n + k - 1);
printf("%lld\n", res);Let n be a non-negative integer. Let
n! = 1 \cdot 2 \cdot ... \cdot n~(0! = 1),
C_n^{k} = {n! \over k! \cdot (n - k)!} (0 \le k \le n)
You are given the numbers n and k . Calculate C_n^{k} .
Input. The first line contains the number of test cases t~(t \le 50) . Each of the next t lines contains two integers n and k~(0 \le n < 2^{64}, 0 \le C_n^{k} < 2^{64}) .
Output. Print t lines, each contains the value C_n^{k} for corresponding test.
Open problem
Computations will be performed using 64 -bit unsigned integers (unsigned long long). Its obvious that
C_n^k = {n! \over k! \cdot (n - k)!} = {n \cdot (n - 1) \cdot (n - 2) \cdot ... \cdot (n - k + 1) \over 1 \cdot 2 \cdot 3 \cdot ... \cdot k} = {n \over 1} \cdot {n - 1 \over 2} \cdot {n - 2 \over 3} \cdot ... \cdot {n - k + 1 \over k}
Let’s assign the value of the variable r es to 1 . Then multiply it by {n - i + 1 \over i} for all i from 1 to k . Each time the division by i will yield an integer result, but multiplication can cause overflow. Let d = GCD(res, i) . Then let’s rewrite the operation
res = res * (n – i + 1 ) / i
as
res = (res / d) * ((n – i + 1 ) / (i / d))
In this implementation we’ll avoid overflow (the answer is 64 -bit unsigned integer). Note that first we need to perform the division (n – i + 1 ) / (i / d) , and then multiply r es / d by the resulting quotient.
To compute C_n^k , we must run k iterations. But what to do if we want to compute C_{2000000000}^{1999999999} ? The answer is no more than 2^{64} , so such input values are possible. As long as C_n^k = C_n^{n - k} , then for n – k < k we should assign k = n – k .
Example
Consider the next sample:
C_6^3 = {6 \over 1} \cdot {5 \over 2} \cdot {4 \over 3} = 15 \cdot {4 \over 3}
Let res = 15 , and we need to make a multiplication res \cdot {4 \over 3} = 15 * {4 \over 3} . Compute d = GCD(15, 3) = 3 . So 15 \cdot {4 \over 3} = (15 / 3) \cdot {4 \over 3 / 3} = 5 \cdot {4 \over 1} = 20 .
The gcd function computes the greatest common divisor of a and b .
unsigned long long gcd(unsigned long long a, unsigned long long b)
{
return (!b) ? a : gcd(b,a % b);
}The Cnk function computes the value of the binomial coefficient C_n^k .
unsigned long long Cnk(unsigned long long n, unsigned long long k)
{
unsigned long long CnkRes = 1, t, i;
if (k > n - k) k = n - k;
for(i = 1; i <= k; i++)
{
t = gcd(CnkRes, i);
CnkRes = (CnkRes / t) * ((n - i + 1) / (i / t));
}
return CnkRes;
}The main part of the program. Read the input data. Sequentially process the test cases.
scanf("%d",&t);
while(t--)
{
scanf("%llu %llu",&n,&k);
res = Cnk(n,k);
printf("%llu\n",res);
}Given a positive integer n . Find the value of the next sum
1 \cdot C_n^{0} + 2 \cdot C_n^{1} + 3 \cdot C_n^{2} + ... + (n + 1) \cdot C_n^{n}
Input. One positive integer n~(n \le 30) .
Output. Print the sum value.
Open problem
Rewrite the sum in the form:
1 \cdot C_n^0 + 2 \cdot C_n^1 + 3 \cdot C_n^2 + ... + (n + 1) \cdot C_n^n =
\left( C_n^0 + C_n^1 + C_n^2 + ... + C_n^n \right) + 1 \cdot C_n^1 + 2 \cdot C_n^2 + ... + n \cdot C_n^n
The sum in parentheses is 2^n . If in Newton’s binomial formula
(a + b)^n = \sum_{i=0}^n{C_n^ia^ib^{n-i}}
we set a = b = 1 , then we get the relation: (1 + 1)^n = \sum_{i=0}^n{C_n^i1^i1^{n-i}} , or
2^n = \sum_{i=0}^nC_n^i = C_n^0 + C_n^1 + ... + C_n^n
Let’s compute the remaining sum:
1 \cdot C_n^1 + 2 \cdot C_n^2 + ... + n \cdot C_n^n =
1 \cdot {n! \over 1! \cdot (n - 1)!} + 2 \cdot {n! \over 2! \cdot (n - 2)!} + ... + n \cdot {n! \over n! \cdot (n - n)!} =
n \cdot \left( {(n - 1)! \over 0! \cdot (n - 1)!} + {(n - 1)! \over 1! \cdot (n - 2)!} + ... + {(n - 1)! \over (n - 1)! \cdot (n - n)!} \right) =
n \cdot \left( C_{n-1}^0 + C_{n-1}^1 + ... + C_{n-1}^{n-1} \right) = n \cdot 2^{n - 1}
So the answer is 2^n + n \cdot 2^{n - 1} = (n + 2) \cdot 2^{n - 1} .
Example
Compute the value for n = 3 . For direct calculation:
1 \cdot C_3^0 + 2 \cdot C_3^1 + 3 \cdot C_3^2 + 4 \cdot C_3^3 = 1 \cdot 1 + 2 \cdot 3 + 3 \cdot 3 + 4 \cdot 1 = 1 + 6 + 9 + 4 = 20
When calculated using the formula: 5 \cdot 2^2 = 20 .
Read the input value of n .
Compute and print the answer.
res = (1LL << (n - 1)) * (n + 2);
printf("%lld\n", res);Algorithm implementation – function
Declare an array to store the results: dp[n][k] = C_n^k .
The Cnk function computes the value of the binomial coefficient C_n^k .
int Cnk(int n, int k)
{
if (n == k) return 1;
if (k == 0) return 1;
if (dp[n][k] != -1) return dp[n][k];
return dp[n][k] = (Cnk(n - 1, k - 1) + Cnk(n - 1, k)) % 9929;
}The main part of the program. Read the input value of n .
scanf("%d", &n);
memset(dp, -1, sizeof(dp));Compute the required sum.
res = 0;
for (i = 0; i <= n; i++)
res += (i + 1) * Cnk(n, i);Print the answer.
You have a piece of paper and you choose a rectangle of size n \cdot m on it. Let’s call this rectangle together with the lines it contains a grid. Starting at the lower left corner of the grid, you move your pencil to the upper right corner, taking care that it stays on the lines and moves only to the right or up. The result is shown on the left:
Really a masterpiece, isn’t it? Repeating the procedure one more time, you arrive with the picture shown on the right. Now you wonder: how many different works of art can you produce?
Input. Two positive integers n and m .
Output. Print the number of different art works that can be generated using the procedure described above. You may safely assume that this number fits into a 64 -bit signed integer.
Open problem
The required path is a broken line consisting of n + m links. Of these, n links must be vertical, and the rest must be horizontal. The number of ways to choose n vertical links out n + m is equal to C_{n+m}^n .
Example
For the first test case n = 3, m = 4 . The answer is C_7^3 = {7! \over 3! \cdot 4!} = {7 \cdot 6 \cdot 5 \over 1 \cdot 2 \cdot 3} = 35 .
For the second test case n = 1, m = 1 . The answer is C_2^1 = {2! \over 1! \cdot 1!} = 2 .
The Cnk function computes the value of the binomial coefficient C_n^k .
long long Cnk(long long n, long long k)
{
long long res = 1;
if (k > n - k) k = n - k;
for (long long i = 1; i <= k; i++)
res = res * (n - i + 1) / i;
return res;
}The main part of the program. Read the input data.
scanf("%lld %lld", &n, &m);Compute and print the answer C_{n+m}^n .
res = Cnk(n + m, n);
printf("%lld\n", res);Gunnar is quite an elderly and forgetful researcher. Currently, he is writing a paper on security in social networks that involves some combinatorics. He developed a program to calculate binomial coefficients to assist him in verifying some of his calculations.
The binomial coefficient C_n^{k} is a number defined by
C_n^{k} = {n! \over k! \cdot (n - k)!}
where n and k are non-negative integers.
Gunnar uses his program to calculate C_n^{k} and obtains a number m as a result. Unfortunately, being forgetful, he forgot the numbers of n and k he used as input. These two numbers were the result of lengthy computations and were written on one of the many sheets scattered across his desk. Instead of searching through the papers, he decided to reconstruct the numbers n and k from the obtained answer. Can you help him find all possible values?
Input. The first line contains the number of test cases, at most 100 . Each test is given in a single line and contains an integer m~(2 \le m \le 10^{15}) — the result of the Gunnar’s program.
Output. For each test, print two lines. The first line should contain the number of ways to express m using the binomial coefficient. The second line should contain all pairs ( n , k ) such that C_n^{k} = m . Pairs should be sorted in increasing order of n , and in case of equality, in increasing order of k . The output format of pairs is given in the example.
1
(2,1)
4
(6,2) (6,4) (15,1) (15,14)Open problem
If C_n^k = m , then C_n^{n - k} = m . It is sufficient to find the solution for k \le n / 2 and, along with the pair ( k , n ) , also print the pair (k, n – k) . For k = n / 2 these two pairs coincide.
Let p be the smallest number for which C_{2p}^p > m . Then it is obvious that 0 \le k < p .
Choose k such that C_{2k}^k \le m and consider the function f(n) = C_n^k . Then for 2k \le n \le m , the function f ( n ) is monotonically increasing. Therefore, you can solve the equation f(n) = m by binary search.
To solve the problem, one should iterate over the values of k~(0 \le k < p) , and for each such k , solve the equation C_n^k = m relatively n with binary search. The found value of n must be an integer.
Example
Consider the equation C_n^k = 3003 . Given that C_{12}^6 = 924 and C_{14}^7 = 3432 , it is sufficient to iterate over 0 \le k \le 6 .
Let k = 2 , consider the equation C_n^2 = 3003 or {n \cdot (n - 1) \over 2} = 3003 , n \cdot (n – 1) = 6006 . By binary search in the interval 4 \le n \le 3003 , we find an integer solution n = 78 . Since n \ne 2 \cdot k , we have two solutions: C_{78}^2 = C_{78}^{76} = 3003 .
Store the required pairs in the vector of pairs r es .
vector<pair<long long,long long> > res;The Cnk function computes the value of binomial coefficient C_n^k .
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 m (we are searching for a solution to the equation C_n^k = m ), 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 k from 0 until C_{2k}^k ≤ m .
for(k = 0; Cnk(2*k,k) <= m; k++)
{Find the value of n~(2k \le n \le m) 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 C_{lo}^k = m , 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");
}Asymptotic nonations for the Binomial Coefficients Theorem 1. There exists a relationship between the binomial coefficients:
C_n^0 < C_n^1 < ... < C_n^{\lfloor n/2 \rfloor} \ge C_n^{\lfloor n/2 \rfloor + 1} > ... > C_n^n
Theorem 2. C_{2n}^n < 4^n . Follows from the fact that \sum_{k = 0}^{2n} C_{2n}^k = (1 + 1)^{2n} = 2^{2n} = 4^n
Theorem 3. C_{2n}^n > {4^n \over 2n + 1} . Follows from the fact that \sum_{k = 0}^{2n} C_{2n}^k = 4^n , and the number of coefficients is 2n + 1 . Therefore, the largest of the coefficients will be greater than {4^n \over 2n + 1} .
Theorem 4. Stirling formula. n! ≈ \sqrt{2 \pi n}\left({n \over e} \right) ^ n .
A more accurate approximation is:
n! ≈ \sqrt{2 \pi n} \left({n \over e} \right) ^ n \left( 1 + {1 \over 12n} + {1 \over 288n^2} - {139 \over 51849n^3} - {571 \over 2488320n^4} \right)
Theorem 5.
C_{2n}^n ≈ {\sqrt{4 \pi n}\left({2n \over e} \right) ^ {2n} \over \left( \sqrt{2 \pi n}\left({n \over e} \right) ^ n \right) ^2} = {4^n \over \sqrt{\pi n}}
Theorem 6.
C_n^k = {n! \over k!(n - k)!} = { n \cdot (n - 1) \cdot (n - 2) \cdot ... \cdot (n - k + 1) \over 1 \cdot 2 \cdot 3 \cdot ... \cdot k} =
{n! \over k!} \cdot \left( 1 - {1 \over n} \right) \cdot \left( 1 - {2 \over n} \right) \cdot ... \cdot \left( 1 - {k - 1 \over n} \right) =
{n! \over k!} \cdot e ^{ln \left( 1 - {1 \over n} \right) + ln \left( 1 - {2 \over n} \right) + ... + ln \left( 1 - {k - 1 \over n} \right)} \le (use~the~inequality~ln(1 - x) \le -x)
{n! \over k!} \cdot e ^{-{1 \over n} - {2 \over n} - ... - {k - 1 \over n}} = {n! \over k!} \cdot e^{-{k \cdot (k - 1) \over 2n}}
List of problems
Comments