6860.Sum of Four Squares

Time Limit: 1s Memory Limit: 512MB

A well known result in number theory is that every non-negative integer can be represented as the sum of four squares of non-negative integers.

You are given a non-negative integer $n$ . Your task is to find four non-negative integers $a$ , $b$ , $c$ and $d$ such that $n = a^2 + b^2 + c^2 + d^2$ .

Input Format(From the terminal/stdin)

The first line has an integer $t$ : the number of test cases.

Each of the next $t$ lines has an integer $n$ .

  • $1 \le t \le 1000$
  • $0 \le n \le 10^7$
  • the sum of all $n$ is at most $10^7$

Output Format(To the terminal/stdout)

For each test case, print four non-negative integers $a$ , $b$ , $c$ and $d$ that satisfy $n = a^2 + b^2 + c^2 + d^2$ .

Sample Input

Copy
3
5
30
322266
 \n
 \n
  \n
      \n

Sample Output

Copy
2 1 0 0
1 2 3 4
314 159 265 358
 · · · \n
 · · · \n
   ·   ·   ·   \n
Source: CSES, Mathematics, 3355

Submit

请先 登录

© 2025 FAQs