Exactly bhai , dp ki koi jarurat nhi hai, jab tk koi aisi bit na aye jisme (1ll<<i)&m==0 ho tb tk us stack ki book add karte jao, aise hi teeno stack mei karo, last me check karna hai ki m ban gaya ki nhi
#include <bits/stdc++.h>
using namespace std;
bool fun(int i1,int i2,int i3,int n,long long curr,int m,int arr1[],int arr2[],int arr3[])
{
if(curr==m) return true;
if(curr>m or i1>=n or i2>=n or i3>=n) return false;
if(fun(i1+1,i2,i3,n,curr | arr1[i1],m,arr1,arr2,arr3)) return true;
if(fun(i1,i2+1,i3,n,curr | arr2[i2],m,arr1,arr2,arr3)) return true;
if(fun(i1,i2,i3+1,n,curr | arr3[i3],m,arr1,arr2,arr3)) return true;
return false;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
int arr1[n],arr2[n],arr3[n];
for(int i=0;i<n;i++) cin>>arr1[i];
for(int i=0;i<n;i++) cin>>arr2[i];
for(int i=0;i<n;i++) cin>>arr3[i];
if(fun(0,0,0,n,0,m,arr1,arr2,arr3)) cout<<"Yes"<<"\n";
else cout<<"No"<<"\n";
}
return 0;
}
Do we really need dp here? I mean, we can just do brute force to find the maximum depth that we can go into each stack just by maintaining for a given depth, does any set bit in 'm' appears in the OR of the preffix. Once we know the maximum depth, we can just tak the OR of all the numbers in every stack at lower depths than the maximum allowed depth. In the end if the result is equal to m then the answer is "Yes" else it is "No". Is this correct?
TC (4*n)