基准时间限制:1 秒 空间限制:131072 KB 分值: 0
难度:基础题
莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出。梅滕斯(Mertens)首先使用μ(n)(miu(n))作为莫比乌斯函数的记号。(据说,高斯(Gauss)比莫比乌斯早三十年就曾考虑过这个函数)。
具体定义如下:
如果一个数包含平方因子,那么miu(n) = 0。例如:miu(4), miu(12), miu(18) = 0。
如果一个数不包含平方因子,并且有k个不同的质因子,那么miu(n) = (-1)^k。例如:miu(2), miu(3), miu(30) = -1,miu(1), miu(6), miu(10) = 1。
给出一个数n, 计算miu(n)。
Input
输入包括一个数n,(2 <= n <= 10^9)
Output
输出miu(n)。
Input示例
5
Output示例
-1
感觉自己的做法很傻...不过还是贴出来吧......
#include <bits/stdc++.h>
const int maxn=1e6;
using namespace std;
int a[maxn];
int isprime(int n)
{
double m=sqrt(n);
for(int i=2; i<=m; i++)
{
if(n%i==0)
{
return false;
}
}
return true;
}
int main()
{
int n,i;
cin>>n;
int ans=0;
if(sqrt(n)==(int)sqrt(n))
{
cout<<"0"<<endl;
}
else
{
int j=0;
for(i=2; i<=sqrt(n); i++)
{
if(n%i==0)
{
if(n/i==i)
a[j++]=i;
else
{
a[j++]=i;
a[j++]=n/i;
}
}
}
a[j++]=n;
bool ant=true;
for(i=0; i<j; i++)
{
if((int)sqrt(a[i])==sqrt(a[i]))
{
cout<<"0"<<endl;
ant=false;
break;
}
}
if(ant==true)
{
for(i=0; i<j; i++)
{
if(isprime(a[i]))
{
ans++;
}
}
if(ans&1)
{
cout<<"-1"<<endl;
}
else
cout<<"1"<<endl;
}
}
return 0;
}
大佬的做法:
#include <bits/stdc++.h>
using namespace std;
int n, m;
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 2; i * i <= n; i ++) {
if (n % i == 0) {
n /= i, m ++;
if (n % i == 0) {
puts("0");
return 0;
}
}
}
if (n != 1) m ++;
puts(m & 1 ? "-1" : "1");
return 0;
}