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.
1 1 1 2
4
1 2 3 1
2
10 2 1 7
0
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.
【题解】 数学题,我不是搞数学的,今天突然想写写数学题,这第一道,搞了半天,硬是没弄出来,问了人家专门搞数学的才似懂非懂,不禁大呼数学大法好,,,贴出来纪念一下,,受虐的印记,
标题说了,要用组合数学和容斥原理,又学了一下这些,,题目给了三角形三个边的长度,和可用长度,要求给每条边加上一定的值,使其依然满足三角形,三条边加的数的总和不大于可用长度,也可以三个边都不加,,问最后的三角形数量,,
参考大牛博客:http://http://blog.csdn.net/u013486414/article/details/47950919
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int T,t;
ll solve(ll a,ll b,ll c,ll m)
{
ll ans=min(a-b-c,m);
if(ans<0)
return 0;
else
return (ans+2)*(ans+1)/2;
}
int main()
{
int i,j,k;
ll a,b,c,n,ans;
scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&n);
ans=(n+3)*(n+2)*(n+1)/6;
for(i=0;i<=n;i++)
{
ans-=solve(a+i,b,c,n-i);
ans-=solve(b+i,a,c,n-i);
ans-=solve(c+i,a,b,n-i);
}
printf("%I64d\n",ans);
}