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.
After choosing , the new sum is
where must be a divisor of . So the whole problem is choosing which divisor gives an even sum.
The parity condition is only about and . Their sum is even exactly when they have the same parity.
The largest possible sum without caring about parity is always with :
For any divisor split ,
If is even, you are instantly done. This happens exactly when is odd, meaning both and are odd.
For the remaining cases, only one backup can beat everything else: put the smallest possible leftover divisor that makes parity work. If is odd and is even, you need both terms even, so must be even; the smallest possible even leftover is , giving . If is even, is always even, so must be even too; again try . If is odd there is no even , so impossible.
Let the chosen divisor be . After the operation, the numbers become
so the sum is
We need the biggest even value of this over all divisors of .
First, ignore parity
Write
so and the sum is
The obvious greedy-looking move is , which gives
And this is not just a guess. For any divisor split ,
Since and , this is nonnegative. So is the absolute maximum possible sum.
If is even, answer it immediately. This happens when is odd, i.e. both and are odd.
That gives:
Now handle parity properly
A sum is even iff both terms have the same parity:
Let . Then , and we are choosing a divisor of instead. The sum is
Since the first term grows as gets smaller, the best valid choice wants the smallest possible leftover that makes the parity work. No need to cosplay as a factorization algorithm here.
Case 1: is even
Then is always even. So we need to be even too.
If is odd, every divisor is odd, so this is impossible.
If is even, the smallest even divisor we can leave behind is . That means
and the answer is
Case 2: is odd
Then has the same parity as .
For even , to make the sum even with odd , we need and to have the same parity. Since is even, they cannot both be odd, so they must both be even. That means must be divisible by .
If is divisible by , again the best leftover is , so
and the answer is
If , there is only one factor of in , so and cannot both be even. Impossible.
Final rules
You can implement the second rule directly by computing
when is even, and checking if is even. If yes, it is optimal; if not, there is no valid answer.
All values fit in 64-bit signed integers because , and the answers are at most about .
Time complexity: per test case.
#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;
cin >> a >> b;
if ((a & 1LL) && (b & 1LL)) {
cout << a * b + 1 << '\n';
} else if ((b & 1LL) == 0) {
ll ans = a * (b / 2) + 2;
if ((ans & 1LL) == 0) cout << ans << '\n';
else cout << -1 << '\n';
} else {
cout << -1 << '\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;
cin >> a >> b;
if ((a & 1LL) && (b & 1LL)) {
cout << a * b + 1 << '\n';
} else if ((b & 1LL) == 0) {
ll ans = a * (b / 2) + 2;
if ((ans & 1LL) == 0) cout << ans << '\n';
else cout << -1 << '\n';
} else {
cout << -1 << '\n';
}
}
}