結果

問題 No.1262 グラフを作ろう!
ユーザー Mister
提出日時 2020-10-23 10:55:57
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 532 ms / 3,000 ms
コード長 841 bytes
コンパイル時間 888 ms
コンパイル使用メモリ 71,780 KB
最終ジャッジ日時 2025-01-15 12:48:34
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 96
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

using lint = long long;

void solve() {
    int n, m;
    std::cin >> n >> m;

    std::vector<lint> cnt(n + 1, 0);
    lint ans = 0;
    while (m--) {
        int x;
        std::cin >> x;
        ++cnt[x];
        ans -= x;
    }

    // 畳み込み
    for (int g = 1; g <= n; ++g) {
        for (int x = g * 2; x <= n; x += g) {
            cnt[g] += cnt[x] * x / g;
        }
    }
    // cnt[g]: gcd(a, b) | g なる(a, b)の個数

    // 包除
    for (int g = n; g >= 1; --g) {
        for (int x = g * 2; x <= n; x += g) {
            cnt[g] -= cnt[x];
        }
        ans += cnt[g] * g;
    }
    // cnt[g]: gcd(a, b) = g なる(a, b)の個数

    std::cout << ans << "\n";
}

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

    solve();

    return 0;
}
0