Lengthening Sticks CodeForces - 571A


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

 







  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值