C. Lengthening Sticks(Codeforces Round #317 容斥定理)
来源:互联网 发布:linux压缩命令 tar 编辑:程序博客网 时间:2024/05/30 04:33
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.
题意:给出a,b,c,L,要求a+x,b+y,c+z构成三角形,x+y+z<=L,问有多少中分法(x,y,z可为0)。
本来想写思路了,奈何岩兄写的太好了,我就不好意思写了,索性就用他的吧。
岩兄博客地址:点击打开链接
思路:用容斥来搞,结果ans=全部组合的情况-不符合三角形定理的情况。
1.求全部组合的情况:
当L=0时,res=1;
当L=1时,res=3;所以当L=1时形成的情况为1+3=4
当L=2时,res=6;所以当L=2时形成的情况为4+6=10
当L=3时,res=10; 所以当L=3时形成的情况为10+10=20
……..
所以由上面可以推出当L=n时,全部的组合情况是C(2,2) + C(3,2) + ......C(l,2)。
2.不符合三角形定理的情况:
如果要形成一个三角形,那么必须任意两边之和大于第三边。那么不符合的就是任意一边大于等于其余两边的和。所以分别把a,b,c当成第三边,然后再考虑将剩下的l拆分三份分配给a,b,c依旧不满足的情况即可。
我们先把a当成第三边,然后给a增加一个La,现在i=a+La,Max=a+b+c+L。现在我们考虑b+c的范围,因为是不满足的情况,所以b+c的变化范围<=i,又因为总长度maxn的限制,b+c<=maxn-i,所以b+c的最大变化范围只能在min(i,maxn-i)。令x=min(i,maxn-i)-b-c表示总共变化量的大小,即Lb+Lc<=x,等价于tmp+Lb+Lc的方案数。
#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#include<vector>#include<queue>#include<stack>#include<map>using namespace std;long long int minn(long long int xx,long long int yy){ if(xx<yy){ return xx; } return yy;}long long int IEP(long long int a,long long int b,long long int c,long long int l){ long long int s = 0; long long int maxn = a + b + c + l; for(long long int i=a;i<=a+l;i++){ if(b+c<=i){ long long int xx = min(i,maxn-i)-b-c; s += (xx+1)*(xx+2)/2; } } return s;}int main(){ __int64 a,b,c,l; __int64 ans = 0; while(scanf("%lld%lld%lld%lld",&a,&b,&c,&l)!=EOF){ ans = 0; for(__int64 i=0;i<=l;i++){ ans += (i+1)*(i+2)/2; } ans -= IEP(a,b,c,l); ans -= IEP(b,a,c,l); ans -= IEP(c,a,b,l); printf("%lld\n",ans); } return 0;}
- C. Lengthening Sticks(Codeforces Round #317 容斥定理)
- Codeforces Round #317 A. Lengthening Sticks(组合+容斥)
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 2) C Lengthening Sticks(组合+容斥)
- CodeForces #317 (div2) C.Lengthening Sticks (容斥)
- 容斥 + 组合数学 ---Codeforces Round #317 A. Lengthening Sticks
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 2) C. Lengthening Sticks(数学)
- Codeforces div2 #317C Lengthening Sticks(组合数学+ 容斥原理)
- Codeforces Round #317 A.Lengthening Sticks
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 2) C. Lengthening Sticks
- codeforces 571A--Lengthening Sticks(组合+容斥)
- CF571A - Lengthening Sticks(容斥)
- 【codeforces 572C】Lengthening Sticks
- codeforces 317 A - Lengthening Sticks
- codeforces #317 C. Lengthening Sticks (很好的想法题)
- Lengthening Sticks 组合数+容斥原理
- Lengthening Sticks(容斥,组合数学)
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 1) A. Lengthening Sticks 分类
- Lengthening Sticks CodeForces
- Android第四讲——常用的基本控件(一)TextView、EditText、Button、RadioButton、CheckButton、ImageView
- IOS真机运行环境操作步骤
- 既然没有人来说出真相,那就只有我来了
- 2015/8/24/checkBox/imageView/ArrayAdapter/SimpleAdapter/自定义Adapter
- 生活的触动,从点滴开始!
- C. Lengthening Sticks(Codeforces Round #317 容斥定理)
- 【20150801-20150808】趣题详解
- 上证指数明天跌破3000点可能性很大,感觉2800或者2600可能是阶段性底部
- ZOJ 3883Scan Code 模拟
- 回调函数
- Introduction to Java Programming编程题9.4<求某个字符的出现次数>
- Can you solve this equation?--二分
- JFreeChart的基本使用
- 算菜价(输入输出)