您的位置:首页 > 其它

codeforces 571 A Lengthening Sticks

2015-08-23 11:45 399 查看
题意:给出a,b,c,l要求a+x,b+y,c+z构成三角形,x+y+z<=l,成立的x,y,z有多少种。

做法:很显然可以容斥,首先考虑总的情况,也就是x+y+z<=l有多少种解,就相当于求所有i(i<=l)满足x+y+z=i有多少种解,也就是组合数C(i+2,2),为啥?因为相当于刻度从1开始然后长度为i的尺子拆成三份有多少种情况,也就是在不同的位置切两刀,然后考虑,可以有最多两份长度为0,也就是说在0到1之间可以切两刀于是总共就是i+2个可以切的位置中选择两个。

接下来容斥,将不符能构成三角形的排除,三角形满足的条件是什么?任意两边之和大于第三边,那么不满足的必要条件就是第三边大于等于其它两边之和,枚举a,b,c分别做第三边的情况的时候,再考虑将剩下的l拆分三份分配给a,b,c依旧不满足的情况即可。这里看不懂的,看看代码就懂了。

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
ll work(int a,int b,int c,int l)
{
ll ans=0;
for(int i=max(b+c-a,0);i<=l;i++)
{
ll x=min(l-i,a+i-b-c);
ans+=(1+x)*(2+x)/2;
}
return ans;
}
int main()
{
int a,b,c,l;
cin>>a>>b>>c>>l;
ll ans=0;
for(int i=0;i<=l;i++)
ans+=ll(1+i)*(2+i)/2;
ans-=work(a,b,c,l);
ans-=work(b,a,c,l);
ans-=work(c,a,b,l);
cout<<ans;
}


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·105, 0 ≤ 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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: