#include<bits/stdc++.h> using namespace std; #define REP(i, n) for(int i = 0; i < (n); i++) #define SIZE(s) ((int) (s).size()) #define ALL(s) s.begin(), s.end() #define MP make_pair #define ST first #define ND second using PII = pair<int, int>; using LL = long long; using PLL = pair<LL, LL>; using PIPLL = pair<int, PLL>; int k; // wszystko modulo 10^k; LL ptok; // 10^k LL p10[19]; mt19937 gen(42); struct matrix { LL a[2][2]; matrix(LL a00, LL a01, LL a10, LL a11){ a[0][0] = a00; a[0][1] = a01; a[1][0] = a10; a[1][1] = a11; }; }; LL add(LL a, LL b){ assert(0 <= a && 0 <= b); assert(a < ptok && b < ptok); LL r = a + b; if(r >= ptok) r-=ptok; return r; } LL mul(LL a, LL b){ assert(0 <= a && 0 <= b); assert(a < ptok && b < ptok); LL r = 0; while(a){ if(a&1ll) r = add(r, b); b = add(b, b); a /= 2; } assert(0<=r); assert(r < ptok); return r; } matrix mul(matrix m1, matrix m2){ return matrix(add(mul(m1.a[0][0], m2.a[0][0]), mul(m1.a[0][1], m2.a[1][0])), add(mul(m1.a[0][0], m2.a[0][1]), mul(m1.a[0][1], m2.a[1][1])), add(mul(m1.a[1][0], m2.a[0][0]), mul(m1.a[1][1], m2.a[1][0])), add(mul(m1.a[1][0], m2.a[0][1]), mul(m1.a[1][1], m2.a[1][1]))); } PLL mul(matrix m, PLL a){ return PLL(add(mul(m.a[0][0], a.ST), mul(m.a[0][1], a.ND)), add(mul(m.a[1][0], a.ST), mul(m.a[1][1], a.ND))); } enum op_t { MUL, ADD, VAL }; struct expr { expr *left; expr *right; int val; op_t op; expr(expr *_left, expr *_right, int _val, op_t _op): left(_left),right(_right), val(_val), op(_op){} }; expr *val(int _val){ return new expr(nullptr, nullptr, _val, VAL);} expr *add(expr *_left, expr *_right){ return new expr(_left, _right, -1, ADD); } expr *mul(expr *_left, expr *_right){ return new expr(_left, _right, -1, MUL); } void printbignum(vector<int> v){ if(v.empty()) printf("0"); REP(i, SIZE(v)) printf("%d", v[SIZE(v) - 1 - i]); } vector<int> tobignum(int val){ vector<int> v; while(val){v.push_back(val%10); val/=10;} return v; } vector<int> carry(vector<int> v){ v.push_back(0); REP(i, SIZE(v) - 1){ v[i+1] += v[i] / 10; v[i] %= 10; } if(!v.back()) v.pop_back(); return v; } vector<int> mul(vector<int> v1, vector<int> v2){ if(v1.empty() || v2.empty()) return vector<int>(); vector<int> v(v1.size() + v2.size() - 1); REP(i, SIZE(v1)) REP(j, SIZE(v2)){ v[i+j] += v1[i] * v2[j]; } return carry(v); } vector<int> add(vector<int> v1, vector<int> v2){ vector<int> v(max(SIZE(v1), SIZE(v2))); REP(i, SIZE(v1)) v[i] += v1[i]; REP(i, SIZE(v2)) v[i] += v2[i]; return carry(v); } vector<int> eval(expr *e){ switch(e->op){ case VAL: return tobignum(e->val); case ADD: return add(eval(e->left), eval(e->right)); case MUL: return mul(eval(e->left), eval(e->right)); default: assert(0); } } expr *solve(PLL a0, matrix m, LL c, int l){ if(k == l) return val(0); vector<int> is; vector<PLL> ps; vector<PIPLL> ips; matrix mt(1, 0, 0, 1); PLL p; int t; LL ptol = p10[l+1]; for(t=0, p=a0; t==0 || MP(p.ST%ptol, p.ND%ptol) != MP(a0.ST%ptol, a0.ND%ptol); t++, p = mul(m, p)){ if((p.ST/p10[l])%10 == c%10){ ips.emplace_back(t, p); is.push_back(t); ps.push_back(p); } mt = mul(mt, m); } shuffle(ALL(ips), gen); // in case of some tricky tests REP(i, SIZE(ips)){ expr *e = solve(ips[i].ND, mt, c/10, l+1); if(e != nullptr){ return add(val(ips[i].ST), mul(val(t), e)); } } return nullptr; } matrix powm(matrix m, LL c){ matrix r(1,0,0,1); while(c){ if(c&1ll) r = mul(r, m); m = mul(m, m); c/=2; } return r; } void check(LL c, vector<int> v){ matrix fib(0,1,1,1); matrix res(1,0,0,1); for(int d : v){ res = mul(res, powm(fib, d)); fib = powm(fib, 10); } assert(res.a[0][1] == c); } int main(){ p10[0] = 1ll; REP(i, 18) p10[i+1] = 10 * p10[i]; char digits[20]; assert(scanf("%s", digits) == 1); k = strlen(digits); ptok = 1; REP(i, k) ptok *= 10; LL c; sscanf(digits, "%lld", &c); matrix fib(0,1,1,1); expr *e = solve(MP(0, 1), fib, c, 0); if(e == nullptr){ printf("NIE\n"); } else { vector<int> v = eval(e); check(c, v); printbignum(v); printf("\n"); } }
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | #include<bits/stdc++.h> using namespace std; #define REP(i, n) for(int i = 0; i < (n); i++) #define SIZE(s) ((int) (s).size()) #define ALL(s) s.begin(), s.end() #define MP make_pair #define ST first #define ND second using PII = pair<int, int>; using LL = long long; using PLL = pair<LL, LL>; using PIPLL = pair<int, PLL>; int k; // wszystko modulo 10^k; LL ptok; // 10^k LL p10[19]; mt19937 gen(42); struct matrix { LL a[2][2]; matrix(LL a00, LL a01, LL a10, LL a11){ a[0][0] = a00; a[0][1] = a01; a[1][0] = a10; a[1][1] = a11; }; }; LL add(LL a, LL b){ assert(0 <= a && 0 <= b); assert(a < ptok && b < ptok); LL r = a + b; if(r >= ptok) r-=ptok; return r; } LL mul(LL a, LL b){ assert(0 <= a && 0 <= b); assert(a < ptok && b < ptok); LL r = 0; while(a){ if(a&1ll) r = add(r, b); b = add(b, b); a /= 2; } assert(0<=r); assert(r < ptok); return r; } matrix mul(matrix m1, matrix m2){ return matrix(add(mul(m1.a[0][0], m2.a[0][0]), mul(m1.a[0][1], m2.a[1][0])), add(mul(m1.a[0][0], m2.a[0][1]), mul(m1.a[0][1], m2.a[1][1])), add(mul(m1.a[1][0], m2.a[0][0]), mul(m1.a[1][1], m2.a[1][0])), add(mul(m1.a[1][0], m2.a[0][1]), mul(m1.a[1][1], m2.a[1][1]))); } PLL mul(matrix m, PLL a){ return PLL(add(mul(m.a[0][0], a.ST), mul(m.a[0][1], a.ND)), add(mul(m.a[1][0], a.ST), mul(m.a[1][1], a.ND))); } enum op_t { MUL, ADD, VAL }; struct expr { expr *left; expr *right; int val; op_t op; expr(expr *_left, expr *_right, int _val, op_t _op): left(_left),right(_right), val(_val), op(_op){} }; expr *val(int _val){ return new expr(nullptr, nullptr, _val, VAL);} expr *add(expr *_left, expr *_right){ return new expr(_left, _right, -1, ADD); } expr *mul(expr *_left, expr *_right){ return new expr(_left, _right, -1, MUL); } void printbignum(vector<int> v){ if(v.empty()) printf("0"); REP(i, SIZE(v)) printf("%d", v[SIZE(v) - 1 - i]); } vector<int> tobignum(int val){ vector<int> v; while(val){v.push_back(val%10); val/=10;} return v; } vector<int> carry(vector<int> v){ v.push_back(0); REP(i, SIZE(v) - 1){ v[i+1] += v[i] / 10; v[i] %= 10; } if(!v.back()) v.pop_back(); return v; } vector<int> mul(vector<int> v1, vector<int> v2){ if(v1.empty() || v2.empty()) return vector<int>(); vector<int> v(v1.size() + v2.size() - 1); REP(i, SIZE(v1)) REP(j, SIZE(v2)){ v[i+j] += v1[i] * v2[j]; } return carry(v); } vector<int> add(vector<int> v1, vector<int> v2){ vector<int> v(max(SIZE(v1), SIZE(v2))); REP(i, SIZE(v1)) v[i] += v1[i]; REP(i, SIZE(v2)) v[i] += v2[i]; return carry(v); } vector<int> eval(expr *e){ switch(e->op){ case VAL: return tobignum(e->val); case ADD: return add(eval(e->left), eval(e->right)); case MUL: return mul(eval(e->left), eval(e->right)); default: assert(0); } } expr *solve(PLL a0, matrix m, LL c, int l){ if(k == l) return val(0); vector<int> is; vector<PLL> ps; vector<PIPLL> ips; matrix mt(1, 0, 0, 1); PLL p; int t; LL ptol = p10[l+1]; for(t=0, p=a0; t==0 || MP(p.ST%ptol, p.ND%ptol) != MP(a0.ST%ptol, a0.ND%ptol); t++, p = mul(m, p)){ if((p.ST/p10[l])%10 == c%10){ ips.emplace_back(t, p); is.push_back(t); ps.push_back(p); } mt = mul(mt, m); } shuffle(ALL(ips), gen); // in case of some tricky tests REP(i, SIZE(ips)){ expr *e = solve(ips[i].ND, mt, c/10, l+1); if(e != nullptr){ return add(val(ips[i].ST), mul(val(t), e)); } } return nullptr; } matrix powm(matrix m, LL c){ matrix r(1,0,0,1); while(c){ if(c&1ll) r = mul(r, m); m = mul(m, m); c/=2; } return r; } void check(LL c, vector<int> v){ matrix fib(0,1,1,1); matrix res(1,0,0,1); for(int d : v){ res = mul(res, powm(fib, d)); fib = powm(fib, 10); } assert(res.a[0][1] == c); } int main(){ p10[0] = 1ll; REP(i, 18) p10[i+1] = 10 * p10[i]; char digits[20]; assert(scanf("%s", digits) == 1); k = strlen(digits); ptok = 1; REP(i, k) ptok *= 10; LL c; sscanf(digits, "%lld", &c); matrix fib(0,1,1,1); expr *e = solve(MP(0, 1), fib, c, 0); if(e == nullptr){ printf("NIE\n"); } else { vector<int> v = eval(e); check(c, v); printbignum(v); printf("\n"); } } |