結果

問題 No.843 Triple Primes
ユーザー sansaquasansaqua
提出日時 2019-08-07 06:44:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 2,288 bytes
コンパイル時間 989 ms
コンパイル使用メモリ 94,088 KB
実行使用メモリ 6,196 KB
最終ジャッジ日時 2023-10-19 18:05:41
合計ジャッジ時間 2,419 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
4,484 KB
testcase_01 AC 14 ms
6,196 KB
testcase_02 AC 4 ms
4,484 KB
testcase_03 AC 4 ms
4,484 KB
testcase_04 AC 4 ms
4,484 KB
testcase_05 AC 4 ms
4,484 KB
testcase_06 AC 4 ms
4,484 KB
testcase_07 AC 12 ms
5,776 KB
testcase_08 AC 12 ms
5,932 KB
testcase_09 AC 14 ms
6,196 KB
testcase_10 AC 13 ms
5,932 KB
testcase_11 AC 13 ms
5,984 KB
testcase_12 AC 14 ms
6,196 KB
testcase_13 AC 14 ms
6,032 KB
testcase_14 AC 14 ms
6,032 KB
testcase_15 AC 12 ms
5,796 KB
testcase_16 AC 12 ms
5,932 KB
testcase_17 AC 4 ms
4,484 KB
testcase_18 AC 4 ms
4,484 KB
testcase_19 AC 4 ms
4,484 KB
testcase_20 AC 7 ms
4,876 KB
testcase_21 AC 5 ms
4,612 KB
testcase_22 AC 9 ms
5,404 KB
testcase_23 AC 9 ms
5,404 KB
testcase_24 AC 6 ms
4,876 KB
testcase_25 AC 6 ms
4,664 KB
testcase_26 AC 14 ms
6,196 KB
testcase_27 AC 5 ms
4,484 KB
testcase_28 AC 13 ms
6,048 KB
testcase_29 AC 7 ms
4,876 KB
testcase_30 AC 14 ms
6,196 KB
testcase_31 AC 5 ms
4,484 KB
testcase_32 AC 5 ms
4,484 KB
testcase_33 AC 7 ms
4,900 KB
testcase_34 AC 9 ms
5,232 KB
testcase_35 AC 14 ms
6,196 KB
testcase_36 AC 5 ms
4,616 KB
testcase_37 AC 12 ms
5,716 KB
testcase_38 AC 10 ms
5,496 KB
testcase_39 AC 13 ms
6,064 KB
testcase_40 AC 5 ms
4,484 KB
testcase_41 AC 4 ms
4,484 KB
testcase_42 AC 12 ms
5,932 KB
testcase_43 AC 13 ms
5,980 KB
権限があれば一括ダウンロードができます

ソースコード

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