题面
来源:FrontierCS 官方 statement.txt,与模型收到的题面相同。
```markdown
# Metallic Pink Resonator Layout
## Problem
The Martians have built a circular relay with ports labeled `0,1,…,M-1`.
Some ports will be equipped with **pink emitters**; the rest remain **plain reflectors**.
If port `a` is an emitter and port `b` is a reflector, then together they can generate the frequency
\[
(a+b)\bmod M.
\]
For each frequency `r`, the Martians know:
- a **value** `w_r` gained for each successful generating pair producing `r`,
- a **saturation limit** `d_r`: once `d_r` pairs producing `r` exist, extra pairs for `r` give no additional benefit.
You must choose which ports become emitters, subject to a budget.
Let `A` be the set of chosen emitters, and `B = {0,1,…,M-1} \ A` be the reflectors.
For each residue `r`, define
\[
c_r = |\{(a,b)\in A\times B : (a+b)\bmod M = r\}|,
\]
where pairs are **ordered**: choosing emitter `a` and reflector `b` is one pair.
The contribution of residue `r` is
\[
w_r \cdot \min(c_r, d_r).
\]
Your task is to output any feasible emitter set maximizing the total contribution.
---
## Input
A single test file contains one instance.
- The first line contains two integers `M` and `B` — the number of ports and the total budget.
- The second line contains `M` integers `cost_0, cost_1, …, cost_{M-1}` — the cost of installing an emitter at each port.
- The third line contains `M` integers `w_0, w_1, …, w_{M-1}` — the value of each residue.
- The fourth line contains `M` integers `d_0, d_1, …, d_{M-1}` — the saturation limit of each residue.
All residue arithmetic is modulo `M`.
---
## Output
Output any feasible solution.
- The first line must contain one integer `K` — the number of chosen emitter ports.
- The second line must contain `K` distinct integers `p_1, p_2, …, p_K` in increasing order, each between `0` and `M-1`, denoting the emitter ports.
A solution is **feasible** if:
1. all listed ports are distinct,
2. all listed ports are valid indices in `[0, M-1]`,
3. \(\sum_{i=1}^{K} cost_{p_i} \le B\).
If your output is infeasible or malformed, your objective value is `0`.
If `K = 0`, you may omit the second line.
---
## Objective
For your chosen emitter set `A` and reflector set `B = [0, M-1] \ A`, compute for each residue `r`:
\[
c_r = |\{(a,b)\in A\times B : (a+b)\bmod M = r\}|.
\]
Your objective value is
\[
U = \sum_{r=0}^{M-1} w_r \cdot \min(c_r, d_r).
\]
Higher is better.
---
## Scoring
For each official test instance, the judge also computes a deterministic **baseline** solution:
1. Sort all ports by `(cost_i, i)` ascending.
2. Traverse this order once.
3. Add port `i` to the emitter set iff doing so keeps the total cost at most `B`.
The physical objective is selected-set utility U=compute_objective; larger is better. The physical reference is `a = U_base after affordable-singleton fallback`; matching a positive reference earns 60 points.
Define the historical low-score expression (in points):
P_low = 60*U/R.
If `P_low<=60`, retain `max(0,P_low)`. For `P_low>60` and a positive physical reference and objective, award:
r = U / a
P = min(100, 60 + ln(r)).
Here `r` is the improvement factor relative to the physical reference, and `ln` is the natural logarithm with coefficient 1. In the uncapped upper branch, `r = exp(P-60)`: each additional point means an `exp(1)`-fold improvement in the physical objective. The factor is computed from the physical objective and its reference, not from a transformed reward or the old point score. Invalid output retains its existing rejection or zero score; the zero-cost exception applies only after validation. Use one score per file. Both reported ratio channels use points divided by 100. Across official files, take the arithmetic mean.
R is the greedy baseline objective; if zero, use the cheapest affordable singleton (ties by port index), as in the checker.
## Constraints
- `2 ≤ M ≤ 200000`
- `1 ≤ cost_i ≤ 10^9`
- `1 ≤ w_i ≤ 10^6`
- `1 ≤ d_i ≤ M`
- `1 ≤ B ≤ 10^18`
- At least one port satisfies `cost_i ≤ B`
The search space is exponential in `M`; exact optimization is not expected.
- Time limit: 8 seconds
- Memory limit: 1024 MB
---
## Example
Input
```text
6 5
3 2 2 4 1 3
5 1 4 2 3 6
1 2 1 1 2 1
```
Output
```text
3
1 2 4
```
Brief explanation:
- Chosen emitters: `{1,2,4}`
- Total cost: `2 + 2 + 1 = 5`, so the solution is feasible.
- Reflectors: `{0,3,5}`
Ordered emitter-reflector pairs generate residues with counts:
- residue `0`: 1 pair
- residue `1`: 3 pairs
- residue `2`: 1 pair
- residue `3`: 1 pair
- residue `4`: 2 pairs
- residue `5`: 1 pair
After applying saturation limits `d = [1,2,1,1,2,1]`, the objective is:
\[
5\cdot1 + 1\cdot2 + 4\cdot1 + 2\cdot1 + 3\cdot2 + 6\cdot1 = 25.
\]
For this instance, the baseline picks the same set, so r = U / R = 1 and the test score is 60 points.
结果
Kimi K3.1 ProKimi
max
61.1
平均得分 ↑
- 61.1采样 1 · 部分分
- 35.5k平均输出 token(含思考) ↓
- 30.0输出速度 tokens/s ↑
- 1184 秒延迟 ↓
Kimi K3.1 Pro(high)Kimi high
high
62.2
平均得分 ↑
- 62.2采样 1 · 部分分
- 22.1k平均输出 token(含思考) ↓
- 27.1输出速度 tokens/s ↑
- 817 秒延迟 ↓
DeepSeek Flash V4.1-1017DeepSeek
auto(high)
62.2
平均得分 ↑
- 62.2采样 1 · 部分分
- 41.0k平均输出 token(含思考) ↓
- 180.0输出速度 tokens/s ↑
- 228 秒延迟 ↓
MiniMax M3.1 FlashMiniMax
auto(max)
26.1
平均得分 ↑
- 26.1采样 1 · 部分分
- 76.7k平均输出 token(含思考) ↓
- 109.7输出速度 tokens/s ↑
- 700 秒延迟 ↓
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
Kimi K3.1 Pro · 采样 1 · 657 行
#include <bits/stdc++.h> using namespace std; using ll = long long; using cd = complex<double>; static constexpr double PI = 3.141592653589793238462643383279502884; struct FFT { int n; vector<int> rev; vector<cd> roots; explicit FFT(int m) { n = 1; while (n < 2 * m) n <<= 1; rev.resize(n); for (int i = 1; i < n; ++i) { rev[i] = (rev[i >> 1] >> 1) | ((i & 1) ? (n >> 1) : 0); } roots.resize(n / 2); for (int i = 0; i < n / 2; ++i) { double ang = -2.0 * PI * i / n; roots[i] = cd(cos(ang), sin(ang)); } } void transform(vector<cd>& a, bool inverse) const { for (int i = 0; i < n; ++i) { if (i < rev[i]) swap(a[i], a[rev[i]]); } for (int len = 2; len <= n; len <<= 1) { int step = n / len; int half = len >> 1; for (int i = 0; i < n; i += len) { for (int j = 0; j < half; ++j) { cd w = roots[j * step]; if (inverse) w = conj(w); cd u = a[i + j]; cd v = a[i + j + half] * w; a[i + j] = u + v; a[i + j + half] = u - v; } } } if (inverse) { double inv = 1.0 / n; for (cd& x : a) x *= inv; } } }; struct Solver { int M; ll budget; vector<ll> cost; vector<int> w, d; FFT fft; chrono::steady_clock::time_point startTime; mt19937 rng{123456789}; ll totalW = 0, upperBound = 0; vector<char> bestA; ll bestValue = -1, bestSpent = 0; vector<char> A; vector<int> c; ll value = 0, spent = 0; int k = 0; Solver(int m, ll b, const vector<ll>& co, const vector<int>& ww, const vector<int>& dd, chrono::steady_clock::time_point st) : M(m), budget(b), cost(co), w(ww), d(dd), fft(m), startTime(st) { for (int x : w) totalW += x; for (int i = 0; i < M; ++i) upperBound += (ll)w[i] * d[i]; } double elapsed() const { return chrono::duration<double>(chrono::steady_clock::now() - startTime).count(); } inline ll utility(int r, int cnt) const { return (ll)w[r] * min(cnt, d[r]); } void consider() { if (value > bestValue || (value == bestValue && spent < bestSpent)) { bestValue = value; bestSpent = spent; bestA = A; } } void computeCounts() { vector<int> S; bool useA = (k <= M - k); S.reserve(min(k, M - k)); for (int i = 0; i < M; ++i) { if ((bool)A[i] == useA) S.push_back(i); } int s = (int)S.size(); c.assign(M, s); if (s == 0) return; if (1LL * s * s <= 4LL * M || s <= 32) { for (int x : S) { for (int y : S) { --c[(x + y) % M]; } } return; } vector<cd> fa(fft.n, cd(0, 0)); for (int x : S) fa[x] = cd(1, 0); fft.transform(fa, false); vector<cd> y(fft.n); for (int i = 0; i < fft.n; ++i) y[i] = fa[i] * fa[i]; fft.transform(y, true); for (int r = 0; r < M; ++r) { ll t = llround(y[r].real() + y[r + M].real()); int cr = s - (int)t; if (cr < 0) cr = 0; if (cr > s) cr = s; c[r] = cr; } } void initializeState(const vector<char>& start) { A = start; spent = 0; k = 0; for (int i = 0; i < M; ++i) { if (A[i]) { ++k; spent += cost[i]; } } computeCounts(); value = 0; for (int r = 0; r < M; ++r) value += utility(r, c[r]); consider(); } ll flipPort(int p) { bool removing = A[p]; ll delta = 0; if (!removing) { for (int q = 0; q < M; ++q) { if (q == p) continue; int r = (p + q) % M; int dc = A[q] ? -1 : 1; int old = c[r]; int nw = old + dc; delta += utility(r, nw) - utility(r, old); c[r] = nw; } A[p] = 1; ++k; spent += cost[p]; } else { for (int q = 0; q < M; ++q) { if (q == p) continue; int r = (p + q) % M; int dc = A[q] ? 1 : -1; int old = c[r]; int nw = old + dc; delta += utility(r, nw) - utility(r, old); c[r] = nw; } A[p] = 0; --k; spent -= cost[p]; } value += delta; return delta; } void computeGains(vector<ll>& gain) { vector<ll> G(M), L(M); ll sumG = 0, sumL = 0; for (int r = 0; r < M; ++r) { G[r] = (c[r] < d[r]) ? w[r] : 0; L[r] = (c[r] <= d[r]) ? w[r] : 0; sumG += G[r]; sumL += L[r]; } bool useA = (k <= M - k); vector<int> S; S.reserve(min(k, M - k)); for (int i = 0; i < M; ++i) { if ((bool)A[i] == useA) S.push_back(i); } vector<ll> corrG(M, 0), corrL(M, 0); int s = (int)S.size(); if (1LL * s * M <= 3000000LL) { for (int p = 0; p < M; ++p) { ll sg = 0, sl = 0; for (int q : S) { int r = p + q; if (r >= M) r -= M; sg += G[r]; sl += L[r]; } corrG[p] = sg; corrL[p] = sl; } } else { vector<cd> fs(fft.n, cd(0, 0)), z(fft.n, cd(0, 0)); for (int q : S) fs[q] = cd(1, 0); for (int r = 0; r < M; ++r) { z[r] = cd((double)G[r], (double)L[r]); } fft.transform(fs, false); fft.transform(z, false); for (int i = 0; i < fft.n; ++i) z[i] = conj(fs[i]) * z[i]; fft.transform(z, true); for (int p = 0; p < M; ++p) { int other = fft.n + p - M; double sg = z[p].real() + z[other].real(); double sl = z[p].imag() + z[other].imag(); ll cg = llround(sg); ll cl = llround(sl); cg = max(0LL, min(sumG, cg)); cl = max(0LL, min(sumL, cl)); corrG[p] = cg; corrL[p] = cl; } } gain.resize(M); for (int p = 0; p < M; ++p) { int twice = (int)((2LL * p) % M); if (useA) { if (A[p]) { gain[p] = corrG[p] - G[twice] - (sumL - corrL[p]); } else { gain[p] = sumG - corrG[p] - G[twice] - corrL[p]; } } else { if (A[p]) { gain[p] = sumG - corrG[p] - G[twice] - corrL[p]; } else { gain[p] = corrG[p] - G[twice] - (sumL - corrL[p]); } } } } ll evaluateSwap(int rem, int add) { ll removeDelta = 0; for (int q = 0; q < M; ++q) { if (q == rem) continue; int r = (rem + q) % M; int dc = A[q] ? 1 : -1; int old = c[r]; int nw = old + dc; removeDelta += utility(r, nw) - utility(r, old); c[r] = nw; } A[rem] = 0; ll addDelta = 0; for (int q = 0; q < M; ++q) { if (q == add) continue; int r = (add + q) % M; if (A[q]) { if (c[r] <= d[r]) addDelta -= w[r]; } else { if (c[r] < d[r]) addDelta += w[r]; } } for (int q = 0; q < M; ++q) { if (q == rem) continue; int r = (rem + q) % M; int dc = A[q] ? 1 : -1; c[r] -= dc; } A[rem] = 1; return removeDelta + addDelta; } pair<int, int> findSwap(const vector<ll>& gain, double deadline) { if (k == 0 || k == M) return {-1, -1}; ll maxCostA = 0; vector<int> additions; additions.reserve(M - k); for (int i = 0; i < M; ++i) { if (A[i]) maxCostA = max(maxCostA, cost[i]); } for (int j = 0; j < M; ++j) { if (!A[j] && spent - maxCostA + cost[j] <= budget) { additions.push_back(j); } } if (additions.empty()) return {-1, -1}; sort(additions.begin(), additions.end(), [&](int x, int y) { if (gain[x] != gain[y]) return gain[x] > gain[y]; if (cost[x] != cost[y]) return cost[x] < cost[y]; return x < y; }); if ((int)additions.size() > 28) additions.resize(28); struct Candidate { ll approximate; int rem, add; }; vector<Candidate> candidates; for (int add : additions) { int best1 = -1, best2 = -1; for (int rem = 0; rem < M; ++rem) { if (!A[rem]) continue; if (spent - cost[rem] + cost[add] > budget) continue; auto better = [&](int x, int y) { if (y == -1) return true; if (gain[x] != gain[y]) return gain[x] > gain[y]; return cost[x] > cost[y]; }; if (better(rem, best1)) { best2 = best1; best1 = rem; } else if (better(rem, best2)) { best2 = rem; } } if (best1 != -1) { candidates.push_back({gain[best1] + gain[add], best1, add}); } if (best2 != -1) { candidates.push_back({gain[best2] + gain[add], best2, add}); } } sort(candidates.begin(), candidates.end(), [](const Candidate& x, const Candidate& y) { return x.approximate > y.approximate; }); ll bestDelta = 0; pair<int, int> answer{-1, -1}; int evaluated = 0; for (const Candidate& cand : candidates) { if (evaluated >= 24) break; if ((evaluated & 3) == 0 && elapsed() >= deadline) break; ++evaluated; ll delta = evaluateSwap(cand.rem, cand.add); if (delta > bestDelta) { bestDelta = delta; answer = {cand.rem, cand.add}; } } return answer; } void localSearch(const vector<char>& start, double deadline) { if (elapsed() >= deadline) return; initializeState(start); if (bestValue >= upperBound) return; vector<ll> gain; while (elapsed() < deadline) { computeGains(gain); int bestPort = -1; ll bestGain = 0; for (int p = 0; p < M; ++p) { if (!A[p] && spent + cost[p] > budget) continue; ll g = gain[p]; if (g <= 0) continue; bool take = false; if (g > bestGain) { take = true; } else if (g == bestGain && bestPort != -1) { bool curRemove = A[p], oldRemove = A[bestPort]; if (curRemove != oldRemove) { take = !curRemove; } else if (!curRemove) { take = cost[p] < cost[bestPort]; } else { take = cost[p] > cost[bestPort]; } } else if (bestPort == -1) { take = true; } if (take) { bestGain = g; bestPort = p; } } if (bestPort != -1) { ll oldValue = value; flipPort(bestPort); if (value <= oldValue) { flipPort(bestPort); return; } consider(); if (bestValue >= upperBound) return; continue; } pair<int, int> sw = findSwap(gain, deadline); if (sw.first == -1) return; ll oldValue = value; flipPort(sw.first); flipPort(sw.second); if (value <= oldValue) { flipPort(sw.second); flipPort(sw.first); return; } consider(); if (bestValue >= upperBound) return; } } void printAnswer() const { int count = 0; for (char x : bestA) count += x; cout << count << '\n'; bool first = true; for (int i = 0; i < M; ++i) { if (bestA[i]) { if (!first) cout << ' '; first = false; cout << i; } } if (count) cout << '\n'; } void run() { const double hardDeadline = 7.15; vector<char> singleA(M, 0); int singlePort = -1; ll singleValue = -1; for (int p = 0; p < M; ++p) { if (cost[p] > budget) continue; ll v = totalW - w[(2LL * p) % M]; if (v > singleValue || (v == singleValue && (singlePort == -1 || cost[p] < cost[singlePort]))) { singleValue = v; singlePort = p; } } singleA[singlePort] = 1; bestA = singleA; bestValue = singleValue; bestSpent = cost[singlePort]; if (bestValue >= upperBound) { printAnswer(); return; } vector<int> baseOrder(M); iota(baseOrder.begin(), baseOrder.end(), 0); sort(baseOrder.begin(), baseOrder.end(), [&](int x, int y) { if (cost[x] != cost[y]) return cost[x] < cost[y]; return x < y; }); vector<char> baseline(M, 0); ll baselineSpent = 0; for (int p : baseOrder) { if (baselineSpent + cost[p] <= budget) { baseline[p] = 1; baselineSpent += cost[p]; } } localSearch(baseline, hardDeadline); if (bestValue >= upperBound || elapsed() >= hardDeadline) { printAnswer(); return; } double now = elapsed(); double remaining = hardDeadline - now; if (remaining > 0.08) { localSearch(singleA, now + remaining * 0.45); } int restart = 0; while (bestValue < upperBound && elapsed() + 0.05 < hardDeadline) { vector<char> start; ll startSpent = 0; if ((restart & 1) == 0) { start = bestA; vector<int> emitters; for (int i = 0; i < M; ++i) { if (start[i]) { emitters.push_back(i); startSpent += cost[i]; } } if ((int)emitters.size() > 1) { shuffle(emitters.begin(), emitters.end(), rng); int maxRemove = min((int)emitters.size() - 1, 6); int removeCount = 1 + (int)(rng() % maxRemove); for (int t = 0; t < removeCount; ++t) { int p = emitters[t]; start[p] = 0; startSpent -= cost[p]; } } else if ((rng() & 1) && M > 1) { for (int attempt = 0; attempt < 40; ++attempt) { int p = rng() % M; if (cost[p] <= budget) { fill(start.begin(), start.end(), 0); start[p] = 1; startSpent = cost[p]; break; } } } int randomAdds = rng() % 3; int added = 0; for (int attempt = 0; attempt < 40 && added < randomAdds; ++attempt) { int p = rng() % M; if (!start[p] && startSpent + cost[p] <= budget) { start[p] = 1; startSpent += cost[p]; ++added; } } } else { vector<int> order = baseOrder; int block = 16 + rng() % 32; for (int l = 0; l < M; l += block) { int r = min(M, l + block); shuffle(order.begin() + l, order.begin() + r, rng); } start.assign(M, 0); for (int p : order) { if (startSpent + cost[p] <= budget) { start[p] = 1; startSpent += cost[p]; } } } now = elapsed(); remaining = hardDeadline - now; if (remaining <= 0.04) break; double slice = min(remaining, M > 50000 ? 0.85 : 0.45); localSearch(start, now + slice); ++restart; } printAnswer(); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); auto programStart = chrono::steady_clock::now(); int M; ll budget; cin >> M >> budget; vector<ll> cost(M); vector<int> w(M), d(M); for (ll& x : cost) cin >> x; for (int& x : w) cin >> x; for (int& x : d) cin >> x; if (M <= 20) { int total = 1 << M; vector<ll> costSum(total, 0); ll bestValue = -1; int bestMask = 0; vector<int> c(M); for (int mask = 1; mask < total; ++mask) { int bit = __builtin_ctz(mask); int previous = mask & (mask - 1); costSum[mask] = costSum[previous] + cost[bit]; if (costSum[mask] > budget) continue; fill(c.begin(), c.end(), 0); for (int i = 0; i < M; ++i) { if (!(mask & (1 << i))) continue; for (int j = 0; j < M; ++j) { if (!(mask & (1 << j))) { ++c[(i + j) % M]; } } } ll value = 0; for (int r = 0; r < M; ++r) { value += (ll)w[r] * min(c[r], d[r]); } if (value > bestValue) { bestValue = value; bestMask = mask; } } int count = __builtin_popcount((unsigned)bestMask); cout << count << '\n'; bool first = true; for (int i = 0; i < M; ++i) { if (bestMask & (1 << i)) { if (!first) cout << ' '; first = false; cout << i; } } if (count) cout << '\n'; return 0; } Solver solver(M, budget, cost, w, d, programStart); solver.run(); return 0; }
Kimi K3.1 Pro(high) · 采样 1 · 547 行
#include <bits/stdc++.h> using namespace std; using int64 = long long; using i128 = __int128_t; static const int MOD = 998244353; static const int G = 3; static int modpow(int a, int64 e) { int64 r = 1, x = a; while (e) { if (e & 1) r = r * x % MOD; x = x * x % MOD; e >>= 1; } return (int)r; } static void ntt(vector<int>& a, bool invert) { int n = (int)a.size(); for (int i = 1, j = 0; i < n; ++i) { int bit = n >> 1; for (; j & bit; bit >>= 1) j ^= bit; j ^= bit; if (i < j) swap(a[i], a[j]); } for (int len = 2; len <= n; len <<= 1) { int wlen = modpow(G, (MOD - 1) / len); if (invert) wlen = modpow(wlen, MOD - 2); int half = len >> 1; for (int i = 0; i < n; i += len) { int64 w = 1; for (int j = 0; j < half; ++j) { int u = a[i + j]; int v = (int)(a[i + j + half] * w % MOD); int x = u + v; if (x >= MOD) x -= MOD; int y = u - v; if (y < 0) y += MOD; a[i + j] = x; a[i + j + half] = y; w = w * wlen % MOD; } } } if (invert) { int inv = modpow(n, MOD - 2); for (int& x : a) x = (int)((int64)x * inv % MOD); } } struct RNG { uint64_t x = 0x9e3779b97f4a7c15ULL; uint64_t next() { x ^= x << 7; x ^= x >> 9; x ^= x << 8; return x; } int get(int n) { return (int)(next() % (uint64_t)n); } double uniform() { return (next() >> 11) * (1.0 / 9007199254740992.0); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); auto startTime = chrono::steady_clock::now(); auto elapsed = [&]() -> double { return chrono::duration<double>(chrono::steady_clock::now() - startTime).count(); }; const double TIME_LIMIT = 7.55; int M; int64 budget; if (!(cin >> M >> budget)) return 0; vector<int64> cost(M); vector<int> w(M), d(M); for (auto& x : cost) cin >> x; for (auto& x : w) cin >> x; for (auto& x : d) cin >> x; int64 totalCost = 0, totalW = 0, absoluteUpper = 0; for (int i = 0; i < M; ++i) { totalCost += cost[i]; totalW += w[i]; absoluteUpper += (int64)w[i] * d[i]; } vector<int> costOrder(M); iota(costOrder.begin(), costOrder.end(), 0); sort(costOrder.begin(), costOrder.end(), [&](int a, int b) { if (cost[a] != cost[b]) return cost[a] < cost[b]; return a < b; }); int n = 0; int64 prefixCost = 0; while (n < M && (i128)prefixCost + cost[costOrder[n]] <= (i128)budget) { prefixCost += cost[costOrder[n]]; ++n; } int64 lowInterval, highInterval; if (budget >= totalCost) { lowInterval = totalCost; highInterval = 0; } else { lowInterval = budget; highInterval = totalCost - budget; } auto feasibleSum = [&](int64 s) { return s <= lowInterval || s >= highInterval; }; RNG rng; int64 bestValue = -1; vector<int> bestS; auto consider = [&](const vector<int>& S, int64 value) { if (value > bestValue) { bestValue = value; bestS = S; } }; // Empty set and the best feasible singleton in the symmetric formulation. consider({}, 0); for (int p = 0; p < M; ++p) { if (!feasibleSum(cost[p])) continue; int r = 2 * p; if (r >= M) r -= M; int64 value = totalW - w[r]; if (value > bestValue) { bestValue = value; bestS = {p}; } } auto buildPairSums = [&](const vector<int>& S, vector<int>& sums) { int h = (int)S.size(); sums.assign(M, 0); if (h == 0) return; if ((int64)h * h <= 6000000LL) { for (int i = 0; i < h; ++i) { int self = 2 * S[i]; if (self >= M) self -= M; ++sums[self]; for (int j = i + 1; j < h; ++j) { int r = S[i] + S[j]; if (r >= M) r -= M; sums[r] += 2; } } return; } int need = 2 * M - 1; int len = 1; while (len < need) len <<= 1; vector<int> fa(len, 0); for (int x : S) fa[x] = 1; ntt(fa, false); for (int& x : fa) x = (int)((int64)x * x % MOD); ntt(fa, true); for (int i = 0; i < need; ++i) { int r = i % M; sums[r] += fa[i]; } }; auto objectiveFromSums = [&](int h, const vector<int>& sums) { int64 value = 0; for (int r = 0; r < M; ++r) { int c = h - sums[r]; value += (int64)w[r] * min(c, d[r]); } return value; }; auto evaluateSet = [&](const vector<int>& S) { vector<int> sums; buildPairSums(S, sums); return objectiveFromSums((int)S.size(), sums); }; // Preserve and improve the deterministic baseline. int baselineH = min(n, M - n); vector<int> baselineS; if (baselineH > 0) { vector<char> inBaselineA(M, 0); if (n <= M / 2) { baselineS.reserve(n); for (int i = 0; i < n; ++i) baselineS.push_back(costOrder[i]); } else { for (int i = 0; i < n; ++i) inBaselineA[costOrder[i]] = 1; for (int i = 0; i < M; ++i) { if (!inBaselineA[i]) baselineS.push_back(i); } } consider(baselineS, evaluateSet(baselineS)); } if (bestValue == absoluteUpper) { // Output is handled below; no further search is necessary. } else { int hMax = (n < M / 2 ? n : M / 2); // Forward greedy construction, especially effective for small active sets. int greedyLimit = min(hMax, 300); vector<int> greedyS; vector<int> pos(M, -1), sums(M, 0), deltaS(M, 0); int64 greedySumCost = 0; int64 greedyValue = 0; int64 greedyBestValue = -1; vector<int> greedyBestS; vector<int> touched; touched.reserve(1024); for (int step = 0; step < greedyLimit; ++step) { if ((step & 7) == 0 && elapsed() > TIME_LIMIT - 1.0) break; int h = (int)greedyS.size(); int64 unsaturatedWeight = 0; for (int r = 0; r < M; ++r) { int c = h - sums[r]; if (c < d[r]) unsaturatedWeight += w[r]; } vector<int> candidates; if (h <= 5 || M <= 256) { candidates.reserve(M - h); for (int p = 0; p < M; ++p) { if (pos[p] < 0) candidates.push_back(p); } } else { candidates.reserve(220); for (int i = 0; i < 24; ++i) { int p = costOrder[i]; if (pos[p] < 0) candidates.push_back(p); } for (int i = 0; i < 12; ++i) { int p = costOrder[M - 1 - i]; if (pos[p] < 0) candidates.push_back(p); } for (int i = 0; i < 160; ++i) { int p = rng.get(M); if (pos[p] < 0) candidates.push_back(p); } for (int i = 0; i < M; ++i) { int p = costOrder[i]; if (pos[p] < 0 && feasibleSum(greedySumCost + cost[p])) { candidates.push_back(p); break; } } } int bestP = -1; int64 bestDelta = numeric_limits<int64>::min(); for (int p : candidates) { if (pos[p] >= 0) continue; int64 newSumCost = greedySumCost + cost[p]; if (!feasibleSum(newSumCost)) continue; touched.clear(); auto addDelta = [&](int r, int v) { if (deltaS[r] == 0) touched.push_back(r); deltaS[r] += v; }; for (int a : greedyS) { int r = p + a; if (r >= M) r -= M; addDelta(r, 2); } int self = 2 * p; if (self >= M) self -= M; addDelta(self, 1); int64 change = unsaturatedWeight; for (int r : touched) { int c = h - sums[r]; int globallyChanged = c + 1; int actuallyChanged = c + 1 - deltaS[r]; change += (int64)w[r] * (min(actuallyChanged, d[r]) - min(globallyChanged, d[r])); } for (int r : touched) deltaS[r] = 0; if (change > bestDelta || (change == bestDelta && bestP >= 0 && cost[p] < cost[bestP])) { bestDelta = change; bestP = p; } } if (bestP < 0) break; for (int a : greedyS) { int r = bestP + a; if (r >= M) r -= M; sums[r] += 2; } int self = 2 * bestP; if (self >= M) self -= M; ++sums[self]; pos[bestP] = (int)greedyS.size(); greedyS.push_back(bestP); greedySumCost += cost[bestP]; ++h; greedyValue = objectiveFromSums(h, sums); if (greedyValue > greedyBestValue) { greedyBestValue = greedyValue; greedyBestS = greedyS; } consider(greedyS, greedyValue); if (bestValue == absoluteUpper) break; } if (bestValue != absoluteUpper && hMax > 0 && elapsed() < TIME_LIMIT - 0.7) { vector<int> weightOrder(M); iota(weightOrder.begin(), weightOrder.end(), 0); sort(weightOrder.begin(), weightOrder.end(), [&](int a, int b) { if (w[a] != w[b]) return w[a] > w[b]; return a < b; }); auto upperBound = [&](int h) { int64 remaining = (int64)h * (M - h); int64 result = 0; for (int r : weightOrder) { if (remaining == 0) break; int64 take = min<int64>(min(d[r], h), remaining); result += (int64)w[r] * take; remaining -= take; } return result; }; vector<pair<int64, int>> rankedH; vector<int> grid; for (int i = 1; i <= 30; ++i) { int h = (int)((int64)hMax * i / 30); if (h > 0) grid.push_back(h); } grid.push_back(hMax); grid.push_back(max(1, hMax / 2)); grid.push_back(max(1, hMax / 4)); if (baselineH > 0) grid.push_back(baselineH); sort(grid.begin(), grid.end()); grid.erase(unique(grid.begin(), grid.end()), grid.end()); for (int h : grid) rankedH.push_back({upperBound(h), h}); sort(rankedH.begin(), rankedH.end(), greater<pair<int64, int>>()); struct Spec { int h; vector<int> initial; bool randomize; }; vector<Spec> specs; auto addSpec = [&](int h, const vector<int>& initial, bool randomize) { if (h <= 1 || h > hMax || (int)specs.size() >= 6) return; for (const auto& s : specs) if (s.h == h) return; specs.push_back({h, initial, randomize}); }; if (baselineH > 1) addSpec(baselineH, baselineS, false); if (!greedyBestS.empty() && (int)greedyBestS.size() > 1) { addSpec((int)greedyBestS.size(), greedyBestS, false); } for (auto [ub, h] : rankedH) { if ((int)specs.size() >= 6) break; addSpec(h, {}, true); } vector<int> workDelta(M, 0); for (int specIndex = 0; specIndex < (int)specs.size(); ++specIndex) { if (bestValue == absoluteUpper) break; if (elapsed() > TIME_LIMIT - 0.45) break; int h = specs[specIndex].h; vector<int> S = specs[specIndex].initial; if (S.empty()) { S.reserve(h); if (h <= n) { for (int i = 0; i < h; ++i) S.push_back(costOrder[i]); } else { for (int i = M - h; i < M; ++i) S.push_back(costOrder[i]); } } vector<int> pos2(M, -1); int64 sumCost = 0; for (int i = 0; i < h; ++i) { pos2[S[i]] = i; sumCost += cost[S[i]]; } if (specs[specIndex].randomize) { int randomSteps = min(1000000, 2 * h + 100); for (int it = 0; it < randomSteps; ++it) { int pi = rng.get(h); int p = S[pi]; int q = rng.get(M); if (pos2[q] >= 0) continue; int64 ns = sumCost - cost[p] + cost[q]; if (!feasibleSum(ns)) continue; S[pi] = q; pos2[p] = -1; pos2[q] = pi; sumCost = ns; } } vector<int> pairSums; buildPairSums(S, pairSums); int64 currentValue = objectiveFromSums(h, pairSums); consider(S, currentValue); if (bestValue == absoluteUpper) break; int iterations = (int)min<int64>(180000, max<int64>(140, 14000000LL / max(1, 2 * h + 8))); double averageW = (double)totalW / M; vector<int> touched2; touched2.reserve(min(M, 2 * h + 4)); for (int it = 0; it < iterations; ++it) { if ((it & 127) == 0 && elapsed() > TIME_LIMIT - 0.12) break; bool evaluated = false; int p = -1, q = -1; int64 newSumCost = 0; for (int attempt = 0; attempt < 8 && !evaluated; ++attempt) { p = S[rng.get(h)]; q = rng.get(M); if (pos2[q] >= 0) continue; newSumCost = sumCost - cost[p] + cost[q]; if (!feasibleSum(newSumCost)) continue; evaluated = true; } if (!evaluated) continue; touched2.clear(); auto addDelta = [&](int r, int v) { if (workDelta[r] == 0) touched2.push_back(r); workDelta[r] += v; }; for (int a : S) { if (a == p) continue; int r1 = p + a; if (r1 >= M) r1 -= M; int r2 = q + a; if (r2 >= M) r2 -= M; addDelta(r1, -2); addDelta(r2, 2); } int selfP = 2 * p; if (selfP >= M) selfP -= M; int selfQ = 2 * q; if (selfQ >= M) selfQ -= M; addDelta(selfP, -1); addDelta(selfQ, 1); int64 change = 0; for (int r : touched2) { int oldC = h - pairSums[r]; int newC = h - (pairSums[r] + workDelta[r]); change += (int64)w[r] * (min(newC, d[r]) - min(oldC, d[r])); } double progress = (double)it / max(1, iterations - 1); double temperature = averageW * (0.70 * (1.0 - progress) + 0.0005); bool accept = change >= 0; if (!accept && temperature > 0) { accept = rng.uniform() < exp((double)change / temperature); } if (accept) { for (int r : touched2) pairSums[r] += workDelta[r]; int pi = pos2[p]; S[pi] = q; pos2[p] = -1; pos2[q] = pi; sumCost = newSumCost; currentValue += change; if (currentValue > bestValue) consider(S, currentValue); } for (int r : touched2) workDelta[r] = 0; if (bestValue == absoluteUpper) break; } } } } if (bestS.empty() && bestValue <= 0) { // A feasible singleton always exists; this is only a defensive fallback. for (int p : costOrder) { if ((i128)cost[p] <= (i128)budget) { bestS = {p}; break; } } } int64 bestSumCost = 0; for (int p : bestS) bestSumCost += cost[p]; vector<int> answer; if ((i128)bestSumCost <= (i128)budget) { answer = bestS; } else { vector<char> inS(M, 0); for (int p : bestS) inS[p] = 1; for (int i = 0; i < M; ++i) if (!inS[i]) answer.push_back(i); } sort(answer.begin(), answer.end()); i128 verifyCost = 0; for (int p : answer) verifyCost += cost[p]; if (verifyCost > (i128)budget) { answer.clear(); for (int i = 0; i < n; ++i) answer.push_back(costOrder[i]); sort(answer.begin(), answer.end()); } cout << answer.size() << '\n'; if (!answer.empty()) { for (int i = 0; i < (int)answer.size(); ++i) { if (i) cout << ' '; cout << answer[i]; } cout << '\n'; } return 0; }
DeepSeek Flash V4.1-1017 · 采样 1 · 475 行
#include <bits/stdc++.h> using namespace std; using cd = complex<double>; const double PI = acos(-1.0); const int SHIFT = 2000000; int M; long long B; vector<long long> cost; vector<int> w, d; int L; vector<int> revArr; void fft(vector<cd>& a, bool invert) { int n = (int)a.size(); if ((int)revArr.size() != n) { revArr.assign(n, 0); int lg = __builtin_ctz(n); for (int i = 0; i < n; i++) { int x = i, r = 0; for (int j = 0; j < lg; j++) { r = (r << 1) | (x & 1); x >>= 1; } revArr[i] = r; } } for (int i = 0; i < n; i++) { if (i < revArr[i]) swap(a[i], a[revArr[i]]); } for (int len = 2; len <= n; len <<= 1) { double ang = 2.0 * PI / len * (invert ? -1.0 : 1.0); cd wlen(cos(ang), sin(ang)); for (int i = 0; i < n; i += len) { cd w(1.0, 0.0); for (int j = 0; j < len / 2; j++) { cd u = a[i + j]; cd v = a[i + j + len / 2] * w; a[i + j] = u + v; a[i + j + len / 2] = u - v; w *= wlen; } } } if (invert) { for (cd& x : a) x /= n; } } pair<vector<int>, long long> computeC(const vector<char>& A) { int n = 0; for (int i = 0; i < M; i++) n += A[i]; vector<cd> fa(L); for (int i = 0; i < M; i++) { fa[i] = A[i] ? 1.0 : 0.0; } fft(fa, false); for (int i = 0; i < L; i++) fa[i] *= fa[i]; fft(fa, true); vector<int> c(M); long long U = 0; for (int r = 0; r < M; r++) { long long q = llround(fa[r].real()); if (r + M < 2 * M - 1) { q += llround(fa[r + M].real()); } c[r] = n - (int)q; int sat = min(c[r], d[r]); U += 1LL * w[r] * sat; } return {move(c), U}; } void applyToggle(vector<char>& A, vector<int>& c, int& n, long long& costSum, int x, bool add) { if (add) { for (int t = 0; t < M; t++) { if (t == x) continue; int r = t + x; if (r >= M) r -= M; int delta = A[t] ? -1 : 1; c[r] += delta; } A[x] = 1; n++; costSum += cost[x]; } else { for (int t = 0; t < M; t++) { if (t == x) continue; int r = t + x; if (r >= M) r -= M; int delta = A[t] ? 1 : -1; c[r] += delta; } A[x] = 0; n--; costSum -= cost[x]; } } void improveExact(vector<char>& A, vector<int>& c, int& n, long long& costSum, bool constrained, int maxIter) { vector<cd> fa(L), fb(L); vector<int> U(M), D(M); for (int iter = 0; iter < maxIter; iter++) { fill(fa.begin(), fa.end(), cd(0.0, 0.0)); fill(fb.begin(), fb.end(), cd(0.0, 0.0)); long long sumU = 0, sumD = 0; for (int i = 0; i < M; i++) { int ui = (c[i] < d[i]) ? w[i] : 0; int di = (c[i] <= d[i]) ? -w[i] : 0; U[i] = ui; D[i] = di; sumU += ui; sumD += di; int v = di - ui + SHIFT; int idx = (M - i) % M; fa[i] = A[idx] ? 1.0 : 0.0; fb[i] = v; } fft(fa, false); fft(fb, false); for (int i = 0; i < L; i++) fa[i] *= fb[i]; fft(fa, true); long long bestDelta = 0; int bestX = -1; bool bestAdd = false; for (int x = 0; x < M; x++) { long long lin = llround(fa[x].real()); if (x + M < 2 * M - 1) { lin += llround(fa[x + M].real()); } long long conv = lin - 1LL * SHIFT * n; int u2 = U[(2 * x) % M]; long long delta; if (!A[x]) { delta = sumU + conv - u2; if (constrained && costSum + cost[x] > B) continue; } else { delta = sumD - conv - u2; } if (delta > bestDelta) { bestDelta = delta; bestX = x; bestAdd = !A[x]; } } if (bestX == -1 || bestDelta <= 0) break; applyToggle(A, c, n, costSum, bestX, bestAdd); } } long long evalSwap(const vector<char>& A, const vector<int>& c, int x, int y) { long long delta = 0; int x2 = (2 * x) % M; int y2 = (2 * y) % M; for (int r = 0; r < M; r++) { int cval = c[r]; int t1 = r - x; if (t1 < 0) t1 += M; int d1; if (r == x2) d1 = 0; else d1 = A[t1] ? 1 : -1; int c1 = cval + d1; int t2 = r - y; if (t2 < 0) t2 += M; int d2; if (r == y2) { d2 = 0; } else { int a1 = (t2 == x) ? 0 : A[t2]; d2 = a1 ? -1 : 1; } int c2 = c1 + d2; int sat0 = min(cval, d[r]); int sat2 = min(c2, d[r]); delta += 1LL * w[r] * (sat2 - sat0); } return delta; } void stochasticSwaps(vector<char>& A, vector<int>& c, int& n, long long& costSum, bool constrained, int maxIter, int samples, mt19937& rng) { vector<int> selected, unselected; selected.reserve(n); unselected.reserve(M - n); vector<int> posSel(M, -1), posUn(M, -1); for (int i = 0; i < M; i++) { if (A[i]) { posSel[i] = (int)selected.size(); selected.push_back(i); } else { posUn[i] = (int)unselected.size(); unselected.push_back(i); } } for (int iter = 0; iter < maxIter; iter++) { if (selected.empty() || unselected.empty()) break; long long bestDelta = 0; int bestX = -1, bestY = -1; for (int s = 0; s < samples; s++) { int x = selected[rng() % selected.size()]; int y = -1; for (int tries = 0; tries < 10; tries++) { int cand = unselected[rng() % unselected.size()]; if (!constrained || costSum - cost[x] + cost[cand] <= B) { y = cand; break; } } if (y == -1) continue; long long delta = evalSwap(A, c, x, y); if (delta > bestDelta) { bestDelta = delta; bestX = x; bestY = y; } } if (bestDelta <= 0 || bestX == -1) break; applyToggle(A, c, n, costSum, bestX, false); applyToggle(A, c, n, costSum, bestY, true); int ix = posSel[bestX]; int last = selected.back(); selected[ix] = last; posSel[last] = ix; selected.pop_back(); posSel[bestX] = -1; int iy = posUn[bestY]; int lastU = unselected.back(); unselected[iy] = lastU; posUn[lastU] = iy; unselected.pop_back(); posUn[bestY] = -1; posUn[bestX] = (int)unselected.size(); unselected.push_back(bestX); posSel[bestY] = (int)selected.size(); selected.push_back(bestY); } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> M >> B; cost.resize(M); w.resize(M); d.resize(M); for (int i = 0; i < M; i++) cin >> cost[i]; for (int i = 0; i < M; i++) cin >> w[i]; for (int i = 0; i < M; i++) cin >> d[i]; long long totalCost = 0; for (int i = 0; i < M; i++) totalCost += cost[i]; bool unconstrained = ((__int128)2 * B >= totalCost); L = 1; while (L < 2 * M) L <<= 1; vector<int> order(M); iota(order.begin(), order.end(), 0); sort(order.begin(), order.end(), [&](int a, int b) { if (cost[a] != cost[b]) return cost[a] < cost[b]; return a < b; }); vector<long long> pref(M + 1, 0); for (int i = 0; i < M; i++) { pref[i + 1] = pref[i] + cost[order[i]]; } vector<char> A_base(M, 0); long long sumBase = 0; for (int idx : order) { if (sumBase + cost[idx] <= B) { A_base[idx] = 1; sumBase += cost[idx]; } } long long bestU = -1; vector<char> bestA; vector<int> bestC; long long bestCostSum = 0; auto consider = [&](vector<char> A, long long costSum) { int n = 0; for (char x : A) n += x; if (unconstrained && n > M / 2) { for (int i = 0; i < M; i++) A[i] ^= 1; n = M - n; costSum = totalCost - costSum; } auto res = computeC(A); vector<int> c = move(res.first); long long U = res.second; if (U > bestU) { bestU = U; bestA = A; bestC = move(c); bestCostSum = costSum; } }; consider(A_base, sumBase); auto addPrefix = [&](int n) { if (n < 0) n = 0; if (n > M) n = M; if (!unconstrained && pref[n] > B) return; vector<char> A(M, 0); for (int i = 0; i < n; i++) A[order[i]] = 1; consider(A, pref[n]); }; vector<int> targets; targets.push_back(1); targets.push_back(M / 10); targets.push_back(M / 5); targets.push_back(M / 3); targets.push_back(M / 2); if (unconstrained) { targets.push_back(2 * M / 3); targets.push_back(4 * M / 5); } else { int Kmax = int(upper_bound(pref.begin(), pref.end(), B) - pref.begin()) - 1; targets.push_back(Kmax); targets.push_back(Kmax / 2); targets.push_back(Kmax * 3 / 4); } sort(targets.begin(), targets.end()); targets.erase(unique(targets.begin(), targets.end()), targets.end()); for (int n : targets) addPrefix(n); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int randReps = unconstrained ? 8 : 5; for (int rep = 0; rep < randReps; rep++) { vector<char> A(M, 0); long long cs = 0; if (unconstrained) { for (int i = 0; i < M; i++) { A[i] = (rng() & 1); if (A[i]) cs += cost[i]; } } else { vector<int> idx(M); iota(idx.begin(), idx.end(), 0); shuffle(idx.begin(), idx.end(), rng); for (int i : idx) { if (cs + cost[i] <= B) { A[i] = 1; cs += cost[i]; } } } consider(A, cs); } vector<char> A = bestA; vector<int> c = bestC; int n = 0; for (char x : A) n += x; long long costSum = bestCostSum; improveExact(A, c, n, costSum, !unconstrained, 15); stochasticSwaps(A, c, n, costSum, !unconstrained, 100, 6, rng); improveExact(A, c, n, costSum, !unconstrained, 8); stochasticSwaps(A, c, n, costSum, !unconstrained, 50, 6, rng); improveExact(A, c, n, costSum, !unconstrained, 5); vector<int> ans; if (unconstrained) { if (costSum <= B) { for (int i = 0; i < M; i++) { if (A[i]) ans.push_back(i); } } else { for (int i = 0; i < M; i++) { if (!A[i]) ans.push_back(i); } } } else { if (costSum > B) { for (int i = 0; i < M; i++) { if (A_base[i]) ans.push_back(i); } } else { for (int i = 0; i < M; i++) { if (A[i]) ans.push_back(i); } } } cout << ans.size() << '\n'; if (!ans.empty()) { for (int i = 0; i < (int)ans.size(); i++) { if (i) cout << ' '; cout << ans[i]; } cout << '\n'; } return 0; }
MiniMax M3.1 Flash · 采样 1 · 931 行
#include <bits/stdc++.h> using namespace std; using int64 = long long; using uint8 = unsigned char; static constexpr int MOD = 998244353; static constexpr int PRIMITIVE_ROOT = 3; int modPow(int a, int e) { int64 result = 1; while (e > 0) { if (e & 1) result = result * a % MOD; a = int64(a) * a % MOD; e >>= 1; } return int(result); } struct NTT { int n; vector<int> rev, roots; explicit NTT(int n_) : n(n_), rev(n), roots(n) { for (int i = 1; i < n; ++i) { rev[i] = (rev[i >> 1] >> 1) | ((i & 1) ? (n >> 1) : 0); } for (int len = 1; len < n; len <<= 1) { int w = modPow(PRIMITIVE_ROOT, (MOD - 1) / (len << 1)); roots[len] = 1; for (int j = 1; j < len; ++j) { roots[len + j] = int64(roots[len + j - 1]) * w % MOD; } } } void transform(vector<int>& a, bool inverse) const { if (inverse) { reverse(a.begin() + 1, a.end()); } for (int i = 0; i < n; ++i) { if (i < rev[i]) swap(a[i], a[rev[i]]); } for (int len = 1; len < n; len <<= 1) { for (int i = 0; i < n; i += len << 1) { for (int j = 0; j < len; ++j) { int u = a[i + j]; int v = int64(a[i + j + len]) * roots[len + j] % MOD; int x = u + v; if (x >= MOD) x -= MOD; int y = u - v; if (y < 0) y += MOD; a[i + j] = x; a[i + j + len] = y; } } } if (inverse) { int invN = modPow(n, MOD - 2); for (int& x : a) x = int64(x) * invN % MOD; } } }; struct State { vector<uint8> inA; vector<int> A, R; vector<int> posA, posR; vector<int> c; int64 U = 0; int64 totalCost = 0; }; int M; int64 budget; vector<int> cost, w, d; vector<int> costOrder; int64 sumW = 0; int64 totalCostAll = 0; unique_ptr<NTT> ntt; chrono::steady_clock::time_point deadline; inline int addMod(int a, int b) { int r = a + b; if (r >= M) r -= M; return r; } bool betterState(const State& a, const State& b) { if (a.U != b.U) return a.U > b.U; if (a.totalCost != b.totalCost) return a.totalCost < b.totalCost; return a.A.size() < b.A.size(); } void updateGlobal(State& candidate, State& global) { if (betterState(candidate, global)) global = candidate; } State makeState(const vector<int>& chosen, vector<int> counts) { State s; s.inA.assign(M, 0); s.posA.assign(M, -1); s.posR.assign(M, -1); s.c = move(counts); for (int x : chosen) { s.inA[x] = 1; s.totalCost += cost[x]; } for (int i = 0; i < M; ++i) { if (s.inA[i]) { s.posA[i] = int(s.A.size()); s.A.push_back(i); } else { s.posR[i] = int(s.R.size()); s.R.push_back(i); } } s.U = 0; for (int r = 0; r < M; ++r) { s.U += int64(w[r]) * min(s.c[r], d[r]); } return s; } vector<int> countsFromChosen(const vector<int>& chosen) { vector<int> f(ntt->n, 0); for (int x : chosen) f[x] = 1; ntt->transform(f, false); for (int& x : f) x = int64(x) * x % MOD; ntt->transform(f, true); vector<int> counts(M, 0); int k = int(chosen.size()); for (int r = 0; r < M; ++r) { int h = f[r]; if (r + M <= 2 * M - 2) h += f[r + M]; counts[r] = k - h; } return counts; } State stateFromChosen(const vector<int>& chosen) { return makeState(chosen, countsFromChosen(chosen)); } State complementState(const State& s) { State t = s; t.A = s.R; t.R = s.A; t.totalCost = totalCostAll - s.totalCost; fill(t.posA.begin(), t.posA.end(), -1); fill(t.posR.begin(), t.posR.end(), -1); for (int i = 0; i < M; ++i) { t.inA[i] = s.inA[i] ^ 1; if (t.inA[i]) { t.posA[i] = int(t.A.size()); } else { t.posR[i] = int(t.R.size()); } } return t; } vector<int> singletonCounts(int p) { vector<int> counts(M, 1); counts[addMod(p, p)] = 0; return counts; } vector<int> pairCounts(int p, int q) { vector<int> counts(M, 2); --counts[addMod(p, p)]; --counts[addMod(p, q)]; --counts[addMod(q, q)]; --counts[addMod(p, q)]; return counts; } int64 pairValue(int p, int q) { array<int, 4> ex = { addMod(p, p), addMod(p, q), addMod(q, q), addMod(p, q) }; sort(ex.begin(), ex.end()); int64 value = 2 * sumW; for (int i = 0; i < 4;) { int j = i + 1; while (j < 4 && ex[j] == ex[i]) ++j; int count = j - i; int actual = 2 - count; int r = ex[i]; value += int64(w[r]) * (min(actual, d[r]) - min(2, d[r])); i = j; } return value; } vector<int> orientRandomSet(vector<int> chosen, int64 chosenCost) { int64 complementCost = totalCostAll - chosenCost; if (chosenCost > budget && complementCost <= budget) { vector<int> complement; complement.reserve(M); vector<uint8> selected(M, 0); for (int x : chosen) selected[x] = 1; for (int i = 0; i < M; ++i) { if (!selected[i]) complement.push_back(i); } return complement; } if (chosenCost > budget) { while (chosenCost > budget && !chosen.empty()) { int best = -1; for (int i = 0; i < int(chosen.size()); ++i) { if (best == -1 || cost[chosen[i]] > cost[chosen[best]]) { best = i; } } chosenCost -= cost[chosen[best]]; chosen.erase(chosen.begin() + best); } } complementCost = totalCostAll - chosenCost; if (complementCost <= budget && (chosenCost > budget || complementCost < chosenCost)) { vector<uint8> selected(M, 0); for (int x : chosen) selected[x] = 1; vector<int> complement; for (int i = 0; i < M; ++i) { if (!selected[i]) complement.push_back(i); } return complement; } return chosen; } State randomBalancedState(int k, mt19937_64& rng) { vector<int> order(M); iota(order.begin(), order.end(), 0); shuffle(order.begin(), order.end(), rng); k = max(1, min(k, M)); vector<int> chosen(order.begin(), order.begin() + k); int64 chosenCost = 0; for (int x : chosen) chosenCost += cost[x]; chosen = orientRandomSet(move(chosen), chosenCost); return stateFromChosen(chosen); } void doAdd(State& s, int p) { for (int q = 0; q < M; ++q) { if (q == p) continue; int r = addMod(p, q); if (s.inA[q]) { if (s.c[r] <= d[r]) s.U -= w[r]; --s.c[r]; } else { if (s.c[r] < d[r]) s.U += w[r]; ++s.c[r]; } } s.inA[p] = 1; s.totalCost += cost[p]; int pos = int(s.R.size()); s.posR[p] = pos; s.R.push_back(p); pos = int(s.A.size()); s.posA[p] = pos; s.A.push_back(p); } void doRemove(State& s, int p) { for (int q = 0; q < M; ++q) { if (q == p) continue; int r = addMod(p, q); if (s.inA[q]) { if (s.c[r] < d[r]) s.U += w[r]; ++s.c[r]; } else { if (s.c[r] <= d[r]) s.U -= w[r]; --s.c[r]; } } s.inA[p] = 0; s.totalCost -= cost[p]; int pos = s.posA[p]; int last = s.A.back(); s.A[pos] = last; s.posA[last] = pos; s.A.pop_back(); s.posA[p] = -1; pos = int(s.R.size()); s.posR[p] = pos; s.R.push_back(p); } int64 exactAddDelta(const State& s, int p) { int64 delta = 0; for (int q = 0; q < M; ++q) { if (q == p) continue; int r = addMod(p, q); if (s.inA[q]) { if (s.c[r] <= d[r]) delta -= w[r]; } else { if (s.c[r] < d[r]) delta += w[r]; } } return delta; } int64 exactRemoveDelta(const State& s, int p) { int64 delta = 0; for (int q = 0; q < M; ++q) { if (q == p) continue; int r = addMod(p, q); if (s.inA[q]) { if (s.c[r] < d[r]) delta += w[r]; } else { if (s.c[r] <= d[r]) delta -= w[r]; } } return delta; } void simulateRemove(vector<int>& c, vector<uint8>& status, int64& U, int p) { for (int q = 0; q < M; ++q) { if (q == p) continue; int r = addMod(p, q); if (status[q]) { if (c[r] < d[r]) U += w[r]; ++c[r]; } else { if (c[r] <= d[r]) U -= w[r]; --c[r]; } } status[p] = 0; } void simulateAdd(vector<int>& c, vector<uint8>& status, int64& U, int p) { for (int q = 0; q < M; ++q) { if (q == p) continue; int r = addMod(p, q); if (status[q]) { if (c[r] <= d[r]) U -= w[r]; --c[r]; } else { if (c[r] < d[r]) U += w[r]; ++c[r]; } } status[p] = 1; } class LocalOptimizer { mt19937_64& rng; vector<int> tempCounts; vector<uint8> tempStatus; vector<int> sample; vector<int> gainMask, lossMask; vector<int64> addScore, removeScore; vector<int> topCandidates(const State& s, bool addOperation, int limit) { vector<pair<int64, int>> scored; scored.reserve(s.inA.size()); const auto& scores = addOperation ? addScore : removeScore; for (int p = 0; p < M; ++p) { if (addOperation) { if (!s.inA[p]) scored.emplace_back(scores[p], p); } else { if (s.inA[p]) scored.emplace_back(scores[p], p); } } int k = min(limit, int(scored.size())); partial_sort( scored.begin(), scored.begin() + k, scored.end(), [](const auto& a, const auto& b) { if (a.first != b.first) return a.first > b.first; return a.second < b.second; } ); vector<int> result; result.reserve(k + 16); for (int i = 0; i < k; ++i) result.push_back(scored[i].second); return result; } void addRandomCandidates(const State& s, vector<int>& pool, bool addOperation, int count) { const vector<int>& source = addOperation ? s.R : s.A; if (source.empty()) return; for (int i = 0; i < count; ++i) { pool.push_back(source[rng() % source.size()]); } } void deduplicate(vector<int>& v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } int64 estimateSwap(const State& s, int p, int q) const { int64 delta = 0; for (int x : sample) { if (x == p || x == q) continue; int rp = addMod(p, x); int rq = addMod(q, x); if (s.inA[x]) { delta += gainMask[rp]; delta -= lossMask[rq]; } else { delta -= lossMask[rp]; delta += gainMask[rq]; } } return delta; } public: explicit LocalOptimizer(mt19937_64& rng_) : rng(rng_), tempCounts(M), tempStatus(M), sample(M), gainMask(M), lossMask(M), addScore(M), removeScore(M) {} void improve(State& s, State& global, chrono::steady_clock::time_point endTime, bool deep) { updateGlobal(s, global); bool first = deep; while (chrono::steady_clock::now() < endTime) { int sampleSize = min(M, first ? 192 : 96); iota(sample.begin(), sample.end(), 0); shuffle(sample.begin(), sample.end(), rng); sample.resize(sampleSize); for (int r = 0; r < M; ++r) { gainMask[r] = (s.c[r] < d[r]) ? w[r] : 0; lossMask[r] = (s.c[r] > 0 && s.c[r] <= d[r]) ? w[r] : 0; } fill(addScore.begin(), addScore.end(), 0); fill(removeScore.begin(), removeScore.end(), 0); for (int p = 0; p < M; ++p) { int64 addValue = 0; int64 removeValue = 0; for (int q : sample) { if (q == p) continue; int r = addMod(p, q); if (s.inA[q]) { removeValue += gainMask[r]; addValue -= lossMask[r]; } else { addValue += gainMask[r]; removeValue -= lossMask[r]; } } addScore[p] = addValue; removeScore[p] = removeValue; } vector<int> addPool = topCandidates(s, true, 24); vector<int> removePool = topCandidates(s, false, 24); int cheapAdded = 0, expensiveAdded = 0; for (int p : costOrder) { if (!s.inA[p] && cheapAdded < 4) { addPool.push_back(p); ++cheapAdded; } if (expensiveAdded >= 4) break; } for (int i = M - 1; i >= 0 && expensiveAdded < 4; --i) { int p = costOrder[i]; if (s.inA[p]) { removePool.push_back(p); ++expensiveAdded; } } addRandomCandidates(s, addPool, true, 4); addRandomCandidates(s, removePool, false, 4); deduplicate(addPool); deduplicate(removePool); int64 bestDelta = 0; int bestType = 0; int bestP = -1, bestQ = -1; for (int p : addPool) { if (s.totalCost + cost[p] > budget) continue; int64 delta = exactAddDelta(s, p); if (delta > bestDelta) { bestDelta = delta; bestType = 1; bestP = p; } } for (int p : removePool) { int64 delta = exactRemoveDelta(s, p); if (delta > bestDelta) { bestDelta = delta; bestType = 2; bestP = p; } } struct SwapCandidate { int64 estimate; int p, q; }; vector<SwapCandidate> swaps; swaps.reserve(addPool.size() * removePool.size()); for (int p : removePool) { for (int q : addPool) { int64 finalCost = s.totalCost - cost[p] + cost[q]; if (finalCost > budget) continue; swaps.push_back({estimateSwap(s, p, q), p, q}); } } sort(swaps.begin(), swaps.end(), [](const SwapCandidate& a, const SwapCandidate& b) { if (a.estimate != b.estimate) return a.estimate > b.estimate; if (a.p != b.p) return a.p < b.p; return a.q < b.q; }); int limit = min<int>(int(swaps.size()), first ? 140 : 70); for (int i = 0; i < limit; ++i) { if ((i & 7) == 0 && chrono::steady_clock::now() >= endTime) break; int p = swaps[i].p; int q = swaps[i].q; memcpy(tempCounts.data(), s.c.data(), size_t(M) * sizeof(int)); memcpy(tempStatus.data(), s.inA.data(), size_t(M) * sizeof(uint8)); int64 tempU = s.U; simulateRemove(tempCounts, tempStatus, tempU, p); simulateAdd(tempCounts, tempStatus, tempU, q); int64 delta = tempU - s.U; if (delta > bestDelta) { bestDelta = delta; bestType = 3; bestP = p; bestQ = q; } } if (bestType == 0 || bestDelta <= 0) break; if (bestType == 1) { doAdd(s, bestP); } else if (bestType == 2) { doRemove(s, bestP); } else { doRemove(s, bestP); doAdd(s, bestQ); } updateGlobal(s, global); first = false; } } }; bool randomSwap(State& s, mt19937_64& rng) { if (s.A.empty() || s.R.empty()) return false; int p = s.A[rng() % s.A.size()]; int chosen = -1; for (int attempt = 0; attempt < 150; ++attempt) { int q = s.R[rng() % s.R.size()]; if (s.totalCost - cost[p] + cost[q] <= budget) { chosen = q; break; } } if (chosen == -1) { int seen = 0; for (int q : s.R) { if (s.totalCost - cost[p] + cost[q] > budget) continue; ++seen; if (rng() % seen == 0) chosen = q; } } if (chosen == -1) return false; doRemove(s, p); doAdd(s, chosen); return true; } bool randomAdd(State& s, mt19937_64& rng) { int chosen = -1; int seen = 0; for (int p : s.R) { if (s.totalCost + cost[p] > budget) continue; ++seen; if (rng() % seen == 0) chosen = p; } if (chosen == -1) return false; doAdd(s, chosen); return true; } bool randomRemove(State& s, mt19937_64& rng) { if (s.A.empty()) return false; int p = s.A[rng() % s.A.size()]; doRemove(s, p); return true; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); auto start = chrono::steady_clock::now(); deadline = start + chrono::milliseconds(7200); int64 B; cin >> M >> B; budget = B; cost.resize(M); w.resize(M); d.resize(M); for (int& x : cost) { cin >> x; totalCostAll += x; } for (int& x : w) { cin >> x; sumW += x; } for (int& x : d) cin >> x; int transformSize = 1; while (transformSize < 2 * M) transformSize <<= 1; ntt = make_unique<NTT>(transformSize); costOrder.resize(M); iota(costOrder.begin(), costOrder.end(), 0); sort(costOrder.begin(), costOrder.end(), [](int a, int b) { if (cost[a] != cost[b]) return cost[a] < cost[b]; return a < b; }); vector<int> baselineChosen; int64 baselineCost = 0; for (int p : costOrder) { if (baselineCost + cost[p] <= budget) { baselineCost += cost[p]; baselineChosen.push_back(p); } } State baseline = stateFromChosen(baselineChosen); State global = baseline; if (totalCostAll - baseline.totalCost <= budget) { State complement = complementState(baseline); updateGlobal(complement, global); } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); for (int attempt = 0; attempt < 4; ++attempt) { if (chrono::steady_clock::now() > start + chrono::milliseconds(1300)) break; int k = (attempt & 1) ? M / 2 : M / 4; State candidate = randomBalancedState(k, rng); updateGlobal(candidate, global); } if (chrono::steady_clock::now() <= start + chrono::milliseconds(1600)) { vector<int> order(M); iota(order.begin(), order.end(), 0); shuffle(order.begin(), order.end(), rng); vector<int> chosen; int64 currentCost = 0; for (int p : order) { if (currentCost + cost[p] <= budget) { currentCost += cost[p]; chosen.push_back(p); } } State candidate = stateFromChosen(chosen); updateGlobal(candidate, global); } int bestAnyPair = -1, bestAnyQ = -1; int bestFeasiblePair = -1, bestFeasibleQ = -1; int64 bestAnyPairValue = -1; int64 bestFeasiblePairValue = -1; vector<int> sumOrder(M), diagOrder(M); iota(sumOrder.begin(), sumOrder.end(), 0); iota(diagOrder.begin(), diagOrder.end(), 0); sort(sumOrder.begin(), sumOrder.end(), [](int a, int b) { int64 va = int64(w[a]) * min(2, d[a]); int64 vb = int64(w[b]) * min(2, d[b]); if (va != vb) return va < vb; return a < b; }); sort(diagOrder.begin(), diagOrder.end(), [](int a, int b) { int64 va = d[addMod(a, a)] >= 2 ? w[addMod(a, a)] : 0; int64 vb = d[addMod(b, b)] >= 2 ? w[addMod(b, b)] : 0; if (va != vb) return va < vb; return a < b; }); auto examinePair = [&](int p, int q) { if (p == q) return; int64 value = pairValue(p, q); int64 pc = int64(cost[p]) + cost[q]; if (value > bestAnyPairValue || (value == bestAnyPairValue && p < bestAnyPair)) { bestAnyPairValue = value; bestAnyPair = p; bestAnyQ = q; } bool feasible = pc <= budget || totalCostAll - pc <= budget; if (feasible && (value > bestFeasiblePairValue || (value == bestFeasiblePairValue && p < bestFeasiblePair))) { bestFeasiblePairValue = value; bestFeasiblePair = p; bestFeasibleQ = q; } }; auto pairSearchEnd = min(deadline, start + chrono::milliseconds(2200)); for (int p = 0; p < M; ++p) { if ((p & 511) == 0 && chrono::steady_clock::now() > pairSearchEnd) break; for (int t = 0; t < min(48, M); ++t) { int r = sumOrder[t]; int q = r - p; if (q < 0) q += M; examinePair(p, q); } for (int t = 0; t < min(48, M); ++t) { examinePair(p, diagOrder[t]); } } State pairState; bool havePairState = false; if (bestFeasiblePair != -1) { int p = bestFeasiblePair; int q = bestFeasibleQ; int64 pc = int64(cost[p]) + cost[q]; int64 cc = totalCostAll - pc; vector<int> chosen; if (pc <= budget && (cc > budget || pc <= cc)) { chosen = {p, q}; } else { vector<uint8> excluded(M, 0); excluded[p] = excluded[q] = 1; for (int i = 0; i < M; ++i) { if (!excluded[i]) chosen.push_back(i); } } pairState = makeState(chosen, pairCounts(p, q)); havePairState = true; updateGlobal(pairState, global); } int bestSingleton = -1; int64 bestSingletonValue = -1; int bestFeasibleSingleton = -1; int64 bestFeasibleSingletonValue = -1; for (int p = 0; p < M; ++p) { int64 value = sumW - w[addMod(p, p)]; if (value > bestSingletonValue || (value == bestSingletonValue && p < bestSingleton)) { bestSingletonValue = value; bestSingleton = p; } bool feasible = cost[p] <= budget || totalCostAll - cost[p] <= budget; if (feasible && (value > bestFeasibleSingletonValue || (value == bestFeasibleSingletonValue && p < bestFeasibleSingleton))) { bestFeasibleSingletonValue = value; bestFeasibleSingleton = p; } } State singletonState; bool haveSingletonState = false; if (bestFeasibleSingleton != -1) { int p = bestFeasibleSingleton; int64 sc = cost[p]; int64 cc = totalCostAll - sc; vector<int> chosen; if (sc <= budget && (cc > budget || sc <= cc)) { chosen = {p}; } else { vector<uint8> excluded(M, 0); excluded[p] = 1; for (int i = 0; i < M; ++i) { if (!excluded[i]) chosen.push_back(i); } } singletonState = makeState(chosen, singletonCounts(p)); haveSingletonState = true; updateGlobal(singletonState, global); } LocalOptimizer optimizer(rng); optimizer.improve( baseline, global, min(deadline, start + chrono::milliseconds(2800)), true ); if (haveSingletonState) { optimizer.improve( singletonState, global, min(deadline, start + chrono::milliseconds(3400)), false ); } if (havePairState) { optimizer.improve( pairState, global, min(deadline, start + chrono::milliseconds(4100)), true ); } State current = global; optimizer.improve( current, global, min(deadline, start + chrono::milliseconds(4600)), false ); int iteration = 0; while (chrono::steady_clock::now() < deadline) { if (iteration % 4 == 0) current = global; int kicks = 1 + int(rng() % 4); for (int k = 0; k < kicks; ++k) { int type = int(rng() % 10); if (type < 5) { randomSwap(current, rng); } else if (type < 8) { randomRemove(current, rng); } else { randomAdd(current, rng); } } auto endTime = min(deadline, chrono::steady_clock::now() + chrono::milliseconds(700)); optimizer.improve(current, global, endTime, false); ++iteration; } vector<int> answer = global.A; sort(answer.begin(), answer.end()); cout << answer.size() << '\n'; if (!answer.empty()) { for (int i = 0; i < int(answer.size()); ++i) { if (i) cout << ' '; cout << answer[i]; } cout << '\n'; } return 0; }