莫比烏斯函數
阿新 • • 發佈:2018-02-27
target == include 定義域 src sqrt 因子 ace 自然數
轉自: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 }
莫比烏斯函數