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.
|
|
|
|
#include <bits/stdc++.h>
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
#define int long long
|
|
|
|
|
#define endl "\n"
|
|
|
|
|
|
|
|
|
|
const int N = 1e5 + 10;
|
|
|
|
|
int n = 20;
|
|
|
|
|
|
|
|
|
|
// 筛法求莫比乌斯函数(枚举约数)
|
|
|
|
|
int mu[N], sum[N]; // sum[N]:梅滕斯函数,也就是莫比乌斯函数的前缀和
|
|
|
|
|
int 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];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 维护u(x)前缀和:梅滕斯函数
|
|
|
|
|
for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + mu[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 最简筛法求莫比乌斯函数(枚举倍数)
|
|
|
|
|
void get_mobius_beishu(int x) {
|
|
|
|
|
mu[1] = 1;
|
|
|
|
|
for (int i = 1; i <= x; i++)
|
|
|
|
|
for (int j = i + i; j <= x; j += i)
|
|
|
|
|
mu[j] -= mu[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 单个数的莫比乌斯函数
|
|
|
|
|
int getmob(int x) {
|
|
|
|
|
int sum = 0;
|
|
|
|
|
for (int i = 2; i <= x / i; i++) { // 从2开始,一直到 sqrt(x),枚举所有可能存的小因子
|
|
|
|
|
int cnt = 0;
|
|
|
|
|
if (x % i == 0) { // 如果x可以整除i
|
|
|
|
|
while (x % i == 0) cnt++, x /= i; // 计数,并且不断除掉这个i因子
|
|
|
|
|
if (cnt > 1) return 0; // 如果某个因子,存在两个及以上个,则返回0
|
|
|
|
|
sum++; // 记录因子个数
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (x != 1) sum++; // 如果还存在另一个大因子,那么因子个数+1
|
|
|
|
|
return (sum & 1) ? -1 : 1; // 奇数个因子,返回-1,否则返回1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void printIdx(int n) {
|
|
|
|
|
for (int i = 1; i <= n; i++) printf("%2lld ", i);
|
|
|
|
|
cout << endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
signed main() {
|
|
|
|
|
// 计算单个数字的莫比乌斯函数
|
|
|
|
|
for (int i = 1; i <= n; i++) printf("%2lld ", getmob(i));
|
|
|
|
|
cout << endl;
|
|
|
|
|
|
|
|
|
|
// 枚举约数的筛法
|
|
|
|
|
get_mobius(n);
|
|
|
|
|
for (int i = 1; i <= n; i++) printf("%2lld ", mu[i]);
|
|
|
|
|
cout << endl;
|
|
|
|
|
|
|
|
|
|
// 清空一下,继续测试
|
|
|
|
|
memset(mu, 0, sizeof mu);
|
|
|
|
|
|
|
|
|
|
// 枚举倍数的筛法
|
|
|
|
|
get_mobius_beishu(n);
|
|
|
|
|
for (int i = 1; i <= n; i++) printf("%2lld ", mu[i]);
|
|
|
|
|
cout << endl;
|
|
|
|
|
|
|
|
|
|
printIdx(n);
|
|
|
|
|
}
|