We are given problem: we need to count number of ways to buy exactly n liters of cola using bottles of sizes 0.5, 1, and 2 liters. There are limited numbers: a bottles of size 0.5, b bottles of size 1, c bottles of size 2. Bottles are indistinguishable; we only count combinations (x, y, z) where x is number of 0.5L bottles used, y is number of 1L bottles, z is number of 2L bottles, such that:
0.5x + 1y + 2*z = n (Equation in liters)
0 ≤ x ≤ a, 0 ≤ y ≤ b, 0 ≤ z ≤ c.
We need to count integer nonnegative solutions within limits.
n up to 10000, a,b,c up to 5000.
We need to compute number of ways.
Brute force over z? For each z, we have remaining n - 2z. But we have also 0.5 liters from x. Better to multiply by 2 to avoid fractions. Let total volume in half-liters: target T = 2n (since 0.5L is 1 half-liter, 1L is 2 half-liters, 2L is 4 half-liters). Then we have:
x * 1 + y * 2 + z * 4 = T, with constraints: 0 ≤ x ≤ a, 0 ≤ y ≤ b, 0 ≤ z ≤ c.
So count number of integer triples (x,y,z) satisfying:
x + 2y + 4z = T, with 0 ≤ x ≤ a, 0 ≤ y ≤ b, 0 ≤ z ≤ c.
Now T up to 20000 (since n up to 10000). a,b,c up to 5000.
We can iterate over z from max(0, ceil((T - a - 2b)/4)?) Actually we can iterate z from 0 to min(c, floor(T/4)). For each z, we have remaining: T' = T - 4z. Then we need x + 2y = T', with 0 ≤ x ≤ a, 0 ≤ y ≤ b, x, y integers.
Then for each such z, count number of solutions (x,y). This reduces to counting number of y (or x) values such that constraints hold.
x = T' - 2y must be between 0 and a, and integer, and y between 0 and b. Also x must be integer so T' - 2y is automatically integer if T' is integer; T' integer. So we need y integer with:
0 ≤ y ≤ b
0 ≤ T' - 2y ≤ a
Also y ≤ floor(T'/2) and y ≥ ceil((T' - a)/2). So:
y_min = max(0, ceil((T' - a)/2))
y_max = min(b, floor(T'/2))
And then number of y = max(0, y_max - y_min + 1) if y_min ≤ y_max.
We need to compute ceil division carefully.
But note that T' can be up to 20000, but that's fine.
We iterate z from 0 to min(c, floor(T/4)). That's at most 5000. So overall O(c) ~ 5000 operations, trivial.
But we must be careful: a, b, c up to 5000, but n up to 10000 => T up to 20000. So fine.
Edge Cases: If T' negative then break? Since as z increases, T' decreases, once T' < 0 we stop. So for z from 0 to min(c, floor(T/4)) inclusive.
Complexity O(c) = up to 5000, acceptable.
But wait: Are there any other constraints? The problem says "print the unique number — the solution to the problem. If it is impossible, print 0." The answer could be large? Possibly number of ways could be large? n up to 10000, a,b,c up to 5000, maximum number of combinations? Let's approximate: For each z, number of y up to b, but b up to 5000, and z up to 5000, so max ways could be ~ 25e6, which fits in 32-bit? Actually up to (5001)(5001) ~ 25 million, but maybe more if constraints allow many y for many z. Actually worst-case n large and a,b,c large, say n=10000, T=20000, a=b=c=5000. For each z, y range could be up to min(b, floor(T'/2)) which could be up to 5000. Number of z that yield positive counts could be many. Let's estimate: For T=20000, z can go from 0 to 5000 (but floor(20000/4)=5000). So z from 0 to 5000, that's 5001 values. For each z, y_min = max(0, ceil((T' - a)/2)). With a=5000, T'=20000-4z. For moderate z, T' large, ceil((T' - 5000)/2) maybe still >0. Let's compute y_max = min(5000, floor(T'/2)). For z=0, T'=20000, floor(20000/2)=10000, y_max=5000. y_min = ceil((20000-5000)/2)=ceil(15000/2)=7500, so y_min=7500 > y_max=5000 => no solutions. Actually with a=5000, we need x = T' - 2y ≤ 5000 => 2y ≥ T' - 5000 => y ≥ (T' - 5000)/2. For large T', this lower bound may exceed b. So for z small, there may be no solutions; for z large enough to reduce T', solutions appear. So number of z with solutions may be limited. Let's find condition for existence: Need y_min ≤ y_max => max(0, ceil((T' - a)/2)) ≤ min(b, floor(T'/2)). Since T' positive. Typically we need (T' - a)/2 ≤ floor(T'/2) etc. Actually more precisely, we need there exists y integer such that 0 ≤ y ≤ b and T' - 2y between 0 and a. This is equivalent to 2y between T' - a and T'. So y between ceil((T' - a)/2) and floor(T'/2). So we need ceil((T' - a)/2) ≤ floor(T'/2) and also ≤ b and lower bound ≤ b, but min ensures. So the crucial is that ceil((T' - a)/2) ≤ floor(T'/2). This is equivalent to (T' - a)/2 ≤ T'/2 => always true if a≥0? Actually ceil((T' - a)/2) ≤ floor(T'/2) holds if (T' - a) ≤ T' (obviously) but careful with floor/ceil. For integer T', floor(T'/2) is T'//2. And ceil((T' - a)/2) ≤ T'//2 is equivalent to (T' - a + 1) //2 ≤ T'//2 (if using integer division). Actually we can check: For existence without b constraint, need T' - a ≤ 2(T'//2) and also (T' - a)/2 ≤ T'//2? Let's just compute: The interval for y is [L, R] where L = ceil((T' - a)/2), R = floor(T'/2). The interval is non-empty if L ≤ R. That's equivalent to ceil((T' - a)/2) ≤ floor(T'/2). Since floor(T'/2) = (T' // 2). And ceil((T' - a)/2) = (T' - a + 1) // 2 if (T' - a) not necessarily integer? But T' and a are integers, so T' - a is integer; ceil((T' - a)/2) = (T' - a + 1)//2 if (T' - a) is odd? Actually for integer K, ceil(K/2) = (K+1)//2 (integer division flooring). Since integer division floors: floor division in C++: (K+1)/2 for positive? Actually if K >=0, (K+1)/2 integer division truncates towards zero? In C++, for positive integers, (K+1)/2 gives floor((K+1)/2). For K even, K/2 = (K+1)/2? Example K=4, (4+1)/2=2.5->2 (floor), but ceil(4/2)=2, same. For K=5, (5+1)/2=3, ceil(5/2)=3. So yes, ceil(K/2) = (K+1)/2 for K>=0. So L = (T' - a + 1)/2 (integer division). R = T'/2 (integer division). Condition L ≤ R => (T' - a + 1)/2 ≤ T'/2. Multiply by 2: T' - a + 1 ≤ T'? Actually careful: multiply by 2 but need to be careful with integer division? Let's work with inequalities directly: L ≤ R => ceil((T' - a)/2) ≤ floor(T'/2). Since floor(T'/2) = ⌊T'/2⌋. And note that ⌊T'/2⌋ = (T' - (T' mod 2))/2. But easier: The condition is that there exists integer y such that both constraints: y ≤ T'/2 and y ≥ (T' - a)/2 (not necessarily integer) and y integer. The maximal y integer ≤ T'/2 is floor(T'/2). The minimal integer ≥ (T' - a)/2 is ceil((T' - a)/2). So existence is exactly ceil((T' - a)/2) ≤ floor(T'/2). This is equivalent to (T' - a)/2 ≤ T'/2, i.e., -a/2 ≤ 0, always true. However, the integer rounding could cause issue when (T' - a)/2 is close to T'/2. Let's test: T'=5, a=1 => (5-1)/2=2, ceil=2; floor(5/2)=2 => L=2,R=2 => ok. T'=5,a=0 => (5-0)/2=2.5 ceil=3; floor=2 => L=3 >2 => no solution. So indeed a must be >= (T' mod 2)? Actually condition: There is an integer y between (T' - a)/2 and T'/2 inclusive. This interval length is a/2. So if a/2 < the fractional gap? For existence, we need (T' - a)/2 ≤ T'/2, obviously. But because we need an integer, the condition is that floor(T'/2) - ceil((T' - a)/2) >= 0. This is true if a >= (T' mod 2)? Let's derive exact condition: Write T' = 2q + r, where r = T' mod 2, r ∈ {0,1}. Then floor(T'/2) = q. Also (T' - a)/2 = q + r/2 - a/2. Then ceil((T' - a)/2) = ceil(q + (r - a)/2). Let's compute. Let a be integer. The condition L ≤ q is equivalent to ceil(q + (r - a)/2) ≤ q. That means the smallest integer ≥ q + (r - a)/2 must be ≤ q. This can happen only if q + (r - a)/2 ≤ q, i.e., (r - a)/2 ≤ 0 => a ≥ r. And if (r - a)/2 is exactly an integer less than or equal? Actually if q + (r - a)/2 is exactly q, i.e., (r - a)/2 = 0 => a = r. Then ceil(q) = q, so L = q, okay. If (r - a)/2 < 0, then q + something < q, so ceil could be q if the fractional part makes it? Example: q=2, r=0, a=1 => (0-1)/2 = -0.5 => q + (-0.5) = 1.5, ceil = 2 => L=2, which equals q=2, ok. That fits: a=1, r=0 => a ≥ 0? r=0, a=1≥0 yes. Example: q=2, r=1, a=0 => (1-0)/2 = 0.5 => q+0.5=2.5, ceil=3 > q=2 => no solution. So condition seems to be a ≥ r? Actually for r=0, any a>=0 gives L ≤ q? Let's test r=0, q=2, a=0 => (0-0)/2=0 => q+0=2, ceil=2 => L=2, ok. So a=0 works when r=0. When r=0, r=0 => a ≥ 0 always true. So condition is trivial? Wait test r=0, a=0 works. So maybe condition is always satisfied? Let's test r=0, a=0 we got L=2, R=2, ok. So that is fine. So when a is 0, if T' even, then x must be 0; we have equation 2y = T', y = T'/2 must be integer and within 0..b, that is a single solution if T' even and within bounds. That corresponds to L = ceil((T')/2) = T'/2, R = T'/2, works. So indeed with a=0, for T' even, L=R = T'/2. For T' odd, then (T' - 0)/2 = T'/2, but T' odd => T'/2 = fractional .5, ceil = (T'+1)/2, floor = (T'-1)/2, L > R, no solution. So the condition for existence without b is that either a >= r or something? Actually derived: L = ceil((T' - a)/2), R = floor(T'/2). For existence, need L ≤ R. This can be simplified to (T' - a + 1)//2 ≤ T'//2. It's not always true. For given T', as a increases, L decreases. So for small a there may be no solution; for larger a there will be. So the condition depends on a. So we'll compute directly per z.
Given constraints, simple loop over z, compute T', compute y_min = max(0, (T' - a + 1)/2) if T' - a could be negative? Actually careful: L = ceil((T' - a)/2). If T' - a < 0, then (T' - a)/2 negative, ceil will be a negative number or zero. But we also have y≥0, so y_min = max(0, ceil((T' - a)/2)). So we compute L0 = ceil((T' - a)/2). But if T' - a is negative, then (T' - a)/2 is negative; ceil of a negative number is less than or equal to 0? Example T'=3, a=5 => (3-5)/2 = -1, ceil = -1? Actually -1? Let's compute: (-1) is integer, ceil(-1) = -1. But then y_min = max(0, -1)=0. So that's fine.
Better to compute L0 = (T' - a) // 2? Not directly. For integer division in C++: ceil division for integers: ceil(p/q) = (p + q - 1) // q for p>=0, but if p can be negative, careful. Since T' and a up to 20000, T' - a can be negative. We can compute L0 = (T' - a + 1) // 2? That formula works for non-negative? For negative numbers, integer division truncates toward zero in C++, so we cannot rely on that for ceil. Better to compute using if-else or use formula: ceil division for integers: (p + d - 1) // d for p>=0. For possibly negative, we can use: (p > 0) ? (p + d - 1)/d : p/d (since p/d truncates toward zero, which for negative p is ceil? Actually in C++, integer division truncates toward zero. For negative p, p/d gives the truncated toward zero, which is greater than or equal to the actual quotient? Example p=-3, d=2 => -3/2 = -1 (since truncation toward zero). The actual quotient is -1.5, floor is -2, ceil is -1. So p/d (trunc toward zero) gives the ceiling when p is negative? Check: ceil(-1.5) = -1, and -3/2 = -1, yes. So for negative p, p/d (trunc toward zero) gives the ceiling. For positive p, p/d gives floor. So to compute ceil(p/d) for integer p and d>0, we can do: (p + d - 1) / d if p>=0, else p/d. This works in C++ with truncation toward zero. So we can compute L0 = (T' - a >= 0) ? (T' - a + 1) / 2 : (T' - a) / 2. But careful: when p is exactly divisible? For p>=0, (p+1)/2 gives ceil? Actually for p even: p=4 => (4+1)/2=2.5 -> 2 (floor) but ceil(4/2)=2, OK. For p odd: p=5 => (5+1)/2=3, ceil=3. So yes, (p+1)/2 yields ceil for non-negative integers. So L0 = (T' - a >= 0) ? (T' - a + 1) / 2 : (T' - a) / 2. However, integer division truncates toward zero, and for negative p, p/2 yields ceiling as argued. Let's test: T'=3, a=5 => p = -2 => p/2 = -1, which is ceil(-1) = -1. Correct. So L0 = ceil((T' - a)/2). Then y_min = max(0, L0). For y_max: R = floor(T'/2) = T' / 2 (integer division trunc toward zero). Since T' >=0, T'/2 is floor. So y_max = min(b, T'/2). Then count = max(0, y_max - y_min + 1) if y_min <= y_max.
But also need to ensure x = T' - 2y is ≤ a. However, our lower bound on y from x ≤ a ensures that x ≤ a. But we also need x ≥ 0, which is ensured by y ≤ floor(T'/2). Also need x ≤ a, which is enforced by y ≥ ceil((T' - a)/2). So this counting is correct.
Alternatively, we could iterate y from 0 to b and check if x is within a and integer, but that would be O(b) per z, up to 5000*5000=25e6, still acceptable but maybe borderline time? 25 million simple operations is fine, but we can do O(c) with formula.
But we must also consider that a, b, c up to 5000, but T could be up to 20000, z up to min(c, floor(T/4)) ≤ 5000. So O(5000) is better. However, we need to compute ceil division correctly. Simpler: For each z, we can compute y_min as follows:
We need y such that 0 ≤ y ≤ b and T' - 2y between 0 and a. So:
T' - 2y ≥ 0 => y ≤ T'/2 => y ≤ floor(T'/2).
T' - 2y ≤ a => 2y ≥ T' - a => y ≥ ceil((T' - a)/2).
So y_min = ceil((T' - a)/2) if T' - a >0? Actually if T' - a ≤ 0, then any y satisfies x ≤ a? Because if T' - a ≤ 0, then T' ≤ a, so for any y satisfying x = T' - 2y ≥ 0, we automatically have x ≤ T' ≤ a. So lower bound becomes 0? Wait: Condition x ≤ a => T' - 2y ≤ a => 2y ≥ T' - a. If T' - a is negative, then 2y ≥ negative number is always true for nonnegative y. So the constraint is vacuous, so lower bound from that is 0. So we can define lower = max(0, ceil((T' - a)/2) if T' - a > 0 else 0). But our L0 = ceil((T' - a)/2) could be negative, then max(0, L0) gives 0. So consistent: y_min = max(0, ceil((T' - a)/2)). That's fine.
We also have y must be integer. So we can compute as described.
Implementation steps:
- Read n, a, b, c.
- T = 2*n.
- ans = 0.
- For z = 0; z <= c && 4z <= T; ++z:
T_prime = T - 4z.
// Compute y_max:
y_max = min(b, T_prime / 2); // integer division truncates toward zero, okay.
// Compute y_min:
// Compute L0 = ceil((T_prime - a) / 2). Use conditional:
// If T_prime - a >= 0:
// L0 = (T_prime - a + 1) / 2;
// else:
// L0 = (T_prime - a) / 2; // This yields ceil because negative.
// Then y_min = max(0, L0).
// If y_min <= y_max:
// ans += (y_max - y_min + 1).
- Print ans.
Edge Cases: T_prime may be negative? Loop stops when 4*z <= T, so T_prime >=0.
But wait: x must also be ≤ a. In our derivation, y_min ensures x ≤ a. However, we also need to ensure x = T_prime - 2y is ≤ a. For y >= y_min, we have that. But also need x ≥ 0, which is ensured by y ≤ floor(T_prime/2). So fine.
But also need x ≤ a, but our y_min derived from x ≤ a. However, is there any condition that y_min might be > y_max even though there exists y such that x ≤ a but also that x is non-negative? That's exactly the condition we used: y_min > y_max -> no solutions.
Now also need to ensure that x computed from y is ≤ a, but also x must be ≤ a and also x ≤ a is integer. Our condition x = T_prime - 2y ≤ a gives y ≥ ceil((T_prime - a)/2). So that's covered.
Also need to ensure that x ≤ a and also x integer automatically given y integer. But also need x ≥ 0, which is y ≤ floor(T_prime/2). So all constraints.
Now also need to ensure that x ≤ a is satisfied; but also we need x ≤ a. Our lower bound ensures that if y >= y_min, then x ≤ a, but also we need to ensure that x is integer and within [0,a]. However, there is another nuance: For some y that satisfies y_min ≤ y ≤ y_max, x = T_prime - 2y might be > a? Let's verify: y_min is ceil((T_prime - a)/2). For any y >= y_min, we have y >= (T_prime - a)/2, so 2y >= T_prime - a => T_prime - 2y ≤ a. So indeed x ≤ a holds. So it's correct.
Now about the case when T_prime - a is negative, ceil((T_prime - a)/2) is negative or zero. Then y_min = 0. So that's fine.
Now we must also ensure that x ≤ a but also x ≤ a as per supply. Also we need to ensure that x does not exceed a. Since we are limiting y to be >= y_min, x automatically ≤ a. However, is there any chance that x could be negative? For y <= y_max (which is floor(T_prime/2)), we have y ≤ T_prime/2, so T_prime - 2y ≥ 0, so x >= 0. So both constraints are satisfied.
Thus the counting method is correct.
Now we need to output answer. But note: The problem might ask for large numbers? The answer could be up to? Let's estimate maximum possible ways. Maximum when a,b,c large and n such that many combos exist. For given n, T=2n. The total number of nonnegative solutions (unbounded) to x+2y+4z = T. This is like partitions. The number of solutions grows like O(T^2) maybe. For T=20000, unbounded solutions count is about floor(T/4) * something. Actually number of z from 0 to floor(T/4). For each z, number of y from 0 to floor((T-4z)/2). That sum_{z=0}^{floor(T/4)} (floor((T-4z)/2)+1) = sum_{z} (floor((T-4z)/2)+1). For T=20000, approximate sum_{z=0}^{5000} (10000-2z+1?) Actually (T-4z)/2 = 10000 - 2z, so floor is 10000-2z (since T even? T=20000 even, so T-4z even, division exact). So count per z = (10000-2z+1) = 10001-2z. Sum z=0..5000: (5001)10001 - 2 * (50005001/2?) Actually sum_{z=0}^{5000} (10001-2z) = (5001)10001 - 2 * (50005001/2) = 500110001 - 50005001 = 5001*(10001-5000) = 5001*5001 = 25,010,001. So about 25 million unbounded. With limits a,b,c=5000, this would reduce but not exceed 25 million. So answer fits within 32-bit signed int? 25 million fits in 32-bit. But wait, could it be more? That's for T=20000. If n is smaller, it's less. So answer ≤ 25 million. However, a,b,c can be up to 5000, but n up to 10000, T=20000, maximum ways maybe less than that because of limits. Actually the unbounded count we computed is 25,010,001. That's within 2^31 (~2.147e9). So safe for int. But maybe we need to use 64-bit? 25 million fits in int, but just to be safe we can use long long.
But wait: The unbounded count we computed (25 million) is when a,b,c are at least that high (a must be at least max x needed, b at least max y needed). For T=20000, max x is when z=0,y=0 => x=T=20000, which is > a=5000, so many solutions will be cut. But could the number of feasible solutions exceed 25 million? The unbounded number is the absolute max if limits are infinite, which is 25 million. With limits smaller, the count is less. So answer ≤ 25,010,001. That's well within 32-bit signed int. However, we must ensure we don't overflow intermediate calculations. Using int is okay, but for safety use long long.
But note: a,b,c up to 5000 but T up to 20000. So maximum number of valid y per z is at most b+1 ≤ 5001, and number of z is at most 5001, product ≤ about 25 million. So ans fits in 32-bit int. So we can use int.
But wait, the problem statement might have larger constraints? It says n ≤ 10000, a,b,c ≤ 5000. So yes.
Now we should also consider that a is number of 0.5L bottles, which we multiplied by 1 in half-liters, but a up to 5000. So x ≤ a = 5000. So when T is large, many unbounded solutions would require x > 5000, so they are excluded. Our counting method automatically handles that via lower bound y_min which might be > 0 to enforce x ≤ a.
Double-check: For given T_prime, we need x = T_prime - 2y ≤ a. So the lower bound y_min = ceil((T_prime - a)/2). If T_prime is large, y_min becomes large, possibly > y_max, then no solutions. So that's fine.
Now confirm with examples.
Example 1:
Input: 10 5 5 5
n=10, T=20. a=5,b=5,c=5.
We need to count triples (x,y,z) with x+2y+4z=20, 0≤x≤5,0≤y≤5,0≤z≤5.
We can manually compute or trust answer 9.
Let's test with algorithm:
z from 0 to min(5, floor(20/4)=5):
z=0: T'=20. y_max = min(5, 10)=5. Compute L0 = ceil((20-5)/2)=ceil(15/2)=8? Actually (20-5)=15, ceil(15/2)=8. So y_min = max(0,8)=8. y_min=8 > y_max=5 => 0 solutions.
z=1: T'=16. y_max = min(5,8)=5. L0 = ceil((16-5)/2)=ceil(11/2)=6. y_min=6 >5 =>0.
z=2: T'=12. y_max = min(5,6)=5. L0 = ceil((12-5)/2)=ceil(7/2)=4. y_min= max(0,4)=4. y_max=5 => y=4,5 => count=2. Check: For y=4 => x=12-8=4 (≤5). For y=5 => x=12-10=2 (≤5). Both valid. Also need check x ≤5: both ok. So 2 solutions.
z=3: T'=8. y_max = min(5,4)=4. L0 = ceil((8-5)/2)=ceil(3/2)=2. y_min=2. y_max=4 => y=2,3,4 => 3 solutions. Check: y=2 => x=8-4=4; y=3 => x=8-6=2; y=4 => x=8-8=0. All ≤5. So 3 solutions.
z=4: T'=4. y_max = min(5,2)=2. L0 = ceil((4-5)/2)=ceil(-1/2)=0? (-0.5 ceil =0? Actually (-0.5) ceil = 0? Let's compute: (-0.5) ceiling is 0? Ceil of negative number is the least integer greater or equal: ceil(-0.5)=0? Wait: ceil(-0.5) = 0? Actually the ceiling of a number is the smallest integer ≥ that number. For -0.5, integers greater or equal are 0, -0? Actually -0.5: integers ≥ -0.5 are 0,1,2,... also -0? But integer -0 is 0. So smallest such integer is 0. Yes, ceil(-0.5)=0. But check: -0.5 ceil = 0. So L0 = ceil((4-5)/2) = ceil(-1/2) = 0. So y_min = max(0,0)=0. y_max=2 => y=0,1,2 => 3 solutions? But check x: For y=0 => x=4, (4 ≤5); y=1 => x=2; y=2 => x=0. So 3 solutions.
z=5: T'=0. y_max = min(5,0)=0. L0 = ceil((0-5)/2)=ceil(-5/2)=ceil(-2.5) = -2? Actually ceil(-2.5) = -2. So L0 = -2, y_min = max(0,-2)=0. y_max=0 => y=0 gives x=0, valid (x=0 ≤5). So 1 solution.
Total: 2+3+3+1=9. Yes.
Example 2:
Input: 3 0 0 2
n=3, T=6. a=0,b=0,c=2.
Loop:
z=0: T'=6, y_max = min(0,3)=0; L0 = ceil((6-0)/2)=ceil(3)=3; y_min=max(0,3)=3 >0 =>0.
z=1: T'=2, y_max = min(0,1)=0; L0 = ceil((2-0)/2)=ceil(1)=1; y_min=1>0 =>0.
z=2: T'=-2 but 4*z=8>6, loop stops. So ans=0. Good.
So algorithm works.
Now complexity O(min(c, floor(T/4))) which is ≤ 5000.
Now we should consider if there are any pitfalls: The variables are integers, but when computing ceil for negative numbers, we used the formula with conditional. We can also compute using integer arithmetic: L0 = (T_prime - a + (2-1)) // 2? Not for negative. Simpler: L0 = (T_prime - a + 1) / 2 if T_prime - a >= 0 else (T_prime - a) / 2. Since division truncates toward zero, for negative, (T_prime - a) / 2 gives the ceiling? Let's verify: In C++ integer division for negative numbers truncates toward zero. For p negative, p/2 yields the integer quotient truncating toward zero. For example p=-1: -1/2 = 0 (since 0.5 truncates to 0). Ceil(-0.5) = 0. So works. For p=-3: -3/2 = -1 (since -1.5 truncates to -1). Ceil(-1.5) = -1. Works. For p=-2: -2/2 = -1. Ceil(-1) = -1. Works. So indeed, for negative p, p/2 yields ceil(p/2) because truncation toward zero from negative side goes upward? Let's analyze: For p negative, p = -k, k>0. p/2 = floor(p/2) if p/2 is negative? Actually standard integer division in C++ prior to C++11 is implementation-defined for negative numbers; but from C++11, division truncates toward zero (i.e., fractional part discarded). So -3/2 = -1. So that's floor? floor(-1.5) = -2, so -1 is actually ceil. So indeed it's ceil for negative numbers. So the conditional can be avoided if we use: L0 = (T_prime - a) / 2; but if T_prime - a is positive odd, (T_prime - a)/2 truncates down, but we need ceil. For positive, we need (p+1)/2. So we need conditional. So we implement conditional.
But we can compute L0 = (T_prime - a + 1) / 2 if (T_prime - a) >= 0 else (T_prime - a) / 2. However, careful: When T_prime - a is non-negative, (T_prime - a + 1) / 2 gives ceil. For T_prime - a negative, (T_prime - a) / 2 gives ceil due to truncation toward zero. So we can do:
int diff = T_prime - a;
int L0;
if (diff >= 0)
L0 = (diff + 1) / 2;
else
L0 = diff / 2; // because integer division truncates toward zero, which for negative gives ceil
Then y_min = max(0, L0).
But is there a case where diff negative but diff/2 yields floor instead? Let's test diff = -1: -1/2 = 0? Actually -1/2 = 0? In C++ with truncation toward zero, -1/2 = 0. But ceil(-0.5)=0, correct. diff=-3: -3/2 = -1? Actually -3/2 = -1 (since -1.5 truncates to -1). Ceil(-1.5) = -1, correct. diff=-2: -2/2 = -1, ceil(-1)= -1, correct. So works.
Now also need to check when diff is even negative? Already covered.
Thus fine.
But we also need to consider that y_max = T_prime / 2 (floor). Since T_prime >=0, integer division truncates toward zero, which is floor. So good.
Now what about the possibility that T_prime is odd and we have x = T_prime - 2y; x must be integer, but if T_prime odd, then x parity must match? Actually x = T_prime - 2y, so if T_prime is odd, then x is odd; but x must be integer, so that's fine. The only thing is that y must be integer, but x is automatically integer. Our y_min and y_max derived from constraints are integer. However, we also need to ensure that x ≤ a. In the case where T_prime is odd and a is small, the lower bound might be fractional, but we ceil it to integer, so it's fine.
Now also check that the computed count may be negative if y_min > y_max; we take max(0, ...).
Now consider potential overflow? None.
Thus solution is straightforward.
We should also consider that we might loop z from 0 to min(c, T/4). But note that T/4 might be fractional; floor division. So z_max = min(c, T/4). Since T is integer, we can compute max_z = min(c, T/4). But careful: if T is not divisible by 4, floor(T/4) works. So we can compute:
int max_z = min(c, T / 4);
But is there possibility that 4z <= T, but also we might have z > c? So we limit to c. So for (z = 0; z <= c && 4z <= T; ++z) works.
Now we must output answer as integer.
Now verify with some other cases.
Edge Cases:
-
n=1, a=2,b=0,c=0 => T=2. a=2 (two half-liter bottles). We need x such that x = 2. x≤2 => possible (x=2). So 1 way. Loop: z from 0 to min(0,0)=0 => only z=0, T'=2. y_max = min(0,1)=0. diff = 2-2=0 => L0 = (0+1)/2 = 0? Actually (0+1)/2=0 (since integer division) but we need ceil(0/2)=0, so L0=0. y_min = max(0,0)=0. y_max=0 => count 1. So ans=1.
-
n=1, a=1,b=0,c=0 => T=2, a=1 => x must be 2 but x ≤1 => impossible. Loop: z=0, T'=2, y_max=0, diff=2-1=1 => L0 = (1+1)/2=1, y_min = max(0,1)=1 > y_max=0 => count 0. So ans=0. Good.
-
n=2, a=0,b=1,c=0 => T=4. b=1 (one 1L bottle). Solutions: y=1 => x+2=4 => x=2, but x must be integer and ≤0? a=0 so x=2 not allowed. Also y=0 => x=4, not allowed. Actually y=2? but b=1. So no solution. Our algorithm: z=0,T'=4,y_max=min(1,2)=1. diff=4-0=4? Actually a=0 so diff=4. L0 = ceil(4/2)=2? (4+1)/2=2? (4+1)/2=2 (since 5/2=2). y_min = max(0,2)=2. y_max=1 => 0. So ans=0. Good.
-
n=2, a=2,b=1,c=0 => T=4. Possibilities: Could have x=4,y=0 (x=4 ≤2? No) => not allowed. x=2,y=1 => 2+2=4, x=2 ≤2, y=1 ≤1 => valid. x=0,y=2? y=2 > b=1 invalid. Also x=4? no. So 1 solution. Our algorithm: z=0,T'=4,y_max=min(1,2)=1, diff=4-2=2 => L0=ceil(2/2)=1? (2+1)/2=1? Actually (2+1)/2=1 (since 3/2=1). Wait ceil(2/2)=1 (since 2/2=1 exactly). (2+1)/2=1.5 truncated to 1, correct. y_min = max(0,1)=1. y_max=1 => count=1. Good.
Now check case with negative diff: n=2, a=10,b=0,c=0 => T=4, a=10. diff=4-10=-6 => L0 = (-6)/2 = -3 (since integer division truncates toward zero, -6/2=-3). y_min = max(0,-3)=0. y_max = min(0,2)=0 => count=1? Actually we need x=4, and a=10 allows x=4, so 1 way. Our count: y_max=0, y_min=0, count=1. Good.
Now check case with T' odd: n=1.5? Actually n is integer, so T even. But T' = T - 4z. Since T even, 4z even, so T' even? Actually 4z is multiple of 4, which is even. Even minus even = even. So T' is always even. Because T=2n even. Subtracting 4z (multiple of 4) yields even. So T' is always even. So we don't have odd T'? Wait: 2n is even, 4z is divisible by 4, so indeed T' is even. So we can rely on T' even. Because 2n mod 2 = 0. 4z mod 2 = 0. So T' mod 2 = 0. So T' is even. Good. That simplifies parity. So T' always even, which means floor(T'/2) = T'/2 exactly. And also diff = T' - a, which may be odd or even.
But we computed L0 using general method, works.
Now answer must be printed as integer.
Now we should think about alternative: Could there be any possibility that our counting includes cases where x > a? We argued y_min ensures x ≤ a. But we need also x ≤ a. However, there might be a case where y_min is computed from ceil((T'-a)/2) but if a is huge, L0 may be negative, y_min becomes 0, but does that guarantee that for any y >=0, x ≤ a? Let's check: if a huge (≥ T'), then T' - a ≤ 0, so condition x ≤ a automatically holds because x = T' - 2y ≤ T' ≤ a. So y_min=0 is correct.
Now ensure that for y values less than ceil((T' - a)/2), we would have x > a. For y=0, if T' > a, then x = T' > a violates. So we need y sufficiently large to bring x down. So lower bound is needed. So correct.
Now also need to ensure that x is integer and ≤ a. But also x must be non-negative: for y up to floor(T'/2), we ensure non-negative.
Now also need to check that x ≤ a but also x must be ≤ a supply. Since we used that, okay.
Now also need to ensure that we are counting each triple exactly once. Yes.
Now think about potential performance: loop up to 5000, trivial.
Thus solution is done.
But wait: The problem might also be solved by generating function or DP, but this direct counting is efficient.
One more nuance: In the problem, bottles are 0.5L, 1L, 2L. Our conversion to half-liters makes x up to a, but a up to 5000. Since n up to 10000, T up to 20000. So x (number of 0.5L bottles) needed could be as high as 20000 if only small bottles used. But a is at most 5000, so many combos are cut. So our counting method still works.
Now we need to implement in C++.
Implementation details:
- Use long long for ans to avoid overflow, though not necessary, but safe.
- Read ints.
- Compute T = 2*n.
- Loop z from 0 to min(c, T/4). Actually we can compute max_z = min(c, T/4) because if T/4 is not integer, floor division ensures 4*z <= T. So we can do:
int max_z = min(c, T / 4);
for (int z = 0; z <= max_z; ++z) {
int T_prime = T - 4*z;
// compute y_max
int y_max = min(b, T_prime / 2);
// compute diff
int diff = T_prime - a;
int L0;
if (diff >= 0) {
L0 = (diff + 1) / 2;
} else {
L0 = diff / 2; // because trunc toward zero yields ceil
}
int y_min = L0 > 0 ? L0 : 0; // max(0, L0)
if (y_min > y_max) continue;
ans += (y_max - y_min + 1);
}
But careful: For diff negative, L0 computed as diff/2 might be a negative number or zero. For diff = -1, diff/2 = 0, L0=0, correct. For diff = -3, diff/2 = -1, L0=-1, then y_min = max(0, -1) = 0. Good.
We must check that for diff negative, L0 might be negative but we want ceil(diff/2). Example diff = -1: diff/2 = 0; ceil(-0.5)=0. Good. diff = -2: diff/2 = -1; ceil(-1) = -1. Good. diff = -3: diff/2 = -1? Actually -3/2 = -1 (truncation toward zero), ceil(-1.5) = -1. Good. So correct.
Edge: diff = -5: -5/2 = -2, ceil(-2.5) = -2. Yes.
Thus correct.
Now we need to be cautious about integer division in C++ for negative numbers: Since C++11, division truncates toward zero. So it's fine.
Now test a few more cases manually.
Case: n=5, a=1,b=1,c=1. T=10.
We can list:
z=0: T'=10, y_max = min(1,5)=1, diff=10-1=9, L0 = ceil(9/2)=5 => y_min=5>1 =>0.
z=1: T'=6, y_max=min(1,3)=1, diff=6-1=5, L0=ceil(5/2)=3 => y_min=3>1 =>0.
z=2: T'=2, y_max=min(1,1)=1, diff=2-1=1, L0=ceil(1/2)=1 => y_min=1, y_max=1 => count 1. Check: z=2, y=1 => x = 2 - 21 =0, within a=1. So 1 way.
z=3: 43=12 >10 stop. So ans=1. Also check possibility z=0? Not possible. So answer 1.
Another case: n=4, a=0,b=2,c=1. T=8. Solutions: Can use two 2L bottles (z=2? but c=1, so z max 1). So z=0: need x+2y=8, a=0 => x=0 => 2y=8 => y=4, but b=2 => not allowed. z=1: T'=8-4=4, x+2y=4, a=0 => x=0 => 2y=4 => y=2, b=2 => allowed. So 1 way. Also maybe y=1? then x=2 but a=0 not allowed. So ans=1. Our algorithm: max_z = min(1,8/4=2)=1. z=0: T'=8, y_max=min(2,4)=2? Actually floor(8/2)=4, min with b=2 => y_max=2. diff=8-0=8, L0=ceil(8/2)=4? (8+1)/2=4 => y_min=4, > y_max=2 =>0. z=1: T'=4, y_max=min(2,2)=2. diff=4-0=4, L0=ceil(4/2)=2, y_min=2, y_max=2 => count 1. ans=1.
Works.
Now we must ensure we handle the case where T_prime becomes negative? Loop condition ensures 4*z <= T, so T_prime >=0. So fine.
Now about potential off-by-one: For z loop, we need to include z such that 4z <= T and z <= c. So max_z = min(c, T/4). But if T/4 is not integer, floor division gives max_z where 4z <= T. So that's correct.
Now consider scenario: T_prime may be 0. Then y_max = min(b, 0) = 0. diff = -a. L0 = diff/2 if diff negative, which gives ceil(-a/2). For a=0: diff=0, L0 = (0+1)/2? Actually diff=0 >=0, so L0 = (0+1)/2 = 0? (0+1)/2=0 (integer division). y_min = max(0,0)=0. So count = 1 if y_min <= y_max i.e., 0<=0 => count 1. This corresponds to x=0, y=0, z such that 4z = T. That is valid if a≥0, b≥0, and c≥z. So that is correct.
Now let's test scenario: T_prime=0, a=1. Then diff=-1, L0 = -1/2=0? Actually diff = -1, since diff<0, L0 = diff/2 = -1/2? In C++, -1/2 = 0? Wait -1/2 = 0 because truncation toward zero? -1/2 = -0.5 truncated to 0? Actually integer division in C++: -1/2 yields 0 because it truncates toward zero (i.e., discards fractional part). So -1/2 = 0. L0 = 0. y_min=0, y_max=0 => count 1. That means x = T' - 2y = 0 - 0 =0, which is ≤ a=1, ok.
Now if a=0, diff=0, L0=0, count 1. Good.
Now if a=0 and T_prime=1? But T_prime is even, so no.
Now any other issues? For diff negative, using diff/2 works. But we need to ensure that for diff negative and odd, diff/2 yields the ceiling? Let's double-check: diff = -3. -3/2 in C++ = -1 (since -1.5 truncates to -1). Ceil(-1.5) = -1. So yes. diff = -5 => -5/2 = -2, ceil(-2.5) = -2. So it's consistent because truncation toward zero rounds up for negative numbers. Good.
Now we could also compute L0 = (diff + 1) / 2 if diff >= 0 else diff / 2. That's fine.
Alternatively, we could compute y_min = max(0, (diff + 1) / 2) but only for diff >=0? Not safe.
Now we need to also consider that y_max could be negative? No, T_prime >=0, so T_prime/2 >=0. So y_max is non-negative.
Thus algorithm is sound.
Now we should think about whether we should also check that x is integer and within [0,a] after counting, but we already handled.
Now I'll produce final solution.
But wait, there is a twist: The problem might be from Codeforces (maybe 15A?). The constraints are moderate, but there might be a catch: a, b, c can be up to 5000, but n up to 10000, so T up to 20000. Loop over z is up to 5000, but we could also loop over y. Both fine.
Now confirm the answer for sample 1: we got 9.
Now additional test: n=0? But n≥1 per problem: 1 ≤ n. So n>=1.
Now what about a,b,c up to 5000 but maybe n small? Works.
Now time complexity O(c) ~ 5000, trivial.
Thus we can implement.
Now I should also consider whether we need to use long long for answer? The maximum answer is around 25 million, which fits in int, but sometimes answer can be larger? Let's compute worst-case number of solutions with bounds a,b,c all 5000 and n such that many solutions exist. We need to find n that maximizes number of valid triples. Since a,b,c are 5000 each, the number of triples that satisfy constraints is at most (a+1)(b+1)(c+1) = 5001^3 ≈ 125 billion, but that's not all sum to n. However, the actual count for a given n is limited by the equation. The maximum number of solutions to x+2y+4z = T with x,y,z bounded is at most about the number of partitions of T into at most a ones, b twos, c fours? Actually it's bounded by the number of (y,z) such that x = T - 2y - 4z is between 0 and a. For each z, number of y is at most b+1 but also constrained by x range. Could be as many as b+1 if the x constraint is loose. The number of z that work is at most c+1 but also limited by T. So maximum number is at most (c+1)*(b+1) ~ 5001^2 ≈ 25 million. As argued earlier, for T=20000, the sum of (y_max - y_min + 1) over all z that satisfy constraints could be up to about 25 million, as we computed for unbounded case (when a is large enough to allow all x, and b and c large enough to allow all y and z). Actually if a is large enough (>= T), then the lower bound y_min = max(0, ceil((T'-a)/2)) becomes 0 for all T' because diff negative or zero. So then count per z = min(b, floor(T'/2)) + 1. And if b is also large enough (>= floor(T'/2)), then count = floor(T'/2)+1. And if c is large enough to include all z from 0 to floor(T/4