fork download
  1. #include <iostream>
  2. #include <cassert>
  3.  
  4. int64_t euclid(int64_t a, int64_t b) {
  5. if (b == 0) return a;
  6. return euclid(b, a % b);
  7. }
  8.  
  9. class Ratio {
  10. int64_t a;
  11. int64_t b;
  12. public:
  13. Ratio() : a(0), b(0) {}
  14. Ratio(int64_t x, int64_t y) : a(x), b(y) {}
  15. friend std::ostream &operator<<(std::ostream &s, Ratio r) {
  16. if (r.b == 1)
  17. s << r.a;
  18. else
  19. s << "(" << r.a << "/" << r.b << ")";
  20. return s;
  21. }
  22. friend Ratio operator+(Ratio const &x, Ratio const &y) {
  23. Ratio r;
  24. int64_t c = euclid(x.b, y.b);
  25. int64_t zy = x.b / c;
  26. int64_t zx = y.b / c;
  27. r.b = y.b * zy;
  28. assert(r.b == x.b * zx);
  29. r.a = x.a * zx + y.a * zy;
  30. c = euclid(r.a, r.b);
  31. r.a = r.a / c;
  32. r.b = r.b / c;
  33. return r;
  34. }
  35. friend Ratio operator-(Ratio const &x, Ratio const &y) {
  36. Ratio r;
  37. int64_t c = euclid(x.b, y.b);
  38. int64_t zy = x.b / c;
  39. int64_t zx = y.b / c;
  40. r.b = y.b * zy;
  41. assert(r.b == x.b * zx);
  42. r.a = x.a * zx - y.a * zy;
  43. c = euclid(r.a, r.b);
  44. r.a = r.a / c;
  45. r.b = r.b / c;
  46. return r;
  47. }
  48. friend Ratio operator*(Ratio const &x, Ratio const &y) {
  49. Ratio r;
  50. int64_t xa, xb, ya, yb;
  51. int64_t c1 = euclid(x.a, y.b);
  52. int64_t c2 = euclid(x.b, y.a);
  53. xa = x.a / c1; yb = y.b / c1;
  54. xb = x.b / c2; ya = y.a / c2;
  55. r.a = xa * ya;
  56. r.b = xb * yb;
  57. return r;
  58. }
  59. };
  60.  
  61. class Field5 {
  62. Ratio a;
  63. Ratio b;
  64. public:
  65. Field5() : a(Ratio()), b(Ratio()) {}
  66. Field5(Ratio x, Ratio y) : a(x), b(y) {}
  67. friend std::ostream &operator<<(std::ostream &s, Field5 r) {
  68. // s << "(" << r.a << "," << r.b << ")";
  69. s << r.b;
  70. return s;
  71. }
  72. friend Field5 operator-(Field5 const &x, Field5 const &y) {
  73. Field5 r;
  74. r.a = x.a - y.a; r.b = x.b - y.b;
  75. return r;
  76. }
  77. friend Field5 operator*(Field5 const &x, Field5 const &y) {
  78. Field5 r;
  79. r.a = x.a * y.a + Ratio(5, 1) * x.b * y.b;
  80. r.b = x.a * y.b + x.b * y.a;
  81. return r;
  82. }
  83. };
  84.  
  85. int64_t fib(int n) {
  86. static int64_t t1 = 1, t2 = 1;
  87. if (n == 1 || n == 2)
  88. return 1;
  89. int64_t s = t1 + t2;
  90. t1 = t2; t2 = s;
  91. return s;
  92. }
  93.  
  94. Field5 Field5Power(Field5 a, int n) { /* =a^n */
  95. unsigned int mask = 1 << (sizeof(int) * 8 - 1);
  96. Field5 r(Ratio(1, 1), Ratio(0, 1));
  97. for (;;) {
  98. if ((n & mask) > 0)
  99. r = r * a;
  100. mask = (mask >> 1);
  101. /* out of loop */
  102. if (mask == 0)
  103. break;
  104. r = r * r;
  105. }
  106. return r;
  107. }
  108.  
  109. int const N = 93;
  110. int main() {
  111. Field5 phi1(Ratio(1, 2), Ratio(1, 2));
  112. Field5 phi2(Ratio(1, 2), Ratio(-1, 2));
  113.  
  114. for (int n = 1; n <= N; n++) {
  115. int64_t s = fib(n);
  116.  
  117. Field5 r1, r2, r3;
  118. r1 = Field5Power(phi1, n);
  119. r2 = Field5Power(phi2, n);
  120. r3 = r1 - r2;
  121.  
  122. std::cout << n << ":: " << s << " : " << r3 << std::endl;
  123. }
  124. return 0;
  125. }
  126. /* end */
  127. //7540113804746346429
  128. //9223372036854775807
  129.  
  130.  
Success #stdin #stdout 0.01s 4368KB
stdin
Standard input is empty
stdout
1:: 1 : 1
2:: 1 : 1
3:: 2 : 2
4:: 3 : 3
5:: 5 : 5
6:: 8 : 8
7:: 13 : 13
8:: 21 : 21
9:: 34 : 34
10:: 55 : 55
11:: 89 : 89
12:: 144 : 144
13:: 233 : 233
14:: 377 : 377
15:: 610 : 610
16:: 987 : 987
17:: 1597 : 1597
18:: 2584 : 2584
19:: 4181 : 4181
20:: 6765 : 6765
21:: 10946 : 10946
22:: 17711 : 17711
23:: 28657 : 28657
24:: 46368 : 46368
25:: 75025 : 75025
26:: 121393 : 121393
27:: 196418 : 196418
28:: 317811 : 317811
29:: 514229 : 514229
30:: 832040 : 832040
31:: 1346269 : 1346269
32:: 2178309 : 2178309
33:: 3524578 : 3524578
34:: 5702887 : 5702887
35:: 9227465 : 9227465
36:: 14930352 : 14930352
37:: 24157817 : 24157817
38:: 39088169 : 39088169
39:: 63245986 : 63245986
40:: 102334155 : 102334155
41:: 165580141 : 165580141
42:: 267914296 : 267914296
43:: 433494437 : 433494437
44:: 701408733 : 701408733
45:: 1134903170 : 1134903170
46:: 1836311903 : 1836311903
47:: 2971215073 : 2971215073
48:: 4807526976 : 4807526976
49:: 7778742049 : 7778742049
50:: 12586269025 : 12586269025
51:: 20365011074 : 20365011074
52:: 32951280099 : 32951280099
53:: 53316291173 : 53316291173
54:: 86267571272 : 86267571272
55:: 139583862445 : 139583862445
56:: 225851433717 : 225851433717
57:: 365435296162 : 365435296162
58:: 591286729879 : 591286729879
59:: 956722026041 : 956722026041
60:: 1548008755920 : 1548008755920
61:: 2504730781961 : 2504730781961
62:: 4052739537881 : 4052739537881
63:: 6557470319842 : 6557470319842
64:: 10610209857723 : 10610209857723
65:: 17167680177565 : 17167680177565
66:: 27777890035288 : 27777890035288
67:: 44945570212853 : 44945570212853
68:: 72723460248141 : 72723460248141
69:: 117669030460994 : 117669030460994
70:: 190392490709135 : 190392490709135
71:: 308061521170129 : 308061521170129
72:: 498454011879264 : 498454011879264
73:: 806515533049393 : 806515533049393
74:: 1304969544928657 : 1304969544928657
75:: 2111485077978050 : 2111485077978050
76:: 3416454622906707 : 3416454622906707
77:: 5527939700884757 : 5527939700884757
78:: 8944394323791464 : 8944394323791464
79:: 14472334024676221 : 14472334024676221
80:: 23416728348467685 : 23416728348467685
81:: 37889062373143906 : 37889062373143906
82:: 61305790721611591 : 61305790721611591
83:: 99194853094755497 : 99194853094755497
84:: 160500643816367088 : 160500643816367088
85:: 259695496911122585 : 259695496911122585
86:: 420196140727489673 : 420196140727489673
87:: 679891637638612258 : 679891637638612258
88:: 1100087778366101931 : 1100087778366101931
89:: 1779979416004714189 : 1779979416004714189
90:: 2880067194370816120 : 2880067194370816120
91:: 4660046610375530309 : -4563325426479245499
92:: 7540113804746346429 : -1683258232108429379
93:: -6246583658587674878 : -1634897640160286974