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.
1 1 1 2
4
1 2 3 1
2
10 2 1 7
0
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.
题意,给出四个数a,b,c,l,a b c 可以增加任意长的数但a b c增加的总和小于l,要求a b c能构成三角形的个数。
反向思考,要求能构成三角形的个数,我们可以找不能构成三角形的个数。
a b c 任意加长,总的方案数为 这个式子 x + y + z + left = l;的解的个数,x y z left 分别对用a b c增长的长度,和剩下的长度。
这个不定方程的方案数用隔板法,很容易得知是c(l + 3,3);
然后,可以枚举,不能构成三角形的个数,不能构成三角形的条件就是,如果a是最大边,那么b + c <= a,那一定不能构成三角形,所以可以枚举最大边,那可以得到公式,
b + y + c + z < = a + x (a+ x最大边)
y + z <= l - x(总和最大不能过l)
也就是得到方程y + z <= min(l - x,a + x - b -c)不定方程的解的个数。很明显,也就是c(all,2) all = min(l - x,a + x - b -c)
总的复杂度为o(n )注意要用long long ,会爆int.
#define N 205
#define M 100005
#define maxn 205
#define MOD 1000000000000000007
int n,a,b,c,l;
ll Cal(ll x){
if(x < 0) return 0;
return (x + 2)* (x + 1) / 2;
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
while(S2(a,b)!=EOF)
{
S2(c,l);
ll ans = (ll)(l + 3) * (ll)(l + 2) * (ll)(l + 1) / 6;
FI(l+1){
ans -= Cal(min(a + i - b - c,l - i));
ans -= Cal(min(b + i - a - c,l - i));
ans -= Cal(min(c + i - a - b,l - i));
}
cout<<ans<<endl;
}
//fclose(stdin);
//fclose(stdout);
return 0;
}