結果

問題 No.3 ビットすごろく
ユーザー fushime2fushime2
提出日時 2016-09-23 17:09:55
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 360 ms / 5,000 ms
コード長 999 bytes
コンパイル時間 1,256 ms
コンパイル使用メモリ 148,932 KB
実行使用メモリ 6,088 KB
最終ジャッジ日時 2023-09-14 00:09:03
合計ジャッジ時間 5,557 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 11 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 53 ms
4,380 KB
testcase_10 AC 140 ms
4,376 KB
testcase_11 AC 34 ms
4,376 KB
testcase_12 AC 9 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 118 ms
4,376 KB
testcase_15 AC 310 ms
5,108 KB
testcase_16 AC 179 ms
4,376 KB
testcase_17 AC 276 ms
5,496 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 357 ms
5,988 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 124 ms
4,380 KB
testcase_23 AC 358 ms
5,668 KB
testcase_24 AC 360 ms
6,088 KB
testcase_25 AC 310 ms
5,136 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 167 ms
4,376 KB
testcase_29 AC 36 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 22 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long ll;
#define FOR(i,a,b) for(int i=(a);i<(int)(b);i++)
#define rep(i,n) FOR(i,0,n)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back
#define mp make_pair
#define F first
#define S second

int N;
bool visited[10010];

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

int main() {
    memset(visited,0,sizeof(visited));
    cin >> N;
    queue<pair<int,int>> Q;
    Q.push(mp(1,1));

    while(Q.size()) {
        auto tmp = Q.front();
        if(tmp.F == N) {
            cout << tmp.S << "\n";
            return 0;
        }

        visited[tmp.F] = true;
        int mv = calc(tmp.F);
        int nxt = tmp.F + mv;
        if(!visited[nxt] and 1<=nxt and nxt <= N) Q.push(mp(nxt, tmp.S+1));
        nxt = tmp.F - mv;
        if(!visited[nxt] and 1<=nxt and nxt <= N) Q.push(mp(nxt, tmp.S+1));
        Q.pop();
    }

    cout << -1 << "\n";

    return 0;
}
0