zoukankan      html  css  js  c++  java
  • 51nod 1240 莫比乌斯函数

    莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出。梅滕斯(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)。
     
     

    输入

    输入包括一个数n,(2 <= n <= 10^9)

    输出

    输出miu(n)。

    输入样例

    5

    输出样例

    -1

    从2开始看,能整除就一直除如果可以除超过一次,直接返回,否则记录质数因子的个数。
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    using namespace std;
    int miu(int n) {
        if(n == 1) return 1;
        int c = 0,e = 0;
        for(int i = 2;i * i <= n;i ++) {
            if(n % i == 0) {
                e ++;
                int c = 0;
                while(n && n % i == 0) {
                    c ++;
                    n /= i;
                    if(c > 1) return 0;
                }
            }
        }
        if(n) e ++;
        return e % 2 ? -1 : 1;
    }
    int main() {
        int c = 0;
        int n;
        scanf("%d",&n);
        printf("%d
    ",miu(n));
    }
  • 相关阅读:
    nmake不是内部或外部命令,也不是可运行的程序
    MinGW下载和安装教程
    Qt接单
    C++ web 框架
    原型链
    ssh: Could not resolve hostname的一种解决方案
    oracle客户端安装配置
    linux安装go
    golang 为什么结构体方法要写成指针传递
    traceback模块
  • 原文地址:https://www.cnblogs.com/8023spz/p/9948514.html
Copyright © 2011-2022 走看看