Fu_L's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Fu-L/cp-library

:heavy_check_mark: moebius
(src/math/moebius.hpp)

moebius

int moebius(ll n)

メビウス関数値 $\mu(n)$ を返します.

制約

計算量

Depends on

Verified with

Code

#pragma once
#include "../template/template.hpp"
#include "./prime_factors.hpp"
int moebius(const long long n) {
    assert(n >= 1);
    if(n == 1) return 1;
    const vector<pair<long long, int>> p = prime_factors(n);
    int res = 1;
    for(const auto& it : p) {
        if(it.second >= 2) return 0;
        res = -res;
    }
    return res;
}
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
#line 3 "src/math/prime_factors.hpp"
vector<pair<long long, int>> prime_factors(long long n) {
    assert(n >= 1);
    vector<pair<long long, int>> res;
    for(long long i = 2; i * i <= n; ++i) {
        if(n % i == 0) {
            res.emplace_back(i, 0);
            while(n % i == 0) {
                n /= i;
                ++res.back().second;
            }
        }
    }
    if(n >= 2) res.emplace_back(n, 1);
    return res;
}
#line 4 "src/math/moebius.hpp"
int moebius(const long long n) {
    assert(n >= 1);
    if(n == 1) return 1;
    const vector<pair<long long, int>> p = prime_factors(n);
    int res = 1;
    for(const auto& it : p) {
        if(it.second >= 2) return 0;
        res = -res;
    }
    return res;
}
Back to top page