zoukankan      html  css  js  c++  java
  • CodeForces


    Example
    input
    Copy
    6
    3 1
    4 2
    10 3
    10 2
    16 4
    16 5
    
    output
    Copy
    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个奇数,使这k个奇数相加等于n。

    思路:本题主要用了数学知识,再加上那么一些思考。

           数学知识:1.奇+奇=偶;偶+偶=偶;奇+偶=奇;

                              2.前n个奇数的和为n²,即n个不同的奇数相加最小值为n²。

          应用到本题是这样,

        由数学知识2知,n如果大于k*k,一定无解。

       剩下的情况中,

     ① k为奇数,n为奇数,那么k个奇数相加一定为奇数,符合题意

      ②k为偶数,n为偶数,那么k个偶数相加一定为偶数,符合题意

    其他的都无解,凡是不符合①②的,一定不可以,因为和数学知识1矛盾。

    代码如下:

     1 #include <iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 typedef long long ll;
     5 int main()
     6 {
     7     ll t,n,k;
     8     cin>>t;
     9     while(t--)
    10     {
    11         scanf("%I64d%I64d",&n,&k);
    12         if(n<k*k)
    13             printf("NO
    ");
    14         else
    15         {
    16         if((n&1)==(k&1))//若n和k同为奇或偶,则符合条件
    17             printf("YES
    ");//“与”运算判断奇偶
    18         else
    19         {
    20             printf("NO
    ");
    21         }
    22         }
    23     }
    24     return 0;
    25 }
    View Code

     

  • 相关阅读:
    jQuery事件委托
    jQuery-事件面试题
    jQuery事件处理
    文档—CUD
    jQuery练习
    jQuery-筛选
    5. Longest Palindromic Substring
    340. Longest Substring with At Most K Distinct Characters
    159. Longest Substring with At Most Two Distinct Characters
    438. Find All Anagrams in a String
  • 原文地址:https://www.cnblogs.com/theshorekind/p/12568364.html
Copyright © 2011-2022 走看看