結果

問題 No.3 ビットすごろく
ユーザー kohaku_kohakukohaku_kohaku
提出日時 2016-12-12 00:00:33
言語 Java21
(openjdk 21)
結果
AC  
実行時間 143 ms / 5,000 ms
コード長 704 bytes
コンパイル時間 2,161 ms
コンパイル使用メモリ 72,828 KB
実行使用メモリ 56,248 KB
最終ジャッジ日時 2023-09-14 00:14:31
合計ジャッジ時間 8,028 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
56,132 KB
testcase_01 AC 128 ms
56,012 KB
testcase_02 AC 129 ms
56,196 KB
testcase_03 AC 128 ms
55,564 KB
testcase_04 AC 130 ms
56,092 KB
testcase_05 AC 132 ms
56,044 KB
testcase_06 AC 132 ms
55,724 KB
testcase_07 AC 130 ms
56,092 KB
testcase_08 AC 131 ms
55,532 KB
testcase_09 AC 132 ms
56,148 KB
testcase_10 AC 137 ms
55,912 KB
testcase_11 AC 132 ms
56,048 KB
testcase_12 AC 133 ms
55,720 KB
testcase_13 AC 133 ms
56,056 KB
testcase_14 AC 137 ms
55,736 KB
testcase_15 AC 138 ms
55,752 KB
testcase_16 AC 139 ms
55,756 KB
testcase_17 AC 140 ms
56,008 KB
testcase_18 AC 132 ms
56,084 KB
testcase_19 AC 143 ms
55,928 KB
testcase_20 AC 130 ms
55,752 KB
testcase_21 AC 128 ms
55,568 KB
testcase_22 AC 138 ms
55,948 KB
testcase_23 AC 135 ms
55,812 KB
testcase_24 AC 138 ms
56,136 KB
testcase_25 AC 138 ms
56,036 KB
testcase_26 AC 128 ms
55,540 KB
testcase_27 AC 132 ms
55,768 KB
testcase_28 AC 138 ms
55,800 KB
testcase_29 AC 131 ms
55,796 KB
testcase_30 AC 130 ms
56,004 KB
testcase_31 AC 129 ms
56,248 KB
testcase_32 AC 133 ms
56,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int [] c = new int [N+1];
        c[1]=1;
        Queue<Integer> q = new LinkedList<>();
        q.add(1);
        while(!q.isEmpty()){
            int t = q.poll();
            int p = Integer.bitCount(t);
            int b = t-p;
            int f = t+p;
            if(b>0&&c[b]==0){
                c[b]=c[t]+1;
                q.add(b);
            }
            if(f<=N&&c[f]==0){
                c[f]=c[t]+1;
                q.add(f);
            }
        }
        if(c[N]==0)c[N]=-1;
        System.out.println(c[N]);
    }
}
0