Problem Statement
You are playing a simplified version of darts. For each throw, you choose one of two distances from the target and throw the dart. If you hit the target from the short distance, you get 2 points, and if you hit it from the long distance, you get 3 points. You get no points for missing the target.
You get N throws, and you must get at least W points to win. You hit the target with probability P1 percent from the short distance, and with probability P2 percent from the long distance. Return the expected probability (as a percentage between 0 and 100) that you will win.
Definition
- Class:
- SimplifiedDarts
- Method:
- tryToWin
- Parameters:
- int, int, int, int
- Returns:
- double
- Method signature:
- double tryToWin(int W, int N, int P1, int P2)
- (be sure your method is public)
Notes
- Your return value must have an absolute or relative error less than 1e-9.
Constraints
- W will be between 1 and 1000, inclusive.
- N will be between 1 and 1000, inclusive.
- P1 will be between 0 and 100, inclusive.
- P2 will be between 0 and 100, inclusive.
Examples
5
2
50
25
Returns: 12.5
You should make one throw from the short distance and one throw from the long. Both throws must be successful to win. The expected probability of winning is 0.5*0.25 = 12.5%.
6
3
90
20
Returns: 73.30000000000001
You are a good short distance shooter. Make your first throw from the short distance. If you hit the target, make your next two throws from the short distance as well. If you miss the first throw, you need to make the next two throws from the long distance. The expected probability of winning is 0.9*(0.9*0.9) + 0.1*(0.2*0.2) = 73.3%.
30
384
3
1
Returns: 18.344490479047746
You are a bad shooter, but you have many attempts.
999
333
0
100
Returns: 100.0
You are an excellent 3-point shooter. The victory is yours.
1000
333
0
100
Returns: 0.0
You are an excellent 3-point shooter, but it is impossible to get 1000 points with just 333 throws.
532
479
72
7
Returns: 99.99999999999952
803
771
26
75
Returns: 100.0
341
332
1
62
Returns: 100.0
978
915
62
22
Returns: 99.99999447808712
694
122
23
1
Returns: 0.0
246
3
19
51
Returns: 0.0
632
567
83
17
Returns: 100.0
826
80
62
24
Returns: 0.0
505
774
21
8
Returns: 3.235029605705033E-12
307
851
42
82
Returns: 100.0
29
979
63
60
Returns: 100.0
492
731
18
45
Returns: 100.0
294
17
12
86
Returns: 0.0
530
724
84
2
Returns: 100.0
291
254
71
55
Returns: 99.9999980074316
208
456
1
60
Returns: 100.0
782
236
60
24
Returns: 0.0
818
547
6
25
Returns: 5.963226361492138E-34
738
288
63
51
Returns: 8.23578546675441E-33
197
275
77
23
Returns: 100.0
845
958
9
27
Returns: 4.9180599012723905
57
163
14
24
Returns: 99.99772493742063
410
730
26
3
Returns: 10.806578868329625
762
673
5
30
Returns: 0.0010472478621256177
485
687
30
8
Returns: 0.14399494904573085
453
584
24
29
Returns: 95.84876025867963
839
599
62
18
Returns: 0.0021433240735887612
723
394
1
61
Returns: 49.492091127129875
428
440
18
42
Returns: 99.99826765735607
659
337
60
70
Returns: 97.31657990578955
204
282
31
26
Returns: 78.40843687510235
474
377
29
48
Returns: 99.23335949272146
62
26
31
75
Returns: 33.714411458790885
838
436
93
22
Returns: 0.47220630429819865
251
512
21
3
Returns: 2.7434051431279816
228
182
65
16
Returns: 77.42320991618868
359
227
55
64
Returns: 99.97734105092933
83
797
6
2
Returns: 82.97157145157963
810
467
24
66
Returns: 99.99021295368615
454
350
47
45
Returns: 74.3116072263093
842
434
68
61
Returns: 6.092580947939844
952
888
48
27
Returns: 0.050900483523618464
835
444
54
60
Returns: 12.030085470170029
780
464
67
64
Returns: 99.9826678126776
713
796
41
14
Returns: 1.5709819597507901
830
310
96
86
Returns: 5.043058938335629
711
439
81
63
Returns: 99.99570215526509
817
363
33
70
Returns: 1.6303222276235743
473
313
51
48
Returns: 20.748064663188952
167
125
51
23
Returns: 0.026493002744617068
107
147
24
20
Returns: 11.158850702419116
408
491
36
18
Returns: 0.649990007686892
643
917
29
22
Returns: 16.384821787152344
523
691
11
28
Returns: 94.7236522554947
385
424
51
0
Returns: 98.94685274798275
604
351
16
57
Returns: 43.972050199458536
49
79
41
13
Returns: 96.74635044609903
183
144
8
45
Returns: 76.3843752871948
233
82
64
83
Returns: 0.09248042950304801
217
129
81
47
Returns: 25.79414635080777
1000
653
53
89
Returns: 100.0
191
131
35
48
Returns: 45.63977244862386
1000
407
31
99
Returns: 100.0
237
327
8
27
Returns: 88.963246472822
505
390
61
15
Returns: 6.6497277648268085
268
443
28
3
Returns: 15.841662536644327
860
522
48
54
Returns: 34.28525732079538
754
456
37
56
Returns: 64.27179603173326
501
516
34
31
Returns: 26.56090640028942
241
239
10
33
Returns: 40.84383916005378
1000
570
31
63
Returns: 98.63669433956981
296
102
10
87
Returns: 0.047709075792224345
810
510
31
52
Returns: 35.176476623747675
1000
595
69
59
Returns: 93.13187049539495
336
241
76
19
Returns: 98.96621703634301
1
1
100
100
Returns: 100.0
1000
1000
50
25
Returns: 51.47945169449058
1
5
50
50
Returns: 96.875
30
384
3
1
Returns: 18.344490479047746
1000
824
32
28
Returns: 7.985531368406269E-13
53
55
3
2
Returns: 2.73920017493805E-15
274
403
11
65
Returns: 100.0
564
876
42
91
Returns: 100.0
1000
500
40
30
Returns: 3.409646828907625E-62