HDU 3835R (N) (enumeration of brute force thinking)

Source: Internet
Author: User

R (N) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission (s): 1637 Accepted Submission (s ): 853 Problem DescriptionWe know that some positive integer x can be expressed as x = A ^ 2 + B ^ 2 (A, B are integers ). take x = 10 for example, 10 = (-3) ^ 2 + 1 ^ 2.We define R (N) (N is positive) to be the total number of variable presentation of N. so R (1) = 4, which consists of 1 = 1 ^ 2 + 0 ^ 2, 1 = (-1) ^ 2 + 0 ^ 2, 1 = 0 ^ 2 + 1 ^ 2, 1 = 0 ^ 2 + (-1) ^ 2. given N, you are to calculate R (N ). inputNo more than 100 test cases. each case contains only one integer N (N <= 10 ^ 9 ). outputFor each N, print R (N) in one line. sample Input26102565 Sample Output4081216HintFor the fourth test case, (A, B) can be (), (0,-5), (5, 0), (-5, 0), (3, 4 ), (3,-4), (-), (-3,-4), (), (4,-3), (-), (-4, -3) Source2011 Multi-Uni Versity Training Contest 1-Host by HNU the question is very easy to understand, find how many groups a, B can meet a ^ 2 + B ^ 2 = n. n is a maximum of 10 ^ 9, and it is certainly not allowed to enumerate brute force. Then enumerate a> B> = 0. Then, in each case, x 4 is used. A, B; a,-B;-a, B;-a,-B; if B = 0, 5, 0;-5, 0,-5, 5; also, see the code for details. Address: R (N) AC code:

# Include <iostream> # include <cstring> # include <string> # include <cstdio> # include <cmath> using namespace std; int main () {int n, I, a, B, ma, res; while (~ Scanf ("% d", & n) {ma = sqrt (double (n); // The largest a, a> B> = 0 res = 0; for (a = 1; a <= ma; a ++) {B = sqrt (double (n-a * )); if (a * a + B * B = n) res + = 4; // a, B; a,-B;-a, B;-, -B;} cout <res <endl;} return 0;} // 46 MS 280 K

 


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.