Description
Code Jam to I/O for Woman 2014 (3) Seating Chart (17 pts, 28 pts)
Problem (Simplified)
There are K round tables at the dinner, numbered 1 through K. It is important to have exactly the same number of people sitting at each table. If that is impossible (N is not divisible by K), then the table with the most people must have at most one more person sitting at it than the table with the fewest people.
Each of the N people will be assigned a unique number between 0 and N - 1. What matters is who is sitting next to whom, and not exactly where they’re sitting. In other words, two arrangements, A and B, are considered different if there exists a pair of numbers, α and β, such that persons α and β are sitting next to each other at the same table in arrangement A, but they are not sitting next to each other in arrangement B.
For example, if N is 5, and K is 2, we must have 3 people seated at one of the tables, and 2 people seated at the other table. Here is the list of all 10 of the possible arrangements:
1
2
3
4
5
6
7
8
9
10 [[0, 1, 2], [3, 4]]
[[0, 1, 3], [2, 4]]
[[0, 1, 4], [2, 3]]
[[0, 2, 3], [1, 4]]
[[0, 2, 4], [1, 3]]
[[0, 3, 4], [1, 2]]
[[1, 2, 3], [0, 4]]
[[1, 2, 4], [0, 3]]
[[1, 3, 4], [0, 2]]
[[2, 3, 4], [0, 1]]All other arrangements are similar to one of the arrangements above and are not counted as different. In particular, all of the following arrangements are considered to be the same:
1
2
3
4
5 [[0, 1, 2], [3, 4]]
[[2, 0, 1], [3, 4]]
[[1, 2, 0], [4, 3]]
[[0, 2, 1], [3, 4]]
[[3, 4], [0, 2, 1]]This is because the following pairs of people (and no other pairs) are sitting next to each other in each of these 5 arrangements:
1
2
3
4 0 and 1
0 and 2
1 and 2
3 and 4Another example is N = 5 and K = 3, which requires having two tables with two people each, and one table with a single person sitting at it. There are 15 possible arrangements in this case:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 [[0, 1], [2, 3], [4]]
[[0, 1], [2, 4], [3]]
[[0, 1], [3, 4], [2]]
[[0, 2], [1, 3], [4]]
[[0, 2], [1, 4], [3]]
[[0, 2], [3, 4], [1]]
[[0, 3], [1, 2], [4]]
[[0, 3], [1, 4], [2]]
[[0, 3], [2, 4], [1]]
[[0, 4], [1, 2], [3]]
[[0, 4], [1, 3], [2]]
[[0, 4], [2, 3], [1]]
[[1, 2], [3, 4], [0]]
[[1, 3], [2, 4], [0]]
[[1, 4], [2, 3], [0]]In this final example, N = 5 and K = 1, which means that we only have a single table, seating all 5 guests. Here, the answer is 12:
1
2
3
4
5
6
7
8
9
10
11
12 [[0, 1, 2, 3, 4]]
[[0, 1, 2, 4, 3]]
[[0, 1, 3, 2, 4]]
[[0, 1, 3, 4, 2]]
[[0, 1, 4, 2, 3]]
[[0, 1, 4, 3, 2]]
[[0, 2, 1, 3, 4]]
[[0, 2, 1, 4, 3]]
[[0, 2, 3, 1, 4]]
[[0, 2, 4, 1, 3]]
[[0, 3, 1, 2, 4]]
[[0, 3, 2, 1, 4]]Input
The first line of the input gives the number of test cases, T. T lines, each one containing two integers, N and K.
Output
For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the number of different possible seating arrangements.
Thinking Steps
Different arrangements of N people at one table
If we want to place N people on one table and starting from a random sit:
- for the first sit, we have N people to choose from,
- for the second sit, we have N-1 people to choose from,
- for the third sit, we have N-2 people to choose from
……
there will be $n(n-1)(n-2)…2*1 = n!$ different combinations.
But beside this, we need to pay attention to this 2 specific cases:
1 2 3 4 5
(clock-wise) presents the same as5 4 3 2 1
(anti-clockwise) in this problem, we divide n! by 2.1 2 3 4 5
(start counting from 1) presents the same as2 3 4 5 1
(start counting from 2) in this problem, we divide n! by n as we can count form n different positions.
Therefore there will be $n!/2/n = (n-1)!/2$ different combinations if we want to place N people at one table and ensure in each combination at least one person has a different neighbor.
Note: this formula is only valid when $n >= 3$, if $n=2$ or $n=1$, there will be only one arrangement.
Different arrangements of N people at K table
Case 1: N can be divided by K
If we have N=12, K=3, we will have 4 people at each table and there are $(4-1)!/2 = 3!/2 = 3$ combinations for each table.
Then we need to consider how we choose the people for each table. In the example above:
- we first choose 4 from 12 people (12 choose 4)
- we then choose 4 from 8 people (8 choose 4)
- the last 4 people automatically form 1 group (4 choose 4)
Beside this, the order of each group should not be considered. For example the combination of 1234, 5678 and 9 10 11 12 is the same as 5678, 9 10 11 12 and 1234. Therefore we need to divided the result by 3! (in general K!)
Putting them together, the final combination for this example should be
The general formula is
1 | // n can be divided by k |
Case 2: N cannot be divided by K
For example N=13 and K=3, we will have 2 tables which contains 4 people and 1 table contains 5 people. In this case we can divide this into 2 problem using divide-and-conquer:
- choose 5 from 13 people and count different arrangements of 5 people at 1 tables:
- different arrangements of 8 people at 2 tables
So in summary we have $15444*315 = 4864860$ different combinations.
1 | if (n % k == 0) { |
Combinatoric - N choose K
In order to get the value of combinatoric N choose K
quickly, we create a 2-dimensional array combi
to store the values. combi[n][k]
simply stores the value of N choose K
.
1 | 0C0 |
values:
1 | 1 |
The formula for (N choose K)
is
Therefore, N choose K+1
is
Beside this, we know for the simplest case: $nC0 = nCn = 1$.
Putting them together, we can generate a helper class (the system out are for your validation):
1 | // for the large dataset, n can be maximal 20 |
Factorial
We need another helper method to calculate the factorial:
1 | // for the large dataset, n can be maximal 20 |
Solution
1 | import java.util.*; |
Time Complexity: O(n*k)
Space Complexity: O(k)
Summary
- Quite difficult problem which requires good combinatoric knowledge.
- Use divide and conquer principle to handle the situation that n can’t be divided by k.
- Combinatoric and Factorial are good helper class.