Dice (I) lightoj 1145

Source: Internet
Author: User

** Things **:

There are several ways to arrange n k-faces in a column so that the numbers on the face are S?

** Question type **:

Dynamic Planning

** Solutions **:

DP [I] [J] There are several methods to save the sum of the first I partitions as J.
Transition equation: DP [I] [J] = DP [I-1] [J-1] + dp [I-1] [J-2] +... + dp [I-1] [J-K];

There are N * s of statuses, And the transfer complexity is K. The simple practice will definitely time out. How can we optimize it?

Imagine the DP array as a two-dimensional matrix of the plane, and then the transfer equation, we can find that the transfer equations of DP [I] [J] and DP [I] [J-1] overlap a lot, as shown below:

DP [I] [J] = DP [I-1] [J-1] + dp [I-1] [J-2] +... + dp [I-1] [J-K];

DP [I] [J-1] = DP [I-1] [J-2] +... + dp [I-1] [J-K] + dp [I-1] [j-K-1];

Therefore, we can obtain DP [I] [J] = DP [I] [J-1]-DP [I-1] [j-K-1] + dp [I-1] [J-1];

The transfer has been optimized to O (1) complexity, but the DP array is N * s = W complexity. Therefore, we need to optimize the array to O (s ).

Although this question is classified as DP, there are no special restrictions, so there can be more efficient methods, and the O (n) complexity can be achieved by the principle of rejection.

If f (I) = n operators, at least I have more than K operators, and the total number of workers is S;

Then the answer is F (0)-f (1) + F (2)-f (3)... ± F (n)

F (I) method: N operators select I with C (n, I) types (this I is guaranteed to exceed K, other super does not matter ), in each case, the s-I * K should be divided into N parts and each part should be at least 1. So f (I) = C (n, I) * C (s-I * K-1, N-1 );

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.