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;
}



