Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Think backwards. Instead of deleting from the original sequence and asking where the -th survivor lands, ask: if something is at position after one deletion round, what position did it occupy before that round?
After one operation, exactly the positions divisible by vanish. So the remaining positions are . The -th remaining position is not hard to write directly.
For , the old position of the -th survivor is
Every block of old positions contributes survivors, so after every survivor positions, you skip one deleted position.
Apply that inverse map times starting from . If the value ever becomes bigger than , the original finite sequence does not contain this survivor, so the answer is .
Watch the cursed corner case: deletes every position on the first operation, so the answer is always . Otherwise, , so directly applying the inverse formula times is completely fine.
We start with the sequence
Each operation deletes the elements currently sitting at positions
Then the sequence closes up and positions are renumbered. We need the value at final position after doing this times.
The annoying part is that deletions are based on current positions, not original values. So trying to track which original numbers get deleted forward is a mess. Classic trap. Don't step in it.
Go backwards instead.
Suppose after one deletion round, an element is at position . What was its position before that deletion round?
During one operation, every -th old position is deleted. So among every block of old positions, only survive:
That means the survivor positions after deletion correspond to old positions like this:
So the -th survivor has skipped one deleted old position for every full group of survivors before it.
Therefore, for :
That's the whole trick. Tiny formula, big vibes.
Now if the final -th element exists, we can reverse all operations:
and repeat times:
After reversing all operations, pos is the original position of the final -th survivor. Since the original sequence is literally
that original position is also the answer value.
If at any point pos > 10^{12}, then this supposed survivor would need to come from outside the original sequence. In normal human terms: the resulting sequence has fewer than elements. Output .
There is one special case: .
Then the operation removes positions
so the whole sequence gets nuked on the first operation. Since , the answer is always .
Why checking pos > 10^{12} works
The inverse map tells us the minimum original position needed for the final position to exist after a certain number of remaining reverse steps. If that original position exceeds , then the finite starting sequence simply was not long enough. No extra length simulation is needed.
Complexity
We do exactly iterations per test case.
Since this is the easy version and , with at most tests, this is easily fine. The computer will survive. Probably.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
const ll LIM = 1000000000000LL;
while (t--) {
ll x, y, k;
cin >> x >> y >> k;
if (y == 1) {
cout << -1 << '\n';
continue;
}
ll pos = k;
bool bad = false;
for (ll i = 0; i < x; i++) {
pos += (pos - 1) / (y - 1);
if (pos > LIM) {
bad = true;
break;
}
}
cout << (bad ? -1 : pos) << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
const ll LIM = 1000000000000LL;
while (t--) {
ll x, y, k;
cin >> x >> y >> k;
if (y == 1) {
cout << -1 << '\n';
continue;
}
ll pos = k;
bool bad = false;
for (ll i = 0; i < x; i++) {
pos += (pos - 1) / (y - 1);
if (pos > LIM) {
bad = true;
break;
}
}
cout << (bad ? -1 : pos) << '\n';
}
return 0;
}