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.
Multiplying can only make a number larger or keep it the same if you multiply by . So if the numbers are already equal, obviously do nothing.
Ask whether one number can become the other directly. For example, can become in one move exactly when is a multiple of .
The same logic works in the other direction: one operation is enough if or .
If neither number divides the other, do not try to be fancy. You can multiply both numbers to reach some common multiple.
A universal target is : multiply by and multiply by . So the answer is only ever , , or .
There are only three possible answers: , , or . That is the whole problem. Tiny, but still easy to overthink if you start doing prime factorization gymnastics for no reason.
What One Operation Can Do
In one operation, we choose any positive integer and multiply either or by .
So from , we can reach any number of the form:
where is a positive integer. That means can become exactly in one operation iff:
for some integer , which is exactly the same as saying:
Similarly, can become in one operation iff:
So:
Why Two Operations Are Always Enough
If neither number divides the other, we still need to make them equal somehow.
We are allowed to multiply both numbers, so just make them both equal to a common multiple. The dumbest possible common multiple is .
Starting from :
Starting from :
So in two operations:
Now both are .
Could the answer be more than ? Nope. Two always works. Case closed.
Could the answer be when neither divides the other? Also nope. With one move, only one number changes, so the changed number must become exactly the unchanged number. That requires divisibility. No divisibility, no one-move magic.
Therefore the minimum is:
Complexity
Each test case uses only a couple modulo checks.
Time complexity: per test case.
Memory complexity: .
Brutally small. The intended solution is basically just not tripping over the statement.
#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--) {
int a, b;
cin >> a >> b;
if (a == b) {
cout << 0 << '\n';
} else if (a % b == 0 || b % a == 0) {
cout << 1 << '\n';
} else {
cout << 2 << '\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;
while (t--) {
int a, b;
cin >> a >> b;
if (a == b) {
cout << 0 << '\n';
} else if (a % b == 0 || b % a == 0) {
cout << 1 << '\n';
} else {
cout << 2 << '\n';
}
}
return 0;
}