[Modulo of combination] method summary

Source: Internet
Author: User

[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 <
  


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.