Vasilije in Cacak
Contest: Codeforces Round 900 (Div. 3)
Difficulty rating: 900
Tags: math

Aca and Milovan, two fellow competitive programmers, decided to give Vasilije a problem to test his skills.

Vasilije is given three positive integers: $$$n$$$, $$$k$$$, and $$$x$$$, and he has to determine if he can choose $$$k$$$ distinct integers between $$$1$$$ and $$$n$$$, such that their sum is equal to $$$x$$$.

Since Vasilije is now in the weirdest city in Serbia where Aca and Milovan live, Cacak, the problem seems weird to him. So he needs your help with this problem.

Input format:
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.

The only line of each test case contains three integers $$$n$$$, $$$k$$$ and $$$x$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$1 \le k \le n$$$, $$$1 \le x \le 4 \cdot 10^{10}$$$) — the maximum element he can choose, the number of elements he can choose and the sum he has to reach.

Note that the sum of $$$n$$$ over all test cases may exceed $$$2 \cdot 10^5$$$.

Output format:
For each test case output one line: "YES", if it is possible to choose $$$k$$$ distinct integers between $$$1$$$ and $$$n$$$, such that their sum is equal to $$$x$$$, and "NO", if it isn't.

You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first test case $$$n = 5,\ k=3,\ x=10$$$, so we can choose the numbers: $$$2$$$, $$$3$$$, $$$5$$$, whose sum is $$$10$$$, so the answer is "YES".

In the second test case $$$n = 5, \ k=3, \ x=3$$$, there is no three numbers which satisfies the condition, so the answer is "NO". It can be shown that there are no three numbers whose sum is $$$3$$$.

Example input:
12
5 3 10
5 3 3
10 10 55
6 5 20
2 1 26
187856 87856 2609202300
200000 190000 19000000000
28 5 2004
2 2 2006
9 6 40
47202 32455 613407217
185977 145541 15770805980

Example output:
YES
NO
YES
YES
NO
NO
YES
NO
NO
NO
YES
YES

Editorial:
It is clear that the minimum sum is obtained for the numbers $$$1, 2, 3, \dots, k$$$, and its value is $$$\frac{k\cdot(k+1)}{2}$$$ (the sum of the first $$$k$$$ natural numbers).
Furthermore, it is evident that the maximum sum is achieved for the numbers $$$n, n-1, n-2, \dots, n-k+1$$$, and its value is $$$\frac{n\cdot(n+1)-(n-k)\cdot(n-k+1)}{2}$$$ (the sum of all numbers from $$$1$$$ to $$$n$$$ minus the sum of all numbers from $$$1$$$ to $$$n-x$$$ elements).
Let's prove that among any $$$k$$$ numbers (whose sum is not maximal), there exists a number $$$a < n$$$ such that $$$a+1$$$ is not among those $$$k$$$ numbers. Let's assume the opposite, that is, there exist $$$k$$$ numbers whose sum is not maximal, and for each of those $$$k$$$ numbers, $$$k + 1$$$ is also among those numbers. Let $$$v$$$ be the smallest among them. Consequently, $$$v + 1$$$ is also among these $$$k$$$ numbers. Since $$$v + 1$$$ is in these $$$k$$$ numbers, then $$$v + 2$$$ is also among these $$$k$$$ numbers. Similarly, we can conclude that $$$v, v + 1, v + 2, v + 3, \dots$$$ are all among these $$$k$$$ numbers. However, since we have $$$k$$$ of them, these are the $$$k$$$ numbers that would yield the maximum sum ($$$n, n-1, n-2, \dots, n-k+1$$$). This is a contradiction!
So, among any $$$k$$$ numbers (whose sum is not maximal), there exists a number $$$a < n$$$ such that $$$a+1$$$ is not among those $$$k$$$ numbers. Based on this, starting from the minimum sum $$$S$$$, we can obtain $$$S + 1$$$ (by replacing the number $$$a$$$ with $$$a + 1$$$, the sum increases by $$$1$$$), then from the sum $$$S + 1$$$, we obtain the sum $$$S + 2$$$, and so on.
Therefore, by applying the principle of mathematical induction, we can obtain any sum that is greater than or equal to minumum sum and less than or equal to maximum sum.