UVA Triangle Counting 11401 "geometry + math"

Source: Internet
Author: User

11401-triangle countingtime limit:1.000 seconds



Test instructions: give you n a line, length 1-n. Ask how many different triangles can be formed.

Problem Solving Ideas:

The triangles with the largest edge length x have C (x), the other two sides are Y and Z respectively, and have y+z>x according to the triangular inequalities. So Z's range is x-y < Z < x.

① According to this inequality, when Y=1 x-1 < Z < x, there is no solution, y=2 there is a solution (z=x-1), y=3, there are two solutions (z=x-1 or z=x-2) ... Until Y=x-1, there is a x-2 solution. According to the arithmetic progression summation formula, there are altogether 0+1+2+......+ (x-3) + (x-2) = (x-1) (x-2)/2 solutions.

The ② above contains the Y=z case, and each triangle is two times. So we should count the y=z of the situation. When y = Z is x-z < Z < x, then there is x < 2z, X/2 < Z < x, so the number of Z (x-1) is met (x/2+1)

Then subtract the solutions and divide by 2.

Why does ③ calculate both sides? You can push the x=5 y case starting from 1. You will see that the triangles are repeated two times.

C (x) = ((i-2) * (i-1)/2-((i-1)-(i/2+1))/2;; The original question required "the number of triangles with a maximum edge length of not more than n" F (n) ", then f (n) =c (1) +c (2) +...+c (n). Written recursively is f (n) = f (n-1) + C (n).


AC Code:

#include <stdio.h> #include <math.h> #include <vector> #include <queue> #include <string> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm>using Namespace Std;typedef long Long ll;const int maxn = 1000002; LL F[maxn];int Main () {    f[3]=0;    for (LL i=4;i<maxn;i++)        f[i]=f[i-1]+ ((i-2) * (i-1)/2-((i-1)-(i/2+1))/2;    int n;    while (scanf ("%d", &n)} {        if (n<3) break;        printf ("%lld\n", F[n]);    }    return 0;}


Copyright NOTICE: This article is the original blogger articles, reproduced please indicate the source.

UVA Triangle Counting 11401 "geometry + math"

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.