Under construction

Still being built. Things may change

Perfect Squares

Time limit1sMemory limit2048 MB

Summary
Given n up to 10^12, find three integers whose squares sum to n, or print -1 if no such triple exists by Legendre's three-square condition.
Level

Medium5 of 10

Topics
Math, Number theory, Brute force
Solved
No attempts yet

Problem

A famous theorem in number theory states that every positive integer can be written as the sum of four perfect squares. Usually, though, fewer squares are enough. For example, 2727 needs only three perfect squares: 27=52+12+1227 = 5^2 + 1^2 + 1^2.

A mathematician friend lists these facts about perfect squares:

  • An odd prime pp is a sum of two squares if and only if p≡1(mod4)p \equiv 1 \pmod 4.
  • If two positive integers aa and bb are each a sum of two squares, then their product abab is also a sum of two squares.
  • Every positive integer is a sum of three perfect squares, unless it has the form 4a⋅(8b+7)4^a \cdot (8b + 7), where aa and bb are non-negative integers.

The last fact about sums of three squares interests you, so you decide to write a program that checks the claim and produces the actual squares.

Input

The input is a single integer nn (1≤n≤10121 \le n \le 10^{12}).

Output

If nn can be expressed as the sum of three squares, output three integers xx, yy, and zz. An answer is accepted if 0≤x,y,z≤n0 \le x, y, z \le \sqrt{n} and n=x2+y2+z2n = x^2 + y^2 + z^2. If several choices work, output any of them. Always print exactly three integers, even when nn is a sum of two or fewer squares. If nn cannot be expressed as the sum of three squares, output −1-1 and nothing else.

Examples3

  1. Example 1

    Input
    22
    
    Expected output
    3 3 2
    
  2. Example 2

    Input
    23
    
    Expected output
    -1
    
  3. Example 3

    Input
    999999999989
    
    Expected output
    471545 0 881842