TopCoder problem "StringBeads" used in TCHS SRM 11 (Division I Level Three)



Problem Statement

    

You have beads of several different colors that are to be placed on a string, with the requirement that for any group of three adjacent beads, all three must have different colors.

You are given a int[] beads indicating how many of each color bead you have. The i-th element of beads is the number of beads of color i, where each i represents a distinct color. You are to return a long representing the number of ways they can be placed on the string, meeting the given requirement.

 

Definition

    
Class:StringBeads
Method:numWays
Parameters:int[]
Returns:long
Method signature:long numWays(int[] beads)
(be sure your method is public)
    
 

Notes

-The resulting arrangement of beads is linear, not circular. (Thus, the first bead on the string, and the last, are not adjacent.)
 

Constraints

-beads will contain between 3 and 5 elements, inclusive.
-Each element of beads will be between 1 and 10, inclusive.
-The total number of beads will not exceed 35.
 

Examples

0)
    
{ 1, 1, 1 }
Returns: 6
There are three beads, each a different color. The number of ways to arrange them is simply 3! = 6.
1)
    
{ 1, 1, 1, 1 }
Returns: 24
Now with four beads, there are 4! = 24 arrangements.
2)
    
{ 2, 1, 1 }
Returns: 2
Lets say that we have 2 green beads, 1 blue, and 1 red. In order to satisfy the requirement that each group of three consecutive beads have no two of the same color, the green beads must go at the beginning and end of the string. There are two ways to put the blue and the red in the middle: GRBG or GBRG.
3)
    
{ 3, 1, 1 }
Returns: 0
There is no way to meet the requirement.
4)
    
{ 3, 2, 2 }
Returns: 2

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=5913

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10063&pm=5913

Writer:

timmac

Testers:

PabloGilberto , brett1479 , Olexiy , marian

Problem categories:

Dynamic Programming