結果

問題 No.843 Triple Primes
ユーザー m-44
提出日時 2019-09-08 18:48:38
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 639 bytes
コンパイル時間 1,559 ms
コンパイル使用メモリ 169,032 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-27 15:07:11
合計ジャッジ時間 3,109 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
  int n;
  cin>>n;
  vector<long> primes;
  bool checked[n+1];
  for (int i=0; i<=n; i++) checked[i] = false;
  for (int i=2; i<=n; i++) {
    if (checked[i]) {
      continue;
    }
    primes.push_back(i);
    for (int j=2*i; j<=n; j+=i) {
      checked[j] = true;
    }
  }
  int cnt = 0;
  for (long r: primes) {
    long q = r * r - 2;
    auto it = lower_bound(primes.begin(), primes.end(), q);
    if (it == primes.end()) {
      continue;
    }
    if (*it == q) {
      if (q == r) {
        ++cnt;
      } else {
        cnt += 2;
      }
    }
  }
  cout<<cnt<<endl;
}
0