import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.ArrayList; import java.util.List; public class fib { private static final long B = 1_000_000_000_000_000_000L; private static final BigInteger D = BigInteger.valueOf(B); private static final long[][] ONE = new long[][] { { 1, 1 }, { 1, 0 } }; private static final int M = 150100; private long[] f; BufferedReader rd; fib() throws IOException { f = new long[M]; f[1] = 1; for(int i=2;i<M;i++) { f[i] = (f[i-1] + f[i-2]) % B; } rd = new BufferedReader(new InputStreamReader(System.in)); String s = rd.readLine(); out(compute(s)); } private String compute(String s) throws IOException { long res; if(s.matches("0+")) { res = 0; } else { res = -1; if(s.length() <= 5) { for(int i=0;i<M;i++) { if(Long.toString(f[i]).endsWith(s)) { res = i; break; } } } else { String last5 = s.substring(s.length() - 5); List<Long> candidates = new ArrayList<>(); if(last5.matches("0+")) { int pos = s.length()-2; long pr = 15; while(pos >= 0 && s.charAt(pos)=='0') { pr *= 10; pos--; } candidates.add(pr); candidates.add(pr + (pr / 2)); } else { for(int i=0;i<M;i++) { if(("00000"+Long.toString(f[i])).endsWith(last5)) { candidates.add((long)i); } } } if(!candidates.isEmpty()) { for(Long v2: candidates) { long v = v2; long fv = cfib(v); long m = 75000; long mod = 1000000; long y = Long.parseLong(s); boolean found = Long.toString(fv).endsWith(s); if(!found) { while(!Long.toString(fv).endsWith(s)) { found = false; for(int i=0;i<20;i++) { long g = v + m*i; long h = cfib(g); if(h%mod == y%mod) { v = g; fv = h; found = true; break; } } if(!found || mod == B) { break; } m *= 10; mod *= 10; } } if(found) { res = v; break; } } } } } if(res == -1) { return "NIE"; } else { char[] res2 = "150000000000000000000000000000000".toCharArray(); char[] resc = Long.toString(res).toCharArray(); System.arraycopy(resc,0,res2,res2.length - resc.length, resc.length); return new String(res2); } } private long cfib(long y) { if(y == 0) { return 0; } long[][] a = pow(ONE, y); return a[1][0]; } private long[][] mul(long[][] a, long[][] b) { long[][] res = new long[2][2]; for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { BigInteger sum = BigInteger.ZERO; for(int k=0;k<2;k++) { BigInteger m = BigInteger.valueOf(a[i][k]).multiply(BigInteger.valueOf(b[k][j])); sum = sum.add(m); } sum = sum.mod(D); res[i][j] = sum.longValue(); } } return res; } private long[][] pow(long[][] a, long b) { if(b > 1) { long[][] u = pow(a,b/2); long[][] v = mul(u,u); if(b%2==1) { v = mul(v, a); } return v; } else if(b == 1) { return a; } else { return ONE; } } private static void out(Object x) { System.out.println(x); } public static void main(String[] args) throws IOException { new fib(); } }
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 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.ArrayList; import java.util.List; public class fib { private static final long B = 1_000_000_000_000_000_000L; private static final BigInteger D = BigInteger.valueOf(B); private static final long[][] ONE = new long[][] { { 1, 1 }, { 1, 0 } }; private static final int M = 150100; private long[] f; BufferedReader rd; fib() throws IOException { f = new long[M]; f[1] = 1; for(int i=2;i<M;i++) { f[i] = (f[i-1] + f[i-2]) % B; } rd = new BufferedReader(new InputStreamReader(System.in)); String s = rd.readLine(); out(compute(s)); } private String compute(String s) throws IOException { long res; if(s.matches("0+")) { res = 0; } else { res = -1; if(s.length() <= 5) { for(int i=0;i<M;i++) { if(Long.toString(f[i]).endsWith(s)) { res = i; break; } } } else { String last5 = s.substring(s.length() - 5); List<Long> candidates = new ArrayList<>(); if(last5.matches("0+")) { int pos = s.length()-2; long pr = 15; while(pos >= 0 && s.charAt(pos)=='0') { pr *= 10; pos--; } candidates.add(pr); candidates.add(pr + (pr / 2)); } else { for(int i=0;i<M;i++) { if(("00000"+Long.toString(f[i])).endsWith(last5)) { candidates.add((long)i); } } } if(!candidates.isEmpty()) { for(Long v2: candidates) { long v = v2; long fv = cfib(v); long m = 75000; long mod = 1000000; long y = Long.parseLong(s); boolean found = Long.toString(fv).endsWith(s); if(!found) { while(!Long.toString(fv).endsWith(s)) { found = false; for(int i=0;i<20;i++) { long g = v + m*i; long h = cfib(g); if(h%mod == y%mod) { v = g; fv = h; found = true; break; } } if(!found || mod == B) { break; } m *= 10; mod *= 10; } } if(found) { res = v; break; } } } } } if(res == -1) { return "NIE"; } else { char[] res2 = "150000000000000000000000000000000".toCharArray(); char[] resc = Long.toString(res).toCharArray(); System.arraycopy(resc,0,res2,res2.length - resc.length, resc.length); return new String(res2); } } private long cfib(long y) { if(y == 0) { return 0; } long[][] a = pow(ONE, y); return a[1][0]; } private long[][] mul(long[][] a, long[][] b) { long[][] res = new long[2][2]; for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { BigInteger sum = BigInteger.ZERO; for(int k=0;k<2;k++) { BigInteger m = BigInteger.valueOf(a[i][k]).multiply(BigInteger.valueOf(b[k][j])); sum = sum.add(m); } sum = sum.mod(D); res[i][j] = sum.longValue(); } } return res; } private long[][] pow(long[][] a, long b) { if(b > 1) { long[][] u = pow(a,b/2); long[][] v = mul(u,u); if(b%2==1) { v = mul(v, a); } return v; } else if(b == 1) { return a; } else { return ONE; } } private static void out(Object x) { System.out.println(x); } public static void main(String[] args) throws IOException { new fib(); } } |