Codeforces Round #317 [AimFund Thanks-Round] (Div. 1) A. Lengthening Sticks 分类

44 篇文章 0 订阅
A. 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 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;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值