Statistics

Problem Statement for "CalkinWilfReversed"

Problem Statement

The Calkin-Wilf tree is a rooted tree that contains each positive rational number exactly once. The definition of the tree is really simple:

  • The root of the tree contains the value 1/1.
  • Each node in the tree has one left child and one right child.
  • If a node contains the fraction a/b, its left child contains the fraction a/(a+b) and its right child contains the fraction (a+b)/b.

Here is an ASCII art drawing of the first few levels of the tree:

                ____________1/1____________
               /                           \
        ____1/2____                     ____2/1____
       /           \                   /           \
    1/3             3/2             2/3             3/1
   /   \           /   \           /   \           /   \
1/4     4/3     3/5     5/2     2/5     5/3     3/4     4/1

The depth of the root is 0. If a node has depth n, its children have depth n+1.

You are given the longs a and b. Compute and return the depth of the node that contains the fraction a/b.

Definition

Class:
CalkinWilfReversed
Method:
getDepth
Parameters:
long, long
Returns:
long
Method signature:
long getDepth(long a, long b)
(be sure your method is public)

Constraints

  • a will be between 1 and 10^18, inclusive.
  • b will be between 1 and 10^18, inclusive.
  • a and b will be relatively prime. (I.e., their greatest common divisor will be 1.)

Examples

  1. 1

    1

    Returns: 0

    The fraction 1/1 appears in the root of the tree, and by definition the depth of the root is 0.

  2. 4

    3

    Returns: 3

    The fraction 4/3 can be found in the bottom level of the figure shown in the problem statement. The depth of its node is 3.

  3. 1

    1234567890123

    Returns: 1234567890122

    The fraction 1/1234567890123 is in the leftmost branch of the tree, and its node is quite deep.

  4. 1234567

    7654321

    Returns: 8837

  5. 1234567890123

    1

    Returns: 1234567890122

  6. 425

    425714616047350198

    Returns: 1001681449523200

  7. 719433774099204033

    532449225634529765

    Returns: 301

  8. 938681652839233510

    270904436553010679

    Returns: 114

  9. 449144843248846699

    624766698590466374

    Returns: 105

  10. 553056322653628590

    31732635724698667

    Returns: 573

  11. 899

    270361549640470092

    Returns: 300735872792668

  12. 112925283035402051

    129

    Returns: 875389790972129

  13. 985554669660712003

    857744876225643905

    Returns: 199

  14. 745594282516936336

    327666700308704705

    Returns: 103

  15. 295698654017171035

    714110448362160427

    Returns: 99

  16. 124613693185048386

    541

    Returns: 230339543780141

  17. 351

    60162289364907391

    Returns: 171402533803192

  18. 351637995429999877

    621415064023300605

    Returns: 217

  19. 448616607852837213

    443148034045473509

    Returns: 220

  20. 76313610130859795

    909914298465648232

    Returns: 258

  21. 358032404507481657

    811994882756629310

    Returns: 103

  22. 658834888160345336

    481764886321987623

    Returns: 98

  23. 161007885428476854

    134968361615629733

    Returns: 211

  24. 610387032121568866

    769215648016174975

    Returns: 104

  25. 455

    630639562155654776

    Returns: 1386021015726729

  26. 338914788102509440

    349421733435796333

    Returns: 184

  27. 160360075626260195

    83916796503352629

    Returns: 9077

  28. 529732992768310561

    530953185541489288

    Returns: 603

  29. 495970969500886627

    361

    Returns: 1373880801941539

  30. 712

    201902233898944635

    Returns: 283570553228871

  31. 650166809806686892

    880156715225054665

    Returns: 200

  32. 415661963164951408

    403270272231987979

    Returns: 208

  33. 997445735399331185

    730880354584413331

    Returns: 208

  34. 559083626841562699

    738637614205066793

    Returns: 110

  35. 973402044540343580

    564072768509662491

    Returns: 232

  36. 894324517027910917

    365997336532448669

    Returns: 307

  37. 722312518870275373

    921731871906944696

    Returns: 103

  38. 245

    969173362437934021

    Returns: 3955809642603856

  39. 455

    752425824415489437

    Returns: 1653683130583507

  40. 181

    147431320293504472

    Returns: 814537681179594

  41. 217

    497644075228405429

    Returns: 2293290669255338

  42. 38350906486876537

    981

    Returns: 39093686531015

  43. 230685904486689697

    799982446078690966

    Returns: 105

  44. 838947514815112547

    511332845794921416

    Returns: 101

  45. 846050736993328193

    372935984945476948

    Returns: 105

  46. 389

    291849059982055533

    Returns: 750254652910183

  47. 580329488052172225

    591719867317294017

    Returns: 296

  48. 487786896893974777

    573611777387862827

    Returns: 296

  49. 946518513211440295

    513332680028123663

    Returns: 175

  50. 38255831016900615

    941

    Returns: 40654443163578

  51. 544323398746626361

    756348195613031830

    Returns: 101

  52. 565375655953876603

    779419297721358496

    Returns: 101

  53. 817491862470740273

    690504415857737733

    Returns: 165

  54. 78935955745694384

    906192017559693567

    Returns: 458

  55. 271

    161914473327440222

    Returns: 597470381282086

  56. 541

    649792948554074850

    Returns: 1201096023205343

  57. 619299488308235380

    911714735711098129

    Returns: 338

  58. 707962095758001537

    991383819553345460

    Returns: 320

  59. 415192958792629296

    125436731148287815

    Returns: 226

  60. 995882133582379213

    268865561418622488

    Returns: 102

  61. 586036654120253061

    593

    Returns: 988257426847023

  62. 666920745161036992

    475114102575554611

    Returns: 156

  63. 701707653598957063

    921

    Returns: 761897560910948

  64. 690995642197888637

    406129986792963905

    Returns: 102

  65. 360852540475742969

    26594938095846912

    Returns: 136

  66. 963348685515392407

    757

    Returns: 1272587431328143

  67. 589413648960073712

    77

    Returns: 7654722713767209

  68. 735937495250479788

    937

    Returns: 785418885005863

  69. 507487651082263372

    595968218643453603

    Returns: 106

  70. 829

    411527553924696742

    Returns: 496414419692069

  71. 310

    354714200209114447

    Returns: 1144239355513285

  72. 751827388745874118

    475495439811194271

    Returns: 114

  73. 683762750946593239

    556431807186785493

    Returns: 101

  74. 26049163320518491

    113200535694542075

    Returns: 1780

  75. 621

    723351232771187536

    Returns: 1164816799953648

  76. 692

    330102544348112637

    Returns: 477026798190930

  77. 303519947644271533

    556495650696534001

    Returns: 407

  78. 271882838110757763

    768024667583809486

    Returns: 99

  79. 47164541759745349

    402

    Returns: 117324730745655

  80. 706724396751885327

    209211228233017834

    Returns: 303

  81. 648792920646534737

    496

    Returns: 1308050243239001

  82. 105429505529393010

    590747514777978667

    Returns: 197

  83. 2

    1111111333333333

    Returns: 555555666666667

  84. 1

    1234567890122

    Returns: 1234567890121

  85. 3

    1234567890128

    Returns: 411522630044

  86. 2

    999999999999999999

    Returns: 500000000000000000

  87. 420196140727489673

    679891637638612258

    Returns: 85

  88. 1

    1000000000000

    Returns: 999999999999

  89. 1234567123123123

    2

    Returns: 617283561561562

  90. 999999999999

    999999999997

    Returns: 500000000000

  91. 420196140727489673

    5

    Returns: 84039228145497937

  92. 1

    2

    Returns: 1

  93. 2

    10000000000001

    Returns: 5000000000001

  94. 1234567890124

    1234567890123

    Returns: 1234567890123


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: