Acdream 1014 dice

Source: Internet
Author: User

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 ;}

 

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.