結果

問題 No.843 Triple Primes
ユーザー ninoinui
提出日時 2020-07-18 09:05:15
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 564 bytes
コンパイル時間 3,757 ms
コンパイル使用メモリ 195,672 KB
最終ジャッジ日時 2025-01-12 00:09:25
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
  int N;
  cin >> N;
  vector<bool> B(N + 1, true);
  B.at(0) = B.at(1) = false;
  for (int i = 2; i <= N; i++) {
    if (!B.at(i)) continue;
    for (int j = i + i; j <= N; j += i) B.at(j) = false;
  }
  long ans = 0;
  for (int i = 2; i <= N; i++) {
    if (!B.at(i)) continue;
    if (i == 2) {
      ans++;
      continue;
    }
    long tmp = i + 2;
    if (sqrt(tmp) == (long) sqrt(tmp)) {
      long tmp2 = sqrt(tmp);
      if (tmp2 <= N && B.at(tmp2)) ans += 2;
    }
  }
  cout << ans << "\n";
}
0