結果

問題 No.3 ビットすごろく
ユーザー airuaiairuai
提出日時 2016-09-14 14:22:04
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2,925 ms / 5,000 ms
コード長 1,008 bytes
コンパイル時間 518 ms
コンパイル使用メモリ 63,708 KB
実行使用メモリ 37,836 KB
最終ジャッジ日時 2023-09-14 00:08:56
合計ジャッジ時間 24,848 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 6 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 63 ms
4,380 KB
testcase_06 AC 6 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 23 ms
4,376 KB
testcase_09 AC 408 ms
7,632 KB
testcase_10 AC 1,128 ms
11,972 KB
testcase_11 AC 253 ms
6,752 KB
testcase_12 AC 52 ms
4,376 KB
testcase_13 AC 4 ms
4,376 KB
testcase_14 AC 945 ms
12,140 KB
testcase_15 AC 2,499 ms
30,096 KB
testcase_16 AC 1,429 ms
12,124 KB
testcase_17 AC 2,225 ms
29,676 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 2,897 ms
37,752 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 994 ms
12,004 KB
testcase_23 AC 2,925 ms
37,740 KB
testcase_24 AC 2,895 ms
37,836 KB
testcase_25 AC 2,493 ms
30,056 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 5 ms
4,376 KB
testcase_28 AC 1,320 ms
12,068 KB
testcase_29 AC 259 ms
6,760 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 155 ms
5,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
using namespace std;

int BinaryCount(int a) {
	/*int count = 0;
	while (n > 0) {
	count += n % 2;
	n /= 2;
	}
	return count;*/

	int cnt = 0;
	while (a>0) {
		cnt++;
		a &= a - 1;
	}
	return cnt;

}

class Data {
public:
	int p, kind;
	Data(){}
	Data(int p, int kind) {
		this->p = p;
		this->kind = kind;
	}
	void set(int a, int b) {
		p = a;
		kind = b;
	}
};

int main(void) {
	int n;
	cin >> n;

	bool* flag = new bool[n];
	for (int i = 0; i < n; ++i) {
		flag[i] = false;
	}

	int hosuu;
	queue<Data> q;
	Data d(1, 1);
	Data out;
	q.push(d);
	do {
		d = q.front(); q.pop();
		if (d.p == n) {
			cout << d.kind << endl;
			return 0;
		}
		flag[d.p - 1] = true;
		hosuu = BinaryCount(d.p);
		if (d.p - hosuu > 0 && !flag[d.p - 1 - hosuu]) {
			out.set(d.p - hosuu, d.kind + 1);
			q.push(out);
		}
		if (d.p + hosuu <= n && !flag[d.p - 1 + hosuu]) {
			out.set(d.p + hosuu, d.kind + 1);
			q.push(out);
		}
	} while (!q.empty());

	cout << "-1" << endl;

	return 0;
}
0