51Nod 1240:莫比乌斯函数

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代码

直接暴力查找n的因子情况就行了

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ull unsigned long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
inline int check(int n)
{
	int k=2;
	int res=0;
	while(k*k<=n)
	{
		int _=0;
		if(n%k==0)
		{
			res++;
			while(n%k==0)
			{
				n/=k;
				_++;
			}
			if(_>1)
				return 0;
		}
		k++;
	}
	if(res&1)
		return 1;
	else
		return -1;
}
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	int n;
	cin>>n;
	if(n==1)
	{
		cout<<1<<endl;
		return 0;
	}
	cout<<check(n)<<endl;
	return 0;
}
原文地址:https://www.cnblogs.com/Friends-A/p/10324350.html