結果

問題 No.732 3PrimeCounting
ユーザー gyouzasushigyouzasushi
提出日時 2020-03-30 00:54:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,800 ms / 3,000 ms
コード長 5,323 bytes
コンパイル時間 1,638 ms
コンパイル使用メモリ 174,068 KB
実行使用メモリ 30,796 KB
最終ジャッジ日時 2024-06-10 19:41:30
合計ジャッジ時間 42,136 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 1 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,940 KB
testcase_20 AC 7 ms
6,940 KB
testcase_21 AC 34 ms
6,944 KB
testcase_22 AC 35 ms
6,940 KB
testcase_23 AC 3 ms
6,944 KB
testcase_24 AC 3 ms
6,940 KB
testcase_25 AC 73 ms
6,940 KB
testcase_26 AC 44 ms
6,940 KB
testcase_27 AC 11 ms
6,944 KB
testcase_28 AC 11 ms
6,944 KB
testcase_29 AC 21 ms
6,940 KB
testcase_30 AC 22 ms
6,944 KB
testcase_31 AC 34 ms
6,944 KB
testcase_32 AC 44 ms
6,944 KB
testcase_33 AC 45 ms
6,940 KB
testcase_34 AC 45 ms
6,940 KB
testcase_35 AC 43 ms
6,940 KB
testcase_36 AC 43 ms
6,944 KB
testcase_37 AC 5 ms
6,940 KB
testcase_38 AC 6 ms
6,940 KB
testcase_39 AC 43 ms
6,944 KB
testcase_40 AC 34 ms
6,944 KB
testcase_41 AC 34 ms
6,944 KB
testcase_42 AC 34 ms
6,944 KB
testcase_43 AC 35 ms
6,940 KB
testcase_44 AC 35 ms
6,944 KB
testcase_45 AC 16 ms
6,944 KB
testcase_46 AC 16 ms
6,944 KB
testcase_47 AC 20 ms
6,944 KB
testcase_48 AC 74 ms
6,944 KB
testcase_49 AC 74 ms
6,940 KB
testcase_50 AC 34 ms
6,940 KB
testcase_51 AC 34 ms
6,940 KB
testcase_52 AC 10 ms
6,944 KB
testcase_53 AC 165 ms
6,940 KB
testcase_54 AC 1,225 ms
17,356 KB
testcase_55 AC 1,243 ms
17,484 KB
testcase_56 AC 1,253 ms
17,484 KB
testcase_57 AC 381 ms
10,000 KB
testcase_58 AC 384 ms
9,868 KB
testcase_59 AC 171 ms
6,944 KB
testcase_60 AC 509 ms
10,264 KB
testcase_61 AC 509 ms
10,156 KB
testcase_62 AC 1,312 ms
17,984 KB
testcase_63 AC 884 ms
16,360 KB
testcase_64 AC 533 ms
10,432 KB
testcase_65 AC 543 ms
10,684 KB
testcase_66 AC 4 ms
6,944 KB
testcase_67 AC 3 ms
6,940 KB
testcase_68 AC 1,284 ms
17,560 KB
testcase_69 AC 1,265 ms
17,556 KB
testcase_70 AC 1,240 ms
17,364 KB
testcase_71 AC 1,251 ms
17,368 KB
testcase_72 AC 894 ms
16,296 KB
testcase_73 AC 2,178 ms
29,776 KB
testcase_74 AC 2,186 ms
29,692 KB
testcase_75 AC 71 ms
6,940 KB
testcase_76 AC 1,275 ms
17,552 KB
testcase_77 AC 386 ms
9,932 KB
testcase_78 AC 2,056 ms
29,232 KB
testcase_79 AC 1,274 ms
17,820 KB
testcase_80 AC 2,036 ms
29,012 KB
testcase_81 AC 1,233 ms
17,340 KB
testcase_82 AC 11 ms
6,940 KB
testcase_83 AC 380 ms
9,912 KB
testcase_84 AC 473 ms
10,116 KB
testcase_85 AC 1,174 ms
17,048 KB
testcase_86 AC 2,007 ms
28,940 KB
testcase_87 AC 2,783 ms
30,796 KB
testcase_88 AC 2,800 ms
30,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll LINF = 1e18;
const ll MOD = LINF;
template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
struct NumberTheoreticTransform {
    ll ext_gxd(ll a, ll b, ll& x, ll& y) {
        if (b == 0) {
            x = 1;
            y = 0;
            return a;
        }
        ll q = a / b;
        ll g = ext_gxd(b, a - q * b, x, y);
        ll z = x - q * y;
        x = y;
        y = z;
        return g;
    }

    ll modinv(ll a, ll m) {
        ll x, y;
        ext_gxd(a, m, x, y);
        x %= m;
        if (x < 0) x += m;
        return x;
    }

    ll modpow(ll a, ll n, ll m) {
        ll ret = 1;
        ll now = a;
        while (n > 0) {
            if (n % 2 == 1) ret = ret * now % m;
            now = now * now % m;
            n /= 2;
        }
        return ret;
    }

    void ntt(vector<ll>& a, ll mod, bool inv = 0) {
        const int n = sz(a);
        assert((n & (n - 1)) == 0);

        const ll g = 3;
        ll h = modpow(g, (mod - 1) / n, mod);
        if (inv) h = modinv(h, mod);

        int i = 0;
        for (int j = 1; j < n - 1; j++) {
            for (int k = n >> 1; k > (i ^= k); k >>= 1) {
            };
            if (j < i) swap(a[i], a[j]);
        }

        for (int m = 1; m < n; m *= 2) {
            const int m2 = m * 2;
            const ll base = modpow(h, n / m2, mod);
            ll w = 1;
            for (int x = 0; x < m; x++) {
                for (int s = x; s < n; s += m2) {
                    ll u = a[s];
                    ll d = a[s + m] * w % mod;
                    a[s] = u + d;
                    if (a[s] >= mod) a[s] -= mod;
                    a[s + m] = u - d;
                    if (a[s + m] < 0) a[s + m] += mod;
                }
                w = w * base % mod;
            }
        }

        for (auto& x : a) {
            if (x < 0) x += mod;
        }

        if (inv) {
            const int n_inv = modinv(n, mod);
            for (auto& x : a) x = x * n_inv % mod;
        }
    }

    vector<ll> convolution(const vector<ll>& a, vector<ll>& b, ll mod) {
        int ntt_size = 1;
        while (ntt_size < sz(a) + sz(b)) ntt_size <<= 1;

        vector<ll> _a = a, _b = b;
        _a.resize(ntt_size);
        _b.resize(ntt_size);

        ntt(_a, mod);
        ntt(_b, mod);

        for (int i = 0; i < ntt_size; i++) {
            _a[i] *= _b[i];
            _a[i] %= mod;
        }

        ntt(_a, mod, 1);
        return _a;
    }

    vector<ll> modconv(vector<ll> a, vector<ll> b, ll mod = MOD) {
        for (auto& x : a) x %= mod;
        for (auto& x : b) x %= mod;

        auto x = convolution(a, b, 167772161);
        auto y = convolution(a, b, 469762049);
        auto z = convolution(a, b, 1224736769);

        const ll mod1 = 167772161, mod2 = 469762049, mod3 = 1224736769;
        const ll mod1_inv_mod2 = modinv(mod1, mod2);
        const ll mod1mod2_inv_mod3 = modinv(mod1 * mod2, mod3);
        const ll mod1mod2_mod = mod1 * mod2 % mod;
        vector<ll> ret(sz(x));
        for (int i = 0; i < sz(x); i++) {
            ll v1 = (y[i] - x[i]) * mod1_inv_mod2 % mod2;
            if (v1 < 0) v1 += mod2;
            ll v2 =
                (z[i] - (x[i] + mod1 * v1) % mod3) * mod1mod2_inv_mod3 % mod3;
            if (v2 < 0) v2 += mod3;
            ll constants3 = (x[i] + mod1 * v1 + mod1mod2_mod * v2) % mod;
            if (constants3 < 0) constants3 += mod;
            ret[i] = constants3;
        }
        return ret;
    }
};
int main() {
    NumberTheoreticTransform ntt;
    int n;
    cin >> n;
    vector<ll> v(n + 1);
    for (int p = 2; p <= n; p++) {
        bool isprime = 1;
        for (int d = 2; d * d <= p; d++) {
            isprime &= (p % d > 0);
        }
        if (isprime) {
            v[p]++;
        }
    }

    auto c = ntt.modconv(ntt.modconv(v, v), v);
    ll ans = 0;
    for (int p = 2; p <= 3 * n; p++) {
        bool isprime = 1;
        for (int d = 2; d * d <= p; d++) {
            isprime &= (p % d > 0);
        }
        if (isprime) {
            ans += c[p];
        }
    }

    vector<ll> w(2 * n + 1);
    for (int p = 2; p <= n; p++) {
        bool isprime = 1;
        for (int d = 2; d * d <= p; d++) {
            isprime &= (p % d > 0);
        }
        if (isprime) {
            w[p * 2]++;
        }
    }
    auto d = ntt.modconv(v, w);
    for (int p = 2; p <= 3 * n; p++) {
        bool isprime = 1;
        for (int d = 2; d * d <= p; d++) {
            isprime &= (p % d > 0);
        }
        if (isprime) ans -= 3 * d[p];
    }
    cout << ans / 6 << endl;
}
0