Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)/CF1323 A. Even Subset Sum Problem(水题)
You are given an array aa consisting of nn positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by 22 ) or determine that there is no such subset.
Both the given array and required subset may contain equal values.
The first line contains a single integer tt (1≤t≤1001≤t≤100 ), number of test cases to solve. Descriptions of tt test cases follow.
A description of each test case consists of two lines. The first line contains a single integer nn (1≤n≤1001≤n≤100 ), length of array aa .
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1001≤ai≤100 ), elements of aa . The given array aa can contain equal values (duplicates).
For each test case output −1−1 if there is no such subset of elements. Otherwise output positive integer kk , number of elements in the required subset. Then output kk distinct integers (1≤pi≤n1≤pi≤n ), indexes of the chosen elements. If there are multiple solutions output any of them.
3 3 1 4 3 1 15 2 3 5
1 2 -1 2 1 2
利用奇数偶数性质就可以。
#include <bits/stdc++.h> using namespace std; int n,a[105]; int main() { int t; cin>>t; while(t--) { cin>>n; int i,j; int ans=0,ans1=0,ans2=0,flag=0; for(i=1;i<=n;i++) { scanf("%d",&a[i]); if(a[i]%2==0) { flag=1; ans=i; } else { if(!ans1) { ans1=i; } else if(!ans2) { ans2=i; } } } if(flag) { cout<<1<<endl; cout<<ans<<endl; continue; } if(ans1&&ans2) { cout<<2<<endl; cout<<ans1<<' '<<ans2<<endl; } else { cout<<-1<<endl; } } return 0; }