Sum of Squares 2 (More Huge)
Time limit0.5sMemory limit512 MB
Given n up to 10^18, find the minimum number of perfect squares summing to n and output the actual square roots used.
- Level
Hard8 of 10
- Topics
- Math, Number theory, Binary search, Brute force
- Solved
- No attempts yet
Problem
In 1770, Lagrange proved that every natural number can be expressed as a sum of four or fewer squares. Some natural numbers have multiple such representations. For example, 26 is the sum of and ; it can also be written as . Historically, the problem commonly given to mental calculation experts was to express a natural number as a sum of four or fewer squares. In the early 1900s, one mental calculator reportedly took 8 seconds to find the solution . A harder problem took 56 seconds: .
Given a natural number , write a computer program that expresses as a sum of the minimum number of squares.
Input
The input is read from standard input. It consists of one line containing the natural number , where .
Output
The output is written to standard output. On the first line, print the minimum number of squares whose sum equals .
On the second line, print the numbers whose squares sum to , separated by spaces, for as many numbers as printed on the first line. Do not output negative integers.
If there are multiple answers, you may print any of them.