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.
The answer can never be huge. Since , you can always use repeatedly and repeatedly, so the cost is at most .
So the whole problem is just deciding whether the answer is or . Cost means every operation uses the exact same pair .
If one pair is used times, then and . So must divide both and .
The best chance is to use the largest possible common divisor: . That gives the smallest proportional step: .
Answer exactly when and . Otherwise answer .
The sneaky part is that we minimize cost, not the number of operations.
A pair only costs money the first time we use it. After that, reusing it is free. So we are really asking:
How many different move vectors do we need?
And because , the answer is never more than :
Both pairs are valid because their coordinates are between and . So the answer is either or . Nice. Tiny problem, big ego.
When is cost 1 possible?
Cost means we choose one pair , pay for it once, and then reuse that same pair some number of times.
Suppose we use it times. Then the total movement is:
We need this to equal , so:
Therefore, must divide both and .
The largest possible such is:
Using gives the smallest possible integer step:
If even this smallest proportional step does not fit inside the allowed range , then no one-pair strategy can work.
So cost is possible exactly when:
and
where .
If both are true, print 1. Otherwise, print 2.
Why checking only is enough
Maybe it feels like we should try every common divisor. Nope.
If we use some common divisor , the move is:
Larger means smaller coordinates in the move. Since is the largest common divisor, it gives the smallest possible move among all one-pair options.
So:
Then answer is , because we already know always works.
Complexity: per test case because of gcd.
#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;
while (t--) {
ll a, b, k;
cin >> a >> b >> k;
ll g = gcd(a, b);
if (a / g <= k && b / g <= k) cout << 1 << '\n';
else cout << 2 << '\n';
}
}#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;
while (t--) {
ll a, b, k;
cin >> a >> b >> k;
ll g = gcd(a, b);
if (a / g <= k && b / g <= k) cout << 1 << '\n';
else cout << 2 << '\n';
}
}