统计用大小 1到n的那条边能组成多少个三角形。
利用y+z>x转化为不等式 x-y<z<x 枚举x。当x >3时一共有(x-1)(x-2)/2种方法。其中有y=z这种不合法的情况。(因为每条边只能用一次),所以要减去重复的(x-1)(x-2)/2
Time Limit: 1000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
![Download as PDF Download as PDF](https://i-blog.csdnimg.cn/blog_migrate/2ea975a8d421ce3226c46b0946fd5932.png)
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 withn<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 |
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
#define MAXN 1000100
long long f[MAXN];
int main()
{
f[3]=0;
for(long long i=4;i<=1000000;i++)
f[i]=f[i-1]+((i-1)*(i-2)/2-(i-1)/2)/2;
int n;
while(~scanf("%d",&n)&&n>=3)
printf("%lld\n",f[n]);
return 0;
}