1891D - Suspicious logarithms - CodeForces Solution


binary search math

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>

using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

using ll = long long;
using ld = long double;
using ull = unsigned long long;

const int mod = 1e9 + 7;
const int MAX = 5e5 + 7;


ll g(ll x) {
    ll pw = __lg(x);
    if (pw <= 1) return 0;
    ll cnt = 1;
    while (x / pw >= __lg(x)) {
        cnt++;
        pw *= __lg(x);
    }
    return cnt;
}

vector<pair<ll, ll>> fs;

ll f(ll x) {
    ll res = 0;
    for (int i = 0; i < (int)(fs.size()) - 1; i++) {
        ll l = fs[i].first;
        ll r = fs[i + 1].first;
        if (l == r) continue;
        r--;
        r = min(r, x);
        if (l > r) continue;
        r = r - l + 1;
        r %= mod;
        res += (r * fs[i].second) % mod;
        res %= mod;
    }
    return res;
}

void solve() {
    for (ll lg = 2; lg <= 60; lg++) {
        ll pw = 1;
        for (ll cnt = 1; cnt <= 80; cnt++) {
            pw *= lg;
            if (__lg(pw) == lg) {
                if (pw > 4) {
                    fs.push_back({pw - 1, g(pw - 1)});
                }
                fs.push_back({pw + 1, g(pw + 1)});
                fs.push_back({pw, g(pw)});
            }
        }
        fs.push_back({(1LL << lg), g((1LL << lg))});
        // cout << '\n';
    }
    sort(fs.begin(), fs.end());
    int t;
    cin >> t;
    while (t--) {
        ll l, r;
        cin >> l >> r;
        ll r1 = f(r);
        ll r2 = f(l - 1);
        r1 -= r2;
        while (r1 < 0) r1 += mod;
        cout << r1 << '\n';
    }
}



int main() {
#ifdef __APPLE__
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0); cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals