結果

問題 No.732 3PrimeCounting
コンテスト
ユーザー WutongDeath
提出日時 2025-12-21 23:33:51
言語 C++17
(gcc 13.3.0 + boost 1.89.0)
結果
AC  
実行時間 2,648 ms / 3,000 ms
コード長 1,110 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 723 ms
コンパイル使用メモリ 77,944 KB
実行使用メモリ 7,852 KB
最終ジャッジ日時 2025-12-21 23:34:24
合計ジャッジ時間 29,441 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 89
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:30:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   30 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long LL;

const int N = 300010;

int n, freq[N], ma;
LL ans;
vector<int> primes;
bool is_prime[N];

void SievePrime(int sz) {
    fill(is_prime, is_prime + sz, true);
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; i <= sz; ++i) {
        if (is_prime[i]) primes.push_back(i);
        for (auto p : primes) {
            if (1LL * i * p > sz) continue;
            is_prime[i * p] = false;
            if (i % p == 0) break;
        }
    }
}

int main() {
    scanf("%d", &n);
    
    SievePrime(n * 3);

    ans = 0LL;

    for (int i = 2; i < primes.size() && primes[i] <= n; ++i) {
        int c = primes[i];
        int b = primes[i - 1];
        for (int j = 0; j < i - 1; ++j) {
            int a = primes[j];
            ++freq[a + b];
        }
        for (int j = 0; j < primes.size() && primes[j] <= 3 * c; ++j) {
            int sum = primes[j];
            if (sum - c >= 0) {
                ans += freq[sum - c];
            }
        }
    }
    printf("%lld\n", ans);

    return 0;
}
0