結果

問題 No.732 3PrimeCounting
ユーザー maimai
提出日時 2018-09-07 23:32:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 5,150 bytes
コンパイル時間 2,348 ms
コンパイル使用メモリ 214,844 KB
実行使用メモリ 9,520 KB
最終ジャッジ日時 2024-05-07 08:32:09
合計ジャッジ時間 95,078 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 343 ms
9,388 KB
testcase_01 AC 346 ms
9,392 KB
testcase_02 AC 351 ms
9,384 KB
testcase_03 AC 354 ms
9,392 KB
testcase_04 AC 348 ms
9,392 KB
testcase_05 AC 343 ms
9,388 KB
testcase_06 AC 348 ms
9,392 KB
testcase_07 AC 348 ms
9,388 KB
testcase_08 AC 339 ms
9,388 KB
testcase_09 AC 344 ms
9,388 KB
testcase_10 AC 343 ms
9,384 KB
testcase_11 AC 342 ms
9,388 KB
testcase_12 AC 345 ms
9,388 KB
testcase_13 AC 343 ms
9,392 KB
testcase_14 AC 343 ms
9,384 KB
testcase_15 AC 346 ms
9,392 KB
testcase_16 AC 345 ms
9,388 KB
testcase_17 AC 343 ms
9,384 KB
testcase_18 AC 344 ms
9,388 KB
testcase_19 AC 357 ms
9,384 KB
testcase_20 AC 385 ms
9,388 KB
testcase_21 AC 480 ms
9,392 KB
testcase_22 AC 485 ms
9,388 KB
testcase_23 AC 372 ms
9,384 KB
testcase_24 AC 355 ms
9,388 KB
testcase_25 AC 579 ms
9,388 KB
testcase_26 AC 517 ms
9,388 KB
testcase_27 AC 399 ms
9,384 KB
testcase_28 AC 402 ms
9,388 KB
testcase_29 AC 453 ms
9,384 KB
testcase_30 AC 470 ms
9,388 KB
testcase_31 AC 501 ms
9,344 KB
testcase_32 AC 545 ms
9,384 KB
testcase_33 AC 546 ms
9,392 KB
testcase_34 AC 541 ms
9,516 KB
testcase_35 AC 497 ms
9,392 KB
testcase_36 AC 514 ms
9,384 KB
testcase_37 AC 371 ms
9,384 KB
testcase_38 AC 376 ms
9,392 KB
testcase_39 AC 509 ms
9,388 KB
testcase_40 AC 498 ms
9,388 KB
testcase_41 AC 496 ms
9,388 KB
testcase_42 AC 496 ms
9,388 KB
testcase_43 AC 474 ms
9,348 KB
testcase_44 AC 471 ms
9,388 KB
testcase_45 AC 426 ms
9,388 KB
testcase_46 AC 412 ms
9,392 KB
testcase_47 AC 430 ms
9,388 KB
testcase_48 AC 606 ms
9,344 KB
testcase_49 AC 600 ms
9,388 KB
testcase_50 AC 502 ms
9,388 KB
testcase_51 AC 484 ms
9,344 KB
testcase_52 AC 404 ms
9,516 KB
testcase_53 AC 794 ms
9,388 KB
testcase_54 AC 2,243 ms
9,352 KB
testcase_55 AC 2,205 ms
9,384 KB
testcase_56 AC 2,281 ms
9,388 KB
testcase_57 AC 1,190 ms
9,384 KB
testcase_58 AC 1,192 ms
9,388 KB
testcase_59 AC 833 ms
9,392 KB
testcase_60 AC 1,400 ms
9,388 KB
testcase_61 AC 1,416 ms
9,388 KB
testcase_62 AC 2,487 ms
9,388 KB
testcase_63 AC 1,744 ms
9,388 KB
testcase_64 AC 1,551 ms
9,388 KB
testcase_65 AC 1,565 ms
9,388 KB
testcase_66 AC 355 ms
9,520 KB
testcase_67 AC 357 ms
9,384 KB
testcase_68 AC 2,390 ms
9,392 KB
testcase_69 AC 2,299 ms
9,388 KB
testcase_70 AC 2,279 ms
9,388 KB
testcase_71 AC 2,229 ms
9,388 KB
testcase_72 AC 1,735 ms
9,388 KB
testcase_73 AC 2,920 ms
9,388 KB
testcase_74 AC 2,812 ms
9,388 KB
testcase_75 AC 583 ms
9,384 KB
testcase_76 AC 2,539 ms
9,392 KB
testcase_77 AC 1,224 ms
9,392 KB
testcase_78 AC 2,764 ms
9,516 KB
testcase_79 AC 2,335 ms
9,384 KB
testcase_80 AC 2,619 ms
9,384 KB
testcase_81 AC 2,319 ms
9,388 KB
testcase_82 AC 404 ms
9,384 KB
testcase_83 AC 1,236 ms
9,384 KB
testcase_84 AC 1,249 ms
9,388 KB
testcase_85 AC 2,091 ms
9,384 KB
testcase_86 AC 2,696 ms
9,392 KB
testcase_87 TLE -
testcase_88 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("O3")
#include "bits/stdc++.h"

using namespace std;
using ll = long long int;

#define debugos cout
#define debug(v) {printf("L%d %s > ",__LINE__,#v);debugos<<(v)<<endl;}
#define debugv(v) {printf("L%d %s > ",__LINE__,#v);for(auto e:(v)){debugos<<e<<" ";}debugos<<endl;}
#define debuga(m,w) {printf("L%d %s > ",__LINE__,#m);for(int x=0;x<(w);x++){debugos<<(m)[x]<<" ";}debugos<<endl;}
#define debugaa(m,h,w) {printf("L%d %s >\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){debugos<<(m)[y][x]<<" ";}debugos<<endl;}}
#define ALL(v) (v).begin(),(v).end()
#define repeat(cnt,l) for(remove_reference<remove_const<decltype(l)>::type>::type cnt=0;(cnt)<(l);++(cnt))
#define rrepeat(cnt,l) for(auto cnt=(l)-1;0<=(cnt);--(cnt))
#define iterate(cnt,b,e) for(auto cnt=(b);(cnt)!=(e);++(cnt))
#define diterate(cnt,b,e) for(auto cnt=(b);(cnt)!=(e);--(cnt))
const ll MD = 1000000007ll; const long double PI = 3.1415926535897932384626433832795L;
inline void assert_call(bool assertion, function<void()> f) { if (!assertion) { cerr << "assertion fault:" << endl; f(); abort(); } }
template<typename T1, typename T2> inline ostream& operator <<(ostream &o, const pair<T1, T2> p) { o << '(' << p.first << ':' << p.second << ')'; return o; }
template<typename Vec> inline ostream& _ostream_vecprint(ostream& os, const Vec& a) {
    os << '['; for (const auto& e : a) os << ' ' << e << ' '; os << ']'; return os;
}
template<typename T> inline ostream& operator<<(ostream& o, const vector<T>& v) { return _ostream_vecprint(o, v); }
template<typename T, size_t S> inline ostream& operator<<(ostream& o, const array<T, S>& v) { return _ostream_vecprint(o, v); }
template<typename T> inline T& maxset(T& to, const T& val) { return to = max(to, val); }
template<typename T> inline T& minset(T& to, const T& val) { return to = min(to, val); }
void bye(string s, int code = 0) { cout << s << endl; exit(code); }
mt19937_64 randdev(8901016);
template<typename T> inline T rand(T l, T h) { return uniform_int_distribution<T>(l, h)(randdev); }
template<> inline double rand<double>(double l, double h) { return uniform_real_distribution<double>(l, h)(randdev); }
template<> inline float rand<float>(float l, float h) { return uniform_real_distribution<float>(l, h)(randdev); }



template<int Max = 2000>
class IsPrimeC {
    bool d_[Max + 1];

    template<typename T>
    static T cesqrt(T s) {
        double x = s / 2.0;
        double prev = 0.0;
        while (x != prev) {
            prev = x;
            x = (x + s / x) / 2.0;
        }
        return x;
    }

public:
    IsPrimeC() : d_() {
        d_[0] = d_[1] = 1;
        int sqh = cesqrt<int>(Max);
        for (int i = 2; i <= sqh; i++) {
            if (d_[i] == 0) {
                for (int j = i * i; j <= Max; j += i) { // i*i
                    d_[j] = 1;
                }
            }
        }
        for (int i = 0; i <= Max; ++i) d_[i] = !d_[i];
    }
    inline bool operator[](int x) const { return d_[x]; }

};

template<int Max = 2000>
class PrimeList {
    int d_[Max];
public:
    PrimeList() : d_() {
        int n = 1;
        d_[0] = 2;
        for (int x = 3; n < Max; ++x) {
            bool f = true;
            for (int i = 0; d_[i] * d_[i] <= x; ++i)
                if (x % d_[i] == 0) { f = false; break; }
            if (f) d_[n++] = x;
        }
    }
    inline int operator[](int x) const { return d_[x]; }
    template<int Max_>
    class iterator {
        const PrimeList<Max_>& pl;
        int ptr = 0;
    public:
        iterator(const decltype(pl)& _pl, int _ptr = 0) :pl(_pl), ptr(_ptr) { }
        int operator*() const { return pl[ptr]; }
        iterator<Max_>& operator++() { ptr++; return *this; } // prefix
        inline bool operator!=(const iterator<Max_>& it) const { return ptr != it.ptr ? !(Max < ptr && Max < it.ptr) : false; }
        inline bool operator==(const iterator<Max_>& it) const { return ptr != it.ptr ? (Max < ptr && Max < it.ptr) : true; }
    };
    PrimeList::iterator<Max> begin() const { return PrimeList::iterator<Max>(*this, 0); }
    PrimeList::iterator<Max> end() const { return PrimeList::iterator<Max>(*this, Max); }
};


IsPrimeC<300001> isPrime;
PrimeList<300001> primeList;

ll N;


int main() {
    cin >> N;

    vector<ll> dp;
    dp.resize(300001);

    int last = 0;
    for (last = 0; primeList[last] <= N; ++last) {
        dp[primeList[last]] = 1;
    }
    repeat(_, 2) {
        vector<ll> dp2(300001);
        rrepeat(i, last) {
            int p = primeList[i];
            for (int j = 2; j + p <= 300001; ++j) {
                dp2[j + p] += dp[j];
            }
        }
        dp.swap(dp2);
    }


    ll ans = 0;
    for (ll p : primeList) {
        if (p >= dp.size()) break;
        ans += dp[p];
    }

    for (int i = 0; primeList[i] <= N; ++i) {
        int vi = primeList[i];
        for (int j = i + 1; primeList[j] <= N; ++j) {
            if (vi * 2 + primeList[j] >= dp.size()) break;
            ans -= isPrime[vi * 2 + primeList[j]] * 3;
            ans -= isPrime[vi + primeList[j] * 2] * 3;
        }
    }

    cout << ans / 6 << endl;


    return 0;
}
0