結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  zenito9970 | 
| 提出日時 | 2017-03-26 19:29:49 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 4 ms / 5,000 ms | 
| コード長 | 2,052 bytes | 
| コンパイル時間 | 1,866 ms | 
| コンパイル使用メモリ | 179,204 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-07-01 08:37:32 | 
| 合計ジャッジ時間 | 2,802 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) FOR(i,0,n)
#define repr(i,n) for(int i=(n)-1;0<=i;--i)
#define each(e,v) for(auto&& e:(v))
#define all(v) begin(v),end(v)
#define DUMP(x) cerr<<#x<<": "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<": "<<(x)<<" (L"<<__LINE__<<")"<<endl
using namespace std;
using vint = vector<int>;
using vdouble = vector<double>;
using vstring = vector<string>;
using ll = long long;
template <class T> void chmin(T& a, const T& b) { a = min(a, b); }
template <class T> void chmax(T& a, const T& b) { a = max(a, b); }
constexpr int popcnt(int a) { return __builtin_popcount(a); }
template <class E>
using Graph = vector<vector<E>>;
struct Edge {
    int to, cost;
    Edge(int to, int cost): to(to), cost(cost) {}
};
template <class E>
void addEdge(Graph<E>& graph, int from, int to, int cost) {
    graph[from].push_back(E(to, cost));
}
template <class T, T INF>
struct Dijkstra {
    vector<T> res;
    template <class E>
    Dijkstra(const Graph<E>& graph, int s) {
        res = vector<T>(graph.size(), INF);
        using P = pair<T, int>; // cost, to
        priority_queue<P, vector<P>, greater<P>> Q;
        Q.push(P(0, s));
        res[s] = 0;
        while(!Q.empty()) {
            const T cost = Q.top().first;
            const int to = Q.top().second;
            Q.pop();
            if(res[to] < cost) continue;
            for(E e: graph[to]) {
                if(cost + e.cost < res[e.to]) {
                    res[e.to] = cost + e.cost;
                    Q.push(P(res[e.to], e.to));
                }
            }
        }
    }
};
int main() {
    int N; cin >> N;
    vint to(N);
    rep(i, N) to[i] = popcnt(i+1);
    Graph<Edge> graph(N);
    rep(i, N) {
        if(i + to[i] < N) addEdge(graph, i, i + to[i], 1);
        if(0 < i - to[i]) addEdge(graph, i, i - to[i], 1);
    }
    const int inf = 1e9;
    Dijkstra<int, inf> dijk(graph, 0);
    if(dijk.res[N-1] == inf) cout << -1 << endl;
    else cout << (dijk.res[N-1]+1) << endl;
    return 0;
}
            
            
            
        