题意:给定三根木棍的长度,可以任意木棍增加任意长度,但总和不超过L,问最后能构成三角形的方案数是多少
题解:用所有方案数-不能构成的方案数
对于长度l,能够构成的方案数是
(l+22)
(
l
+
2
2
)
,因为相当于一个长度为
l
l
的线段分成三份,并且可以同时去两个。
不能构成的方案数则是,枚举
a、b、c
a
、
b
、
c
当做最长的线段,然后对其增加
i(0<=i<=l)
i
(
0
<=
i
<=
l
)
,对于剩下的长度,那么就相当于长为
l−i
l
−
i
分配的方案数了
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <sstream>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
const int maxn = 500000 + 5;
LL solve(LL a, LL b, LL c, LL l){
LL ans = 0;
for(LL i=max(b+c-a,0LL); i<=l; i++){
LL temp = min(l-i,a+i-b-c);
ans += (1+temp)*(2+temp)/2LL;
}
return ans;
}
int main(){
LL a,b,c,l;
scanf("%lld%lld%lld%lld",&a,&b,&c,&l);
LL ans = 0;
for(LL i=0; i<=l; i++)
ans += (1+i)*(2+i)/2LL;
ans -= solve(a,b,c,l);
ans -= solve(b,a,c,l);
ans -= solve(c,a,b,l);
printf("%lld\n",ans);
}