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$, 其 ...
随机推荐
- Swift3.0 split函数切割字符串
我们先看函数的原型: public func split(separator: Self.Iterator.Element, maxSplits: Int = default, omittingEmp ...
- Dragon Ball--hdoj
Dragon Ball Problem Description Five hundred years later, the number of dragon balls will increase u ...
- golang 初体验
1.下载golang https://code.google.com/p/go/downloads/list 在windows下安装,下载windows32版本 2.安装 安装完毕,默认在C:\Go ...
- BZOJ 1443 二分图博弈 网络流
思路: 二分图博弈嘛 找到最大匹配的必须点 跑个网络流 前后DFS一遍 //By SiriusRen #include <queue> #include <cstdio> #i ...
- .net web api跨域问题
No 'Access-Control-Allow-Origin' Ajax跨域访问解决方案 No 'Access-Control-Allow-Origin' header is present o ...
- ansible upload
# 链接地址:https://www.cnblogs.com/xiaoxiaoleo/p/6626299.html # synchronize: 从拉取远程服务器文件,需要加mode: pull # ...
- 【Oracle】查询当前SCN
介绍两种方式: 一.sys用户下: select current_scn from v$database; select dbms_flashback.get_system_change_number ...
- mysql Seconds_Behind_Master
通过show slave status查看到的Seconds_Behind_Master,从字面上来看,他是slave落后master的秒数,一般情况下,也确实这样,通过Seconds_Behind_ ...
- bootstrap初用新得1
## 基本准备 1. 首先把相关软件窗口规划好,对于我的喜好,我喜欢把除了浏览器外的其他软件分为左右两个半屏.左边和右边很多软件之间是需要配合使用的: * 左边: scss文件,ps的guid ...
- PHP 时间处理
1:获取当前日期格式时间 date("Y-m-d H:i:s"); 2:转化为时间戳 strtotime( date("Y-m-d") ) 3:转化为日期 ...