kyopro-lib

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

View on GitHub

:warning: Möbius function
(Mylib/Number/mobius_function.cpp)

Operations

Requirements

Notes

Problems

References

Code

#pragma once
#include <vector>

namespace haar_lib {
  template <typename Checker>
  std::vector<int> mobius_function(int n, Checker is_prime) {
    std::vector<int> ret(n + 1), ps;

    ret[1] = 1;

    for (int i = 2; i <= n; ++i) {
      if (is_prime(i)) {
        ps.push_back(i);
        ret[i] = -1;
      }
      for (auto &j : ps) {
        if (i * j > n) break;
        if (i % j == 0)
          ret[i * j] = 0;
        else
          ret[i * j] = ret[i] * ret[j];
      }
    }

    return ret;
  }
}  // namespace haar_lib
#line 2 "Mylib/Number/mobius_function.cpp"
#include <vector>

namespace haar_lib {
  template <typename Checker>
  std::vector<int> mobius_function(int n, Checker is_prime) {
    std::vector<int> ret(n + 1), ps;

    ret[1] = 1;

    for (int i = 2; i <= n; ++i) {
      if (is_prime(i)) {
        ps.push_back(i);
        ret[i] = -1;
      }
      for (auto &j : ps) {
        if (i * j > n) break;
        if (i % j == 0)
          ret[i * j] = 0;
        else
          ret[i * j] = ret[i] * ret[j];
      }
    }

    return ret;
  }
}  // namespace haar_lib
Back to top page