Uva 11401 - Triangle Counting 解题报告(计数)
2014-03-25 19:34
295 查看
Problem G
Triangle Counting
Input: Standard Input
Output: Standard Output
You are given n rods of length 1, 2…, n. You have to pick any 3 of them & build a triangle. How many distinct triangles can you make? Note that, two triangles will be considered different if they have at least 1 pair of arms with different
length.
Input
The input for each case will have only a single positive integer n (3<=n<=1000000). The end of input will be indicated by a case with n<3. This case should not be processed.
Output
For each test case, print the number of distinct triangles you can make.
Sample Input Output for Sample Input
解题报告: 分析。假设现在最场边的长度为n。另外两条边的长度为a和b。由三角形的性质可以知道n-a<b<n。我们递推。如果a=1,那么该不等式无解;如果a=2,该不等式有1个解;直到a=n-2,不等式有n-2个解。那么一共有(n-2)(n-1)/2个解。当然,两条边除了相同相同长度,其他长度对都出现了两次,需要去重。相同长度的三角形数量为(n-1)/2。n长度以内的三角形是为最长边为3-n所有的三角形,所以预处理求和。代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1000010;
LL num[maxn];
void init()
{
for(int i=3;i<maxn;i++)
num[i]=num[i-1]+((long long)(i-1)*(i-2)/2-(i-1)/2)/2;
}
int main()
{
init();
int n;
while(~scanf("%d",&n) && n>=3)
printf("%lld\n", num
);
}
Triangle Counting
Input: Standard Input
Output: Standard Output
You are given n rods of length 1, 2…, n. You have to pick any 3 of them & build a triangle. How many distinct triangles can you make? Note that, two triangles will be considered different if they have at least 1 pair of arms with different
length.
Input
The input for each case will have only a single positive integer n (3<=n<=1000000). The end of input will be indicated by a case with n<3. This case should not be processed.
Output
For each test case, print the number of distinct triangles you can make.
Sample Input Output for Sample Input
5 8 0 | 3 22 |
解题报告: 分析。假设现在最场边的长度为n。另外两条边的长度为a和b。由三角形的性质可以知道n-a<b<n。我们递推。如果a=1,那么该不等式无解;如果a=2,该不等式有1个解;直到a=n-2,不等式有n-2个解。那么一共有(n-2)(n-1)/2个解。当然,两条边除了相同相同长度,其他长度对都出现了两次,需要去重。相同长度的三角形数量为(n-1)/2。n长度以内的三角形是为最长边为3-n所有的三角形,所以预处理求和。代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1000010;
LL num[maxn];
void init()
{
for(int i=3;i<maxn;i++)
num[i]=num[i-1]+((long long)(i-1)*(i-2)/2-(i-1)/2)/2;
}
int main()
{
init();
int n;
while(~scanf("%d",&n) && n>=3)
printf("%lld\n", num
);
}
相关文章推荐
- Uva 11038 - How Many O's? 解题报告(计数)
- Uva 1640(计数) 解题报告
- 解题报告_Triangle Counting UVA - 11401_数学规律
- Uva 1510 - Neon Sign 解题报告(计数)
- UVA 12543 Longest Word 解题报告
- 【组合计数dp】UVA 11401 Triangle Counting
- Uva 10079 - Pizza Cutting 解题报告(递推)
- UVA 10878 解题报告
- UVA 755 解题报告
- UVa 1585 - Score - ACM/ICPC Seoul 2005 解题报告 - C语言
- UVa 10305 Ordering Tasks 拓扑排序 解题报告
- 【解题报告】uva10003_Cutting Sticks(切割木棍, dp)
- UVAlive 3890&Poj3525 半平面交+二分 解题报告
- UVA 10047 The Monocycle 解题报告
- 解题报告 之 UVA1605 Building for UN
- UVa 11549 Calculator Conundrum 解题报告(判环)
- UVa 1312 Cricket Field 解题报告(枚举 + 离散化)
- Uva 11361 Investigating Div-Sum Property 解题报告(递推)
- Uva 12050 (POJ 2402) - Palindrome Numbers 解题报告(数学)
- UVA 12537 Radiation 解题报告