Perfect Squares

시간 제한1초메모리 제한2048 MB

요약
n이 10^12 이하로 주어질 때 x^2+y^2+z^2=n인 정수 x, y, z를 찾고, 4^a(8b+7) 꼴이면 -1을 출력한다.
난이도

보통10점 중 7점

유형
정수론, 수학, 완전 탐색
정답자
아직 제출이 없습니다

문제

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

You share your observations with a mathematician friend, who rattles off the following perfect squares facts:

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

This last fact about sums of three squares intrigues you, and so you would like to write a program that verifies the claim is true by producing the actual squares.

입력

Input contains a single integer nn (1≤n≤10121 ≤ n ≤ 10^{12}).

출력

If nn can be expressed as the sum of three squares, output three integers xx, yy, and zz. Your answer will be judged correct if 0≤x,y,z≤n0 ≤ x, y, z ≤ \sqrt{n} and n=x2+y2+z2n = x^2 +y^2 +z^2. If there are multiple valid choices for xx, yy, and zz you may output any of them. You must output exactly three integers, even if nn can be expressed as the sum of two or fewer squares.

If nn cannot be expressed as the sum of three squares, output −1-1 and no further output.

예제3

  1. 예제 1

    입력
    22
    
    예상 출력
    3 3 2
    
  2. 예제 2

    입력
    23
    
    예상 출력
    -1
    
  3. 예제 3

    입력
    999999999989
    
    예상 출력
    471545 0 881842