CF 317 A. Lengthening Sticks(容斥+组合数学)
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.
The single line contains 4 integers a, b, c, l (1 ≤ a, b, c ≤ 3·10^5, 0 ≤ l ≤ 3·10^5).
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.
1 1 1 2
4
1 2 3 1
2
10 2 1 7
0
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(容斥+组合数学)的更多相关文章
- 2015 asia xian regional F Color (容斥 + 组合数学)
2015 asia xian regional F Color (容斥 + 组合数学) 题目链接http://codeforces.com/gym/100548/attachments Descrip ...
- 容斥 + 组合数学 ---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 ...
- cf#305 Mike and Foam(容斥)
C. Mike and Foam time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces #317 C.Lengthening Sticks(数学)
C. Lengthening Sticks time limit per test 1 second memory limit per test 256 megabytes input standar ...
- BZOJ2839:集合计数(容斥,组合数学)
Description 一个有N个元素的集合有2^N个不同子集(包含空集),现在要在这2^N个集合中取出若干集合(至少一个),使得它们的交集的元素个数为K,求取法的方案数,答案模1000000007. ...
- 【BZOJ4559】[JLoi2016]成绩比较 动态规划+容斥+组合数学
[BZOJ4559][JLoi2016]成绩比较 Description G系共有n位同学,M门必修课.这N位同学的编号为0到N-1的整数,其中B神的编号为0号.这M门必修课编号为0到M-1的整数.一 ...
- Gym 100548F Color 给花染色 容斥+组合数学+逆元 铜牌题
Problem F. ColorDescriptionRecently, Mr. Big recieved n flowers from his fans. He wants to recolor th ...
- [CTS2019]随机立方体(容斥+组合数学)
这题七次方做法显然,但由于我太菜了,想了一会发现也就只会这么多,而且别的毫无头绪.发现直接做不行,那么,容斥! f[i]为至少i个极值的方案,然后这里需要一些辅助变量,a[i]表示选出i个三维坐标均不 ...
- 51nod1667-概率好题【容斥,组合数学】
正题 题目链接:http://www.51nod.com/Challenge/Problem.html#problemId=1667 题目大意 两个人. 第一个人有\(k_1\)个集合,第\(i\)个 ...
随机推荐
- HTML前序
HTML基本格式 <!DOCTYPE html> //文档类型声明 <html lang="zh-cn"> //表示HTML文档开始,属性lang,属性值= ...
- s2第六章继承和多态
public class Employee { //年龄 public int Age { get; set; } //性别 public Gender Gender { get; set; } // ...
- 在IDEA中实战Git-branch
工作中多人使用版本控制软件协作开发,常见的应用场景归纳如下: 假设小组中有两个人,组长小张,组员小袁 场景一:小张创建项目并提交到远程Git仓库 场景二:小袁从远程Git仓库上获取项目源码 场景三:小 ...
- py-day3-4 python 匿名函数
# 匿名函数 lamdba name = 'xiaoma' f = lambda x:x+'jun' res = f(name) print('匿名函数的运行结果:',res) 匿名函数的运行结果: ...
- Java面试题 OOAD & UML+XML+SQL+JDBC & Hibernate
二.OOA/D 与UML 部分:(共6 题:基础2 道,中等难度4 道) 96.UML 是什么?常用的几种图?[基础] 答:UML 是标准建模语言:常用图包括:用例图,静态图(包括类图.对象图和包图) ...
- jekins构建触发器详解
jenkins版本:2.89.2 1.触发远程构建 (例如,使用脚本):通过一个网址的访问来触发构建,这样就不需要登录jenkins系统也能触发构建了. 示例地址: http://localhost: ...
- 算法实践--最长公共子序列(Longest Common Subsquence)
什么是最长公共子序列 X=ACCG Y=CCAGCA 长度为1的公共子序列: {A} {C} {G} 长度为2的公共子序列:{AC} {CC} {CG} {AG} 长度为3的公共子序列:{ACG} 长 ...
- ubuntu16.04 安装最新版nodejs
ubuntu软件仓库中自带的nodejs版本过低 $ apt-cache policy nodejs nodejs: Installed: (none) Candidate: 4.2.6~dfsg-1 ...
- docker容器内存占用 之 系统cache,docker下java的内存该如何配置
缘起: 监控(docker stats)显示容器内存被用完了,进入容器瞅了瞅,没有发现使用内存多的进程,使用awk等工具把容器所有进程使用的内存加起来看看,距离用完还远了去了,何故? 分析: 该不会d ...
- CSS之form&span
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...