Sum of Odd Integers (奇数的和)

CodeForces - 1327A传送门

数据:

Input

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 n and k (1≤n,k≤107).

Output

For each test case, print the answer — "YES" (without quotes) if nn 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

题意:6组数据,n和k    若k个不同的奇数求和等于n,即输出yes

思路:这里分享两种代码,首先分析清楚题意,是k个不同的奇数之和,先求出k个不同奇数之和的最小值,n至少大于等于这个最小值,才有可能yes(在某个奇数上一直+2...总会等于n的),其次n和k一定奇偶性相同(第二种代码表示的很简洁)...

 code:注意的是,要开成long long型,两个数相乘可能超过int....

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e6+10;
#define mem(a,b) memset(a,b,sizeof(a))
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll n,k;///!!
        scanf("%lld %lld",&n,&k);
        ll minn=(1+2*k-1)*k/2;///k个数所能组成的最小数值
        if(k%2==0)///k为偶数  k个奇数之和比为偶数
        {
            if(n>=minn&&n%2==0)///n也为偶数
                ///通过最小值中的任意一个奇数一直+2...一定能取到之和等于n
                printf("YES\n");
            else printf("NO\n");
        }
        else ///k为奇数  k个之和比为奇数
        {
            if(n>=minn&&n%2!=0)///同理
                printf("YES\n");
            else printf("NO\n");
        }
    }
    return 0;
}
///给定n,k   若k个不同的奇数相加得到n  则输出YES,反之no

the secondary: 

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e6+10;
#define mem(a,b) memset(a,b,sizeof(a))
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll n,k;
        scanf("%lld %lld",&n,&k);
        if(n>=k*k&&(n-k)%2==0)
            printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
///给定n,k   若k个不同的奇数相加得到n  则输出YES,反之no

很喜欢做cf上的题,大多数都偏向于开拓思维的题,单指前面的题,哈哈哈,后面的题是既要思维,又要算法知识....还是太菜了,继续努力!坚持不放弃

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵩韵儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值