The idea of this question is to enumerate the number of each number from 1 to M. First, we can arrange the N numbers in full order and divide them by the factorial of the same number. The enumerated method is DFS. This task is completed in two steps. First, you can find the combination of the first K numbers and then make any combination.
The Code is as follows:
# Include <cstdlib> # include <cstdio> # include <cstring> # include <iostream> using namespace STD; // for a given m-plane failover, we throw N times, the first K is the largest K number in the enumeration, then sort and combine the remaining n-k numbers smaller than // the smallest number of the first K. // specify N, M, P, Kint N, M, P, and K; long long FAC [25]; long sdfs (int x, int box, long tot, int last) {If (x = 0 & box! = 0) {// if this number has been enumerated to 0 or has no place to return 0;} If (box = 0) return TOT/FAC [last]; // If the box is already equal to 0 long ret = 0; For (INT I = 0; I <= box; ++ I) {RET + = sdfs (x-1, box-I, TOT/FAC [I + last], 0);} return ret;} long fdfs (INT X, int N, int K, int P, long long ToT) {long ret = 0; If (x = 0 | n = 0) return 0; For (INT I = 0; I <= K; + + I) {if (I * x <p) {RET + = fdfs (x-1, N-I, K-I, p-I * X, TOT/FAC [I]);} else if (I * x = P & K = I) {RET + = sdfs (x, n-I, TOT, i) ;}} return ret;} int main () {FAC [0] = FAC [1] = 1; for (INT I = 2; I <= 20; + + I) {FAC [I] = FAC [I-1] * I ;} // calculate all factorial while (scanf ("% d", & N, & M, & K, & P) = 4) {If (M * k <p) {puts ("0"); continue;} printf ("% LLD \ n", fdfs (m, n, k, p, FAC [N]);} return 0 ;}