UV 11401-Triangle Counting)

Source: Internet
Author: User

Question: from 1 ~ 3 integers are selected from n so that they can form a triangle on the three sides. Given an n, how many different triangles can they form? Question: n can reach a maximum of 1000000, so it can only be solved by O (n. The number of triangles whose maximum edge is x is C (x), y + z> x, x-y <z <x. If y = 1, z is unsolvable, y = 2, z one solution ...... Y = X-1, z has a X-2 solution so 0 + 1 + 2 + ...... + X-2 = (x-1) (X-2)/2, but there is a case of y = z, there are x/2-1. And each triangle in it repeats twice, so c (x) = (x-1) * (X-2)/2-(x/2-1)/2, f [x] = f [x-1] + (x-1) * (X-2)/2-(x/2-1)/2. AC code:

# Include <iostream> # include <cstdio> # include <cstring> # include <string> # include <cstdlib> # include <cmath> # include <vector> # include <list> # include <deque> # include <queue> # include <iterator> # include <stack> # include <map> # include <set> # include <algorithm> # include <cctype> using namespace std; # define si1 (a) scanf ("% d", & a) # define I2 (a, B) scanf ("% d", & a, & B) # define sd1 (a) scanf ("% lf", & a) # define sd2 (a, B) scanf ("% lf", & a, & B) # define ss1 (s) scanf ("% s", s) # define pi1 (a) printf ("% d \ n", a) # define pi2 (a, B) printf ("% d \ n", a, B) # define mset (a, B) memset (a, B, sizeof (a) # define forb (I, a, B) for (int I = a; I <B; I ++) # define ford (I, a, B) for (int I =; I <= B; I ++) typedef long LL; const int N = 1100001; const int M = 6666666; const int INF = 0x3f3f3f; const double PI = acos (-1.0); const double eps = 1e-7; LL f [N]; int main () {f [3] = 0; for (LL x = 4; x <= 1000000; x ++) // This place should note that x should be longlong f [x] = f [x-1] + (x-1) * (X-2)/2-(x/2-1)/2; int n; while (si1 (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.