Day 39 A. Odd Selection

Problem
Shubham has an array a of size n, and wants to select exactly x elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct.

Tell him whether he can do so.

Input
The first line of the input contains a single integer t (1≤t≤100) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n and x (1≤x≤n≤1000) — the length of the array and the number of elements you need to choose.

The next line of each test case contains n integers a1,a2,…,an (1≤ai≤1000) — elements of the array.

Output
For each test case, print “Yes” or “No” depending on whether it is possible to choose x elements such that their sum is odd.

You may print every letter in any case you want.

Example
input
5
1 1
999
1 1
1000
2 1
51 50
2 2
51 50
3 3
101 102 103

output
Yes
No
Yes
Yes
No

Note
For 1st case: We must select element 999, and the sum is odd.

For 2nd case: We must select element 1000, so overall sum is not odd.

For 3rd case: We can select element 51.

For 4th case: We must select both elements 50 and 51 — so overall sum is odd.

For 5th case: We must select all elements — but overall sum is not odd.

题目意思为 给你n个数 问在这n个数中取x个数 是否能让这x个数总和加起来为奇数
//这里用到了奇偶性 当n=x时 如果有偶数个奇数 那么是不成立的 其他皆成立 当n>x时 如果取得数为偶数个 并且 没有偶数 那么也是不成立

#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<stdio.h>
#include<limits.h>
#include<cmath>
#include<set>
#include<map>
#define ll long long
using namespace std;
int a[1005];
int main()
{
    int t;
    int n, x;
    int sum1, sum2;
    cin >> t;
    while (t--)
    {
        sum1 = 0;
        sum2 = 0;
        cin >> n;
        cin >> x;
        for (int i = 0; i < n; i++) 
        {
            cin >> a[i];
            if (a[i] % 2 == 1) 
            {
                sum1++;
            }
            else
            {
                sum2++;
            }
                
        }
        if (sum1 == 0) 
        {
            cout << "No" << endl;
        }
        else
        {
            if (n == x) 
            {
                if (sum1 % 2 == 0) 
                {
                    cout << "No" << endl;
                }
                else
                {
                    cout << "Yes" << endl;
                }
            }
            else 
            {
                if (x % 2 == 0 && sum2 == 0)
                {
                    cout << "No" << endl;
                }
                else 
                {
                    cout << "Yes" << endl;
                }
            }
        }
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值