Problem Statement
Red Dragon Inn is a board game in which the players' characters spend time in an inn brawling and drinking.
At any moment each character has some gold coins. If a character has "too much fun" during the game, they pass out. When that happens, the gold they have is divided between the inn and all other characters who are left standing. This works as follows:
- The inn takes half of the coins, rounded up to the nearest integer.
- The remaining coins, if any, are split evenly among the remaining characters.
- The remaining coins, if any, are taken by the inn again. (This occurs whenever in step 2 the number of coins isn't divisible by the number of players.)
A character just passed out.
Let C be the number of coins they had.
You do not know the value of C.
You are given the
Definition
- Class:
- RedDragonInn
- Method:
- maxGold
- Parameters:
- int, int
- Returns:
- int
- Method signature:
- int maxGold(int N, int X)
- (be sure your method is public)
Constraints
- N will be between 2 and 100, inclusive.
- X will be between 1 and 100,000, inclusive.
Examples
3
2
Returns: 17
The process of dividing 17 coins between the inn and three characters looks as follows: First, the inn gets 9 coins (half of all coins, rounded up). Next, the remaining 8 coins are divided between the three characters. This means that each of them gets 2 coins (i.e., 8/3, rounded down). Finally, the inn takes the 2 coins that were left over. It can be shown that 17 is the largest initial amount of coins for which the players receive two coins each. For example, when dividing 18 coins the inn takes 9 and then each player takes 3.
8
13
Returns: 223
42
1234
Returns: 103739
2
1
Returns: 7
100
100000
Returns: 20000199
2
99999
Returns: 399999
99
1
Returns: 395
48
520
Returns: 50015
16
6044
Returns: 193439
5
39306
Returns: 393069
29
989
Returns: 57419
8
98894
Returns: 1582319
82
34
Returns: 5739
40
70909
Returns: 5672799
4
50516
Returns: 404135
85
77015
Returns: 13092719
46
98417
Returns: 9054455
90
9
Returns: 1799
43
2730
Returns: 234865
94
73087
Returns: 13740543
7
41176
Returns: 576477
11
51856
Returns: 1140853
74
26990
Returns: 3994667
51
9788
Returns: 998477
6
7590
Returns: 91091
67
24356
Returns: 3263837
15
61632
Returns: 1848989
40
9533
Returns: 762719
67
70168
Returns: 9402645
47
80518
Returns: 7568785
57
63257
Returns: 7211411
2
69201
Returns: 276807
91
66651
Returns: 12130663
18
40348
Returns: 1452563
57
6650
Returns: 758213
86
75485
Returns: 12983591
59
29043
Returns: 3427191
66
43071
Returns: 5685503
53
337
Returns: 35827
16
43465
Returns: 1390911
65
34656
Returns: 4505409
86
1095
Returns: 188511
10
9999
Returns: 199999