思路:
利用容斥原理,首先考虑总的情况,即求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;
}