解法一
奇数肯定由至少一个奇数和若干偶数相加得到,不难发现偶数对答案没有影响,只是为了凑个数,那么我们从 1 1 1到 n n n枚举所有的奇数,判断是否存在选择了某个数量的奇数能够构造成功
注意x-i要判断是否大于0
解法二:
有一个比较重要的结论,那就是只要一个序列中既含有奇数有含有偶数且 x x x小于 n n n,那么一定可以构造成功(之前也不知道,找不到正确的证明,就先记住8)
然后考虑下面的几种情况:
- 全为奇数时,若 x x x为奇数,则成功;否则失败
- 全为偶数时,失败
- x < n x<n x<n且既含有奇数又含有偶数时,成功
- x = = n x==n x==n且既含有奇数又含有偶数时,奇数的个数为奇数,成功;否则失败
扩展
在写如下式子时,我发现了和异或的联系,设奇数为1,偶数为0:
- 奇 + 奇 = 偶 ------ 0 x o r 0 = 0 0~xor~0~=~0 0 xor 0 = 0
- 偶 + 偶 = 偶 ------ 1 x o r 1 = 0 1~xor~1~=~0 1 xor 1 = 0
- 奇 + 偶 = 奇 ------ 1 x o r 0 = 1 1~xor~0~=~1 1 xor 0 = 1
当然如果本题改成一个01序列,任选x个判断是否为奇数,也可以用如下的方法做
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <math.h>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define ins insert
#define lowbit(x) (x&(-x))
#define mkp(x,y) make_pair(x,y)
#define mem(a,x) memset(a,x,sizeof a);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> P;
const double eps=1e-8;
const double pi=acos(-1.0);
const int inf=0x3f3f3f3f;
const ll INF=1e18;
const int Mod=1e9+7;
const int maxn=2e5+10;
int t,n,x;
int s1,s2;
//解法一
int work(){
for(int i=1;i<=n;i+=2){
if(s1>=i && x-i>=0 && s2>=x-i) return 1;
}
return 0;
}
//解法二
int solve(){
if(s2==n) return 0;
if(s1==n){
if(x&1) return 1;
else return 0;
}
if(x<n) return 1;
else{
if(s1&1) return 1;
else return 0;
}
}
int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>t;
while(t--){
cin>>n>>x;
s1=s2=0;
for(int i=0,u;i<n;i++){
cin>>u;
if(u&1) s1++;
else s2++;
}
if(work()) cout<<"Yes\n";
else cout<<"No\n";
/*if(solve()) cout<<"Yes\n";
else cout<<"No\n";*/
}
return 0;
}