結果

問題 No.3691 Calculate Mu Sum
コンテスト
ユーザー shingo0909
提出日時 2026-09-06 18:10:47
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 174 ms / 2,000 ms
+ 500µs
コード長 2,789 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,155 ms
コンパイル使用メモリ 336,848 KB
実行使用メモリ 124,732 KB
最終ジャッジ日時 2026-09-06 18:11:01
合計ジャッジ時間 4,006 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 11
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

// https://github.com/shingo0909/kyopro/blob/39029fab39c1735c38e5e9e4494d9934f81d6918/Library/PrimeTable.cpp

struct PrimeTable {
  private:
    int n;
    vector<int> spf;
    vector<int> primes;
    vector<int> phi_table;
    vector<int> mu_table;

    void build() {
        spf[0] = -1;
        spf[1] = -1;
        phi_table[1] = 1;
        mu_table[1] = 1;

        for (int i = 2; i <= n; i++) {
            if (spf[i] == 0) {
                spf[i] = i;
                primes.push_back(i);
                phi_table[i] = i - 1;
                mu_table[i] = -1;
            }
            for (int p : primes) {
                if (p > spf[i] || 1LL * p * i > n)
                    break;
                spf[p * i] = p;
                if (p == spf[i]) {
                    phi_table[p * i] = phi_table[i] * p;
                    mu_table[p * i] = 0;
                } else {
                    phi_table[p * i] = phi_table[i] * (p - 1);
                    mu_table[p * i] = -mu_table[i];
                }
            }
        }
        return;
    }

  public:
    PrimeTable(int n) : n(n),
                        spf(n + 1, 0),
                        phi_table(n + 1, 0),
                        mu_table(n + 1, 0) {
        build();
    }

    bool is_prime(int x) const {
        return x >= 2 && spf[x] == x;
    }

    // 素因数分解
    vector<pair<int, int>> factorize(int x) const {
        vector<pair<int, int>> res;
        while (x > 1) {
            int p = spf[x];
            int cnt = 0;
            while (x % p == 0) {
                x /= p;
                cnt++;
            }
            res.emplace_back(p, cnt);
        }
        return res;
    }

    // 約数列挙
    // ソートされていないことに注意
    vector<int> divisors(int n) const {
        vector<int> res{1};
        auto pf = factorize(n);
        for (auto p : pf) {
            int s = (int)res.size();
            for (int i = 0; i < s; i++) {
                int v = 1;
                for (int j = 0; j < p.second; j++) {
                    v *= p.first;
                    res.push_back(res[i] * v);
                }
            }
        }
        return res;
    }

    // オイラーのトーシェント関数 φ(x)
    long long phi(int x) const {
        return phi_table[x];
    }
    // メビウス関数 μ(x)
    int mu(int x) const {
        return mu_table[x];
    }
};

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    int n;
    cin >> n;
    PrimeTable p(n + 10);
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
        ans += p.mu(i);
    }
    cout << ans << endl;

    return 0;
}
0