codeforces 571 A Lengthening Sticks
2015-08-23 11:45
399 查看
题意:给出a,b,c,l要求a+x,b+y,c+z构成三角形,x+y+z<=l,成立的x,y,z有多少种。
做法:很显然可以容斥,首先考虑总的情况,也就是x+y+z<=l有多少种解,就相当于求所有i(i<=l)满足x+y+z=i有多少种解,也就是组合数C(i+2,2),为啥?因为相当于刻度从1开始然后长度为i的尺子拆成三份有多少种情况,也就是在不同的位置切两刀,然后考虑,可以有最多两份长度为0,也就是说在0到1之间可以切两刀于是总共就是i+2个可以切的位置中选择两个。
接下来容斥,将不符能构成三角形的排除,三角形满足的条件是什么?任意两边之和大于第三边,那么不满足的必要条件就是第三边大于等于其它两边之和,枚举a,b,c分别做第三边的情况的时候,再考虑将剩下的l拆分三份分配给a,b,c依旧不满足的情况即可。这里看不懂的,看看代码就懂了。
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.
Input
The single line contains 4 integers a, b, c, l (1 ≤ a, b, c ≤ 3·105, 0 ≤ l ≤ 3·105).
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.
Sample test(s)
input
output
input
output
input
output
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.
做法:很显然可以容斥,首先考虑总的情况,也就是x+y+z<=l有多少种解,就相当于求所有i(i<=l)满足x+y+z=i有多少种解,也就是组合数C(i+2,2),为啥?因为相当于刻度从1开始然后长度为i的尺子拆成三份有多少种情况,也就是在不同的位置切两刀,然后考虑,可以有最多两份长度为0,也就是说在0到1之间可以切两刀于是总共就是i+2个可以切的位置中选择两个。
接下来容斥,将不符能构成三角形的排除,三角形满足的条件是什么?任意两边之和大于第三边,那么不满足的必要条件就是第三边大于等于其它两边之和,枚举a,b,c分别做第三边的情况的时候,再考虑将剩下的l拆分三份分配给a,b,c依旧不满足的情况即可。这里看不懂的,看看代码就懂了。
#include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<iostream> #include<algorithm> #include<bitset> #include<climits> #include<list> #include<iomanip> #include<stack> #include<set> using namespace std; typedef long long ll; ll work(int a,int b,int c,int l) { ll ans=0; for(int i=max(b+c-a,0);i<=l;i++) { ll x=min(l-i,a+i-b-c); ans+=(1+x)*(2+x)/2; } return ans; } int main() { int a,b,c,l; cin>>a>>b>>c>>l; ll ans=0; for(int i=0;i<=l;i++) ans+=ll(1+i)*(2+i)/2; ans-=work(a,b,c,l); ans-=work(b,a,c,l); ans-=work(c,a,b,l); cout<<ans; }
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.
Input
The single line contains 4 integers a, b, c, l (1 ≤ a, b, c ≤ 3·105, 0 ≤ l ≤ 3·105).
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.
Sample test(s)
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.
相关文章推荐
- 淘宝网采用什么技术架构来实现网站高负载分析
- 树状数组
- 欧几里得算法与扩展欧几里得算法
- AngularJS中文入门教程
- 问答题
- 分布式全文检索系统SolrCloud简介
- hdu5418Victor and World(floyd算法+状压dp)
- Codeforces Round #270(利用prim算法)
- VS2015环境配置: VS2015 未能正确加载“ResourceManagerPackage”包的问题
- Scala学习笔记(五):类和对象
- 个人的一点总结
- 258-e-Add Digits
- 阿里校招-java研发 题目
- SQLite学习笔记(7)-源代码整理
- OracleHelper(for produce)
- android获得圆角图片
- 一致性hash和solr千万级数据分布式搜索引擎中的应用
- poj1840
- iOS 多线程NSThread-NSOperation-GCD
- 论攻击Web应用的常见技术