传送门:点我
A. Lengthening Sticks 
time limit per test       

1 second

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.

Input

The single line contains 4 integers a, b, c, l (1 ≤ a, b, c ≤ 3·10^5, 0 ≤ l ≤ 3·10^5).

Output

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.

Examples
input
1 1 1 2
output
4
input
1 2 3 1
output
2
input
10 2 1 7
output
0
Note

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.

大意:

给定三角形的三边a,b,c,然后给定一个长度L。可以把长度 len(len<= L)拆成三份后,添加到a,,b,c任意一边。问能构成的三角形的个数。(a=2,b=2,c=3 和 a=3,b=2,c=2是两种不同的情况)

比如说样例2,1 2 3 1。可以把1加到a上 变成2 2 3,可以把1加到b上 变成1 3 3。所以答案输出2。

思路:

全部情况 - 不构成三角形的情况 = 构成三角形的情况

全部情况:

对于长度len,考虑切2刀分成3部分,因为可以是切在0这个位置上,即可以出现0,0,len这种情况,所以情况一共有C(len+2,2),表示把长度为len的线段,取2个点切开,然后把这三段分配给a,b,c的所有情况。(包括可构成三角形和不可构成三角形)

不构成三角形的情况

一个三角形三边分别为a,b,c,如果a>=b+c,则构不成三角形

首先考虑最长边是a,对于要加的len,分成p和len-p两部分,其中p给a,len-p给b和c。可以确定的是如果a>=b+c,那么p最少可以是0,如果b+c>a,那么p起码得是b+c-a。这样才能满足即使len-p是0的情况下,让a+p>=b+c。

所以枚举p的起点是max(b+c-a,0)。之后考虑len-p分配给b和c的情况有几种。

因为要构成a+p>=b+c+len-p。所以最大可增加的长度是min(len-p,a+p-b-c),(解释一下,len-p是给了a之后剩下来的,a+p-(b+c)是本身要满足a+p>=b+c,最多允许a+p==b+c,所以对这两个值取最小值就是b和c最大可以增加的长度)

记这个最大可以增加的长度为max_addlen,分配给b和c的情况一共有 (max_addlen+1)*(max_addlen+2)/2 种。

这个很容易证明,在max_len中间砍两刀分为3部分,其中2部分给b和c。情况依旧是C(max_addlen+2,2)

所以去枚举增加长度就可以得到构不成三角形的全部情况了。具体看代码,同时注意用long long。

代码:

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
#define MMINF(x) memset(x,INF,sizeof(x))
#define LL long long
using namespace std;
LL l;
LL solve(LL a,LL b,LL c)//枚举a为最长边时候所有构不成三角形的情况
{
LL ans = ;
for(LL i = max(b+c-a,0LL) ; i <= l ; i ++) //起点是max(b+c-a,0LL)
{
LL max_addlen = min(a+i-b-c,l-i);//给b和c 最大可以增加的长度
ans += (max_addlen+)*(max_addlen+)/;
}
return ans;
}
int main()
{
LL a,b,c ;
cin>>a>>b>>c>>l;
LL ans = ;
for(LL i = ; i <= l ; i ++){
ans += (i+)*(i+)/;
}//可增加的所有情况
ans -= solve(a,b,c);
ans -= solve(b,a,c);
ans -= solve(c,a,b);
//分别枚举a,b,c是最长边的情况
cout<<ans<<endl;
}
/*
10 2 1 7
1 2 3 1
*/

CF 317 A. Lengthening Sticks(容斥+组合数学)的更多相关文章

  1. 2015 asia xian regional F Color (容斥 + 组合数学)

    2015 asia xian regional F Color (容斥 + 组合数学) 题目链接http://codeforces.com/gym/100548/attachments Descrip ...

  2. 容斥 + 组合数学 ---Codeforces Round #317 A. Lengthening Sticks

    Lengthening Sticks Problem's Link: http://codeforces.com/contest/571/problem/A Mean: 给出a,b,c,l,要求a+x ...

  3. cf#305 Mike and Foam(容斥)

    C. Mike and Foam time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  4. Codeforces #317 C.Lengthening Sticks(数学)

    C. Lengthening Sticks time limit per test 1 second memory limit per test 256 megabytes input standar ...

  5. BZOJ2839:集合计数(容斥,组合数学)

    Description 一个有N个元素的集合有2^N个不同子集(包含空集),现在要在这2^N个集合中取出若干集合(至少一个),使得它们的交集的元素个数为K,求取法的方案数,答案模1000000007. ...

  6. 【BZOJ4559】[JLoi2016]成绩比较 动态规划+容斥+组合数学

    [BZOJ4559][JLoi2016]成绩比较 Description G系共有n位同学,M门必修课.这N位同学的编号为0到N-1的整数,其中B神的编号为0号.这M门必修课编号为0到M-1的整数.一 ...

  7. Gym 100548F Color 给花染色 容斥+组合数学+逆元 铜牌题

    Problem F. ColorDescriptionRecently, Mr. Big recieved n flowers from his fans. He wants to recolor th ...

  8. [CTS2019]随机立方体(容斥+组合数学)

    这题七次方做法显然,但由于我太菜了,想了一会发现也就只会这么多,而且别的毫无头绪.发现直接做不行,那么,容斥! f[i]为至少i个极值的方案,然后这里需要一些辅助变量,a[i]表示选出i个三维坐标均不 ...

  9. 51nod1667-概率好题【容斥,组合数学】

    正题 题目链接:http://www.51nod.com/Challenge/Problem.html#problemId=1667 题目大意 两个人. 第一个人有\(k_1\)个集合,第\(i\)个 ...

随机推荐

  1. vue2中使用mint-ui,性别选择

    安装需要的组件 import { DatetimePicker,Toast,Popup,Picker } from 'mint-ui'; templete部分 <div class=" ...

  2. psql的安装与数据库创建(ubuntu)

    来自阮一峰日志 http://www.ruanyifeng.com/blog/2013/12/getting_started_with_postgresql --------------------- ...

  3. PythonStudy1——Python 值拷贝 浅拷贝 深拷贝

    拷贝:对值进行复制的过程 # 值拷贝:应用场景最多  ls = [1, 'abc', [10]] ls1 = ls # ls1直接将ls中存放的地址拿过来  # ls内部的值发生任何变化,ls1都会随 ...

  4. 乐乐课堂_leleketang.com

    乐乐课堂_leleketang.com https://www.baidu.com/sf?pd=video_page&sign=12394301609542619800&word=抛物 ...

  5. mongodb与mysql命令详细对比

    传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(collection).文档对象(docu ...

  6. leetcode习题练习

    day001 #!user/bin/env python # -*- coding:utf-8 -*- #day001 两数之和 #方法1 def Sum(nbs,tgt): len_nums = l ...

  7. 树莓派apt-get The value '\stable' is invalid for APT 错误

    对apt-get进行任何操作都会报错: pi@raspberrypi:~ $ sudo apt-get upgrade Reading package lists... Done E: The val ...

  8. Entity Framework Code first 可能会导致循环或多个级联路径.

    用code first映射数据库报错 Introducing FOREIGN KEY constraint 'FK_dbo.Roles_dbo.SubSystems_SubSystemID' on t ...

  9. 廖雪峰Java7处理日期和时间-3java.time的API-2ZonedDateTime

    ZonedDatetime = LocalDateTime + ZoneId ZonedDateTime:带时区的日期和时间 ZoneId:新的API定义的时区对象(取代几句的java.util.Ti ...

  10. 廖雪峰Java7处理日期和时间-3java.time的API-1LocalDateTime

    1.java.time提供了新的日期和时间API: LocalDate/LocalTime/LocalDateTime ZoneDateTime/ZoneId Instant Formatter 新A ...