Aca and Milovan, two fellow competitive programmers, decided to give Vasilije a problem to test his skills.
Vasilije is given three positive integers: nn, kk, and xx, and he has to determine if he can choose kk distinct integers between 11 and nn, such that their sum is equal to xx.
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
The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.
The only line of each test case contains three integers nn, kk and xx (1≤n≤2⋅1051≤n≤2⋅105, 1≤k≤n1≤k≤n, 1≤x≤4⋅10101≤x≤4⋅1010) — 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 nn over all test cases may exceed 2⋅1052⋅105.
Output
For each test case output one line: "YES", if it is possible to choose kk distinct integers between 11 and nn, such that their sum is equal to xx, 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).
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
output
YES
NO
YES
YES
NO
NO
YES
NO
NO
NO
YES
YES
Code
#include <iostream>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cout.tie(0);
int _;
cin >> _;
while (_) {
long long n, k, x;
cin >> n >> k >> x;
// 使用条件表达式来判断是否输出 "YES" 或 "NO"
cout << ((x >= k * (k + 1) / 2 && x <= k * (n + n - k + 1) / 2) ? "YES" : "NO") << endl;
// 这里减少了一次无用的输入,将 _ 自减来控制循环次数
_--;
}
return 0;
}
Conclusion
上面的代码是一个循环处理多个测试用例的程序。它首先从输入中读取一个整数 _,表示待处理的测试用例数量。
然后,在进入循环之前,通过设置 ios::sync_with_stdio(0); 和 cout.tie(0); 来优化输入输出流的同步。
接下来,通过一个 while 循环来处理每个测试用例。在每次循环迭代中,从输入中读取三个整数 n、k 和 x,它们分别表示测试用例的参数。
接下来,通过使用条件表达式 (x >= k * (k + 1) / 2 && x <= k * (n + n - k + 1) / 2) 来判断条件是否为真。如果条件成立,则输出 "YES",否则输出 "NO"。这里用到了数学计算,判断变量 x 是否在某个范围内。
最后,通过将 _ 变量自减来控制循环次数,以处理所有的测试用例。
总的来说,这段代码通过循环处理多个测试用例,根据给定的条件进行判断并输出结果。代码逻辑相对简单,主要用到了条件判断和输入输出操作。