zoukankan      html  css  js  c++  java
  • CodeForces 1327A

    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;
    }
    
  • 相关阅读:
    2019-9-2-正则表达式30分钟入门教程
    2019-6-23-开源项目使用-appveyor-自动构建
    2019-8-29-dotnet-core-使用-sqlite-部署到-Centos-服务器
    2018-10-19-Roslyn-使用-Directory.Build.props-文件定义编译
    2019-4-29-dotnet-通过-WMI-获取系统安装软件
    2018-12-24-win10-uwp-求两个矩形相连的几何
    shell公共函数functions
    linux防火墙和SELinux
    ubuntu开启ssh
    文件夹操作
  • 原文地址:https://www.cnblogs.com/Hayasaka/p/14294289.html
Copyright © 2011-2022 走看看