We'd like to thank you all for participating in the contest, and hope you enjoyed it. Hope to see you again soon!
Author: JainWinn
The expression $$$(x$$$ & $$$y)$$$ $$$+$$$ $$$(x \oplus y)$$$ $$$=$$$ $$$n$$$ reduces to $$$(x \ | \ y) = n $$$. So for each set bit in $$$n$$$ , we have $$$3$$$ possibilities:
- set in $$$x$$$ and unset in $$$y$$$
- unset in $$$x$$$ and set in $$$y$$$
- set in both $$$x$$$ and $$$y$$$
So answer is simply $$$3^b$$$ where $$$b$$$ is number of set bits in $$$n$$$ which can be calculated in $$$O(log_2(b))$$$ time and space complexity(recursive stack space included).
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define countbits(x) __builtin_popcount(x)
#define endl '\n'
int binpow(int x, int y)
{
int res = 1;
while (y > 0)
{
if (y % 2 == 1)
{
res = (res * x);
}
y = y >> 1;
x = (x * x);
}
return res;
}
void solve(){
int n;
cin>>n;
cout<<binpow(3,countbits(n))<<endl;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}
Author: JainWinn
Given $$$n$$$ poles and each pole can be used at most $$$k$$$ times , the answer is simply $$$(n*k)/3$$$ if $$$n \gt 2$$$ and $$$0$$$ otherwise . We can treat each pole as collection of $$$k$$$ poles such that our selection of poles for each triangle ensure that each pole belongs to different pole collection .
#include<bits/stdc++.h>
using namespace std;
//=======================================================================
#define int long long
#define endl '\n'
void solve(){
int n,k;
cin>>n>>k;
if(n<3){
cout<<0<<endl;
return;
}
cout<<(n*k)/3<<endl;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}
Author: JainWinn
This problem can be solved using priority queue or ordered set .
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
template <class T> using pbds = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update> ;
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
//=======================================================================
#define ll long long
#define int long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int,int>
#define vpi vector<pair<int,int>>
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
void printarray(vector<int> &a){
for(int i=0 ; i<(int)a.size() ; i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
void solve(){
int n;
cin>>n;
vi st(n,0);
for(int i=0 ; i<n ; i++){
cin>>st[i];
}
vi ed(n,0);
for(int i=0 ; i<n ; i++){
cin>>ed[i];
}
int m;
cin>>m;
vpi t;
for(int i=0 ; i<m ; i++){
int x;
cin>>x;
t.pb(make_pair(x,i));
}
vvi s;
for(int i=0 ; i<n ; i++){
s.pb({st[i],ed[i]});
}
sort(all(s));
sort(all(t));
int itr=0;
vi ans(m,0);
priority_queue<int,vector<int>,greater<int>> exam;
for(int i=0 ; i<m ; i++){
while((itr<n) && (s[itr][0] <= t[i].ff)){
exam.push(s[itr][1]);
itr++;
}
while(!exam.empty() && exam.top()<t[i].ff){
exam.pop();
}
int curr=exam.size();
ans[t[i].ss]=curr;
}
printarray(ans);
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
template <class T> using pbds = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update> ;
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
//=======================================================================
#define ll long long
#define int long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int,int>
#define vpi vector<pair<int,int>>
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
void printarray(vector<int> &a){
for(int i=0 ; i<(int)a.size() ; i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
void solve(){
int n;
cin>>n;
vi st(n,0);
for(int i=0 ; i<n ; i++){
cin>>st[i];
}
vi ed(n,0);
for(int i=0 ; i<n ; i++){
cin>>ed[i];
}
int m;
cin>>m;
vpi t;
for(int i=0 ; i<m ; i++){
int x;
cin>>x;
t.pb(make_pair(x,i));
}
vvi s;
for(int i=0 ; i<n ; i++){
s.pb({st[i],ed[i],i});
}
sort(all(s));
sort(all(t));
int itr=0;
vi ans(m,0);
pbds<pair<int,int>> exam;
for(int i=0 ; i<m ; i++){
while((itr<n) && (s[itr][0] <= t[i].ff)){
exam.insert({s[itr][1],s[itr][2]});
itr++;
}
int curr=exam.order_of_key({t[i].ff,-1});
ans[t[i].ss]=itr-curr;
}
printarray(ans);
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
solve();
}
//for(int i=1 ; i<=t ; i++){
// cout<<"Case #"<<i<<": ";
// solve();
//}
return 0;
}




