Sum of squares of 10,802 numbers

Source: Internet
Author: User

Base time limit: 1 seconds space limit: 131072 KB Score: 5 Difficulty: 1-level algorithm problem

Given an integer n, the n is represented as the sum of squares of 2 integers i J (i <= J), and if there are multiple representations, it is output in ascending order of I. For example: N = 130,130 = 3^2 + 11^2 = 7^2 + 9^2 (Note: 3 11 with 11 3 count 1 kinds) Input

A number n (1 <= n <= 10^9)
Output
A total of k rows: 2 numbers per line, I j, indicating n = i^2 + j^2 (0 <= i <= j). Output no solution if it cannot be decomposed to a sum of 2 numbers
Input example
130
Output example
3 117 9

Simple math problem began without brain violence T got a pitch,

Meow meow meow??? Just card me a set of data??? Frighten me hurriedly spent five nod shield to write a special sentence ...

Bah! You acmer face still need!!

We can set t^2=n, then we can see that I and J must be no more than T, so we traverse to sqrt (n),

Of course, we can not have no brain traversal, when we know I can be j^2= (n-i^2) to obtain J, in addition, I will not be greater than J.

The AC code is attached:

1#include <iostream>2#include <cmath>3 using namespacestd;4 5 intMain () {6     intN;7Cin>>N;8     intans=0;9     intt=sqrt (n);Ten      for(intI=0; i<=t;i++){ One         intJ=SQRT (n-i*i); A         if(j*j+i*i==n&&j>=i) { -ans++; -cout<<i<<" "<<j<<Endl; the         } -     } -     if(ans==0) -cout<<"No Solution"<<Endl; +     return 0; -}

Sum of squares of 10,802 numbers

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.