codeforces 571A--Lengthening Sticks(组合+容斥)
1 second
256 megabytes
standard input
standard output
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·105, 0 ≤ l ≤ 3·105).
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.
题目链接:点击打开链接
题目大意:给出三条边的长度。能够给随意一条边添加随意的长度,可是添加的长度的总和不能超过l。问有多少种添加的方法,可使得三条边任然能组成一个三角形。
用总的方法数-不能组成三角形的方法数
首先求出全部的能添加的方法,假设三条边添加的长度和是l,那么一共同拥有C(l+2,2)种,计算出从0到l的全部的方法。
然后计算不能组成三角形的方法,假设最长边>=另外两边之和,那么就不是三角形,所以分别枚举a+i,b+i,c+i为最长边,然后计算有多少种不可能的办法。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#define LL __int64
LL sum[300100] ;
LL solve(int a,int b,int c,int l) {
if( a < b+c ) return (LL)0 ;
LL ans = min(a-b-c,l) ;
return (ans+1)*(ans+2)/2 ;
}
int main() {
int a , b , c , l ;
LL i , ans ;
while( scanf("%d %d %d %d", &a, &b, &c, &l) != EOF ) {
sum[0] = 1 ;
for(i = 1 ; i <= l ; i++) {
sum[i] = (i+1)*(i+2)/2 + sum[i-1] ;
}
ans = sum[l] ;
for(i = 0 ; i <= l ; i++) {
ans -= solve(a+i,b,c,l-i) ;
ans -= solve(b+i,a,c,l-i) ;
ans -= solve(c+i,a,b,l-i) ;
}
printf("%I64d\n", ans) ;
}
return 0 ;
}
codeforces 571A--Lengthening Sticks(组合+容斥)的更多相关文章
- CF 317 A. Lengthening Sticks(容斥+组合数学)
传送门:点我 A. Lengthening Sticks time limit per test 1 second You are given three sticks with po ...
- codeforces 571a//Lengthening Sticks// Codeforces Round #317
题意:三角形3条边,最多共添加ll长,问组成的合法三角形个数. 本来想用暴搜,觉得会超时就搜题解了.不过保证我解释得更清晰. 先计算ll长分配给3条边有几种分法?由于不分也是合法的,因此最后实际分出去 ...
- CodeForces571A. Lengthening Sticks(组合数学-容斥)
题目大意: a,b,c三根木棍可以增加三个不同的数字,aa,bb,cc,且aa+bb+cc<=L,问能构成三角形的木棒有多少种方案 题目思路: 如果我们直接考虑把L分配给aa,bb,cc好像不好 ...
- bzoj4710: [Jsoi2011]分特产 组合+容斥
4710: [Jsoi2011]分特产 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 289 Solved: 198[Submit][Status] ...
- Codeforces Round #258 (Div. 2) 容斥+Lucas
题目链接: http://codeforces.com/problemset/problem/451/E E. Devu and Flowers time limit per test4 second ...
- bzoj3622已经没有什么好害怕的了 dp+组合+容斥(?)
3622: 已经没有什么好害怕的了 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1033 Solved: 480[Submit][Status][ ...
- Codeforces.449D.Jzzhu and Numbers(容斥 高维前缀和)
题目链接 \(Description\) 给定\(n\)个正整数\(a_i\).求有多少个子序列\(a_{i_1},a_{i_2},...,a_{i_k}\),满足\(a_{i_1},a_{i_2}, ...
- BZOJ.4767.两双手(组合 容斥 DP)
题目链接 \(Description\) 棋盘上\((0,0)\)处有一个棋子.棋子只有两种走法,分别对应向量\((A_x,A_y),(B_x,B_y)\).同时棋盘上有\(n\)个障碍点\((x_i ...
- Jzzhu and Numbers CodeForces - 449D (高维前缀和,容斥)
大意: 给定集合a, 求a的按位与和等于0的非空子集数. 首先由容斥可以得到 $ans = \sum \limits_{0\le x <2^{20}} (-1)^{\alpha} f_x$, 其 ...
随机推荐
- open Command window here
http://www.sevenforums.com/tutorials/134831-open-command-window-here-add-remove.html 按照教程里面,下载一个脚本 需 ...
- 通过top 5等待事件查看sql语句
设计的动态性能视图有:v$session_event,v$session,v$sqlarea,首先在v$session_event中可以找到event,然后通过其动态性能视图找到sid,可以在v$se ...
- django URL多层路由
一.多层路由 如果django里的app数量越来越多,那项目里的urls文件配置起来将会很麻烦,而且也不利于后续项目的改动和整理 所以看了杨老师的视频https://www.bilibili.com/ ...
- jQuery动画知识总结
jQuery动画概述 我们之前实现的下拉菜单的案例,是没有动画效果的,但是在日常开发中,动画效果是经常会用到的,所以我们可以尝试使用jQuery动画,将下拉菜单案例实现的更动感一些. jQuery提供 ...
- 读《我是一只 IT 小小鸟》
读<我是一只 IT 小小鸟> 作为一个一向看重节操的体面人,即使面临许多 DDL 包括期中考试,在忙乱不堪的时候我也断不断告诫自己,不能迫于课程要求仅为了写出一篇笔记而去读书,以后更是如此 ...
- hadoop基础学习
MR系类: ①hadoop生态 >MapReduce:分布式处理 >Hdfs:hadoop distribut file system >其他相关框架 ->unstructur ...
- Qt5.2 for Android 配置及部署到手机运行
使用DNK编程也没有那么难,使用QT为安卓跨平台编程需要安装NDK,SDK通过NDK调用C++程序,偶尔能提高一些效率. SDK下载地址:http://developer.android.com/sd ...
- 精通css学习记录
#字体 * 无衬线字体(Sans-serif):Helvetica,Arial,'Lucida Family',Verdana,Tohoma,'Trebuchet MS' * 有衬线字体(Serif ...
- RabbitMQ学习之基于spring-rabbitmq的消息异步发送
spring-rabbitmq的源码到http://github.com/momania/spring-rabbitmq下载,并可以下载实例代码.由于我使用的rabbitmq版本是3.0.4,部分代码 ...
- 编写模块时的声明(含MODULE_LICENSE等)(转)
编写模块必须先声明下面两句: #include <linux/module.h> //这个头文件包含了许多符号与函数的定义,这些符号与函数多与加载模块有关 #i ...