Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New codeforces dynamic programing problem added ps name -> F1. Korney Korneevich and XOR (easy version) #776

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define ll long long int

template<typename T>
T maxt(T a,T b){return (a>b)?a:b;}
template<typename T>
T mint(T a,T b){return (a<b)?a:b;}

int main() {

ll test_case_number=1;

for(ll test=0;test<test_case_number;test++){

ll n;
cin>>n;
vector<ll>arr(n+1);// take input 1 - indexed
for(ll i=1;i<=n;i++)cin>>arr[i];

vector<vector<ll>>dp(n+1,vector<ll>(513,-1)); // dp[i][x] represent the
// minimum value of the last picked element in the subsequence formed from first i
// elements of array such that the xor of all of them equals to x.


for(ll i=1;i<=n;i++){
for(ll x=0;x<=512;x++){

if(arr[i]>=dp[i-1][x] && dp[i-1][x]!=-1){ // if we can take the element

if(dp[i][x^arr[i]]==-1)dp[i][x^arr[i]]=arr[i]; //checks if the value is -1, if so we just add the new number else
else dp[i][x^arr[i]]=mint<ll>(dp[i][x^arr[i]],arr[i]); // we take the minimum of the two values

}


if(dp[i-1][x]!=-1){ // if we cannot take the element

if(dp[i][x]==-1)dp[i][x]=dp[i-1][x]; // value remains same if it is -1 else
else dp[i][x]=mint<ll>(dp[i-1][x],dp[i][x]); // take the minimum of the two values

}


if(dp[i][arr[i]]==-1)dp[i][arr[i]]=arr[i]; // if we are starting a new subsequence from i th position.
else dp[i][arr[i]]=mint<ll>(dp[i][arr[i]],arr[i]); // take the minimum of the two values

}
}
set<ll>ans;
ans.insert(0); // base case

for(ll i=1;i<=n;i++){
for(ll x=0;x<=512;x++){
if(dp[i][x]!=-1){
ans.insert(x); // take all possible values.
}
}
}
// output
cout<<ans.size()<<endl;
for(auto nei:ans){
cout<<nei<<" ";
}
cout<<endl;

}
return 0;
}