Problem Statement
Andrew wants to connect the two nails using a piece of rope. His only condition is that the rope must be perfectly taut. In other words, there must be no slack anywhere on the rope. (Assume that the width of the rope is negligible and that the length of the rope is not limited.)
Of course, the rope cannot go directly from one nail to the other, because the pulleys are in the way. The rope has to go around them somehow: touching some of them, or even wrapping around them. The rope is even allowed to touch the same point on a pulley arbitrarily many times.
Given these constraints it's easy to see that there are infinitely many ways how to stretch the rope between the two nails. Note that two ways are considered different if the rope follows a different curve, even if the length of the rope is exactly the same in both ways.
Andrew ordered all possible ways according to the length of the rope used. (The order starts with the shortest ways, and ties are broken arbitrarily.) You are given the
Definition
- Class:
- PulleyTautLine
- Method:
- getLength
- Parameters:
- int, int, int, long
- Returns:
- double
- Method signature:
- double getLength(int d, int r, int n, long k)
- (be sure your method is public)
Notes
- Your return value must have an absolute or a relative error of less than 1e-9.
Constraints
- r will be between 1 and 499,999,999, inclusive.
- d will be between 2*r+1 and 1,000,000,000, inclusive.
- n will be between 1 and 50, inclusive.
- k will be between 1 and 1,000,000,000,000,000,000 (10^18), inclusive.
Examples
100
30
1
1
Returns: 209.06939952431298
Here we are looking for the shortest possible solution. There are two ways to achieve it: start at the left nail, pull the rope above or below the pulley and directly proceed to the right nail.
100
30
1
2
Returns: 209.06939952431298
As there are two ways to achieve the shortest possible solution, the answer here is the same as in previous example.
1000000000
499999999
1
1000000000000000000
Returns: 1.570796323653304E27
One of the solutions with such length is: start at the left nail, pull the rope above the pulley, wrap it 499,999,999,999,999,999 times around the pulley and finish at the right nail.
100
30
2
3
Returns: 327.67946605191
One solution with such length is shown on the picture below.
100
30
2
35
Returns: 734.7850917948947
One solution with such length starts as on the left picture and ends as on the right picture.
987564321
123
3
123456789987654321
Returns: 4.299887159886938E9
1000000000
499999999
50
1000000000000000000
Returns: 6.981316870248074E10
3
1
1
1
Returns: 6.336528068400624
min answer
1000000000
499999999
3
1000000000000000000
Returns: 8.079989149920386E10
max answer?
32057107
16028553
1
86
Returns: 4.302145085126367E9
998139269
499069634
1
999999999999790705
Returns: 1.5678734958038187E27
115195119
57597559
1
1000000000000000000
Returns: 1.809480682191047E26
5
2
2
10
Returns: 30.086770260547254
999999993
499999996
2
65761038
Returns: 4.895476104534259E10
29320203
14660101
2
1000000000000000000
Returns: 3.2776061287131424E9
999963951
499981975
3
1
Returns: 4.2554961707317533E9
588623499
294311749
3
23584184
Returns: 2.032515965906144E10
69
34
3
1000000000000000000
Returns: 5508.832608670127
999999801
499999900
5
607
Returns: 1.310962859994841E10
3314121
1657060
4
999999999992614648
Returns: 2.3513794833152306E8
5626471
2813235
5
1000000000000000000
Returns: 3.726858418932302E8
317
158
10
18
Returns: 3746.9428304719095
48545413
24272706
6
17149806
Returns: 1.3643787893032594E9
999640579
499820289
7
1000000000000000000
Returns: 6.221557706385803E10
868298885
434149442
16
1
Returns: 1.4983061292491173E10
999999833
499999916
17
4
Returns: 1.8826442763851524E10
83
41
11
1000000000000000000
Returns: 4910.99509552505
998644813
499322406
23
993
Returns: 2.5932847010040222E10
999998741
499999370
23
511612
Returns: 2.8251188290911385E10
999999993
499999996
28
1000000000000000000
Returns: 6.095476096135443E10
822671663
411335831
44
26
Returns: 3.770011846403261E10
999999957
499999978
41
999999999952732983
Returns: 6.538396225127026E10
45
22
48
1000000000000000000
Returns: 3067.280596032556
147328447
73664223
50
2
Returns: 7.551415252540619E9
998976349
499488174
50
585407
Returns: 5.405424183066228E10
5225
2612
50
1000000000000000000
Returns: 364738.96055232536
132
28
1
521
Returns: 46011.5510074728
1000000000
499500044
1
8768
Returns: 1.3758087338127293E13
989615955
492839938
1
1000000000000000000
Returns: 1.5483023286164491E27
999989262
279024477
2
99
Returns: 8.905030357746284E9
999999839
499999595
2
3466754
Returns: 4.124234405911841E10
999999502
499998187
2
1000000000000000000
Returns: 1.1178624280265579E11
999830414
499909508
3
189
Returns: 1.2536630968371727E10
999999994
494075633
3
999999999999890597
Returns: 8.001166502631393E10
999999994
499997197
3
1000000000000000000
Returns: 8.079951015806079E10
999998198
496251075
5
6
Returns: 6.811362248571842E9
999996929
499941344
4
152569
Returns: 2.2961690326197372E10
999903486
463857326
4
1000000000000000000
Returns: 6.735003110917125E10
999999992
497969154
7
666
Returns: 1.3076341093908882E10
999999756
494257058
7
9
Returns: 8.803587520128536E9
999999995
499999798
6
1000000000000000000
Returns: 6.3808724358713394E10
999999999
499605229
12
7
Returns: 1.382481338243871E10
999965657
483155134
13
263149536
Returns: 3.0487438332894836E10
999992813
499475563
16
1000000000000000000
Returns: 5.9207325014128265E10
999999457
499823319
22
309
Returns: 2.439594837234355E10
999999886
416763272
27
103269352
Returns: 3.3410760932986084E10
999876383
137023964
28
1000000000000000000
Returns: 3.917925790655844E10
999486910
494755350
46
45
Returns: 4.77819813842719E10
1000000000
499999998
41
4
Returns: 4.282644590158484E10
999999906
499999051
41
1000000000000000000
Returns: 6.5383892876708664E10
999999999
499999996
50
885
Returns: 5.239724215870473E10
999999996
498864785
50
125516649
Returns: 5.5225785763158035E10
3498
341
50
1000000000000000000
Returns: 184450.5313466753
993547240
5321838
1
898
Returns: 1.6967389245267511E10
15006
37
1
3
Returns: 30244.56908658646
999758203
22259800
1
1000000000000000000
Returns: 6.993122415037808E25
999995640
16519142
2
636
Returns: 4.764737924575586E9
10144
141
2
999999999999999923
Returns: 200907.6620065879
999237503
8896002
2
1000000000000000000
Returns: 1.7518789111213776E10
40105953
104504
3
1
Returns: 1.6042408430601388E8
794002869
12335791
3
59
Returns: 3.3316025863486214E9
64
3
3
1000000000000000000
Returns: 1662.7673998813893
999999653
83331394
4
11
Returns: 5.034787746982868E9
999999948
5798809
4
999999999999999999
Returns: 1.4712612057412968E10
34481
245
5
1000000000000000000
Returns: 524630.0197567008
48884618
1638906
6
501
Returns: 3.629523104921808E8
998343518
33475885
9
999999999999999004
Returns: 2.2300395368223328E10
999999987
14505394
6
1000000000000000000
Returns: 1.7923037005063553E10
362676
8111
20
1000
Returns: 7617465.967791431
13014
145
20
8680
Returns: 273308.54066226433
999999988
61954966
19
1000000000000000000
Returns: 2.907227458904786E10
13211
810
28
163
Returns: 383367.581430317
3348
68
23
999999999999998923
Returns: 88071.62624787266
26766
1849
26
1000000000000000000
Returns: 888270.854114241
978860697
3423170
43
1000
Returns: 4.306993052398099E10
999330197
18544886
42
999999999999999999
Returns: 4.35644728851539E10
985039461
3731183
41
1000000000000000000
Returns: 4.148939886262584E10
999999999
1863536
50
995
Returns: 5.100001731284921E10
2024184
8567
50
999999999999986370
Returns: 1.0334296179874367E8
209486516
9610174
50
1000000000000000000
Returns: 1.0827959412614033E10
999999995
23984
1
999
Returns: 2.0751972528625224E9
701115699
64816
1
999999999999999992
Returns: 2.0362546943507744E23
160746749
1674
1
1000000000000000000
Returns: 5.259026102109635E21
998286153
7957
2
4
Returns: 2.9948584591902676E9
48017875
38911
2
2949163
Returns: 2.506023886664884E8
949244168
96272
2
1000000000000000000
Returns: 7.549631869394976E9
999849822
53189
3
31
Returns: 3.999733498490777E9
999707122
43692
3
41368
Returns: 4.007064241701889E9
999999937
1196
3
1000000000000000000
Returns: 6.033733063741631E9
1302170
6
4
957
Returns: 6511000.796585603
961867522
14194
5
196
Returns: 5.771383499273956E9
999996567
142484
5
1000000000000000000
Returns: 8.457447152894798E9
999999972
1320
7
1
Returns: 7.999999776001741E9
830497
1
9
133
Returns: 8304970.0000084285
999967564
210464
10
1000000000000000000
Returns: 1.1182132637792446E10
21455230
1463
19
979
Returns: 4.2910460069831836E8
999996308
63293
12
999999999999999877
Returns: 1.303097120857066E10
999999158
48185
18
1000000000000000000
Returns: 1.90087639493509E10
999603038
247681
32
5
Returns: 3.2986900438110718E10
999999835
227441
26
999999999680016265
Returns: 2.7020003593527756E10
6390
1
28
1000000000000000000
Returns: 185385.40276202734
999999999
100806
49
54
Returns: 4.999999998048555E10
999999914
44929
43
2
Returns: 4.3999996218018616E10
999999987
3391
45
1000000000000000000
Returns: 4.600008462752797E10
22186
2
50
78
Returns: 1131486.0005408814
96457381
2682
50
22011953
Returns: 4.91932643196945E9
999730450
66683
50
1000000000000000000
Returns: 5.0987091149026215E10
999999995
13
1
999
Returns: 2.0000407490230877E9
984095951
9
1
999999999999999999
Returns: 2.827433388427633E19
999999999
207
1
1000000000000000000
Returns: 6.503096792950871E20
999999910
4
2
978
Returns: 3.000000257787566E9
921114799
26
2
1
Returns: 2.763344397000001E9
999999968
227
2
1000000000000000000
Returns: 5.049912615850475E9
999996382
865
3
951
Returns: 4.000023572690776E9
999999979
2
3
3375
Returns: 4.0000000667964478E9
185903186
2
3
1000000000000000000
Returns: 7.550300331190116E8
999998033
15
4
182
Returns: 4.99999035349556E9
999996208
29
5
999999999999941423
Returns: 6.000921654733971E9
999999996
615
5
1000000000000000000
Returns: 6.020027911911865E9
999999994
1
8
1
Returns: 8.999999946E9
999992018
5
9
49461193
Returns: 9.999920525575191E9
999999998
367
6
1000000000000000000
Returns: 7.003445043938229E9
999999971
93
20
975
Returns: 2.099999939100006E10
321039074
318
17
451
Returns: 5.778703332002205E9
82850716
2
15
1000000000000000000
Returns: 1.3256120089203076E9
999999999
505
22
2
Returns: 2.299999997700025E10
999999999
4
33
999999999999999999
Returns: 3.400000019219467E10
204541037
17
24
1000000000000000000
Returns: 5.113527634026447E9
999970138
386
41
2
Returns: 4.199874579600015E10
33837252
1
42
1000000000000000000
Returns: 1.4550018674159274E9
999999999
846
45
1000000000000000000
Returns: 4.600002121632413E10
999999993
391
50
999
Returns: 5.099999964300076E10
923359356
1
50
11
Returns: 4.7091327156E10
101046099
29
50
1000000000000000000
Returns: 5.15335141342519E9