結果

問題 No.1661 Sum is Prime (Hard Version)
ユーザー carrot46carrot46
提出日時 2021-08-27 22:49:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,295 bytes
コンパイル時間 2,070 ms
コンパイル使用メモリ 204,636 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-01 03:35:45
合計ジャッジ時間 8,047 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 550 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 AC 583 ms
6,940 KB
testcase_17 RE -
testcase_18 RE -
testcase_19 AC 393 ms
6,940 KB
testcase_20 RE -
testcase_21 AC 448 ms
6,944 KB
testcase_22 AC 910 ms
6,940 KB
testcase_23 AC 879 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/modint>
using namespace std;
using namespace atcoder;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()

using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;

ll N,M,H,W,Q,K,A,B;
string S;
using P = pair<ll, ll>;
using tp = tuple<ll, ll, ll>;
const ll INF = (1LL<<60);

template<class T> bool chmin(T &a, const T b){
    if(a > b) {a = b; return true;}
    else return false;
}
template<class T> bool chmax(T &a, const T b){
    if(a < b) {a = b; return true;}
    else return false;
}
template<class T> void my_printv(std::vector<T> v,bool endline = true){
    if(!v.empty()){
        for(std::size_t i{}; i<v.size()-1; ++i) std::cout<<v[i]<<" ";
        std::cout<<v.back();
    }
    if(endline) std::cout<<std::endl;
}

using mint = modint998244353;
using vm = vector<mint>;

ll msqrt(ll n){
    ll lb = 0, ub = n + 1;
    while(ub - lb > 1){
        ll cen = lb + ((ub - lb)>>1);
        (cen * cen <= n ? lb : ub) = cen;
    }
    return lb;
}

ll pr(ll n){
    //[1, n)
    if((--n) <= 1) return 0;
    ll sn = msqrt(n);
    vec smalls(n / sn + 1), larges(sn + 1);
    for(int i = 1; i <= sn; ++i) larges[i] = n / i - 1;
    for(int i = 1; i <= n / sn; ++i) smalls[i] = i - 1;
    auto access_inv = [&](int i){
        ll n_i = n / i;
        if(n_i == 0) return 0LL;
        if(n_i <= n / sn) return smalls[n_i];
        if(i > sn) exit(0);
        return larges[i];
    };
    for(ll p = 2; p <= sn; ++p){
        if(p <= n / sn && smalls[p] <= smalls[p - 1]) continue;
        if(n / p <= sn && larges[n / p] <= smalls[p - 1]) continue;
        ll pp = p * p, now_pi = smalls[p - 1];
        for(ll i = 1, ip = p, ipp = pp; ipp <= n && i <= sn; ++i, ip += p, ipp += pp){
            larges[i] -= access_inv(ip) - now_pi;
        }
        for(ll i = n / sn; i >= pp; --i){
            smalls[i] -= smalls[i / p] - now_pi;
        }
    }
    return larges[1];
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    //reps(i, 3, 102) if(pr(i) - pr(i) == 1) cout<<i<<endl;
    cin>>A>>B;
    ++B;
    cout<<pr(B) - pr(A) + pr(B * 2 - 1) - pr(A * 2 + 1)<<endl;
}
0