TopcoderArchiveVisit Topcoder
TCO20 Apr 2018
2018 TCO 1A

RedDragonInn

Problem statement, definition, constraints, and public examples.

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:

  1. The inn takes half of the coins, rounded up to the nearest integer.
  2. The remaining coins, if any, are split evenly among the remaining characters.
  3. 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 int N: the number of characters who are still standing. You are also given the int X: the number of gold coins each of those characters received when the C coins were split between the inn and the players. Determine and return the largest possible value of C.

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

  1. 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.
  2. 8
    13
    Returns: 223
  3. 42
    1234
    Returns: 103739
Back to all problems