CodeForces 1327A - Sum of Odd Integers(思维)

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 (1≤t≤105) — 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 (1≤n,k≤107).

Output

For each test case, print the answer — “YES” (without quotes) if n 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 16 as 1+3+5+7.

In the sixth test case, you cannot represent 16 as the sum of five distinct positive odd integers.

给出两个整数n和k。您的任务是查找n是否可以表示为k个不同的正奇数(不能被2整除)整数之和。

您必须回答t个独立的测试用例。

输入值
输入的第一行包含一个整数t(1≤t≤105)-测试用例的数量。

接下来的t行描述了测试用例。测试用例的唯一一行包含两个整数n和k(1≤n,k≤107)。

输出量
对于每个测试用例,如果n可以表示为k个不同的正奇数(不能被2整除)整数的总和,否则打印“ NO”(否),打印答案-“ YES”(不带引号)。


输入
6
3 1
4 2
10 3
10 2
16 4
16 5
输出
YES
YES
NO
YES
YES
NO
注意
在第一个测试用例中,您可以将3表示为3。

在第二个测试用例中,表示4的唯一方法是1 + 3。

在第三个测试用例中,不能将10表示为三个不同的正整数的和。

例如,在第四个测试用例中,您可以将10表示为3 + 7。

在第五个测试用例中,您可以将16表示为1 + 3 + 5 + 7。

在第六个测试用例中,您不能将16表示为五个不同的正整数的和。

题目大意:多个测试样例,对于每个测试样例,给出n和k,要求判断n是否可可以由k个不相同的正奇数相加得到,可以看一下样例分析。

解题思路:首先我们应该知道:偶数个奇数相加一定是偶数,奇数个奇数相加一定是奇数,所以对于给出的n和k,如果n是偶数,k是奇数,或者n是奇数,k是偶数,n和k不是同奇同偶,则n一定不可能由k个奇数相加得到。所以我们先判断n和k是否同为奇数(偶数),这是要考虑的第一点,第二点,k个奇数有一个最小值,如果k=4,则k个不同的奇数最小值为1+3+5+7=16,如果n的值<=16,n也不可能由k个奇数相加得到,k=1时候,最小值是1,k等于2时,最小值4,k等于3时,最小值9,通过观察可以发现,k的最小值就是k*k。所以判断一下n是否大于k的平方即可。ac代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
using LL = long long;//范围是1e7,开一下long long(WA一次)
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		LL n,k;
		cin>>n>>k;
		if((n+k)%2!=0)//是否同为奇数(偶数)
		  cout<<"NO"<<endl;
		else
		{
			if(n>=k*k)//判断n和最小值的关系
			  cout<<"YES"<<endl;
			else
			  cout<<"NO"<<endl;
		}
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值