zoukankan      html  css  js  c++  java
  • 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

    思路:在线求μ(d)

    代码:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 ll mubi(ll n) {
     5     ll mu=1;
     6     for(ll i=2;i*i<=n;++i) {
     7         if(n%i==0) {
     8             mu*=-1;
     9             ll k=0;
    10             do {
    11                 k++;
    12                 if(k>1) {
    13                     mu=0;break;
    14                 }
    15                 n/=i;
    16             }while(n%i==0);
    17         } 
    18     }
    19     if(n>1) mu*=-1;
    20     return mu;
    21 }
    22 int main() {
    23     ios::sync_with_stdio(false);
    24     ll n;
    25     cin>>n;
    26     cout<<mubi(n)<<endl;
    27     return 0;
    28 }
    View Code
  • 相关阅读:
    [loj6271]生成树求和
    [cf1209E]Rotate Columns
    [cf1491H]Yuezheng Ling and Dynamic Tree
    [atARC064F]Rotated Palindromes
    [cf1491G]Switch and Flip
    [cf1491F]Magnets
    [atARC063F]Snuke's Coloring 2
    [atARC062F]Painting Graphs with AtCoDeer
    [atARC061F]Card Game for Three
    [atARC112E]Rvom and Rsrev
  • 原文地址:https://www.cnblogs.com/lemonbiscuit/p/7801291.html
Copyright © 2011-2022 走看看