Problem Statement
Hero is preparing a party for his friends. He has a round table with N seats. The seats are numbered 0 through N-1, in order. In other words, seats with consecutive numbers are adjacent, and seat N-1 is adjacent to seat 0.
Hero knows that exactly K friends will attend the party, and that each of them will arrive at a different time. Each time a new friend arrives, Hero has to assign him (or her) one of the empty seats at the table. The friend then sits there for the rest of the party. Hero is not sitting at the table.
For the purpose of this problem, a cluster is a maximal group of people that occupy consecutive chairs. For example, if there are people on chairs 3, 4, 5, and 6, while chairs 2 and 7 are empty, then these four people form a cluster.
At a party, clusters are good: people who sit in a cluster can talk to each other and have fun. A party with too many clusters is bad. Therefore, Hero wants to make sure that at no point in time are there more than G clusters at his table.
For example, let N = 4 and K = 3. That is, we have a table with four seats, and three friends are going to arrive. We will use A, B, and C to denote the three friends (in the order in which they arrive) and a period ('.') to denote an empty chair. So, for example, "ABC." denotes that A got seat 0, B seat 1, C seat 2, and seat 3 remained empty. The configurations ".ABC" and "C.AB" are considered different from "ABC." and from each other: the friends sit in the same order but on different seats.
Continuing our example, let G = 1. That is, we must never have more than one cluster. This constraint restricts the set of possible final configurations. For example, "ABC.", "C.AB", "B.CA", and ".BAC" are all possible, but "A.BC" and ".ACB" are not. (Note that if the final configuration were "A.BC", then the configuration before C arrived was "A.B.", which means that there was more than one cluster at that point in time.)
You are given the
Definition
- Class:
- Seatfriends
- Method:
- countseatnumb
- Parameters:
- int, int, int
- Returns:
- int
- Method signature:
- int countseatnumb(int N, int K, int G)
- (be sure your method is public)
Constraints
- N will be between 2 and 2000, inclusive.
- K will be between 1 and N, inclusive.
- G will be between 1 and K, inclusive.
Examples
3
2
1
Returns: 6
There are 6 ways how to seat your 2 friends: "AB.", "A.B", "BA.", "B.A", ".AB", and ".BA". All 6 are valid.
4
2
1
Returns: 8
The first friend can take any of the four seats. The second one must then sit next to him (on either side). Thus, there are 4*2=8 valid final configurations.
2
2
1
Returns: 2
5
4
2
Returns: 120
42
23
7
Returns: 917668006
2
1
1
Returns: 2
2
2
2
Returns: 2
3
1
1
Returns: 3
3
2
2
Returns: 6
3
3
1
Returns: 6
3
3
2
Returns: 6
3
3
3
Returns: 6
4
1
1
Returns: 4
4
2
2
Returns: 12
4
3
1
Returns: 16
4
3
3
Returns: 24
4
4
1
Returns: 16
4
4
2
Returns: 24
4
4
3
Returns: 24
4
4
4
Returns: 24
5
1
1
Returns: 5
5
2
1
Returns: 10
5
2
2
Returns: 20
5
3
1
Returns: 20
5
3
2
Returns: 60
5
3
3
Returns: 60
5
4
1
Returns: 40
5
4
2
Returns: 120
5
4
3
Returns: 120
5
4
4
Returns: 120
5
5
1
Returns: 40
5
5
2
Returns: 120
5
5
3
Returns: 120
5
5
4
Returns: 120
5
5
5
Returns: 120
1825
1
1
Returns: 1825
1473
2
1
Returns: 2946
372
2
2
Returns: 138012
1550
3
1
Returns: 6200
1995
3
2
Returns: 23844240
30
3
3
Returns: 24360
1809
4
1
Returns: 14472
249
4
2
Returns: 1828656
1274
4
3
Returns: 658030320
1121
4
4
Returns: 708240850
825
5
1
Returns: 13200
1300
5
2
Returns: 242377200
1039
5
3
Returns: 881988599
598
5
4
Returns: 28613662
125
5
5
Returns: 143752804
89
6
1
Returns: 2848
716
6
2
Returns: 347589360
1559
6
3
Returns: 624084216
160
6
4
Returns: 831518873
675
6
5
Returns: 605020990
894
6
6
Returns: 65110581
481
7
1
Returns: 30784
1744
7
2
Returns: 813515841
1933
7
3
Returns: 916411627
960
7
4
Returns: 70602307
1043
7
5
Returns: 411440578
1058
7
6
Returns: 67194312
1419
7
7
Returns: 948463110
1294
387
1
Returns: 517423343
1133
122
2
Returns: 619657076
1283
820
3
Returns: 920671482
1836
128
5
Returns: 778608950
140
93
6
Returns: 82260974
1105
851
7
Returns: 699885106
1200
1122
295
Returns: 116399779
220
24
24
Returns: 746435413
1040
787
286
Returns: 63613102
143
82
73
Returns: 862271504
1754
289
222
Returns: 342370244
1987
1545
989
Returns: 701439290
29
2
1
Returns: 58
1196
948
643
Returns: 809112049
1738
565
160
Returns: 678877322
1139
133
109
Returns: 288863401
1971
510
375
Returns: 340811356
1304
997
925
Returns: 774981568
1690
1321
565
Returns: 583950290
1976
1723
118
Returns: 956589962
1940
1811
1658
Returns: 15564497
1082
838
760
Returns: 557122456
1557
931
31
Returns: 425829267
1924
506
179
Returns: 877583060
1500
1
1
Returns: 1500
1500
2
1
Returns: 3000
1500
2
2
Returns: 2248500
1500
1500
1
Returns: 876072731
1500
1500
1500
Returns: 367552188
1995
1
1
Returns: 1995
1995
2
1
Returns: 3990
1995
2
2
Returns: 3978030
1995
1500
1
Returns: 550353452
1995
1500
2
Returns: 722163478
1995
1500
1500
Returns: 201274744
1995
1995
1
Returns: 646022887
1995
1995
2
Returns: 276589060
1995
1995
1500
Returns: 442383389
1995
1995
1995
Returns: 442383389
1999
1
1
Returns: 1999
1999
2
1
Returns: 3998
1999
1500
1
Returns: 45692503
1999
1500
2
Returns: 984021408
1999
1500
1500
Returns: 163147227
1999
1995
1
Returns: 588871926
1999
1995
2
Returns: 351570218
1999
1995
1500
Returns: 462522926
1999
1995
1995
Returns: 462522926
1999
1999
2
Returns: 487901650
1999
1999
1500
Returns: 100550147
1999
1999
1995
Returns: 100550147
1999
1999
1999
Returns: 100550147
2000
1
1
Returns: 2000
2000
2
1
Returns: 4000
2000
2
2
Returns: 3998000
2000
1500
1
Returns: 669527271
2000
1500
1500
Returns: 652588908
2000
1995
1
Returns: 413078464
2000
1995
2
Returns: 882660179
2000
1995
1500
Returns: 9169105
2000
1995
1995
Returns: 9169105
2000
1999
1
Returns: 609255382
2000
1999
2
Returns: 745105257
2000
1999
1500
Returns: 100292593
2000
1999
1995
Returns: 100292593
2000
1999
1999
Returns: 100292593
2000
2000
1
Returns: 609255382
2000
2000
2
Returns: 745105257
2000
2000
1500
Returns: 100292593
2000
2000
1995
Returns: 100292593
2000
2000
1999
Returns: 100292593
2000
2000
2000
Returns: 100292593