CF955C Sad Powers
Hanx16Kira · · 题解
CF955C Sad powers
Luogu CF955C
Solution
2023 春赛观光团。
对于一个询问
考虑
记
对于一个数
代码实现的时候有一个很麻烦的东西,就是
Code
#include<bits/stdc++.h>
#define int long long
using namespace std;
int l, r;
int a[100005];
inline long double Qpow(int x, int y) {
long double res = 1, base = x;
for (; y; y >>= 1, base *= base) {
if (y & 1) {
res *= base;
}
}
return res;
}
inline int GetRoot(int x, int y) {
int v = powl(x, (long double)1.0 / y + 1e-9);
if (Qpow(v + 1, y) < x)
for (++v; Qpow(v + 1, y) < x; ++v);
if (Qpow(v, y) > x)
for (--v; Qpow(v, y) > x; --v);
return v;
}
int Calc(int n) {
if (n == 0) return 0;
int res = 1;
int lim = 0;
for (int i = 2; ; ++i) {
int v = GetRoot(n, i);
lim = i;
if (v <= 1) break;
a[i] = v - 1;
}
for (int i = lim - 1; i >= 2; --i) {
for (int j = i + i; j < lim; j += i) {
a[i] -= a[j];
}
}
for (int i = 2; i < lim; ++i) {
res += a[i];
}
return res;
}
void Solve() {
cin >> l >> r;
cout << Calc(r) - Calc(l - 1) << '\n';
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int T; cin >> T;
while (T--) Solve();
}