結果
問題 | No.3 ビットすごろく |
ユーザー | Pachicobue |
提出日時 | 2017-03-13 19:59:17 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 605 ms / 5,000 ms |
コード長 | 1,867 bytes |
コンパイル時間 | 555 ms |
コンパイル使用メモリ | 67,028 KB |
実行使用メモリ | 6,212 KB |
最終ジャッジ日時 | 2024-07-01 08:37:19 |
合計ジャッジ時間 | 6,312 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#include <cassert> #include <iostream> #include <vector> #include <algorithm> #include <utility> #include <queue> #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--) #define rep(i, n) for (int i = 0; i < (n); i++) #define rep1(i, n) for (int i = 1; i <= (n); i++) #define rrep(i, n) for (int i = (n)-1; i >= 0; i--) #define pb push_back #define mp make_pair #define fst first #define snd second #define show(x) cout << #x << " = " << x << endl #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define pii pair<int, int> #define vi vector<int> using namespace std; template <class S, class T> ostream& operator<<(ostream& o, const pair<S, T>& p) { return o << "(" << p.first << "," << p.second << ")"; } template <class T> ostream& operator<<(ostream& o, const vector<T>& vc) { o << "sz = " << vc.size() << endl << "["; for (const T& v : vc) o << v << ","; o << "]"; return o; } typedef long long ll; int bits(int n) { int c = 0; while (n != 0) { c += n & 1; n /= 2; } return c; } int N; #define MAX 10001 queue<pii> q; bool v[MAX + 1]; int main() { cin >> N; q.push(mp(1, 1)); bool f = true; while (not q.empty()) { pii pos = q.front(); v[pos.fst] = true; // cout << pos << endl; q.pop(); if (pos.fst == N) { cout << pos.snd << endl; f = false; break; } int mae = pos.fst + bits(pos.fst); int ushiro = pos.fst - bits(pos.fst); if (mae <= N and (not v[mae])) { q.push(mp(mae, pos.snd + 1)); } if (ushiro >= 1 and (not v[ushiro])) { q.push(mp(ushiro, pos.snd + 1)); } } if (f) { cout << -1 << endl; } return 0; }