This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
using namespace std;
#define int long long
const int mod=998244353,mxn=500+5,lg=30,inf=1e18,minf=-1e18,Mxn=501*501;
int dp[Mxn+10],val[mxn+10],last[Mxn+10];
bool ans[Mxn+10];
int32_t main(){
fastio
int n,sum=0;cin>>n;
for(int i=1;i<=n;i++)cin>>val[i],sum+=val[i];
dp[0]=1;
for(int i=1;i<=n;i++)for(int j=sum;j>=0;j--)if(j-val[i]>=0)dp[j]+=dp[j-val[i]];
if(sum%2||dp[sum/2]==0){
cout<<0;
return 0;
}
for(int i=0;i<=sum;i++)ans[i]=true;
for(int i=1;i<=n;i++){
for(int j=0;j<=sum;j++)last[j]=dp[j];
for(int j=0;j+val[i]<=sum;j++)dp[j+val[i]]-=dp[j];
for(int j=0;j<=sum;j++){
if((sum-val[i]+j)%2==0&&dp[(sum-val[i]+j)/2])ans[j]&=true;
else ans[j]=false;
}
for(int j=0;j<=sum;j++)dp[j]=last[j];
}
int cnt=0;
for(int i=0;i<=sum;i++)if(ans[i])cnt++;
cout<<cnt<<'\n';
for(int i=0;i<=sum;i++)if(ans[i])cout<<i<<" ";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |