結果

問題 No.732 3PrimeCounting
ユーザー tnakao0123
提出日時 2018-09-14 08:09:22
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 318 ms / 3,000 ms
コード長 1,419 bytes
コンパイル時間 657 ms
コンパイル使用メモリ 88,360 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-01 04:37:01
合計ジャッジ時間 6,461 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 89
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:63:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   63 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 732.cc:  No.732 3PrimeCounting - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_P = MAX_N * 3;

/* typedef */

typedef long long ll;
typedef vector<int> vi;

/* global variables */

bool primes[MAX_P + 1];
int ds[MAX_P];

/* subroutines */

int gen_primes(int maxp, vi &pnums) {
  memset(primes, true, sizeof(primes));
  primes[0] = primes[1] = false;

  int p;
  for (p = 2; p * p <= maxp; p++)
    if (primes[p]) {
      pnums.push_back(p);
      for (int q = p * p; q <= maxp; q += p) primes[q] = false;
    }
  for (; p <= maxp; p++)
    if (primes[p]) pnums.push_back(p);
  return (int)pnums.size();
}

/* main */

int main() {
  int n;
  scanf("%d", &n);
  int n3 = n * 3;
  
  vi pnums;
  int pn = gen_primes(n3, pnums);
  //printf("pn=%d\n", pn);

  memset(ds, 0, sizeof(ds));
  ll sum = 0;
  for (int i = 2; pnums[i] <= n; i++) {
    for (int j = 0; j < i - 1; j++)
      ds[pnums[j] + pnums[i - 1]]++;

    for (int j = i + 1; j < pn; j++)
      sum += ds[pnums[j] - pnums[i]];
  }

  printf("%lld\n", sum);
  return 0;
}
0