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; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!