// Author : Hanczar #include <iostream> #include <stdint.h> #include <stdio.h> using namespace std; typedef unsigned long long int llu; #define DEBUG 0 const llu MAX_VALUE = 1000000000000000000LL; static void print128(llu _res) { char bufor[128]; bufor[127] = '\0'; int pos = 127; do { bufor[--pos] = _res % 10 + '0'; _res /= 10; } while (_res > 0 ); cout << bufor + pos << endl; } static const llu INT_1G = 1000000000; static llu doMul(llu f,llu s) { llu fhi = f / INT_1G; llu shi = s / INT_1G; llu flo = f % INT_1G; llu slo = s % INT_1G; llu lowres = ( flo * slo ); llu fhi_sec =(fhi * slo) % INT_1G; llu shi_f =(shi * flo) % INT_1G; llu result = lowres + ((fhi_sec + shi_f )* INT_1G); result %= MAX_VALUE; return result; } static void mul(llu M1[][2], llu M2[][2]) { llu x = (doMul(M1[0][0],M2[0][0]) + doMul(M1[0][1],M2[1][0])) % MAX_VALUE; llu y = (doMul(M1[0][0],M2[0][1]) + doMul(M1[0][1],M2[1][1])) % MAX_VALUE; llu z = (doMul(M1[1][0],M2[0][0]) + doMul(M1[1][1],M2[1][0])) % MAX_VALUE; llu w = (doMul(M1[1][0],M2[0][1]) + doMul(M1[1][1],M2[1][1])) % MAX_VALUE; M1[0][0] = x; M1[0][1] = y; M1[1][0] = z; M1[1][1] = w; } void pow(llu n, llu F[][2]) { if (n > 1) { llu M[2][2] = { { 1, 1 }, { 1, 0 } }; pow(n / 2, F); mul(F, F); if (1 == (n & 1)) { mul(F, M); } } } llu fib(llu n) { llu F[2][2] = { { 1, 1 }, { 1, 0 } }; if (n > 0) { pow(n - 1, F); return F[0][0]; } else { return 0; } } llu find(llu posmul, llu n, llu k, llu period, llu ending) { for (int i = 0; i < 10; i++, k += period) { llu fi = fib(k); if (fi % posmul == n % posmul ) { if (posmul == ending ) { if (fi > ending / 10 ) { return k; } else { break; } } else { llu result = find(posmul * 10, n, k, period * 10, ending); if (result != -1) { return result; } } } } return -1; } int main(int argc, char *argv[]) { for (int j = 0; j < 1; j++) { if(DEBUG)cout << "------------------------------------------------------" << endl; llu n = 0; llu k = 0; llu ending = 1; string text; bool notfound = false; bool found = true; cin >> text; if(DEBUG)cout << "Text length : " << text.length() << endl; if(DEBUG)cout << "Text : " << text << endl; int textpos = 0; while (textpos < text.length()) { n = n * 10 + text[textpos++] - '0'; ending *= 10; } if (DEBUG) cout << " N:" << (uint64_t)n << " ending:" << (uint64_t)ending << endl; int i; llu result; for (i = 0; i < 60; i++) { result = find(10,n,i,60,ending); if (-1!=result){ break; } } if (-1==result) { cout << "NIE" << endl; } else { print128(result); if (DEBUG)cout << "fi(result) " ; if (DEBUG)print128(fib(result)); } } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | // Author : Hanczar #include <iostream> #include <stdint.h> #include <stdio.h> using namespace std; typedef unsigned long long int llu; #define DEBUG 0 const llu MAX_VALUE = 1000000000000000000LL; static void print128(llu _res) { char bufor[128]; bufor[127] = '\0'; int pos = 127; do { bufor[--pos] = _res % 10 + '0'; _res /= 10; } while (_res > 0 ); cout << bufor + pos << endl; } static const llu INT_1G = 1000000000; static llu doMul(llu f,llu s) { llu fhi = f / INT_1G; llu shi = s / INT_1G; llu flo = f % INT_1G; llu slo = s % INT_1G; llu lowres = ( flo * slo ); llu fhi_sec =(fhi * slo) % INT_1G; llu shi_f =(shi * flo) % INT_1G; llu result = lowres + ((fhi_sec + shi_f )* INT_1G); result %= MAX_VALUE; return result; } static void mul(llu M1[][2], llu M2[][2]) { llu x = (doMul(M1[0][0],M2[0][0]) + doMul(M1[0][1],M2[1][0])) % MAX_VALUE; llu y = (doMul(M1[0][0],M2[0][1]) + doMul(M1[0][1],M2[1][1])) % MAX_VALUE; llu z = (doMul(M1[1][0],M2[0][0]) + doMul(M1[1][1],M2[1][0])) % MAX_VALUE; llu w = (doMul(M1[1][0],M2[0][1]) + doMul(M1[1][1],M2[1][1])) % MAX_VALUE; M1[0][0] = x; M1[0][1] = y; M1[1][0] = z; M1[1][1] = w; } void pow(llu n, llu F[][2]) { if (n > 1) { llu M[2][2] = { { 1, 1 }, { 1, 0 } }; pow(n / 2, F); mul(F, F); if (1 == (n & 1)) { mul(F, M); } } } llu fib(llu n) { llu F[2][2] = { { 1, 1 }, { 1, 0 } }; if (n > 0) { pow(n - 1, F); return F[0][0]; } else { return 0; } } llu find(llu posmul, llu n, llu k, llu period, llu ending) { for (int i = 0; i < 10; i++, k += period) { llu fi = fib(k); if (fi % posmul == n % posmul ) { if (posmul == ending ) { if (fi > ending / 10 ) { return k; } else { break; } } else { llu result = find(posmul * 10, n, k, period * 10, ending); if (result != -1) { return result; } } } } return -1; } int main(int argc, char *argv[]) { for (int j = 0; j < 1; j++) { if(DEBUG)cout << "------------------------------------------------------" << endl; llu n = 0; llu k = 0; llu ending = 1; string text; bool notfound = false; bool found = true; cin >> text; if(DEBUG)cout << "Text length : " << text.length() << endl; if(DEBUG)cout << "Text : " << text << endl; int textpos = 0; while (textpos < text.length()) { n = n * 10 + text[textpos++] - '0'; ending *= 10; } if (DEBUG) cout << " N:" << (uint64_t)n << " ending:" << (uint64_t)ending << endl; int i; llu result; for (i = 0; i < 60; i++) { result = find(10,n,i,60,ending); if (-1!=result){ break; } } if (-1==result) { cout << "NIE" << endl; } else { print128(result); if (DEBUG)cout << "fi(result) " ; if (DEBUG)print128(fib(result)); } } return 0; } |