奇数凑数问题

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.

题意:你有两个整数n和k,你的任务是找出n是否可以表示为k个不同的正奇数的和(感谢有道词典:))。

感受:怎么感觉自己近期越来越不行了。。(这种问题越想越复杂,我都想到了二进制解决,发现还是越来越复杂,我是真的脑残)状态不怎么佳,这道题还是看的我的大佬伙伴的代码,不过倒是很好理解。

思路:我们首先要搞明白k个奇数能够凑成哪几个数,这是主要的思路,我们观察前k个奇数最小能够凑成k2,此后k2 + 2, k2 + 4…
都可以凑出来,那么我们只需要判断给定的 n 是不是范围内的可以凑成的数就可以了。

#include <iostream>

using namespace std;
typedef long long LL;
int main()
{
    int T;cin >> T;
    while(T --)
    {
        LL a, k; cin >> a >> k;
        LL t = k * k;
        if(((LL)a + k) % 2) 
        {
            cout << "NO" << endl;
            continue;
        }
        if(a >= t) cout << "YES" << endl;
        else cout << "NO" << endl;   
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值