CodeForces - 1327A Sum of Odd Integers(数学+思维)


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²。

  应用到本题是这样,k为奇数,n为奇数,那么k个奇数相加一定为奇数,符合题意

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

然后剩下的情况中,由数学知识2知,n不能大于k*k,否则一定无解。

代码如下:

 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\n");
14         else
15         {
16         if((n&1)==(k&1))//若n和k同为奇或偶,则符合条件
17             printf("YES\n");//“与”运算判断奇偶
18         else
19         {
20             printf("NO\n");
21         }
22         }
23     }
24     return 0;
25 }
View Code

 

猜你喜欢

转载自www.cnblogs.com/theshorekind/p/12568364.html