結果

問題 No.843 Triple Primes
ユーザー もりを
提出日時 2019-06-28 21:31:43
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 10 ms / 2,000 ms
コード長 1,642 bytes
コンパイル時間 1,479 ms
コンパイル使用メモリ 170,816 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-19 13:49:06
合計ジャッジ時間 2,678 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int (i)=0;(i)<(n);++(i))
#define FOR(i,a,b) for(int (i)=(a);(i)<(b);++(i))
#define EACH(e,v) for(auto& e:v)
#define ALL(v) (v).begin(),(v).end()
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort((v).rbegin(),(v).rend())
#define PERM(v) SORT(v);for(bool c##p=1;c##p;c##p=next_permutation(ALL(v)))
#define UNIQUE(v) SORT(v);(v).erase(unique(ALL(v)),(v).end())
template<typename A,typename B> inline bool chmax(A &a,const B &b){if(a<b){a=b;return 1;}return 0;}
template<typename A,typename B> inline bool chmin(A &a,const B &b){if(a>b){a=b;return 1;}return 0;}

const int MOD = (int)1e9 + 7;
const int INF = 1 << 30;
const ll INFF = 1LL << 62;

// 下にy, 右にx
enum {R, U, L, D};
const int dx[] = {1,  0, -1, 0};
const int dy[] = {0, -1,  0, 1};

using vb = vector<bool>;

/*
・エラトステネスの篩
  > O(nloglogn)
[使用例] vb prime = Eratosthenes(100000);
[備考] nが10^6以下のときに使うべき
*/

inline vb Eratosthenes(const int n) {
  vb r(n+1,true);
  for(int i=2;i*i<=n;++i){
    if(r[i]){
      for(int j=i*2;j<=n;j+=i)r[j]=false;
    }
  }
  if(r.size()>2){r[0]=0;r[1]=0;}
  else if(r.size()>1){r[0]=0;}
  return r;
}

signed main() {

    int N;
    cin >> N;

    vector<int> prime;
    auto era = Eratosthenes(N);
    REP(i, N + 1) if (era[i]) prime.emplace_back(i);

    ll res = 0;
    EACH(r, prime) EACH(q, prime) {
        ll p = (ll)r * r - q;
        if (p >= 2 && p <= N) {
            if (era[p]) ++res;
        }
        if (p > N) break;
    }

    cout << res << endl;

    // EACH(e,prime)cerr<<e<<endl;

}
0