結果

問題 No.3 ビットすごろく
ユーザー fushime2fushime2
提出日時 2016-09-15 13:17:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 456 ms / 5,000 ms
コード長 1,026 bytes
コンパイル時間 1,797 ms
コンパイル使用メモリ 145,224 KB
実行使用メモリ 394,472 KB
最終ジャッジ日時 2023-09-14 00:08:12
合計ジャッジ時間 8,660 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 42 ms
94,348 KB
testcase_04 AC 9 ms
28,348 KB
testcase_05 AC 149 ms
199,044 KB
testcase_06 AC 49 ms
104,516 KB
testcase_07 AC 22 ms
61,216 KB
testcase_08 AC 99 ms
166,316 KB
testcase_09 AC 229 ms
251,844 KB
testcase_10 AC 310 ms
312,284 KB
testcase_11 AC 194 ms
228,808 KB
testcase_12 AC 139 ms
192,540 KB
testcase_13 AC 32 ms
81,984 KB
testcase_14 AC 289 ms
299,232 KB
testcase_15 AC 428 ms
389,892 KB
testcase_16 AC 370 ms
362,236 KB
testcase_17 AC 420 ms
384,060 KB
testcase_18 AC 26 ms
69,460 KB
testcase_19 AC 436 ms
394,472 KB
testcase_20 AC 6 ms
22,220 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 296 ms
314,520 KB
testcase_23 AC 440 ms
394,376 KB
testcase_24 AC 456 ms
394,460 KB
testcase_25 AC 431 ms
389,984 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 36 ms
88,124 KB
testcase_28 AC 365 ms
354,044 KB
testcase_29 AC 200 ms
254,944 KB
testcase_30 AC 2 ms
7,812 KB
testcase_31 AC 4 ms
9,844 KB
testcase_32 AC 173 ms
234,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(int)(b);i++)
#define rep(i,n) FOR(i,0,n)
#define ZERO(a) memset(a,0,sizeof(a))

const int INF = 114514810;
int cost[10010][10010];
bool used[10010];
int d[10010];
int n;

int calc(int n) {
    int res = 0;
    while(n) {
        if(n & 1) res++;
        n >>= 1;
    }
    return res;
}

void dijkstra(int s) {
    ZERO(used);
    fill(d,d+n+1,INF);
    d[s] = 0;
    while(1) {
        int v = -1;
        FOR(u,1,n+1) {
            if(!used[u] and (v == -1 or d[u] < d[v])) v = u;
        }
        if(v == -1) break;
        used[v] = 1;
        FOR(u,1,n+1) d[u] = min(d[u], d[v] + cost[v][u]);
    }
}

int main() {
    scanf("%d",&n);
    rep(i,n+1) rep(j,n+1) cost[i][j] = INF;
    vector<int> a(n+1);
    rep(i,n+1) a[i] = calc(i);

    FOR(i,1,n+1) {
        if(i + a[i] <= n) cost[i][i+a[i]] = 1;
        if(i - a[i] >= 1) cost[i][i-a[i]] = 1;
    }

    dijkstra(1);
    cout << (d[n]>=INF ? -1 : d[n] + 1) << "\n";

    return 0;
}
0