Problem F codeforces round #183 (Div. 2) A. pythagorean theorem II

Source: Internet
Author: User
A. pythagorean theorem iitime limit per test

3 seconds

Memory limit per test

256 megabytes

Input

Standard Input

Output

Standard output

In mathematics, the Pythagorean theorem-is a relation in Euclidean Geometry among the three sides of a right-angled triangle. In terms of areas, it states:

In any right-angled triangle, the area of the square whose side is the hypotenuse (the side opposite the right angle) is equal to the sum of the areas of the squares whose sides are the two legs (
Two sides that meet at a right angle ).

The theorem can be written as an equation relating the lengths of the sidesA,BAndC,
Often called the Pythagorean equation:

A 2 cores + CoresB2 bytes = bytesC2

WhereCRepresents the length of the hypotenuse, andAAndBRepresent
The lengths of the other two sides.

GivenN, Your task is to count how many right-angled triangles with side-lengthsA,BAndCThat
Satisfied an inequality 1 limit ≤ limitALimit ≤ limitBLimit ≤ limitCLimit ≤ limitN.

Input

The only line contains one integerN(1 digit ≤ DigitNLimit ≤ limit 104)
We mentioned above.

Output

Print a single integer-the answer to the problem.

Sample test (s) Input
5
Output
1
Input
74
Output
35
 
Link: http://codeforces.com/problemset/problem/304/A
 
Question: Give N for you to find out how many groups A, B, and C meet 1 ≤ABCNAndA2 +B2 =C2
 
Thoughts: because the maximum N value is only 10 ^ 4 and the time is 3000 ms (I feel that the time given by this question is too wide. Otherwise, many teams will not be able to pass it once because it is more than 1 s) O (N ^ 2) can be used directly) the brute force is slightly optimized. The last 390 ms, but the LOR team only has 15 ms. You can refer to their code. They are table-making and use a program (generally TMl programs ).) output the result to a file and directly create an array in another program. ans [] = {copy} is enough. This method is useful for questions that require a small amount of data in time. I learned another trick.
 
Idea: A, B, C is combined and saved to a num [] array. Then, the required answer is the first n items and S [N] of num [].
 
The following is my code:
 
# Include <iostream> # include <cstdio> # include <cmath> # include <cstring> # include <string> # include <algorithm> # include <map> # define e SQRT (2.0) using namespace STD; int num [10005], s [10005]; // num [I] is equivalent to the series of AI s [I] is equivalent to Si (the first I term and) int main () {int I, j, B, c, temp, t, n, T1, T2; double D1, D2; memset (Num, 0, sizeof (Num); // initialize num [] s [0] = s [1] = s [2] = s [3] = s [4] = 0; // The first four items of S [] are 0 for (C = 5; C <= 10000; C ++) // calculate a for each c loop that meets the condition, b, c {T = C; Temp = (INT) (T * 1.0/e + 0.5); // E is the root number. 2 because B> A, B> = (C/root number 2) can be obtained) + 0.5 is rounded up to prevent loss of precision. For (B = C-1; B> = temp; B --) // for B loop, C-1 cannot be 0, B cannot be equal to C PS: enumeration B is faster. If enumerating a, it is slower from 1 to C/root number 2. {d1 = d2 = SQRT (C + B) * (C-B) * 1.0); // calculate C ^ 2-B ^ 2, that is, calculate a if (INT) (d2 + 0.5) = D1) // determine whether a is an integer. If num [] ++ {num [c] ++;} s [c] = s [C-1] + num [c]; // calculate s []} while (~ Scanf ("% d", & N) {printf ("% d \ n", s [N]); // After the answer is saved, the direct output is enough, which is faster than calculating the answer every time} 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.