The sum of squares of the seventh Blue Bridge cup

Source: Internet
Author: User

The sum of squares of the seventh Blue Bridge cup

Reprinted please indicate the source: http://www.cnblogs.com/zhishoumuguinian/p/8372337.html

Quartile and theorem, also known as the Laplace theorem: Each positive integer can be expressed as the sum of squares of up to four positive integers. If 0 is included, it can be expressed as the sum of squares of four numbers. For example: 5 = 0 ^ 2 + 0 ^ 2 + 1 ^ 2 + 2 ^ 2 7 = 1 ^ 2 + 1 ^ 2 + 1 ^ 2 + 2 ^ 2 (^ represents the multiplication meaning) for a given positive integer, there can be multiple squares. Order the four numbers: 0 <= a <= B <= c <= d. All possible representations are arranged in ascending order of a, B, c, and d, and the first notation is output. The input is a positive integer N (N <5000000). Four non-negative integers must be output, sorted in ascending order, separated by spaces.

For example, enter:
5
The program should output:
0 0 1 2

For example, enter:
12
The program should output:
0 2 2 2

For example, enter:
773535
The program should output:
1 267 838

Resource conventions:
Peak memory consumption <256 M
CPU consumption <3000 ms

Please output strictly as required. Do not print anything that is similar to "Please input. All codes are stored in the same source file. After debugging is successful, copy and submit the source code.

Note: The main function must return 0.
Note: Only ansi c/ansi c ++ standards are used. Do not call special functions that depend on the compiling environment or operating system.
Note: All dependent functions must be explicitly included in the source file # include <xxx>. Common header files cannot be omitted through engineering settings.

When submitting, select the expected compiler type.

 

Idea: the first thought is a layer-4 loop, but the last layer can be omitted. Loop through each layer to sqrt (5000000) + 1, you can first output, sqrt (5000000) + 1 value is 2237. This is the idea of the big question, and then paste the code.

1 # include <iostream> 2 # include <cmath> 3 using namespace std; 4 5 6 int main () 7 {8 int n; 9 cin> n; 10 // n = 5000000; 11 for (int a = 0; a <= 2237; a ++) 12 {13 for (int B = 0; B <= 2237; B ++) 14 {15 for (int c = 0; c <= 2237; c ++) 16 {17 int dj = n-a * a-B * B-c * c; 18 int d = sqrt (dj); 19 if (dj = d * d) // If the dj is equal to the square of d, the fourth digit is the square of an integer, the integer d is the fourth digit 20 {21 cout <a <"" <B <"" <c <"" <d; 22 return 0; 23} 24} 25} 26} 27}

The question time limit is 3 seconds.

Calculated 5000000, DEV-C ++ execution time CODEBLOCKS execution time

If this article is helpful to you, please give

Related Article

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.