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
/*
    data:2018.5.12
    author:gsw
    link:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1240
    account:[email protected]
*/
#define ll long long
#define IO ios::sync_with_stdio(false);

#include<math.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
using namespace std;
int prime(int n)
{
    int m=sqrt(n)+1;
    int kk=0;
    for(int i=2;i<=m;i++)
    {
        if(n%i==0)
        {
            int cnt=0;
            while(n%i==0)
            {
                n=n/i;
                cnt++;
                if(cnt>=2)return 0;
            }
            kk++;
        }
    }
    if(n>1)kk++;
    if(kk%2==0)return 1;
    return -1;
}
int main()
{
    int n;
    scanf("%d",&n);
    printf("%d\n",prime(n));
    //main();
    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/fantastic123/p/9029973.html