結果

問題 No.732 3PrimeCounting
ユーザー st1vdyst1vdy
提出日時 2021-08-11 11:22:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 107 ms / 3,000 ms
コード長 8,288 bytes
コンパイル時間 2,994 ms
コンパイル使用メモリ 221,932 KB
実行使用メモリ 28,196 KB
最終ジャッジ日時 2024-09-24 20:19:38
合計ジャッジ時間 6,353 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 3 ms
6,944 KB
testcase_22 AC 3 ms
6,944 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 5 ms
6,940 KB
testcase_26 AC 4 ms
6,940 KB
testcase_27 AC 3 ms
6,940 KB
testcase_28 AC 3 ms
6,940 KB
testcase_29 AC 3 ms
6,944 KB
testcase_30 AC 4 ms
6,944 KB
testcase_31 AC 3 ms
6,944 KB
testcase_32 AC 5 ms
6,944 KB
testcase_33 AC 4 ms
6,940 KB
testcase_34 AC 4 ms
6,940 KB
testcase_35 AC 5 ms
6,940 KB
testcase_36 AC 5 ms
6,944 KB
testcase_37 AC 2 ms
6,940 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 4 ms
6,944 KB
testcase_40 AC 3 ms
6,944 KB
testcase_41 AC 3 ms
6,944 KB
testcase_42 AC 4 ms
6,940 KB
testcase_43 AC 4 ms
6,940 KB
testcase_44 AC 3 ms
6,940 KB
testcase_45 AC 3 ms
6,940 KB
testcase_46 AC 2 ms
6,940 KB
testcase_47 AC 4 ms
6,940 KB
testcase_48 AC 5 ms
6,940 KB
testcase_49 AC 5 ms
6,944 KB
testcase_50 AC 3 ms
6,944 KB
testcase_51 AC 3 ms
6,940 KB
testcase_52 AC 2 ms
6,944 KB
testcase_53 AC 8 ms
6,940 KB
testcase_54 AC 50 ms
15,796 KB
testcase_55 AC 49 ms
15,792 KB
testcase_56 AC 49 ms
15,920 KB
testcase_57 AC 14 ms
6,940 KB
testcase_58 AC 14 ms
6,940 KB
testcase_59 AC 8 ms
6,944 KB
testcase_60 AC 26 ms
9,628 KB
testcase_61 AC 26 ms
9,632 KB
testcase_62 AC 50 ms
16,188 KB
testcase_63 AC 28 ms
9,464 KB
testcase_64 AC 25 ms
9,784 KB
testcase_65 AC 26 ms
9,912 KB
testcase_66 AC 2 ms
6,940 KB
testcase_67 AC 2 ms
6,940 KB
testcase_68 AC 51 ms
16,012 KB
testcase_69 AC 51 ms
16,140 KB
testcase_70 AC 50 ms
15,796 KB
testcase_71 AC 50 ms
15,924 KB
testcase_72 AC 28 ms
9,424 KB
testcase_73 AC 55 ms
15,772 KB
testcase_74 AC 54 ms
15,648 KB
testcase_75 AC 5 ms
6,944 KB
testcase_76 AC 50 ms
15,860 KB
testcase_77 AC 14 ms
6,944 KB
testcase_78 AC 54 ms
15,240 KB
testcase_79 AC 50 ms
16,020 KB
testcase_80 AC 54 ms
15,116 KB
testcase_81 AC 50 ms
15,820 KB
testcase_82 AC 3 ms
6,940 KB
testcase_83 AC 15 ms
6,940 KB
testcase_84 AC 25 ms
9,544 KB
testcase_85 AC 48 ms
15,824 KB
testcase_86 AC 53 ms
15,272 KB
testcase_87 AC 107 ms
28,192 KB
testcase_88 AC 103 ms
28,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
namespace FFT {
    typedef double dbl;

    struct num {
        dbl x, y;
        num() { x = y = 0; }
        num(dbl x, dbl y) : x(x), y(y) { }
    };

    inline num operator+(num a, num b) { return num(a.x + b.x, a.y + b.y); }
    inline num operator-(num a, num b) { return num(a.x - b.x, a.y - b.y); }
    inline num operator*(num a, num b) { return num(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); }
    inline num conj(num a) { return num(a.x, -a.y); }

    int base = 1;
    vector<num> roots = { {0, 0}, {1, 0} };
    vector<int> rev = { 0, 1 };

    const dbl PI = acosl(-1.0);

    void ensure_base(int nbase) {
        if (nbase <= base) {
            return;
        }
        rev.resize(1 << nbase);
        for (int i = 0; i < (1 << nbase); i++) {
            rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1));
        }
        roots.resize(1 << nbase);
        while (base < nbase) {
            dbl angle = 2 * PI / (1 << (base + 1));
            for (int i = 1 << (base - 1); i < (1 << base); i++) {
                roots[i << 1] = roots[i];
                dbl angle_i = angle * (2 * i + 1 - (1 << base));
                roots[(i << 1) + 1] = num(cos(angle_i), sin(angle_i));
            }
            base++;
        }
    }

    void fft(vector<num>& a, int n = -1) {
        if (n == -1) {
            n = a.size();
        }
        assert((n & (n - 1)) == 0);
        int zeros = __builtin_ctz(n);
        ensure_base(zeros);
        int shift = base - zeros;
        for (int i = 0; i < n; i++) {
            if (i < (rev[i] >> shift)) {
                swap(a[i], a[rev[i] >> shift]);
            }
        }
        for (int k = 1; k < n; k <<= 1) {
            for (int i = 0; i < n; i += 2 * k) {
                for (int j = 0; j < k; j++) {
                    num z = a[i + j + k] * roots[j + k];
                    a[i + j + k] = a[i + j] - z;
                    a[i + j] = a[i + j] + z;
                }
            }
        }
    }

    vector<num> fa, fb;

    vector<int> multiply(vector<int>& a, vector<int>& b) {
        int need = a.size() + b.size() - 1;
        int nbase = 1;
        while ((1 << nbase) < need) nbase++;
        ensure_base(nbase);
        int sz = 1 << nbase;
        if (sz > (int)fa.size()) {
            fa.resize(sz);
        }
        for (int i = 0; i < sz; i++) {
            int x = (i < (int)a.size() ? a[i] : 0);
            int y = (i < (int)b.size() ? b[i] : 0);
            fa[i] = num(x, y);
        }
        fft(fa, sz);
        num r(0, -0.25 / (sz >> 1));
        for (int i = 0; i <= (sz >> 1); i++) {
            int j = (sz - i) & (sz - 1);
            num z = (fa[j] * fa[j] - conj(fa[i] * fa[i])) * r;
            if (i != j) {
                fa[j] = (fa[i] * fa[i] - conj(fa[j] * fa[j])) * r;
            }
            fa[i] = z;
        }
        for (int i = 0; i < (sz >> 1); i++) {
            num A0 = (fa[i] + fa[i + (sz >> 1)]) * num(0.5, 0);
            num A1 = (fa[i] - fa[i + (sz >> 1)]) * num(0.5, 0) * roots[(sz >> 1) + i];
            fa[i] = A0 + A1 * num(0, 1);
        }
        fft(fa, sz >> 1);
        vector<int> res(need);
        for (int i = 0; i < need; i++) {
            if (i % 2 == 0) {
                res[i] = fa[i >> 1].x + 0.5;
            } else {
                res[i] = fa[i >> 1].y + 0.5;
            }
        }
        return res;
    }

    vector<long long> square(const vector<int>& a) {
        int need = a.size() + a.size() - 1;
        int nbase = 1;
        while ((1 << nbase) < need) nbase++;
        ensure_base(nbase);
        int sz = 1 << nbase;
        if ((sz >> 1) > (int)fa.size()) {
            fa.resize(sz >> 1);
        }
        for (int i = 0; i < (sz >> 1); i++) {
            int x = (2 * i < (int)a.size() ? a[2 * i] : 0);
            int y = (2 * i + 1 < (int)a.size() ? a[2 * i + 1] : 0);
            fa[i] = num(x, y);
        }
        fft(fa, sz >> 1);
        num r(1.0 / (sz >> 1), 0.0);
        for (int i = 0; i <= (sz >> 2); i++) {
            int j = ((sz >> 1) - i) & ((sz >> 1) - 1);
            num fe = (fa[i] + conj(fa[j])) * num(0.5, 0);
            num fo = (fa[i] - conj(fa[j])) * num(0, -0.5);
            num aux = fe * fe + fo * fo * roots[(sz >> 1) + i] * roots[(sz >> 1) + i];
            num tmp = fe * fo;
            fa[i] = r * (conj(aux) + num(0, 2) * conj(tmp));
            fa[j] = r * (aux + num(0, 2) * tmp);
        }
        fft(fa, sz >> 1);
        vector<long long> res(need);
        for (int i = 0; i < need; i++) {
            if (i % 2 == 0) {
                res[i] = fa[i >> 1].x + 0.5;
            } else {
                res[i] = fa[i >> 1].y + 0.5;
            }
        }
        return res;
    }

    vector<int> multiply_mod(vector<int>& a, vector<int>& b, int m, int eq = 0) {
        int need = a.size() + b.size() - 1;
        int nbase = 0;
        while ((1 << nbase) < need) nbase++;
        ensure_base(nbase);
        int sz = 1 << nbase;
        if (sz > (int)fa.size()) {
            fa.resize(sz);
        }
        for (int i = 0; i < (int)a.size(); i++) {
            int x = (a[i] % m + m) % m;
            fa[i] = num(x & ((1 << 15) - 1), x >> 15);
        }
        fill(fa.begin() + a.size(), fa.begin() + sz, num{ 0, 0 });
        fft(fa, sz);
        if (sz > (int) fb.size()) {
            fb.resize(sz);
        }
        if (eq) {
            copy(fa.begin(), fa.begin() + sz, fb.begin());
        } else {
            for (int i = 0; i < (int)b.size(); i++) {
                int x = (b[i] % m + m) % m;
                fb[i] = num(x & ((1 << 15) - 1), x >> 15);
            }
            fill(fb.begin() + b.size(), fb.begin() + sz, num{ 0, 0 });
            fft(fb, sz);
        }
        dbl ratio = 0.25 / sz;
        num r2(0, -1);
        num r3(ratio, 0);
        num r4(0, -ratio);
        num r5(0, 1);
        for (int i = 0; i <= (sz >> 1); i++) {
            int j = (sz - i) & (sz - 1);
            num a1 = (fa[i] + conj(fa[j]));
            num a2 = (fa[i] - conj(fa[j])) * r2;
            num b1 = (fb[i] + conj(fb[j])) * r3;
            num b2 = (fb[i] - conj(fb[j])) * r4;
            if (i != j) {
                num c1 = (fa[j] + conj(fa[i]));
                num c2 = (fa[j] - conj(fa[i])) * r2;
                num d1 = (fb[j] + conj(fb[i])) * r3;
                num d2 = (fb[j] - conj(fb[i])) * r4;
                fa[i] = c1 * d1 + c2 * d2 * r5;
                fb[i] = c1 * d2 + c2 * d1;
            }
            fa[j] = a1 * b1 + a2 * b2 * r5;
            fb[j] = a1 * b2 + a2 * b1;
        }
        fft(fa, sz);
        fft(fb, sz);
        vector<int> res(need);
        for (int i = 0; i < need; i++) {
            long long aa = fa[i].x + 0.5;
            long long bb = fb[i].x + 0.5;
            long long cc = fa[i].y + 0.5;
            res[i] = (aa + ((bb % m) << 15) + ((cc % m) << 30)) % m;
        }
        return res;
    }

    vector<int> square_mod(vector<int>& a, int m) {
        return multiply_mod(a, a, m, 1);
    }
};

vector<bool> isPrime; // true 表示非素数  false 表示是素数
vector<int> prime; // 保存素数
int sieve(int n) {
    isPrime.resize(n + 1, false);
    isPrime[0] = isPrime[1] = true;
    for (int i = 2; i <= n; i++) {
        if (!isPrime[i]) prime.emplace_back(i);
        for (int j = 0; j < (int)prime.size() && prime[j] * i <= n; ++j) {
            isPrime[prime[j] * i] = true;
            if (!(i % prime[j])) break;
        }
    }
    return (int)prime.size();
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n;
    cin >> n;
    sieve(3 * n + 1);
    vector<int> a(n + 1, 0), b(n + 1, 0), c(2 * n + 1, 0);
    for (int i = 2; i <= n; i++) {
        if (!isPrime[i]) {
            a[i] = b[i] = 1;
            c[i * 2] = 1;
        }
    }
    a = FFT::multiply(a, b);
    a = FFT::multiply(a, b);
    c = FFT::multiply(c, b);
    long long res = 0;
    for (int i = 2; i <= 3 * n; i++) {
        if (!isPrime[i]) {
            res += (a[i] - c[i] * 3) / 6;
            //cout << (a[i] - c[i] * 3) / 6 << " " << i << "\n";
        }
    }
    cout << res;
    return 0;
}
0