A. Sum of Odd Integers
256 megabytes
You are given two integers n and k. Your task is to find if nn 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.
The first line of the input contains one integer t (1≤t≤105) — 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 (1≤n,k≤107).
For each test case, print the answer — "YES" (without quotes) if nn can be represented as a sum of kk distinct positive odd (not divisible by 2) integers and "NO" otherwise.
6 3 1 4 2 10 3 10 2 16 4 16 5
YES YES NO YES YES NO
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.
题意:
给定 正整数n和k。要判断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; }
来源:https://www.cnblogs.com/love-chili/p/12566266.html