結果
問題 | No.1152 10億ゲーム |
ユーザー |
|
提出日時 | 2020-07-09 04:49:45 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 575 ms / 2,000 ms |
コード長 | 4,438 bytes |
コンパイル時間 | 2,439 ms |
コンパイル使用メモリ | 82,876 KB |
実行使用メモリ | 79,900 KB |
平均クエリ数 | 23.14 |
最終ジャッジ日時 | 2024-07-17 03:59:50 |
合計ジャッジ時間 | 34,079 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 50 |
ソースコード
import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.HashMap;import java.util.List;import java.util.NoSuchElementException;class FastScanner {private final InputStream in = System.in;private final byte[] buffer = new byte[1024];private int ptr = 0;private int buflen = 0;private boolean hasNextByte() {if (ptr < buflen) {return true;}else{ptr = 0;try {buflen = in.read(buffer);} catch (IOException e) {e.printStackTrace();}if (buflen <= 0) {return false;}}return true;}private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}public boolean hasNext() { skipUnprintable(); return hasNextByte();}public String next() {if (!hasNext()) throw new NoSuchElementException();StringBuilder sb = new StringBuilder();int b = readByte();while(isPrintableChar(b)) {sb.appendCodePoint(b);b = readByte();}return sb.toString();}public long nextLong() {if (!hasNext()) throw new NoSuchElementException();long n = 0;boolean minus = false;int b = readByte();if (b == '-') {minus = true;b = readByte();}if (b < '0' || '9' < b) {throw new NumberFormatException();}while(true){if ('0' <= b && b <= '9') {n *= 10;n += b - '0';}else if(b == -1 || !isPrintableChar(b)){return minus ? -n : n;}else{throw new NumberFormatException();}b = readByte();}}public int nextInt() {return (int)nextLong();}}public class Main {public static void main(String[] args) {new Main().run();}HashMap<List<Integer>,Integer> dp=new HashMap<>();final int INF=Integer.MAX_VALUE/3;final long M=(long)1e9;ArrayList<Integer> divs=new ArrayList<>();{for (int i=0;i<=9;++i) {for (int j=0;j<=9;++j) {int d=(int)Math.round(Math.pow(2, i)*Math.pow(5, j));divs.add(d);}}Collections.sort(divs);for (int d:divs) dp.put(List.of(d,d), 0);}int g(int x,int i) {if (i==0&&x%2==0) return x/2;else if (i==1&&x%5==0) return x/5;else if (i==2) return (int)Math.min((long)2*x, M);else if (i==3) return (int)Math.min((long)5*x, M);else return 0;}void build() {for (int t=0;t<100;++t) {for (int x0:divs) {in:for (int x1:divs) {if (dp.containsKey(List.of(x0,x1))) continue;for (int i=0;i<4;++i) {long nx0=g(x0,i);if (nx0==x0 || nx0==0 || M%nx0!=0) continue;if (nx0==x1) {dp.put(List.of(x0,x1), 1);continue in;}}int ret=INF;in2:for (int i=0;i<4;++i) {int nx0=g(x0,i);if (nx0==x0 || nx0==0 || M%nx0!=0) continue;int tmp=0;for (int j=0;j<4;++j) {int nx1=g(x1,j);if (nx1==x1 || nx1==0 || M%nx1!=0) continue;if (!dp.containsKey(List.of(nx0,nx1))) {continue in2;}tmp=Math.max(tmp, 1+dp.get(List.of(nx0,nx1)));}ret=Math.min(ret, tmp);}if (ret!=INF) dp.put(List.of(x0,x1), ret);}}}}void run() {FastScanner sc=new FastScanner();build();int x0=sc.nextInt();while (true) {int x1=sc.nextInt();if (x0==x1) return;int cur=dp.get(List.of(x0,x1));for (int i=0;i<4;++i) {int nx0=g(x0,i);if (nx0==x0 || nx0==0 || M%nx0!=0) continue;if (x1==nx0) {System.out.println(nx0);return;}int next=0;for (int j=0;j<4;++j) {int nx1=g(x1,j);if (nx1==x1 || nx1==0 || M%nx1!=0) continue;next=Math.max(next,dp.get(List.of(nx0,nx1)));}if (next==cur-1) {x0=nx0;System.out.println(x0);if (x0==x1) return;break;}}}}void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));}}