codeforces571A. Lengthening Sticks

传送门:http://codeforces.com/problemset/problem/571/A

思路:直接算方案数比较困难,可以先求出不成立的方案数,再拿总方案数去减.

若增加的总长度为l,根据插板法,方案数就是C(l+2,2),;

对于不成立的方案

只要满足下面3个条件之一即可

a+x+b+y<=c+z

a+x+c+z<=b+y

b+y+c+z<=a+x

而且这三个条件最多只会有一个成立

所以可以把三种方案数加起来

另外还要满足x+y+z<=l

对于第一种,即为x+y<=min(c+z-a-b,l-z)方案数

枚举z即可,其他两种同理

#include<cstdio>
#include<cstring>
#include<algorithm>
typedef long long ll;
using namespace std;
int a,b,c,l;ll ans;

ll f(int a,int b,int c){
	ll res=0;
	for (int i=0;i<=l;i++){
		int t=min(l-i,c+i-a-b);
		if (t>=0) res+=1ll*(t+2)*(t+1)/2;
	}
	return res;
}

int main(){
	scanf("%d%d%d%d",&a,&b,&c,&l);
	for (int i=0;i<=l;i++) ans+=1ll*(i+2)*(i+1)/2;
	ans-=f(c,b,a),ans-=f(b,a,c),ans-=f(c,a,b);
	printf("%I64d
",ans);
	return 0;
}




原文地址:https://www.cnblogs.com/thythy/p/5493514.html