結果

問題 No.1661 Sum is Prime (Hard Version)
ユーザー umezoumezo
提出日時 2021-08-28 18:12:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,714 bytes
コンパイル時間 2,424 ms
コンパイル使用メモリ 213,752 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-01 16:04:00
合計ジャッジ時間 2,948 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;

__attribute__((target("avx"),optimize("O3","unroll-loops")))
ll prime_pi(const ll n){
  if(n<=1) return 0;
  if(n==2) return 1;
  const int sq=sqrtl(n);
  int s=1+sq>>1;
  vector<int> smalls(s); for(int i=1;i<s;++i) smalls[i]=i;
  vector<int> roughs(s); for(int i=0;i<s;++i) roughs[i]=i<<1|1;
  vector<ll> larges(s); for(int i=0;i<s;++i) larges[i]=(n/(i<< 1|1)-1)>>1;
  vector<bool> skip(sq+1);
  const auto divide=[](ll n, ll d) -> int{return (long double)n/d;};
  const auto half=[](int n) -> int{return (n-1)>>1;};
  int pc=0;
  for(int p=3;p<=sq;p+=2) if(!skip[p]){
    int q=p*p;
    if((ll)q*q>n) break;
    skip[p]=true;
    for (int i=q;i<=sq;i+=p<<1) skip[i]=true;
    int ns=0;
    for(int k=0;k<s;++k){
      int i=roughs[k];
      if(skip[i]) continue;
      ll d=(ll)i*p;
      larges[ns]=larges[k]-(d<=sq?larges[smalls[d>>1]-pc]:smalls[half(divide(n,d))])+pc;
      roughs[ns++]=i;
    }
    s=ns;
    for(int i=half(sq),j=((sq/p)-1)|1;j>=p;j-=2){
      int c=smalls[j>>1]-pc;
      for(int e=(j*p)>>1;i>=e;--i) smalls[i]-=c;
    }
    ++pc;
  }
  larges[0]+=(ll)(s+2*(pc-1))*(s-1)/2;
  for(int k=1;k<s;++k) larges[0]-=larges[k];
  for(int l=1;l<s;++l){
    int q=roughs[l];ll m=n/q;
    int e=smalls[half(m/q)]-pc;
    if(e<l+1) break;ll t=0;
    for(int k=l+1;k<=e;++k) t+=smalls[half(divide(m,roughs[k]))];
    larges[0]+=t-(ll)(e-l)*(pc+l-1);
  }
  return larges[0]+1;
}

int main(){
  ll l,r;
  cin>>l>>r;
  if(l==r) cout<<prime_pi(r)-prime_pi(l-1)<<endl;
  else cout<<prime_pi(r)-prime_pi(l-1)+prime_pi(2*r-2)-prime_pi(2*l-1)<<endl;
  return 0;
}
0