結果

問題 No.2617 容量3のナップザック
ユーザー kwm_tkwm_t
提出日時 2024-01-28 17:21:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,819 bytes
コンパイル時間 2,750 ms
コンパイル使用メモリ 215,316 KB
実行使用メモリ 46,496 KB
最終ジャッジ日時 2024-01-28 17:21:46
合計ジャッジ時間 8,481 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 WA -
testcase_03 AC 2 ms
6,676 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 2 ms
6,676 KB
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 AC 139 ms
30,836 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 90 ms
25,644 KB
testcase_26 WA -
testcase_27 AC 67 ms
15,228 KB
testcase_28 WA -
testcase_29 AC 54 ms
16,592 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 235 ms
46,496 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 166 ms
42,540 KB
testcase_40 AC 204 ms
45,412 KB
testcase_41 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
//using mint = modint998244353;
//const int mod = 998244353;
//using mint = modint1000000007;
//const int mod = 1000000007;
//const int INF = 1e9;
const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
long long Ternarysearch() {
	long long a, b; std::cin >> a >> b;
	long long r = LINF;
	long long l = 0;
	auto f = [&](long long x)->long long {
		return (double)a / sqrt(1.0 + x) + (double)b * x;
	};
	// 上に凸
	while (2 < abs(r - l)) {
		long long ml = (l * 2 + r) / 3;
		long long mr = (l + r * 2) / 3;
		if (f(ml) > f(mr)) r = mr;
		else l = ml;
	}
	long long ret = -LINF;
	for (long long i = l; i <= r; ++i)ret = std::max(ret, f(i));
	return ret;
}
int f0(int s1, int s2) {
	if (s1 >= s2) {
		return s2 + (s1 - s2) / 3;
	}
	return s1;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, k;
	long long seed, a, b, m;
	cin >> n >> k >> seed >> a >> b >> m;
	vector<int>w(n), v(n);
	vector<long long>f(n * 2);
	f[0] = seed;
	rep2(i, 1, n * 2) f[i] = (a * f[i - 1] + b) % m;
	rep(i, n) {
		w[i] = f[i] % 3 + 1;
		v[i] = w[i] * f[i + n];
	}
	vector item(4, vector<long long>());
	rep(i, n)item[w[i]].push_back(v[i]);
	rep(i, 4)sort(allR(item[i]));
	vector itemSum(4, vector<long long>());
	rep(i, 4) {
		itemSum[i].resize(item[i].size() + 1);
		rep(j, item[i].size()) itemSum[i][j + 1] = itemSum[i][j] + item[i][j];
	}
	long long ans = 0;
	int tmp = f0(item[1].size(), item[2].size());
	rep(i, min((int)item[3].size(), k) + 1) {// 3の数を全探索する
		long long val = itemSum[3][i] - itemSum[3][0];
		int j = min(k - i, tmp);
		{
			int r = item[2].size();// 2を使う数
			int l = 0;
			auto f = [&](int x)->long long {
				int y = (k - i) * 3 - x * 2;
				if (x > item[2].size())return -LINF;
				if (y > item[1].size())return -LINF;
				long long ret = 0;
				ret += itemSum[1][y] - itemSum[1][0];
				ret += itemSum[2][x] - itemSum[2][0];
				return ret;
			};
			// 上に凸
			while (2 < abs(r - l)) {
				int ml = (l * 2 + r) / 3;
				int mr = (l + r * 2) / 3;
				if (f(ml) > f(mr)) r = mr;
				else l = ml;
			}
			long long ret = -LINF;
			for (int i = l; i <= r; ++i) ret = std::max(ret, f(i));
			chmax(ans,val + ret);
		}
	}
	cout << ans << endl;
	return 0;
}
0