[ACM] HDU 4248 a famous Stone collector (DP + combination)

Source: Internet
Author: User

A famous Stone collector

Problem descriptionmr. B loves to play with colorful stones. There are n colors of stones in his collection. Two stones with the same color are indistinguishable. MR. B wocould like
Select some stones and arrange them in line to form a beautiful pattern. after several arrangements he finds it very hard for him to enumerate all the patterns. so he asks you to write a program to count the number of different possible patterns. two patterns are considered different, if and only if they have different number of stones or have different colors on at least one position.
Inputeach test case starts with a line containing an integer n indicating the kinds of stones mr. B have. Following this is a line containing N integers-the number
Available stones of each color respectively. All the input numbers will be nonnegative and no more than 100.
Outputfor each test case, display a single line containing the case number and the number of different patterns mr. B can make with these stones, modulo 1,000,000,007,
Which is a prime number.
Sample Input
31 1 121 2
 
Sample output
Case 1: 15Case 2: 8HintIn the first case, suppose the colors of the stones Mr. B has are B, G and M, the different patterns Mr. B can form are: B; G; M; BG; BM; GM; GB; MB; MG; BGM; BMG; GBM; GMB; MBG; MGB. 
 
Sourcefudan local programming contest 2012


Question:

There are n colors of the stone, each color has num [I] blocks, each color of the stone is not differentiated, in so many stones choose the stone to form a line, how many sorting methods are available? The line length is (1 to num [I] + num [2] + num [3] + ..... num [N];

For example, there are three color stones, B, G, M, indicating the color, each color of a stone, then the possible sequence is B; G; m; BG; BM; GM; GB; MB; mg; BGM; BMG; GBM; GMB; mbg; MGB

Using DP [I] [J] to represent a sequence of J lengths arranged with stones of the first I colors

In this case:

① The Stone of the I color is not used, so DP [I] [J] = DP [I-1] [J];

② Use the stone of the I color (the sequence can reach the length of the original length plus the number of the I color), there are num [I, remove K from the original sequence of stones and K from the I-colored stones. Here (k <= J) is inserted into the original sequence, there are a total of J positions, and you can select K from them (not just plug-ins, it can be understood that to form a series of length j, there are j positions, first, let the K stones in the I color pick K positions, which includes adjacent and non-adjacent situations, because the same color is not distinguished, there are C [J] [k] methods, and then let the rest of the stone sequence in order to enter each empty position in order ).

DP [I] [J] + = DP [I-1] [J-K] * C [J] [k];

Code:

# Include <iostream> # include <algorithm> # include <string. h> using namespace STD; typedef long ll; const int maxn = 110; // maximum number of types const int mod = 1000000007; ll DP [maxn] [maxn * maxn]; // indicates the number of methods that use the first I colors to form a sequence with a length of J ll C [maxn * maxn] [maxn]; // select J from the current sequence length plus 1, and J will not exceed maxnint N; // int num [maxn] In N colors; // how many stones each color has int sum; // The longest sequence length void GETC () {c [0] [0] = C [1] [0] = C [1] [1] = 1; for (INT I = 2; I <maxn * maxn-1; I ++) {C [I] [0] = 1; for (Int J = 1; j <maxn-1; j ++) {if (I = J) c [I] [J] = 1; else {C [I] [J] = C [I-1] [J] + C [I-1] [J-1]; c [I] [J] % = mod ;}}} int dp () {memset (DP, 0, sizeof (DP); // some initialization values are 0, DP [I] [J] = DP [I-1] [J] is used later .. for (INT I = 1; I <= N; I ++) DP [I] [0] = 1; for (INT I = 1; I <= num [1]; I ++) DP [1] [I] = 1; int tsum = num [1]; for (INT I = 2; I <= N; I ++) // a total of N {tsum + = num [I]; // The total length of the first I color, for (Int J = 1; j <= tsum; j ++) {DP [I] [J] = DP [I-1] [J]; // for the current length j, one case is that the stone in the I-colored for (int K = 1; k <= num [I] & K <= J; k ++) // use the current color stone, but remove a certain length, fill {DP [I] [J] + = DP [I-1] [J-K] * C [J] [k] with the new color stone; // Why c [J] [k]? It can be understood that there are a total of J positions, so that we can select K positions for the color in I, then let the remaining position put down the original color order. DP [I] [J] % = mod ;}} ll ans = 0; For (INT I = 1; I <= sum; I ++) {ans + = DP [N] [I]; ans % = MOD;} return ans ;}int main () {GETC (); int CAS = 1; while (CIN> N) {sum = 0; For (INT I = 1; I <= N; I ++) {CIN> num [I]; sum + = num [I];} cout <"case" <CAS ++ <":" <dp () <Endl;} 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.