結果
問題 | No.3 ビットすごろく |
ユーザー | みしあ |
提出日時 | 2021-10-21 21:57:56 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 5 ms / 5,000 ms |
コード長 | 3,583 bytes |
コンパイル時間 | 5,213 ms |
コンパイル使用メモリ | 265,988 KB |
最終ジャッジ日時 | 2025-01-25 02:37:00 |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#pragma region Macros #include <bits/stdc++.h> #if defined(LOCAL) || defined(ONLINE_JUDGE) || defined(_DEBUG) #include <atcoder/all> #endif using namespace std; #define REP(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define REPR(i, n) for(int i=(n); i>=0; --i) #define FOR(i, n, m) for(int i=(m), i##_len=(n); i<i##_len; ++i) #define EACH(i, v) for(const auto& i : v) #define ALL(x) (x).begin(),(x).end() #define ALLR(x) (x).rbegin(),(x).rend() template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } template<class T>using vec = vector<T>; template<class T, class U>using umap = unordered_map<T, U>; template<class T>using uset = unordered_set<T>; using ll = long long; using ld = long double; using P = pair<ll, ll>; using T = tuple<ll, ll, ll>; using vl = vec<ll>; #define fi first #define se second #define el endl constexpr ll INF = numeric_limits<ll>::max()/2-1; #pragma endregion #pragma region IOMacros template<class T> istream &operator>>(istream &stream, vec<T>& o){REP(i, o.size())stream >> o[i];return stream;} template<class T> ostream &operator<<(ostream &stream, vec<T>& objs){REP(i, objs.size())stream << objs[i] << " ";stream << el;return stream;} #define I(T, ...) ;T __VA_ARGS__;__i(__VA_ARGS__); void __i() {} template<class T, class... Ts> void __i(T&& o, Ts&&... args){cin >> o;__i(forward<Ts>(args)...);} void O() {cout << el;} template<class T, class... Ts> void O(T&& o, Ts&&... args){cerr << o << " ";O(forward<Ts>(args)...);} #pragma endregion void Main(); int main(){ std::cin.tie(nullptr); std::cout << std::fixed << std::setprecision(15); Main(); return 0; } #pragma region graph struct edge{ ll from, to, cost; bool operator<(const edge& r) const{return cost<r.cost;} bool operator>(const edge& r) const{return cost>r.cost;} }; struct graph{ ll V; vector<vector<edge> > G; graph(ll n){ init(n); } void init(ll n){ V = n; G.resize(V); } // 無向グラフ void add_edge(ll s, ll t, ll cost = 1){ add_diedge(s, t, cost); add_diedge(t, s, cost); } // 有向グラフ void add_diedge(ll s, ll t, ll cost = 1){ if(s < 0 || t < 0 || s >= V || t >= V) return; G[s].push_back({s, t, cost}); } auto pos2d(ll height, ll width){ return [height, width](ll y, ll x){ return (y < 0 || y >= height || x < 0 || x >= width) ? -1 : y*width + x; }; } }; #pragma endregion // O(V+E) umap<ll, ll> bfs(const graph& g, ll s, ll limit = INF){ vector<ll> d(g.V, INF); umap<ll, ll> ret; d[s] = 0; queue<P> que; que.push({0, s}); ret[s] = 0; while(!que.empty()){ auto [c, v] = que.front(); que.pop(); if(d[v]<c) continue; for(auto e : g.G[v]){ if(d[e.to]>d[v]+1 && limit>=d[v]+1){ d[e.to] = d[v]+1; que.push({d[e.to],e.to}); ret[e.to] = d[e.to]; } } } return ret; } void Main(){ I(ll, N); graph g(N); REP(i, N){ ll s = i+1; ll t = s+__builtin_popcountll(s); ll u = s-__builtin_popcountll(s); if(t <= N){ g.add_diedge(s-1, t-1); } if(t > 0){ g.add_diedge(s-1, u-1); } } auto d = bfs(g, 0); if(d.count(N-1)){ cout << d[N-1]+1 << el; }else{ cout << -1 << el; } }