Official

C - Unexpressed Editorial by en_translator


Since \(N ≤ 10^{10}\), we cannot check every integers from \(1\) through \(N\).
Notice that there are very few integers that can be represented as \(a^b\).
The number of pairs of integers \((a, b)\) such that both \(a\) and \(b\) are at least \(2\) and that \(a^b ≤ N\) are

\[ \sum_{x=2}^{\lfloor\log_2N\rfloor}(\lfloor\sqrt[x]{N}\rfloor - 1) ≤ 102719, \]

so we can brute force every \(a, b\) in the range of \(a^b ≤ N\), manage those integers represented as \(a^b\) with HashSet, and thus solve the problem.

Sample Code (C++)

#include <iostream>
#include <unordered_set>
using namespace std;
using ll = int64_t;

int main(){
    ll N;
    cin >> N;
    unordered_set<ll> s;
    for(ll a = 2; a * a <= N; a++){
        ll x = a * a;
        while(x <= N){
            s.insert(x);
            x *= a;
        }
    }
    cout << N - s.size() << endl;
}

Sample Code (Python)

N = int(input())
sq = int(N ** 0.5)
s = set()
for a in range(2, sq + 1):
    x = a * a
    while x <= N:
        s.add(x)
        x *= a
print(N - len(s))

posted:
last update: