結果

問題 No.3 ビットすごろく
ユーザー yuustiyuusti
提出日時 2014-12-05 01:42:37
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,321 bytes
コンパイル時間 742 ms
コンパイル使用メモリ 92,836 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-01 07:07:23
合計ジャッジ時間 1,713 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>
#include <tuple>
#include <climits>
#include <bitset>
#include <cassert>
#include <random>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define UNIQUE(v) (v).erase(unique(ALL(v)), (v).end())
#define MP make_pair
#define MT make_tuple

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n;
	cin >> n;

	vector<int> dist(n+1, -1);
	dist[1] = 1;

	queue<int> q;
	q.push(1);
	while (!q.empty()){
		int pos = q.front();
		q.pop();

		int cnt = 0;
		rep(i, 30) cnt += pos >> i & 1;

		for (auto e : { -1, 1 }){
			int nxt = pos + e*cnt;
			if (nxt <= 0 || nxt > n) continue;
			if (dist[nxt] >= 0) continue;
			dist[nxt] = dist[pos] + 1;
			q.push(nxt);
		}
	}

	cout << dist[n] << endl;


	return 0;
}
0