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.
Try rewriting each rectangle by its actual side lengths instead of half-sizes: These are always odd. That already tells you something mildly rude about even .
For a fixed pair of rectangles, the number of possible centers depends only on the largest vertical size and largest horizontal size among the two rectangles. If then how many grid positions can hold the whole union?
Now freeze some odd outer dimensions . There are only two structural cases:
That split is the whole problem. Seriously.
In the second case, the union area is Because all side lengths are odd, both differences are even. Write then So for fixed , you’re just counting factor pairs of
Enumerate all odd Their center count is
Sum everything. That’s the accepted solution; the rest is just not screwing up the implementation.
Each of the two centered rectangles has odd side lengths:
So a cross is the union of two axis-aligned odd-by-odd rectangles with the same center.
That already implies a cute fact:
Free pruning. We take those.
Let These are the outer height and outer width of the union.
To fit the whole cross on the grid, the center must stay far enough from every border. The number of valid rows for the center is and similarly the number of valid columns is So every ordered pair of rectangle sizes with outer dimensions contributes placements.
So the real task is:
For every odd and odd , count how many ordered pairs of rectangles have outer dimensions exactly and union area .
That’s the whole game.
Fix odd .
There are only two possibilities.
Then the union area is simply The other rectangle can be any odd sub-rectangle:
How many choices?
So the number of possible second rectangles is
Since the pair is ordered, the full rectangle can be either the first or the second one, so multiply by . But if both rectangles are , that ordered pair was counted twice and should only count once.
Therefore, when , the number of ordered pairs is
Then one rectangle must provide the maximum height, and the other must provide the maximum width. So the pair looks like where and all four numbers are odd.
Their union area is A nicer form is
Now the trick: since are odd, both differences are even. Write with Then So for fixed , the crossing case reduces to counting factor pairs of
If is not a positive integer, there is no crossing contribution. If it is, we count ordered pairs such that
\quad 1\le u\le \frac{R-1}{2}, \quad 1\le v\le \frac{T-1}{2}.$$ Each such $(u,v)$ determines exactly one pair $(P,Q)$, and then there are exactly **2** ordered rectangle pairs: - first is $R\times Q$, second is $P\times T$, - or vice versa. So the crossing contribution is $$2\times \#\{(u,v): uv=K,\ u\le \tfrac{R-1}{2},\ v\le \tfrac{T-1}{2}\}.$$ ## Final algorithm Enumerate all odd $$R=1,3,5,\dots\le n,$$ $$T=1,3,5,\dots\le m.$$ For each pair: 1. let $$W=(n-R+1)(m-T+1);$$ 2. if $$RT=s,$$ add $$W\left(2\cdot \frac{R+1}{2}\cdot \frac{T+1}{2}-1\right);$$ 3. else if $$RT>s$$ and $$RT-s\equiv 0\pmod 4,$$ let $$K=\frac{RT-s}{4};$$ count ordered divisor pairs $(u,v)$ of $K$ satisfying the bounds, and add $$W\cdot 2\cdot \text{count}.$$ That’s it. No DP, no black magic, no pretending $250^4$ is acceptable. ## Why it’s correct We partition all ordered rectangle pairs with outer dimensions $(R,T)$ into exactly two disjoint sets: - one rectangle equals $R\times T$, - or neither does, in which case the maxima come from different rectangles. These are exhaustive and disjoint. - In the first set, union area is exactly $RT$, and the counting formula above is exact. - In the second set, every pair is uniquely represented by $u=(R-P)/2$ and $v=(T-Q)/2$, hence by a factorization of $K=(RT-s)/4$, and vice versa. Then multiplying by the number of valid centers $W$ counts all sextuples $$(a,b,c,d,x_0,y_0)$$ exactly once. So the algorithm is correct. ## Complexity There are at most about $250$ odd values of $R$ and $250$ odd values of $T$, so about $$250\cdot 250 = 62500$$ pairs. For each pair, divisor enumeration costs $$O(\sqrt{K})\le O(\sqrt{nm})\le O(500).$$ In practice it’s much smaller, since here $K\le 62500$. So the total complexity is roughly $$O\left(\frac{n}{2}\cdot \frac{m}{2}\cdot \sqrt{nm}\right),$$ which is easily fast enough for $n,m\le 500$. Memory usage is $$O(1).$$ Clean, brute-forcey in the good way, and very Codeforces-2100-core.#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 n, m, s;
cin >> n >> m >> s;
if (s % 2 == 0) {
cout << 0 << '\n';
return 0;
}
ll ans = 0;
for (int R = 1; R <= n; R += 2) {
ll waysRow = n - R + 1;
int maxU = (R - 1) / 2;
for (int T = 1; T <= m; T += 2) {
ll outerArea = 1LL * R * T;
if (outerArea < s) continue;
ll ways = waysRow * 1LL * (m - T + 1);
if (outerArea == s) {
ll cntSub = 1LL * ((R + 1) / 2) * ((T + 1) / 2);
ll cntPairs = 2 * cntSub - 1; // ordered pairs, equal-full case counted once
ans += ways * cntPairs;
continue;
}
ll diff = outerArea - s;
if (diff % 4 != 0) continue;
ll K = diff / 4;
if (K <= 0) continue;
int maxV = (T - 1) / 2;
ll cntFactorPairs = 0; // ordered (u, v)
for (ll d = 1; d * d <= K; ++d) {
if (K % d != 0) continue;
ll e = K / d;
// (u, v) = (d, e)
if (d <= maxU && e <= maxV) ++cntFactorPairs;
// (u, v) = (e, d)
if (d != e && e <= maxU && d <= maxV) ++cntFactorPairs;
}
// For every (u, v), we have two ordered rectangle pairs:
// (R x Q, P x T) and (P x T, R x Q)
ans += ways * 2LL * cntFactorPairs;
}
}
cout << ans << '\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 n, m, s;
cin >> n >> m >> s;
if (s % 2 == 0) {
cout << 0 << '\n';
return 0;
}
ll ans = 0;
for (int R = 1; R <= n; R += 2) {
ll waysRow = n - R + 1;
int maxU = (R - 1) / 2;
for (int T = 1; T <= m; T += 2) {
ll outerArea = 1LL * R * T;
if (outerArea < s) continue;
ll ways = waysRow * 1LL * (m - T + 1);
if (outerArea == s) {
ll cntSub = 1LL * ((R + 1) / 2) * ((T + 1) / 2);
ll cntPairs = 2 * cntSub - 1; // ordered pairs, equal-full case counted once
ans += ways * cntPairs;
continue;
}
ll diff = outerArea - s;
if (diff % 4 != 0) continue;
ll K = diff / 4;
if (K <= 0) continue;
int maxV = (T - 1) / 2;
ll cntFactorPairs = 0; // ordered (u, v)
for (ll d = 1; d * d <= K; ++d) {
if (K % d != 0) continue;
ll e = K / d;
// (u, v) = (d, e)
if (d <= maxU && e <= maxV) ++cntFactorPairs;
// (u, v) = (e, d)
if (d != e && e <= maxU && d <= maxV) ++cntFactorPairs;
}
// For every (u, v), we have two ordered rectangle pairs:
// (R x Q, P x T) and (P x T, R x Q)
ans += ways * 2LL * cntFactorPairs;
}
}
cout << ans << '\n';
return 0;
}