#include <cstdio> #include <string.h> #include <vector> #include <algorithm> // matrix start typedef unsigned long long ulli; ulli MOD = 1000000000000000000; struct matrix { ulli a1, a2, b1, b2; }; ulli mulMod(ulli a, ulli b, ulli n) { ulli w=0; ulli m=1; while(m) { if (b&m) w = (w+a)%n; a = (a<<1)%n; m<<=1; } return w; } matrix mul(const matrix &a, const matrix &b) { matrix c; c.a1 = (mulMod(a.a1,b.a1,MOD) + mulMod(a.b1,b.a2,MOD))%MOD; c.a2 = (mulMod(a.a2,b.a1,MOD) + mulMod(a.b2,b.a2,MOD))%MOD; c.b1 = (mulMod(a.a1,b.b1,MOD) + mulMod(a.b1,b.b2,MOD))%MOD; c.b2 = (mulMod(a.a2,b.b1,MOD) + mulMod(a.b2,b.b2,MOD))%MOD; return c; } matrix matrixtab[65]; matrix fib0 = { 2, 1, 1, 0 }; void premul() { matrix x = { 1,1,1,0}; matrixtab[1] = x; for (int i=2; i<65; ++i) { matrixtab[i] = mul(matrixtab[i-1], matrixtab[i-1]); } } ulli generate(ulli n) { matrix start = fib0; for (int i=1; i<64; ++i, n >>=1) { if (n & 1) { start = mul(start, matrixtab[i]); } } return start.b2; } // matrix end ulli l2m[19] = { 0, 0, 60, 150, 1500, 15000, 150000, 1500000, 15000000, 150000000, 1500000000, 15000000000, 150000000000, 1500000000000, 15000000000000, 150000000000000, 1500000000000000, 15000000000000000, 150000000000000000}; int tabs; char tab[20]; ulli bin; ulli mod = 1; std::vector<ulli> can; bool quickcheck(ulli f, ulli mmm) { return (f%mmm) == (bin%mmm); } bool realcheck(ulli f, const char *patt) { char dupa[32]; int pattlen = strlen(patt); int ile = sprintf(dupa, "%llu", f); if (pattlen > ile) return false; // printf("%s %s\n", dupa, patt); return 0 == strncmp(&dupa[ile-pattlen], patt, strlen(patt)); } int main(void) { premul(); scanf("%s", tab); sscanf(tab, "%llu", &bin); tabs = strlen(tab); // lets find candidates from 0 to 60 ulli i; ulli f1=0; ulli f2=1; for (i = 0; i<60; ++i) { if ((f1%10) == bin%10) can.push_back(i); ulli temp = (f1+f2)%1000000000000000000; f1 = f2; f2 = temp; } int level = 2; int distance = 150; ulli mo = 100; for (int level=2; level <= tabs; ++level, mo *=10) { // printf("==> level %d\n", level); std::vector<ulli> cannext; for (int j=0; j < can.size(); ++j) { // printf("%llu ", can[j]); for (int k=0; k < 10; ++k) { ulli current = can[j]+k*l2m[level]; ulli fib = generate(current); // printf(" skok: %llu fib: %llu\n", current, fib); if(quickcheck(fib, mo)) { // printf("%llu %llu %llu\n", mo, current, fib); // printf(" * %llu", current); cannext.push_back(current); } } // printf("\n"); } std::vector<ulli>::iterator it = std::unique(cannext.begin(), cannext.end()); cannext.erase(it, cannext.end()); can = cannext; } // printf("Wynik:\n"); for (int i=0; i<can.size(); ++i) { ulli x = generate(can[i]); if (realcheck(x, tab)) { printf("%llu\n", can[i]); return 0; } } printf("NIE\n"); // if (can.empty()) // printf("NIE\n"); // else // printf("%llu\n", can[0]); 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | #include <cstdio> #include <string.h> #include <vector> #include <algorithm> // matrix start typedef unsigned long long ulli; ulli MOD = 1000000000000000000; struct matrix { ulli a1, a2, b1, b2; }; ulli mulMod(ulli a, ulli b, ulli n) { ulli w=0; ulli m=1; while(m) { if (b&m) w = (w+a)%n; a = (a<<1)%n; m<<=1; } return w; } matrix mul(const matrix &a, const matrix &b) { matrix c; c.a1 = (mulMod(a.a1,b.a1,MOD) + mulMod(a.b1,b.a2,MOD))%MOD; c.a2 = (mulMod(a.a2,b.a1,MOD) + mulMod(a.b2,b.a2,MOD))%MOD; c.b1 = (mulMod(a.a1,b.b1,MOD) + mulMod(a.b1,b.b2,MOD))%MOD; c.b2 = (mulMod(a.a2,b.b1,MOD) + mulMod(a.b2,b.b2,MOD))%MOD; return c; } matrix matrixtab[65]; matrix fib0 = { 2, 1, 1, 0 }; void premul() { matrix x = { 1,1,1,0}; matrixtab[1] = x; for (int i=2; i<65; ++i) { matrixtab[i] = mul(matrixtab[i-1], matrixtab[i-1]); } } ulli generate(ulli n) { matrix start = fib0; for (int i=1; i<64; ++i, n >>=1) { if (n & 1) { start = mul(start, matrixtab[i]); } } return start.b2; } // matrix end ulli l2m[19] = { 0, 0, 60, 150, 1500, 15000, 150000, 1500000, 15000000, 150000000, 1500000000, 15000000000, 150000000000, 1500000000000, 15000000000000, 150000000000000, 1500000000000000, 15000000000000000, 150000000000000000}; int tabs; char tab[20]; ulli bin; ulli mod = 1; std::vector<ulli> can; bool quickcheck(ulli f, ulli mmm) { return (f%mmm) == (bin%mmm); } bool realcheck(ulli f, const char *patt) { char dupa[32]; int pattlen = strlen(patt); int ile = sprintf(dupa, "%llu", f); if (pattlen > ile) return false; // printf("%s %s\n", dupa, patt); return 0 == strncmp(&dupa[ile-pattlen], patt, strlen(patt)); } int main(void) { premul(); scanf("%s", tab); sscanf(tab, "%llu", &bin); tabs = strlen(tab); // lets find candidates from 0 to 60 ulli i; ulli f1=0; ulli f2=1; for (i = 0; i<60; ++i) { if ((f1%10) == bin%10) can.push_back(i); ulli temp = (f1+f2)%1000000000000000000; f1 = f2; f2 = temp; } int level = 2; int distance = 150; ulli mo = 100; for (int level=2; level <= tabs; ++level, mo *=10) { // printf("==> level %d\n", level); std::vector<ulli> cannext; for (int j=0; j < can.size(); ++j) { // printf("%llu ", can[j]); for (int k=0; k < 10; ++k) { ulli current = can[j]+k*l2m[level]; ulli fib = generate(current); // printf(" skok: %llu fib: %llu\n", current, fib); if(quickcheck(fib, mo)) { // printf("%llu %llu %llu\n", mo, current, fib); // printf(" * %llu", current); cannext.push_back(current); } } // printf("\n"); } std::vector<ulli>::iterator it = std::unique(cannext.begin(), cannext.end()); cannext.erase(it, cannext.end()); can = cannext; } // printf("Wynik:\n"); for (int i=0; i<can.size(); ++i) { ulli x = generate(can[i]); if (realcheck(x, tab)) { printf("%llu\n", can[i]); return 0; } } printf("NIE\n"); // if (can.empty()) // printf("NIE\n"); // else // printf("%llu\n", can[0]); return 0; } |