571A Lengthening Sticks

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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·1050 ≤ 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.

Sample test(s)
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.

题意:给出a,b,c,l,要求a+x,b+y,c+z构成三角形,x+y+z<=l,成立的x,y,z有多少种。

这题因为直接求很难,所以可以求出全部的情况,然后再减去不能成立的情况。

对于长度为l的木棒,分成3份,总数是组合数C(n+2,2)。所以算总方案数可以直接把长度从0到n的方案数加一下就行。

然后考虑不符合的方案数,因为要使三角形不能构成,只要使两条边的和小于等于第三条边,所以依次枚举a,b,c为第三条边,然后一次减去不符合的方案数。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define ll __int64
#define inf 0x7fffffff

ll cal(ll a,ll b,ll c,ll l)
{
    ll  ans=0,x,i;
    for(i=max((ll)0,b+c-a);i<=l;i++){
        x=min(l-i,a+i-b-c);
        ans+=(x+1)*(x+2)/2;
    }
    return ans;

}

int main()
{
    ll n,m,i,j,a,b,c,l;
    ll ans;
    while(scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&l)!=EOF)
    {
        ans=1;
        for(i=1;i<=l;i++){
            ans+=(i+1)*(i+2)/2;
        }
        ans-=cal(a,b,c,l);
        ans-=cal(b,a,c,l);
        ans-=cal(c,a,b,l);
        printf("%I64d
",ans);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/herumw/p/9464658.html