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 bits set in $$$n$$$ .
#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
Considering $$$0-$$$ based indexing , for each index $$$i$$$ $$$(1 \leq i \leq n-2 )$$$ we can find out number of ordered pairs $$$(x,y)$$$ for which $$$a_i$$$ is the V-Score . We find for each $$$i$$$, the index $$$l_i$$$ such that $$$a_{l_i}$$$ is the previous smaller or equal element and $$$r_i$$$ such that $$$a_{r_i}$$$ is the next smaller element of $$$a_i$$$ . This can easily be done using stack .
Number of ordered pairs for which $$$a_i$$$ is the V-Score is given by : $$$(i-l_i) \times (r_i-i)$$$ . So the answer is given by :
$$$\sum_{i=1}^{n-2}V_i$$$ where $$$V_i=(i-l_i) \times (r_i-i) \times a_i$$$ .
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define endl '\n'
//Previous smaller or equal Element
vector<int> prevSmaller(vi &arr)
{
stack<int> s;
s.push(0);
vi prevg(arr.size(),0);
for (int i = 1; i < arr.size(); i++) {
while (s.empty() == false && arr[s.top()] >= arr[i]){
s.pop();
}
if(!s.empty()){
prevg[i]=s.top();
}
s.push(i);
}
return prevg;
}
//Next smaller Element
vector<int> nextSmaller(vi &arr)
{
stack<int> s;
s.push(arr.size()-1);
vi nextg(arr.size(),arr.size()-1);
for (int i = arr.size()-2 ; i>=0 ; i--) {
while (s.empty() == false && arr[s.top()] > arr[i]){
s.pop();
}
if(!s.empty()){
nextg[i]=s.top();
}
s.push(i);
}
return nextg;
}
void solve(){
int n;
cin>>n;
vi a(n,0);
for(int i=0 ; i<n ; i++){
cin>>a[i];
}
vi left=prevSmaller(a);
vi right=nextSmaller(a);
int ans=0;
for(int i=1 ; i<n-1 ;i++){
ans+=(right[i]-i)*(i-left[i])*a[i];
}
cout<<ans<<endl;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}



