[Modulo of combination] method summary
1. Use the unique integer decomposition theorem to calculate (n + 1-m) * (n + m )! /(M! * (N + 1 )! )
Any positive integer has only one way to write the form of multiplication of the prime factor power. For example, 18 = 2*3 ^ 2
A = (p1 ^ k1) * (p2 ^ k2) * (p3 ^ k3) * (p4 ^ k4 )*...... * (pn ^ kn) pi is a prime number
We also regard the class as a number, which is better than m! How to find m! What is the index of prime number 2 in it?
Cnt = 0; while (m) {m/= 2; cnt + = m;}. Why? Consider m = 4, then m! = 4*3*2*1, the first m/= 2, is the calculation m! Therefore, cnt + = 2 has two contributions:
The second m/= 2 is the calculation of m! The number can be divided into 4, and one number contributes a prime number 2.
Therefore, we should first sieve out the prime number in the maximum range, and then consider each prime number. The key is to find the index of the prime number of the entire formula.
Template:
Bool isprime [maxn * 2 + 10]; int prime [maxn * 2 + 10]; int len = 0; // Number of prime numbers int n, m; void sieve (int n) // The prime number {for (int I = 0; I <= n; I ++) isprime [I] = 1; isprime [0] = isprime [1] = 0; for (int I = 2; I <= n; I ++) if (isprime [I]) {prime [len ++] = I; for (int j = 2 * I; j <= n; j + = I) isprime [j] = 0 ;}} int cal (int p, int n) // calculate n! How many p are involved in the multiplication {int ans = 0; while (n) {n/= p; ans + = n;} return ans;} int main () {sieve (maxn * 2); long ans = 1; // remember to use long cin> n> m; int nm = n + 1-m; for (int I = 0; I
= Mod) ans % = mod ;}} cout <