結果
| 問題 |
No.3 ビットすごろく
|
| コンテスト | |
| ユーザー |
vintersn0w
|
| 提出日時 | 2018-05-08 16:34:55 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 833 bytes |
| コンパイル時間 | 710 ms |
| コンパイル使用メモリ | 80,756 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-01 09:00:07 |
| 合計ジャッジ時間 | 1,770 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
#include <iostream>
#include <algorithm>
#include <vector>
#include <stdio.h>
#include <cmath>
#include <queue>
using namespace std;
typedef uint uint32_t;
int f(int N) {
queue<int> q;
q.push(1);
int count[10001] = {0};
count[1] = 1;
while (q.size() > 0) {
int n = q.front();
// cout << n << endl;
q.pop();
if (n == N) return count[n];
int bc = __builtin_popcount(n);
int n1 = n + bc;
int n2 = n - bc;
if (0 < n1 && n1 <=N && count[n1] == 0) {
count[n1] = count[n] + 1;
q.push(n1);
}
if (0 < n2 && n2 <=N && count[n2] == 0) {
count[n2] = count[n] + 1;
q.push(n2);
}
}
return -1;
}
int main() {
int N;
cin >> N;
cout << f(N) << endl;
return 0;
}
vintersn0w