import java.io.*; import java.math.BigInteger; import java.util.Arrays; import java.util.StringTokenizer; public class fib { private static final BigInteger max = new BigInteger("10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); private static BigInteger iterator = BigInteger.ONE; private static String f0 = "0"; private static char[] f0c = new char[18]; private static String f1 = "1"; private static char[] f1c = new char[18]; private static char[] sum = new char[18]; static { Arrays.fill(f0c, (char)0); f0c[0] = '0'; Arrays.fill(f1c, (char)0); f1c[0] = '1'; } private static void sum(){ int add = 0; for (int i = 0; i < 18; i++) { if (f0c[i] != 0 && f1c[i] != 0) { sum[i] = (char) (f0c[i] + f1c[i] - '0' + add); } else if (f0c[i] != 0 || f1c[i] != 0) { sum[i] = (char) (f0c[i] + f1c[i] + add); } else if (add > 0) { sum[i] = '1'; } if (sum[i] - '0' > 9) { sum[i] = (char) (sum[i] - 10); add = 1; } else { add = 0; } } } private static String nextFib(){ iterator = iterator.add(BigInteger.ONE); String f2 = new BigInteger(f0).add(new BigInteger(f1)).toString(); int len = f2.length(); f2 = f2.substring(len > 18? len - 18 : 0 ,len); f0 = f1; f1 = f2; System.out.println(f2); return f2; } private static void nextFibc(){ iterator = iterator.add(BigInteger.ONE); sum(); for (int i = 0; i < 18; i++) { f0c[i] = f1c[i]; f1c[i] = sum[i]; } } private static boolean endsWith(){ for (int i = 0, max = endc.length; i < max; i++) { if (endc[max-i-1] != f1c[i]) { return false; } } return true; } private static void doMagic() { if ("0".endsWith(end)) { System.out.println("0"); return; } if ("1".endsWith(end)) { System.out.println("1"); return; } /* //to slow while (iterator.compareTo(max) == -1) { if (nextFib().endsWith(end)) { System.out.println(iterator); return; } }*/ iterator = BigInteger.ONE; while (iterator.compareTo(max) == -1) { nextFibc(); if (endsWith()) { System.out.println(iterator); return; } } System.out.println("NIE"); } private static String end; private static char[] endc; private static void readInput(String[] args) { BufferedReader br; try { br = new BufferedReader(new FileReader(args[0])); } catch (FileNotFoundException | IndexOutOfBoundsException e) { br = new BufferedReader(new InputStreamReader(System.in)); } try { StringTokenizer st = new StringTokenizer(br.readLine()); end = st.nextToken(); endc = end.toCharArray(); } catch (IOException ignore) {} } public static void main(String[] args) { readInput(args); doMagic(); } }
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 | import java.io.*; import java.math.BigInteger; import java.util.Arrays; import java.util.StringTokenizer; public class fib { private static final BigInteger max = new BigInteger("10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); private static BigInteger iterator = BigInteger.ONE; private static String f0 = "0"; private static char[] f0c = new char[18]; private static String f1 = "1"; private static char[] f1c = new char[18]; private static char[] sum = new char[18]; static { Arrays.fill(f0c, (char)0); f0c[0] = '0'; Arrays.fill(f1c, (char)0); f1c[0] = '1'; } private static void sum(){ int add = 0; for (int i = 0; i < 18; i++) { if (f0c[i] != 0 && f1c[i] != 0) { sum[i] = (char) (f0c[i] + f1c[i] - '0' + add); } else if (f0c[i] != 0 || f1c[i] != 0) { sum[i] = (char) (f0c[i] + f1c[i] + add); } else if (add > 0) { sum[i] = '1'; } if (sum[i] - '0' > 9) { sum[i] = (char) (sum[i] - 10); add = 1; } else { add = 0; } } } private static String nextFib(){ iterator = iterator.add(BigInteger.ONE); String f2 = new BigInteger(f0).add(new BigInteger(f1)).toString(); int len = f2.length(); f2 = f2.substring(len > 18? len - 18 : 0 ,len); f0 = f1; f1 = f2; System.out.println(f2); return f2; } private static void nextFibc(){ iterator = iterator.add(BigInteger.ONE); sum(); for (int i = 0; i < 18; i++) { f0c[i] = f1c[i]; f1c[i] = sum[i]; } } private static boolean endsWith(){ for (int i = 0, max = endc.length; i < max; i++) { if (endc[max-i-1] != f1c[i]) { return false; } } return true; } private static void doMagic() { if ("0".endsWith(end)) { System.out.println("0"); return; } if ("1".endsWith(end)) { System.out.println("1"); return; } /* //to slow while (iterator.compareTo(max) == -1) { if (nextFib().endsWith(end)) { System.out.println(iterator); return; } }*/ iterator = BigInteger.ONE; while (iterator.compareTo(max) == -1) { nextFibc(); if (endsWith()) { System.out.println(iterator); return; } } System.out.println("NIE"); } private static String end; private static char[] endc; private static void readInput(String[] args) { BufferedReader br; try { br = new BufferedReader(new FileReader(args[0])); } catch (FileNotFoundException | IndexOutOfBoundsException e) { br = new BufferedReader(new InputStreamReader(System.in)); } try { StringTokenizer st = new StringTokenizer(br.readLine()); end = st.nextToken(); endc = end.toCharArray(); } catch (IOException ignore) {} } public static void main(String[] args) { readInput(args); doMagic(); } } |