Vasya and balls#4538

Vasya and balls (Editorial)

If is the side length of the triangle, then to completely fill it,

1 + 2 + ... + n = n \cdot (n + 1) / 2

balls are required.

Suppose there are balls available. To find the maximum possible , solve the equation

n \cdot (n + 1) / 2 = k

and round the non-negative root down to the nearest integer.

Solve the quadratic equation:

n^2 + n - 2k = 0, \\ d = 1 + 8k, n = (-1 + \sqrt{1 + 8k}) / 2

The answer will be \lfloor(-1 + \sqrt{1 + 8k}) / 2 \rfloor.

Algorithm implementation

Read the value of .

scanf("%d",&k);

Compute and print the answer.

n = int((-1 + sqrt(1.0 + 8*k)) / 2.0);
printf("%d\n",n);