import java.math.BigInteger; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class fib { static Map<Integer, BigInteger> memo = new HashMap<>(); static BigInteger fib(int n) { if (n == 0) { return BigInteger.ZERO; } else if (n == 1) { return BigInteger.ONE; } else if (memo.containsKey(n)) { return memo.get(n); } else { BigInteger result = fib(n - 1).add(fib(n - 2)); memo.put(n, result); return result; } } static Map<Integer, BigInteger> periods = new HashMap<Integer, BigInteger>() {{ put(4, new BigInteger("150000")); put(5, new BigInteger("1500000")); put(6, new BigInteger("15000000")); put(7, new BigInteger("150000000")); put(8, new BigInteger("1500000000")); put(9, new BigInteger("15000000000")); put(10, new BigInteger("150000000000")); put(11, new BigInteger("1500000000000")); put(12, new BigInteger("15000000000000")); put(13, new BigInteger("150000000000000")); put(14, new BigInteger("1500000000000000")); put(15, new BigInteger("15000000000000000")); put(16, new BigInteger("150000000000000000")); put(17, new BigInteger("1500000000000000000")); put(18, new BigInteger("15000000000000000000")); }}; static String find(String pattern) { for (int i=0; i<=1500; i++) { if (("" + fib(i)).endsWith(pattern)) return "" + i; } return "NIE"; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String pattern = in.nextLine(); System.out.println(find(pattern)); } }
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 | import java.math.BigInteger; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class fib { static Map<Integer, BigInteger> memo = new HashMap<>(); static BigInteger fib(int n) { if (n == 0) { return BigInteger.ZERO; } else if (n == 1) { return BigInteger.ONE; } else if (memo.containsKey(n)) { return memo.get(n); } else { BigInteger result = fib(n - 1).add(fib(n - 2)); memo.put(n, result); return result; } } static Map<Integer, BigInteger> periods = new HashMap<Integer, BigInteger>() {{ put(4, new BigInteger("150000")); put(5, new BigInteger("1500000")); put(6, new BigInteger("15000000")); put(7, new BigInteger("150000000")); put(8, new BigInteger("1500000000")); put(9, new BigInteger("15000000000")); put(10, new BigInteger("150000000000")); put(11, new BigInteger("1500000000000")); put(12, new BigInteger("15000000000000")); put(13, new BigInteger("150000000000000")); put(14, new BigInteger("1500000000000000")); put(15, new BigInteger("15000000000000000")); put(16, new BigInteger("150000000000000000")); put(17, new BigInteger("1500000000000000000")); put(18, new BigInteger("15000000000000000000")); }}; static String find(String pattern) { for (int i=0; i<=1500; i++) { if (("" + fib(i)).endsWith(pattern)) return "" + i; } return "NIE"; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String pattern = in.nextLine(); System.out.println(find(pattern)); } } |