結果
| 問題 |
No.3 ビットすごろく
|
| コンテスト | |
| ユーザー |
gunmaneko
|
| 提出日時 | 2019-08-29 21:04:50 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 5,000 ms |
| コード長 | 1,000 bytes |
| コンパイル時間 | 1,465 ms |
| コンパイル使用メモリ | 164,008 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-01 09:28:32 |
| 合計ジャッジ時間 | 2,319 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
#include "bits/stdc++.h"
using namespace std;
#define REP(i,a) for(int i = 0; i < (a);i++)
#define ALL(a) (a).begin(),(a).end()
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const int MOD = 1e9 + 7;
using Graph = vector < vector<int>>;
long int sum[200020] = {};
int main() {
int N;
cin >> N;
queue<int> que;
vector<int> dist(N+1,0);
vector<int> check(N+1,0);
que.push(1);
dist[1] = 1;
check[1] = 1;
while (!(que.empty())) {
int a = que.front();
que.pop();
int sum = a;
int count = 0;
while (sum) {
count = count + sum % 2;
sum = sum / 2;
}
int b = a + count;
int c = a - count;
if (b <= N) {
if (check[b] == 0) {
que.push(b);
dist[b] = 1 + dist[a];
check[b] = 1;
if (b == N) {
break;
}
}
}
if (c > 1) {
if (check[c] == 0) {
que.push(c);
dist[c] = 1 + dist[a];
check[c] = 1;
if (c == N) {
break;
}
}
}
}
if (check[N] == 0) {
cout << -1;
}
else {
cout << dist[N];
}
}
gunmaneko