Codeforces Round #317 [AimFund Thanks-Round] (Div. 2) C Lengthening Sticks(组合+容斥)

思路:

利用容斥原理,首先考虑总的情况,即求x+y+z<=l有多少种情况,即求x+y+z=i(i<=l)有多少种情况。

是组合数C(i+2,2),因为相当于刻度从1开始然后长度为i的尺子拆成三份有多少种情况,也就是在不同的位置切两刀,然后考虑,可以有最多两份长度为0,也就是说在0到1之间可以切两刀于是总共就是i+2个可以切的位置中选择两个。

然后进行容斥,把不符合形成三角形条件的情况(两边之和小于等于第三边)减去,枚举a,b,c分别做第三条边时的情况(设为a),先把它加成最大第三边,用去长度i(i>=0)。然后枚举它长度是a+i, a+i+1,....a+L的情况。此时假设当前用去长度i,则最大可用剩余长度是min( L - i, a + i - b - c),然后在这些长度里面任意分配。


#include<bits\stdc++.h>
using namespace std;
#define LL long long
LL cal(LL a,LL b,LL c,LL l)
{
     LL ans = 0;
	 for(LL i = max(b+c-a,0LL);i<=l;i++)
	 {
		 LL x = min(l-i,a+i-b-c);
		 ans+=(x+2)*(x+1)/2;
	 }
	 return ans;
}
int main()
{
     LL a,b,c,l;
	 LL sum = 0;
	 cin >> a >> b >> c >> l;
	 for(LL i = 0;i<=l;i++)
		 sum+=(i+2)*(i+1)/2;
	 sum-=cal(a,b,c,l);
	 sum-=cal(b,a,c,l);
	 sum-=cal(c,a,b,l);
	 cout << sum << endl;
}


C. 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.

Examples
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.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值