import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; public class fib { public static void main(String args[]) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); System.out.println(getFib(bufferedReader.readLine())); bufferedReader.close(); } private static String getFib(String digits) { long[] buffer = { 0, 1 }; long fib; long fibSmaller; int len = digits.length(); int timesCut = 0; long allowedTimesCut = (long)Math.min(1000000, Math.pow(10, len)); long digitsLong = new Long(digits); BigInteger k = new BigInteger("2"); BigInteger limit = new BigInteger("10").pow(100); boolean sizeOk = len == 1; while (timesCut < allowedTimesCut && k.compareTo(limit) <= 0 ) { fib = (buffer[0] + buffer[1]); if (!sizeOk && fib > Math.pow(10, len - 1)) { sizeOk = true; } fibSmaller = fib % (long) Math.pow(10, len); if (fib != fibSmaller) { fib = fibSmaller; timesCut++; } if (sizeOk && fib == digitsLong) { return k.toString(10); } k = k.add(BigInteger.ONE); buffer[0] = buffer[1]; buffer[1] = fib; } return "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 | import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; public class fib { public static void main(String args[]) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); System.out.println(getFib(bufferedReader.readLine())); bufferedReader.close(); } private static String getFib(String digits) { long[] buffer = { 0, 1 }; long fib; long fibSmaller; int len = digits.length(); int timesCut = 0; long allowedTimesCut = (long)Math.min(1000000, Math.pow(10, len)); long digitsLong = new Long(digits); BigInteger k = new BigInteger("2"); BigInteger limit = new BigInteger("10").pow(100); boolean sizeOk = len == 1; while (timesCut < allowedTimesCut && k.compareTo(limit) <= 0 ) { fib = (buffer[0] + buffer[1]); if (!sizeOk && fib > Math.pow(10, len - 1)) { sizeOk = true; } fibSmaller = fib % (long) Math.pow(10, len); if (fib != fibSmaller) { fib = fibSmaller; timesCut++; } if (sizeOk && fib == digitsLong) { return k.toString(10); } k = k.add(BigInteger.ONE); buffer[0] = buffer[1]; buffer[1] = fib; } return "NIE"; } } |