結果

問題 No.1661 Sum is Prime (Hard Version)
ユーザー ace_amuroace_amuro
提出日時 2021-08-31 17:54:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 995 ms / 3,000 ms
コード長 1,456 bytes
コンパイル時間 659 ms
コンパイル使用メモリ 80,424 KB
実行使用メモリ 12,936 KB
最終ジャッジ日時 2023-08-17 07:37:07
合計ジャッジ時間 10,244 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,792 KB
testcase_01 AC 3 ms
9,920 KB
testcase_02 AC 798 ms
12,936 KB
testcase_03 AC 3 ms
7,748 KB
testcase_04 AC 3 ms
9,748 KB
testcase_05 AC 2 ms
9,792 KB
testcase_06 AC 3 ms
9,712 KB
testcase_07 AC 3 ms
9,768 KB
testcase_08 AC 3 ms
9,704 KB
testcase_09 AC 3 ms
9,744 KB
testcase_10 AC 3 ms
9,708 KB
testcase_11 AC 3 ms
9,712 KB
testcase_12 AC 663 ms
12,768 KB
testcase_13 AC 644 ms
12,688 KB
testcase_14 AC 686 ms
12,692 KB
testcase_15 AC 872 ms
12,864 KB
testcase_16 AC 797 ms
12,796 KB
testcase_17 AC 754 ms
12,844 KB
testcase_18 AC 315 ms
12,476 KB
testcase_19 AC 515 ms
12,548 KB
testcase_20 AC 342 ms
12,368 KB
testcase_21 AC 590 ms
12,904 KB
testcase_22 AC 458 ms
12,920 KB
testcase_23 AC 995 ms
12,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
using namespace std;
typedef long long LL;
const int M=1e6+10;
const LL MOD=1e9+7;

bool is_p[M];
int prime[M];
LL sqrtn;

int euler(int N) {
    int len = 0;
    memset(is_p, true, sizeof(is_p));
    for(int i = 2; i < N; i++) {
        if(is_p[i]) {
            len++;
            prime[len] = i;
        }
        for(int j = 1; j <= len && prime[j] <= i; j++) {
            if(i * prime[j] >= N) {
                break;
            }
            is_p[i * prime[j]] = false;
            if(i % prime[j] == 0) {
               
                break;
            } else {

            }
        }
    }
    return len;
}


LL L[M],R[M];
LL primepi(LL n){
    if(n<2) return 0;
    for(LL i=1;i<=sqrtn;++i)   R[i]=n/i-1;
    for(LL i=1;i<=sqrtn;++i)   L[i]=i-1;
    for(int s=1;prime[s]<=sqrtn;s++){
        LL ps=prime[s];
        for(LL i=1,tn=min(n/(ps*ps),sqrtn);i<=tn;++i){
            R[i] -= (i*ps<=sqrtn?R[i*ps]:L[n/(i*ps)])-L[ps-1];
        }
        for(LL i=sqrtn;i>=ps*ps;--i){
            L[i] -= L[i/ps]-L[ps-1];
        }
    }
    return R[1];
}


int main(){
    LL l,r;
    scanf("%lld%lld",&l,&r);
    sqrtn=ceil(sqrt(2*r));
    euler(sqrtn*2);
    LL ans1 = primepi(r)-primepi(l-1);
    LL ans2 = l==r?0:primepi(2*r-1)-primepi(2*l);
    printf("%lld\n",ans1+ans2);
    return 0;
}
0