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 (,,???,,))