結果

問題 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,776 ms
コンパイル使用メモリ 214,972 KB
実行使用メモリ 14,760 KB
最終ジャッジ日時 2024-11-29 23:48:44
合計ジャッジ時間 101,680 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 391 ms
9,380 KB
testcase_01 AC 390 ms
9,388 KB
testcase_02 AC 390 ms
9,388 KB
testcase_03 AC 391 ms
9,388 KB
testcase_04 AC 390 ms
9,388 KB
testcase_05 AC 391 ms
9,392 KB
testcase_06 AC 391 ms
9,384 KB
testcase_07 AC 391 ms
9,388 KB
testcase_08 AC 392 ms
9,388 KB
testcase_09 AC 391 ms
9,388 KB
testcase_10 AC 392 ms
9,388 KB
testcase_11 AC 393 ms
9,260 KB
testcase_12 AC 392 ms
9,384 KB
testcase_13 AC 392 ms
9,388 KB
testcase_14 AC 392 ms
9,392 KB
testcase_15 AC 393 ms
9,388 KB
testcase_16 AC 392 ms
9,380 KB
testcase_17 AC 393 ms
9,388 KB
testcase_18 AC 392 ms
9,392 KB
testcase_19 AC 393 ms
9,384 KB
testcase_20 AC 433 ms
9,384 KB
testcase_21 AC 527 ms
9,392 KB
testcase_22 AC 530 ms
9,388 KB
testcase_23 AC 406 ms
9,384 KB
testcase_24 AC 406 ms
9,388 KB
testcase_25 AC 635 ms
9,392 KB
testcase_26 AC 575 ms
9,384 KB
testcase_27 AC 449 ms
9,388 KB
testcase_28 AC 450 ms
9,368 KB
testcase_29 AC 502 ms
9,388 KB
testcase_30 AC 506 ms
9,520 KB
testcase_31 AC 543 ms
9,500 KB
testcase_32 AC 595 ms
9,384 KB
testcase_33 AC 597 ms
9,388 KB
testcase_34 AC 595 ms
9,492 KB
testcase_35 AC 555 ms
9,392 KB
testcase_36 AC 564 ms
9,260 KB
testcase_37 AC 419 ms
9,384 KB
testcase_38 AC 420 ms
9,392 KB
testcase_39 AC 558 ms
9,264 KB
testcase_40 AC 544 ms
9,384 KB
testcase_41 AC 544 ms
9,388 KB
testcase_42 AC 546 ms
9,344 KB
testcase_43 AC 514 ms
9,216 KB
testcase_44 AC 517 ms
9,388 KB
testcase_45 AC 462 ms
9,388 KB
testcase_46 AC 462 ms
9,388 KB
testcase_47 AC 480 ms
9,384 KB
testcase_48 AC 652 ms
9,384 KB
testcase_49 AC 646 ms
9,392 KB
testcase_50 AC 531 ms
9,516 KB
testcase_51 AC 533 ms
9,388 KB
testcase_52 AC 456 ms
9,388 KB
testcase_53 AC 872 ms
9,388 KB
testcase_54 AC 2,323 ms
9,384 KB
testcase_55 AC 2,286 ms
9,388 KB
testcase_56 AC 2,385 ms
9,392 KB
testcase_57 AC 1,281 ms
9,392 KB
testcase_58 AC 1,288 ms
9,348 KB
testcase_59 AC 903 ms
9,384 KB
testcase_60 AC 1,501 ms
9,392 KB
testcase_61 AC 1,467 ms
9,392 KB
testcase_62 AC 2,608 ms
9,520 KB
testcase_63 AC 1,887 ms
9,388 KB
testcase_64 AC 1,626 ms
9,388 KB
testcase_65 AC 1,606 ms
9,388 KB
testcase_66 AC 403 ms
9,388 KB
testcase_67 AC 406 ms
9,388 KB
testcase_68 AC 2,519 ms
9,388 KB
testcase_69 AC 2,461 ms
9,384 KB
testcase_70 AC 2,416 ms
9,388 KB
testcase_71 AC 2,504 ms
9,392 KB
testcase_72 AC 1,864 ms
9,384 KB
testcase_73 TLE -
testcase_74 TLE -
testcase_75 AC 620 ms
9,388 KB
testcase_76 AC 2,479 ms
9,388 KB
testcase_77 AC 1,324 ms
9,384 KB
testcase_78 AC 2,859 ms
9,388 KB
testcase_79 AC 2,495 ms
9,516 KB
testcase_80 AC 2,664 ms
9,344 KB
testcase_81 AC 2,388 ms
9,344 KB
testcase_82 AC 443 ms
9,388 KB
testcase_83 AC 1,289 ms
9,384 KB
testcase_84 AC 1,340 ms
9,384 KB
testcase_85 AC 2,261 ms
9,384 KB
testcase_86 AC 2,807 ms
9,384 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