1. 程式人生 > >51nod 1240 莫比烏斯函數 (質因數分解)

51nod 1240 莫比烏斯函數 (質因數分解)

mage ans return 空間 clu 使用 lap 技術 com

1240 莫比烏斯函數技術分享 基準時間限制: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
質因數分解模板題

AC代碼:
技術分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 using namespace std;
 5 const int maxsum=1e6;
 6 int flag=0;
 7 int factor(int x)
 8 {
 9     int ret=0;
10     int tmp=(int)(double(sqrt(x)+1));
11     for(int i=2
;i<=tmp;i++) 12 if(x%i==0) 13 { 14 ret++; 15 int sum=0; 16 while(x%i==0) 17 { 18 x/=i; 19 sum++; 20 if(sum>=2) 21 { 22 flag=1; 23 return
0; 24 } 25 } 26 } 27 if(x!=1) 28 ret++; 29 return ret; 30 } 31 int main() 32 { 33 int n; 34 cin>>n; 35 flag=0; 36 int ans=factor(n); 37 if(flag) 38 cout<<0<<endl; 39 else 40 printf("%d\n",ans%2==0?1:-1); 41 return 0; 42 }
View Code

 

51nod 1240 莫比烏斯函數 (質因數分解)