Question:
How many different triangles can be formed by an edge?
If the three sides of a triangle are not equal, the same method has two different triangles.
First, it is divided into three types,
1. First, if the three sides are equal, direct the side length (I % 3 = 0) to ans ++.
2. It is easier to find the two sides that are equal.
3. Search for the three sides. I will first give the longest side,
A must be less than or equal to the side length/2,
Find another long side, B = A-1
Then the next shortest edge is c = I-a-B.
It should be clear that there is no need to look for brute force information here. We only need to find the second-length edge B and the shortest side C, which is the biggest difference,
For example, I = 23, A = 11,
So we can see that B C can take, 10, 8,
When I = 24, A = 11
B c: 10 3, 9, 8, 5, 7 6
The result is related to the B-c value, that is
If (B-c) & 1) ans + = 2*(B-c)/2 + 1 );
Else ans + = 2*(B-c)/2 );
Feelings;
A very simple math problem. I always want to do it with brute force. In fact, I think about it and find the rule.
However, I am still using my own ideas. It takes 15 ms to see other people's code more concise on the Internet.
# Include <iostream> # include <cstdio> # include <algorithm> # include <cstring> using namespace STD; int main () {int I, A, B, C, ANS, t, T; scanf ("% d", & T); While (t --) {scanf ("% d", & T, & I ); ans = 0; if (I % 3 = 0) ans ++; // The first if (I & 1) A = 1; // The second else a = 2; for (; A <= (I-1)/2; A + = 2) {B = (I-A)/2; if (! = B) ans ++;} For (A = (I-1)/2; A> 2; A --) // The third {B = A-1; C = I-a-B; If (C> B) continue; If (B-c) & 1) ans + = 2 * (B-c) /2 + 1); else ans + = 2 * (B-c)/2);} printf ("% d \ n", T, ANS );} return 0 ;}