結果

問題 No.843 Triple Primes
ユーザー Mayimg
提出日時 2019-06-29 19:34:27
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 626 bytes
コンパイル時間 2,184 ms
コンパイル使用メモリ 195,996 KB
最終ジャッジ日時 2025-01-07 05:44:04
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() { 
  ios::sync_with_stdio(false); cin.tie(0); 
  int n;
  cin >> n;
  vector<long long> primes, is_prime(n + 1, 1);
  is_prime[0] = 0;
  is_prime[1] = 0;
  for (int i = 2; i <= n; i++) {
    if (!is_prime[i]) continue;
    primes.push_back(i);
    for (int j = i + i; j <= n; j += i) {
      is_prime[j] = 0;
    }
  }
  long long ans = 0;
  for (long long x : primes) {
    if (x * x - 2 > n) continue;
    else if (is_prime[x * x - 2]) {
      if (x * x - 2 == 2) ans++;
      else ans += 2;
    }
  }
  cout << ans << endl;
  return 0;
}
0