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$, 其 ...
随机推荐
- Asura监控---AsuraMonitor,阿修罗监控开源
阿修罗Monitor是一个功能强大.灵活的监控系统. 系统安装简单,配置简单,相比zabbix, nagios,cacti,小米监控等都使用相当简单.只需要会写脚本,语言不限就可以实现任意监控需求. ...
- php获取前天的昨天的日期
在PHP里得到前天和昨天的日期的代码前天去面试的时候也是这样,不过我当时记不起来了.就记得MYSQL里面的date_sub(now(),'interval 1 day');date('Y/m/d h: ...
- kindoreditor上传图片
<!doctype html><html> <head> <meta charset="utf-8" /> <title> ...
- 谈谈cookie的弊端
一.cookie虽然在数据在客户端持久存储提供了方便,但是分担了服务器数据传输的负担,还是存在很大的局限性的. 局限性: (1)在特定的域名下最多存储20个cookie. 浏览器版本 ...
- ettercap + driftnet 实现同网段下流量欺骗
前言: 由于在局域网中,网关会不断地发送 ARP 数据包询问当前是否有新的客户端上线,如果我们可以欺骗当前局域网网段下的主机, 把我们当成网关地址,并且我们把欺骗的流量转发到真正的网关地址,这样我们就 ...
- 对JVM还有什么不懂的?一文章带你深入浅出JVM!
本文跟大家聊聊JVM的内部结构,从组件中的多线程处理,JVM系统线程,局部变量数组等方面进行解析 JVM JVM = 类加载器(classloader) + 执行引擎(execution engine ...
- golang iris html/temple
在使用golang的模板语法的过程中遇见自动转义问题(或者以我的理解下发的富文本html代码不是template.html类型,而是string类型),需要强制转型 func unescaped(x ...
- 使用NPOI实现简单的Excel导出功能
[1]NPOI是啥? NPOI是指构建在POI 3.x版本之上的一个程序,NPOI可以在没有安装Office的情况下对Word或Excel文档进行读写操作. POI是一个开源的Java读写Excel. ...
- WinForm上传文件,下载文件
上传文件: 使用OpenFileDialog控件选择文件, 具体代码示例: private void btnUpLoadPic_Click(object sender, EventArgs e) { ...
- css网页布局方式的理解
一,标准流(默认状态,元素盒按照文档中出现的顺序排列) 块级元素--垂直排版 display:block; 单独一行,可以设置宽高,宽度默认和父元素宽度一致 一般结构性标记都为块级元素,如div,h, ...