C. And Matching
You are given a set of ( is always a power of ) elements containing all integers exactly once.
Find pairs of elements such that:
- Each element in the set is in exactly one pair.
- The sum over all pairs of the bitwise AND of its elements must be exactly equal to . Formally, if and are the elements of the -th pair, then the following must hold:where denotes the bitwise AND operation.
If there are many solutions, print any of them, if there is no solution, print instead.
The input consists of multiple test cases. The first line contains a single integer () — the number of test cases. Description of the test cases follows.
Each test case consists of a single line with two integers and (, is a power of , ).
The sum of over all test cases does not exceed . All test cases in each individual input will be pairwise different.
For each test case, if there is no solution, print a single line with the integer .
Otherwise, print lines, the -th of them must contain and , the elements in the -th pair.
If there are many solutions, print any of them. Print the pairs and the elements in the pairs in any order.
In the first test, .
In the second test, .
In the third test, .
In the fourth test, there is no solution.
Solution:
- #include<bits/stdc++.h>
- using namespace std;
- #define int long long
- #define fi first
- #define se second
- #define pii pair<long long,long long>
- #define mp make_pair
- #define pb push_back
- const int mod=998244353;
- const int inf=0x3f3f3f3f;
- const int INF=1e18;
- int fpow(int x,int b){
- if(x==0) return 0;
- if(b==0) return 1;
- int res=1;
- while(b>0){
- if(b&1) res=res*x%mod;
- x=x*x%mod;
- b>>=1;
- }
- return res;
- }
- int fac[300005];
- int C(int x,int y)
- {
- return fac[y]*fpow(fac[y-x],mod-2)%mod*fpow(fac[x],mod-2)%mod;
- }
- int n;
- int a[200005];
- int t[800005];
- void update(int id,int l,int r,int x,int d)
- {
- if(l==r)
- {
- t[id]=d;
- return;
- }
- int mid=(l+r)>>1;
- if(x<=mid) update(id<<1,l,mid,x,d);
- else update(id<<1|1,mid+1,r,x,d);
- t[id]=min(t[id<<1],t[id<<1|1]);
- }
- int query(int id,int l,int r,int x,int y)
- {
- if(x>y) return INF;
- if(x<=l&&r<=y) return t[id];
- int mid=(l+r)>>1;
- int res=INF;
- if(x<=mid) res=min(res,query(id<<1,l,mid,x,y));
- if(y>mid) res=min(res,query(id<<1|1,mid+1,r,x,y));
- return res;
- }
- int dp[200005];
- vector <pii > vec;
- int l[200005],r[200005];
- bool cmp(pii x,pii y)
- {
- if(x.se==y.se) return x.fi<y.fi;
- else return x.se<y.se;
- }
- void solve()
- {
- memset(t,0x3f,sizeof(t));
- memset(dp,0x3f,sizeof(dp));
- memset(l,0x3f,sizeof(l));
- memset(r,-0x3f,sizeof(r));
- scanf("%lld",&n);
- for(int i=1;i<=n;i++) scanf("%lld",&a[i]),l[a[i]]=min(l[a[i]],i),r[a[i]]=max(r[a[i]],i);
- for(int i=1;i<=n;i++) if(l[i]<=r[i]) vec.pb(mp(l[i],r[i]));
- sort(vec.begin(),vec.end(),cmp);
- dp[0]=0;
- for(int i=1;i<=n;i++) dp[i]=i,update(1,1,n,i,i);
- for(int i=0;i<vec.size();i++)
- {
- if(vec[i].fi==vec[i].se) dp[vec[i].se]=min(dp[vec[i].se],dp[vec[i].fi-1]+1);
- else dp[vec[i].se]=min(dp[vec[i].se],dp[vec[i].fi-1]+2);
- dp[vec[i].se]=min(dp[vec[i].se],query(1,1,n,vec[i].fi+1,vec[i].se-1)+1);
- update(1,1,n,vec[i].se,dp[vec[i].se]);
- }
- cout<<n-dp[n];
- }
- signed main()
- {
- int _=1;
- //cin>>_;
- while(_--) solve();
- return 0;
- }
when you will give solution?
ReplyDeleteSolution come yesterday at 9:30PM .but you have to follow our blog to get notification of solution .if you follow our blog then get notification when answer in updated in blog.so click on follow button.also follow on our youtube channel for furture notification.
Deletewhen you will give solution?
ReplyDeleteSolution come yesterday at 9:30PM .but you have to follow our blog to get notification of solution .if you follow our blog then get notification when answer in updated in blog.so click on follow button.also follow on our youtube channel for furture notification.
Delete