結果

問題 No.732 3PrimeCounting
ユーザー maimai
提出日時 2018-09-07 23:37:03
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 5,194 bytes
コンパイル時間 2,681 ms
コンパイル使用メモリ 216,828 KB
最終ジャッジ日時 2025-01-06 13:11:09
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 84 TLE * 5
権限があれば一括ダウンロードができます

ソースコード

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;
    }
    vector<ll> dp2(300001);
    repeat(_, 2) {
        rrepeat(i, last) {
            int p = primeList[i];
            for (int j = 2; __builtin_expect(j + p <= 300001, 1); ++j) {
                dp2[j + p] += dp[j];
            }
        }
        dp.swap(dp2);
        fill(ALL(dp2), 0);
    }


    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