結果

問題 No.732 3PrimeCounting
ユーザー maimai
提出日時 2018-09-07 23:37:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 5,194 bytes
コンパイル時間 2,552 ms
コンパイル使用メモリ 216,384 KB
実行使用メモリ 9,472 KB
最終ジャッジ日時 2024-05-07 08:59:01
合計ジャッジ時間 99,383 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 376 ms
9,320 KB
testcase_01 AC 375 ms
9,328 KB
testcase_02 AC 379 ms
9,344 KB
testcase_03 AC 376 ms
9,344 KB
testcase_04 AC 379 ms
9,400 KB
testcase_05 AC 379 ms
9,344 KB
testcase_06 AC 376 ms
9,344 KB
testcase_07 AC 376 ms
9,344 KB
testcase_08 AC 379 ms
9,344 KB
testcase_09 AC 384 ms
9,344 KB
testcase_10 AC 383 ms
9,472 KB
testcase_11 AC 373 ms
9,344 KB
testcase_12 AC 384 ms
9,344 KB
testcase_13 AC 373 ms
9,344 KB
testcase_14 AC 382 ms
9,344 KB
testcase_15 AC 384 ms
9,344 KB
testcase_16 AC 373 ms
9,344 KB
testcase_17 AC 379 ms
9,344 KB
testcase_18 AC 380 ms
9,304 KB
testcase_19 AC 377 ms
9,344 KB
testcase_20 AC 419 ms
9,360 KB
testcase_21 AC 512 ms
9,356 KB
testcase_22 AC 513 ms
9,344 KB
testcase_23 AC 393 ms
9,344 KB
testcase_24 AC 390 ms
9,472 KB
testcase_25 AC 641 ms
9,344 KB
testcase_26 AC 569 ms
9,344 KB
testcase_27 AC 438 ms
9,376 KB
testcase_28 AC 436 ms
9,344 KB
testcase_29 AC 482 ms
9,324 KB
testcase_30 AC 490 ms
9,344 KB
testcase_31 AC 529 ms
9,344 KB
testcase_32 AC 581 ms
9,312 KB
testcase_33 AC 588 ms
9,312 KB
testcase_34 AC 573 ms
9,344 KB
testcase_35 AC 543 ms
9,344 KB
testcase_36 AC 544 ms
9,344 KB
testcase_37 AC 408 ms
9,344 KB
testcase_38 AC 409 ms
9,472 KB
testcase_39 AC 549 ms
9,344 KB
testcase_40 AC 533 ms
9,420 KB
testcase_41 AC 527 ms
9,360 KB
testcase_42 AC 534 ms
9,344 KB
testcase_43 AC 494 ms
9,308 KB
testcase_44 AC 501 ms
9,344 KB
testcase_45 AC 443 ms
9,336 KB
testcase_46 AC 452 ms
9,344 KB
testcase_47 AC 466 ms
9,344 KB
testcase_48 AC 632 ms
9,344 KB
testcase_49 AC 635 ms
9,328 KB
testcase_50 AC 516 ms
9,344 KB
testcase_51 AC 535 ms
9,344 KB
testcase_52 AC 442 ms
9,328 KB
testcase_53 AC 860 ms
9,308 KB
testcase_54 AC 2,331 ms
9,344 KB
testcase_55 AC 2,410 ms
9,344 KB
testcase_56 AC 2,342 ms
9,344 KB
testcase_57 AC 1,261 ms
9,344 KB
testcase_58 AC 1,263 ms
9,388 KB
testcase_59 AC 880 ms
9,344 KB
testcase_60 AC 1,474 ms
9,344 KB
testcase_61 AC 1,466 ms
9,344 KB
testcase_62 AC 2,605 ms
9,344 KB
testcase_63 AC 1,873 ms
9,344 KB
testcase_64 AC 1,575 ms
9,344 KB
testcase_65 AC 1,651 ms
9,360 KB
testcase_66 AC 393 ms
9,344 KB
testcase_67 AC 386 ms
9,472 KB
testcase_68 AC 2,539 ms
9,372 KB
testcase_69 AC 2,445 ms
9,344 KB
testcase_70 AC 2,349 ms
9,344 KB
testcase_71 AC 2,435 ms
9,344 KB
testcase_72 AC 1,766 ms
9,344 KB
testcase_73 AC 2,936 ms
9,396 KB
testcase_74 TLE -
testcase_75 AC 614 ms
9,472 KB
testcase_76 AC 2,457 ms
9,344 KB
testcase_77 AC 1,275 ms
9,308 KB
testcase_78 AC 2,877 ms
9,344 KB
testcase_79 AC 2,431 ms
9,396 KB
testcase_80 AC 2,720 ms
9,344 KB
testcase_81 AC 2,406 ms
9,344 KB
testcase_82 AC 428 ms
9,344 KB
testcase_83 AC 1,266 ms
9,344 KB
testcase_84 AC 1,337 ms
9,328 KB
testcase_85 AC 2,219 ms
9,344 KB
testcase_86 AC 2,646 ms
9,324 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;
    }
    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