Number Theory Tour 6---combinations (Combination dafa good (,,???,,))

Source: Internet
Author: User

The number of combinations is not strange (´@@facesymbol@@ ')

We've all learned how to combine numbers.

Will you find the number of combinations?

Generally we use Yang Hui triangle property

Line N, the first M is, C (n, m) (starting from 0)

On the computer we open an array to save, like this

Using recursion to deduce

1#include <cstdio>2 Const intN = -+5;3 Const intMOD = (int) 1e9 +7;4 intComb[n][n];//Comb[n][m] is C (n,m)5 voidinit () {6      for(inti =0; i < N; i + +){7comb[i][0] = Comb[i][i] =1;8          for(intj =1; J < I; J + +){9COMB[I][J] = comb[i-1][J] + comb[i-1][j-1];TenCOMB[I][J]%=MOD; One         } A     } - } - intMain () { the init (); -}

The complexity of this method is O (n^2), there is no O (n) practice, of course (´@@facesymbol@@ ')

Because most of the problems have redundancy, so we can use the principle of inverse (no problem of redundancy, in fact, you can also open the MoD itself a little bit, so the same can be done with the inverse of the yuan)

According to this formula

We need to find factorial and inverse factorial.

The code is as follows:

1#include <cstdio>2 Const intN =200000+5;3 Const intMOD = (int) 1e9 +7;4 intF[n], Finv[n], inv[n];//f is factorial, FINV is the factorial of inverse element5 voidinit () {6inv[1] =1;7      for(inti =2; i < N; i + +){8Inv[i] = (mod-mod/i) * 1LL * inv[mod% i]%MOD;9     }Tenf[0] = finv[0] =1; One      for(inti =1; i < N; i + +){ AF[i] = f[i-1] * 1LL * I%MOD; -Finv[i] = finv[i-1] * 1ll * Inv[i]%MOD; -     } the } - intComb (intNintm) {//comb (n, m) is C (n, m) -     if(M <0|| M > N)return 0; -     returnF[N] * 1ll * finv[n-m]% MOD * Finv[m]%MOD; + } - intMain () { + init (); A}

The combination of Dafa good, to understand the use of good (. -' ω´-')

Number Theory Tour 6---combinations (Combination dafa good (,,???,,))

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.