Algorithm: Permutation & Combination

Combination count

The existence of combinatorics is to study a set of discrete objects that satisfy certain conditions of the arrangement, construction and counting problems. Theoretical count is narrow Combinatorial Mathematics a basic research, research is primarily meet certain conditions permutations and counting problems. Composition comprising a counting principle count, counting method, counting formulas.

Combination count Fundamentals

Addition principle

\ [If one goal can be accomplished in different cases n and i for the first case there are different ways m_i, \\ then the total number N of the method: N = m_1 + m_2 + ... + m_n = \ sum_ {i = 1} ^ n m_i \]

Wherein each of the conditions to achieve a single goal can be achieved without depending on other conditions; any case either of the two methods are unique.

Multiplication principle

\ [Achieve a goal if after n steps, for the step k there m_k different ways, \\ then the total number N of the method: N = m_1 \ times m_2 \ times ... \ times m_n = \ prod_ {i = 1} ^ n m_i \]

Among them, there is the relationship between the steps may topology, but each step is essential; choose how not affect other steps within each step.

Inclusion-exclusion principle

Combined counts the most important thing is not repeated , do not miss . But there is always a lot of double counting occurs under normal circumstances, or omission of certain conditions in the classification discussion. Then we need to inclusion and exclusion. The basic idea of inclusion-exclusion principle is: not considered repeat, the number of cases come to all, and then exclude some double counting, because each step has reason to follow, do not miss the correct use can not be repeated.

Formula:
\ [set S is a finite set, A_i \ subseteq S, i \ in N ^ +, then \\ | \ bigcup_ {i = 1 } ^ n A_i | = \ sum_ {k = 1} ^ n (-1 ) ^ {k-1} \ sum_ {1 \ le i_1 <i_2 <... <i_k \ le n} | A_ {i_1} \ bigcap A_ {i_2} \ bigcap ... \ bigcap A_ {i_k} | \ ]
Inclusion and exclusion proved by mathematical induction, and the formula can be obtained by the following basic equation of set operations (DeMorgan equation):
\ [\ overline {\ bigcup_. 1} = {I} ^ n-A_i = \ bigcap_. 1 = {I} ^ n- \ overline {A_i} \\ \ overline {\ bigcap_ {i = 1} ^ n A_i} = \ bigcup_ {i = 1} ^ n \ overline {A_i} \]

Combination count basic formula

Arrangement number Formula

From any of n different elements taken m (m ≦ n) elements arranged in a row (considering the order of the elements has appeared), a known arrangement of m elements taken from n different elements. Is the total number of array arrangement number, i.e. the number of permutations of m is called the extraction elements (number of permutations) from the n different elements. Arrangement number indicated by symbol P (Permutation) or A (Arrangement).
\ [A_n ^ m (P_n ^ m) denotes the m removed from n elements and arranged in the program number obtained (the order to be considered). \\ A_n ^ m = n (n -1) ... (nm + 1) = {n! \ Over (nm)!} \]

expand

\ [\ Begin {aligned} & 1. cyclic permutations: circular arrangement also known, refers to the number of permutations N m remove elements constituting the cyclic permutations of n elements from the inside, N = {A_n ^ m \ over m} \\ & 2 . the n elements into k classes, the number of the i-th elements is n_i, at this time the number of permutations of n elements N = {n! \ over \ prod_ {i = 1} ^ k n_i!} \\\ end {aligned} \]

The number of combinations formula

From any of n different elements taken m (m ≦ n) constitute a group of elements (the order is not considered), a known combination of m elements taken from n different elements. The total number of combinations is the number of combinations, i.e., the number of combinations of m is called the extraction elements (number of combinations) from the n different elements. The number of combinations indicated by symbol C:
\ [C_n ^ m m represents number of programs removed (without regard to order) obtained from the group composed of n elements, the binomial coefficient may be expressed by (_n ^ m). \\ C_n ^ m = {A_n ^ m \ over m!} = {N! \ Over (nm)! M!} \]

expand

\ [K-earth element, are unlimited number of elements of each type, the number of combinations of these elements from the m for C_ {k + m-1} ^ m \]

The number of combinations of properties

\[ \begin{align} & 1.C_n^m=C_n^{n-m}\\ & 2.mC_n^m=nC_{n-1}^{m-1}\\ & 3.组合数递推式:C_n^m=C_{n-1}^{m-1}+C_{n-1}^m \\ & 4.二项式系数:\sum_{i=0}^n C_n^i=C_n^0+C_n^1+...+C_n^n=2^n \\ &\space\space C_n^0+C_n^2+...=C_n^1+C_n^3+...=2^{n-1} \\ & 5.C_m^m+C_{m+1}^m+...+C_{m+n}^m=C_{m+n+1}^{m+1} \\ & 6.C_n^0+C_{n+1}^1+...+C_{n+m}^m=C_{n+m+1}^m\\ & 7.C_{2n}^2=2C_n^2+n^2\\ \end{align} \]

错排 official

Wrong row of the problem is a combination of one of the classic problems of mathematics. Staggered arrangement i.e. error: For one arrangement consisting of n elements, reorder so that each element not in the original position, this arrangement is called a Staggered original arrangement. Number Staggered an array of n elements is denoted as D (n), then:
\ [\ the begin {align = left} & D (n) \\ = & n \ left [- {. 1 \ over1! {. 1 \ over0!}! } + {1 \ over2!} -... + {\ left \\ = & n (-1 ^ n \ right) \ over n!} \ right]! \ cdot \ sum_ {i = 0} ^ n {( ! -1) ^ i \ over i } = n \ cdot \ sum_ {i = 2} ^ n {(-!! 1) ^ i \ over i} \\ = & \ left [{n \ over e}! +0.5 \ right] (rounded, introduced by {1 \ over e} expandable) \ end {align} \]

Common techniques combined count

Bundled law

When asked certain elements must be contiguous, put them as a whole, then as a whole and other elements considered together. To note: there are a whole internal order.

Interpolation Method

When not require certain elements adjacent, other elements may be first lined, and then the required non-adjacent elements into voids or both ends of the element has been properly arranged.

Partition method

In addressing the problem of several packets of the same element, if required at least one element per group, may be inserted into the number of groups minus one "spacer" Among these elements in a row, to achieve the purpose of the packet.
\ [Example: n balls m boxes, each box is not empty, there C_ {n-1} ^ { m-1} kinds of programs; non-empty if the box is not required, may be by the introduction of m \\ balls, each of the partition method to solve the ball is removed from each of a box, the program number C_ {n + m-1} ^ {m-1} \]

example

  1. A, B, C, D, E five in a row, where A, B do not stand together, a total of how many STATION?
    \ [Interpolation Method: as A, B can not stand together, so we can consider give C, D, E sorting program number A_3 ^ 3; \\ 3 people sorted out after leaving four gaps, A, interpolation B, the program number A_4 ^ 2, according to the multiplication principle, the total number of programs A_3 ^ 3 \ cdot A_4 ^ 2 . \]

  2. A, B, C, D, E five in a row, where A, B must stand together, a total of how many STATION?
    \ [Bundle method: Since A, B must stand together, then we simply put the A, B, "bundling", viewed as a whole, the overall \\ then treated equally and C, D, E. For internal A, B's, the number of programs A_2 ^ 2; \\ for external program number A_4 ^ 4. Total scheme: A_2 ^ 2 \ cdot A_4 ^ 4. \]

  3. There is a program on the table three programs, maintains its same relative order, then add two new programs, how many kinds of programs?
    \ [\ Begin {align} & bundle + interpolation method in Method: \\ & two cases: 1 \\ & neighboring two new program, the three programs have four empty, then consider internal sequence, the total program atoms C_4 ^ 1 \ cdot A_2 ^ 2 ;. \\ & 2 shows two non-adjacent, two four empty interpolation program, program number A_4 ^ 2; \\ & then the total number of programs i.e. C_4 ^ 1 \ cdot A_2 ^ 2 + A_4 ^ 2. \ end {align} \]

  4. The eight identical balls into three different boxes, each box required to put at least one ball, how many ways?
    \ [Method separator: the eight balls in a row, the number of gaps between the balls 7, assume that we now have two separators, separator 7 \\ inserted two vacancies, there can be divided into ball sequence of three groups, corresponding to the three boxes, the program number C_7 ^ 2. \]

  5. (Hdu 6397) that Italy: n-number to three, m, k, selected in 0 ~ n-1 the number m of a row such that their equal and k, which may be the same number m, as long as the arrangement of different It can be. Seeking a total of how many arrangements to meet the meaning of the questions.

\ [N-m spherical cartridge similar problems may be empty cassette. Irrespective of the size limit numbers, the total number of programs C_ {k + m-1} ^ {m-1}. \\ set number is x, there x_1 + x_2 + ... + x_m = k; \\ order y_i = x_i + 1, the separator was analyzed: y_1 + y_2 + ... + y_m = k + m. \\ but the size can limit the number of topics within n, then consider this: Suppose there are i number \\ beyond our limited enumeration, i.e. the number m, there is a greater than or equal x n, then number scheme at this time is: \\ C_m ^ i \ cdot C_ {k + m-1-i \ times n} ^ {m-1}, the corresponding analysis: \\ x_1 '+ x_2' + ... + x_m '= ki \ times n. \\ Finally, the inclusion-exclusion odd plus even be able to reduce the answer. \]

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn = 2e5 + 100;
const int mod = 998244353;

ll inv[maxn], F[maxn], Finv[maxn];

void init() {
    inv[1] = 1;
    for(int i = 2; i < maxn; i++)
        inv[i] = (mod - mod / i) * inv[mod % i] % mod;
    F[0] = Finv[0] = 1;
    for(int i = 1; i < maxn; i++) {
        F[i] = F[i - 1] * i % mod;
        Finv[i] = Finv[i - 1] * inv[i] % mod;
    }
}

ll C(ll n, ll m) {
    if(n < 0 || m < 0 || m > n) return 0;
    return F[n] * Finv[m] % mod * Finv[n - m] % mod;
}

int main() {
    int t, n, m, k;
    init();
    cin >> t;
    while(t--) {
        cin >> n >> m >> k;
        ll ans = 0;
        for(int i = 0; i * n <= k; i++) {
            int r = C(m, i) * C(k - i * n + m - 1, m - 1) % mod;
            if(i & 1) ans = (ans - r + mod) % mod;
            else ans = (ans + r) % mod;
        }
        cout << ans << endl;
    }
    return 0;
}
  1. (Uva 10943) of k does not exceed the demand of non-negative integer n add up to the number of such programs and the n. Method separator can empty cassette. Since the data range is relatively small, so the direct result of all the pre-dp, O (1) query.

    #include<bits/stdc++.h>
    using namespace std;
    const int mod = 1e6;
    int n, k, dp[111][111]; // dp[i][j]表示用j个数使得和为i的方案数
    int main() {
     for (int i = 1; i <= 100; i++) {
         dp[i][1] = 1;
         dp[1][i] = i;
     }
     for (int i = 2; i <= 100; i++)
         for (int j = 2; j <= 100; j++)
             dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % mod;
     while (cin >> n >> k && (n|k)) { 
            cout << dp[n][k] << endl;
     }
        return 0;
    }
  2. (CF GYM 100548) n are arranged in a flower colors painted with m, the adjacent different color requirements, and using exactly the number of colors to be k. Seeking the number of programs% 1e9 + 7.

    Analysis: + multiplication principle of inclusion and exclusion.
    \ [\ Begin {align} & m is selected from the first color in color with k Alternatively, the program number C_m ^ k; \\ & multiplication principle: for the first flower, there are k choices, for each subsequent flower, & \\ not repeated in order to ensure that only the (k-1) choices, the program number & \\ k \ times (k-1) ^ {n-1}. \\ & but this time using the color contains a number of deficiencies turnover number k program requires the inclusion-exclusion principle excluded. \\ & taken from consideration in the i k colors painted, the program number C_k ^ i \ cdot p (p -1) ^ {n-1}, the odd-repellent binding capacity reduction even increase, can be drawn: \\ & overall program number N = C_m ^ k \ cdot k (k-1) ^ {n-1} + \ sum_ {i = 2} ^ {k-1} (- 1) ^ iC_k ^ i \ cdot i (i -1) ^ {n-1} \\ & obviously need to use an inverse element, the number of combinations, rapid power, following a standard process. \ end {align} \]

    #include <iostream>
    using namespace std;
    typedef long long ll;
    
    const int mod = 1e9 + 7;
    const int maxn = 1000010;
    
    ll ans, inv[maxn], F[maxn], Finv[maxn];
    
    ll qpow(ll a, ll b) {
        ll ans = 1;
        while(b) {
            if(b & 1) ans = (ans * a) % mod;
            b >>= 1; a = (a * a) % mod;
        }
        return ans;
    }
    
    void init() {
        inv[1] = 1;
        for(int i = 2; i < maxn; i++)
            inv[i] = (mod - mod / i) * inv[mod % i] % mod;
        F[0] = Finv[0] = 1;
        for(int i = 1; i < maxn; i++) {
            F[i] = F[i - 1] * i % mod;
            Finv[i] = Finv[i - 1] * inv[i] % mod;
        }
    }
    
    ll C(ll n, ll m) {
        if(n < 0 || m < 0 || m > n) return 0;
        return F[n] * Finv[m] % mod * Finv[n - m] % mod;
    }
    
    int main() {
     init();
     int t, kase = 0;
        cin >> t;
        while (t--) {
            cin >> n >> m >> k;
            ans = C(m, k) * k % mod * qpow(k - 1, n - 1) % mod;
            ll sign = 1;
            for(ll i = 2; i < k; i++) {
             ans = (ans + C(k, i) * i % mod * qpow(i - 1, n - 1) % mod * sign + mod) % mod;
             sign = -sign;
            }
            cout << "Case #" << ++kase << ": " << ans << endl;
        }
        return 0;
    }

Guess you like

Origin www.cnblogs.com/Li-F/p/11894612.html
Recommended