Lengthening Sticks CodeForces

来源:互联网 发布:网络建设服务商 编辑:程序博客网 时间:2024/06/05 02:53


Lengthening Sticks

You are given three sticks with positive integer lengths ofa, 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 mostl 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 integersa, 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 mostl centimeters, so that you can make a non-degenerate triangle from it.

Input

1 1 1 2
Output
4
Input
1 2 3 1
Output 
2



         题意: 给定 a b c L, a b c 分别为  三角形的三边,L 为可加的最大长度,即在三条边上加任意长度,但不可以都不加,不可以加的总长度超过 l,求方案数;

        思路: 容斥,先算出总的方案数,再减去不满足条件的即  :   不能构成三角形的,还有没有加的。
对于  总数:  可以自己模拟几个 例子, L =2,3,4,5....  的时候分别是多少然后找出 公式 ,即  ans+=(i+1)*(i+2)/2;
减的时候 分别以三边为底边,然后枚举一遍 L 就行。
       注意 :好像会爆 int  , 并不只是 在 ans  , s  上爆,反正我直接全部搞成 Longlong 就过了样例八。


#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;}

 







原创粉丝点击