莫比乌斯函数

转自:http://www.cnblogs.com/Leonard-/p/8403678.html

莫比乌斯函数数学定义:

通俗表达:

  1)莫比乌斯函数μ(n)的定义域是N(N为自然数集)

  2)μ(1)=1

  3)当n存在平方因子时,μ(n)=0 (例如4,9,16即为平方因子)

  4)当n是素数或奇数个不同素数之积时,μ(n)=-1 

  5)当n是偶数个不同素数之积时,μ(n)=1

例题:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1240

根据定义模拟过程

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 using namespace std;
 5 
 6 int miu(int n){
 7     if(n==1)
 8         return 1;
 9     int k=1;
10     int lim=sqrt(n);
11     for(int i=2;i<=lim;i++){
12         if(n%i==0){
13             k++;
14             n/=i;
15             if(n%i==0) return 0;
16         }
17     }
18     if(k&1)
19         return -1;
20     return 1;
21 }
22 
23 int main(){
24     int n;
25     while(cin>>n){
26         cout<<miu(n)<<endl;
27     }
28     return 0;
29 }
原文地址:https://www.cnblogs.com/fu3638/p/8479042.html