import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; public class fib { public static class City { public int neighnum = 0; public ArrayList<City> neighbours = new ArrayList<>(); boolean alive = true; // int citynumber; // public City(int num) { // citynumber = num; // } } public static class CityComp implements Comparator<City> { @Override public int compare(City str1, City str2) { return str1.neighnum > str2.neighnum ? 1 : str1.neighnum == str2.neighnum ? 0 : -1; } } // public static BigInteger getNthfibo(BigInteger n) { // BigInteger[][] result = {{1, 0}, {0, 1}}; // identity matrix. // BigInteger[][] fiboM = {{1, 1}, {1, 0}}; // // while (n.compareTo(BigInteger.ZERO) == 1) { // if (n.mod(BigInteger.TWO) == 1) { // multMatrix(result, fiboM); // } // n = n / 2; // multMatrix(fiboM, fiboM); // } // // return result[1][0]; // } // // private static void multMatrix(BigInteger[][] m, BigInteger [][] n) { // BigInteger a = m[0][0] * n[0][0] + m[0][1] * n[1][0]; // BigInteger b = m[0][0] * n[0][1] + m[0][1] * n[1][1]; // BigInteger c = m[1][0] * n[0][0] + m[1][1] * n[0][1]; // BigInteger d = m[1][0] * n[0][1] + m[1][1] * n[1][1]; // // m[0][0] = a; // m[0][1] = b; // m[1][0] = c; // m[1][1] = d; // } // Multiplies two BigIntegers. This function makes it easy to swap in a faster algorithm like Karatsuba multiplication. private static BigInteger multiply(BigInteger x, BigInteger y) { return x.multiply(y); } //Z NETA private static BigInteger fastFibonacciMatrix(BigInteger n) { BigInteger[] matrix = {BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ZERO}; return matrixPow(matrix, n)[1]; } static BigInteger MAXBIG = new BigInteger("1000000000000000000"); static BigInteger BTWO = BigInteger.ONE.add(BigInteger.ONE); // Computes the power of a matrix. The matrix is packed in row-major order. private static BigInteger[] matrixPow(BigInteger[] matrix, BigInteger n) { BigInteger[] result = {BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE}; while (n.compareTo(BigInteger.ZERO) != 0) { // Exponentiation by squaring if (n.mod(BTWO).compareTo(BigInteger.ZERO) != 0) result = matrixMultiply(result, matrix); n = n.divide(BTWO); //n /= 2; matrix = matrixMultiply(matrix, matrix); } return result; } // Multiplies two matrices. private static BigInteger[] matrixMultiply(BigInteger[] x, BigInteger[] y) { return new BigInteger[] { multiply(x[0], y[0]).add(multiply(x[1], y[2])).mod(MAXBIG), multiply(x[0], y[1]).add(multiply(x[1], y[3])).mod(MAXBIG), multiply(x[2], y[0]).add(multiply(x[3], y[2])).mod(MAXBIG), multiply(x[2], y[1]).add(multiply(x[3], y[3])).mod(MAXBIG) }; } public static String padLeft(String s, int n) { return String.format("%1$" + n + "s", s).replace(' ','0'); } public static void main(String[] args) throws Throwable { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); // StringTokenizer tokenizer = new StringTokenizer(in.readLine()); // // for(int i=1;i<20;++i) { // //BigInteger newb = new BigInteger(i); // System.out.print(fastFibonacciMatrix(BigInteger.TEN.pow(i)).toString() + "\n"); // } //for(int hh = 0;hh<36;++hh) { String inputStr = in.readLine(); BigInteger input = new BigInteger(inputStr); int numlen = inputStr.length(); String digits = padLeft(inputStr, 18); //System.out.print(digits); BigInteger previousperiod = BigInteger.ONE; BigInteger period = new BigInteger("60"); ArrayList<BigInteger> options = new ArrayList<>(); //ArrayList<BigInteger> preoptions = new ArrayList<>(); options.add(BigInteger.ZERO); for (int pos = 0; pos < numlen; pos += 1) { char currentDigit = digits.charAt(17 - pos); ArrayList<BigInteger> nextoptions = new ArrayList<>(); for (BigInteger opt : options) { BigInteger pom = new BigInteger(opt.toString()); for (; pom.compareTo(period) == -1; pom = pom.add(previousperiod)) { //String wat =padLeft(fastFibonacciMatrix(pom).toString(),18); // watwat = wat.charAt(17-pos); String watwat = fastFibonacciMatrix(pom).toString(); int watlen = watwat.length(); if(watlen < numlen) { watlen=numlen; } // if(pom.compareTo(BigInteger.ZERO)) { // // } if (padLeft(watwat, numlen).charAt(watlen - 1 - pos) == currentDigit) { //if(padLeft(fastFibonacciMatrix(pom).toString(),18).charAt(17-pos) == currentDigit) { // System.out.println(watlen + " " + pos + " " + pom.toString() + " " + currentDigit + " " + numlen + " " + padLeft(watwat, numlen)); nextoptions.add(pom); } } } //preoptions = options; options = nextoptions; previousperiod = period; BigInteger multiplier = new BigInteger("5"); if (pos > 1) { multiplier = multiplier.multiply(BTWO); } previousperiod = period; period = period.multiply(multiplier); } if (options.size() == 0) { System.out.println("NIE"); } else { //System.out.println("TAK"); System.out.println(options.get(0).toString()); //System.out.println(fastFibonacciMatrix(options.get(0))); } //} // int n = Integer.parseInt(tokenizer.nextToken()); // int m = Integer.parseInt(tokenizer.nextToken()); // int d = Integer.parseInt(tokenizer.nextToken()); // // ArrayList<City> cities = new ArrayList<>(); // for(int i=0;i<n;++i) { // City lol = new City(); // cities.add(lol); // } // // // int ac, bc; // for(int i =0;i<m;++i) { // tokenizer = new StringTokenizer(in.readLine()); // ac = Integer.parseInt(tokenizer.nextToken()); // bc = Integer.parseInt(tokenizer.nextToken()); // --ac; // --bc; // cities.get(ac).neighbours.add(cities.get(bc)); // cities.get(bc).neighbours.add(cities.get(ac)); // cities.get(ac).neighnum++; // cities.get(bc).neighnum++; // } // TreeSet<City> cityset = new TreeSet<>(new CityComp()); // cityset.addAll(cities); // // while(!cityset.isEmpty()) { // City testcity = cityset.pollFirst(); // if(! testcity.alive) // continue; // if(testcity.neighnum < d) { // testcity.alive =false; // for(City ziomek : testcity.neighbours) { // if(!ziomek.alive) // continue; // cityset.remove(ziomek); // ziomek.neighnum--; // cityset.add(ziomek); // } // } // } // // boolean fail = true; // int citynum = 0; // for(int i =0; i<n;++i) { // City elo = cities.get(i); // if(elo.alive) { // citynum++; // } // } // // if(citynum==0) { // out.print("NIE"); // } else { // out.print(citynum+"\n"); // for(int i =0; i<n;++i) { // City elo = cities.get(i); // if(elo.alive) { // out.print(i+1 + " "); // } // } // // } // // // // // // // in.close(); // out.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 | import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; public class fib { public static class City { public int neighnum = 0; public ArrayList<City> neighbours = new ArrayList<>(); boolean alive = true; // int citynumber; // public City(int num) { // citynumber = num; // } } public static class CityComp implements Comparator<City> { @Override public int compare(City str1, City str2) { return str1.neighnum > str2.neighnum ? 1 : str1.neighnum == str2.neighnum ? 0 : -1; } } // public static BigInteger getNthfibo(BigInteger n) { // BigInteger[][] result = {{1, 0}, {0, 1}}; // identity matrix. // BigInteger[][] fiboM = {{1, 1}, {1, 0}}; // // while (n.compareTo(BigInteger.ZERO) == 1) { // if (n.mod(BigInteger.TWO) == 1) { // multMatrix(result, fiboM); // } // n = n / 2; // multMatrix(fiboM, fiboM); // } // // return result[1][0]; // } // // private static void multMatrix(BigInteger[][] m, BigInteger [][] n) { // BigInteger a = m[0][0] * n[0][0] + m[0][1] * n[1][0]; // BigInteger b = m[0][0] * n[0][1] + m[0][1] * n[1][1]; // BigInteger c = m[1][0] * n[0][0] + m[1][1] * n[0][1]; // BigInteger d = m[1][0] * n[0][1] + m[1][1] * n[1][1]; // // m[0][0] = a; // m[0][1] = b; // m[1][0] = c; // m[1][1] = d; // } // Multiplies two BigIntegers. This function makes it easy to swap in a faster algorithm like Karatsuba multiplication. private static BigInteger multiply(BigInteger x, BigInteger y) { return x.multiply(y); } //Z NETA private static BigInteger fastFibonacciMatrix(BigInteger n) { BigInteger[] matrix = {BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ZERO}; return matrixPow(matrix, n)[1]; } static BigInteger MAXBIG = new BigInteger("1000000000000000000"); static BigInteger BTWO = BigInteger.ONE.add(BigInteger.ONE); // Computes the power of a matrix. The matrix is packed in row-major order. private static BigInteger[] matrixPow(BigInteger[] matrix, BigInteger n) { BigInteger[] result = {BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE}; while (n.compareTo(BigInteger.ZERO) != 0) { // Exponentiation by squaring if (n.mod(BTWO).compareTo(BigInteger.ZERO) != 0) result = matrixMultiply(result, matrix); n = n.divide(BTWO); //n /= 2; matrix = matrixMultiply(matrix, matrix); } return result; } // Multiplies two matrices. private static BigInteger[] matrixMultiply(BigInteger[] x, BigInteger[] y) { return new BigInteger[] { multiply(x[0], y[0]).add(multiply(x[1], y[2])).mod(MAXBIG), multiply(x[0], y[1]).add(multiply(x[1], y[3])).mod(MAXBIG), multiply(x[2], y[0]).add(multiply(x[3], y[2])).mod(MAXBIG), multiply(x[2], y[1]).add(multiply(x[3], y[3])).mod(MAXBIG) }; } public static String padLeft(String s, int n) { return String.format("%1$" + n + "s", s).replace(' ','0'); } public static void main(String[] args) throws Throwable { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); // StringTokenizer tokenizer = new StringTokenizer(in.readLine()); // // for(int i=1;i<20;++i) { // //BigInteger newb = new BigInteger(i); // System.out.print(fastFibonacciMatrix(BigInteger.TEN.pow(i)).toString() + "\n"); // } //for(int hh = 0;hh<36;++hh) { String inputStr = in.readLine(); BigInteger input = new BigInteger(inputStr); int numlen = inputStr.length(); String digits = padLeft(inputStr, 18); //System.out.print(digits); BigInteger previousperiod = BigInteger.ONE; BigInteger period = new BigInteger("60"); ArrayList<BigInteger> options = new ArrayList<>(); //ArrayList<BigInteger> preoptions = new ArrayList<>(); options.add(BigInteger.ZERO); for (int pos = 0; pos < numlen; pos += 1) { char currentDigit = digits.charAt(17 - pos); ArrayList<BigInteger> nextoptions = new ArrayList<>(); for (BigInteger opt : options) { BigInteger pom = new BigInteger(opt.toString()); for (; pom.compareTo(period) == -1; pom = pom.add(previousperiod)) { //String wat =padLeft(fastFibonacciMatrix(pom).toString(),18); // watwat = wat.charAt(17-pos); String watwat = fastFibonacciMatrix(pom).toString(); int watlen = watwat.length(); if(watlen < numlen) { watlen=numlen; } // if(pom.compareTo(BigInteger.ZERO)) { // // } if (padLeft(watwat, numlen).charAt(watlen - 1 - pos) == currentDigit) { //if(padLeft(fastFibonacciMatrix(pom).toString(),18).charAt(17-pos) == currentDigit) { // System.out.println(watlen + " " + pos + " " + pom.toString() + " " + currentDigit + " " + numlen + " " + padLeft(watwat, numlen)); nextoptions.add(pom); } } } //preoptions = options; options = nextoptions; previousperiod = period; BigInteger multiplier = new BigInteger("5"); if (pos > 1) { multiplier = multiplier.multiply(BTWO); } previousperiod = period; period = period.multiply(multiplier); } if (options.size() == 0) { System.out.println("NIE"); } else { //System.out.println("TAK"); System.out.println(options.get(0).toString()); //System.out.println(fastFibonacciMatrix(options.get(0))); } //} // int n = Integer.parseInt(tokenizer.nextToken()); // int m = Integer.parseInt(tokenizer.nextToken()); // int d = Integer.parseInt(tokenizer.nextToken()); // // ArrayList<City> cities = new ArrayList<>(); // for(int i=0;i<n;++i) { // City lol = new City(); // cities.add(lol); // } // // // int ac, bc; // for(int i =0;i<m;++i) { // tokenizer = new StringTokenizer(in.readLine()); // ac = Integer.parseInt(tokenizer.nextToken()); // bc = Integer.parseInt(tokenizer.nextToken()); // --ac; // --bc; // cities.get(ac).neighbours.add(cities.get(bc)); // cities.get(bc).neighbours.add(cities.get(ac)); // cities.get(ac).neighnum++; // cities.get(bc).neighnum++; // } // TreeSet<City> cityset = new TreeSet<>(new CityComp()); // cityset.addAll(cities); // // while(!cityset.isEmpty()) { // City testcity = cityset.pollFirst(); // if(! testcity.alive) // continue; // if(testcity.neighnum < d) { // testcity.alive =false; // for(City ziomek : testcity.neighbours) { // if(!ziomek.alive) // continue; // cityset.remove(ziomek); // ziomek.neighnum--; // cityset.add(ziomek); // } // } // } // // boolean fail = true; // int citynum = 0; // for(int i =0; i<n;++i) { // City elo = cities.get(i); // if(elo.alive) { // citynum++; // } // } // // if(citynum==0) { // out.print("NIE"); // } else { // out.print(citynum+"\n"); // for(int i =0; i<n;++i) { // City elo = cities.get(i); // if(elo.alive) { // out.print(i+1 + " "); // } // } // // } // // // // // // // in.close(); // out.close(); } } |