Statistics

Problem Statement for "JumpAcrossTheRiver"

Problem Statement

There is a wide river. Somebody placed a sequence of N+1 stones into the river along a line that goes from one river bank to the other. People can now use these stones to jump across the river.

We will number the stones from 0 to N. Stone 0 is on one bank of the river, stone N is on the other bank, the other stones lie between them in numerical order. For the purpose of this problem, assume that each stone is a point and that all these points lie on one straight line.


For each valid i, the distance between stones i and i+1 is equal to 1 + ((i*i) mod M). For example, if M = 10, the distance between stones 6 and 7 is 1 + (36 mod 10) = 1 + 6 = 7.


You want to get across the river by making at most J jumps, but you are afraid that your maximum jump length might be too short for that. Calculate and return the smallest L such that a person capable of making jumps of length at most L can get across the river in at most J jumps.

(When jumping across the river, the first jump must begin on stone 0; each jump must end on a stone; and in particular, the last jump must end on stone N.)

Definition

Class:
JumpAcrossTheRiver
Method:
minLength
Parameters:
int, int, int
Returns:
long
Method signature:
long minLength(int N, int M, int J)
(be sure your method is public)

Notes

  • It can be shown that the smallest L we seek is always an integer.

Constraints

  • N will be between 1 and 1,000,000, inclusive.
  • M will be between 1 and 10^9 + 7, inclusive.
  • J will be between 1 and N, inclusive.

Examples

  1. 10

    1

    1

    Returns: 10

    There are 10+1 = 11 rocks in this river, and as M = 1, the distance between any two consecutive rocks is 1. Hence, the distance between the two banks of the river is 10. We want to get across this river in a single jump. Clearly, the jump length we need for that is at least 10.

  2. 10

    1

    3

    Returns: 4

    L = 4 is sufficient to get across this river in three jumps. E.g., we can jump from stone 0 (the first bank) to stone 3, from there to stone 6, and from there to stone 10 (the other bank). L = 3 is not sufficient (in three jumps we can only get to stone 9), so L = 4 must be optimal.

  3. 10

    1234567

    10

    Returns: 82

    Here we are allowed to make 10 jumps, which means that we can simply jump from one stone to the next. The longest jump we'll need to make is the jump from stone 9 to stone 10. The length of this jump is 82. Above we have shown that L = 82 is long enough. Now all that remains is to realize that a person with maximum jump length L = 81 cannot get to the other bank of this river at all, so L = 82 must indeed be optimal.

  4. 10

    1234567

    4

    Returns: 87

    If our maximum jump length is 87, we can get across this river in four jumps: from stone 0 to stone 6 (jump length 1+2+5+10+17+26 = 61), from stone 6 to stone 8 (jump length 37+50 = 87), from stone 8 to stone 9 (jump length 65) and from stone 9 to stone 10 (jump length 82).

  5. 123456

    1000000007

    2

    Returns: 27561114912135

    Watch out for integer overflow, in particular when computing the distances between consecutive stones.

  6. 998987

    1000000007

    989000

    Returns: 999999938

  7. 998987

    1000000007

    603212

    Returns: 1092605551

  8. 29

    128654

    6

    Returns: 1513

  9. 94494

    33064496

    85092

    Returns: 33064373

  10. 1

    24

    1

    Returns: 1

  11. 27

    3

    5

    Returns: 10

  12. 993762

    8148

    133

    Returns: 29787937

  13. 879358

    12

    42677

    Returns: 90

  14. 6

    19

    1

    Returns: 42

  15. 102

    171

    1

    Returns: 8021

  16. 946850

    16802533

    58

    Returns: 136913399360

  17. 129166

    4443723

    1

    Returns: 285056739668

  18. 963923

    2366812

    1240

    Returns: 920222209

  19. 848031

    41

    40

    Returns: 445237

  20. 5

    2478386

    4

    Returns: 17

  21. 7761

    81979605

    8

    Returns: 19484789042

  22. 7514

    9877000

    21

    Returns: 1456999625

  23. 859619

    1

    3

    Returns: 286540

  24. 959638

    8247

    4

    Returns: 979700507

  25. 4831

    234731

    15

    Returns: 36291433

  26. 112

    22696922

    2

    Returns: 231133

  27. 947103

    19211

    1

    Returns: 9026764284

  28. 896

    84877

    17

    Returns: 1956308

  29. 991625

    160

    1322

    Returns: 46928

  30. 830745

    803

    6

    Returns: 54136950

  31. 922517

    40355407

    1

    Returns: 18560485731421

  32. 1105

    784

    165

    Returns: 2517

  33. 948010

    6

    81

    Returns: 37064

  34. 871747

    29267708

    393708

    Returns: 44380511

  35. 155889

    371196

    6

    Returns: 4800891508

  36. 851997

    28

    117

    Returns: 83755

  37. 11853

    854970

    52

    Returns: 94471810

  38. 10116

    921689

    24

    Returns: 187248464

  39. 429136

    201003968

    35488

    Returns: 1268194995

  40. 982162

    2691

    17

    Returns: 75551936

  41. 311397

    3281090

    24

    Returns: 21234830368

  42. 346592

    44720

    15952

    Returns: 491933

  43. 893205

    1

    240070

    Returns: 4

  44. 919040

    593677405

    35776

    Returns: 7737980778

  45. 1512

    214566

    5

    Returns: 28298380

  46. 980608

    1501688

    284148

    Returns: 3089530

  47. 2402

    416501745

    102

    Returns: 46997500

  48. 955364

    181000599

    258713

    Returns: 391954558

  49. 917576

    1

    17808

    Returns: 52

  50. 501469

    2772

    289292

    Returns: 3056

  51. 899

    24299896

    59

    Returns: 4340125

  52. 861781

    1

    17

    Returns: 50693

  53. 845775

    63

    102315

    Returns: 231

  54. 8977

    19967

    3673

    Returns: 30660

  55. 257446

    100

    22220

    Returns: 507

  56. 876039

    92

    9933

    Returns: 3513

  57. 930913

    26925107

    53

    Returns: 235854731757

  58. 7574

    3946

    1

    Returns: 15001249

  59. 959819

    14861509

    85577

    Returns: 88250403

  60. 856391

    12

    20751

    Returns: 175

  61. 827812

    25856114

    108

    Returns: 98847634555

  62. 1

    362

    1

    Returns: 1

  63. 2552

    594751008

    776

    Returns: 9644552

  64. 873530

    10852593

    308

    Returns: 15367918885

  65. 857136

    2

    31

    Returns: 41475

  66. 832165

    3415

    20928

    Returns: 67375

  67. 901670

    1882

    23

    Returns: 36909651

  68. 56144

    180437

    6

    Returns: 843155714

  69. 846467

    225550367

    4293

    Returns: 22146883083

  70. 24890

    131557444

    1

    Returns: 1316253225127

  71. 26

    12446

    19

    Returns: 626

  72. 64364

    343494966

    1204

    Returns: 8234473016

  73. 62

    495620524

    1

    Returns: 77593

  74. 933755

    3122108

    374329

    Returns: 4940593

  75. 36

    22

    3

    Returns: 133

  76. 899771

    1

    95

    Returns: 9472

  77. 901368

    125955

    5

    Returns: 11306892985

  78. 850798

    7137318

    8

    Returns: 379076263693

  79. 3

    86327405

    2

    Returns: 5

  80. 13151

    3363

    2

    Returns: 10943111

  81. 13

    289346930

    3

    Returns: 267

  82. 850759

    3036908

    3

    Returns: 430316114173

  83. 933746

    8048921

    24

    Returns: 156396596135

  84. 111841

    7

    1424

    Returns: 237

  85. 12

    392

    11

    Returns: 122

  86. 894743

    7

    18

    Returns: 149126

  87. 1157

    1160

    5

    Returns: 125001

  88. 947026

    252231

    87889

    Returns: 1435577

  89. 706243

    2

    4772

    Returns: 222

  90. 63486

    158026653

    4

    Returns: 1148717286002

  91. 992507

    223935554

    258300

    Returns: 506823580

  92. 5852

    16062

    1843

    Returns: 30520

  93. 158

    68052327

    2

    Returns: 659001

  94. 2013

    137

    735

    Returns: 226

  95. 20

    52820

    1

    Returns: 2490

  96. 48

    2930

    3

    Returns: 12390

  97. 2

    10112

    1

    Returns: 3

  98. 21

    5817

    8

    Returns: 437

  99. 801067

    139176966

    117

    Returns: 473568728261

  100. 916184

    502397

    73309

    Returns: 3305295

  101. 950993

    928654566

    29

    Returns: 15024707626494

  102. 45878

    4078

    18195

    Returns: 6396

  103. 877372

    4023

    342883

    Returns: 6324

  104. 8

    449231193

    1

    Returns: 148

  105. 958249

    951830

    792

    Returns: 575280660

  106. 886422

    254962024

    433

    Returns: 259093373323

  107. 830847

    3445

    467695

    Returns: 4130

  108. 1000000

    1000000006

    50

    Returns: 9869997907375

  109. 1000000

    1000000000

    10000

    Returns: 49680147347

  110. 1000000

    2341

    34

    Returns: 34441507


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: