A. Sum of Odd Integers
time limit per test :2 seconds memory limit per test :256 megabytes
input :standard input output :standard output
You are given two integers n and k. Your task is to find if n can be represented as a sum of k distinct positive odd (not divisible by 2) integers or not.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t () — the number of test cases.
The next t lines describe test cases. The only line of the test case contains two integers n and k ().
Output
For each test case, print the answer — "YES" (without quotes) if nn can be represented as a sum of k distinct positive odd (not divisible by 2) integers and "NO" otherwise.
Example
input
6
3 1
4 2
10 3
10 2
16 4
16 5
output
YES
YES
NO
YES
YES
NO
Note
In the first test case, you can represent 3 as 3.
In the second test case, the only way to represent 4 is 1+3.
In the third test case, you cannot represent 10 as the sum of three distinct positive odd integers.
In the fourth test case, you can represent 10 as 3+7, for example.
In the fifth test case, you can represent 16as 1+3+5+7.
In the sixth test case, you cannot represent 16as the sum of five distinct positive odd integers.
题目大意
给你n和k,问你n能否由k个互不相同的奇数组成
思路
思路很简单(虽然很简单,但第一次看错题目,第二次没开long long wa了两发,难受!所以还是写一篇记录一下)
1. 前k个奇数的和是,如果n比小一定不可能
2. 偶数个奇数一定是偶数,奇数个奇数一定是奇数,满足时判断奇偶性即可,n,k奇偶性相同一定可以(为什么一定可以,我们只要取最小的k个互不相同的奇数,然后把最后一个一直加2即可,这样就能保证k个奇数仍然互不相同)
3. 注意开long long,k平方会爆
代码
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5 + 10;
int main()
{
int t;
cin >>t;
while (t --){
ll n, k;
cin >> n >>k;
if (n < k * k) printf("NO\n");
else {
if ((n % 2 == 1 && k % 2 == 0) || (n % 2 == 0 && k % 2 == 1)) printf("NO\n");
else printf("YES\n");
}
}
return 0;
}