UVA 11401 Triangle Counting
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
58
0 |
322 |
思路:列出不等式进行优化
[codesyntax lang=”c++”]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include #include using namespace std; unsigned long long f[1000010],i,n; int main() { f[3]=0; for(i=4;i<=1000000;i++) f[i]=f[i-1]+((i-1)*(i-2)/2-(i-1)/2)/2; while(cin>>n&&n>=3) { cout<<f[n]<<endl; }="" return="" 0;="" }<="" pre="">[/codesyntax] <a href="http://xiaoshig-wordpress.stor.sinaapp.com/uploads/2014/07/猫.png"><img class="alignnone size-medium wp-image-97" src="http://xiaoshig-wordpress.stor.sinaapp.com/uploads/2014/07/猫-300x300.png" alt="猫" width="300" height="300" /></a> |