You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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;
}
}