Mathematics Knowledge - Combination Counting

combination count

overview

The goal of combinatorial counting is to count a certain class of mathematical objects.
Commonly used methods include dp, addition & multiplication principle, inclusion-exclusion principle, Cattelan number, etc.

dynamic programming

Taurus Wagyu

John wants to take N cows to participate in the exhibition activities in the fair. These cows can be male or female.
The cows have to stand in a row, but the bulls are aggressive. In order to prevent the bulls from making trouble, John decided that there must be at least K cows between any two bulls.

Please calculate how many queuing methods there are. All male cows can be regarded as the same, and so are all cows. The answer is modulo 5000011.

Input format
One line, input two integers N and K.

Output Format
An integer representing the number of methods queued.

Data range
1≤N≤10^5, 0≤K<N
Input sample:
4 2
Output sample:
6
Sample explanation The
6
methods are: male male female female, female female male male female, female male male female, Male and female, male and female, male and female.

train of thought

For 1 0 5 10^5105 data volume, you can use dynamic programming.
We think in terms of dp.
State representation f[i] :

Set: Indicates the legal set of station methods at position 1~i when station i is at position Attribute
: num

State calculation: f[i] = sum(f[j]), j = 0 ~ i - k - 1
can use prefix and optimization

the code

MOD = 5000011
N = 100010

f = [0] * N
s = [0] * N

n, m = map(int, input().split())

f[0], s[0] = 1, 1
for i in range(1, n + 1) :
	f[i] = s[max(0, i - m - 1)] % MOD
	s[i] = (s[i - 1] + f[i]) % MOD

print(s[n])

partition method

If n identical elements are allocated to m different objects, each object has at least one, ask how many different division methods there are.

Solution of the equation

Jiajia has encountered a problem, please come and help me solve it.

For the indeterminate equation a1+a2+⋯+ak−1+ak=g(x), where k≥1 and k∈N∗, x is a positive integer, g(x)=xxmod1000 (that is, the remainder of xx divided by 1000),
x ,k is a given number.

What we require is the number of positive integer solutions to this indefinite equation.

For example, when k=3, x=2, the solutions of the equations are:

⎧⎩⎨a1=1a2=1a3=2 ⎧⎩⎨a1=1a2=2a3=1 ⎧⎩⎨a1=2a2=1a3=1 The
input format
has one and only one line, which is two positive integers separated by spaces, in order k, x.

The output format
has one and only one line, which is the number of positive integer solutions to the equation.

Data range
1≤k≤100, 1≤x<231, k≤g(x)
Input sample:
3 2
Output sample:
3

train of thought

x x x^x xx is directly solved using the fast power, and the rest is the board of the bare partition methodC g ( x ) − 1 k − 1 C_{g(x) - 1}^{k - 1}Cg(x)1k1

the code

'''
对于g(x)使用快速幂计算
计算出结果,使用隔板法,计算结果
对于g(x)最多为1000,且查询次数较少,可以直接暴力
'''
from math import factorial
def qmi(a, k) :
    res = 1
    while k :
        if k & 1 :
            res = res * a % 1000
        k >>= 1
        a = a * a % 1000
    return res

def c(a, b) :
    return factorial(a) //(factorial(a - b) * factorial(b))
n, m = map(int, input().split())
print(c(qmi(m, m) - 1, n - 1))

sequence statistics

Given three integers N, L, R, count the number of monotonically non-decreasing sequences whose length is between 1 and N and whose element sizes are all between L and R.

Output the answer modulo 106+3.

Input Format
The first line of input contains an integer T, indicating the number of data sets.

The second to T+1th lines each contain three integers N, L, R.

Output Format
The output contains T lines, and each line has a number, which represents the result of the modulo 106+3 of your answer.

The data range
is 0≤N, L, R≤109, 1≤T≤100, and
the input data is guaranteed to be L≤R.

Input sample:
2
1 4 5
2 4 5
Output sample:
2
5
Sample explanation
For the first set of inputs, the two sequences that satisfy the condition are {4}, {5}.

train of thought

枚举长度k:a 1 ≤ a 2 ≤ , . . . , ≤ ak a_1 \le a_2 \le ,...,\le a_ka1a2,...,ak, translate everything to [ 0 , R − L ] [0, R - L][0,RL ]
projection is differencexi = ai − ai − 1 x_i = a_i - a_{i - 1}xi=aiai1, xi ≥ 0 x_i \geq 0xi0, ∑ i = 1 k x i ≤ R − L \sum_{i = 1}^kx_i \le R-L i=1kxiRL,若 x i + = 1 x_i += 1 xi+=1,则 x i > 0 x_i > 0 xi>0, ∑ i = 1 k x i ≤ R − L + 1 + k \sum_{i = 1}^kx_i \le R-L+1 + k i=1kxiRL+1+k , because it is an inequality here, so there is no need to allocate all of them, so an additional board is added. The count at this time isCR − L + kk C_{R - L + k}^kCRL+kk. The final answer is
insert image description here
to calculate the number of combinations for very large numbers using Lucas' theorem

the code

MOD = int(1e6) + 3

def qmi(a, k) :
	res = 1
	while k :
		if k & 1 :
			res = res * a % MOD
		k >>= 1
		a = a * a % MOD
	return res

def C(a, b) :
	if a < b :
		return 0
	up, down = 1, 1
	j = a
	for i in range(1, b + 1) :
		up = up * j % MOD
		down = down * i % MOD
		j -= 1
	return up * qmi(down, MOD - 2) % MOD

def lucas(a, b) :
	if a < MOD and b < MOD :
		return C(a, b)
	return C(a % MOD, b % MOD) * lucas(a // MOD, b // MOD) % MOD

T = int(input())

for _ in range(T) :
	n, l, r = map(int, input().split())
	print(lucas((r - l + n + 1, r - l + 1) - 1) % mod)

Addition & Multiplication Principles

addition principle

There are n types of ways to complete a project, ai ( 1 ≤ i ≤ n ) a_i(1 \le i \le n)ai(1in ) represents the number of methods of class i. Then to complete this thing, there areS = a 1 + a 2 + ⋯ + an S=a_1+a_2+\cdots +a_nS=a1+a2++andifferent methods.

multiplication principle

It takes n steps to complete a project, ai ( 1 ≤ i ≤ n ) a_i(1 \le i \le n)ai(1in ) represents the number of different methods for the ith step. Then to complete this thing, there areS = a 1 × a 2 × ⋯ × an S = a_1 \times a_2 \times \cdots \times a_nS=a1×a2××andifferent methods.

Placement of the car

There is a grid chessboard like the following, a, b, c, d
represent the length of the corresponding side, that is, the corresponding number of grids.

1.png

When a=b=c=d=2, it corresponds to the following board:

2.png

To put k cars that do not attack each other on this chessboard, that is, there are no two cars in the same row, and no two cars in the same column among the k cars. How many options are there?

Only need to output the result after the answer mod100003.

Input format
A total of one line, five non-negative integers a, b, c, d, k.

The output format
includes a positive integer, which is the result of mod100003 of the answer.

The data range
is 0≤a,b,c,d,k≤1000, ensuring that there is at least one feasible solution.

Input sample:
2 2 2 2 2
Output sample:
38

train of thought

For this car placement project, for this irregular figure, we can divide it into two pieces a ∗ ba * bab and( a + c ) ∗ d (a + c) * d(a+c)d graphics, place k cars, and by enumerating the number i of cars placed in the first block, the number k - i of cars placed in the second block can be determined. The different steps of placing two pieces can use the multiplication principleC bi ∗ A ai ∗ C di ∗ C a + c − ik − i C_b^i * A_a^i * C_d^i * C_{a + c - i}^{k -i}CbiAaiCdiCa+cikiGet the general plan. For each i is a way to complete this project, using the principle of addition ∑ i = 0 k C bi ∗ A ai ∗ C di ∗ C a + c − ik − i \sum_{i = 0}^kC_b^i * A_a^i * C_d^i * C_{a + c - i}^{k- i}i=0kCbiAaiCdiCa+cikiGet the general plan.

the code

N = 2010
MOD = 100003

fact = [0] * N
infact = [0] * N
def qmi(a, k) :
	res = 1
	while k :
		if k & 1 :
			res = res * a % MOD
		k >>= 1
		a = a * a % MOD
	return res

def init(n) :
	fact[0] = 1
	infact[0] = 1
	for i in range(1, n + 1) :
		fact[i] = i * fact[i - 1] % MOD
		infact[i] = qmi(i, MOD - 2) * infact[i - 1] % MOD
init(N - 1)

def C(a, b) :
	if a < b : return 0
	return fact[a] * infact[a - b] * infact[b] % MOD

def A(a, b) :
	if a < b : return 0
	return fact[a] * infact[a - b] % MOD
a, b, c, d, k = map(int, input().split())

res = 0

for i in range(k + 1) :
	res = (res + C(b, i) * A(a, i) * C(d, k - i) * A(a + c - i, k - i)) % MOD
print(res)

Principle of inclusion and exclusion

number triangle

Given an n×m grid, please calculate the total number of triangles with all three points on the grid.

The figure below shows a triangle on a 4×4 grid.

a.png

Note: The three points of the triangle cannot be collinear.


Input format Input a line, including two positive integers m and n separated by spaces .

Output format
Output a positive integer, which is the number of triangles sought.

Data range
1≤m, n≤1000
Input samples:
2 2
Output samples:
76

train of thought

General idea, the number of schemes to take 3 of all points C ( m + 1 ) ∗ ( n + 1 ) 3 C_{(m + 1) * (n + 1)}^3C(m+1)(n+1)3- The number of schemes with three collinear
points

  1. When the slope is 0 or infinite: ( n + 1 ) ∗ C m + 1 3 + ( m + 1 ) ∗ C n + 1 3 (n + 1) * C_{m+1}^3 + (m + 1) * C_{n + 1}^3(n+1)Cm+13+(m+1)Cn+13
  2. When the slope is greater than 0 or less than 0: by enumerating the distance between the horizontal and vertical coordinates of the points in the lower left corner and upper right corner ( i , j ) (i, j)(i,j ) , the method to form a collinear situation is( gcd ( i , j ) − 1 ) (gcd(i, j) - 1)(gcd(i,j)1 ) , the possible cases of the same distance are( n + 1 − i ) ∗ ( m + 1 − j ) (n + 1 - i) * (m + 1- j)(n+1i)(m+1j ) . The enumeration for all (i, j) is a scheme, which conforms to the principle of addition.

the code

def C(a) :
	if a < 3 :
		return 0
	return a * (a - 1) * (a - 2) // 6

def gcd(a, b) :
	return a if b == 0 else gcd(b, a % b)

m, n = map(int, input().split())
m, n = m + 1, n + 1
res = C(m * n) - n * C(m) - m * C(n)
for i in range(1, n + 1) :
	for j in range(1, m + 1) :
		res -= 2 * (gcd(i, j) - 1) * (n - i) * (m - j)
print(res)

Devu and flowers

Devu has N boxes, and there are Ai branches of flowers in the i-th box.

The flowers in the same box have the same color, and the flowers in different boxes have different colors. Devu needs to select M branches of flowers from these boxes to form a bunch, and find out how many schemes there are in total.

If the number of flowers of each color in two bunches of flowers is the same, the two bunches of flowers are considered to be the same scheme.

The result can only be output after taking the modulus of 10^9+7.

Input format
The first line contains two integers N and M.

The second line contains N space-separated integers, representing A1,A2,…,AN.

Output format
Output an integer, which represents the result of modulo 109+7 of the number of schemes.

Data range
1≤N≤20, 0≤M≤1014
, 0≤Ai≤1012 Input example:
3 5
1 3 2
Output example:
3

train of thought

Suppose the i-th box takes out xi x_ixi朵花,则足足0 ≤ xi ≤ A i ,∑ 1 N xi = M 0\le x_i\le A_i,\sum_1^Nx_i=M0xiAi1Nxi=M. _ Generalxi x_ixiProjection to yi = xi + 1 y_i = x_i + 1yi=xi+1 ,then≤ yi ≤ A i + 1 , ∑ 1 N yi = M + N 1 \le y_i\le A_i + 1, \sum_1^Ny_i=M + N1yiAi+1,1Nyi=M+N. _ If each box is unlimited, then the partition method can be used to obtain the scheme S:CM + N − 1 N − 1 C_{M + N - 1}^{N - 1}CM+N1N1. S i S_iSiFetch more than A i A_i for the i-th boxAiThe number of schemes of branches.
Using the principle of inclusion and exclusion: ans = S − ∑ S i + ∑ s ! = j S i US j + . . . + . . . . ans = S - \sum S_i + \sum_{s !=j} S_i U S_j + ... + ....ans=SSi+s!=jSiUSj+...+....
opposed toS i US j U . . . S_i U S_j U...SiUSjU ... expressed asCM + N − 1 − ( A i + 1 ) − ( A j + 1 ) − . . . . N − 1 C_{M + N - 1 - (A_i+ 1) - (A_j+ 1) - ....}^{N-1}CM+N1(Ai+1)(Aj+1)....N1
This formula has a total of 2 n 2^n2n items, by enumeration1 << n 1 << n1<<n can be obtained in all cases.

the code

MOD = int(1e9) + 7
N = 25
A = [0] * N

def qmi(a, k) :
	res = 1
	while k :
		if k & 1 :
			res = res * a % MOD
		k >>= 1
		a = a * a % MOD
	return res

def C(a) :
	if a < n - 1 :
		return 0
	j = a
	up = 1
	for i in range(1, n) :
		up = up * j % MOD
		j -= 1
	return up * qmi(down, MOD - 2) % MOD

down = 1
n, m = map(int, input().split())
for i in range(1, n) :
	down = down * i % MOD
A[:n] = list(map(int, input().split()))

# 容斥原理
res = 0
for i in range(1 << n) :
	sign = 1
	a = n + m - 1
	for j in range(n) :
		if i >> j & 1 :
			sign *= -1
			a -= A[j] + 1
	res = (res + sign * C(a)) % MOD
print(res)

Cattelan number

The general title has the property that the value of an attribute in any prefix is ​​less than or equal to the value of another attribute

grid

The streets of a certain city are grid-like, the coordinates of the lower left corner are A(0,0), and the coordinates of the upper right corner are B(n,m), where n≥m.

Now starting from point A(0,0), you can only walk to the right or right along the street, and you cannot pass the point on the upper left of the straight line in the figure, that is, any passing point (x, y) must satisfy x≥y, under these conditions, how many ways are there to reach B(n,m).

1.png

The input format
is a single line containing two integers n and m, indicating the size of the city block.

Output Format
Outputs an integer representing the total number of distinct schemes.

Data range
1≤m≤n≤5000
Input samples:
6 6
Output samples:
132

train of thought

Cattelan Number: Endpoint ( n , m ) (n, m)(n,m ) abouty = x + 1 y = x + 1y=x+1 Symmetric point (m - 1, n + 1)
ans = C n + mm (total movement) − C n + mm − 1 (illegal movement) ans = C_{n + m}^{m} (total move) - C_{n + m}^{m - 1} (illegal move)ans=Cn+mm( total movement )Cn+mm1( Illegal move )

the code

from math import factorial

def C(a, b) :
	if a < b :
		return 0
	return factorial(a) // (factorial(a - b) * factorial(b))
n, m = map(int, input().split())
print(C(n + m, m) - C(n + m, m - 1))

Summarize

The general feature of combined counting questions is that N is particularly large, so it is necessary to explore the nature orz in the question.

Guess you like

Origin blog.csdn.net/qq_57150526/article/details/129566030