Question: Give you two sequences a and b, let you calculate a sequence of c, c[i]=a[i]&b[j] (the same element in b can be used multiple times), let you find c [1] | c[2] |… | The smallest value of c[n].
Idea: Enumerate the value of c (0-2^9) violently. vis[i][j] indicates that the number of first i can construct j. Just swipe the meter to show all the conditions.
var readline=require("readline");
var rl=readline.createInterface({
input:process.stdin,
output:process.stdout
});
var arr=[],a,b,n,m;
rl.on('line',function(inp){
arr.push(inp);
var len=arr.length;
if(len===1){
n=parseInt(arr[0].split(' ')[0]);
m=parseInt(arr[0].split(' ')[1]);
}else if(len===2){
a=arr[1].split(' ');
for(var i=0;i<n;i++) a[i]=parseInt(a[i]);
}else if(len===3){
b=arr[2].split(' ');
for(var i=0;i<m;i++) b[i]=parseInt(b[i]);
var C=Math.pow(2,9),vis=new Array(201);
for(var i=0;i<201;i++) vis[i]=new Array(C+1).fill(0);
for(var i=0;i<n;i++){
for(var j=0;j<m;j++){
var ab=a[i]&b[j];
for(var k=0;k<C+1;k++){
if(i>0&&vis[i-1][k]) vis[i][k|ab]=1;
}
if(i===0) vis[i][ab]=1;
}
}
for(var i=0;i<=C;i++){
if(vis[n-1][i]){
console.log(i);
process.exit(0);
}
}
}
});