zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 84 (Rated for Div. 2) A. Sum of Odd Integers(思维题)

    You are given two integers nn and kk. Your task is to find if nn can be represented as a sum of kk distinct positive odd (not divisible by 22) integers or not.

    You have to answer tt independent test cases.

    Input

    The first line of the input contains one integer tt (1t1051≤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 kk (1n,k1071≤n,k≤107).

    Output

    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 22) 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
    挺好一道思维水题,一开始没看到distinct直接WA到怀疑人生...
    举个例子就能明白,比如k=4,组合出来的最小值为1+3+5+7=16,之后可以对某些数+2使得能够组合出例如3+3+5+7=18,所以k=4的话能组合出16 18 20 22......全为偶数,而如果k=3能组合出9 11 13......全为奇数,所以首先判断n和k的奇偶性,如果相同的话看看是否大于这个k能组合出的最小数即可。最小值可以用等差数列算,记得得开long long。
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            long long n,k;
            cin>>n>>k;
            long long mmin=(1+2*k-1)*k/2;
            if(k%2==0)
            {
                if(n>=mmin&&n%2==0)cout<<"YES"<<endl;
                else cout<<"NO"<<endl;
            }
            else
            {
                if(n>=mmin&&n%2!=0)cout<<"YES"<<endl;
                else cout<<"NO"<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    js 实现图片上传
    关于IOS不能使用JQUERY的ON事件
    js实现复制
    订单列表倒计时
    小程序实现倒计时
    微信小程序服务消息推送
    python爬虫七
    python爬虫六
    python爬虫五
    python爬虫四
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/12559719.html
Copyright © 2011-2022 走看看