You are given three sticks with positive integer lengths of a, b, and c centimeters. You can increase length of some of them by some positive integer number of centimeters (different sticks can be increased by a different length), but in total by at most l centimeters. In particular, it is allowed not to increase the length of any stick.
Determine the number of ways to increase the lengths of some sticks so that you can form from them a non-degenerate (that is, having a positive area) triangle. Two ways are considered different, if the length of some stick is increased by different number of centimeters in them.
Input
The single line contains 4 integers a, b, c, l (1 ≤ a, b, c ≤ 3·105, 0 ≤ l ≤ 3·105).
Output
Print a single integer — the number of ways to increase the sizes of the sticks by the total of at most l centimeters, so that you can make a non-degenerate triangle from it.
Examples
Input
1 1 1 2
Output
4
Input
1 2 3 1
Output
2
Input
10 2 1 7
Output
0
Note
In the first sample test you can either not increase any stick or increase any two sticks by 1 centimeter.
In the second sample test you can increase either the first or the second stick by one centimeter. Note that the triangle made from the initial sticks is degenerate and thus, doesn't meet the conditions.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to count number of ways to increase lengths of three sticks with initial lengths a,b,c by integer amounts (nonnegative integers) x,y,z such that x+y+z ≤ l, and after increasing, the new lengths a' = a+x, b'=b+y, c'=c+z form a non-degenerate triangle: i.e., a' + b' > c', a' + c' > b', b' + c' > a'. Since all lengths are positive, the only condition is that the sum of any two sides > third; equivalently, the triangle inequality strict.
Given constraints a,b,c,l up to 3e5. So total possible (x,y,z) naive enumeration would be O(l^3) impossible. Need combinatorial count.
We are to count number of integer triples (x,y,z) with x,y,z ≥ 0, x+y+z ≤ l, such that triangle holds.
Let’s denote new lengths: A = a+x, B = b+y, C = c+z.
Triangle conditions:
A + B > C
A + C > B
B + C > A
Since original a,b,c are positive, but could be degenerate initially.
Approach: total number of ways to increase with total increase ≤ l (i.e., all triples nonnegative x+y+z ≤ l) is something like number of nonnegative integer solutions to x+y+z ≤ l. That's C(l+3,3) if we include slack variable. But we need to subtract those triples that lead to degenerate triangle (including those that violate any triangle inequality). However, the degenerate conditions are not mutually exclusive; some triples may violate more than one inequality? Actually for three lengths, degeneracy occurs when at least one inequality fails (i.e., one side is >= sum of other two). It is possible that more than one fails? Suppose one side is larger than sum of other two, then automatically the other two sums are less? Let's check: If A >= B+C, then B+C > A? No, B+C ≤ A. For other inequalities: A+B > C? Since A >= B+C, A+B >= (B+C)+B = 2B+C > C (since B>0), so that holds. Similarly A+C > B holds. So only one inequality fails typically. Could also have A = B+C and also B = A+C? That would imply B = (B+C)+C => B = B+2C => C=0, but lengths positive (since we can increase by nonnegative, but a,b,c ≥1 so initial positive; after increase, lengths are at least original, so still positive). So positive lengths can't have two equalities simultaneously unless zero. So the degenerate cases (non-triangle) are exactly those where one side is at least the sum of the other two. They are mutually exclusive because if one side is ≥ sum of other two, the other sides are strictly less than sum of others? Actually if A ≥ B+C, then B < A+C (since A>0) and C < A+B, so only that one inequality fails. So the three bad events are disjoint: event X: A ≥ B + C; event Y: B ≥ A + C; event Z: C ≥ A + B. Since A,B,C positive, these cannot happen simultaneously (except maybe if all zero, but not). So we can count total number of triples (x,y,z) with x+y+z ≤ l and then subtract the number of triples that satisfy any of these three conditions. Because they are disjoint, we can sum the counts for each condition and subtract from total.
Thus answer = total_ways - (N_A + N_B + N_C), where N_A = number of (x,y,z) with x+y+z ≤ l and a+x ≥ b+y + c+z etc.
We need to compute each N.
Let's focus on N_A: condition a + x ≥ (b + y) + (c + z) => a + x ≥ b + c + y + z => x - y - z ≥ (b + c - a). Let delta_A = b + c - a. If delta_A ≤ 0? Then initial a already ≥ b+c? Then condition might be always true? Actually if a ≥ b+c initially, then for any nonnegative x,y,z, we have a+x ≥ a ≥ b+c ≥ b+y + c+z? Wait: careful: b+y + c+z ≥ b+c because y,z≥0. So a ≥ b+c implies a ≥ b+c, but we need a+x ≥ b+y + c+z. Since a may be ≥ b+c, but adding x,y,z could change? Actually if a ≥ b+c, then a + x ≥ a ≥ b+c. But b+y + c+z ≥ b+c, so we cannot guarantee a+x ≥ b+y + c+z because the right-hand side increases with y,z. For example a=5, b=2, c=2, a≥b+c (4)? Actually 5 ≥4. Then with y=1, z=1, b+y+c+z=2+1+2+1=6, a+x=5+x. For x=0, 5 <6, so fails. So condition is not always satisfied. So we need to count all (x,y,z) such that x - y - z ≥ delta_A, where delta_A = b + c - a. This can be negative; then the inequality becomes easier because the right-hand side is negative, but still we need to consider nonnegative x,y,z.
But also we have the total increase constraint: x+y+z ≤ l.
So N_A = number of nonnegative integer triples (x,y,z) with:
(1) x + y + z ≤ l
(2) x - y - z ≥ D, where D = b + c - a.
We need to count these.
Similarly N_B: condition b + y ≥ a + x + c + z => y - x - z ≥ a + c - b = D_B, with D_B = a + c - b, and x+y+z ≤ l.
N_C: condition c + z ≥ a + x + b + y => z - x - y ≥ a + b - c = D_C.
We need to count each efficiently.
We can solve N_A by transformation. Let’s define variables u = x, v = y, w = z. Condition: u - v - w ≥ D. Also u,v,w ≥0, and u+v+w ≤ l.
We can try to express as u ≥ v + w + D.
Since u is nonnegative, we need v+w+D ≤ u ≤ ? Actually also from u+v+w ≤ l => u ≤ l - v - w.
So we need existence of u satisfying max( v+w+D, 0 ) ≤ u ≤ l - v - w, and u integer.
So for given v,w, number of u is max(0, (l - v - w) - max(v+w+D, 0) + 1 ), provided that the lower bound ≤ upper bound.
But careful: u must be integer. Also lower bound must be at least 0 anyway.
So count = sum_{v=0}^{?} sum_{w=0}^{?} f(v,w) where f(v,w) = number of u such that u ≥ v+w+D and u ≤ l - v - w, with u≥0.
Let’s denote L = max(v+w+D, 0). Upper U = l - v - w. Then number = max(0, U - L + 1). This is defined for v,w such that U ≥ L.
We can also change variables: let s = v+w, and then count over distributions of v,w? But the condition only depends on v+w (s) not individually? Indeed, u condition depends only on sum s = v+w, because inequality is u ≥ s + D. Also total constraint: u ≤ l - s. So for a fixed sum s, the number of u is f(s) = max(0, (l - s) - max(s + D, 0) + 1). But we need to count number of (v,w) with given s, and v,w ≥0 integer. Number of nonnegative integer pairs (v,w) with sum s is (s+1). However, there might be additional constraints from something? Only condition is that v,w nonnegative. So indeed for each s from 0 to l (since s = v+w must be such that possible u exists and s ≤ l?), we can compute contributions: number of (v,w) with sum s times f(s). Then N_A = sum_{s=0}^{l} (s+1) * f(s).
But careful: When we sum over s, we must ensure that for each s, the number of u is computed based on the constraints, and v,w can be any with sum s. That's valid because u condition only depends on s.
Simplify: Let M = max(s + D, 0). So f(s) = max(0, l - s - M + 1) if l - s ≥ M, else 0.
Case analysis on D = b + c - a.
If D <= 0, then s + D may be negative for small s. Since s ≥ 0, s + D can be negative if s < -D. But D negative means -D positive. Let's denote T = -D = a - (b + c) (if a ≥ b+c, D ≤0, T ≥0). For s < T, s + D < 0, then max(s+D,0) = 0. For s ≥ T, s + D ≥0, max = s+D.
So we split.
Similarly, if D > 0, then s+D ≥ D > 0 always, so max(s+D,0) = s+D for all s ≥ 0. Since s≥0, s+D ≥ D >0. So no zero case.
Thus we need to compute sum for D>0 and D≤0 separately.
We also have upper limit for s: For given l, we need that there exists u satisfying u ≤ l - s, u ≥ max(0, s+D). This requires max(0, s+D) ≤ l - s.
Condition: If D>0: need s+D ≤ l - s => 2s ≤ l - D => s ≤ floor((l - D)/2). Also need s ≥ 0 obviously. And also l - s ≥ 0 => s ≤ l. But more restrictive is s ≤ floor((l - D)/2). Also need s integer.
If D≤0: For s < T = -D: condition max=0, so need 0 ≤ l - s (always true because s ≤ l), so condition holds if l - s ≥ 0, i.e., s ≤ l. So for s in [0, T-1], f(s) = (l - s) - 0 + 1 = l - s + 1, provided s ≤ l. But also we need that the lower bound 0 ≤ upper bound, which holds if l - s ≥ 0. So f(s) = l - s + 1 for s ≤ min(T-1, l). But also note that s may exceed l? We'll restrict s up to l.
For s ≥ T: then max = s+D = s - T (since D = -T). Condition: need s - T ≤ l - s => 2s ≤ l + T => s ≤ floor((l + T)/2). Also need s ≤ l (but less restrictive maybe). So for s in [T, floor((l+T)/2)], f(s) = (l - s) - (s - T) + 1 = l - s - s + T + 1 = l - 2s + T + 1. For s beyond that, f(s)=0.
Also note that for s ≥ T, we also need that the lower bound s - T ≥ 0, which holds.
Thus we can compute sums efficiently as arithmetic series.
Now we need to sum (s+1) * f(s). Since l up to 3e5, we could in principle loop over s from 0 to l, but l up to 300k, and we have to compute for three events, that is O(l) which is fine (3e5). Actually we need to compute three sums; O(l) per sum is okay (900k operations). But careful: l up to 3e5, and we might have to compute three such sums, maybe with loops from 0 to l. That's trivial.
However, we need to also consider that the counts might be large, up to about C(l+3,3) ~ (l^3)/6 ~ 4.5e15 for l=300k, which fits in 64-bit? (300k)^3 = 27e15 = 2.7e16, /6 = 4.5e15, within 2^63-1 ~9.22e18, so 64-bit signed integer safe. Use int64.
So we could just loop over s for each event and compute contributions. But we must be careful about bounds to avoid O(l^2). Looping over s 0..l is fine.
But we also have to ensure correctness of counting; also consider that the condition includes equality (≥) for degenerate triangle? The problem says we need non-degenerate triangle: all inequalities strict >. So degenerate (including equality) should not count. In our bad events we counted triples where a' ≥ b' + c' (so including equality). That's correct: such triangles are degenerate (zero area). So we subtract them.
Thus answer = total_ways - (N_A + N_B + N_C).
Now verify with examples.
Example 1: a=1,b=1,c=1,l=2.
Compute total_ways: number of nonnegative integer (x,y,z) with sum ≤2. That's C(2+3,3) = C(5,3)=10. Wait check: For l=2, possible (x,y,z) with 0≤x+y+z≤2. Count = sum_{t=0}^{2} C(t+3-1,3-1)= sum_{t=0}^{2} C(t+2,2)= C(2,2)=1 (t=0) + C(3,2)=3 (t=1) + C(4,2)=6 (t=2) =10. So total=10.
Now compute N_A: D = b+c - a = 1+1-1 =1. So D=1 >0. Condition: a+x ≥ b+y + c+z => x - y - z ≥ 1.
We need count of (x,y,z) with x+y+z ≤2 and x ≥ y+z+1.
Let's enumerate manually: (x,y,z) all nonnegative sum≤2.
List all triples with sum 0: (0,0,0): x=0, y+z=0 => 0≥1? false. Sum1: (1,0,0): x=1, y+z=0 =>1≥1 true; (0,1,0): 0≥1 false; (0,0,1): false. Sum2: (2,0,0): 2≥1 true; (0,2,0): false; (0,0,2): false; (1,1,0): x=1, y+z=1 =>1≥1 true? 1≥1 true (≥) so counts; (1,0,1): 1≥1 true; (0,1,1): 0≥2 false; (0,2,0) already, (0,0,2), (2,0,0), (1,1,0), (1,0,1), also (0,1,1) done, (2,0,0) done, (0,2,0) done, (0,0,2) done; (2,0,0) counted; also (0,2,0) and (0,0,2) false; (1,1,0) true, (1,0,1) true; (0,1,1) false; Also (0,2,0) false; (0,0,2) false; (2,0,0) true; Are there any other with sum2? (2,0,0), (0,2,0), (0,0,2), (1,1,0), (1,0,1), (0,1,1). That's 6. So true: (1,0,0), (2,0,0), (1,1,0), (1,0,1). That's 4.
Check: (2,0,0) sum=2: 2≥0+0+1? 2≥1 true; (1,1,0): x=1, y+z=1 =>1≥1 true; (1,0,1): similar true; (1,0,0): true. So N_A=4.
Similarly N_B: D_B = a+c - b = 1+1-1=1, symmetric. So N_B also 4? But careful: Overlap? They are disjoint? In our enumeration, does any triple satisfy both a'≥b'+c' and b'≥a'+c'? For positive lengths, impossible because would imply a'≥b'+c'≥c' and b'≥a'+c'≥a' => a'≥b'+c' and b'≥a'+c' => adding: a'+b'≥a'+b'+2c' => 0≥2c' impossible unless c'=0, but lengths positive. So disjoint. So N_B count: by symmetry also 4. But let's enumerate: Condition b+y ≥ a+x + c+z => y ≥ x+z+1. List triples with sum≤2: (0,1,0) -> y=1, x+z=0 =>1≥1 true; (0,2,0): 2≥0 true? 2≥1 true; (1,1,0): y=1, x+z=1 =>1≥1 true; (0,1,1): y=1, x+z=1 =>1≥1 true; (0,0,1) not; (0,0,0) no; (2,0,0) no; etc. So true: (0,1,0), (0,2,0), (1,1,0), (0,1,1). That's also 4. N_C similarly 4? Check N_C: D_C = a+b - c =1, condition z ≥ x+y+1. True triples: (0,0,1), (0,0,2), (1,0,1), (0,1,1). That's also 4. Sum of bad = 12. Then total - bad = 10 - 12 = -2, which is not 4. Something is wrong. Indeed, N_A,N_B,N_C counted too many? But they can't be disjoint? Let's test if some triples satisfy more than one condition. Consider (1,1,0): For a,b,c=1. After increase: a'=2,b'=2,c'=1. Check triangle: 2+2>1 yes, 2+1>2? 3>2 yes, 2+1>2 yes => it is non-degenerate triangle! Actually (1,1,0) yields lengths (2,2,1): forms a triangle? 2+1 >2 (3>2) OK. So (1,1,0) should be good, not bad. But we counted it in N_A? Condition a'≥b'+c'? 2 ≥ 2+1? 2 ≥ 3 false. So not in N_A. Wait earlier we said (1,1,0) satisfied N_A? Check: (1,1,0) means x=1,y=1,z=0: a'=2, b'=2, c'=1. Condition a'≥b'+c'? 2≥2+1 false. But we derived condition x - y - z ≥ 1. For (1,1,0): x - y - z = 1-1-0=0, which is not ≥1. So it's not counted in N_A. So my earlier enumeration for N_A incorrectly included (1,1,0) and (1,0,1). Let's recalc N_A for a=b=c=1, D=1: Condition x ≥ y+z+1. So (1,1,0): x=1, y+z=1 => 1≥2? false. (1,0,1): 1≥1? 1≥1+? y+z=1, need 2? Actually x=1, y+z=1, so condition x ≥ y+z+1 => 1≥2 false. So they are not counted. My list for N_A true: (1,0,0): x=1, y+z=0 => 1≥1 true; (2,0,0): 2≥1 true; (1,1,0) false; (1,0,1) false. Also maybe (2,1,? ) not sum≤2. Also (3?) no. Also (0,?,?) not. Also (2,0,0) counts. Are there any others with sum=2? (2,0,0) we counted; (1,0,1) false; (1,1,0) false; (0,2,0) no; (0,0,2) no; (0,1,1) no; So N_A=2? Let's enumerate all triples that satisfy condition: (1,0,0), (2,0,0). Also sum=3? but l=2 so not. Also maybe (0,0,0) no. So N_A=2.
Now N_B: condition y ≥ x+z+1. For sum≤2: (0,1,0): y=1, x+z=0 => 1≥1 true; (0,2,0): 2≥0+0+1? 2≥1 true; (1,1,0): y=1, x+z=1 => 1≥2 false; (0,1,1): y=1, x+z=1 => 1≥2 false; (0,0,1) no; (1,0,1) no; (2,0,0) no; (0,0,2) no. Also (1,0,0) no. So true: (0,1,0), (0,2,0). That's 2.
Total bad = 6. Total ways = 10. 10-6=4. That matches example output 4. Good.
So our method works, but we must compute each N correctly.
Thus the core is computing for each event, given D = (sum of the other two) - (this side) (like for side a: D_A = b+c - a), count number of triples (x,y,z) with x+y+z ≤ l and x - y - z ≥ D (or correspondingly).
We can compute this count efficiently by summation over s = y+z for N_A, or over appropriate sum for other events. Note for N_B, condition involves y - x - z ≥ D_B, so we can similarly set s = x+z (the sum of the other two variables except y) and sum over that. For N_C, s = x+y.
So we can define a generic function to count number of solutions for variables (p,q,r) where p is the variable on LHS (the side that is supposed to be large), and we have condition p - (q+r) ≥ D, with p+q+r ≤ l, p,q,r ≥0. This is symmetric. So we can compute using sum over t = q+r.
Thus for each D, we can compute:
N(D) = number of (p,q,r) ∈ ℕ³ with p + q + r ≤ l and p - q - r ≥ D.
Note: D can be any integer (could be negative large). But we'll compute as above.
Now we need to ensure we count correctly when D is such that condition is trivially satisfied for many combos? But we have formula.
So implement function count_bad(D, l) that returns N(D). Then for side a: D_A = b + c - a, N_A = count_bad(D_A, l). For side b: D_B = a + c - b, N_B = count_bad(D_B, l). For side c: D_C = a + b - c, N_C = count_bad(D_C, l).
But careful: In count_bad for side a, the variables are (x,y,z) with p=x, q=y, r=z. For side b, it's (y,x,z) but note that the roles change: we need p=y, q=x, r=z. However, the count function is symmetric in the sense that the variables are just labels; the count depends only on D and l, and the set of variables (p,q,r) is all nonnegative. Since we are counting all triples (x,y,z) anyway, but when we apply count_bad(D_B, l) we are counting number of triples (x,y,z) such that condition y - x - z ≥ D_B. That's exactly N_B. Because we treat p=y, q=x, r=z. The function count_bad doesn't care about ordering of q and r; it only sums over sum of q+r (which is x+z). So the count is symmetric with respect to which variables are q and r, but not p. So we can use same function but we must interpret that the variables order (p,q,r) correspond to (the side variable, the other two). So for each side we call with appropriate mapping. But we need to ensure we sum over all (x,y,z). The function will sum over t = q+r, with multiplicity (t+1) because number of nonnegative pairs (q,r) with sum t is (t+1). So it's consistent.
Thus we can compute N(D) = ∑_{t=0}^{l} (t+1) * f(t) where f(t) = max(0, (l - t) - max(t + D, 0) + 1).
But we need to be careful: t = q+r. However, there's also an upper bound from p ≤? Actually we sum over t, and for each t, p must satisfy max(t + D, 0) ≤ p ≤ l - t. So number of p is f(t). Then multiply by number of (q,r) pairs with sum t, which is (t+1). So yes.
Now, the sum over t from 0 to l: but many t will have f(t)=0 after some threshold. So we can find the range where f(t)>0 and compute sum using formulas, or just loop t=0..l and compute (l up to 300k, which is fine). However, we need to do this for three D's, so 3*300k = 900k operations, trivial. But we must ensure we use 64-bit and careful with overflow? It's fine.
But can we just loop? Yes, 300k is small. But perhaps we need to handle l up to 3e5, and we have three such loops, that's fine.
But wait, there's a nuance: For D negative large (e.g., a much larger than b+c), then the condition p - q - r ≥ D is easily satisfied for many. But we still need to count correctly; our summation formula works for any D. However, for D negative large, max(t+D,0) may be 0 for many t, and f(t) = l - t + 1 until t reaches a certain point where t+D becomes positive. That's fine.
Now, ensure that we don't double count any triple in bad events? As argued, they are disjoint because at most one side can be at least sum of the other two for positive lengths. However, we need to confirm that if after increases, two sides equal sum of the third? Actually if one side equals sum of the other two, it's degenerate. Could there be a triple where two inequalities are violated? Suppose A = B + C, then B = A + C would imply B = (B+C)+C = B+2C => C=0, impossible positive. So at most one. So disjoint.
Thus answer = total - sum bad.
Now compute total ways: number of (x,y,z) with x,y,z≥0, x+y+z ≤ l. That's known to be C(l+3,3) = (l+3)(l+2)(l+1)/6.
Now verify with second example: a=1,b=2,c=3,l=1.
Total ways: C(1+3,3)=C(4,3)=4.
Now compute N_A: D_A = b+c - a = 2+3-1=4. Condition: x - y - z ≥4. With l=1, max sum=1, x,y,z≥0. The only triples: (0,0,0) sum0, (1,0,0),(0,1,0),(0,0,1). Check condition: For (1,0,0): 1-0-0=1 <4 false; others 0, negative. So N_A=0.
N_B: D_B = a+c - b = 1+3-2=2. Condition: y - x - z ≥2. Check triples: (0,1,0): y=1, x+z=0 => 1≥2? false; (0,0,0):0≥2 false; (1,0,0): y=0 false; (0,0,1): y=0 false. So N_B=0? But maybe (0,2,?) not possible l=1. So 0.
N_C: D_C = a+b - c = 1+2-3=0. Condition: z - x - y ≥0 => z ≥ x+y. For l=1, triples: (0,0,0): z=0, x+y=0 => 0≥0 true (so degenerate triangle? After increases: a'=1,b'=2,c'=3. Initially c=3, a=1,b=2 => 1+2=3, so it's degenerate, and we include equality. So (0,0,0) qualifies as bad. Also (0,0,1): z=1, x+y=0 =>1≥0 true; (0,1,0): z=0, x+y=1 =>0≥1 false; (1,0,0): false. So bad: (0,0,0) and (0,0,1). Also check (0,0,1): new lengths (1,2,4). 1+2=3<4 => not triangle. So indeed bad. So N_C=2.
Thus total bad=2, answer=4-2=2. matches example.
Third example: a=10,b=2,c=1,l=7. Let's compute manually maybe later.
Now compute with our method:
total = C(7+3,3)=C(10,3)=120.
N_A: D_A = b+c - a = 2+1-10 = -7. So D negative. We'll compute count_bad(-7,7). N_B: D_B = a+c - b =10+1-2=9. N_C: D_C = a+b - c =10+2-1=11.
We expect answer 0.
But we can test after coding.
Now ensure our count_bad function correctly counts all (x,y,z) that satisfy the inequality, including those where condition is trivially satisfied? Let's test with D negative.
General approach: For each t from 0 to l, compute lower = max(0, t+D). But careful: t = y+z for N_A. Condition: x ≥ y+z + D? Actually condition: x - y - z ≥ D => x ≥ y+z + D. So lower bound for x is max(0, y+z + D) because x must be nonnegative. But note that if y+z + D is negative, then the condition imposes no lower bound beyond 0. So lower = max(0, t + D). Good.
Then we also have constraint x ≤ l - t (since x+y+z ≤ l => x ≤ l - t). So number of x = max(0, (l - t) - lower + 1) if lower ≤ l - t else 0.
Then multiply by number of (y,z) with sum t, which is t+1. So sum over t.
Now check with D negative. For t=0: t + D = D = -7, so lower = 0. Then x ≤ l - 0 =7. So number of x = 7-0+1=8. t+1=1 => contribution 8. For t=1: t+D = -6, lower=0, x ≤ 6 => #x=7? Actually l=7, so x ≤ 6 => 0..6 inclusive =>7 values. t+1=2 =>14. t=2: lower=0, x≤5 =>6 values, t+1=3 =>18. t=3: lower=0, x≤4 =>5 values, t+1=4 =>20. t=4: lower=0, x≤3 =>4 values, t+1=5 =>20. t=5: lower=0, x≤2 =>3 values, t+1=6 =>18. t=6: lower=0, x≤1 =>2 values, t+1=7 =>14. t=7: lower=0, x≤0 =>1 value, t+1=8 =>8. Sum contributions: 8+14=22, +18=40, +20=60, +20=80, +18=98, +14=112, +8=120. So N_A would be 120 if all t up to 7 allowed. But is that correct? Let's check condition: D=-7, condition x - y - z ≥ -7, i.e., x + 7 ≥ y+z. For any nonnegative x,y,z with sum ≤7, is this always true? For some combos maybe not? For example, take x=0, y=4, z=4: sum=8 >7 not allowed. Within sum ≤7, the worst case is y+z as large as possible relative to x. For a given total sum S = x+y+z ≤ l, condition becomes x ≥ y+z -7. Since y+z = S - x, condition: x ≥ (S - x) -7 => 2x ≥ S -7 => x ≥ (S -7)/2. Since x is integer, and S ≤7, S-7 ≤0, so RHS ≤0, so condition automatically holds because x≥0. So indeed for l=7, condition x - y - z ≥ -7 is always satisfied for any x,y,z with sum ≤7? Let's test extreme: x=0, y=7, z=0: sum=7, condition: 0-7-0 = -7 ≥ -7? yes equality holds. So indeed all triples satisfy. So N_A should be total ways =120. That matches our sum. So N_A=120.
Now N_B: D_B=9 >0. Compute count_bad(9,7). For t = x+z (since condition y - x - z ≥9). t ranges 0..7. For each t, number of y: lower = max(0, t+9) = t+9 (since >0). Upper = l - t. So we need t+9 ≤ 7 - t? Actually upper is l - t =7 - t. So we need t+9 ≤ 7 - t => 2t ≤ -2 => impossible. So no t satisfies. So N_B=0.
N_C: D_C=11 >0, similarly no solutions. So total bad = 120, answer = 0. matches example.
Thus method works.
Now need to implement count_bad(D, l). However, note that D can be as large as? b+c - a, where a,b,c up to 3e5, so b+c - a can be up to (3e5+3e5)-1 = about 6e5-1, but since a,b,c ≥1, max D could be around 6e5. Actually maximum: if a=1, b=3e5, c=3e5 => D = 600000 -1? Wait: b+c - a = 300000+300000-1 = 599999, about 6e5. Also D can be negative as low as a up to 3e5, b=c=1 => D = 2 - 300000 = -299998, about -3e5. So D magnitude up to ~6e5. l up to 3e5. So t+D could exceed typical range; we must be careful when computing max(0, t+D) as it may become large. But our sum only goes t up to l, which is ≤3e5. So if D is large positive (>>0), t+D will be large, making lower bound > l - t for all t, so count =0. That's fine.
Now loop over t from 0 to l, compute lower = max(0, t+D). That's safe. But we need to compute using 64-bit.
But we can also derive closed form formulas to avoid loop, but loop is fine.
Complexity O(l) per event, up to 3*l <= 9e5, trivial.
However, careful: We need to be sure we are not double-counting any triple across bad events? Already argued they are disjoint. But we must consider also that some triples might satisfy condition for one side with equality and also for another? As argued not. Good.
Now, are there any triples that form a degenerate triangle but do not satisfy any of these conditions? Actually degenerate triangle condition is that one of the three inequalities is not strict, i.e., at least one side is >= sum of the other two. So exactly our three conditions. So yes.
Thus answer = total - N_A - N_B - N_C.
Now implement.
But before finalizing, let's test some edge cases.
Edge case: l = 0. Then only one triple: (0,0,0). total=1. Compute N_A, N_B, N_C based on D values. Answer = 1 if (a,b,c) forms non-degenerate triangle else 0. Since if they already form a non-degenerate triangle, then (0,0,0) should count; else if degenerate, it shouldn't. Let's test: If a,b,c satisfy triangle strict, then all D_A = b+c - a >0? Actually if triangle strict: a+b>c, a+c>b, b+c>a => each of b+c - a >0, etc. So D_A >0, D_B>0, D_C>0. For l=0, t only 0, and for N_A: lower = max(0,0+D_A) = D_A >0. Upper = l - t =0. So need lower ≤0, but D_A>0, so no. So N_A=0, similarly others, bad sum=0, answer=1, correct. If degenerate, say a=1,b=2,c=3: D_C = a+b-c = 0. For N_C: t = x+y? Actually for N_C, condition z - x - y ≥ 0, with l=0, t=0 => lower = max(0,0+0)=0, upper=0 => f=1, number of (x,y) pairs with sum 0 is 1 (x=0,y=0). So N_C=1. Others 0. So answer=1-1=0, correct.
If a=2,b=2,c=4? Actually 2+2=4 degenerate, D_C=0, similarly.
If a=5,b=1,c=1: D_A = b+c-a = -3? Actually 2-5=-3, D_B = a+c-b = 5+1-1=5? >0, D_C = a+b-c=5+1-1=5>0. For l=0, N_A: D=-3, t=0, lower=0, upper=0, f=1 => N_A=1? Check: (0,0,0): a'=5,b'=1,c'=1, does a' ≥ b'+c'? 5≥2, yes, so degenerate. So N_A should count it. Good. Others zero. So answer 0. Works.
Thus method works.
Now we just need to implement count_bad(D, l). However, careful: In our derivation for N_A we assumed that we sum over t = y+z. But for N_B, we would sum over t = x+z, which is still sum of the two variables other than the one on LHS. So the same function count_bad(D, l) can be used if we treat (p,q,r) generic: we sum over t = q+r. The count of (q,r) pairs with sum t is t+1, independent of which variables. So it's fine. But we must call it three times, each time with D appropriate.
Thus code:
int64_t total = (int64_t)(l+3)(l+2)(l+1)/6; // careful overflow: compute in 64-bit.
Then for each D: int64_t bad = count_bad(D, l); accumulate sum.
Then answer = total - sum_bad.
Now implement count_bad(D, l):
int64_t count = 0;
for(int t = 0; t <= l; t++) {
int64_t lower = t + D;
if(lower < 0) lower = 0;
int64_t upper = l - t;
if(lower <= upper) {
int64_t ways_p = upper - lower + 1; // number of p values
count += ways_p * (t + 1);
}
}
return count;
That's it.
But we should check if any potential overflow: ways_p up to l+1, t+1 up to l+1, product up to ~ (3e5+1)^2 ~ 9e10, which fits in 64-bit (9e10 < 9e18). Count may accumulate up to total ~4.5e15, still within 64-bit.
Now, we can further optimize by early break when lower > upper for all larger t? but not needed.
However, one nuance: For D very large positive, lower > upper for all t, so count=0; for D very negative, lower=0 for many t and upper = l - t, so we sum all t. In that case count = total ways? Let's test: For D extremely negative such that t+D < 0 for all t ≤ l, lower=0 always, then f(t) = (l - t) - 0 + 1 = l - t + 1. So count = sum_{t=0}^{l} (t+1)(l - t + 1). Does that equal total ways? total ways = number of triples (p,q,r) with sum ≤ l. That is sum_{t=0}^{l} (number of (q,r) with sum t)(number of p with 0≤p≤l - t). Number of (q,r) with sum t = t+1. Number of p values = l - t + 1. So indeed count = total ways. So indeed if D is so negative that condition is always satisfied (i.e., p - q - r ≥ D holds automatically for all triples with sum ≤ l), then count_bad = total. That matches expectation: if a is much larger than b+c, then any increases? Actually condition a'≥b'+c' may not always hold, but our condition is p - q - r ≥ D, with D = b+c-a. If D is very negative, the inequality p ≥ q+r + D, with D negative, is easier. For example a=10,b=1,c=1 => D = -8. For l=7, we saw all triples satisfied a' ≥ b'+c'? Actually we checked a'≥ b'+c'? a+x >= b+y+c+z. With a=10, b=1,c=1. For some triples, maybe not? Let's test: (0,7,0): a'=10, b'=8, c'=1 => 10 ≥ 9? 10≥9 true. (0,0,7): 10≥8 true. (0,4,3): a'=10, b'=5, c'=4 => 10≥9 true. (1,6,0): a'=11, b'=7, c'=1 => 11≥8 true. (0,6,0): 10≥7? 10≥7 true. (0,7,0) already. It seems always true because a is so big? Actually check (0,7,0): b'+c'=8+1=9, a'=10, ok. What about (0,8,0) but sum>7. Within sum≤7, max b'+c' occurs when y+z is as large as possible and x minimal. Since a=10, b+c=2. The worst case: x=0, y+z = l=7, so b'+c' = b+y + c+z = 1+0? Actually b=1, c=1 => b'+c' = 2 + (y+z) = 2+7=9. a'=10, so still ≥9. So yes all triples satisfy a'≥b'+c'? Actually condition is a' ≥ b'+c'? For (0,7,0): b'+c'= (1+7)+(1+0)=8+1=9, a'=10≥9, yes. So indeed with a=10, b=1,c=1 and l=7, all triples satisfy a'≥b'+c', making triangle impossible. So N_A = total. Good.
Thus our count_bad works.
Now confirm that for D negative but not so large in magnitude, the count may be less than total because some triples may violate condition. But does our formula capture that? Yes, for t where t+D > 0, the lower bound becomes positive, reducing number of p values.
Now, but is there any subtlety about the condition being p - q - r ≥ D? That includes equality. For degenerate triangle we need a' ≥ b'+c', i.e., p ≥ q+r, which corresponds to D = b+c - a? Wait careful: Condition a' ≥ b' + c' translates to a + x ≥ (b + y) + (c + z) => x - y - z ≥ (b + c - a). So indeed D = b+c - a. So if D is negative, the required difference is negative, meaning even if x-y-z is negative but not too negative, condition holds. So our formula includes all degenerate cases.
Now, one more nuance: Could there be any triple where the triangle is degenerate due to two sides exactly equal and the third side zero? But sides are positive integers, cannot be zero because a,b,c≥1, and x,y,z≥0 so all sides ≥1. So fine.
Thus solution is straightforward.
Now we need to implement in C++ (or other). Input four integers, compute answer.
Potential pitfalls: integer overflow when computing total with (l+3)(l+2)(l+1) might overflow 32-bit; use long long.
Now, performance: Loop from 0 to l for each event: up to 300k *3 = 900k iterations, each with simple arithmetic, fine.
But we can also reduce to one loop by computing all three simultaneously, but not necessary.
Now test with some random cases against brute force for small l to ensure correctness.
But before finalizing, we must double-check our derivation that N_A = sum_{t} (t+1)*f(t) is correct. Let's reason:
We have variables x (p), y (q), z (r). Condition: x ≥ y+z + D. Also x+y+z ≤ l.
For each nonnegative integer pair (y,z) with sum t = y+z, the constraint on x is: x ≥ max(0, t + D) and x ≤ l - t. The number of integer x satisfying is max(0, l - t - max(0,t+D) + 1). And number of (y,z) with sum t is t+1. So multiply and sum. Yes.
But is there any double counting? No.
Now, potential off-by-one: For t such that max(0, t+D) is 0, then x can be 0,1,...,l-t. That's (l-t+1) values. That's correct.
Now test with small l manually:
Example: a=2,b=3,c=4,l small. But we can brute for l=0,1,2.
Case: a=2,b=3,c=4,l=1.
total = C(4,3)=4. Triples: (0,0,0); (1,0,0); (0,1,0); (0,0,1).
Check triangle: original 2,3,4 => 2+3=5>4, ok triangle. So only degenerate if increases cause violation? Let's see:
(0,0,0): lengths (2,3,4) good -> should count.
(1,0,0): (3,3,4): 3+3>4, 3+4>3, ok.
(0,1,0): (2,4,4): 2+4>4? 6>4, 2+4>4? 6>4, 4+4>2 ok.
(0,0,1): (2,3,5): 2+3=5 not >5 (equal), degenerate. So that one bad.
Thus answer = 3.
Now compute using our method:
D_A = b+c - a = 3+4-2=5. N_A: D=5>0, l=1. For t=0: lower = max(0,0+5)=5, upper =1, lower>upper ->0. t=1: lower = max(0,1+5)=6, upper=0 ->0. So N_A=0.
D_B = a+c - b = 2+4-3=3. N_B: D=3>0. t from 0 to 1: t=0: lower=3, upper=1 ->0; t=1: lower=4, upper=0 ->0. So N_B=0.
D_C = a+b - c = 2+3-4=1. N_C: D=1>0? Actually 2+3-4=1 >0. So D=1 positive. t = x+y sum? Condition z - x - y ≥1. For t=0: lower = max(0,0+1)=1, upper = l - t =1? Actually upper = 1 - 0 =1? Wait l=1, so upper=1? But careful: For N_C, p = z, q = x, r = y. So t = q+r = x+y. l=1. Then upper = l - t = 1 - t. For t=0: upper=1, lower=1 => ways = 1 (z=1). And number of (x,y) with sum 0 is 1. So contribution 1. For t=1: lower = max(0,1+1)=2, upper = 0 ->0. So N_C=1. So total bad = 1, total = 4, answer =3. matches.
Now test a scenario where D negative.
Suppose a=5,b=2,c=2,l=2.
Total = C(5,3)=10? Wait l=2 => total = C(2+3,3)=10.
Now N_B: D_B = a+c - b =5+2-2=5. Positive, l=2 => no solutions =>0.
N_C: D_C = a+b - c =5+2-2=5 =>0.
So bad=7, total=10, answer=3. So there should be 3 good triangles. Let's list the good ones: (0,2,0) -> a'=5,b'=4,c'=2 => 5+2>4? 7>4, 5+4>2, 4+2>5? 6>5 -> triangle? 4+2=6 >5, good. (0,0,2) -> a'=5,b'=2,c'=4 => 5+2=7>4, 5+4=9>2, 2+4=6>5 -> good. (0,1,1) -> a'=5,b'=3,c'=3 => 5+3=8>3, 5+3=8>3, 3+3=6>5 -> good. So indeed 3. Good.
Thus our method holds.
Now check scenario where two of D's are negative? That's possible if two sides are large relative to the third? For example a=5,b=5,c=1. Then D_A = b+c - a =5+1-5=1 >0? Actually positive? b+c=6, a=5, D_A=1 >0. D_B = a+c - b =5+1-5=1 >0. D_C = a+b - c =5+5-1=9 >0. So all positive. In fact if one side is very small, then only that side's condition could be negative? Let's check: a=1,b=5,c=5: D_A = 5+5-1=9 >0; D_B =1+5-5=1>0; D_C =1+5-5=1>0. So actually only the side that is smallest may have D positive? Actually D for the largest side is negative? Wait D for side a is b+c - a; if a is the largest, then b+c - a may be negative. So only the largest side can have negative D; the two smaller sides will have positive D because sum of other two includes the largest side, which is large, making D positive. So at most one of D_A, D_B, D_C can be negative. That's fine.
Now we should consider the possibility that D can be zero. That's covered: For D=0, lower = max(0, t+0) = t, which is positive for t>0. So count_bad works.
Now complexity fine.
Now, there is one more subtlety: We are counting triples (x,y,z) with x+y+z ≤ l. But in our count_bad, we sum over t = y+z from 0 to l. For each t, we considered all (y,z) with sum t, and all x from lower to upper. However, is it possible that some (x,y,z) are counted where the total x+y+z > l? No, because we enforce x ≤ l - t. Since x+y+z = x + t ≤ (l - t) + t = l. So it's exactly those with sum ≤ l. Good.
Now, let's think about the possibility of using combinatorial formulas to compute sums faster, but not necessary.
Now, one more test: When l is large, say 300k, the loop of 300k three times is fine.
But we should be cautious about using int for D because D may be as high as 600k, which fits in int (since 3e5*2 = 600k, plus maybe small). Use long long for D? But D is integer, we can store in int (since 600k < 2^31-1). However, when computing lower = t + D, this could be up to 600k+300k=900k, still within int. But for safety, use long long.
Now, code.
Implementation in C++:
#include
#include
using namespace std;
using ll = long long;
ll count_bad(ll D, ll l) {
ll bad = 0;
for (ll t = 0; t <= l; t++) {
ll lower = t + D;
if (lower < 0) lower = 0;
ll upper = l - t;
if (lower <= upper) {
bad += (upper - lower + 1) * (t + 1);
}
}
return bad;
}
int main() {
ll a, b, c, l;
cin >> a >> b >> c >> l;
ll total = (l+3)(l+2)(l+1)/6; // might overflow 32-bit if l up to 3e5: (300003300002300001)/6 ~ 4.5e15, need 64-bit multiplication, so use ll.
ll bad = 0;
bad += count_bad(b + c - a, l);
bad += count_bad(a + c - b, l);
bad += count_bad(a + b - c, l);
ll ans = total - bad;
cout << ans << endl;
return 0;
}
But wait: The function count_bad expects D as the threshold: condition p - q - r ≥ D. For side a, D = b + c - a, correct. For side b, D = a + c - b, correct. For side c, D = a + b - c, correct.
Now verify with earlier examples.
Edge case: Suppose l=0 and all D >0: total=1, bad=0, answer=1. That's correct for non-degenerate triangle.
Now test with a=1,b=1,c=1,l=2 we got answer 4.
Now test random small l with brute to ensure no mistake.
But there is a nuance: Are we counting all triples with total increase ≤ l, i.e., x+y+z ≤ l? Yes, total computed as C(l+3,3) counts that. In our count_bad, we used condition x ≤ l - t, which ensures sum ≤ l. So consistent.
But need to confirm: In total we counted all triples (x,y,z) with sum ≤ l. In count_bad, we are counting those that satisfy condition and sum ≤ l. So subtraction works.
Now, is there any chance that some triples that are counted in two different bad events? We argued they are disjoint because cannot have two sides each at least sum of the other two. But let's double-check with equality: Suppose a' = b' + c' and also b' = a' + c'? That would imply b' = (b' + c') + c' => b' = b' + 2c' => c'=0 impossible. So indeed no overlap.
Thus final answer.
But let's consider more carefully: Could a triple satisfy a' ≥ b' + c' and b' ≥ a' + c' simultaneously if one of the sides is zero? But all sides are at least 1, since a,b,c≥1 and increases nonnegative. So no.
Thus answer correct.
Now, potential performance improvement: The loop in count_bad is O(l). For l up to 3e5, 3*300k = 900k, fine. But we need to be careful: For D large positive, the loop still goes through all t but we could break early. However, not needed.
But we need to ensure that in count_bad we handle D up to maybe > l? For D > l? For D positive and > l, t+D > l for all t, so lower > l - t (since lower ≥ D > l ≥ l - t? Actually need to check: l - t ≤ l, and D > l implies lower = t+D > t+l >=? Actually worst case: when t=0, lower = D > l, upper = l, so lower > upper. So no contributions. So loop runs but nothing added. It's fine.
Now, test extreme: a=1,b=1,c=1,l=300000. total huge, but computing in loops 300k each, still fine.
Now, think about potential pitfalls: Our count_bad uses (t+1) * (upper-lower+1). upper-lower+1 could be negative? We guard with lower <= upper, so it's positive.
Now, ensure we use long long consistently.
Now, double-check total formula: number of nonnegative integer solutions to x+y+z ≤ l. The number of solutions to x+y+z = k is C(k+3-1,3-1) = C(k+2,2) = (k+2)(k+1)/2. Sum_{k=0}^{l} (k+2)(k+1)/2. The closed form is C(l+3,3). Indeed, (l+3 choose 3) = (l+3)(l+2)(l+1)/6. Good.
Now, alternative verification: For l=2, we got 10. Good.
Now, check with sample 2: we did, correct.
Now, confirm for sample 3: we did, correct.
Thus solution is ready.
But before finalizing, consider if there is any scenario where total - bad could become negative? Should not, as total includes all triples, and bad are disjoint subsets. But if we miscount and count some bad multiple times, could lead negative. But our counts are disjoint? We argued they are, but what about triple where a' = b' + c' exactly? That is counted in N_A. Could it also be counted in N_B? Check: If a' = b' + c', then b' = a' - c'. Since a' = b' + c', then b' = (b' + c') - c' = b', trivial. But does b' ≥ a' + c'? That would require b' ≥ (b'+c')+c' = b'+2c' -> 0≥2c' impossible. So not in N_B. So disjoint.
Thus answer non-negative.
Now, we might want to verify with a scenario where l is large and D negative to ensure no overflow in multiplication (t+1)(upper-lower+1) could be up to (l+1)(l+1) ~ 9e10, and summed up to ~4.5e15. That's within 64-bit.
Now, what about potential performance when D negative large? In that case, for many t, lower=0, and we add contributions. That sum yields total. But we also call count_bad for the other sides with D positive; that will be quick because contributions zero for many t? Actually it still loops through all t, but that's fine.
Thus final code.
Now, I'd like to test with some random small values via brute force to be confident. Let's do mental test for a few.
Case: a=3,b=4,c=5,l=3.
Total = C(6,3)=20.
We can manually compute? Possibly, but let's trust.
Compute N_A: D_A=4+5-3=6 >0, l=3, so no t with 2t ≤ l - D? Actually need t such that t+6 ≤ 3-t -> t+6 ≤3-t => 2t ≤ -3 impossible. So N_A=0.
N_C: D_C=3+4-5=2 >0: need t+2 ≤3-t => 2t ≤1 => t=0 possible. For t=0: lower=2, upper=3 =>? l=3, upper = l - t =3, lower=2, so ways_p = 2 (p=2,3?) Actually p values: from lower to upper inclusive: 2,3 => 2 ways. t+1=1 => contribution 2. For t=1: lower=3, upper=2 -> 0. So N_C=2.
Thus bad=2, answer=18.
Now let's enumerate all triples (x,y,z) with sum≤3 that cause degenerate triangle? Only condition C might hold: z ≥ x+y+2? Actually D_C=2, so condition z - x - y ≥2 => z ≥ x+y+2.
Thus degenerate triples: (0,0,2), (0,0,3), (1,0,2), (0,1,2). That's 4? Wait also (2,0,1)? no. Check (1,0,2): z=2, x=1,y=0 => x+y=1 =>2≥3? No, 2 ≥ 1+2? Actually condition: z ≥ x+y+2 => 2 ≥ 1+0+2 =3? false! Oops miscalc: D_C=2, condition z - x - y ≥ 2 => z ≥ x+y+2. For (1,0,2): x+y=1, so x+y+2=3, z=2 -> 2≥3 false. So not degenerate. Wait earlier we computed for (1,0,2) as true? Let's recalc: (1,0,2) sum=3. Check triangle: new lengths: a'=3+1=4, b'=4+0=4, c'=5+2=7. Check 4+4=8 >7 ok, 4+7=11>4, 4+7=11>4 -> triangle. So it's non-degenerate, not bad. So my earlier counting might be off.
Let's recompute N_C from formula gave 2. That suggests only 2 degenerate triples. Which are they? Possibly (0,0,2) and (0,0,3)? Let's check (0,0,2): lengths 3,4,7 -> 3+4=7 degenerate. (0,0,3): 3,4,8 -> 3+4=7<8? Actually 3+4=7 <8, that's not degenerate? Wait degenerate requires one side >= sum of other two. For (0,0,3): a'=3, b'=4, c'=8. Check: 3+4=7 <8, so c' is larger than sum of others, so indeed 8 ≥ 3+4? 8 ≥ 7 yes, so it's degenerate because c' >= a'+b'? Actually condition for triangle is a'+b' > c'. Here 7 is not >8, it's less, so c' > a'+b', so triangle invalid. So (0,0,3) is degenerate (since 8 > 7). But condition we used is z ≥ x+y+2: for (0,0,3): x=0,y=0,z=3 => x+y+2=2, 3≥2 true, so qualifies as bad. That matches.
Now (0,0,2): x=0,y=0,z=2 => 2≥2 true, qualifies.
Now (1,0,2) we saw false. (0,1,2): 2≥0+1+2=3 false. So only two. So N_C=2. That matches enumeration: (0,0,2) and (0,0,3). Also check (0,0,? ) maybe (0,0,1) false; (0,0,0) false. So indeed 2.
Thus answer 18. That seems plausible.
Now count total triples: sum_{k=0}^{3} C(k+2,2) = for k=0:1, k=1:3, k=2:6, k=3:10 total=20. Subtract 2 = 18. Good.
So our method is consistent.
Thus final answer.
Now edge case: l up to 300k, a,b,c up to 300k, D might be as low as -300k. Our loop from t=0 to l includes 300k iterations, each with constant operations, fine.
Now, one more test: If D is extremely negative, say D = -1e9, but D can't be that low because a,b,c ≤ 3e5, so D min about -3e5. So safe.
Now we should consider if we need to handle the case where total - bad might become negative due to overflow? No.
Now, I will implement.
But there is one more nuance: The problem says "increase length of some of them by some positive integer number of centimeters (different sticks can be increased by a different length), but in total by at most l centimeters." That means we can increase some sticks by positive integer amounts, but it says "by some positive integer number of centimeters". It says "increase length of some of them by some positive integer number of centimeters (different sticks can be increased by a different length), but in total by at most l centimeters. In particular, it is allowed not to increase the length of any stick." The phrase "positive integer number of centimeters" suggests that if we increase a stick, we must increase it by a positive amount? Actually reading: "You can increase length of some of them by some positive integer number of centimeters (different sticks can be increased by a different length), but in total by at most l centimeters. In particular, it is allowed not to increase the length of any stick." This implies that for each stick, the amount we increase is a nonnegative integer. Because they say "it is allowed not to increase the length of any stick" meaning we can choose to increase zero. But they also say "increase length of some of them by some positive integer number of centimeters" might be ambiguous. Usually in such problems, the increase amounts are nonnegative integers, and you can choose not to increase some. The phrase "positive integer" might refer to the fact that if you decide to increase, the amount must be positive, but you can also choose not to increase. The note clarifies that it's allowed not to increase any stick. So yes, x,y,z ∈ ℕ (nonnegative integers). So our model correct.
Now, check if there is any other condition about "some sticks" meaning at least one? The note says "it is allowed not to increase the length of any stick." So all can be zero.
Thus fine.
Now, final answer.
But before finalizing, I realize something: In our count_bad we used (t+1) as number of (q,r) pairs with sum t. This is correct for unordered? Actually for given t, q and r are two independent variables that can be any nonnegative integers summing to t. The number of such pairs is t+1 (since q=0..t, r = t - q). Yes.
Now for N_B: we treat q = x, r = z, with sum t = x+z. So number of (x,z) with sum t is t+1. Good.
Now for N_C: q = x, r = y, sum t = x+y, number = t+1. Good.
So all consistent.
Thus answer.
Now, we might also consider that we could compute total directly as sum_{x=0}^{l} number of (y,z) with y+z ≤ l - x, which is sum_{x=0}^{l} C(l - x + 2, 2) but we have closed form.
Now, I will produce final code.
But wait: double-check for l=0 and D=0, N_C: t=0: lower = max(0, 0+0)=0, upper =0, ways_p=1, t+1=1 => N_C=1. That counts (0,0,0) as bad when c ≥ a+b? Actually D_C = a+b - c =0 means a+b = c. So indeed (0,0,0) is degenerate. Good.
Now, test a=2,b=2,c=4,l=0: D_C=0, N_C=1, others 0, total=1, ans=0, correct.
Now test a=3,b=4,c=5,l=0: D_C=2>0, all D positive, N_A,N_B,N_C zero, total=1, ans=1, correct.
Now test a=5,b=1,c=1,l=0: D_A = 1+1-5=-3, N_A=1, others 0, total=1, ans=0, correct.
Thus correct.
Now let's test scenario with l>0 but D=0: count_bad(0,l) counts triples where p ≥ q+r. So N = number of triples with p ≥ q+r and sum ≤ l. That's exactly the number of triples where the side with original length a (if D corresponds to that) after increase becomes at least sum of other two.
Now we can test a=2,b=2,c=4,l=1. Initially c = a+b, degenerate. Total=4. Triples: (0,0,0): degenerate; (1,0,0): (3,2,4) => 3+2=5>4 ok? 3+2>4, 3+4>2, 2+4>3 => triangle; (0,1,0): (2,3,4) => 2+3=5>4, triangle; (0,0,1): (2,2,5) => 2+2=4<5 degenerate. So good: (1,0,0),(0,1,0) are good, (0,0,0) and (0,0,1) are bad. So answer=2.
Compute with our method: total=4. D_A = b+c - a = 2+4-2=4 >0 -> N_A=0; D_B = a+c - b = 2+4-2=4 ->0; D_C = a+b - c = 2+2-4=0 -> N_C = count_bad(0,1). t from 0 to1: t=0: lower=0, upper=1 => ways_p=2 (z=0,1), t+1=1 =>2. t=1: lower=1, upper=0 =>0. So N_C=2. Bad=2, answer=2. Good.
Now, test a=2,b=2,c=4,l=2. Total = C(5,3)=10. Let's compute manually? Could but trust.
Now compute N_A=0, N_B=0, N_C: D=0, count_bad(0,2). t=0: lower=0, upper=2 -> ways_p=3, t+1=1 =>3. t=1: lower=1, upper=1 -> ways_p=1, t+1=2 =>2. t=2: lower=2, upper=0 -> 0. So N_C=5. So answer=5. Let's see if that matches expectation: total=10, good=5. Which are good? We can list: triples sum ≤2. List all 10 triples and see which are non-degenerate.
Now, we should also consider if the answer may exceed 2^31-1, but it's within 64-bit.
Now, implement.
Potential micro-optimization: Use int for loop counter to avoid 64-bit overhead? But it's fine.
Now, final code.
But I recall that the total number of triples x+y+z ≤ l is C(l+3,3). However, note that l can be up to 300k, so (l+3)(l+2)(l+1) may overflow 32-bit, but we use long long. However, in C++ if we write (l+3)(l+2)(l+1)/6, these multiplications are performed with the type of l. If l is int, then (l+3)(l+2) might overflow int before being promoted to long long. To avoid, we can cast to long long: ll total = (ll)(l+3)(l+2)*(l+1)/6; or define l as long long from the beginning. Since input constraints are within int, but to be safe we read into long long variables.
I'll read into long long.
Now, for count_bad, we might want to break early if lower > upper and for D>0 it will be from some t onward always >? Not necessary.
Now, final answer.
But there is one more nuance: The problem statement says "increase length of some of them by some positive integer number of centimeters (different sticks can be increased by a different length), but in total by at most l centimeters." This might be interpreted as we have to allocate a total amount of increase (which is a positive integer? Actually total increase ≤ l, but the increase per stick, if chosen, must be positive. However, they explicitly say "it is allowed not to increase the length of any stick." So it means we can increase by 0 as well. So x,y,z can be zero. So okay.
Now, think about if total increase exactly equals l? Yes, ≤ l.
Now, done.
We should also consider if there is any restriction that we cannot increase a stick beyond some bound? No.
Thus final solution.
But before concluding, I'd like to check if there is any subtle issue with the interpretation of "some sticks" meaning at least one? The note clarifies that it's allowed not to increase any stick, so zero allowed.
Thus final.
Now, I'll provide final answer with explanation.
However, I notice that the problem statement may be from Codeforces (problem 1045I? Not sure). The constraints are moderate, and many solutions use combinatorial formulas.
One more check: In our count_bad, we included the case where p, q, r all zero? Yes, that's part of the sum. That's fine.
Now, I will code accordingly.
But let's also test with random small values using brute force in mind:
So good: all except (0,2,0) and (0,0,2). That's 8 good. So matches.
Thus method works.
Now we can implement.
But we need to ensure that our count_bad for N_B and N_C is indeed counting the correct triples. For N_B we used D_B = a + c - b, and we treat p=y, q=x, r=z. That matches condition y ≥ x+z + D_B? Actually condition b+y ≥ a+x + c+z => y - x - z ≥ (a + c - b) = D_B. So yes.
Thus final.
Now, final step: Provide answer.
Before final code, I will think about an alternative approach: using inclusion-exclusion? But we already have.
Now, consider if there is any scenario where total - sum_bad might be negative due to integer overflow? No.
Now, I will code.
But there is a subtlety: In count_bad, we use for (ll t = 0; t <= l; t++). But if D is large negative, lower may be 0 and upper may be negative for large t? Actually t goes up to l; for t = l, upper = l - l = 0, lower = max(0, l + D). If D is negative enough that l+D <=0, then lower = 0, and upper = 0, so f=1? Actually upper - lower +1 = 1. That's fine. So all t contribute. So for D negative, we include t up to l.
Now, but what if D is less than -l? For example, D = -400000, but l=300000, then for t up to l, t+D is ≤ -100000, so lower always 0, upper = l - t, which goes from l down to 0, so f(t) = l - t + 1, always positive. So sum is total. So it works.
Now, we should consider that we might be able to compute count_bad more directly using formulas for the sum ∑_{t} (t+1)*(l-t+1) when D very negative, which equals total. That's fine.
Thus solution is ready.
Now I'll write final answer.
But wait, there is another nuance: The problem says "increase length of some of them by some positive integer number of centimeters". This could be interpreted that we can increase only some sticks, i.e., we can choose a subset of sticks to increase, and for those chosen, we must increase by at least 1. But they also say "it is allowed not to increase the length of any stick." This might imply that if we decide to increase a stick, the increase must be positive. But they didn't explicitly say that we cannot increase a stick by 0. Usually in such phrasing, it's ambiguous. However, typical Codeforces problem "Increase and Decrease" or something? Let's search memory: This looks like Codeforces problem "Three sticks" maybe? Actually I recall a problem: "A. Make a triangle!" or something else? There is a known problem: given three sticks lengths a,b,c, you can increase total length by at most l, count number of ways to form non-degenerate triangle. Many solutions used combinatorial counting like above. And they considered x,y,z nonnegative. Because they allowed not to increase any stick. Usually the statement says "increase some sticks by positive integer lengths", but then clarifies "it is allowed not to increase any stick". So it means that for each stick, we either leave it unchanged or increase it by a positive amount. That suggests that x,y,z can be zero or positive, but if we decide to increase, we must add at least 1. But the phrase "increase some sticks by some positive integer number of centimeters" could be interpreted as we can only increase sticks that we choose, and we increase each chosen stick by a positive amount. However, the note "it is allowed not to increase the length of any stick" means we may choose to increase none. But does it also allow increasing a stick by 0? If we don't increase it, it's not increased. So it's essentially same as x,y,z being nonnegative. Because if we decide not to increase a stick, we set increase to 0, which is allowed. There's no requirement that we cannot set increase to 0 for a stick we "choose to increase"? Actually the phrase "increase some of them" suggests we can select a subset to increase; for those selected, we must increase by positive amount; for those not selected, increase is 0. So indeed x,y,z are either 0 or positive. But that's exactly nonnegative integers, with the interpretation that 0 means not increased. But does this disallow increasing a stick by 0 if we "select" it? No, we wouldn't select it then. So it's fine: any nonnegative integer assignment is achievable by selecting sticks with positive increase for those where x>0, and not selecting those with x=0. So it's the same as x,y,z ≥ 0.
Thus our interpretation matches.
Now, what about the phrase "in total by at most l centimeters" meaning sum of increases is ≤ l. So correct.
Now, verify with sample 1: they counted (0,0,0) as a way, which corresponds to not increasing any stick. That's allowed.
Thus we are good.
Now, produce final answer.
But I want to double-check the total formula again: number of solutions to x+y+z ≤ l, with x,y,z ≥ 0. That's indeed combination with repetition: we have 3 types and we allocate up to l indistinguishable units, with each variable nonnegative. The number of solutions to x+y+z ≤ l is C(l+3,3). Because we can add a slack variable s ≥ 0 such that x+y+z+s = l. Then number of nonnegative solutions to 4 variables sum to l is C(l+4-1,4-1)=C(l+3,3). Yes.
Thus total = (l+3 choose 3).
Now final code.
Time complexity O(l) = 3e5, memory O(1).
Now, possible pitfalls: using int for loop variable might overflow when l is 300k, but int is fine (up to 2e9). Use int for t.
Now, code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll count_bad(ll D, ll l) {
ll bad = 0;
for (ll t = 0; t <= l; ++t) {
ll lower = t + D;
if (lower < 0) lower = 0;
ll upper = l - t;
if (lower <= upper) {
bad += (upper - lower + 1) * (t + 1);
}
}
return bad;
}
int main() {
ll a, b, c, l;
cin >> a >> b >> c >> l;
ll total = (l+1)(l+2)(l+3)/6; // compute (l+3 choose 3)
ll bad = count_bad(b + c - a, l) + count_bad(a + c - b, l) + count_bad(a + b - c, l);
cout << total - bad << endl;
return 0;
}
Note: I used (l+1)(l+2)(l+3)/6; Wait which is correct: (l+3 choose 3) = (l+3)(l+2)(l+1)/6. So either order. Good.
Now test with sample: 1 1 1 2.
l=2 => total = (345)/6 = 60/6=10. OK.
Now, what about l=0: (123)/6 = 6/6=1.
Now, final answer.
But I recall that sometimes integer division should be done after multiplication but careful about order to avoid overflow? But we are using 64-bit.
Now, we might also consider if there is any scenario where total - bad is negative due to overflow? No.
Now, the answer fits in 64-bit.
Thus final.
Now, we should also check for large l maybe near 3e5: total is about 4.5e15, which fits in signed 64-bit (max 9e18). So safe.
Now, we can possibly micro-optimize: use for (int t=0; t<=l; t++) and cast to ll when needed. But it's fine.
Now, final answer.
Now, note: We are counting N_A, N_B, N_C separately. But there is a possibility of overcounting if the same triple satisfies two conditions? Already argued they are disjoint. However, there is a subtle corner: when a,b,c are such that some sides become zero? Not possible.
Thus done.
Now, final answer.
One more thought: In the problem statement, they say "You are given three sticks with positive integer lengths of a, b, and c centimeters." So initial lengths are positive. After increase, they remain positive. So no zero.
Thus all good.
Now, produce final solution.
But before final, we may consider if there is any need to use unsigned long long? Not necessary.
Now, I will write final answer with explanation.
However, I notice that the code might be slightly optimized by early breaking when lower > upper for D>0 and t sufficiently large, but not needed.
Now, we can also reduce loops by computing analytically, but not needed.
Now, finalize.
Let's test with some random small values via mental brute to further assure:
Case: a=3,b=4,c=8,l=2.
Total=10.
D_A=4+8-3=9>0 ->0.
D_B=3+8-4=7>0 ->0.
D_C=3+4-8=-1. So N_C count_bad(-1,2).
So good: (2,0,0), (0,2,0), (1,1,0) => 3 good. matches.
Thus correct.
Now, we are done.
Now, about performance: 3 loops each 300k -> 900k iterations, each with a few integer operations, under 0.01 seconds.
Now, final answer.
I'll now produce final response with solution explanation and code. We can count the total number of ways to increase the sticks and then subtract those that lead to a degenerate triangle.
The sticks after increases are (a' = a + x,; b' = b + y,; c' = c + z) with (x,y,z \ge 0) and (x+y+z \le l).
A triangle is degenerate exactly when one side is at least the sum of the other two.
The three conditions
[
a' \ge b' + c', \quad b' \ge a' + c', \quad c' \ge a' + b'
]
are mutually exclusive (they cannot happen together for positive lengths).
The same formula works for (N_B) and (N_C) with the appropriate (D) and the roles of the variables permuted.
Total number of triples
The number of non‑negative integer triples ((x,y,z)) with (x+y+z \le l) is
[
\binom{l+3}{3} = \frac{(l+1)(l+2)(l+3)}{6}.
]
Implementation
For each of the three sides we compute (N) by iterating (t) from (0) to (l).
The values fit in 64‑bit integers, and the loop length is at most (3 \cdot 10^5), which is fast.
cpp
#include <iostream>
using namespace std;
using ll = long long;
ll count_bad(ll D, ll l) {
ll bad = 0;
for (ll t = 0; t <= l; ++t) {
ll lower = t + D;
if (lower < 0) lower = 0;
ll upper = l - t;
if (lower <= upper)
bad += (upper - lower + 1) * (t + 1);
}
return bad;
}
int main() {
ll a, b, c, l;
cin >> a >> b >> c >> l;
ll total = (l + 1) * (l + 2) * (l + 3) / 6;
ll bad = count_bad(b + c - a, l)
+ count_bad(a + c - b, l)
+ count_bad(a + b - c, l);
cout << total - bad << endl;
return 0;
}