博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Educational Codeforces Round 84 ( Div. 2)A. Sum of Odd Integers
阅读量:4034 次
发布时间:2019-05-24

本文共 1714 字,大约阅读时间需要 5 分钟。

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个不同的正奇数的和。

思路

很显然n和k的奇偶性必须一致(奇数个奇数相加还是奇数,偶数个奇数相加是偶数)

而且k应该还有个范围,k个奇数相加的最小和(也就是前k项奇数相加的值)应该小于等于n,满足这两个条件才能输出YES
但是求前k项的和还有一个问题,如果是暴力求解的话一定会超时的,那就找找规律
1+3+5+…+(2K-1)
=(1+(2
K-1))K/2
=K
K
找到这个规律这个题也就解决了
注意数据类型用long long int

代码实现

#include
using namespace std;typedef long long ll;int main(){
ll t; ll n, k; cin >> t; while (t--) {
cin >> n >> k; if (n < k * k) {
cout << "NO" << endl; continue; } else {
if ((n % 2 == 1 && k % 2 == 1)||( n % 2 == 0 && k % 2 == 0)) cout << "YES" << endl; else cout << "NO" << endl; } } return 0;}

码字不易,留个赞吧

授人一赞,手留余香~

转载地址:http://mafdi.baihongyu.com/

你可能感兴趣的文章
S3C6410启动模式介绍
查看>>
2440初始化存储器原理(接上一篇)
查看>>
S3C2440 USB 设备控制器(转)
查看>>
Linux usb 设备驱动 (1)
查看>>
解决跨网场景下,CAS重定向无法登录的问题(无需修改现有代码)
查看>>
java反编译命令
查看>>
activemq依赖包获取
查看>>
概念区别
查看>>
关于静态块、静态属性、构造块、构造方法的执行顺序
查看>>
final 的作用
查看>>
在Idea中使用Eclipse编译器
查看>>
idea讲web项目部署到tomcat,热部署
查看>>
优化IDEA启动速度,快了好多。后面有什么优化点,会继续往里面添加
查看>>
JMeter 保持sessionId
查看>>
IDEA Properties中文unicode转码问题
查看>>
Idea下安装Lombok插件
查看>>
zookeeper
查看>>
Idea导入的工程看不到src等代码
查看>>
技术栈
查看>>
Jenkins中shell-script执行报错sh: line 2: npm: command not found
查看>>