I. Slot Machine
Imperial Chance & Play Casino offers games using a slot machine that has n wheels arranged next to each other. Each of the wheels has n distinct symbols on it, and these symbols appear in the same order on each wheel. Each wheel shows one of its symbols thr...
Problem Statement
Formatted from the contest statement text, with sample tests broken out into copyable blocks.
Imperial Chance & Play Casino offers games using a slot machine that has n wheels arranged next to each other. Each of the wheels has n distinct symbols on it, and these symbols appear in the same order on each wheel. Each wheel shows one of its symbols through a window on the front of the machine, which results in a sequence of n symbols being shown next to each other.
Figure I.1: The initial configuration in Sample Interaction 1.
You are standing behind the machine and notice that a maintenance panel has been left open. When you stick your hand inside, you are able to secretly rotate any of the wheels by any number of steps, thus changing the symbol shown on that wheel. You want to win a jackpot, which will happen if all the wheels show the same symbol at the same time. Unfortunately, you cannot see the symbols from your position, so you asked your good friend to help you. The friend is standing in front of the machine and she tells you the number of distinct symbols in the sequence she can currently see. Can you win the jackpot by manipulating the wheels if your friend updates the information after every action you make?
Interaction
The first line of input contains an integer n (3 ≤ n ≤ 50), giving the number of wheels and symbols in the machine. Interaction then proceeds in rounds. In each round, one line of input becomes available, containing an integer k (1 ≤ k ≤ n), the number of distinct symbols in the current sequence. If k > 1, output two integers i and j (1 ≤ i ≤ n; −109 ≤ j ≤ 109 ), representing your action: rotating the ith wheel by j positions, where negative numbers indicate rotating in the opposite direction. Otherwise, if k = 1, indicating that all wheels show the same symbol, your program must exit without printing more output. At most 10 000 actions are allowed – if your submission uses more rounds, it will not be accepted. It is guaranteed that the initial configuration of wheels does not already have all wheels showing the same symbol (k > 1 in the first round). The judge program will not behave in an adversarial way, which means the initial configuration is fixed before the first action. A testing tool is provided to help you develop and test your solution.
Read Sample Interaction 1 Write
5
4
1 1
3
4 2
3
3 1
3
3 1
2
5 4
1
Read Sample Interaction 2 Write
3
3
2 -1
2
3 -1
2
2 -1
1Editorial
The solution write-up is rendered from the LaTeX source, with equations kept live through MathJax.
Key Observations
Fix all wheels except one wheel $i$, and rotate only $i$ through a full cycle. For each position of wheel $i$, the reported number of distinct symbols is minimal exactly when the symbol shown by wheel $i$ is already present on at least one of the other wheels. So one full scan of a wheel gives a binary mask of which symbols are currently present elsewhere.
First scan wheel $1$. From this mask we can pick one symbol $s$ that is absent from all other wheels, and one symbol $r$ that is present on some other wheel.
Now fix wheel $1$ to show $s$ and scan another wheel $j$. Then fix wheel $1$ to show $r$ and scan $j$ again. The only difference between the two scans is the position where wheel $j$ itself shows symbol $s$. Therefore comparing the two masks reveals exactly how much wheel $j$ must be rotated to show $s$.
Repeating this for every wheel gives a common target symbol, so the final phase is just rotating every wheel to its discovered offset.
Algorithm
Maintain the current offset of each wheel modulo $n$.
Define
scan(i): rotate wheel $i$ through all $n$ positions, record the reported distinct-count value for each one, and mark the positions that attain the minimum value.Scan wheel $1$ once. Choose one position labeled $0$ in the resulting mask and call its symbol $s$; choose one position labeled $1$ and call its symbol $r$.
Rotate wheel $1$ to $s$. For each wheel $j \ge 2$:
scan $j$ with wheel $1$ fixed at $s$;
rotate wheel $1$ to $r$ and scan $j$ again;
compare the two masks to find the unique position where wheel $j$ shows symbol $s$;
rotate wheel $1$ back to $s$.
Finally, rotate every wheel to the stored position that makes it show $s$. enumerate
Correctness Proof
We prove that the algorithm returns the correct answer.
Lemma 1.
For a scan of wheel $i$, a position is marked by the algorithm if and only if the symbol shown by wheel $i$ at that position is already shown by at least one other wheel.
Proof.
Fix the other $n-1$ wheels. If wheel $i$ is rotated to a symbol that is absent from all other wheels, the total number of distinct visible symbols increases by one compared to showing any already-present symbol. Therefore the minimum possible distinct-count value is attained exactly at the positions where wheel $i$ shows a symbol already present elsewhere. □
Lemma 2.
For every wheel $j \ge 2$, comparing the two scans described above identifies the unique offset at which wheel $j$ shows the chosen target symbol $s$.
Proof.
The two scans differ only in the symbol shown by wheel $1$: once it is $s$, once it is $r$. All other wheels are unchanged. By construction, $s$ is absent from the other wheels when wheel $1$ shows $s$, while $r$ is present on at least one other wheel when wheel $1$ shows $r$. Hence the membership test from Lemma 1 changes exactly at the position where wheel $j$ itself shows $s$. That position is therefore identified uniquely. □
Theorem.
The algorithm outputs the correct answer for every valid input.
Proof.
By Lemma 2, for every wheel we discover an offset that makes it show the same symbol $s$. After the final rotation phase, all wheels therefore display $s$, so the machine shows only one distinct symbol and the jackpot condition is met. □
Complexity Analysis
One scan uses exactly $n$ actions. We perform one scan for wheel $1$, two scans for every other wheel, and then $O(n)$ final rotations. Thus the number of actions is at most \[ 1 + n + 2(n-1)n + O(n) < 2n^2 + 3n \le 5150 \] for $n \le 50$, well below the limit of $10\,000$. The local memory usage is $O(n)$.
Implementation Notes
The helper
rotateToalways uses the shorter cyclic rotation.The solution exits immediately when the judge reports $k=1$, as required by the interaction protocol.
Code
C++ solution used for this page.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
int k;
cin >> k;
vector<int> offset(n + 1, 0);
auto ask_rotate = [&](int wheel, int delta) {
cout << wheel << ' ' << delta << '\n' << flush;
offset[wheel] = ((offset[wheel] + delta) % n + n) % n;
if (!(cin >> k)) {
exit(0);
}
if (k == 1) {
exit(0);
}
};
auto rotate_to = [&](int wheel, int target) {
int cur = offset[wheel];
int delta = target - cur;
delta %= n;
if (delta < 0) {
delta += n;
}
if (delta > n - delta) {
delta -= n;
}
if (delta != 0) {
ask_rotate(wheel, delta);
}
};
auto scan_wheel = [&](int wheel) {
vector<int> val(n);
val[0] = k;
for (int i = 1; i < n; ++i) {
ask_rotate(wheel, 1);
val[i] = k;
}
ask_rotate(wheel, 1);
int best = *min_element(val.begin(), val.end());
vector<int> mask(n, 0);
for (int i = 0; i < n; ++i) {
if (val[i] == best) {
mask[i] = 1;
}
}
return mask;
};
vector<int> first_mask = scan_wheel(1);
int target_symbol = -1;
int common_symbol = -1;
for (int i = 0; i < n; ++i) {
if (first_mask[i] == 0 && target_symbol == -1) {
target_symbol = i;
}
if (first_mask[i] == 1 && common_symbol == -1) {
common_symbol = i;
}
}
if (target_symbol == -1) {
target_symbol = 0;
}
if (common_symbol == -1) {
common_symbol = (target_symbol + 1) % n;
}
vector<int> desired(n + 1, 0);
desired[1] = target_symbol;
rotate_to(1, target_symbol);
for (int wheel = 2; wheel <= n; ++wheel) {
vector<int> mask_with_target = scan_wheel(wheel);
rotate_to(1, common_symbol);
vector<int> mask_with_common = scan_wheel(wheel);
int pos = -1;
for (int i = 0; i < n; ++i) {
if (mask_with_target[i] == 1 && mask_with_common[i] == 0) {
pos = i;
break;
}
}
if (pos == -1) {
for (int i = 0; i < n; ++i) {
if (mask_with_target[i] != mask_with_common[i]) {
pos = i;
break;
}
}
}
if (pos == -1) {
pos = 0;
}
desired[wheel] = pos;
rotate_to(1, target_symbol);
}
for (int wheel = 1; wheel <= n; ++wheel) {
rotate_to(wheel, desired[wheel]);
}
return 0;
}
Source Files and Assets
Raw files are still available here when you want the original TeX, C++, or statement assets.
Show raw files
competitive_programming/icpc/2025/I-slot-machine/solution.texC++ implementationcompetitive_programming/icpc/2025/I-slot-machine/solution.cppStatement textcompetitive_programming/icpc/2025/I-slot-machine/statement.txtStatement PDFcompetitive_programming/icpc/2025/I-slot-machine/statement.pdfMetadatacompetitive_programming/icpc/2025/I-slot-machine/meta.jsonYear packetcompetitive_programming/icpc/2025/contest_problems.pdf