結果
問題 |
No.3 ビットすごろく
|
ユーザー |
![]() |
提出日時 | 2016-09-14 14:59:50 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 985 bytes |
コンパイル時間 | 477 ms |
コンパイル使用メモリ | 56,060 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-01 08:05:38 |
合計ジャッジ時間 | 1,445 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#include <iostream> using namespace std; #define MAX 10000000 int q[MAX]; int head = 0, tail = 0; void enq(int p) { q[tail] = p; ++tail; if (tail >= MAX) { tail = 0; } } bool deq(int* p) { if (head == tail) { return false; } else { *p = q[head]; ++head; if (head >= MAX) { head = 0; } return true; } } int BinaryCount(int a) { int cnt = 0; while (a>0) { cnt++; a &= a - 1; } return cnt; } int main(void) { int n; cin >> n; int* count = new int[n+1]; for (int i = 1; i <= n; ++i) { count[i] = 0; } int posi = 1, hosuu; count[posi] = 1; do { if (posi == n) { cout << count[posi] << endl; return 0; } hosuu = BinaryCount(posi); if (posi - hosuu > 0 && !count[posi - hosuu]) { count[posi - hosuu] = count[posi] + 1; enq(posi - hosuu); } if (posi + hosuu <= n && !count[posi + hosuu]) { count[posi + hosuu] = count[posi] + 1; enq(posi + hosuu); } } while (deq(&posi)); cout << "-1" << endl; return 0; }