import java.math.BigInteger; import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; /* class MatrixLong { public long m11,m12,m21,m22; static long MOD = 1000000000000000000L; static long mod(long m) { return m % MOD; } public Matrix(long m11,long m12,long m21,long m22) { this.m11 = m11; this.m12 = m12; this.m21 = m21; this.m22 = m22; } public static Matrix mul(Matrix A, Matrix B) { return new Matrix( mod(mod(A.m11 * B.m11) + mod(A.m12 * B.m21)), mod(mod(A.m11 * B.m12) + mod(A.m12 * B.m22)), mod(mod(A.m21 * B.m11) + mod(A.m22 * B.m21)), mod(mod(A.m21 * B.m12) + mod(A.m22 * B.m22)) ); } public static Matrix fpow(Matrix A, BigInteger n) { if (n.equals(BigInteger.ONE)) { return A; } if (n.equals(BigInteger.ZERO)) { return new Matrix(1, 0, 0, 1); } BigInteger[] dr = n.divideAndRemainder(BigInteger.valueOf(2)); Matrix B = fpow(A, dr[0]); B = mul(B,B); if (dr[1].equals(BigInteger.ONE)) { B = mul(B, A); } return B; } @Override public String toString() { return "[" + m11 + "," + m12 + "]\n[" + m21 + "," + m22 + "]"; } } */ class Matrix { public BigInteger m11,m12,m21,m22; static BigInteger MOD = BigInteger.valueOf(1000000000000000000L); static BigInteger mod(BigInteger m) { return m.mod(MOD); } public Matrix(BigInteger m11,BigInteger m12,BigInteger m21,BigInteger m22) { this.m11 = m11; this.m12 = m12; this.m21 = m21; this.m22 = m22; } public static Matrix mul(Matrix A, Matrix B) { return new Matrix( mod(mod(A.m11.multiply(B.m11)).add(mod(A.m12.multiply(B.m21)))), mod(mod(A.m11.multiply(B.m12)).add(mod(A.m12.multiply(B.m22)))), mod(mod(A.m21.multiply(B.m11)).add(mod(A.m22.multiply(B.m21)))), mod(mod(A.m21.multiply(B.m12)).add(mod(A.m22.multiply(B.m22)))) ); } public static Matrix fpow(Matrix A, BigInteger n) { if (n.equals(BigInteger.ONE)) { return A; } if (n.equals(BigInteger.ZERO)) { return new Matrix(BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE); } BigInteger[] dr = n.divideAndRemainder(BigInteger.valueOf(2)); Matrix B = fpow(A, dr[0]); B = mul(B,B); if (dr[1].equals(BigInteger.ONE)) { B = mul(B, A); } return B; } } class Candidate { public BigInteger value; public int level; public BigInteger src; public Candidate(BigInteger value, BigInteger src, int level) { this.value = value; this.src = src; this.level = level; } } public class fib { static Matrix FIB = new Matrix(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ZERO); // slow public static BigInteger fibn(BigInteger n) { if (n.equals(BigInteger.ZERO)) { return BigInteger.ZERO; } Matrix m = Matrix.fpow(FIB, n); return m.m12; } public static long nthBit(long l, int b) { for(int i = 0; i < b; i++) { l /= 10; } return l % 10; } public static int nthBit(String s, int b) { char c = s.charAt(s.length() - b - 1); return Integer.parseInt(Character.toString(c)); } public static void reverseArray(char[] arr) { for(int idx = 0; idx < arr.length / 2; idx++) { char t = arr[idx]; arr[idx] = arr[arr.length - idx - 1]; arr[arr.length - idx - 1] = t; } } public static boolean matchFullSuffix(String pattern, String candidate) { if (candidate.length() < pattern.length()) { return false; } return matchNChars(pattern, candidate, pattern.length()); } public static boolean matchNChars(String pattern, String candidate, int n) { char[] pat = pattern.toCharArray(); char[] can = candidate.toCharArray(); reverseArray(pat); reverseArray(can); for(int i = 0; i < n; i++) { if (pat[i] != can[i]) { return false; } } return true; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); long[] shifts = new long[18]; shifts[0] = 60; shifts[1] = 300; for(int i = 2; i < 18; i++) { String s = "1"; for(int j = 0; j < i; j++) { s += "0"; } shifts[i] = 15 * Long.valueOf(s); } String pattern = sc.nextLine(); int lowestBit = nthBit(pattern, 0); Queue<Candidate> candidates = new LinkedList<Candidate>(); for(int i = 0; i < 61; i++) { BigInteger f = fibn(BigInteger.valueOf(i)); String match = f.toString(); if(nthBit(match, 0) == lowestBit) { //System.out.println("Added match " + match + " for i=" + i); candidates.add(new Candidate(f, BigInteger.valueOf(i), 0)); } } while (!candidates.isEmpty()) { Candidate c = candidates.poll(); if (matchFullSuffix(pattern, c.value.toString())) { System.out.println(c.src.toString()); sc.close(); return; } if (c.level > 17) continue; BigInteger nextShift = BigInteger.valueOf(shifts[c.level + 1]); BigInteger currentShift = BigInteger.valueOf(shifts[c.level]); if (c.src.compareTo(nextShift) < 1) { c.src = c.src.add(currentShift); c.value = fibn(c.src); candidates.add(c); //System.out.println("Added match " + c.value.toString() + " for i=" + c.src.toString()); if(matchNChars(pattern, c.value.toString(), c.level + 1)) { Candidate d = new Candidate(c.value, c.src, c.level+1); candidates.add(d); } } else { c.level++; c.src = c.src.add(nextShift); c.value = fibn(c.src); if(matchNChars(pattern, c.value.toString(), c.level)) { candidates.add(c); //System.out.println("Added match " + c.value.toString() + " for i=" + c.src.toString()); } } } System.out.println("NIE"); sc.close(); } }
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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | import java.math.BigInteger; import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; /* class MatrixLong { public long m11,m12,m21,m22; static long MOD = 1000000000000000000L; static long mod(long m) { return m % MOD; } public Matrix(long m11,long m12,long m21,long m22) { this.m11 = m11; this.m12 = m12; this.m21 = m21; this.m22 = m22; } public static Matrix mul(Matrix A, Matrix B) { return new Matrix( mod(mod(A.m11 * B.m11) + mod(A.m12 * B.m21)), mod(mod(A.m11 * B.m12) + mod(A.m12 * B.m22)), mod(mod(A.m21 * B.m11) + mod(A.m22 * B.m21)), mod(mod(A.m21 * B.m12) + mod(A.m22 * B.m22)) ); } public static Matrix fpow(Matrix A, BigInteger n) { if (n.equals(BigInteger.ONE)) { return A; } if (n.equals(BigInteger.ZERO)) { return new Matrix(1, 0, 0, 1); } BigInteger[] dr = n.divideAndRemainder(BigInteger.valueOf(2)); Matrix B = fpow(A, dr[0]); B = mul(B,B); if (dr[1].equals(BigInteger.ONE)) { B = mul(B, A); } return B; } @Override public String toString() { return "[" + m11 + "," + m12 + "]\n[" + m21 + "," + m22 + "]"; } } */ class Matrix { public BigInteger m11,m12,m21,m22; static BigInteger MOD = BigInteger.valueOf(1000000000000000000L); static BigInteger mod(BigInteger m) { return m.mod(MOD); } public Matrix(BigInteger m11,BigInteger m12,BigInteger m21,BigInteger m22) { this.m11 = m11; this.m12 = m12; this.m21 = m21; this.m22 = m22; } public static Matrix mul(Matrix A, Matrix B) { return new Matrix( mod(mod(A.m11.multiply(B.m11)).add(mod(A.m12.multiply(B.m21)))), mod(mod(A.m11.multiply(B.m12)).add(mod(A.m12.multiply(B.m22)))), mod(mod(A.m21.multiply(B.m11)).add(mod(A.m22.multiply(B.m21)))), mod(mod(A.m21.multiply(B.m12)).add(mod(A.m22.multiply(B.m22)))) ); } public static Matrix fpow(Matrix A, BigInteger n) { if (n.equals(BigInteger.ONE)) { return A; } if (n.equals(BigInteger.ZERO)) { return new Matrix(BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE); } BigInteger[] dr = n.divideAndRemainder(BigInteger.valueOf(2)); Matrix B = fpow(A, dr[0]); B = mul(B,B); if (dr[1].equals(BigInteger.ONE)) { B = mul(B, A); } return B; } } class Candidate { public BigInteger value; public int level; public BigInteger src; public Candidate(BigInteger value, BigInteger src, int level) { this.value = value; this.src = src; this.level = level; } } public class fib { static Matrix FIB = new Matrix(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ZERO); // slow public static BigInteger fibn(BigInteger n) { if (n.equals(BigInteger.ZERO)) { return BigInteger.ZERO; } Matrix m = Matrix.fpow(FIB, n); return m.m12; } public static long nthBit(long l, int b) { for(int i = 0; i < b; i++) { l /= 10; } return l % 10; } public static int nthBit(String s, int b) { char c = s.charAt(s.length() - b - 1); return Integer.parseInt(Character.toString(c)); } public static void reverseArray(char[] arr) { for(int idx = 0; idx < arr.length / 2; idx++) { char t = arr[idx]; arr[idx] = arr[arr.length - idx - 1]; arr[arr.length - idx - 1] = t; } } public static boolean matchFullSuffix(String pattern, String candidate) { if (candidate.length() < pattern.length()) { return false; } return matchNChars(pattern, candidate, pattern.length()); } public static boolean matchNChars(String pattern, String candidate, int n) { char[] pat = pattern.toCharArray(); char[] can = candidate.toCharArray(); reverseArray(pat); reverseArray(can); for(int i = 0; i < n; i++) { if (pat[i] != can[i]) { return false; } } return true; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); long[] shifts = new long[18]; shifts[0] = 60; shifts[1] = 300; for(int i = 2; i < 18; i++) { String s = "1"; for(int j = 0; j < i; j++) { s += "0"; } shifts[i] = 15 * Long.valueOf(s); } String pattern = sc.nextLine(); int lowestBit = nthBit(pattern, 0); Queue<Candidate> candidates = new LinkedList<Candidate>(); for(int i = 0; i < 61; i++) { BigInteger f = fibn(BigInteger.valueOf(i)); String match = f.toString(); if(nthBit(match, 0) == lowestBit) { //System.out.println("Added match " + match + " for i=" + i); candidates.add(new Candidate(f, BigInteger.valueOf(i), 0)); } } while (!candidates.isEmpty()) { Candidate c = candidates.poll(); if (matchFullSuffix(pattern, c.value.toString())) { System.out.println(c.src.toString()); sc.close(); return; } if (c.level > 17) continue; BigInteger nextShift = BigInteger.valueOf(shifts[c.level + 1]); BigInteger currentShift = BigInteger.valueOf(shifts[c.level]); if (c.src.compareTo(nextShift) < 1) { c.src = c.src.add(currentShift); c.value = fibn(c.src); candidates.add(c); //System.out.println("Added match " + c.value.toString() + " for i=" + c.src.toString()); if(matchNChars(pattern, c.value.toString(), c.level + 1)) { Candidate d = new Candidate(c.value, c.src, c.level+1); candidates.add(d); } } else { c.level++; c.src = c.src.add(nextShift); c.value = fibn(c.src); if(matchNChars(pattern, c.value.toString(), c.level)) { candidates.add(c); //System.out.println("Added match " + c.value.toString() + " for i=" + c.src.toString()); } } } System.out.println("NIE"); sc.close(); } } |