Triangle Counting
时间限制:
1000 ms | 内存限制:
65535 KB
难度:
3
-
描述
-
You are given n rods of length 1, 2…, n. You have to pick any 3 of them and 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.
-
输入
- The input for each case will have only a single positive integer n(1<=n<=1000000). The end of input will be indicated by a case with n<1. This case should not be processed. 输出
- For each test case, print the number of distinct triangles you can make. 样例输入
-
5 8 0
样例输出
-
3 22
来源
- UVA 上传者
思路详解:http://blog.csdn.net/lyhvoyage/article/details/21692527
ac代码
#include<stdio.h> #include<string.h> long long ans[1000005]; void fun() { long long i; ans[1]=ans[2]=ans[3]=0; for(i=4;i<=1000000;i++) ans[i]=ans[i-1]+((i-1)*(i-2)/2-(i-1)/2)/2; } int main() { int n; fun(); while(scanf("%d",&n)!=EOF) { if(n<1)break; printf("%lld\n",ans[n]); } }