#include <iostream> #include <string> #include <cmath> #include <vector> using namespace std; string dodaj(string s1, string s2){ string s3 = ""; for (int i=0; i<min(s1.length(),s2.length()); i++){ int x = (s1[i]-48)+s2[i]; s3 += x; if (x-48 > 9){ s3[i] = ((x-48)%10)+48; s3 += (x-48)/10+48; } s3 += '0'; if (s1.length() >= s2.length()){ for (int i=min(s1.length(),s2.length()); i<s1.length(); i++){ s3[i] += (s1[i]-48); x = s3[i]; if (x-48 > 9){ s3[i] = ((x-48)%10)+48; s3 += (x-48)/10+48; } s3 += '0'; } } else{ for (int i=min(s1.length(),s2.length()); i<s2.length(); i++){ s3[i] += (s2[i]-48); x = s3[i]; if (x-48 > 9){ s3[i] = ((x-48)%10)+48; s3 += (x-48)/10+48; } s3 += '0'; } } } string wynik = ""; int wsk = 0; for (int i=s3.length()-1; i>=0; i--){ if (s3[i] == '0'){ wsk++; } else break; } for (int i=0; i<s3.length()-wsk; i++){ wynik += s3[i]; } return wynik; } string mnoz(string s1, string s2){ string wynik = "0"; string mn = ""; for (int i=0; i<s2.length(); i++){ string pom = "0"; for (int j=0; j<s2[i]-48; j++){ pom = dodaj(pom,s1); } wynik = dodaj(wynik,mn+pom); mn += "0"; } return wynik; } long long int m=0, mp=0, r=0, rp=1, nwdw=0; void NWD(long long int a, long long int b){ if(b==0){ nwdw=a; r=a; return; } else NWD(b,a%b); long long int t=mp; mp=(rp-floor(a/b)*mp); m=b; rp=t; r=a; } void pot(long long int n, long long int T[2][2], long long int mod){ if (n == 0){ T[0][0] = 1; T[0][1] = 0; T[1][0] = 0; T[1][1] = 1; } if (n>1){ long long int T1[2][2]; for (int i=0; i<2; i++){ for (int j=0; j<2; j ++){ T1[i][j] = T[i][j]; } } pot(n/2,T1,mod); long long int T2[2][2]; T2[0][0] = (T1[0][0]*T1[0][0]+T1[0][1]*T1[1][0])%mod; T2[1][0] = (T1[1][0]*T1[0][0]+T1[1][1]*T1[1][0])%mod; T2[0][1] = (T1[0][0]*T1[0][1]+T1[0][1]*T1[1][1])%mod; T2[1][1] = (T1[1][0]*T1[0][1]+T1[1][1]*T1[1][1])%mod; if (n%2 == 1){ long long int T3[2][2]; T3[0][0] = (T2[0][0]*T[0][0]+T2[0][1]*T[1][0])%mod; T3[1][0] = (T2[1][0]*T[0][0]+T2[1][1]*T[1][0])%mod; T3[0][1] = (T2[0][0]*T[0][1]+T2[0][1]*T[1][1])%mod; T3[1][1] = (T2[1][0]*T[0][1]+T2[1][1]*T[1][1])%mod; for (int i=0; i<2; i++){ for (int j=0; j<2; j++){ T[i][j] = T3[i][j]; } } } else{ for (int i=0; i<2; i++){ for (int j=0; j<2; j++){ T[i][j] = T2[i][j]; } } } } for (int i=0; i<2; i++){ for (int j=0; j<2; j++){ if (T[i][j] < 0) T[i][j] += mod; } } } long long int fib(long long int n, long long int mod){ long long int T[2][2] = {{1,1},{1,0}}; pot(n,T,mod); return T[1][0]; } int main() { int n; string s; cin >> s; n = s.length(); long long int k = 0; long long int mn = 1; for (int i=1; i<=n; i++){ k += mn*(s[n-i]-48); mn *= 10; } long long int pot = min(n,n/2+1); long long int mod1 = 1; long long int mod2 = 1; for (int i=0; i<pot; i++){ mod1 *= 2; mod2 *= 5; } long long int k2 = k%mod1; long long int k5 = k%mod2; vector <long long int>dw; vector <long long int>pt; long long int f1 = 0; long long int f2 = 1; long long int wsk2 = 0; while (!(f1 == 0 && f2 == 1 && wsk2 != 0)){ if (f1 == k2) dw.push_back(wsk2); long long int pom = (f1 + f2)%mod1; f1 = f2; f2 = pom; wsk2++; } f1 = 0; f2 = 1; long long int wsk5 = 0; while (!(f1 == 0 && f2 == 1 && wsk5 != 0)){ if (f1 == k5) pt.push_back(wsk5); long long int pom = (f1 + f2)%mod2; f1 = f2; f2 = pom; wsk5++; } mod1 = 1; mod2 = 1; for (int i=0; i<n; i++){ mod1 *= 2; mod2 *= 5; } k2 = k%mod1; k5 = k%mod2; long long int ws2 = 3*mod1/2; long long int ws5 = 4*mod2; vector <long long int>ost2; vector <long long int>ost5; for (int i=0; i<ws2/wsk2; i++){ for (int j=0; j<dw.size(); j++){ if (fib(dw[j],mod1) == k2) ost2.push_back(dw[j]); dw[j] += wsk2; } } for (int i=0; i<ws5/wsk5; i++){ for (int j=0; j<pt.size(); j++){ if (fib(pt[j],mod2) == k5) ost5.push_back(pt[j]); pt[j] += wsk5; } } long long int nwd = 1; if (n<3){ for (int i=0; i<n-1; i++){ nwd *= 2; } } else nwd = 4; bool czy = 0; for (int i=0; i<ost2.size(); i++){ for (int j=0; j<ost5.size(); j++){ if (abs(ost2[i]-ost5[j])%nwd == 0 && czy == 0){ czy = 1; NWD(ws2/nwd,ws5/nwd); string s[4]; long long int l[4]; l[0] = abs(ost2[i]-ost5[j]); l[1] = abs(mp); l[2] = ws5/nwd; l[3] = ost5[j]; cout << l[0]*l[1]*l[2]+l[3]; } } } if (czy == 0) cout << "NIE"; }
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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | #include <iostream> #include <string> #include <cmath> #include <vector> using namespace std; string dodaj(string s1, string s2){ string s3 = ""; for (int i=0; i<min(s1.length(),s2.length()); i++){ int x = (s1[i]-48)+s2[i]; s3 += x; if (x-48 > 9){ s3[i] = ((x-48)%10)+48; s3 += (x-48)/10+48; } s3 += '0'; if (s1.length() >= s2.length()){ for (int i=min(s1.length(),s2.length()); i<s1.length(); i++){ s3[i] += (s1[i]-48); x = s3[i]; if (x-48 > 9){ s3[i] = ((x-48)%10)+48; s3 += (x-48)/10+48; } s3 += '0'; } } else{ for (int i=min(s1.length(),s2.length()); i<s2.length(); i++){ s3[i] += (s2[i]-48); x = s3[i]; if (x-48 > 9){ s3[i] = ((x-48)%10)+48; s3 += (x-48)/10+48; } s3 += '0'; } } } string wynik = ""; int wsk = 0; for (int i=s3.length()-1; i>=0; i--){ if (s3[i] == '0'){ wsk++; } else break; } for (int i=0; i<s3.length()-wsk; i++){ wynik += s3[i]; } return wynik; } string mnoz(string s1, string s2){ string wynik = "0"; string mn = ""; for (int i=0; i<s2.length(); i++){ string pom = "0"; for (int j=0; j<s2[i]-48; j++){ pom = dodaj(pom,s1); } wynik = dodaj(wynik,mn+pom); mn += "0"; } return wynik; } long long int m=0, mp=0, r=0, rp=1, nwdw=0; void NWD(long long int a, long long int b){ if(b==0){ nwdw=a; r=a; return; } else NWD(b,a%b); long long int t=mp; mp=(rp-floor(a/b)*mp); m=b; rp=t; r=a; } void pot(long long int n, long long int T[2][2], long long int mod){ if (n == 0){ T[0][0] = 1; T[0][1] = 0; T[1][0] = 0; T[1][1] = 1; } if (n>1){ long long int T1[2][2]; for (int i=0; i<2; i++){ for (int j=0; j<2; j ++){ T1[i][j] = T[i][j]; } } pot(n/2,T1,mod); long long int T2[2][2]; T2[0][0] = (T1[0][0]*T1[0][0]+T1[0][1]*T1[1][0])%mod; T2[1][0] = (T1[1][0]*T1[0][0]+T1[1][1]*T1[1][0])%mod; T2[0][1] = (T1[0][0]*T1[0][1]+T1[0][1]*T1[1][1])%mod; T2[1][1] = (T1[1][0]*T1[0][1]+T1[1][1]*T1[1][1])%mod; if (n%2 == 1){ long long int T3[2][2]; T3[0][0] = (T2[0][0]*T[0][0]+T2[0][1]*T[1][0])%mod; T3[1][0] = (T2[1][0]*T[0][0]+T2[1][1]*T[1][0])%mod; T3[0][1] = (T2[0][0]*T[0][1]+T2[0][1]*T[1][1])%mod; T3[1][1] = (T2[1][0]*T[0][1]+T2[1][1]*T[1][1])%mod; for (int i=0; i<2; i++){ for (int j=0; j<2; j++){ T[i][j] = T3[i][j]; } } } else{ for (int i=0; i<2; i++){ for (int j=0; j<2; j++){ T[i][j] = T2[i][j]; } } } } for (int i=0; i<2; i++){ for (int j=0; j<2; j++){ if (T[i][j] < 0) T[i][j] += mod; } } } long long int fib(long long int n, long long int mod){ long long int T[2][2] = {{1,1},{1,0}}; pot(n,T,mod); return T[1][0]; } int main() { int n; string s; cin >> s; n = s.length(); long long int k = 0; long long int mn = 1; for (int i=1; i<=n; i++){ k += mn*(s[n-i]-48); mn *= 10; } long long int pot = min(n,n/2+1); long long int mod1 = 1; long long int mod2 = 1; for (int i=0; i<pot; i++){ mod1 *= 2; mod2 *= 5; } long long int k2 = k%mod1; long long int k5 = k%mod2; vector <long long int>dw; vector <long long int>pt; long long int f1 = 0; long long int f2 = 1; long long int wsk2 = 0; while (!(f1 == 0 && f2 == 1 && wsk2 != 0)){ if (f1 == k2) dw.push_back(wsk2); long long int pom = (f1 + f2)%mod1; f1 = f2; f2 = pom; wsk2++; } f1 = 0; f2 = 1; long long int wsk5 = 0; while (!(f1 == 0 && f2 == 1 && wsk5 != 0)){ if (f1 == k5) pt.push_back(wsk5); long long int pom = (f1 + f2)%mod2; f1 = f2; f2 = pom; wsk5++; } mod1 = 1; mod2 = 1; for (int i=0; i<n; i++){ mod1 *= 2; mod2 *= 5; } k2 = k%mod1; k5 = k%mod2; long long int ws2 = 3*mod1/2; long long int ws5 = 4*mod2; vector <long long int>ost2; vector <long long int>ost5; for (int i=0; i<ws2/wsk2; i++){ for (int j=0; j<dw.size(); j++){ if (fib(dw[j],mod1) == k2) ost2.push_back(dw[j]); dw[j] += wsk2; } } for (int i=0; i<ws5/wsk5; i++){ for (int j=0; j<pt.size(); j++){ if (fib(pt[j],mod2) == k5) ost5.push_back(pt[j]); pt[j] += wsk5; } } long long int nwd = 1; if (n<3){ for (int i=0; i<n-1; i++){ nwd *= 2; } } else nwd = 4; bool czy = 0; for (int i=0; i<ost2.size(); i++){ for (int j=0; j<ost5.size(); j++){ if (abs(ost2[i]-ost5[j])%nwd == 0 && czy == 0){ czy = 1; NWD(ws2/nwd,ws5/nwd); string s[4]; long long int l[4]; l[0] = abs(ost2[i]-ost5[j]); l[1] = abs(mp); l[2] = ws5/nwd; l[3] = ost5[j]; cout << l[0]*l[1]*l[2]+l[3]; } } } if (czy == 0) cout << "NIE"; } |