結果

問題 No.3 ビットすごろく
ユーザー satori16satori16
提出日時 2016-08-14 09:49:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,087 bytes
コンパイル時間 744 ms
コンパイル使用メモリ 77,372 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-25 06:32:32
合計ジャッジ時間 1,661 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdint>
#include <limits>
#include <algorithm>
#include <tuple>

#define Test 1


typedef std::int16_t IntT;
typedef std::tuple<IntT, IntT> DType;
typedef std::vector<DType> Stack;
typedef std::vector<IntT> Stack2;
IntT BitCount(const IntT& v) {
	IntT C =0;

	for (IntT i = 0; i <= std::numeric_limits<IntT>::digits; i++) {
		C += (v&(1 << i)) > 0;
	}

	return C;
}

bool Rec(Stack2& S,const IntT& N,const IntT& V) {
	IntT B = BitCount(V);
	if (S.size() == 0) return false;
	if (V == N) return true;
	if (V > N)return false;
	if (V == 0) return false;
	
	//auto it = std::find(S.rbegin(), S.rend(), V+B);
	//if (it != S.rend()) return false;

	S.push_back(V + B);
	if (Rec(S, N, V + B) == true) return true;
	S.pop_back();
	auto it = std::find(S.rbegin(), S.rend(), V-B);
	if (it != S.rend()) return false;
	S.push_back(V - B);
	if (Rec(S, N,V - B) == true) return true;
	S.pop_back();
	return false;
}

IntT MakeHoge(const IntT& N) {
	Stack2 S;
	S.push_back(1);
	Rec(S, N,1);

	return S.back()==N? S.size():0;
}

/** /
std::size_t MakeHoge(const IntT& N) {
	Stack S;
	IntT C = 1;
	IntT B = 0;	
	S.push_back(std::make_tuple(C, 0));
	while (C != N && S.size() != 0) {
		if (std::get<1>(S.back()) == 2) {
			S.pop_back();
			continue;
		}
		C = std::get<0>(S.back());
		B = BitCount(C);
		if (std::get<1>(S.back()) == 0) {
			C += B;
		}
		else {
			C -= B;
		}
		std::get<1>(S.back())++;
		auto rit = std::find_if(S.rbegin(), S.rend(), [&](auto& o) {return std::get<0>(o) == C; });
		if (rit != S.rend()) {
			S.erase(rit.base(), S.rbegin().base());
		}
		else {
			if (C <= N) {
				S.push_back(std::make_tuple(C, 0));
			}
		}
	}
	return S.size();
}
/**/
int main() {

	IntT R = 0;
	IntT N = 0;

#if Test
	N = 5;
	R = MakeHoge(N);
	std::cout << ((R == 0) ? -1 : R) << std::endl;
	N = 11;
	R = MakeHoge(N);
	std::cout << ((R == 0) ? -1 : R) << std::endl;
	N = 4;
	R = MakeHoge(N);
	std::cout << ((R == 0) ? -1 : R) << std::endl;
#else
	std::cin >> N;
	R = MakeHoge(N);
	std::cout << ((R == 0) ? -1 : R) << std::endl;
#endif

	return 0;
}
0