結果
問題 |
No.3 ビットすごろく
|
ユーザー |
![]() |
提出日時 | 2015-02-07 12:27:31 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 1,102 bytes |
コンパイル時間 | 714 ms |
コンパイル使用メモリ | 87,308 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-01 07:12:24 |
合計ジャッジ時間 | 1,706 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <iostream> #include <map> #include <set> #include <list> #include <cstdio> #include <cmath> #include <cstring> #include <string> #include <vector> #include <algorithm> #include <utility> #include <queue> #include <iomanip> #define SIZE 10001 using namespace std; /* 1のビット数 */ int numofbits(long bits) { bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555); bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333); bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f); bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff); return (bits & 0x0000ffff) + (bits >>16 & 0x0000ffff); } int main() { int d[SIZE]; int N; queue<int> que; cin >> N; memset(d, -1, sizeof(d)); que.push(1); d[1] = 1; while (que.size()) { int p = que.front(); que.pop(); int n = p + numofbits(p); if (0 < n && n <= N && d[n] == -1) { que.push(n); d[n] = d[p] + 1; } n = p - numofbits(p); if (0 < n && n <= N && d[n] == -1) { que.push(n); d[n] = d[p] + 1; } } cout << d[N] << endl; }