题目:
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=22646
1. 因为三角形的三条边均不同,所以不妨令z<y<x
2. 所以,对于固定的x和y,基于三角形不等式z+y>x,z的范围是已知的:
x-y < z < y
3. 记f(x)为最大边为x的符合题意要求的三角形的个数,则:
| y | z的范围(x-y, y) | z的集合 | z的取值个数 |
| x-1 | (1, x-1) | {2,3,..,x-2} | x-3 |
| x-2 | (2, x-2) | {3,4,...,x-3} | x-5 |
| ... | ... | ... | ... |
| x-k (k=floor(x/2)-1) | x-(2*k+1) |
所以, f(x) = [(x-2*k-1)+(x-3)]*k/2
ans(n) = f(3) + f(4) + ... + f(n)
最后,记得数学问题保险起见的话用long long,尤其是对于本题较难推测三角形个数的上限的时候!!!这么做可以免去不必要的麻烦!
#include <cstdio> #include <iostream> #include <assert.h> using namespace std; long long a[1000005]; int main () { int n, cur; for(int i=3; i<=1000005; i++) { cur = i/2-1; a[i] = a[i-1] + (i-3 + i-(2LL*cur+1))*cur/2; } while(scanf("%d", &n) != EOF && n >= 3) { printf("%lld\n", a[n]); } return 0; }
本文探讨了一个涉及三角形性质的问题,通过分析三角形边长的限制来确定符合条件的三角形数量。利用数学推导,作者给出了计算任意长度边界上的三角形数量的公式,并通过代码实现验证了理论结果。
418

被折叠的 条评论
为什么被折叠?



