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 of each civilization as receiving whole teams, not individual people. So each side's population must be a sum of some 's and 's.
Which population sizes can one civilization have? Using teams of size and , every size is possible except ; also is possible if no team chooses that civilization.
So the task is basically: choose people for one civilization and for the other, minimizing , with the only forbidden side size being .
For large enough even , split evenly: . For large enough odd , split as close as possible: and .
The only traps are and . The naive answer fails there because a side with exactly person cannot be made from teams of or .
Each team has size or , and a whole team chooses exactly one civilization. So we are not splitting people freely; each civilization gets a sum of team sizes.
The key observation is simple:
A nonnegative integer can be formed as a sum of 's and 's iff .
Why?
Now suppose one civilization gets people. The other gets people. We need both and to be valid side populations, meaning neither can equal . The difference is
To minimize this, we want as close to as possible.
If is even and , take
Then both civilizations get people, so both are valid, and the answer is .
If is odd and , take
Both values are at least , so both are valid. Their difference is , and no odd total can be split with difference , so the answer is .
Now for the annoying tiny cases, because of course they exist:
So the final formula is:
The common wrong assumption is "just balance people individually." Nope. Teams are indivisible, and a civilization cannot receive exactly one person. That's the whole problem hiding under the couch.
Complexity per test case is , with memory.
#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 n;
cin >> n;
if (n == 2 || n == 3) {
cout << n << '\n';
} else {
cout << (n % 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 n;
cin >> n;
if (n == 2 || n == 3) {
cout << n << '\n';
} else {
cout << (n % 2) << '\n';
}
}
return 0;
}