import java.io.InputStream; import java.io.PrintStream; import java.math.BigInteger; import java.util.*; public class fib { public static void main(String[] args) { InputStream in = System.in; PrintStream out = System.out; calculate(in, out); } public static int calculate(InputStream in, PrintStream out) { /** Read input and init tables */ Scanner scanner = new Scanner(in); String ending = scanner.next(); scanner.close(); long found = findFn(lastChars(ending, 2), 1000); if (found == -1) { out.println("NIE"); return 0; } found = findFn(lastChars(ending, 3), 10000); if (found == -1) { out.println("NIE"); return 0; } found = findFn(lastChars(ending, 5), 100000); if (found == -1) { out.println("NIE"); } else { out.println(found); } return 0; } private static String lastChars(String ending, int size) { if (size > 0 && ending.length() > size) { return ending.substring(ending.length() - size); } else { return ending; } } private static long findFn(String ending, int limit) { BigInteger tail = new BigInteger(ending); BigInteger tailCut = BigInteger.TEN.pow(ending.length()); BigInteger tfn0 = BigInteger.ZERO; BigInteger tfn1 = BigInteger.ONE; BigInteger tfn = null; long n = 1; while (n < limit || limit == -1) { n++; tfn = tfn0.add(tfn1).mod(tailCut); tfn0 = tfn1; tfn1 = tfn; if (tfn.compareTo(tail) == 0) { String v = tfn.toString(); if (pad(v, ending.length()).equals(ending)) { return n; } } } return -1; } private static String pad(String v, int length) { while (v.length() < length) { v = "0" + v; } return v; } }
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 | import java.io.InputStream; import java.io.PrintStream; import java.math.BigInteger; import java.util.*; public class fib { public static void main(String[] args) { InputStream in = System.in; PrintStream out = System.out; calculate(in, out); } public static int calculate(InputStream in, PrintStream out) { /** Read input and init tables */ Scanner scanner = new Scanner(in); String ending = scanner.next(); scanner.close(); long found = findFn(lastChars(ending, 2), 1000); if (found == -1) { out.println("NIE"); return 0; } found = findFn(lastChars(ending, 3), 10000); if (found == -1) { out.println("NIE"); return 0; } found = findFn(lastChars(ending, 5), 100000); if (found == -1) { out.println("NIE"); } else { out.println(found); } return 0; } private static String lastChars(String ending, int size) { if (size > 0 && ending.length() > size) { return ending.substring(ending.length() - size); } else { return ending; } } private static long findFn(String ending, int limit) { BigInteger tail = new BigInteger(ending); BigInteger tailCut = BigInteger.TEN.pow(ending.length()); BigInteger tfn0 = BigInteger.ZERO; BigInteger tfn1 = BigInteger.ONE; BigInteger tfn = null; long n = 1; while (n < limit || limit == -1) { n++; tfn = tfn0.add(tfn1).mod(tailCut); tfn0 = tfn1; tfn1 = tfn; if (tfn.compareTo(tail) == 0) { String v = tfn.toString(); if (pad(v, ending.length()).equals(ending)) { return n; } } } return -1; } private static String pad(String v, int length) { while (v.length() < length) { v = "0" + v; } return v; } } |