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 ;}