Problem Statement
Monicka has an array of N cells, numbered 0 through N-1. She chooses one cell of the array uniformly at random and places a token into that cell.
Misko will then play a game with Monicka, trying to guess the location of the token. The gameplay is similar to binary searching for the token – in each turn, Misko picks a cell of the array and receives one of three possible answers: "left" if the token is in a cell with a smaller number, "right" if it is in a cell with a larger number, or "correct" if the chosen cell contains the token. The game ends when Misko gets the answer "correct".
Misko is not allowed to ask useless questions. For example, if he already chose the cell 47 and received the answer "right", he is not allowed to choose any of the cells 3, 12, and 47: it is already known that these cells do not contain the token.
Monicka does not like games that are too short or too long. She is happy with a game that takes at least A, but at most B turns. Misko wants to make Monicka happy, therefore he aims to finish the game in such a number of turns.
You are given the
Definition
- Class:
- WellTimedSearch
- Method:
- getProbability
- Parameters:
- int, int, int
- Returns:
- double
- Method signature:
- double getProbability(int N, int A, int B)
- (be sure your method is public)
Notes
- Return values with absolute or relative error at most 1e-9 will be accepted as correct.
Constraints
- N will be between 1 and 1,000,000, inclusive.
- A will be between 1 and N, inclusive.
- B will be between A and N, inclusive.
Examples
3
2
2
Returns: 0.6666666666666666
Monicka will be happy if Misko's second guess is correct. The best strategy for Misko is to choose the index 1 first. If he gets the answer "correct", he won the game too early. But if he gets the answer "left" or "right", he will win the game in the second turn. Thus the probability that Monicka will be happy when Misko uses this strategy is 2/3.
3
3
3
Returns: 0.3333333333333333
This time Misko wants to postpone his correct guess until the third turn.
123456
1
20
Returns: 1.0
Misko can use binary search to guarantee that he will be able to guess Monicka's number in well under 20 guesses.
5
3
4
Returns: 0.6
1
1
1
Returns: 1.0
1000000
3
5
Returns: 2.8E-5
1000000
100000
900000
Returns: 0.900001
1000000
100000
100017
Returns: 0.9
1000000
100000
100016
Returns: 0.899997
1000000
100000
100015
Returns: 0.899991
1000000
7
999947
Returns: 0.999994
622760
234
628
Returns: 0.999625859078939
212578
50
15775
Returns: 0.999769496373096
40
2
11
Returns: 0.975
104
31
44
Returns: 0.7115384615384616
446
14
37
Returns: 0.9708520179372198
46
4
14
Returns: 0.9347826086956522
286135
51
51
Returns: 0.4999318503503591
45
31
35
Returns: 0.3333333333333333
988984
6355
6357
Returns: 0.8693902024704141
25
2
14
Returns: 0.96
77590
931
933
Returns: 0.8646217296043305
221
3
15
Returns: 0.9909502262443439
5606
2281
2296
Returns: 0.5932929004637888
101
21
23
Returns: 0.7227722772277227
146807
163
165
Returns: 0.8740931971908696
4591
29
34
Returns: 0.9793073404487039
48
2
13
Returns: 0.9791666666666666
10977
8746
8772
Returns: 0.20333424432905164
11
1
3
Returns: 0.6363636363636364
2402
7
10
Returns: 0.3996669442131557
903
42
51
Returns: 0.9545957918050941
2740
7
374
Returns: 0.9978102189781022
20828
440
481
Returns: 0.9789226041866718
477
27
29
Returns: 0.8343815513626834
139
1
4
Returns: 0.1079136690647482
50425
2257
2262
Returns: 0.9404462072384729
362802
1
16
Returns: 0.18063571865645725
16669
15
25
Returns: 0.9987401763753074
3910
12
19
Returns: 0.9943734015345268
365461
655
658
Returns: 0.9358372028752726
450897
2752
19730
Returns: 0.9938988283355179
100283
1
5
Returns: 3.091251757526201E-4
161
7
11
Returns: 0.9440993788819876
491266
54
65
Returns: 0.9996620975194701
115
9
10
Returns: 0.7304347826086957
144
58
81
Returns: 0.6041666666666666
24
5
18
Returns: 0.8333333333333334
111736
139
105312
Returns: 0.9987649459440109
670152
1909
1922
Returns: 0.9970961811648701
3671
18
18
Returns: 0.49850177063470447
767724
579061
579061
Returns: 0.12287749243217615
527123
10
13
Returns: 0.014569654520861355
12
4
10
Returns: 0.75
50
10
24
Returns: 0.82
1860
291
300
Returns: 0.8440860215053764
12
7
8
Returns: 0.5
525863
37
183
Returns: 0.9999315411048124
7664
385
388
Returns: 0.8913100208768268
23341
1
3
Returns: 2.999014609485455E-4
22453
12636
12642
Returns: 0.4340177259163586
231
11
14
Returns: 0.9090909090909091
203
1
5
Returns: 0.15270935960591134
39
1
6
Returns: 1.0
176697
40515
40527
Returns: 0.7706299484428145
444950
1
55
Returns: 1.0
18597
125
128
Returns: 0.9314405549282142
130892
5
10
Returns: 0.0077010054090395135
942
18
27
Returns: 0.9819532908704883
58
12
24
Returns: 0.8103448275862069
301602
20407
20493
Returns: 0.9323412974715022
96381
17859
17859
Returns: 0.4073935734221475
573
3
109
Returns: 0.9965095986038395
90
35
45
Returns: 0.6222222222222222
843
384
388
Returns: 0.5326215895610913
226
5
6
Returns: 0.21238938053097345
41972
41
46
Returns: 0.9835842942914323
92583
470
536
Returns: 0.9949342751909098
45
12
19
Returns: 0.7555555555555555
56
9
10
Returns: 0.6785714285714286
35085
454
468
Returns: 0.9870884993587002
304
1
15
Returns: 1.0
228901
4
15
Returns: 0.1431186408097824
1661
154
164
Returns: 0.9078868151715834
12054
2
2
Returns: 1.6592002654720425E-4
3730
2391
2406
Returns: 0.35924932975871315
322999
6
496
Returns: 0.9999845200759135
161738
3
13
Returns: 0.050625085014035044
471754
1
10
Returns: 0.0021685030757555845
12705
1102
1104
Returns: 0.799685163321527
96
56
57
Returns: 0.34375
108411
5049
47832
Returns: 0.9534364593998764
464
10
21
Returns: 0.9806034482758621
122
62
74
Returns: 0.5
1005
128
134
Returns: 0.8696517412935323
240881
43
58
Returns: 0.9998214886188616
313456
16
31
Returns: 0.9999425756725027
160109
628
1135
Returns: 0.9960839178309776
59596
20968
20984
Returns: 0.6481810859789248
14002
12
29
Returns: 0.999214397943151
127
7
8
Returns: 0.7480314960629921
241
128
133
Returns: 0.4730290456431535
13
7
7
Returns: 0.3076923076923077
3908
31
33
Returns: 0.8697543500511771
24
6
20
Returns: 0.7916666666666666
260076
777
799
Returns: 0.9970162567864778
642
2
40
Returns: 0.9984423676012462
827864
15
20
Returns: 0.9843669974778466
97
50
92
Returns: 0.4948453608247423
3906
1013
1029
Returns: 0.7409114183307731
29379
26643
26651
Returns: 0.09305966847067633
11
4
4
Returns: 0.45454545454545453
1000000
100000
100010
Returns: 0.899569
651345
41234
431234
Returns: 0.9366956067828878
945817
16352
84572
Returns: 0.9827123005824594
1000000
15
25
Returns: 0.999503
888888
393939
737373
Returns: 0.5568193068193068
1000000
15
20
Returns: 0.984368
1000000
16
18
Returns: 0.229376
100
8
10
Returns: 0.84
10000
37
43
Returns: 0.9891
123456
1000
10000
Returns: 0.9919080482115086
745234
30
40
Returns: 0.9994820418821471
1000000
37
43
Returns: 0.992158
50
18
20
Returns: 0.6
1000000
1000000
1000000
Returns: 1.0E-6
50
4
6
Returns: 0.86
1000000
500000
500000
Returns: 0.250005
100
5
7
Returns: 0.86
100000
12
17
Returns: 0.98432
1000000
100
100
Returns: 0.499956
16
5
5
Returns: 0.5
1000000
310879
398810
Returns: 0.689122
1000000
1
1000000
Returns: 1.0
1000000
1000
1010
Returns: 0.998521
1000000
456123
765948
Returns: 0.543878
1000000
500000
500003
Returns: 0.468758