import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.StringTokenizer; public class fib { public static void main(String[] args) throws IOException { fib solver = new fib(); solver.init(); solver.solve(); } private void init() { } private static class Loop { List<Long> idxs = new LinkedList<Long>(); long length; public Loop(List<Long> idxs, long length) { super(); this.idxs = idxs; this.length = length; } } private static class DigitPair { final int d1; final int d2; public DigitPair(long d1, long d2) { super(); this.d1 = (int)d1; this.d2 = (int)d2; } @Override public boolean equals(Object obj) { DigitPair dp = (DigitPair)obj; return d1 * 10 + d2 == dp.d1 * 10 + dp.d2; } @Override public int hashCode() { return d1 * 10 + d2; } } private void solve() throws IOException { Reader in = new Reader(System.in); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); char[] number = in.next().toCharArray();//in.next() Loop loop = new Loop(Arrays.asList(0L), 1); long digit = 1; for (int i = number.length - 1; i >= 0; i--) { digit *= 10; loop = nextDigit(loop, digit, number[i] - '0'); if (loop.idxs.isEmpty()) { break; } } if (loop.idxs.isEmpty()) { out.println("NIE"); } else { out.println(loop.idxs.get(0)); } out.flush(); out.close(); } public Loop nextDigit(Loop curr, long modMask , int d) { Set<DigitPair> cycle = new HashSet<DigitPair>(); List<Long> indxes = new LinkedList<Long>(); long len = 0; for (int i = 0; i <= 100; i++) { Iterator<Long> it = curr.idxs.iterator(); long idx = it.next() + i * curr.length; long[] fibs = countFib(idx); DigitPair dp = new DigitPair((fibs[0] % modMask)/(modMask/10), (fibs[1] % modMask)/(modMask/10)); if (!cycle.add(dp)) { len = i * curr.length - 1; break; } else { if ((fibs[0] % modMask)/(modMask/10) == d) { indxes.add(idx); } for (; it.hasNext();) { idx = it.next() + i * curr.length; if ((countFib(idx)[0] % modMask)/(modMask/10) == d) { indxes.add(idx); } } } } return new Loop(indxes, len); } //zwraca n i n-1 liczbe fibonacciego public long[] countFib(long n) { if (n <= 0) return new long[]{0,0}; else { long[] fib = pow(new long[][]{{0,1},{1,1}}, n)[0]; return new long[]{fib[1],fib[0]}; } } private long[][] pow(long[][] a, long n) { if (n == 1) return a; long[][] a2 = mul(a,a); if ((n & 1) == 0) { return pow(a2, n >> 1); } else { return mul(a, pow(a2, n >> 1)); } } final static long MOD = 1000000000000000000L; private long[][] mul(long[][] a, long[][] b) { long[][] r = new long[2][2]; r[0][0] = BigInteger.valueOf(a[0][0]).multiply(BigInteger.valueOf(b[0][0])).longValue() + BigInteger.valueOf(a[0][1]).multiply(BigInteger.valueOf(b[1][0])).longValue(); if (r[0][0] >= MOD) r[0][0] -= MOD; r[0][1] = BigInteger.valueOf(a[0][0]).multiply(BigInteger.valueOf(b[0][1])).longValue() + BigInteger.valueOf(a[0][1]).multiply(BigInteger.valueOf(b[1][1])).longValue(); if (r[0][1] >= MOD) r[0][1] -= MOD; r[1][0] = BigInteger.valueOf(a[1][0]).multiply(BigInteger.valueOf(b[0][0])).longValue() + BigInteger.valueOf(a[1][1]).multiply(BigInteger.valueOf(b[1][0])).longValue(); if (r[1][0] >= MOD) r[1][0] -= MOD; r[1][1] = BigInteger.valueOf(a[1][0]).multiply(BigInteger.valueOf(b[0][1])).longValue() + BigInteger.valueOf(a[1][1]).multiply(BigInteger.valueOf(b[1][1])).longValue(); if (r[1][1] >= MOD) r[1][1] -= MOD; return r; } private static class Reader { BufferedReader reader; StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ Reader(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } public void skipLine() throws IOException { reader.readLine(); } /** get next word */ public String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt( next() ); } public double nextDouble() throws IOException { return Double.parseDouble( next() ); } public long nextLong() throws IOException { return Long.parseLong(next()); } } }
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 | import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.StringTokenizer; public class fib { public static void main(String[] args) throws IOException { fib solver = new fib(); solver.init(); solver.solve(); } private void init() { } private static class Loop { List<Long> idxs = new LinkedList<Long>(); long length; public Loop(List<Long> idxs, long length) { super(); this.idxs = idxs; this.length = length; } } private static class DigitPair { final int d1; final int d2; public DigitPair(long d1, long d2) { super(); this.d1 = (int)d1; this.d2 = (int)d2; } @Override public boolean equals(Object obj) { DigitPair dp = (DigitPair)obj; return d1 * 10 + d2 == dp.d1 * 10 + dp.d2; } @Override public int hashCode() { return d1 * 10 + d2; } } private void solve() throws IOException { Reader in = new Reader(System.in); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); char[] number = in.next().toCharArray();//in.next() Loop loop = new Loop(Arrays.asList(0L), 1); long digit = 1; for (int i = number.length - 1; i >= 0; i--) { digit *= 10; loop = nextDigit(loop, digit, number[i] - '0'); if (loop.idxs.isEmpty()) { break; } } if (loop.idxs.isEmpty()) { out.println("NIE"); } else { out.println(loop.idxs.get(0)); } out.flush(); out.close(); } public Loop nextDigit(Loop curr, long modMask , int d) { Set<DigitPair> cycle = new HashSet<DigitPair>(); List<Long> indxes = new LinkedList<Long>(); long len = 0; for (int i = 0; i <= 100; i++) { Iterator<Long> it = curr.idxs.iterator(); long idx = it.next() + i * curr.length; long[] fibs = countFib(idx); DigitPair dp = new DigitPair((fibs[0] % modMask)/(modMask/10), (fibs[1] % modMask)/(modMask/10)); if (!cycle.add(dp)) { len = i * curr.length - 1; break; } else { if ((fibs[0] % modMask)/(modMask/10) == d) { indxes.add(idx); } for (; it.hasNext();) { idx = it.next() + i * curr.length; if ((countFib(idx)[0] % modMask)/(modMask/10) == d) { indxes.add(idx); } } } } return new Loop(indxes, len); } //zwraca n i n-1 liczbe fibonacciego public long[] countFib(long n) { if (n <= 0) return new long[]{0,0}; else { long[] fib = pow(new long[][]{{0,1},{1,1}}, n)[0]; return new long[]{fib[1],fib[0]}; } } private long[][] pow(long[][] a, long n) { if (n == 1) return a; long[][] a2 = mul(a,a); if ((n & 1) == 0) { return pow(a2, n >> 1); } else { return mul(a, pow(a2, n >> 1)); } } final static long MOD = 1000000000000000000L; private long[][] mul(long[][] a, long[][] b) { long[][] r = new long[2][2]; r[0][0] = BigInteger.valueOf(a[0][0]).multiply(BigInteger.valueOf(b[0][0])).longValue() + BigInteger.valueOf(a[0][1]).multiply(BigInteger.valueOf(b[1][0])).longValue(); if (r[0][0] >= MOD) r[0][0] -= MOD; r[0][1] = BigInteger.valueOf(a[0][0]).multiply(BigInteger.valueOf(b[0][1])).longValue() + BigInteger.valueOf(a[0][1]).multiply(BigInteger.valueOf(b[1][1])).longValue(); if (r[0][1] >= MOD) r[0][1] -= MOD; r[1][0] = BigInteger.valueOf(a[1][0]).multiply(BigInteger.valueOf(b[0][0])).longValue() + BigInteger.valueOf(a[1][1]).multiply(BigInteger.valueOf(b[1][0])).longValue(); if (r[1][0] >= MOD) r[1][0] -= MOD; r[1][1] = BigInteger.valueOf(a[1][0]).multiply(BigInteger.valueOf(b[0][1])).longValue() + BigInteger.valueOf(a[1][1]).multiply(BigInteger.valueOf(b[1][1])).longValue(); if (r[1][1] >= MOD) r[1][1] -= MOD; return r; } private static class Reader { BufferedReader reader; StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ Reader(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } public void skipLine() throws IOException { reader.readLine(); } /** get next word */ public String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt( next() ); } public double nextDouble() throws IOException { return Double.parseDouble( next() ); } public long nextLong() throws IOException { return Long.parseLong(next()); } } } |