Розбір
Let’s consider objects and find the number of ways to select objects out of .
We’ll divide the set in half: . To choose objects from , we’ll select objects from the left half (from the set and objects from the right half (from the set . The number of ways to make such a selection, according to the rule of multiplication, is equal to . Since can take values from to , is equal to the number of ways to choose objects from , which is equal . Thus,
Example
For the answer is
At the same time .
Algorithm realization
The function Cnk computes the value of the binomial coefficient .
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 .
scanf("%lld", &n);
Compute and print the answer.
res = Cnk(2*n, n); printf("%lld\n", res);
Java realization
import java.util.*; public class Main { public static long Cnk(long n, long k) { long res = 1; if (k > n - k) k = n - k; for (long i = 1; i <= k; i++) res = res * (n - i + 1) / i; return res; } public static void main(String[] args) { Scanner con = new Scanner(System.in); long n = con.nextLong(); long res = Cnk(2*n, n); System.out.println(res); con.close(); } }
Python realization
The function Cnk computes the value of the binomial coefficient .
def Cnk(n, k): res = 1 if k > n - k: k = n – k for i in range(1, k+1): res = res * (n - i + 1) // i return res
The main part of the program. Read the value of .
n = int(input())
Compute and print the answer.
res = Cnk(2 * n, n) print(res)
Python realization – comb
Read the value of .
n = int(input())
Compute and print the answer.
res = math.comb(2 * n, n) print(res)