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.
Look for something every move has in common. The exact order of jumps is a distraction.
Each move increases by exactly . So after moves, the endpoint must satisfy .
For exactly moves, the horizontal increase is the sum of numbers, each one being , , or .
That means the smallest possible after moves is , and the largest possible is . There are no gaps between them.
So compute . The answer is YES iff is divisible by and .
The clean observation is that all three moves have the same value of :
So after exactly moves, we must have
That gives the first hard requirement: must be divisible by . If not, the answer is instantly NO; no amount of parkour cope fixes arithmetic.
Now assume
This is the number of moves. For these moves, each move contributes one of the horizontal values , , or . Therefore the total coordinate must be at least
and at most
So we need
The only subtle point is proving that every integer in this range is actually reachable, not just the endpoints. Write
where . Think of each move as starting from horizontal value , then adding an extra amount:
With moves, we need the extras to sum to , using numbers from . Every value from to is possible: use enough 's, then maybe one , and fill the rest with 's. So there are no missing horizontal totals.
Once is valid, is automatically valid too because fixes it:
So the complete condition is:
This also handles negative naturally. For example, going downward means using more moves, which increases horizontal distance faster, exactly reflected by the upper bound .
Complexity is per test case, with memory.
Research checked: Codeforces problem statement, official editorial thread, and a public C++ solution. The supplied statement remains canonical.
#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 x, y;
cin >> x >> y;
ll sum = x + y;
if (sum % 3 != 0) {
cout << "NO\n";
continue;
}
ll k = sum / 3;
cout << (2 * k <= x && x <= 4 * k ? "YES\n" : "NO\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 x, y;
cin >> x >> y;
ll sum = x + y;
if (sum % 3 != 0) {
cout << "NO\n";
continue;
}
ll k = sum / 3;
cout << (2 * k <= x && x <= 4 * k ? "YES\n" : "NO\n");
}
}