Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth Odd divisors problem solution

YASH PAL, 31 July 2024
In this HackerEarth Odd divisors problem solution you are given a positive integer N.f(N) is the greatest odd divisor of N. Find the sum (f(1) + f(2) + … + f(N))%M.
HackerEarth Odd divisors problem solution

HackerEarth Odd divisors problem solution.

#include<bits/stdc++.h>
using namespace std;

int T;
long long N, M;

long long sqr(long long x){
x %= M;
return x * x % M;
}

int main(){

cin >> T;
while(T-->0){
cin >> N >> M;
long long ans = 0;
while(N){
ans = (ans + sqr(N / 2 + N % 2)) % M;
N /= 2;
}
cout << ans << 'n';
}

return 0;
}

second solution

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int maxn = 2e5 + 14;
template<class cmp> struct LS{
vector<int> v;
vector<pair<int, int> > his;
void add(int x){
int p = lower_bound(v.begin(), v.end(), x, cmp()) - v.begin();
if(p == v.size()){
his.push_back({-1, 0});
v.push_back(x);
}
else{
his.push_back({p, v[p]});
v[p] = x;
}
}
void swap(LS<cmp> &od){ od.v.swap(v); }
void merge(LS<cmp> &od){
if(od.v.size() > v.size())
v.swap(od.v);
for(int i = 0; i < od.v.size(); i++)
if(cmp()(od.v[i], v[i])) v[i] = od.v[i];
}
void undo(){
assert(his.size());
if(his.back().first == -1)
v.pop_back();
else
v[his.back().first] = his.back().second;
his.pop_back();
}
};
typedef LS<less<int> > Lis;
int p[maxn];
int main(){
ios::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
for(int i = 0; i < n; i++){
cin >> p[i];
}
Lis ans;
for(int i = 0; i < n; i++)
ans.add(p[n - i - 1]);
cout << ans.v.size() << 'n';
}
coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes