uva 11401 数三角形

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 (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

/*
设最大边长为x的三角形有c(x)个,跟三角形的定义两边之和大于第三边有x<y+z
变形下的x-y<z<x;当y=1时无解,当y=2时只有一个解z=x-1,知道y=x-1时又x-2个解
,所以共有(x-1)(x-2)/2个解,由于题意中不能存在y=z的解所以y=z这部分解,
当x/2+1至x-1才存在y=z的可能,共有(x-1)/2个.还过有过程中每种三角形算了两遍
所以c(x)=((x-1)(x-2)/2-(x-1)/2)/2);
f(n)=c(1)+c(2)+.....+c(n);
*/
#include<iostream>
#include<cstdio>
using namespace std;
__int64 f[1000010];

void Init()
{
    __int64 i;//用int定义结果Wrong answer,不定义__int64计算过程中会溢出
    f[1]=0;
    f[2]=0;
    f[3]=0;
    for(i=4;i<=1000000;i++)
        f[i]=f[i-1]+((i-1)*(i-2)/2-(i-1)/2)/2;
}

int main()
{
    Init();
    int n;
    while(cin>>n,n>=3)
        printf("%I64d
",f[n]);
    return 0;
}
 
原文地址:https://www.cnblogs.com/xiong-/p/3209303.html