import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.ArrayList; public class fib { private static int obliczMaleN(int n, int size) { assert size <= 3 && size >= 1; int max = 1500, mod = 1000; if (size == 1) { max = 60; mod = 10; } else if (size == 2) { max = 300; mod = 100; } int a = 0, b = 1, k = 0; while (k < max) { int c = a + b; if (c >= mod) { c -= mod; } if (a == n) { return k + max; } a = b; b = c; ++k; } return -1; } private static long fib(long k, long mod) { if (k == 0) return 0; if (k == 1 || k == 2) return 1; BigInteger modBig = BigInteger.valueOf(mod); BigInteger a2 = BigInteger.ONE; BigInteger a1 = BigInteger.ONE; BigInteger a0 = BigInteger.ZERO; BigInteger f1 = BigInteger.ONE; BigInteger f0 = BigInteger.ZERO; --k; while (k > 0) { if ((k & 1) == 1) { BigInteger f1a2 = f1.multiply(a2); BigInteger f0a1 = f0.multiply(a1); BigInteger f1a1 = f1.multiply(a1); BigInteger f0a0 = f0.multiply(a0); f1 = f1a2.add(f0a1).mod(modBig); f0 = f1a1.add(f0a0).mod(modBig); } BigInteger a2a2 = a2.pow(2); BigInteger a1a1 = a1.pow(2); BigInteger a0a0 = a0.pow(2); BigInteger a2a1 = a2.multiply(a1); BigInteger a1a0 = a1.multiply(a0); a2 = a2a2.add(a1a1).mod(modBig); a1 = a2a1.add(a1a0).mod(modBig); a0 = a1a1.add(a0a0).mod(modBig); k >>= 1; } return f1.longValue(); } private static ArrayList<Long> potencjalneLiczby(long n, long size) { if (size <= 3) { ArrayList<Long> wynik = new ArrayList<>(); int a = 0, b = 1, k = 0; int nInt = (int)(n % 1000); while (k < 1500) { int c = a + b; if (c >= 1000) { c -= 1000; } if (a == nInt) { wynik.add((long)k); } a = b; b = c; ++k; } return wynik; } ArrayList<Long> potencjalne = potencjalneLiczby(n, size - 1); if (potencjalne.isEmpty()) { return potencjalne; } long mod = 1; for (int i = 0; i < size; ++i) { mod *= 10; } long k_15 = 15; for (int i = 2; i < size; ++i) { k_15 *= 10; } long nMod = n % mod; ArrayList<Long> wynik = new ArrayList<>(); for(long k : potencjalne) { for (int i = 0; i < 10; ++i) { if (fib(k, mod) == nMod) { wynik.add(k); } k += k_15; } } return wynik; } private static long obliczDuzeN(long n, int size) { ArrayList<Long> wynik = potencjalneLiczby(n, size); if (wynik.isEmpty()) { return -1; } long k_15 = 15; for (int i = 1; i < size; ++i) { k_15 *= 10; } return wynik.get(0) + k_15; } public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); String nStr = input.readLine(); long nInt = Long.parseLong(nStr); int nSize = nStr.length(); long wynik = nSize <= 3 ? obliczMaleN((int) nInt, nSize) : obliczDuzeN(nInt, nSize); if (wynik == -1){ System.out.print("NIE"); } else { System.out.print(wynik); //System.out.print("TAK"); } } }
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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.ArrayList; public class fib { private static int obliczMaleN(int n, int size) { assert size <= 3 && size >= 1; int max = 1500, mod = 1000; if (size == 1) { max = 60; mod = 10; } else if (size == 2) { max = 300; mod = 100; } int a = 0, b = 1, k = 0; while (k < max) { int c = a + b; if (c >= mod) { c -= mod; } if (a == n) { return k + max; } a = b; b = c; ++k; } return -1; } private static long fib(long k, long mod) { if (k == 0) return 0; if (k == 1 || k == 2) return 1; BigInteger modBig = BigInteger.valueOf(mod); BigInteger a2 = BigInteger.ONE; BigInteger a1 = BigInteger.ONE; BigInteger a0 = BigInteger.ZERO; BigInteger f1 = BigInteger.ONE; BigInteger f0 = BigInteger.ZERO; --k; while (k > 0) { if ((k & 1) == 1) { BigInteger f1a2 = f1.multiply(a2); BigInteger f0a1 = f0.multiply(a1); BigInteger f1a1 = f1.multiply(a1); BigInteger f0a0 = f0.multiply(a0); f1 = f1a2.add(f0a1).mod(modBig); f0 = f1a1.add(f0a0).mod(modBig); } BigInteger a2a2 = a2.pow(2); BigInteger a1a1 = a1.pow(2); BigInteger a0a0 = a0.pow(2); BigInteger a2a1 = a2.multiply(a1); BigInteger a1a0 = a1.multiply(a0); a2 = a2a2.add(a1a1).mod(modBig); a1 = a2a1.add(a1a0).mod(modBig); a0 = a1a1.add(a0a0).mod(modBig); k >>= 1; } return f1.longValue(); } private static ArrayList<Long> potencjalneLiczby(long n, long size) { if (size <= 3) { ArrayList<Long> wynik = new ArrayList<>(); int a = 0, b = 1, k = 0; int nInt = (int)(n % 1000); while (k < 1500) { int c = a + b; if (c >= 1000) { c -= 1000; } if (a == nInt) { wynik.add((long)k); } a = b; b = c; ++k; } return wynik; } ArrayList<Long> potencjalne = potencjalneLiczby(n, size - 1); if (potencjalne.isEmpty()) { return potencjalne; } long mod = 1; for (int i = 0; i < size; ++i) { mod *= 10; } long k_15 = 15; for (int i = 2; i < size; ++i) { k_15 *= 10; } long nMod = n % mod; ArrayList<Long> wynik = new ArrayList<>(); for(long k : potencjalne) { for (int i = 0; i < 10; ++i) { if (fib(k, mod) == nMod) { wynik.add(k); } k += k_15; } } return wynik; } private static long obliczDuzeN(long n, int size) { ArrayList<Long> wynik = potencjalneLiczby(n, size); if (wynik.isEmpty()) { return -1; } long k_15 = 15; for (int i = 1; i < size; ++i) { k_15 *= 10; } return wynik.get(0) + k_15; } public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); String nStr = input.readLine(); long nInt = Long.parseLong(nStr); int nSize = nStr.length(); long wynik = nSize <= 3 ? obliczMaleN((int) nInt, nSize) : obliczDuzeN(nInt, nSize); if (wynik == -1){ System.out.print("NIE"); } else { System.out.print(wynik); //System.out.print("TAK"); } } } |