Files
python/TangDou/AcWing/RongChi/HDU2204_2.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

58 lines
1.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
const int N = 110;
/*
莫比乌斯系数简化计算
在上一个版本中,我们是按照奇加偶减的原则来进行的,同样这个计算的过程可以通过莫比乌斯中的mu函数来 **直接算出**,
每次相乘的系数是 mu[i]。
题意:要你输出1到n中,能够表示成a^b的数,a,b都是大于0的整数的个数,其中b大于1。
思路:
n以内可以表示为x^2的数有 pow(n,1/2)个
n以内可以表示为x^3的数有 pow(n,1/3)个
这两种里面重复的是 x^6 ,( 在(x^2)^3 和 (x^3)^2 )里面各计算一次
所以就需要减去 n以内可以表示为x^6的数,有pow(n,1/6)个
这样进行下去他们的容斥系数就是莫比乌斯函数
执行时间:15MS
*/
// 筛法求莫比乌斯函数
int mu[N], primes[N], cnt;
bool st[N];
void get_mobius(int n) {
mu[1] = 1;
for (int i = 2; i <= n; i++) {
if (!st[i]) {
primes[cnt++] = i;
mu[i] = -1;
}
for (int j = 0; primes[j] <= n / i; j++) {
int t = primes[j] * i;
st[t] = true;
if (i % primes[j] == 0) {
mu[t] = 0;
break;
}
mu[t] = -mu[i];
}
}
}
signed main() {
// 筛法求莫比乌斯函数
get_mobius(N - 1);
int n;
while (cin >> n) {
int s = 1;
// 对于1e18次方的话,最多就是2的64次方,逐个枚举2的i次方
for (int i = 2; i <= 64; i++)
s -= mu[i] * (int)(pow(n, 1.0 / i) - 1);
cout << s << endl;
}
}