CodeForces 571 A.Lengthening Sticks(组合数学)

Description

有三根长度分别为 a,b,c a , b , c 的木棍,现在可以给每根木棍加长度,但加的总长不能超过 l l ,问使得这三根木棍可以组成三角形的方案数

Input

四个整数a,b,c,l(1a,b,c105,0l105)

Output

输出方案数

Sample Input

1 1 1 2

Sample Output

4

Solution

设给三根木根加的长度分别为 aa,bb,cc a a , b b , c c ,则有 0aa+bb+ccl 0 ≤ a a + b b + c c ≤ l ,总方案数 C3l+3 C l + 3 3 (即为 l l 个物品放到四个盒子的方案数),现在考虑不合法情况,假设a+aab+bb+c+cc,枚举 aa a a ,则有 0bb+ccabc+aa 0 ≤ b b + c c ≤ a − b − c + a a ,且注意到 0bb+cclaa 0 ≤ b b + c c ≤ l − a a ,令 t=min(abcaa,laa) t = m i n ( a − b − c − a a , l − a a ) ,那么问题转化为将 t t 个物品放到三个盒子(bb, 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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值