51Nod 10,802-digit sum of squares

Source: Internet
Author: User
Tags square root

Seeing 1e9 of the data basically excludes the possibility of violence. If a number can be represented by the sum of squares of two numbers, then these two numbers must not exceed his square root. So the basic idea is to first give the input of this number, reduce the amount of data, enumeration 0 to sqrt (number), and then to [0,sqrt (number)] This interval two to find whether there is such a number so that his square plus the previous enumeration of the sum of squares equals number. One thing to note is that the output is based on a small order in front of the number, and is going to be heavy. Based on the map container's one by one correspondence and internal automatic sorting, you can use the map container to do both of these tasks. The code is as follows:

#include <cstdio> #include <iostream> #include <algorithm> #include <string> #include < cstring> #include <cmath> #include <stack> #include <vector> #include <map> #include <set > #include <queue> #include <utility> #define LL long long#define Ull_ unsigned long longusing namespace std;    int n; int searh_binary (int begin_, int end_, int num) {int L = begin_, r = end_;        while (L < r) {int mid = (L + r) >> 1;        int Index_one = mid * mid;        int index_two = num * num;        if (index_one + index_two = = N) {return mid;        }else if (Index_one + Index_two < n) {L = mid + 1;        }else{r = Mid; }} return-1;}    int main () {CIN >> n;    int num = (int) sqrt (n);    int index;    BOOL check = FALSE;    Map<int, int> Map_;        for (int i = 0; I <= num; i + +) {index = searh_binary (0, num, i);if (Index! =-1) {map_[min (I, index)] = max (i, index);        Check = true;    }} if (!check) {cout << "No solution" << Endl;        }else{Map<int, int>::iterator it; for (it = Map_.begin (); It! = Map_.end (); it + +) {cout << it->first << "" << it->s        Econd << Endl; }} return 0;}

51Nod 10,802 number of squares and

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.