結果

問題 No.843 Triple Primes
ユーザー ninja
提出日時 2019-06-28 22:53:57
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 790 bytes
コンパイル時間 1,542 ms
コンパイル使用メモリ 167,528 KB
実行使用メモリ 5,560 KB
最終ジャッジ日時 2024-09-19 14:08:52
合計ジャッジ時間 3,242 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int m = 500001;
  vector<bool> used(m);
  vector<ll> p;
  used[0] = used[1] = true;
  for (int i = 2; i < m; i++) {
    if (used[i]) continue;
    p.push_back(i);
    for (int j = 1; i * j < m; j++) used[i * j] = true;
  }
  set<ll> s;
  for (auto &i : p) s.insert(i * i);
  ll ans = 0;
  if (n < 2) {
    cout << 0 << endl;
    return 0;
  }
  for (auto &i : p) {
    if (i > n) break;
    if (s.find(i + 2) != s.end()) {
      if (i != 2) ans += 2;
      else ans += 1;
    }
  }
  cout << ans << endl;
  return 0;
}
0