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.
A configuration is just a pair: number of chickens and number of cows. No ordering, no positions, no farm layout DLC.
If there are chickens and cows, the total legs are .
The left side is always even, so if is odd, there are zero possible configurations immediately.
For even , divide by : . Once you choose , the value of is forced.
The cow count can be any integer from to , so the answer is for even .
Core Idea
Let:
A chicken has legs and a cow has legs, so every valid configuration must satisfy:
where and are integers.
We are counting how many different pairs work.
Odd Is Impossible
The expression is always even because both terms are even. So if is odd, there is no way to make exactly legs. Answer: .
That handles cases like . No need to overthink it. Odd legs on a chicken/cow farm means someone is lying or the farm is cursed.
Even
Now suppose is even. Divide the equation by :
Let:
Then:
Here is the key move: choose the number of cows . Then the number of chickens is forced:
So every valid cow count gives exactly one valid configuration, as long as .
That means:
Since can also be , the possible cow counts are:
The number of integers in that range is:
Substitute :
So the full answer is:
Checking Samples
For :
Answer: .
For :
Answer: .
For :
Answer: .
Complexity
Each test case is solved with one parity check and one division.
Time complexity: per test case.
Memory complexity: .
Tiny problem, tiny formula. Beautiful. Annoyingly easy to overcomplicate, though.
#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 == 1) {
cout << 0 << '\n';
} else {
cout << n / 4 + 1 << '\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 == 1) {
cout << 0 << '\n';
} else {
cout << n / 4 + 1 << '\n';
}
}
return 0;
}