Problem Statement
This problem and the division 2 easy problem NailingABanner are two different problems. Make sure to read the statement carefully to note the differences.
You were asked to attach a very long banner to an equally long wooden fence. You did that by using some nails.
The banner is exactly 2^60 (two to the power of 60) units long.
You started by using the first nail to attach the top left corner of the banner to the fence and then the second nail to attach the top right corner. The location of the first nail is coordinate 0, the location of the second nail is coordinate 2^60.
From this point on, you were adding more nails to the banner. The nails were added in rounds. In each round, you first identified all pairs of nails that were currently adjacent (i.e., had no other nail between them), and then for each such pair you placed another nail exactly half-way between the two adjacent nails. Within each round, these new nails were placed from the left to the right, i.e., their coordinates increased.
For example:
- Round 1: Exactly one nail (nail #3) was placed into the middle of the banner.
- Round 2: We placed two nails. Nail #4 was placed between nails #1 and #3, and then nail #5 was placed between nails #3 and #2. (Nail #4 is in one fourth of the banner, nail #5 is in its three fourths.)
- Round 3: The five nails we had already placed formed four pairs of adjacent nails. Thus, in this round we added four new nails - one into the middle of each of the four segments of the banner.
The process terminated after placing the nail number 2^60 + 1. It can be shown that at this moment there were nails precisely at all integer coordinates between 0 and 2^60, inclusive.
You are given the coordinate X of a nail. Return the number of this nail.
Definition
- Class:
- BannerWithNails
- Method:
- number
- Parameters:
- long
- Returns:
- long
- Method signature:
- long number(long X)
- (be sure your method is public)
Constraints
- X will be between 0 and 2^60, inclusive.
Examples
0
Returns: 1
The nail placed at coordinate 0 was nail #1.
576460752303423488
Returns: 3
The nail placed at coordinate 2^59 is the nail #3. (The number 576460752303423488 equals 2^59.)
468374361246531584
Returns: 24
1152921504606846975
Returns: 1152921504606846977
The nail at coordinate 2^60 - 1 is the very last nail placed. Its number is 2^60 + 1.
1152921504606846976
Returns: 2
144173241876164931
Returns: 648547373241505955
593588263146915922
Returns: 436627441938440726
891339906305781388
Returns: 255532676364078547
1444627880656616
Returns: 72147883280468976
21065618412756336
Returns: 36687097594362605
727672435468264032
Returns: 29384280313673611
986207816313420096
Returns: 16711947819689588
548194760315859072
Returns: 6644985409854322
1019692271275886336
Returns: 4243386281020965
558129508687545856
Returns: 1670948255170307
815008047178185728
Returns: 960903101457537
788404390055172096
Returns: 473956517251471
451899697650978816
Returns: 195901025666240
289358758592552960
Returns: 88029801220668
225789927518978048
Returns: 42074933841732
223843773076045824
Returns: 21007770960739
523476528514662400
Returns: 12789901986097
772193525957787648
Returns: 7343731043110
1085859649850441728
Returns: 4270136174122
255553063319240704
Returns: 1343226020744
961726358779592704
Returns: 1008342677779
821540432115662848
Returns: 470748409445
802058296075747328
Returns: 233051753127
1083754126409465856
Returns: 133316256466
977052599683383296
Returns: 63478174935
518703604702380032
Returns: 24909155211
349534004178845696
Returns: 11194165863
238754232084201472
Returns: 5184396122
1119253417303212032
Returns: 4232255598
583698119552663552
Returns: 1617353061
344037028343578624
Returns: 697075639
1023532275477774336
Returns: 506745136
35026244277895168
Returns: 138295321
392674969949044736
Returns: 89965557
190868226375155712
Returns: 39109430
172026874862501888
Returns: 19280538
612777552649388032
Returns: 12847153
1098757500238299136
Returns: 8191562
1004455274141974528
Returns: 3924247
842966427957723136
Returns: 1815251
76511715642048512
Returns: 559083
611079975415578624
Returns: 401089
1087922775219240960
Returns: 254756
726319789122781184
Returns: 106824
51281222319472640
Returns: 34227
560381494258827264
Returns: 24349
487303553430323200
Returns: 11656
797840821486354432
Returns: 6932
995014042672168960
Returns: 3817
423901314926247936
Returns: 1402
406449866370187264
Returns: 694
1083115710382604288
Returns: 498
454863562364420096
Returns: 180
297237575406452736
Returns: 82
234187180623265792
Returns: 40
324259173170675712
Returns: 22
792633534417207296
Returns: 15
1008806316530991104
Returns: 9
288230376151711744
Returns: 4
144115188075855870
Returns: 324259173170675713
99999999997
Returns: 576460802303423488