結果

問題 No.843 Triple Primes
ユーザー sansaqua
提出日時 2019-08-07 06:44:17
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 2,288 bytes
コンパイル時間 1,083 ms
コンパイル使用メモリ 93,888 KB
実行使用メモリ 6,052 KB
最終ジャッジ日時 2024-09-19 14:25:30
合計ジャッジ時間 2,137 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <utility>
#include <set>
#include <list>
#include <cassert>
#include <queue>
#include <cmath>
#include <algorithm>

using namespace std;
using ll = long long int;
#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define YES(n) std::cout << ((n) ? "YES" : "NO"  ) << "\n";
#define Yes(n) std::cout << ((n) ? "Yes" : "No" ) << "\n";

#define ALL_INCLUDED_ENVIRONMENT
/*
 * Eratosthenes' sieve; prime factorization
 */

#ifndef ALL_INCLUDED_ENVIRONMENT
#include <vector>
#include <utility>
#endif

const long long MAX_PRIME = 500000;
bool is_prime[MAX_PRIME+1];
std::vector<long long> primes;

void build_sieve () {
  // initialize
  for (long long x = 0; x <= MAX_PRIME; x++)
    is_prime[x] = 1;
  
  // special treatment for 2
  is_prime[0] = 0;
  is_prime[1] = 0;
  primes.push_back(2);
  for (long long x = 4; x <= MAX_PRIME; x += 2) {
    is_prime[x] = 0;
  }

  for (long long p = 3; p <= MAX_PRIME; p += 2) {
    if (is_prime[p] == 1) {
      primes.push_back(p);
      for (long long x = p*p; x <= MAX_PRIME; x += p) {
	is_prime[x] = 0;
      }
    }
  }
}

std::vector<std::pair<long long, long long> > factorize (long long x) {
  std::vector<std::pair<long long, long long> > res;
  for (auto p : primes) {
    if (x % p == 0) {
      std::pair<long long, long long> node = std::make_pair(p, 0);
      while (x % p == 0) {
	x = x / p;
	node.second++;
      }
      res.push_back(node);
    }
  }
  if (x != 1)
    res.push_back(std::make_pair(x, 1));
  return res;
}

template <class T, class Alloc, template <class, class> class C> std::ostream& operator<<(std::ostream& o, const C<T, Alloc>& v) {
  o << "{";
  bool init = 1;
  for (auto e : v) {
    if (init) { init = 0; }
    else { o << ", "; }
    o << e;
  }
  o << "}";
  return o;
}

int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);

  ll n;
  cin >> n;

  build_sieve();
  primes.erase(upper_bound(primes.begin(), primes.end(), n), primes.end());
  set<ll> sq_set;

  // cout << primes.size() << endl;
  ll ans = 0;
  for (auto r : primes)
    sq_set.insert(r*r);
  for (auto p : primes) {
    if (sq_set.find(p+2) != sq_set.end())
      ans += 2;
  }
  if (n >= 2) // exclude the duplication of (2, 2, 2)
    ans--;
  cout << ans << "\n";
  return 0;
}
0