Advertisement
Sanath12345

Untitled

Jul 25th, 2021
2,316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. //Problem link :
  2. //CREDITS : sanath kumar
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. #define ll long long int
  6. #define ld long double
  7. #define mod 1000000007
  8. #define inf 1e9
  9. #define endl "\n"
  10. #define pb push_back
  11. #define vi vector<ll>
  12. #define vs vector<string>
  13. #define pii pair<ll,ll>
  14. #define ump unordered_map
  15. #define mp make_pair
  16. #define pq_max priority_queue<ll>
  17. #define pq_min priority_queue<ll,vector<ll>,greater<ll>>
  18. #define all(v) v.begin(),v.end()
  19. #define ff first
  20. #define ss second
  21. #define mid(l,r) (l+(r-l))/2
  22. #define bitc(x) __builtin_popcount(x)
  23. #define read(a,n) for(int i=0;i<n;i++) cin>>a[i];
  24. #define loop(i,a,b) for(int i=(a);i<=(b);i++)
  25. #define looprev(i,a,b) for(int i=(a);i>=(b);i--)
  26. #define iter(c,it) for(__typeof(c.begin()) it=c.begin();it!=c.end();it++)
  27. #define boost ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
  28. #define log(args...) {string _s=#args;replace(_s.begin(),_s.end(),',',' ');stringstream _ss(_s);istream_iterator<string> _it(_ss);err(_it,args);}
  29. #define logarr(arr,a,b) for(int z=(a);z<=(b);z++) cout<<(arr[z])<<" ";cout<<endl;
  30. template<typename T> void shiftr(T *arr,ll n){for(int i=n;i>=1;i--) arr[i]=arr[i-1];arr[0]=0;}
  31. template<typename T> T gcd(T a,T b) {if(b==0) return a; return gcd(b,a%b);}
  32. template<typename T> T lcm(T a,T b) {return a*(b/gcd(a,b));}
  33. template<class T> T add(T a,T b) {return ((a%mod)+(b%mod))%mod;}
  34. template<class T> T sub(T a,T b) {return ((a%mod)-(b%mod)+mod)%mod;}
  35. template<class T> T mul(T a,T b) {return ((a%mod)*(b%mod)*(1LL))%mod;}
  36. template<class T> void debug(set<T> S){cout<<"[ ";for(auto i:S) cout<<i<<" ";cout<<"] "<<endl;}
  37. template<class T> void debug(vector<T> v){cout<<"[ ";for(auto i:v) cout<<i<<" ";cout<<"] "<<endl;}
  38. template<class T> void debug(multiset<T> S){cout<<"[ ";for(auto i:S) cout<<i<<" ";cout<<"] "<<endl;}
  39. template<class T> void debug(unordered_set<T> S){cout<<"[ ";for(auto i:S) cout<<i<<" ";cout<<"] "<<endl;}
  40. template<class T,class X> void debug(T *arr,X s,X e){cout<<"[ ";loop(i,s,e) cout<<arr[i]<<" ";cout<<"] "<<endl;}
  41. template<class T,class V> void debug(pair<T,V> p){cout<<"{";cout<<p.ff<<" "<<p.ss<<"}"<<endl;}
  42. template<class T,class V> void debug(vector<pair<T,V>> v){cout<<"[ "<<endl;for(auto i:v) debug(i);cout<<"]"<<endl;}
  43. template<class T,class V> void debug(set<pair<T,V>> s){cout<<"[ "<<endl;for(auto i:s) debug(i);cout<<"]"<<endl;}
  44. template<class T,class V> void debug(multiset<pair<T,V>> s){cout<<"[ "<<endl;for(auto i:s) debug(i);cout<<"]"<<endl;}
  45. void err(istream_iterator<string> it){}
  46. template<typename T,typename... Args>
  47. void err(istream_iterator<string> it,T a,Args... args){
  48. cout<<*it<<" = "<<a<<endl;
  49. err(++it,args...);
  50. }
  51. ll *getfactorial(){
  52. int m=1e6;
  53. ll *fact=new ll[m+1];
  54. fact[0]=1;
  55. loop(i,1,m){
  56. fact[i]=(fact[i-1]*i)%mod;
  57. }
  58. // time complexity O(n)
  59. return fact;
  60. }
  61. vi *getfactors(){
  62. int n=2e5;
  63. vi *factors=new vi[n+5];
  64. loop(i,1,n){
  65. for(int j=i;j<=n;j+=i){
  66. factors[j].pb(i);
  67. }
  68. }
  69. // time complexity O(nlogn)
  70. return factors;
  71. }
  72. ll power(ll a,ll b,ll m=mod){
  73. if(b==0) return 1;
  74. ll smallans=power(a,b/2,m);
  75. ll ans=(smallans*smallans)%m;
  76. if(b%2==1){
  77. ans=(ans*a)%m;
  78. }
  79. return ans;
  80. // time complexity O(logb)
  81. }
  82. bool *isprime(){
  83. int m=1e6;
  84. bool *p=new bool[m+5];
  85. loop(i,0,m) p[i]=true;
  86. p[0]=false;
  87. p[1]=false;
  88. loop(i,2,sqrt(m)){
  89. for(int j=2*i;j<=m;j+=i){
  90. p[j]=false;
  91. }
  92. }
  93. // time complexity O(nlogn)
  94. return p;
  95. }
  96. ll *fac=getfactorial();
  97. ll nCr(ll n,ll r,int m=mod){
  98. if(n<r) return 0;
  99. ll ans=fac[n];
  100. ans=(ans*power(fac[n-r],m-2))%m;
  101. ans=(ans*power(fac[r],m-2))%m;
  102. return ans;
  103. }
  104. //==================START==================
  105. ll freq[15];
  106. ll n,k;
  107. vector<pair<ll,ll>> v[25];
  108. ll dp[25];
  109. ll solve(int i){
  110. if(i==n) return 0;
  111. if(dp[i]!=-1) return dp[i];
  112. ll ans=-1;
  113. for(int j=0;j<k;j++){
  114. if(freq[j]==2) continue;
  115. freq[v[i][j].ss]++;
  116. ans=max(ans,v[i][j].ff+solve(i+1));
  117. freq[v[i][j].ss]--;
  118. }
  119. return dp[i]=ans;
  120. }
  121. int main(){
  122. boost
  123. int t;
  124. cin>>t;
  125. while(t--){
  126. cin>>n>>k;
  127. loop(i,0,n-1) v[i].clear();
  128. ll a[n];
  129. read(a,n);
  130. loop(j,0,n-1){
  131. loop(i,1,k){
  132. int x=(a[j]&i);
  133. v[j].pb({x,i});
  134. }
  135. }
  136. loop(i,0,n-1) dp[i]=-1;
  137. cout<<solve(0)<<endl;
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement