結果

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

テストケース

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

ソースコード

diff #

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

#define Test 0


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