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
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 in $$$O(n)$$$ time and space complexity.
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;
}
Author: tt12
In how much time can a racer $$$A$$$ overtake a racer $$$B$$$?
Think about how we can calculate the total overtakes by a single racer against the opposite team in less than $$$O(n)$$$.
The time in which racer $$$A$$$ would overtake a racer $$$B$$$ one time would be $$$t = p / v_A - v_B$$$.
Total overtakes $$$(N) = d / t = (d / p) * (v_A - v_B)$$$.
Since $$$d$$$ is an integral multiple of $$$p$$$, the total number of overtakes would always be an integer. Also, the number of overtakes will be reduced by $$$1$$$ since the formula for $$$t$$$ actually calculates the time after which racer $$$A$$$ will again catch up with $$$B$$$. A small time $$$dT \gt 0$$$ would be required after each $$$t$$$ for a valid overtake to occur. Since this cannot occur after the last second, the last overtake would noṭ happen and racer $$$A$$$ would just reach racer $$$B$$$.
So, total overtakes = $$$N - 1$$$.
These overtakes can be calculated for all racers in $$$O(n^2)$$$, which is too slow.
We can deduce that a certain racer would always overtake a set of racers of the opposing racing team which would form a prefix array. With the help of the two-pointer method, we can optimise this solution to $$$O(n)$$$.
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define mod 998244353
void solve(){
ll n, p, d;
cin >> n >> p >> d;
vector<ll> a(n), b(n), pref_a(n), pref_b(n);
for(int i=0; i<n; i++){
cin >> a[i];
if(i) pref_a[i] = (pref_a[i - 1] + a[i]) % mod;
else pref_a[i] = a[i];
}
for(int i=0; i<n; i++){
cin >> b[i];
if(i) pref_b[i] = (pref_b[i - 1] + b[i]) % mod;
else pref_b[i] = b[i];
}
ll l = n - 1;
ll a_points = 0, b_points = 0;
ll k = d / p;
for(int i=n-1; i>=0; i--){
while(l > 0 && a[i] <= b[l]){
l--;
}
if(a[i] > b[l]){
ll count = l + 1;
a_points = (a_points + (((((count * a[i]) % mod - pref_b[l] + mod) % mod) * k) % mod - count + mod) % mod) % mod;
}
}
l = n - 1;
for(int i=n-1; i>=0; i--){
while(l > 0 && b[i] <= a[l]){
l--;
}
if(b[i] > a[l]){
ll count = l + 1;
b_points = (b_points + (((((count * b[i]) % mod - pref_a[l] + mod) % mod) * k) % mod - count + mod) % mod) % mod;
}
}
cout << a_points << ' ' << b_points << '\n';
}
int main(){
fastio();
int t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}



