Lengthening Sticks
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.
The single line contains 4 integers a, b, c, l (1 ≤ a, b, c ≤ 3·105, 0 ≤ l ≤ 3·105).
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.
Input
1 1 1 2
4
1 2 3 1
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll work(ll a,ll b,ll c,ll l)
{
ll s=0;
ll mn=0;
ll mx=a+b+c+l;
for(ll i=a;i<=a+l;i++)
{
if(b+c<=i)
{
mn=min(mx-i,i)-b-c;
s+=1ll*(mn+1)*(mn+2)/2;
}
}
return s;
}
int main()
{
ll a,b,c,l;
while(scanf("%lld%lld%lld%lld",&a,&b,&c,&l)!=EOF)
{
ll ans=0;
for(ll i=0;i<=l;i++)
ans+=(i+2)*(i+1)/2;
ans-=work(a,b,c,l);
ans-=work(b,a,c,l);
ans-=work(c,a,b,l);
printf("%I64d\n",ans);
}
return 0;
}