Description
有三根长度分别为 a,b,c a , b , c 的木棍,现在可以给每根木棍加长度,但加的总长不能超过 l l ,问使得这三根木棍可以组成三角形的方案数
Input
四个整数
Output
输出方案数
Sample Input
1 1 1 2
Sample Output
4
Solution
设给三根木根加的长度分别为 aa,bb,cc a a , b b , c c ,则有 0≤aa+bb+cc≤l 0 ≤ a a + b b + c c ≤ l ,总方案数 C3l+3 C l + 3 3 (即为 l l 个物品放到四个盒子的方案数),现在考虑不合法情况,假设,枚举 aa a a ,则有 0≤bb+cc≤a−b−c+aa 0 ≤ b b + c c ≤ a − b − c + a a ,且注意到 0≤bb+cc≤l−aa 0 ≤ b b + c c ≤ l − a a ,令 t=min(a−b−c−aa,l−aa) t = m i n ( a − b − c − a a , l − a a ) ,那么问题转化为将 t t 个物品放到三个盒子(, cc c c 和多余的)的方案数,由插板法知方案数为 C2t+2 C t + 2 2 ,其他两种情况同理,时间复杂度 O(l) O ( l )
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=100001;
int main()
{
int a,b,c,l;
while(~scanf("%d%d%d%d",&a,&b,&c,&l))
{
ll ans=(ll)(l+1)*(l+2)*(l+3)/6;
for(int i=0;i<=l;i++)
{
int t=min(a-b-c+i,l-i);
if(t>=0)ans-=(ll)(t+1)*(t+2)/2;
t=min(b-a-c+i,l-i);
if(t>=0)ans-=(ll)(t+1)*(t+2)/2;
t=min(c-a-b+i,l-i);
if(t>=0)ans-=(ll)(t+1)*(t+2)/2;
}
printf("%I64d\n",ans);
}
return 0;
}