結果

問題 No.10 +か×か
ユーザー OnjuOnju
提出日時 2015-02-12 02:58:03
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,522 bytes
コンパイル時間 422 ms
コンパイル使用メモリ 61,096 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-21 06:09:59
合計ジャッジ時間 1,116 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 4 ms
4,384 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <bitset>
#include <algorithm>
#include <climits>
#define N_MAX 50
#define T_MAX 100000
#define setBit(x,b) ((x)|((ULL)1<<(b)))
typedef unsigned long long ULL;
using namespace std;

int N, T, A[N_MAX];
int Amin[N_MAX], Amax[N_MAX];

ULL calc(int t, int n, ULL op)
{
	static ULL ans = ULLONG_MAX;

	if (n < 0 && t == 0)
	{
		if (op < ans)
			ans = op;
		return 0;
	}
	if (n < 0 || t <= 0)
		return 0;

	if (t < Amin[n] || t > Amax[n])
		return 0;

	for (; n > 0 && (t%A[n]); --n)
		t -= A[n];

	//1の連続だけは単独で計算しないと全通り探索される
	if (A[n] == 1 )
	{
		int count = 1;
		for (int i = n - 1; i > 0 && A[i] == 1; --i)
			++count;

		for (int i = count; i >= 0; --i)
		{
			calc(t - i, n - count, op);
			op = setBit(op, N - n + (count - i));
		}

		return ans;
	}

	calc(t - A[n], n - 1, op);
	if (n > 0)
		calc(t / A[n], n - 1, setBit(op, N - n));

	return ans;
}

ULL calc()
{
	Amin[0] = A[0];
	Amax[0] = A[0];
	for (int i = 1; i < N; ++i)
	{
		if (A[i] == 1 || Amin[i - 1] == 1)
			Amin[i] = Amin[i - 1] * A[i];
		else
			Amin[i] = Amin[i - 1] + A[i];

		if (A[i] == 1 || Amax[i - 1] == 1)
			Amax[i] = Amax[i - 1] + A[i];
		else
			Amax[i] = Amax[i - 1] * A[i];
		if (Amax[i] > T_MAX)
			Amax[i] = T_MAX;
	}

	return calc(T, N - 1, 0);
}

int main()
{
	const char* op = "+*";

	cin >> N >> T;
	for (int i = 0; i < N; ++i)
		cin >> A[i];

	//計算
	bitset<64> ans(calc());
	for (int i = N - 1; i >= 1; --i)
		cout << op[ans[i]];
	cout << endl;

	return 0;
}
0