結果

問題 No.1661 Sum is Prime (Hard Version)
ユーザー tko919tko919
提出日時 2021-08-27 21:26:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 205 ms / 3,000 ms
コード長 2,299 bytes
コンパイル時間 2,330 ms
コンパイル使用メモリ 205,324 KB
実行使用メモリ 8,640 KB
最終ジャッジ日時 2023-08-13 07:51:09
合計ジャッジ時間 4,925 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 128 ms
6,792 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 115 ms
6,848 KB
testcase_13 AC 112 ms
6,432 KB
testcase_14 AC 127 ms
7,044 KB
testcase_15 AC 163 ms
7,772 KB
testcase_16 AC 135 ms
7,044 KB
testcase_17 AC 125 ms
6,668 KB
testcase_18 AC 52 ms
4,560 KB
testcase_19 AC 94 ms
6,220 KB
testcase_20 AC 59 ms
5,208 KB
testcase_21 AC 102 ms
6,000 KB
testcase_22 AC 205 ms
8,520 KB
testcase_23 AC 197 ms
8,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
using ll=long long int;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}

bool Miller(ll n){
   if(n<2 or (n&1)==0)return (n==2);
   ll d=n-1; while((d&1)==0)d>>=1;
   vector<ll> seeds;
   auto MP=[&](ll x,ll t,ll m)->ll{
      ll res=1;
      while(t){
         if(t&1)res=(__int128_t(res)*x)%m;
         x=(__int128_t(x)*x)%m; t>>=1;
      } return res;
   };
   if(n<(1<<30))seeds={2, 7, 61};
   else seeds={2, 325, 9375, 28178, 450775, 9780504};
   for(auto& x:seeds){
      if(n<=x)break;
      ll t=d,y=MP(x,t,n);
      while(t!=n-1 and y!=1 and y!=n-1){
         y=(__int128_t(y)*y)%n; t<<=1;
      }
      if(y!=n-1 and (t&1)==0)return 0;
   } return 1;
}

struct PrimeCount{
   const ll N; const ll M;
   vector<int> lo; vector<ll> hi;
   PrimeCount(const ll& _N):N(_N),M(int(sqrt(N))),lo(M+1),hi(M+1){
      rep(i,1,M+1)lo[i]=i-1,hi[i]=N/i-1;
      int cnt=0;
      for(int p=2;p<=M;p++){
         if(lo[p-1]==lo[p])continue;
         const ll n=N/p;
         const ll q=ll(p)*p;
         const int w=M/p;
         const ll L=min(M,N/q);
         for(int i=1;i<=w;i++)hi[i]-=hi[i*p]-cnt;
         const int t=min<int>(sqrt(n),L);
         for(int i=w+1;i<=t;i++)hi[i]-=lo[n/i]-cnt;
         for(int i=L,j=n/L;i>t;j++){
            int c=lo[j];
            while(j+1<=M and lo[j+1]==c)j++;
            c-=cnt;
            for(int e=max<int>(t,n/(j+1));i>e;i--)hi[i]-=c;
         }
         for(int i=M,j=M/p;j>=p;j--){
            const int c=lo[j]-cnt;
            for(int e=j*p;i>=e;i--)lo[i]-=c;
         }
         cnt++;
      }
   }
   ll pi(ll x){
      if(x<=M)return lo[x];
      else return hi[N/x];
   }
};

int main(){
   ll L,R;
   cin>>L>>R;

   PrimeCount Ls(L-1),Rs(R),L2s(L*2),R2s(R*2);
   ll res=Rs.pi(R)-Ls.pi(L-1)+R2s.pi(R*2)-L2s.pi(L*2);
   cout<<res<<'\n';
   return 0;


   rep(a,L,R+1){
      if(Miller(a))res++;
      if(a!=R and Miller(a*2+1))res++;
   }
   cout<<res<<'\n';
   return 0;
}
0