結果

問題 No.3 ビットすごろく
ユーザー kichirb3kichirb3
提出日時 2018-03-07 22:14:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,635 bytes
コンパイル時間 946 ms
コンパイル使用メモリ 93,236 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-14 00:56:53
合計ジャッジ時間 2,044 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 3 ms
4,380 KB
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// No.3 ビットすごろく
// https://yukicoder.me/problems/no/3
//

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <numeric>
using namespace std;

struct edge {
    int to;
    int cost;
};
typedef pair<int, int> P;
const int INF = 999999999;

int pop_count(unsigned int n);
vector<int> dijkstra(vector<edge>& adj, int s);


int pop_count(unsigned int n) {
    int res;
    __asm__( "popcnt %1, %0" : "=r"(res) : "r"(n) );
    return(res);
}


vector<int> dijkstra(vector<vector<edge>>& adj, int s)
{
    vector<int> d(adj.size()+1);
    priority_queue<P, vector<P>, greater<P>> que;
    fill(d.begin(), d.end(), INF );
    d[s] = 0;
    que.push(P(0, s));

    while (!que.empty()) {
        P p = que.top();
        que.pop();
        int v = p.second;
        if (d[v] < p.first)
            continue;
        for (int i = 0; i < adj[v].size(); ++i) {
            edge e = adj[v][i];
            if (d[e.to] > d[v] + e.cost) {
                d[e.to] = d[v] + e.cost;
                que.push(P(d[e.to], e.to));
            }
        }
    }
    return d;
}


int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    int N;
    cin >> N;

    vector<vector<edge>> adj;
    adj.resize(N+1);

    for (auto i = 1; i <= N; ++i) {
        int b = pop_count(i);
        if (i-b >= 1)
            adj[i].push_back({i-b, 1});
        if (i+b <= N)
            adj[i].push_back({i+b, 1});
    }

    vector<int> dist = dijkstra(adj, 1);
    if (dist[N] != INF)
        cout << dist[N] +1 << endl;
    else
        cout << -1 << endl;
}
0