結果
| 問題 | No.3 ビットすごろく |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-04-17 19:04:24 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 5,000 ms |
| コード長 | 1,307 bytes |
| コンパイル時間 | 709 ms |
| コンパイル使用メモリ | 86,156 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-01 09:18:00 |
| 合計ジャッジ時間 | 1,758 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
#include <iostream>
#include <string>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <iomanip>
using namespace std;
#define REP(i, n) for(ll i = 0;i < n;i++)
#define REPR(i, n) for(ll i = n;i >= 0;i--)
#define FOR(i, m, n) for(ll i = m;i < n;i++)
#define FORR(i, m, n) for(ll i = m;i >= n;i--)
#define REPO(i, n) for(ll i = 1;i <= n;i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(),n.end()
#define MOD 1000000007
#define P pair<ll, ll>
ll ans[11000], n;
queue<P> q;
int main() {
REP(i, 11000)ans[i] = INF;
cin >> n;
ans[1] = 1;
q.push(P(1, 1));
while (!q.empty()) {
P now = q.front();
q.pop();
ll cnt = 0, nowf = now.first;
while (nowf > 0) {
if (nowf % 2 == 1)cnt++;
nowf /= 2;
}
if (now.first - cnt > 0) {
if (ans[now.first - cnt] > now.second + 1) {
q.push(P(now.first - cnt, now.second + 1));
ans[now.first - cnt] = now.second + 1;
}
}
if (now.first + cnt <= n) {
if (ans[now.first + cnt] > now.second + 1) {
q.push(P(now.first + cnt, now.second + 1));
ans[now.first + cnt] = now.second + 1;
}
}
}
if (ans[n] == INF)cout << -1 << endl;
else cout << ans[n] << endl;
}