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 <bits/stdc++.h>
using namespace std;
long long dp[500*500+1];
long long mod = int(1e9+7);
bitset<500*500+1> su;
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n;
cin>>n;
int arr[n] , sum = 0;
for(int i = 0;i<n;i++){
cin>>arr[i];sum+=arr[i];
}
dp[0] = 1;
for(int i = 0;i<n;i++){
for(int j = sum;j>=arr[i];j--){
dp[j]+=dp[j-arr[i]];
if(dp[j]>=1e18)dp[j]%=mod;
}
}
if(sum%2||(!dp[sum/2])){
cout<<"0";
return 0;
}
for(int i = (arr[0]%2?1:2);i<=sum;i+=2){su[i] = 1;}
for(int it = 0;it<n;it++){
for(int j = arr[it];j<=sum;j++){
dp[j]-=dp[j-arr[it]];
dp[j]%=mod;
if(dp[j]<0){dp[j]%=mod;dp[j]+=mod;dp[j]%=mod;}
}
for(int j = (arr[0]%2?1:2);j<=sum;j+=2){
if((sum-arr[it]+j)%2){
su[j] = 0;
continue;
}
int ne = sum-arr[it]+j;
int d = 0;
if(ne/2<=sum)d = (dp[ne/2]>0);
else break;
if(j<=ne/2&&(ne/2)-j<=sum)d|=(dp[(ne/2)-j]>0);
if(d==0)su[j] = 0;
}
for(int j = sum;j>=arr[it];j--){
dp[j]+=dp[j-arr[it]];
if(dp[j]>=1e18)dp[j]%=mod;
}
}
cout<<su.count()<<"\n";
for(int i = 1;i<=sum;i++)if(su[i])cout<<i<<" ";
}
# | 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... |