結果
問題 | No.3 ビットすごろく |
ユーザー |
|
提出日時 | 2023-01-09 19:42:39 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 5 ms / 5,000 ms |
コード長 | 1,044 bytes |
コンパイル時間 | 926 ms |
コンパイル使用メモリ | 114,788 KB |
最終ジャッジ日時 | 2025-02-10 01:25:55 |
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#include <iostream>#include <algorithm>#include <iomanip>#include <map>#include <set>#include <queue>#include <stack>#include <numeric>#include <bitset>#include <cmath>static const int MOD = 1000000007;using ll = long long;using u32 = unsigned;using u64 = unsigned long long;using namespace std;template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;int main() {int n;cin >> n;vector<int> d(n, INF<int>);vector<vector<int>> v(n);for (int i = 0; i < n; ++i) {int k = __builtin_popcount(i+1);if(i > k) {v[i].emplace_back(i-k);}if(i+k < n){v[i].emplace_back(i+k);}}stack<pair<int, int>> Q;Q.emplace(0, 1);while(!Q.empty()){auto [x, c] = Q.top(); Q.pop();d[x] = c;for (auto &&i : v[x]) {if(c+1 < d[i]){Q.emplace(i, c+1);}}}if(d[n-1] == INF<int>) puts("-1");else cout << d[n-1] << "\n";return 0;}