結果

問題 No.2552 Not Coprime, Not Divisor
ユーザー coindarwcoindarw
提出日時 2024-02-11 14:20:33
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 227 ms / 2,000 ms
コード長 1,386 bytes
コンパイル時間 3,170 ms
コンパイル使用メモリ 252,256 KB
実行使用メモリ 11,176 KB
最終ジャッジ日時 2024-02-11 14:20:40
合計ジャッジ時間 6,673 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
11,176 KB
testcase_01 AC 172 ms
11,176 KB
testcase_02 AC 81 ms
11,176 KB
testcase_03 AC 159 ms
11,176 KB
testcase_04 AC 161 ms
11,176 KB
testcase_05 AC 39 ms
11,176 KB
testcase_06 AC 170 ms
11,176 KB
testcase_07 AC 188 ms
11,176 KB
testcase_08 AC 134 ms
11,176 KB
testcase_09 AC 227 ms
11,176 KB
testcase_10 AC 74 ms
11,176 KB
testcase_11 AC 118 ms
11,176 KB
testcase_12 AC 192 ms
11,176 KB
testcase_13 AC 88 ms
11,176 KB
testcase_14 AC 63 ms
11,176 KB
testcase_15 AC 14 ms
11,176 KB
testcase_16 AC 13 ms
11,176 KB
testcase_17 AC 54 ms
11,176 KB
testcase_18 AC 14 ms
11,176 KB
testcase_19 AC 14 ms
11,176 KB
testcase_20 AC 14 ms
11,176 KB
testcase_21 AC 14 ms
11,176 KB
testcase_22 AC 14 ms
11,176 KB
testcase_23 AC 13 ms
11,176 KB
testcase_24 AC 13 ms
11,176 KB
testcase_25 AC 13 ms
11,176 KB
testcase_26 AC 14 ms
11,176 KB
testcase_27 AC 14 ms
11,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using ll = long long;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define all(v) begin(v), end(v)
using namespace std;

vector<int> min_pf;

void prime_factorize_init(int n) {
    min_pf.resize(n + 1, 0);
    for (int i = 2; i <= n; i++) {
        if (min_pf.at(i) == 0) {
            for (int mul = i; mul <= n; mul += i) {
                min_pf.at(mul) = i;
            }
        }
    }
}

vector<int> prime_factorize(int n) {
    vector<int> res;
    if (n <= 1) return res;
    while (n != 1) {
        res.push_back(min_pf.at(n));
        n /= min_pf.at(n);
    }
    res.erase(unique(res.begin(), res.end()), res.end());
    return res;
}

constexpr int N = 1000'000;
int main() {
    prime_factorize_init(N);

    int n;
    cin >> n;

    vector<int> cnt(N + 1);

    ll ans = ll(n) * (n - 1) / 2 - (n - 1);
    for (int i = n; i > 1; --i) {
        ans -= (n / i) - 1;

        auto pf = prime_factorize(i);
        ll x = 0;
        for (int S = 0; S < (1 << pf.size()); ++S) {
            int mul = 1;
            for (int i = 0; i < pf.size(); ++i)
                if (S >> i & 1) mul *= pf[i];

            if (__builtin_parity(S)) x -= cnt[mul];
            else x += cnt[mul];
            cnt[mul]++;
        }
        ans -= x;
    }

    cout << ans << endl;
}
0