1516C - Baby Ehab Partitions Again(Codeforces Round #717 (Div. 2)题解)
题目链接:1516C - Baby Ehab Partitions Again
思路:背包判断\(\frac{sum}{2}\)是否能够组成,如果不可以,答案显然,否则进行缩放,因为我们处理该规模/GCD的数比我们直接处理该规模的数的好处体现在,将该规模数除以GCD之后,绝对不可能全是偶数的存在,因为如果全是偶数,那么显然现在gcd不为1,所以一定存在一个奇数,在缩放之后处理该奇数,就是答案,因为有答案,所以缩放后的sum和肯定也是偶数,那么偶数-奇数=奇数,所以一定不可能构成两个相同的集合。
\(Code:\)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#define rep(i, a, b) for (auto i = a; i <= b; ++i)
#define bep(i, a, b) for (auto i = a; i >= b; --i)
#define ch() getchar()
#define pc(x) putchar(x)
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define PI acos(-1)
#define mst fflush(stdout)
using namespace std;
template <typename T>
void read(T& x) {
static char c;
static int f;
for (c = ch(), f = 1; c < '0' || c > '9'; c = ch())
if (c == '-') f = -f;
for (x = 0; c >= '0' && c <= '9'; c = ch()) x = x * 10 + (c & 15);
x *= f;
}
template <typename T>
void write(T x) {
static char q[65];
int cnt = 0;
if (x < 0) pc('-'), x = -x;
q[++cnt] = x % 10, x /= 10;
while (x) q[++cnt] = x % 10, x /= 10;
while (cnt) pc(q[cnt--] + '0');
}
const int N = 1e5+11;
int n,a[N],f[N];
void solve(){
read(n);
int sum = 0;
int id = -1;
int mi = 9999999;
rep(i,1,n){
read(a[i]);sum += a[i];
if(a[i]%2)id = i;mi = min(mi,a[i]);
}
if(sum % 2){
pc('0');return ;
}
sum/=2;
rep(i,1,n){
bep(j,sum,a[i]){
f[j] = max(f[j],f[j-a[i]]+a[i]);
}
}
if(f[sum] != sum){
pc('0');return ;
}
int now = a[1];
rep(i,2,n){now = __gcd(now,a[i]);}
rep(i,1,n){
if((a[i]/now)%2){
printf("1\n%d\n",i);return ;
}
}
}
signed main(){solve();return 0; }

浙公网安备 33010602011771号