CF 317 A. Lengthening Sticks(容斥+组合数学)
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·10^5, 0 ≤ l ≤ 3·10^5).
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。可以把长度 len(len<= L)拆成三份后,添加到a,,b,c任意一边。问能构成的三角形的个数。(a=2,b=2,c=3 和 a=3,b=2,c=2是两种不同的情况)
比如说样例2,1 2 3 1。可以把1加到a上 变成2 2 3,可以把1加到b上 变成1 3 3。所以答案输出2。
思路:
全部情况 - 不构成三角形的情况 = 构成三角形的情况
全部情况:
对于长度len,考虑切2刀分成3部分,因为可以是切在0这个位置上,即可以出现0,0,len这种情况,所以情况一共有C(len+2,2),表示把长度为len的线段,取2个点切开,然后把这三段分配给a,b,c的所有情况。(包括可构成三角形和不可构成三角形)
不构成三角形的情况:
一个三角形三边分别为a,b,c,如果a>=b+c,则构不成三角形。
首先考虑最长边是a,对于要加的len,分成p和len-p两部分,其中p给a,len-p给b和c。可以确定的是如果a>=b+c,那么p最少可以是0,如果b+c>a,那么p起码得是b+c-a。这样才能满足即使len-p是0的情况下,让a+p>=b+c。
所以枚举p的起点是max(b+c-a,0)。之后考虑len-p分配给b和c的情况有几种。
因为要构成a+p>=b+c+len-p。所以最大可增加的长度是min(len-p,a+p-b-c),(解释一下,len-p是给了a之后剩下来的,a+p-(b+c)是本身要满足a+p>=b+c,最多允许a+p==b+c,所以对这两个值取最小值就是b和c最大可以增加的长度)
记这个最大可以增加的长度为max_addlen,分配给b和c的情况一共有 (max_addlen+1)*(max_addlen+2)/2 种。
这个很容易证明,在max_len中间砍两刀分为3部分,其中2部分给b和c。情况依旧是C(max_addlen+2,2)
所以去枚举增加长度就可以得到构不成三角形的全部情况了。具体看代码,同时注意用long long。
代码:
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
#define MMINF(x) memset(x,INF,sizeof(x))
#define LL long long
using namespace std;
LL l;
LL solve(LL a,LL b,LL c)//枚举a为最长边时候所有构不成三角形的情况
{
LL ans = ;
for(LL i = max(b+c-a,0LL) ; i <= l ; i ++) //起点是max(b+c-a,0LL)
{
LL max_addlen = min(a+i-b-c,l-i);//给b和c 最大可以增加的长度
ans += (max_addlen+)*(max_addlen+)/;
}
return ans;
}
int main()
{
LL a,b,c ;
cin>>a>>b>>c>>l;
LL ans = ;
for(LL i = ; i <= l ; i ++){
ans += (i+)*(i+)/;
}//可增加的所有情况
ans -= solve(a,b,c);
ans -= solve(b,a,c);
ans -= solve(c,a,b);
//分别枚举a,b,c是最长边的情况
cout<<ans<<endl;
}
/*
10 2 1 7
1 2 3 1
*/
CF 317 A. Lengthening Sticks(容斥+组合数学)的更多相关文章
- 2015 asia xian regional F Color (容斥 + 组合数学)
2015 asia xian regional F Color (容斥 + 组合数学) 题目链接http://codeforces.com/gym/100548/attachments Descrip ...
- 容斥 + 组合数学 ---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#305 Mike and Foam(容斥)
C. Mike and Foam time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces #317 C.Lengthening Sticks(数学)
C. Lengthening Sticks time limit per test 1 second memory limit per test 256 megabytes input standar ...
- BZOJ2839:集合计数(容斥,组合数学)
Description 一个有N个元素的集合有2^N个不同子集(包含空集),现在要在这2^N个集合中取出若干集合(至少一个),使得它们的交集的元素个数为K,求取法的方案数,答案模1000000007. ...
- 【BZOJ4559】[JLoi2016]成绩比较 动态规划+容斥+组合数学
[BZOJ4559][JLoi2016]成绩比较 Description G系共有n位同学,M门必修课.这N位同学的编号为0到N-1的整数,其中B神的编号为0号.这M门必修课编号为0到M-1的整数.一 ...
- Gym 100548F Color 给花染色 容斥+组合数学+逆元 铜牌题
Problem F. ColorDescriptionRecently, Mr. Big recieved n flowers from his fans. He wants to recolor th ...
- [CTS2019]随机立方体(容斥+组合数学)
这题七次方做法显然,但由于我太菜了,想了一会发现也就只会这么多,而且别的毫无头绪.发现直接做不行,那么,容斥! f[i]为至少i个极值的方案,然后这里需要一些辅助变量,a[i]表示选出i个三维坐标均不 ...
- 51nod1667-概率好题【容斥,组合数学】
正题 题目链接:http://www.51nod.com/Challenge/Problem.html#problemId=1667 题目大意 两个人. 第一个人有\(k_1\)个集合,第\(i\)个 ...
随机推荐
- webpack的知识内容
webpack的构建工具: 浏览器的兼容性? 转换ES6语法: 转化JSX: css
- 学习笔记TF045:人工智能、深度学习、TensorFlow、比赛、公司
人工智能,用计算机实现人类智能.机器通过大量训练数据训练,程序不断自我学习.修正训练模型.模型本质,一堆参数,描述业务特点.机器学习和深度学习(结合深度神经网络). 传统计算机器下棋,贪婪算法,Alp ...
- 关于PostmanURL中不能传递中文的问题
众所周知,中文乱码等问题在开发过程中让人厌烦,本人最近在使用Postman插件测试web的时候,在请求中添加了中文,结果请求错误 截图如下: 原因:不能直接处理中文,需要自行转化 解决: 1.新建一个 ...
- xamarin C# 安卓实现 ListView 放大缩小
翻译自java示例https://raw.githubusercontent.com/Xjasz/AndroidZoomableViewGroup/master/ZoomListView.java u ...
- PythonStudy——赋值运算符 Assignment operator
eg: num = 10 num += 1 # 等价于 num = num + 1 => 11 print(num) 特殊操作: 1.链式赋值 a = b = num print(a, b, n ...
- fges
基本用法: fgets函数用来从文件中读入字符串.fgets函数的调用形式如下:fgets(str,n,fp):此处,fp是文件指针:str是存放在字符串的起始地址:n是一个int类型变量.函数的功能 ...
- C# 数据库
连接: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...
- C现代编程
1.C语言没有像面向对象语言一样提供访问控制的功能,无法实现数据隐藏,可以通过规定成员命名来规避这个问题,例如不允许直接访问的成员以“_”开头. 2.模板模式,利用函数指针,抽离固有代码,差异代码放到 ...
- PHP 获取数组是几维数组
// 判断数组是几维数组$data = array(); // 是你要判断的数组$al = array(0);function aL($data,&$al,$level=0){ if(is_a ...
- CSS之form&span
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...