cf Div#317 Lengthening Sticks
2015-08-26 21:24
288 查看
A. Lengthening Sticks
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.
题意:给3跟长度为a、b、c的木条和长度为L的可增加量。可以给任意一根木条增加长度或不加,只要3个增加量的和不超过L,求增加后能使三根木条组成三角形的方法数。
下面为一位朋友的解题思路和代码,思路清晰而且代码简洁。
这道题差不多可以用容斥原理来做。
枚举长度(0-l):
对于每一个长度,如果没有特殊情况,即(i+2)*(i+1)/2个。
但是若(其中一个+L)>((a+b+c+i-1)/2)个,则减去(i-tmp+a+1)*(i-tmp+a)/2,另两个也是一样的。
至于为什么是(i+2)*(i+1)/2个,
如果我们想加上的总长度为i,则:
我们可以对a,b,c分别加上:
0,0,i 0,1,i-1, 0,2,i-2 0,3,i-3 ......
1,0,i-1 1,1,i-2 ......
i,0,0
是个三角状的。
======
=====
====
===
==
=
(形状差不多就是这样的)
所以是(i+2)*(i+1)/2。
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.
题意:给3跟长度为a、b、c的木条和长度为L的可增加量。可以给任意一根木条增加长度或不加,只要3个增加量的和不超过L,求增加后能使三根木条组成三角形的方法数。
下面为一位朋友的解题思路和代码,思路清晰而且代码简洁。
这道题差不多可以用容斥原理来做。
枚举长度(0-l):
对于每一个长度,如果没有特殊情况,即(i+2)*(i+1)/2个。
但是若(其中一个+L)>((a+b+c+i-1)/2)个,则减去(i-tmp+a+1)*(i-tmp+a)/2,另两个也是一样的。
至于为什么是(i+2)*(i+1)/2个,
如果我们想加上的总长度为i,则:
我们可以对a,b,c分别加上:
0,0,i 0,1,i-1, 0,2,i-2 0,3,i-3 ......
1,0,i-1 1,1,i-2 ......
i,0,0
是个三角状的。
======
=====
====
===
==
=
(形状差不多就是这样的)
所以是(i+2)*(i+1)/2。
#include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <vector> #include <stack> #include <map> #include <set> #include <string> #include <cstring> using namespace std; long long a,b,c,l; long long Ans; int main() { scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&l); for(long long i=0;i<=l;i++) { long long tmp=(a+b+c+i-1)/2;//根据三角形的性质a<(c+b) if(a<=tmp&&b<=tmp&&c<=tmp) { Ans+=(i+2)*(i+1)/2; if(a+i>tmp) { Ans-=(i-tmp+a+1)*(i-tmp+a)/2;//减去不不能构成三角形的情况 } if(b+i>tmp) { Ans-=(i-tmp+b+1)*(i-tmp+b)/2; } if(c+i>tmp) { Ans-=(i-tmp+c+1)*(i-tmp+c)/2; } } } printf("%I64d\n",Ans); return 0; }
相关文章推荐
- C++中虚继承派生类构造函数的正确写法
- xcode快捷键
- 跨域资源共享(CORS)安全性浅析[内有提及OPTIONS请求发起的条件]
- Reduce侧联接
- 折叠Collapse插件
- linux 根据sp和fp查调用栈
- html 利用 frameset 进行简单的框架布局
- PHP的curl带验证码模拟登陆学校的教务系统(验证码已被自动识别,不需要输入)
- 02类型转化 进程空间 数组2015/08/26
- 设计模式之策略模式
- 最新制作 Mac 电脑 U 盘启动盘笔记
- javascirpt对象创建
- HDU3342 - Legal or Not 拓补排序模板
- (转)Qt中如何使用样式表QPalette以及相关注意事项
- js获取?后面具体参数的值
- Object -C NSSet -- 笔记
- 创建园子第一天
- EasyUI DataGrid 的常用设置
- 01文件循环位移加密 C预处理宏2015/08/24
- Windows Azure Active Directory (3) China Azure AD增加新用户