結果
| 問題 | No.843 Triple Primes | 
| コンテスト | |
| ユーザー |  lowr | 
| 提出日時 | 2019-08-16 19:50:37 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 7 ms / 2,000 ms | 
| コード長 | 635 bytes | 
| コンパイル時間 | 1,814 ms | 
| コンパイル使用メモリ | 196,040 KB | 
| 最終ジャッジ日時 | 2025-01-07 12:08:17 | 
| ジャッジサーバーID (参考情報) | judge3 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 42 | 
ソースコード
#include <bits/stdc++.h>
using i64 = long long;
int main() {
  int n;
  std::cin >> n;
  std::vector<int> ps;
  std::vector<int> comp(n + 1);
  for (int i = 2; i <= n; i++) {
    if (!comp[i]) ps.push_back(i);
    for (int j = 0; j < ps.size() && i * ps[j] <= n; j++) {
      comp[i * ps[j]] = 1;
      if (i % ps[j] == 0) break;
    }
  }
  int ret = 0;
  for (auto p : ps) {
    auto t = p * p - 2;
    if (p > n || t > n) break;
    auto it = std::lower_bound(ps.begin(), ps.end(), t);
    if (it != ps.end() && *it == t) {
      if (t == p) ret++;
      else ret += 2;
    }
  }
  std::cout << ret << std::endl;
  return 0;
}
            
            
            
        