結果

問題 No.3 ビットすごろく
ユーザー ふうふう
提出日時 2015-02-07 12:26:12
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 1,100 bytes
コンパイル時間 1,957 ms
コンパイル使用メモリ 87,300 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-09-05 14:59:38
合計ジャッジ時間 5,794 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 RE -
testcase_04 WA -
testcase_05 RE -
testcase_06 RE -
testcase_07 WA -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 WA -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 WA -
testcase_19 RE -
testcase_20 WA -
testcase_21 AC 2 ms
4,376 KB
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 AC 2 ms
4,376 KB
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 WA -
testcase_31 WA -
testcase_32 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <iomanip>

#define SIZE 100

using namespace std;

/* 1のビット数 */
int numofbits(long bits)
{
    bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
    bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
    bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
    bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
    return (bits & 0x0000ffff) + (bits >>16 & 0x0000ffff);
}

int main()
{
	int d[SIZE];
	int N;
	queue<int> que;

	cin >> N;

	memset(d, -1, sizeof(d));
	
	que.push(1);
	d[1] = 1;

	while (que.size()) {
		int p = que.front();
		que.pop();

		int n = p + numofbits(p);
		if (0 < n && n <= N && d[n] == -1) {
			que.push(n);
			d[n] = d[p] + 1;
		} 
		n = p - numofbits(p);
		if (0 < n && n <= N && d[n] == -1) {
			que.push(n);
			d[n] = d[p] + 1;
		} 
	}

	cout << d[N] << endl;
}

0