結果
問題 |
No.3 ビットすごろく
|
ユーザー |
![]() |
提出日時 | 2018-11-03 18:33:57 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3 ms / 5,000 ms |
コード長 | 1,425 bytes |
コンパイル時間 | 900 ms |
コンパイル使用メモリ | 96,996 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-01 09:11:26 |
合計ジャッジ時間 | 1,933 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
/* g++ -std=c++11 -Wall -O2 -o main.exe main.cpp ./main.exe */ #include <cstdlib> #include <cstdio> #include <cmath> #include <climits> //intの最大値: INT_MAX //longの最大値: LONG_MAX //long longの最大値: LLONG_MAX #include <iostream> #include <string> #include <sstream> #include <vector> #include <map> #include <functional> #include <algorithm> #include <complex> #include <numeric> #include <queue> #include <stack> //最大公約数: gcd() //最小公倍数: lcm() #define ll long long int using namespace std; template <typename T> void sortasc(vector<T> &v){ //vectorを昇順にソート sort(v.begin(), v.end(), std::less<T>()); return; } template <typename T> void sortdesc(vector<T> &v){ //vectorを降順にソート sort(v.begin(), v.end(), std::greater<T>()); return; } //------------------------------------------------------------------------------ queue<int> q; int main(){ int n; cin >> n; int c = 1, temp = 1, count = 0; vector<int> d(n + 1); d[1] = 1; while(c != n){ while(temp){ if(temp % 2 == 1) count++; temp /= 2; } int v1 = c + count, v2 = c - count; if(v1 <= n && d[v1] == 0){ q.push(v1); d[v1] = d[c] + 1; } if(v2 > 0 && d[v2] == 0){ q.push(v2); d[v2] = d[c] + 1; } count = 0; if(q.size() == 0){ cout << -1 << endl; return 0; } c = temp = q.front(); q.pop(); } cout << d[c] << endl; return 0; }