結果

問題 No.941 商とあまり
ユーザー QCFium
提出日時 2019-09-09 20:59:17
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,917 bytes
コンパイル時間 2,203 ms
コンパイル使用メモリ 181,384 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-21 04:51:16
合計ジャッジ時間 4,991 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 6
other WA * 104
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

int gcd(int a, int b) {
	while (a && b) {
		if (a > b) a %= b;
		else b %= a;
	}
	return a + b;
}

bool solve(int n, int x, const std::vector<int> &a_) {
	auto a = a_;
	std::sort(a.begin(), a.end());
	if (n == 1) return x == a[0];
	int64_t prod = 1;
	for (int i = 0; i < n; i++) {
		prod *= a[i] + 1;
		if (prod > x + 1) return false;
	}
	int _gcd = 0;
	for (int i = 0; i < n; i++) _gcd = gcd(_gcd, a[i]);
	if (x % _gcd) return false;
	if (n > 2) {
		std::pair<int, int> min = {30000, 30000};
		for (int i = 0; i < n; i++)
			for (int j = 0; j < i; j++)
				if (gcd(a[i], a[j]) == 1 && a[i] * a[j] < min.first * min.second) min = {a[i], a[j]};
		
		if (x - prod + 1 > min.first * (min.first + 1) * min.second) return true;
	}
	int y = x + 1 - prod;
	assert(y >= 0);
	std::vector<std::vector<bool> > dp(1 << n);
	for (int i = 0; i < 1 << n; i++) dp[i].resize(y + 1);
	for (int i = 0; i < n; i++) dp[1 << i][0] = true;
	for (int i = 0; i < 1 << n; i++) {
		/*
		int base = 1;
		for (int j = 0; j < n; j++) if (i >> j & 1) base *= a[j] + 1;
		base--;*/
		for (int j = 0; j < n; j++) {
			if (i >> j & 1) continue;
			std::vector<bool> update(y + 1);
			for (int k = 0; k <= y; k++) if (dp[i][k] && (int64_t)k * (a[j] + 1) <= y) update[k * (a[j] + 1)] = true;
			for (int k = 0; k <= y - a[j]; k++) if (update[k]) update[k + a[j]] = true;
			for (int k = 0; k <= y; k++) if (update[k]) dp[i | 1 << j][k] = true;
		}
	}
	return dp.back()[y];
}

int main() {
	int n = ri(), x = ri();
	assert(1 <= n && n <= 100000);
	assert(1 <= x && x <= 10000000);
	std::vector<int> a(n);
	for (int i = 0; i < n; i++) a[i] = ri(), assert(1 <= a[i] && a[i] <= x);
	clock_t r0 = clock();
	std::cout << (solve(n, x, a) ? "YES" : "NO") << std::endl;
	clock_t r1 = clock();
	std::cerr << (double)(r1 - r0) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl;
	return 0;
}

0