結果

問題 No.2617 容量3のナップザック
ユーザー kwm_tkwm_t
提出日時 2024-01-28 17:33:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 229 ms / 2,000 ms
コード長 2,798 bytes
コンパイル時間 2,922 ms
コンパイル使用メモリ 214,356 KB
実行使用メモリ 53,192 KB
最終ジャッジ日時 2024-01-28 17:33:26
合計ジャッジ時間 7,797 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 1 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 2 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 1 ms
6,548 KB
testcase_12 AC 168 ms
38,572 KB
testcase_13 AC 136 ms
45,592 KB
testcase_14 AC 161 ms
37,372 KB
testcase_15 AC 98 ms
31,016 KB
testcase_16 AC 117 ms
28,264 KB
testcase_17 AC 149 ms
35,952 KB
testcase_18 AC 134 ms
36,316 KB
testcase_19 AC 19 ms
9,180 KB
testcase_20 AC 125 ms
42,688 KB
testcase_21 AC 82 ms
20,696 KB
testcase_22 AC 142 ms
46,456 KB
testcase_23 AC 184 ms
46,704 KB
testcase_24 AC 8 ms
6,548 KB
testcase_25 AC 84 ms
30,140 KB
testcase_26 AC 125 ms
29,760 KB
testcase_27 AC 65 ms
17,532 KB
testcase_28 AC 7 ms
6,548 KB
testcase_29 AC 53 ms
19,264 KB
testcase_30 AC 136 ms
43,728 KB
testcase_31 AC 156 ms
35,232 KB
testcase_32 AC 171 ms
51,624 KB
testcase_33 AC 197 ms
51,620 KB
testcase_34 AC 220 ms
53,192 KB
testcase_35 AC 184 ms
51,552 KB
testcase_36 AC 229 ms
52,540 KB
testcase_37 AC 220 ms
51,624 KB
testcase_38 AC 155 ms
52,384 KB
testcase_39 AC 151 ms
50,364 KB
testcase_40 AC 191 ms
52,076 KB
testcase_41 AC 78 ms
50,364 KB
権限があれば一括ダウンロードができます

ソースコード

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<long long>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;
	rep(i, min((int)item[3].size(), k) + 1) {// 3の数を全探索する
		{
			int r = min(k - i, (int)item[2].size());// 2の数を三分探索する
			int l = 0;
			auto f = [&](int x)->long long {
				int y = (k - i) * 3 - x * 2;
				y = min((int)item[1].size(), y);
				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 val = -LINF;
			for (int i = l; i <= r; ++i) val = std::max(val, f(i));
			chmax(ans, val + itemSum[3][i] - itemSum[3][0]);
		}
	}
	cout << ans << endl;
	return 0;
}
0