結果

問題 No.843 Triple Primes
ユーザー もりをもりを
提出日時 2019-06-28 21:31:43
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 1,642 bytes
コンパイル時間 1,832 ms
コンパイル使用メモリ 171,752 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 17:29:44
合計ジャッジ時間 3,395 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 12 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 9 ms
4,348 KB
testcase_08 AC 10 ms
4,348 KB
testcase_09 AC 11 ms
4,348 KB
testcase_10 AC 9 ms
4,348 KB
testcase_11 AC 11 ms
4,348 KB
testcase_12 AC 11 ms
4,348 KB
testcase_13 AC 10 ms
4,348 KB
testcase_14 AC 10 ms
4,348 KB
testcase_15 AC 9 ms
4,348 KB
testcase_16 AC 9 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 4 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 6 ms
4,348 KB
testcase_23 AC 7 ms
4,348 KB
testcase_24 AC 4 ms
4,348 KB
testcase_25 AC 3 ms
4,348 KB
testcase_26 AC 11 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 11 ms
4,348 KB
testcase_29 AC 4 ms
4,348 KB
testcase_30 AC 11 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 5 ms
4,348 KB
testcase_34 AC 6 ms
4,348 KB
testcase_35 AC 11 ms
4,348 KB
testcase_36 AC 4 ms
4,348 KB
testcase_37 AC 8 ms
4,348 KB
testcase_38 AC 7 ms
4,348 KB
testcase_39 AC 10 ms
4,348 KB
testcase_40 AC 2 ms
4,348 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 10 ms
4,348 KB
testcase_43 AC 10 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

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