Lengthening Sticks 组合数+容斥原理
来源:互联网 发布:蓝牙串口软件ymodem 编辑:程序博客网 时间:2024/05/17 06:26
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.
Example
Input
1 1 1 2
Output
4
Input
1 2 3 1
Output
2
Input
10 2 1 7
Output
0
思路:用容斥来搞,结果ans=全部组合的情况-不符合三角形定理的情况。
题意:给出a,b,c,L,要求a+x,b+y,c+z构成三角形,x+y+z<=L,问有多少中分法(x,y,z可为0)。
当x+y+z =Li时,若x>=0,y>=0,z>=0,假设dx=x+1,dy=y+1,dz=z+1,则dx+dy+dz=Li+3;
利用插板法,Li+3个球分成三份,Li+2个空插两个板子,有C(Li+2,2)种情况
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
…….. ,
C(m-1,n-1)+C(m-1,n)=C(m,n)
所以由上面可以推出当L=n时,全部的组合情况是C(n+3,3)。
2.不符合三角形定理的情况:
如果要形成一个三角形,那么必须任意两边之和大于第三边。那么不符合的就是任意一边大于等于其余两边的和。所以分别把a,b,c当成第三边,然后再考虑将剩下的l拆分三份分配给a,b,c依旧不满足的情况即可。
我们先把a当成第三边,然后给a增加一个La,现在i=a+La,Max=a+b+c+L。现在我们考虑b+c的范围,因为是不满足的情况,所以b+c的变化范围<=i,又因为总长度Max的限制,b+c<=Max-i,所以b+c的最大变化范围只能在min(i,Max-i)。令x=min(i,Max-i)-b-c表示总共变化量的大小,即Lb+Lc<=x,等价于tmp+Lb+Lc的方案数。
#include<cstdio>#include<cmath>#include<algorithm>using namespace std;typedef long long ll;ll ex(ll a ,ll b,ll c,ll k){ ll ans=0; ll maxx=a+b+c+k; for(ll i=a;i<=a+k;i++) { if(b+c>i) continue; else { ll x=min(i,maxx-i)-b- c; ans+=(x+2)*(x+1)/2; } } return ans;}int main(){ ll a,b,c,k,ans; while(~scanf("%lld%lld%lld%lld",&a,&b,&c,&k)) { ans=(k+3)*(k+2)*(k+1)/6; ans-=ex(a,b,c,k); ans-=ex(b,a,c,k); ans-=ex(c,b,a,k); printf("%lld\n",ans); }}
- Lengthening Sticks 组合数+容斥原理
- Lengthening Sticks(容斥,组合数学)
- Codeforces div2 #317C Lengthening Sticks(组合数学+ 容斥原理)
- codeforces 571A--Lengthening Sticks(组合+容斥)
- Codeforces Round #317 A. Lengthening Sticks(组合+容斥)
- 容斥 + 组合数学 ---Codeforces Round #317 A. Lengthening Sticks
- CF571A - Lengthening Sticks(容斥)
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 2) C Lengthening Sticks(组合+容斥)
- CodeForces #317 (div2) C.Lengthening Sticks (容斥)
- C. Lengthening Sticks(Codeforces Round #317 容斥定理)
- Cheerleaders +容斥原理+组合数+UVA
- CF#317-C. Lengthening Sticks-组合数学
- [组合] Codeforces #571A. Lengthening Sticks
- 求几个数的组合数 容斥原理
- 20160331 UVA11806 Cheerleaders(组合数公式+容斥原理)
- UVA 11806 Cheerleaders(容斥原理)(组合数)
- UVA 11806 Cheerleaders(容斥原理 + 组合数)
- uva 11806 Cheerleaders 容斥原理+组合数
- 分割数组
- MySQL、SqlServer、ORACLE三大数据库常用语句之间的区别整理
- LeetCode 530. Minimum Absolute Difference in BST
- GridControl根据条件改变单元格/行颜色(Dev GridControl 单元格着色)
- 频道管理
- Lengthening Sticks 组合数+容斥原理
- ViewPager
- caffe基础-01数据层的配置
- 字典的基础及字典的使用实例
- HDU6027 Easy Summation 2017中国大学生程序设计竞赛
- UVA
- OpenGL超级宝典visual studio 2013开发环境配置,GLTools
- 1.输入子系统
- opencv1-基础库