結果
問題 |
No.3 ビットすごろく
|
ユーザー |
|
提出日時 | 2014-11-18 22:47:28 |
言語 | Java (openjdk 23) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,228 bytes |
コンパイル時間 | 4,117 ms |
コンパイル使用メモリ | 79,876 KB |
実行使用メモリ | 53,980 KB |
最終ジャッジ日時 | 2025-01-02 17:38:15 |
合計ジャッジ時間 | 7,414 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 14 WA * 19 |
ソースコード
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class Yukicoder3 { static int N; static int[] bitCount = new int[10001]; static boolean[] memo = new boolean[10001]; static int count = 0; static int min = Integer.MAX_VALUE; static boolean found = false; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); N = Integer.parseInt(br.readLine()); String buf; Arrays.fill(bitCount, 0); for (int i = 1; i <= 10000; i++) { buf = Integer.toBinaryString(i); for (int j = 0; j < buf.length(); j++) { if (buf.charAt(j) == '1') bitCount[i]++; } } // search Arrays.fill(memo, false); count = 1; search(1); if (found) System.out.println(min); else System.out.println(-1); } private static void search(int n) { if (n < 1 || n > N || memo[n]) { count--; return; } memo[n] = true; if (n == N) { count--; found = true; min = Math.min(min, count); return; } count++; search(n + bitCount[n]); count++; search(n - bitCount[n]); } }