A. Sum of Odd Integers

心不动则不痛 提交于 2020-03-25 16:20:20

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 nn can be represented as a sum of 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 (1t105) — the number of test cases.

The next tt lines describe test cases. The only line of the test case contains two integers nn and k (1n,k107).

Output

For each test case, print the answer — "YES" (without quotes) if nn can be represented as a sum of kdistinct positive odd (not divisible by 2) integers and "NO" otherwise.

Example
input
Copy
6
3 1
4 2
10 3
10 2
16 4
16 5
output
Copy
YES
YES
NO
YES
YES
NO
Note

In the first test case, you can represent 33 as 33.

In the second test case, the only way to represent 44 is 1+31+3.

In the third test case, you cannot represent 1010 as the sum of three distinct positive odd integers.

In the fourth test case, you can represent 1010 as 3+73+7, for example.

In the fifth test case, you can represent 1616 as 1+3+5+71+3+5+7.

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

 

题意:

给定 正整数nk。要判断n是否是由k个正奇数相加可以得到的数。

思路:

根据奇偶数性质,便可得出 n和k只有同奇或同为偶 才能输出YES

 

但n,k的数据范围都是1到 1e5。那如何判断n,k满足什么关系才能YES?

k:                    2     4    6     8     10

n:(最小值)  4    16  36   64   100

k:                    1     3    5     7     9

n:(最小值)  1     9    25    49    81

由此可以得出   k*k>n的话一定NO。换句话说n>k*k 才能YES。

代码:

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define ll long long
 
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long  n,k;
        cin>>n>>k;
        if(k>sqrt(n)) cout<<"NO"<<endl;
        else if(k%2==0&&n%2==0||k%2!=0&&n%2!=0)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
 
    }
    return 0;
}
View Code

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!