Problem Statement
Elly plays a platform game on her computer. There are N platforms with distinct heights, placed on top of each other. You can generate the heights of the platforms as follows:
- Platform number 1 is has height[1] = H1.
- For each i between 2 and N, inclusive, height[i] = (height[i-1] * A + B) % M.
The girl has a "hero" whom she can move from platform to platform. At the beginning of the game there is a coin on each platform. The hero collects the coin the first time she visits the platform. (Once a coin is collected, it is gone. Visiting the same platform again does not give Elly a second coin.)
In the beginning of the game Elly can place her hero on any of the N platforms. Once she does that, the hero will pick up the coin from that platform and then the game starts.
During the game Elly only has one way to move her hero: by using a device that teleports the hero vertically. The device is deterministic and it behaves as follows: if the hero's current platform is at height X, the hero gets teleported to the height Y = (X * P + Q) % M. After the teleport, one of three different things will happen:
- If there is a platform at height Y, the hero will stand on that platform and collect the coin from that platform (if it is still there).
- If there is no platform at height Y but there are some lower platforms, the hero falls down until she hits the closest platform. She will always lands safely, regardless of the height of the fall. Then, she will collect the coin from that platform (if it is still there).
- If there are no platforms at or below the hero's new height, the game ends.
Elly can use the device arbitrarily many times, one after another. Note that the device can only be used while the hero is standing on a platform (i.e., not while she is falling).
You are given all the
Definition
- Class:
- EllysTeleport
- Method:
- getMax
- Parameters:
- int, int, int, int, int, int, int
- Returns:
- int
- Method signature:
- int getMax(int N, int H1, int A, int B, int P, int Q, int M)
- (be sure your method is public)
Constraints
- N will be between 1 and 10,000, inclusive.
- M will be between 1 and 1,000,000,007, inclusive.
- H1, A, B, P and Q will be between 0 and M-1, inclusive.
- All platform heights will be distinct.
Examples
11
9
17
9
7
13
23
Returns: 6
The generated heights are {9, 1, 3, 14, 17, 22, 15, 11, 12, 6, 19}. The optimal choice would be to start from the platform with height 15. The heights Elly's hero will visit are: 15 -> 3 -> 11 -> [21] -> 19 -> [8] -> 6 -> 9 -> [7] -> 6... With [Y] we have denoted a height where the hero is teleported to, but there is no platform so she starts falling down.
8
17
23
19
15
28
43
Returns: 4
The generated numbers are {17, 23, 32, 24, 12, 37, 10, 34}. There are two possible choices for a starting platform, which yields the maximal number of coins: 32 or 12 (the paths being, respectively, 32 -> [35] -> 34 -> [22] -> 17 -> [25] -> 24 -> [1] -> game ends, and 12 -> [36] -> 34 -> [22] -> 17 -> [25] -> 24 -> [1] -> game ends).
15
42
114
52
76
1
131
Returns: 5
The generated numbers are {42, 124, 40, 27, 117, 28, 100, 55, 34, 129, 86, 31, 49, 5, 98}.
10
71
54
85
96
52
100
Returns: 10
Here the heights are {71, 19, 11, 79, 51, 39, 91, 99, 31, 59}. Starting from the platform with height 99 Elly's hero can collect all of the coins: 99 -> [56] -> 51 -> [49] -> 39 -> [96] -> 91 -> [88] -> 79 -> [36] -> 31 -> [28] -> 19 -> [76] -> 71 -> [68] -> 59 -> [16] -> 11 -> [8] -> game ends
1000
1337
706135
1085680
1153206
345473
1234567
Returns: 42
Watch out for overflows when generating the heights!
10
581869302
404620562
708632711
545404204
133711905
795755685
Returns: 5
20
205238249
90813526
207423799
46884967
84433391
372047868
Returns: 7
50
24531490
180862451
30574576
182506777
15198492
196140734
Returns: 14
100
138749190
73732609
121894487
126303053
81133257
150802600
Returns: 18
200
103919524
61040475
131262539
121922632
171932465
183966551
Returns: 26
500
20544909
592656503
483031418
361913170
328410175
609397213
Returns: 46
1000
522136403
173978709
346827080
586119708
867889990
892462744
Returns: 78
2000
17940960
34462715
143305596
130116355
141506394
153380496
Returns: 42
5000
193280970
280854272
354267637
343593670
139963091
379821983
Returns: 92
10000
443648354
110506748
62143011
50639018
612104790
684602216
Returns: 232
1
42
42
42
42
42
1337
Returns: 1
10000
262522450
1
1
17
42
1000000007
Returns: 2
10000
870676135
1
1
1337
666
1000000007
Returns: 1
10000
136721026
1
1
1234567
7654321
1000000007
Returns: 2
10000
359573801
1
1
421337
123456789
1000000007
Returns: 2
10000
2
1
2
1
1
10001
Returns: 10000
10000
100001
1
179351
1
133742
999981825
Returns: 8850
10000
169163754
1
1
1
189375145
189375146
Returns: 10000
10000
45525813
1
2
1
198304612
198304613
Returns: 10000
10000
25475623
1
5
1
417177801
417177802
Returns: 10000
10000
6676782
1
13
1
758242871
758242872
Returns: 10000
10000
226954798
1
17
1
310701080
310701081
Returns: 10000
10000
290822200
1
42
1
361931884
361931885
Returns: 10000
10000
182911688
1
213794687
1
1
213794688
Returns: 10000
10000
113300187
1
147944787
1
2
147944789
Returns: 10000
10000
540721923
1
884392667
1
5
884392672
Returns: 10000
10000
264060007
1
638781080
1
13
638781093
Returns: 10000
10000
7041753
1
7097687
1
17
7097704
Returns: 10000
10000
156513983
1
879609673
1
42
879609715
Returns: 10000
10000
49993
1
5
1
6
49994
Returns: 10000
10000
49993
1
5
1
100
49994
Returns: 10000
10000
999899998
1
100000
1
42424242
999899999
Returns: 10000
10000
0
1
100000
1
100000
999899999
Returns: 10000
10000
2
1
100000
1
100003
999900001
Returns: 10000
10000
999800000
1
999799999
1
100000
999899999
Returns: 10000
10000
999800000
1
999799999
1
999899990
999899999
Returns: 10000
10000
0
1
100000
1
999799999
999899999
Returns: 10000
10000
150263527
498298617
361825002
308554712
488869153
802611721
Returns: 127
10000
365796717
210120468
339840185
15464610
307573909
519074043
Returns: 257
10000
150577917
77035793
12105075
182712069
129405346
185518675
Returns: 85
10000
132351904
511091141
561821923
45221080
272606466
698412072
Returns: 198
10000
101725768
78758356
10627672
52277464
132286397
172898400
Returns: 149
10000
76338300
107034863
767742621
134360178
769482973
961264979
Returns: 160
10000
13583447
73472441
65812705
102871994
110903855
121898320
Returns: 252
10000
51357206
21432300
154326108
70861390
149146565
174842016
Returns: 163
10000
347299895
464776633
175971016
42901327
49299350
641212875
Returns: 141
3643
369786099
4203888
190222791
58150106
303810411
427854494
Returns: 75
9416
397865508
341201326
147657059
72059300
193198051
503168819
Returns: 231
5580
133505891
53860493
560738997
290319951
726585505
949627106
Returns: 173
1639
179039632
371368069
366343581
355650503
527687642
781277156
Returns: 61
9234
292490028
184349496
307398456
7278837
133348379
375163522
Returns: 134
5501
317540307
325791709
23678544
231714000
658911095
856191911
Returns: 92
9164
199661400
32874570
283421978
150468287
557942923
748808120
Returns: 208
7953
844229571
312240886
811534968
51119001
320832263
851888278
Returns: 177
742
27673078
311415874
214646562
535321460
667763804
668894609
Returns: 44
2697
9782578
13282903
14756233
13911552
7324278
24934750
Returns: 54
3059
102021547
174383384
412348616
296873218
159215731
461232475
Returns: 64
109
265410014
360010077
375476813
48387162
253744890
935061429
Returns: 22
3199
654458600
339340132
748110398
191869111
188361674
897221240
Returns: 109
2697
13214088
3735899
10881188
8743688
6483088
16601272
Returns: 62
174
22415547
86218872
19903848
6160775
119366415
141230963
Returns: 16
5511
775197290
563951939
591775754
362649214
702384419
784676681
Returns: 100
3388
497123320
193529268
239687779
221752503
614885118
619011577
Returns: 143
5344
187447947
13495705
75760802
365493589
25781698
383765657
Returns: 122
5181
156264919
207707169
133240619
119032371
92574779
214072533
Returns: 137
2084
34763086
622576118
439820930
631226552
135070672
933335694
Returns: 60
852
623269329
857065948
322457913
592711182
841692685
884059690
Returns: 54
7461
41569465
51608694
173822100
203376339
107614289
326274449
Returns: 87
7946
14649293
126944086
14255240
78498592
86271762
126992369
Returns: 244
3222
42845045
237137193
205919137
19089613
228835099
417831124
Returns: 89
4081
492229683
482212588
429265451
518515435
627638726
958264126
Returns: 111
7413
221918251
466198014
49105784
241078998
425627456
477612193
Returns: 110
6165
20135780
57437844
71338323
102254516
52424886
171465366
Returns: 107
8893
193516735
188400725
1438085
82229262
45950561
248890848
Returns: 163
1189
139006054
181628626
647438557
228288551
767965749
869365981
Returns: 69
3099
19292622
52471256
62488250
64233881
25861987
66157276
Returns: 112
7550
58519297
10824443
91078022
40746576
77371322
109372427
Returns: 246
6964
263580362
327106182
290530998
150954055
671749885
945581997
Returns: 158
5597
537294152
534216451
788174404
40623875
774634184
807995101
Returns: 107
7036
183309798
335959927
204497431
348432514
413679405
607956045
Returns: 136
4946
963306712
334782891
152295753
384120460
902769184
969229186
Returns: 217
7105
129952823
396839388
464122393
273096066
771533956
918921586
Returns: 134
150
225739419
434797764
323648778
355975842
412554788
557543599
Returns: 17
446
601913200
183140819
232332505
128154015
219365083
626114935
Returns: 35
1432
7121757
11660224
23092501
28477947
20579804
42477422
Returns: 67
9456
324769810
530715196
182773470
270789204
110720767
621457153
Returns: 104
3164
41456102
32481049
38989886
42893612
34722500
79245770
Returns: 102
4352
222027819
133820414
634920763
733185481
192148983
836506265
Returns: 125
702
150345485
310618090
475691096
385787519
329081921
860483062
Returns: 29
3835
728252905
793780050
89611408
408533132
326283840
847726654
Returns: 111
50
315454648
326324656
477253416
210726130
62061090
523159177
Returns: 9
5678
574393398
229031367
419951882
370752507
180621093
755438373
Returns: 110
5960
370004247
719646637
82080409
724929116
504304985
870314537
Returns: 111
7759
107348298
272289742
159423158
176503402
114053590
274213157
Returns: 190
2573
367287522
27768175
155036157
293691766
368477458
486158241
Returns: 59
1813
12619163
54568829
44564660
28718752
56168076
125500150
Returns: 92
10000
32423
56547
56234
23434
34634
12345678
Returns: 237
8192
2
5
5
9
9
8192
Returns: 8192
10000
942855823
234992523
123942342
312499249
194995959
1000000007
Returns: 232
10000
948732532
3125322
34563
23435
3232654
948732535
Returns: 151
10000
1
1
1
1
1
1000000000
Returns: 10000
10000
0
1
1
1
1
10000
Returns: 10000
10000
1000000005
1000000002
1000000003
1000000004
1000000001
1000000006
Returns: 235
2
1
1
2
1
2
1000
Returns: 2
10000
1
1
1
1
1
100000
Returns: 10000
10000
0
1
1
1
17
10000
Returns: 10000
2
91033541
112603822
118786616
131388137
52066666
162379610
Returns: 2
10000
1
1
1
1
1
1000000
Returns: 10000
10000
1000000000
998244353
504050000
325346442
345312262
1000000007
Returns: 112
10000
1337
706135
1085680
706135
1085680
12345679
Returns: 10000
3
1000000006
1000005
1000000004
1000000003
1000000002
1000000007
Returns: 1
10000
1
1
1
1
1
1000000007
Returns: 10000
10000
1
1
1
371
17
10001
Returns: 1224
10000
1000000002
1000000003
1000000004
1000000005
1000000006
1000000007
Returns: 233
10000
1
1
1
1
1
10000000
Returns: 10000
9999
1
1
1
1
1
1000000
Returns: 9999
5
1
1
1
1
10
11
Returns: 5
10000
2
1
1
1
1000000006
1000000007
Returns: 10000
10000
1
1
1
1
1000000006
1000000007
Returns: 10000
10000
1337
706135
1085680
1153206
345473
1234567
Returns: 162
10000
1000000006
1000000000
1000000004
1000000003
1000000002
1000000007
Returns: 170
10000
1
1
1
1
1
999999999
Returns: 10000