Statistics

Problem Statement for "DecreasingPopulation"

Problem Statement

Once there was a planet with S inhabitants.

The planet then faced N bad years in a row. Each year exactly one bad thing happened: either the planet got hit by a small asteroid, or the planet got visited by the space dragon.

Preserved historical records tell us the following:

  • The space dragon always ate exactly one inhabitant.
  • An asteroid hit always killed exactly one half of the population.
  • During the N years nobody was born and nobody died for another reason.

Given all this information we would like to determine the current population of this planet. However, the answer to this question is not necessarily unique. Count all options that are consistent with all the above information, and return that count.

Definition

Class:
DecreasingPopulation
Method:
count
Parameters:
int, int
Returns:
int
Method signature:
int count(int S, int N)
(be sure your method is public)

Notes

  • One half of zero is zero. Hence, it is possible that after everyone died there were some additional asteroid hits.

Constraints

  • S will be between 1 and 5,000, inclusive.
  • N will be between 1 and 5,000, inclusive.

Examples

  1. 24

    1

    Returns: 2

    The original population consisted of S = 24 people. There was N = 1 bad event. The answer is 2 because now there are two options: If there was an asteroid hit, the current population is 12. If there was a dragon visit, the current population is 23.

  2. 17

    1

    Returns: 1

    As the original population was odd, we can deduce that the only bad event must have been a dragon visit and thus the current population must be 16.

  3. 2

    2

    Returns: 1

    Regardless of what the first event was, it killed one person. The second event must have been a dragon visit that killed the other person. Thus, there is only one valid option: the current population is 0.

  4. 30

    3

    Returns: 4

  5. 1

    5000

    Returns: 1

  6. 5000

    5000

    Returns: 1

  7. 2883

    54

    Returns: 245

  8. 3525

    4536

    Returns: 1

  9. 3717

    86

    Returns: 369

  10. 2814

    2109

    Returns: 355

  11. 4198

    74

    Returns: 348

  12. 3215

    336

    Returns: 780

  13. 3429

    92

    Returns: 377

  14. 194

    840

    Returns: 1

  15. 6

    66

    Returns: 1

  16. 3945

    4479

    Returns: 1

  17. 75

    70

    Returns: 5

  18. 4650

    3504

    Returns: 575

  19. 1885

    65

    Returns: 247

  20. 4287

    4211

    Returns: 40

  21. 2942

    78

    Returns: 320

  22. 1983

    273

    Returns: 555

  23. 4596

    73

    Returns: 353

  24. 2896

    4924

    Returns: 1

  25. 2801

    85

    Returns: 338

  26. 4712

    1606

    Returns: 1555

  27. 1955

    100

    Returns: 325

  28. 290

    1029

    Returns: 1

  29. 2736

    56

    Returns: 250

  30. 2973

    4738

    Returns: 1

  31. 3143

    82

    Returns: 336

  32. 4425

    2072

    Returns: 1179

  33. 796

    91

    Returns: 203

  34. 1860

    2744

    Returns: 1

  35. 1791

    80

    Returns: 273

  36. 903

    3414

    Returns: 1

  37. 4954

    72

    Returns: 353

  38. 2346

    1958

    Returns: 196

  39. 4549

    66

    Returns: 322

  40. 2925

    4364

    Returns: 1

  41. 4632

    52

    Returns: 270

  42. 3965

    2558

    Returns: 706

  43. 4610

    99

    Returns: 439

  44. 3240

    2132

    Returns: 556

  45. 4255

    64

    Returns: 305

  46. 4968

    934

    Returns: 1478

  47. 1697

    74

    Returns: 256

  48. 3977

    2074

    Returns: 954

  49. 4945

    85

    Returns: 405

  50. 1223

    3637

    Returns: 1

  51. 125

    60

    Returns: 35

  52. 4481

    679

    Returns: 1292

  53. 612

    67

    Returns: 153

  54. 4189

    4480

    Returns: 1

  55. 3904

    100

    Returns: 421

  56. 3569

    2995

    Returns: 289

  57. 272

    88

    Returns: 92

  58. 2171

    4986

    Returns: 1

  59. 4235

    93

    Returns: 405

  60. 3866

    2581

    Returns: 645

  61. 183

    86

    Returns: 51

  62. 1991

    1732

    Returns: 132

  63. 2835

    77

    Returns: 313

  64. 1508

    1448

    Returns: 32

  65. 3849

    62

    Returns: 295

  66. 1048

    807

    Returns: 123

  67. 1120

    96

    Returns: 249

  68. 4667

    2819

    Returns: 926

  69. 4321

    75

    Returns: 355

  70. 886

    2187

    Returns: 1

  71. 3637

    50

    Returns: 243

  72. 1003

    3393

    Returns: 1

  73. 551

    73

    Returns: 151

  74. 170

    2165

    Returns: 1

  75. 342

    58

    Returns: 102

  76. 1485

    4192

    Returns: 1

  77. 4023

    62

    Returns: 292

  78. 4053

    4328

    Returns: 1

  79. 3716

    86

    Returns: 372

  80. 4648

    82

    Returns: 389

  81. 605

    61

    Returns: 144

  82. 1234

    4452

    Returns: 1

  83. 2772

    90

    Returns: 353

  84. 383

    1922

    Returns: 1

  85. 502

    74

    Returns: 146

  86. 1009

    182

    Returns: 300

  87. 5000

    200

    Returns: 724

  88. 100

    5000

    Returns: 1

  89. 2000

    200

    Returns: 476

  90. 1

    2

    Returns: 1


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: