Statistics

Problem Statement for "TokenDoublingGame"

Problem Statement

You are playing a coin-flipping game. At the beginning of the game you are given an int N and a fair coin.

The game starts with N tokens on the table and 1 token in your hand.

In each round of the game, you flip the coin and act according to the outcome:

  • Heads: Discard your hand (the tokens disappear). Then, take two new tokens. Place one on the table and take the other into your hand. If there are now 2*N tokens on the table, the game ends.
  • Tails: Let X be the number of tokens in your hand. Take another X tokens from the table into your hand. If the table becomes empty during or after you do that, the game ends.

The expected number of coin flips during this game can be represented as a reduced fraction A/B. Let B^(-1) be the inverse element to B, modulo 10^9 + 7. Compute and return the value (A * B^(-1)) modulo 10^9 + 7.

Definition

Class:
TokenDoublingGame
Method:
expectation
Parameters:
int
Returns:
int
Method signature:
int expectation(int N)
(be sure your method is public)

Notes

  • For each valid input the value B^(-1) exists.

Constraints

  • N will be between 1 and 100,000, inclusive.

Examples

  1. 100000

    Returns: 386506648

  2. 99999

    Returns: 662623410

  3. 1

    Returns: 1

    The game ends after the first coin flip, regardless of its outcome.

  4. 2

    Returns: 333333339

    The exact answer as a reduced fraction is 10/3. Computing modulo 10^9 + 7, we have 10 * 3^(-1) = 10 * 333,333,336 = 333,333,339.

  5. 3

    Returns: 86956528

  6. 4

    Returns: 143133472

  7. 5

    Returns: 531208514

  8. 70

    Returns: 486692849

  9. 148

    Returns: 666044228

  10. 67

    Returns: 804558285

  11. 262

    Returns: 309970392

  12. 27

    Returns: 465154386

  13. 275

    Returns: 26615014

  14. 274

    Returns: 896355842

  15. 221

    Returns: 824305511

  16. 210

    Returns: 161846485

  17. 58

    Returns: 654834340

  18. 50575

    Returns: 253487369

  19. 21751

    Returns: 707262782

  20. 73252

    Returns: 576761676

  21. 17612

    Returns: 379246407

  22. 18651

    Returns: 94871607

  23. 99751

    Returns: 738254848

  24. 36700

    Returns: 140916593

  25. 22852

    Returns: 40486574

  26. 29660

    Returns: 887215598

  27. 89924

    Returns: 305815617

  28. 96159

    Returns: 61482976

  29. 50172

    Returns: 908406394

  30. 68305

    Returns: 34282102

  31. 56303

    Returns: 735678350

  32. 93872

    Returns: 554694722

  33. 72951

    Returns: 329809157

  34. 95429

    Returns: 502874877

  35. 95827

    Returns: 583227061

  36. 64640

    Returns: 916664936

  37. 75575

    Returns: 912198438

  38. 58858

    Returns: 932472953

  39. 91775

    Returns: 130113917

  40. 50452

    Returns: 390570688

  41. 62394

    Returns: 704738027

  42. 64202

    Returns: 820058898

  43. 99809

    Returns: 923597862

  44. 99833

    Returns: 349502302

  45. 99841

    Returns: 364463649

  46. 99914

    Returns: 944687452

  47. 99866

    Returns: 236232328

  48. 13

    Returns: 444154848

    Here, the exact answer is 2795844533351/105312396244, which means that the return value is (2795844533351 * 105312396244^(-1)) mod (10^9 + 7).


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: