Codeforces #317 C.Lengthening Sticks(数学)
C. 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分成3份有多少种方法呢?
有l个点,那么就有l + 1个空位,你可以在其中选择3个插入,是不是很像组合数,但是你选择的这三个空位是可以重复的,所以我们再增加两个空位,用这两个空位代表重复的情况,可以理解为选择了这个特殊空位就和选了上一个非特殊空位一样,所以如果两个特殊空位都选,那就代表了插入位置的重复。
插入的位置把l分成了3段,那么从l + 1 + 2中选3个 (C(l + 3)(3))即为总个数
公式为(l + 1) * (l + 2) * (l + 3) / 6
如果这样说不能理解,那么换一个方式,如果把第一个断点放在第一个空位上,
把第二个断点放在第一个空位上,那么第三个断点可以选择l+1个; 把第二个断点放在第二个空位上,第三个断点可以选l个
第一个断点位置\第二个断点位置 | 1 | 2 | 3 | 4 | 5 | 6 | L | L + 1 | |||
1 | L + 1 | L | L - 1 | L - 2 | L - 3 | L - 4 | ... | 3 | 2 | 1 | |
2 | L | L - 1 | L - 2 | L - 3 | L - 4 | ... | 3 | 2 | 1 | ||
3 | L - 1 | L - 2 | L - 3 | L - 4 | ... | 3 | 2 | 1 | |||
4 | L - 2 | L - 3 | L - 4 | ... | 3 | 2 | 1 | ||||
5 | L - 3 | L - 4 | ... | 3 | 2 | 1 | |||||
6 | L - 5 | ... | 3 | 2 | 1 | ||||||
... | 3 | 2 | 1 | ||||||||
L | 3 | 2 | 1 | ||||||||
L + 1 | 1 |
将表中数据求和,需要用到1 ^ 2 + 2 ^ 2 + ... + n ^ 2
处理出总情况之后
枚举增加的长度,假如 a + i(增加长度) <= b + c
那么就把不符合情况的删掉,a分配了i, b + c最多为l - i, 又要是不合法的情况所以b + c 一定要比a + i小于等于,所以增加的也不能超过a + i - b - c;
然后就是把这个 0到这个最小值 分别分配给其余两数 分配n时, 方案为n + 1;
所以总方案为(min + 1) * (min + 2) / 2;
#include <stdio.h>
#include <algorithm>
using namespace std;
long long solve(long long x) { //这里的long long返回不要忘了
return (x + ) * (x + ) >> ;
}
int main() {
long long a, b, c, l;
scanf("%I64d%I64d%I64d%I64d", &a, &b, &c, &l);
long long nr = (l + ) * (l + ) * (l + ) / ;
for (long long i = ; i <= l; i++) {
if (a + i >= b + c) nr -= solve(min(l - i, a + i - b - c));
if (b + i >= a + c) nr -= solve(min(l - i, b + i - a - c));
if (c + i >= a + b) nr -= solve(min(l - i, c + i - a - b));
}
printf("%I64d\n", nr);
return ;
}
Codeforces #317 C.Lengthening Sticks(数学)的更多相关文章
- 容斥 + 组合数学 ---Codeforces Round #317 A. Lengthening Sticks
Lengthening Sticks Problem's Link: http://codeforces.com/contest/571/problem/A Mean: 给出a,b,c,l,要求a+x ...
- CF 317 A. Lengthening Sticks(容斥+组合数学)
传送门:点我 A. Lengthening Sticks time limit per test 1 second You are given three sticks with po ...
- codeforces 572 C. Lengthening Sticks(数学)
题目链接:http://codeforces.com/contest/572/problem/C 题意:给出a,b,c,l要求a+x,b+y,c+z构成三角形,x+y+z<=l,成立的x,y,z ...
- [codeforces 317]A. Perfect Pair
[codeforces 317]A. Perfect Pair 试题描述 Let us call a pair of integer numbers m-perfect, if at least on ...
- Codeforces Round #317 (Div. 2) C Lengthening Sticks (组合,数学)
一个合法的三角形的充要条件是a<b+c,其中a为最长的一边,可以考虑找出所有不满足的情况然后用总方案减去不合法的情况. 对于一个给定的总长度tl(一定要分完,因为是枚举tl,不分配的长度已经考虑 ...
- codeforces 571a//Lengthening Sticks// Codeforces Round #317
题意:三角形3条边,最多共添加ll长,问组成的合法三角形个数. 本来想用暴搜,觉得会超时就搜题解了.不过保证我解释得更清晰. 先计算ll长分配给3条边有几种分法?由于不分也是合法的,因此最后实际分出去 ...
- [Codeforces 1178D]Prime Graph (思维+数学)
Codeforces 1178D (思维+数学) 题面 给出正整数n(不一定是质数),构造一个边数为质数的无向连通图(无自环重边),且图的每个节点的度数为质数 分析 我们先构造一个环,每个点的度数都是 ...
- hdu.5203.Rikka with wood sticks(数学推导:一条长度为L的线段经分割后可以构成几种三角形)
Rikka with wood sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- Codeforces 627 A. XOR Equation (数学)
题目链接:http://codeforces.com/problemset/problem/627/A 题意: 告诉你s 和 x,a + b = s a xor b = x a, b > ...
随机推荐
- UVALive 2053 Puzzlestan(深搜+技巧)
这个题目的深搜形式,我也找出来了,dfs(i,j)表示第i个人选到了第j个物品,但是我却无限RE了,原因是我的viod型深搜太过暴力,我当时定义了一个计数器,来记录并限制递归的层数,发现它已经递归到了 ...
- Discuz登录慢、退出也慢的原因?
Discuz登录慢.退出也慢的原因? 2009-02-21 12:50:11 分类: 转载自:http://www.aiseminar.cn/bbs/thread-201-1-1.html 由于服务 ...
- php socket客户端及服务器端应用实例
经常有朋友会对php的socket应用充满疑惑,本文就以实例代码作一讲解,希望能对初学php的朋友起到一点帮助作用 具体代码如下: 1.服务器端代码: <?php class SocketSer ...
- jquery选择器 之 获取父级元素、同级元素、子元素(转)
一.获取父级元素 1. parent([expr]): 获取指定元素的所有父级元素 <div id="par_div"><a id="href_fir& ...
- 尝试在数据库 5 中提取逻辑页 (1:1640) 失败。该逻辑页属于分配单元XXX ,而非 XXX。
此信息表明数据库或表 已经部分损坏可以通过以下步骤尝试修复: 1. DBCC CHECKDB 重启服务器后,在没有进行任何操作的情况下,在SQL查询分析器中执行以下SQL进行数据库的修复,修复数据库存 ...
- git:解决The current branch is not configured for pull No value for key branch.master.merge found in config
网上多半都是命令行下的解决方案,我用的是EGit,所以要在eclipse里(我的版本是kepler)把下面这句话添加到配置文件中. Window->Preference->Team-> ...
- MDK的优化应用(转)
源:http://blog.163.com/zhaojun_xf/blog/static/300505802011291384721/ 使用Keil/MDK这么多年了,一直都没有使用它的代码优化功能. ...
- 自己用wireshark 抓了个包,分析了一下
我自把对应 ip 包的头部拿出来手动分析了一下 :)
- js判断是否是正整数,js判断是否是数字
//判断字符串是否为数字 function checkRate(input) { var re = /^[0-9]+.?[0-9]*$/; if (!re.test(input.rate.value) ...
- ARM汇编指令的一些总结-转
ARM汇编指令的一些总结ARM汇编指令很多,但是真正常用的不是很多,而且需要认真琢磨的又更少了.比较有用的是MOV B BL LDR STR还是通过具体汇编代码来学习吧.@ disable watch ...