题面
来源:FrontierCS 官方 statement.txt,与模型收到的题面相同。
Bakery Survey
This is an interactive problem.
Your city has n bakeries (where n is a power of 2), and bakery i specializes in one type of cake a_i.
You want to determine d — the number of distinct cake types available in the city.
You don't know the values of a_1, ..., a_n. However, your friend can help you by tasting cakes. Your friend has a memory capacity of k (where k is also a power of 2), which works as follows:
Your friend's memory is a queue S. You can perform two types of operations:
1. Query operation: Ask your friend to taste the cake from bakery c. This will:
- Tell you whether a_c is already in S (the last k cake types tasted)
- Add a_c to the end of S
- If |S| > k, remove the front element from S
2. Reset operation: Clear your friend's memory, making S empty.
Your goal is to find d while minimizing the total cost of operations.
This problem is graded based on the total cost of operations. The cost is calculated as:
Total Cost = (number of resets) × n + (number of queries) + 1
You must use at most 100,000 operations in total.
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 1024, both k and n are powers of 2).
Interaction
To perform an operation, output one line in one of the following formats:
? c — Ask your friend to taste the cake from bakery c (1 ≤ c ≤ n).
R — Reset your friend's memory.
After a query operation, read a single character:
- Y (Yes) if a_c is in the memory S
- N (No) if a_c is not in the memory S
When you have found the answer, output:
! d — where d is the number of distinct cake types.
After printing the answer, your program should terminate immediately.
To flush your output, use:
- fflush(stdout) or cout.flush() in C++
- System.out.flush() in Java
- stdout.flush() in Python
Example
Input:
4 2
N
N
Y
N
N
N
N
Output:
? 1
? 2
? 3
? 4
R
? 4
? 1
? 2
! 3
Time limit: 4 seconds
Memory limit: 512 MB
## Scoring
All feasibility and interaction limits apply before scoring; invalid output receives zero. Points are averaged over test files. For the logarithmic branch, write x for the original minimizing quantity and a for its physical 60-point reference. The improvement factor is r=a/x, and the upper-band award is P=min(100,60+ln(r)). Here ln is natural: r=1 corresponds to 60 points, and on the uncapped logarithmic upper band, r=exp(P-60). Each additional point multiplies the physical improvement factor by e (divides a minimizing objective by e or multiplies a maximizing objective by e, as applicable). The lower-band, zero-objective, and completion rules below still apply.
C=n*reset_count+query_count+1 is the total operation cost, and R>0 is the stored reference cost. Define the objective x=C, its physical reference a=A=R, and the lower-band calculation L=60*R/C.
```text
P = 100, if x = 0
P = L, if L <= 60 and x > 0
P = min(100, 60 + ln(r)), otherwise
```
Zero is tested on the valid original count, cost, or error, before the lower-band test. A zero reference anchor has no positive improvement branch; positive objectives keep their lower-band score. The +1 in C is part of the defined operation cost, so C is always positive. The checker reports P/100.
结果
Kimi K3.1 ProKimi
max
58.5
平均得分 ↑
- 58.5采样 1 · 部分分
- 64.3k平均输出 token(含思考) ↓
- 25.1输出速度 tokens/s ↑
- 2564 秒延迟 ↓
Kimi K3.1 Pro(high)Kimi high
high
60.5
平均得分 ↑
- 60.5采样 1 · 部分分
- 25.7k平均输出 token(含思考) ↓
- 24.3输出速度 tokens/s ↑
- 1060 秒延迟 ↓
DeepSeek Flash V4.1-1017DeepSeek
auto(high)
7.9
平均得分 ↑
- 7.9采样 1 · 部分分
- 101.5k平均输出 token(含思考) ↓
- 167.3输出速度 tokens/s ↑
- 607 秒延迟 ↓
MiniMax M3.1 FlashMiniMax
auto(max)
39.5
平均得分 ↑
- 39.5采样 1 · 部分分
- 119.5k平均输出 token(含思考) ↓
- 144.0输出速度 tokens/s ↑
- 831 秒延迟 ↓
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
Kimi K3.1 Pro · 采样 1 · 220 行
#include <bits/stdc++.h> using namespace std; int n, k; char ask(int c) { cout << "? " << c << '\n'; cout.flush(); char r; if (!(cin >> r)) exit(0); return r; } struct Node { int side; int idx; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); if (!(cin >> n >> k)) return 0; if (n == 1) { cout << "! 1\n"; cout.flush(); return 0; } if (k == 1) { vector<int> reps; for (int i = 1; i <= n; i++) { if (ask(i) == 'Y') continue; if (reps.empty()) { reps.push_back(i); continue; } int p = (int)reps.size(); ask(reps[0]); if (ask(i) == 'Y') continue; if (p == 1) { reps.push_back(i); continue; } bool duplicate = false; for (int j = 1; j < p; j++) { if (ask(reps[j]) == 'Y') { duplicate = true; break; } if (j + 1 < p) ask(i); } if (!duplicate) reps.push_back(i); } cout << "! " << reps.size() << '\n'; cout.flush(); return 0; } vector<int> reps; for (int block = 0; block < n; block += k) { vector<int> cand; for (int j = 0; j < k; j++) { int id = block + j + 1; if (ask(id) == 'N') cand.push_back(id); } if (cand.empty()) continue; if (reps.empty()) { reps = cand; continue; } int p = (int)reps.size(); int q = (int)cand.size(); vector< bitset<1024> > compared(q); vector<int> comparedCount(q, 0); vector<unsigned char> state(q, 0); int resolved = 0; auto addComparison = [&](int b, int g) { if (!compared[b].test(g)) { compared[b].set(g); comparedCount[b]++; if (state[b] == 0 && comparedCount[b] == p) { state[b] = 2; resolved++; } } }; deque<Node> memory; int firstLen = min(p, k); int initialPadding = k - firstLen; for (int i = 0; i < initialPadding; i++) { ask(reps[0]); memory.push_back({0, 0}); } for (int i = 0; i < firstLen; i++) { ask(reps[i]); memory.push_back({0, i}); } auto processNode = [&](int side, int idx) { int bakery = (side == 0 ? reps[idx] : cand[idx]); char response = ask(bakery); if (response == 'N') { if (side == 1) { for (const Node &node : memory) { if (node.side == 0) addComparison(idx, node.idx); } } else { for (const Node &node : memory) { if (node.side == 1) addComparison(node.idx, idx); } } } else if (side == 1 && state[idx] == 0) { bool selfInMemory = false; for (const Node &node : memory) { if (node.side == 1 && node.idx == idx) { selfInMemory = true; break; } } if (!selfInMemory) { state[idx] = 1; resolved++; } } memory.push_back({side, idx}); if ((int)memory.size() > k) memory.pop_front(); }; int groups = (p + k - 1) / k; for (int i = 0; i < groups && resolved < q; i++) { for (int b = 0; b < q && resolved < q; b++) { processNode(1, b); } if (resolved == q) break; int gblock = (i + 1 < groups ? i + 1 : 0); int begin = gblock * k; int end = min(p, begin + k); for (int g = begin; g < end && resolved < q; g++) { processNode(0, g); } } vector<int> unresolved; for (int b = 0; b < q; b++) { if (state[b] == 0) unresolved.push_back(b); } if (!unresolved.empty()) { for (int i = 0; i < k; i++) ask(reps[0]); int h = k / 2; int u = (int)unresolved.size(); for (int ds = 0; ds < u; ds += h) { int de = min(u, ds + h); int dsize = de - ds; for (int gs = 0; gs < p; gs += h) { bool needed = false; for (int di = ds; di < de; di++) { if (state[unresolved[di]] == 0) { needed = true; break; } } if (!needed) break; int ge = min(p, gs + h); int gsize = ge - gs; if (gs > 0) { int padding = k - gsize - dsize + 1; for (int i = 0; i < padding; i++) ask(reps[0]); } for (int g = gs; g < ge; g++) ask(reps[g]); for (int di = ds; di < de; di++) { int b = unresolved[di]; if (ask(cand[b]) == 'Y') state[b] = 1; } } } } for (int b = 0; b < q; b++) { if (state[b] != 1) reps.push_back(cand[b]); } } cout << "! " << reps.size() << '\n'; cout.flush(); return 0; }
Kimi K3.1 Pro(high) · 采样 1 · 226 行
#include <bits/stdc++.h> using namespace std; int n, k; char ask(int c) { cout << "? " << c << endl; char response; cin >> response; return response; } struct DSU { vector<int> parent, size; explicit DSU(int n) : parent(n), size(n, 1) { iota(parent.begin(), parent.end(), 0); } int find(int x) { if (parent[x] == x) return x; return parent[x] = find(parent[x]); } void unite(int a, int b) { a = find(a); b = find(b); if (a == b) return; if (size[a] < size[b]) swap(a, b); parent[b] = a; size[a] += size[b]; } }; int solveWithCapacityOne(const vector<int>& items) { int m = (int)items.size(); if (m <= 1) return m; vector<vector<pair<int, int>>> adjacency(m); vector<char> used; int edgeCount = 0; auto addEdge = [&](int u, int v) { int id = edgeCount++; adjacency[u].push_back({v, id}); adjacency[v].push_back({u, id}); }; for (int i = 0; i < m; ++i) { for (int j = i + 1; j < m; ++j) { addEdge(i, j); } } if (m % 2 == 0) { for (int i = 0; i < m; i += 2) { addEdge(i, i + 1); } } used.assign(edgeCount, 0); vector<int> nextEdge(m, 0), st, circuit; st.push_back(0); while (!st.empty()) { int v = st.back(); while (nextEdge[v] < (int)adjacency[v].size() && used[adjacency[v][nextEdge[v]].second]) { ++nextEdge[v]; } if (nextEdge[v] == (int)adjacency[v].size()) { circuit.push_back(v); st.pop_back(); } else { auto [to, id] = adjacency[v][nextEdge[v]++]; if (used[id]) continue; used[id] = 1; st.push_back(to); } } reverse(circuit.begin(), circuit.end()); DSU dsu(m); ask(items[circuit[0]]); for (int i = 1; i < (int)circuit.size(); ++i) { int previous = circuit[i - 1]; int current = circuit[i]; if (ask(items[current]) == 'Y') { dsu.unite(previous, current); } } int answer = 0; for (int i = 0; i < m; ++i) { if (dsu.find(i) == i) ++answer; } return answer; } int solveWithSmallCapacity(const vector<int>& items) { vector<int> representatives; for (int item : items) { bool duplicate = false; for (size_t start = 0; start < representatives.size() && !duplicate; start += k) { int length = (int)min((size_t)k, representatives.size() - start); for (int t = 0; t < k; ++t) { ask(representatives[start + t % length]); } if (ask(item) == 'Y') duplicate = true; } if (!duplicate) representatives.push_back(item); } return (int)representatives.size(); } long long estimatedMergeCost(int a, int b) { if (a == 0 || b == 0) return 0; long long aChunks = (a + k - 1) / k; long long bBatches = (b + k - 1) / k; return 2LL * aChunks * (bBatches * k + b); } vector<int> mergeSets(const vector<int>& first, const vector<int>& second) { if (first.empty()) return second; if (second.empty()) return first; const vector<int>* A = &first; const vector<int>* B = &second; if (estimatedMergeCost((int)second.size(), (int)first.size()) < estimatedMergeCost((int)first.size(), (int)second.size())) { swap(A, B); } vector<int> result = *A; result.reserve(A->size() + B->size()); for (int bStart = 0; bStart < (int)B->size(); bStart += k) { int bLength = min(k, (int)B->size() - bStart); vector<char> duplicate(bLength, 0); for (int aStart = 0; aStart < (int)A->size(); aStart += k) { bool allFound = true; for (char value : duplicate) { if (!value) { allFound = false; break; } } if (allFound) break; int aLength = min(k, (int)A->size() - aStart); vector<int> padded(k); for (int t = 0; t < k; ++t) { padded[t] = (*A)[aStart + t % aLength]; } for (int t = 0; t < k; ++t) ask(padded[t]); for (int j = 0; j < bLength; ++j) { if (ask((*B)[bStart + j]) == 'Y') { duplicate[j] = 1; } } for (int t = k - 1; t >= 0; --t) ask(padded[t]); for (int j = bLength - 1; j >= 0; --j) { if (ask((*B)[bStart + j]) == 'Y') { duplicate[j] = 1; } } } for (int j = 0; j < bLength; ++j) { if (!duplicate[j]) result.push_back((*B)[bStart + j]); } } return result; } vector<int> solveBlocks(const vector<vector<int>>& blocks, int left, int right) { if (right - left == 1) return blocks[left]; int middle = (left + right) / 2; vector<int> a = solveBlocks(blocks, left, middle); vector<int> b = solveBlocks(blocks, middle, right); return mergeSets(a, b); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> k; vector<vector<int>> blocks(n / k); vector<int> candidates; for (int i = 1; i <= n; ++i) { if (ask(i) == 'N') { blocks[(i - 1) / k].push_back(i); candidates.push_back(i); } } int answer; if (k == n || candidates.size() <= 1) { answer = (int)candidates.size(); } else if (k == 1) { answer = solveWithCapacityOne(candidates); } else if (k == 2) { answer = solveWithSmallCapacity(candidates); } else { answer = (int)solveBlocks(blocks, 0, (int)blocks.size()).size(); } cout << "! " << answer << endl; return 0; }
DeepSeek Flash V4.1-1017 · 采样 1 · 125 行
#include <bits/stdc++.h> using namespace std; char ask(int c) { cout << "? " << c << "\n"; cout.flush(); char ans; cin >> ans; return ans; } void reset_mem() { cout << "R\n"; cout.flush(); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; if (!(cin >> n >> k)) return 0; if (n == 1) { cout << "! 1\n"; cout.flush(); return 0; } if (k >= n) { reset_mem(); int d = 0; for (int i = 1; i <= n; ++i) { char ans = ask(i); if (ans == 'N') ++d; } cout << "! " << d << "\n"; cout.flush(); return 0; } if (k == 1) { vector<int> reps; for (int i = 1; i <= n; ++i) { if (reps.empty()) { reps.push_back(i); continue; } int D = reps.size(); bool dup = false; for (int j = 0; j < D; j += 2) { ask(reps[j]); char ans = ask(i); if (ans == 'Y') { dup = true; break; } if (j + 1 < D) { ans = ask(reps[j+1]); if (ans == 'Y') { dup = true; break; } } } if (!dup) reps.push_back(i); } cout << "! " << reps.size() << "\n"; cout.flush(); return 0; } // k >= 2 and k < n vector<int> global_reps; for (int start = 1; start <= n; start += k) { int end = min(n, start + k - 1); reset_mem(); vector<int> C; for (int i = start; i <= end; ++i) { char ans = ask(i); if (ans == 'N') C.push_back(i); } int D_old = global_reps.size(); if (D_old == 0) { for (int c : C) global_reps.push_back(c); continue; } vector<int> new_c; if (D_old < k) { reset_mem(); for (int c : C) { bool dup = false; for (int idx : global_reps) { ask(idx); } char ans = ask(c); if (ans == 'Y') dup = true; if (!dup) new_c.push_back(c); } } else { for (int c : C) { bool dup = false; int fullChunks = D_old / k; int rem = D_old % k; for (int h = 0; h < fullChunks; ++h) { for (int t = 0; t < k; ++t) { ask(global_reps[h * k + t]); } char ans = ask(c); if (ans == 'Y') { dup = true; break; } } if (!dup && rem > 0) { int startRep = fullChunks * k; for (int t = 0; t < rem; ++t) { ask(global_reps[startRep + t]); } for (int t = 0; t < k - rem; ++t) { ask(global_reps[t]); } char ans = ask(c); if (ans == 'Y') dup = true; } if (!dup) new_c.push_back(c); } } for (int c : new_c) global_reps.push_back(c); } cout << "! " << global_reps.size() << "\n"; cout.flush(); return 0; }
MiniMax M3.1 Flash · 采样 1 · 155 行
#include <bits/stdc++.h> using namespace std; class BakerySurvey { private: int n, k; long long queryCount = 0; long long resetCount = 0; int nextLeaf = 0; int blockSize; bool ask(int c) { cout << "? " << c << '\n'; cout.flush(); char response; if (!(cin >> response)) exit(0); if (response != 'Y' && response != 'N') exit(0); ++queryCount; return response == 'Y'; } void resetMemory() { cout << "R\n"; cout.flush(); ++resetCount; } vector<int> buildLeaf(int left, int right) { if (nextLeaf++ > 0) resetMemory(); vector<int> representatives; for (int c = left + 1; c <= right; ++c) { if (!ask(c)) representatives.push_back(c); } return representatives; } vector<int> mergeSets(const vector<int>& source, const vector<int>& target) { int a = static_cast<int>(source.size()); int b = static_cast<int>(target.size()); if (a == 0) return target; if (b == 0) return source; if (a + b <= k + 1) { for (int c : source) ask(c); vector<char> matched(b, false); for (int i = 0; i < b; ++i) { if (ask(target[i])) matched[i] = true; } vector<int> result = source; result.reserve(a + b); for (int i = 0; i < b; ++i) { if (!matched[i]) result.push_back(target[i]); } return result; } long long bestOperations = LLONG_MAX; int bestSourceChunk = 1; int bestTargetChunk = k; for (int s = 1; s <= k; ++s) { int t = k + 1 - s; long long sourceChunks = (a + s - 1) / s; long long targetChunks = (b + t - 1) / t; long long operations = sourceChunks * targetChunks * (k + 1LL); if (operations < bestOperations) { bestOperations = operations; bestSourceChunk = s; bestTargetChunk = t; } } int s = bestSourceChunk; int t = bestTargetChunk; vector<char> matched(b, false); for (int sourceLeft = 0; sourceLeft < a; sourceLeft += s) { int sourceRight = min(a, sourceLeft + s); for (int targetLeft = 0; targetLeft < b; targetLeft += t) { int targetRight = min(b, targetLeft + t); for (int i = sourceLeft; i < sourceRight; ++i) { ask(source[i]); } for (int i = sourceLeft; i < sourceRight; ++i) { ask(source[i]); } for (int i = targetLeft; i < targetRight; ++i) { if (ask(target[i])) matched[i] = true; } int padding = t - (targetRight - targetLeft); for (int i = 0; i < padding; ++i) { ask(source[sourceLeft]); } } } vector<int> result = source; result.reserve(a + b); for (int i = 0; i < b; ++i) { if (!matched[i]) result.push_back(target[i]); } return result; } vector<int> solve(int left, int right) { if (right - left == 1) { return buildLeaf(left * blockSize, min(n, (left + 1) * blockSize) - 1); } int middle = (left + right) / 2; vector<int> first = solve(left, middle); vector<int> second = solve(middle, right); return mergeSets(second, first); } public: BakerySurvey(int n, int k) : n(n), k(k) { blockSize = k + 1; } int run() { int leafCount = (n + blockSize - 1) / blockSize; vector<int> representatives = solve(0, leafCount); return static_cast<int>(representatives.size()); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; if (!(cin >> n >> k)) return 0; BakerySurvey solver(n, k); int answer = solver.run(); cout << "! " << answer << '\n'; cout.flush(); return 0; }