結果

問題 No.1657 Sum is Prime (Easy Version)
ユーザー LayCurseLayCurse
提出日時 2021-08-19 23:07:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 4,723 bytes
コンパイル時間 2,210 ms
コンパイル使用メモリ 219,396 KB
実行使用メモリ 9,732 KB
最終ジャッジ日時 2024-05-01 01:10:55
合計ジャッジ時間 2,981 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,620 KB
testcase_01 AC 2 ms
7,488 KB
testcase_02 AC 5 ms
6,940 KB
testcase_03 AC 2 ms
7,632 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
7,620 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
7,616 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
7,620 KB
testcase_12 AC 6 ms
8,264 KB
testcase_13 AC 4 ms
9,716 KB
testcase_14 AC 5 ms
6,940 KB
testcase_15 AC 5 ms
6,940 KB
testcase_16 AC 4 ms
6,944 KB
testcase_17 AC 4 ms
6,944 KB
testcase_18 AC 4 ms
6,944 KB
testcase_19 AC 6 ms
9,732 KB
testcase_20 AC 5 ms
6,940 KB
testcase_21 AC 5 ms
6,940 KB
testcase_22 AC 6 ms
8,260 KB
testcase_23 AC 6 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("inline")
#include<bits/stdc++.h>
using namespace std;
template<class T> struct cLtraits_identity{
  using type = T;
}
;
template<class T> using cLtraits_try_make_signed =
  typename conditional<
    is_integral<T>::value,
    make_signed<T>,
    cLtraits_identity<T>
    >::type;
template <class S, class T> struct cLtraits_common_type{
  using tS = typename cLtraits_try_make_signed<S>::type;
  using tT = typename cLtraits_try_make_signed<T>::type;
  using type = typename common_type<tS,tT>::type;
}
;
void*wmem;
char memarr[96000000];
template<class S, class T> inline auto min_L(S a, T b)
-> typename cLtraits_common_type<S,T>::type{
  return (typename cLtraits_common_type<S,T>::type) a <= (typename cLtraits_common_type<S,T>::type) b ? a : b;
}
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){
  walloc1d(arr, x2-x1, mem);
  (*arr) -= x1;
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
int Prime_L(int N, int res[], void *mem=wmem){
  int i;
  int a;
  int b;
  int sz = 1;
  const int r = 23000;
  bool*isprime;
  int*sf;
  int ss = 1;
  walloc1d(&isprime, r, &mem);
  walloc1d(&sf, r, &mem);
  N /= 2;
  res[0] = 2;
  b =min_L(r, N);
  for(i=(1);i<(b);i++){
    isprime[i] = 1;
  }
  for(i=(1);i<(b);i++){
    if(isprime[i]){
      res[sz++] = 2*i+1;
      sf[ss] = 2*i*(i+1);
      if(sf[ss] < N){
        while(sf[ss] < r){
          isprime[sf[ss]] = 0;
          sf[ss] += res[ss];
        }
        ss++;
      }
    }
  }
  for(a=r; a<N; a+=r){
    b =min_L(a + r, N);
    isprime -= r;
    for(i=(a);i<(b);i++){
      isprime[i] = 1;
    }
    for(i=(1);i<(ss);i++){
      while(sf[i] < b){
        isprime[sf[i]] = 0;
        sf[i] += res[i];
      }
    }
    for(i=(a);i<(b);i++){
      if(isprime[i]){
        res[sz++] = 2*i+1;
      }
    }
  }
  return sz;
}
long long cReader_ll(long long mn, long long mx, char nx){
  int i;
  int fg = 0;
  int m = 1;
  int f = -1;
  long long res = 0;
  double tmp = 0;
  for(;;){
    i = my_getchar_unlocked();
    if(fg==0 && i=='-'){
      fg++;
      m = -1;
    }
    else if('0' <= i  &&  i <= '9'){
      fg++;
      if(f == -1){
        f = i - '0';
      }
      res = 10 * res + i - '0';
      tmp = 10 * tmp + i - '0';
      assert(tmp < 1e20);
    }
    else{
      break;
    }
  }
  assert(tmp / 2 <= res);
  assert((m==1 && fg >= 1) || (m==-1 && fg >= 2));
  assert(mn <= m * res  &&  m * res <= mx);
  assert(!(res == 0 && m == -1));
  assert(!(res != 0 && f == 0));
  assert(!(res == 0 && fg >= 2));
  assert(i == nx);
  return m * res;
}
void cReader_eof(){
  int i;
  i = my_getchar_unlocked();
  assert(i == EOF);
}
int ps;
int p[2000000];
int main(){
  int i;
  wmem = memarr;
  long long L;
  long long R;
  long long res = 0;
  L = cReader_ll(1, 1000000, ' ');
  R = cReader_ll(L, 1000000, '\n');
  cReader_eof();
  ps =Prime_L(2*R, p);
  for(i=(0);i<(ps);i++){
    if(L <= p[i]  &&  p[i] <= R){
      res++;
    }
    if(2*L+1 <= p[i]  &&  p[i] <= 2*R-1){
      res++;
    }
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay version 20210819-1 [beta]

// --- original code ---
// int ps, p[2d6];
// {
//   ll L, R, res = 0;
//   L = cReader_ll(1, 1d6, ' ');
//   R = cReader_ll(L, 1d6, '\n');
//   cReader_eof();
// 
//   ps = Prime(2*R, p);
//   rep(i,ps){
//     if(L <= p[i] <= R) res++;
//     if(2*L+1 <= p[i] <= 2*R-1) res++;
//   }
//   wt(res);
// }
0