UVa 11401 Triangle Counting: Combination count

Source: Internet
Author: User
Tags time limit

11401-triangle counting

Time limit:1.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=469&page=show_ problem&problem=2396

You are given n rods of length 1, 2 ..., N. You are have to pick any 3 of the them & build a triangle. How many distinct triangles can your make? Note that, two triangles'll be considered different if they have in least 1 pair of arms with different length.

Input

The input for each case would have only a single positive integer n (3<=n<=1000000). The end of input is indicated by a case with n<3. This case should is processed.

Output

This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45374.htm

For the all test case, print the number of distinct triangles you can make.

Sample input Output for sample input

5
8
0
3
22

First of all, when the maximum edge for x of the number of matches, and then the recurrence of the cumulative can be. (Because the problem is to calculate the number of sides of the length of no more than N)

Complete code:

/*0.029s*/
    
#include <cstdio>  
    
long long f[1000010];  
    
int main ()  
{  
    f[3] = 0;  
    For (long long x = 4; x <= 1000000 ++x)  
        f[x] = f[x-1] + ((x-1) * (x-2)/2-(X-1)/2)/2;  
    int n;  
    while (scanf ("%d", &n), n >= 3)  
        printf ("%lld\n", F[n]);  
    return 0;  
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.