結果

問題 No.2620 Sieve of Coins
ユーザー suisensuisen
提出日時 2024-01-27 00:17:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 1,839 bytes
コンパイル時間 1,437 ms
コンパイル使用メモリ 86,080 KB
実行使用メモリ 11,360 KB
最終ジャッジ日時 2024-01-27 00:17:58
合計ジャッジ時間 4,361 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
11,360 KB
testcase_01 AC 30 ms
11,360 KB
testcase_02 AC 29 ms
11,360 KB
testcase_03 AC 37 ms
11,360 KB
testcase_04 AC 36 ms
11,360 KB
testcase_05 AC 32 ms
11,360 KB
testcase_06 AC 30 ms
11,360 KB
testcase_07 AC 29 ms
11,360 KB
testcase_08 AC 30 ms
11,360 KB
testcase_09 AC 30 ms
11,360 KB
testcase_10 AC 32 ms
11,360 KB
testcase_11 AC 29 ms
11,360 KB
testcase_12 AC 32 ms
11,360 KB
testcase_13 AC 31 ms
11,360 KB
testcase_14 AC 30 ms
11,360 KB
testcase_15 AC 30 ms
11,360 KB
testcase_16 AC 31 ms
11,360 KB
testcase_17 AC 31 ms
11,360 KB
testcase_18 AC 36 ms
11,360 KB
testcase_19 AC 39 ms
11,360 KB
testcase_20 AC 32 ms
11,360 KB
testcase_21 AC 35 ms
11,360 KB
testcase_22 AC 35 ms
11,360 KB
testcase_23 AC 32 ms
11,360 KB
testcase_24 AC 32 ms
11,360 KB
testcase_25 AC 30 ms
11,360 KB
testcase_26 AC 30 ms
11,360 KB
testcase_27 AC 30 ms
11,360 KB
testcase_28 AC 32 ms
11,360 KB
testcase_29 AC 31 ms
11,360 KB
testcase_30 AC 30 ms
11,360 KB
testcase_31 AC 36 ms
11,360 KB
testcase_32 AC 37 ms
11,360 KB
testcase_33 AC 31 ms
11,360 KB
testcase_34 AC 30 ms
11,360 KB
testcase_35 AC 30 ms
11,360 KB
testcase_36 AC 37 ms
11,360 KB
testcase_37 AC 34 ms
11,360 KB
testcase_38 AC 37 ms
11,360 KB
testcase_39 AC 43 ms
11,360 KB
testcase_40 AC 42 ms
11,360 KB
testcase_41 AC 37 ms
11,360 KB
testcase_42 AC 33 ms
11,360 KB
testcase_43 AC 33 ms
11,360 KB
testcase_44 AC 32 ms
11,360 KB
testcase_45 AC 30 ms
11,360 KB
testcase_46 AC 34 ms
11,360 KB
testcase_47 AC 34 ms
11,360 KB
testcase_48 AC 35 ms
11,360 KB
testcase_49 AC 34 ms
11,360 KB
testcase_50 AC 35 ms
11,360 KB
testcase_51 AC 42 ms
11,360 KB
testcase_52 AC 39 ms
11,360 KB
testcase_53 AC 37 ms
11,360 KB
testcase_54 AC 40 ms
11,360 KB
testcase_55 AC 33 ms
11,360 KB
testcase_56 AC 33 ms
11,360 KB
testcase_57 AC 32 ms
11,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <array>
#include <cmath>

template <int M>
struct Mobius {
    std::array<int, M + 1> mobius;
    Mobius() : mobius{} {
        mobius[1] = 1;
        std::array<int, M + 1> not_prime{};
        for (int p = 2; p <= M; ++p) {
            if (not_prime[p]) continue;
            for (int v = M / p, vp = v * p; v; --v, vp -= p) {
                mobius[vp] = -mobius[v];
                not_prime[vp] = true;
            }
        }
    }
    int operator[](int i) const { return mobius[i]; }
};

constexpr int M = 1000000;
Mobius<M> mobius;

long long count_square_2_3_free(long long n) {
    const int q = std::sqrt(n);
    long long res = 0;
    for (int i = 1; i <= q; ++i) if (mobius[i] and i % 2 and i % 3) {
        long long v1 = double(n + 1LL * i * i) / (6LL * i * i);
        long long v5 = double(n + 5LL * i * i) / (6LL * i * i);
        res += mobius[i] * (v1 + v5);
    }
    return res;
}

#include <iostream>
#include <vector>

int main() {
    long long l;
    int n;
    std::cin >> l >> n;

    std::array<std::array<bool, 30>, 40> cnt{};
    for (int i = 0; i < n; ++i) {
        long long e;
        std::cin >> e;
        int p2 = 0, p3 = 0;
        while (e % 2 == 0) e /= 2, ++p2;
        while (e % 3 == 0) e /= 3, ++p3;
        cnt[p2][p3] = true;
    }

    long long ans = 0;
    for (int p2 = 0; p2 < 40; ++p2) for (int p3 = 0; p3 < 30; ++p3) {
        int x00 = cnt[p2][p3];
        int x10 = p2 and cnt[p2 - 1][p3];
        int x01 = p3 and cnt[p2][p3 - 1];
        int x11 = p2 and p3 and cnt[p2 - 1][p3 - 1];

        if (x00 xor x01 xor x10 xor x11) {
            long long v = 1;
            for (int e = p2; e--;) v *= 2;
            for (int e = p3; e--;) v *= 3;
            long long lim = l / v;
            ans += count_square_2_3_free(lim);
        }
    }
    std::cout << ans << '\n';
}
0