6861.Triangle Number Sums

Time Limit: 2s Memory Limit: 512MB

A triangle number is a positive integer of the form $1+2+\dots+k$ . The first triangle numbers are $1$ , $3$ , $6$ , $10$ and $15$ .

Every positive integer can be represented as a sum of triangle numbers. For example, $42=21+21$ and $1337=1326+10+1$ .

Given a positive integer $n$ , determine the smallest number of triangle numbers that sum to $n$ .

Input Format(From the terminal/stdin)

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

After that, each line has a positive integer $n$ .

  • $1 \le t \le 100$
  • $1 \le n \le 10^{12}$

Output Format(To the terminal/stdout)

For each test, print the smallest number of triangle numbers.

Sample Input

Copy
5
1
2
3
42
1337
 \n
 \n
 \n
 \n
  \n
    \n

Sample Output

Copy
1
2
1
2
3
 \n
 \n
 \n
 \n
 \n
Source: CSES, Mathematics, 3406

Submit

请先 登录

© 2025 FAQs