結果

問題 No.732 3PrimeCounting
ユーザー kyunakyuna
提出日時 2020-03-08 11:44:34
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 712 ms / 3,000 ms
コード長 1,810 bytes
コンパイル時間 945 ms
コンパイル使用メモリ 84,280 KB
実行使用メモリ 38,480 KB
最終ジャッジ日時 2023-08-06 20:02:18
合計ジャッジ時間 52,579 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 519 ms
36,108 KB
testcase_01 AC 506 ms
36,472 KB
testcase_02 AC 509 ms
36,160 KB
testcase_03 AC 518 ms
36,160 KB
testcase_04 AC 510 ms
36,248 KB
testcase_05 AC 489 ms
36,132 KB
testcase_06 AC 486 ms
36,160 KB
testcase_07 AC 503 ms
36,136 KB
testcase_08 AC 515 ms
36,192 KB
testcase_09 AC 512 ms
36,296 KB
testcase_10 AC 521 ms
36,100 KB
testcase_11 AC 514 ms
36,132 KB
testcase_12 AC 499 ms
36,136 KB
testcase_13 AC 500 ms
36,128 KB
testcase_14 AC 491 ms
36,056 KB
testcase_15 AC 497 ms
36,164 KB
testcase_16 AC 495 ms
36,172 KB
testcase_17 AC 482 ms
36,216 KB
testcase_18 AC 486 ms
36,092 KB
testcase_19 AC 497 ms
36,212 KB
testcase_20 AC 495 ms
36,332 KB
testcase_21 AC 487 ms
36,160 KB
testcase_22 AC 507 ms
36,152 KB
testcase_23 AC 507 ms
36,052 KB
testcase_24 AC 487 ms
36,236 KB
testcase_25 AC 512 ms
36,192 KB
testcase_26 AC 497 ms
36,320 KB
testcase_27 AC 507 ms
36,112 KB
testcase_28 AC 498 ms
36,068 KB
testcase_29 AC 515 ms
36,308 KB
testcase_30 AC 501 ms
36,368 KB
testcase_31 AC 495 ms
36,224 KB
testcase_32 AC 501 ms
36,336 KB
testcase_33 AC 501 ms
36,196 KB
testcase_34 AC 506 ms
36,244 KB
testcase_35 AC 510 ms
36,156 KB
testcase_36 AC 500 ms
36,124 KB
testcase_37 AC 491 ms
36,228 KB
testcase_38 AC 510 ms
36,108 KB
testcase_39 AC 512 ms
36,364 KB
testcase_40 AC 504 ms
36,200 KB
testcase_41 AC 515 ms
36,172 KB
testcase_42 AC 515 ms
36,296 KB
testcase_43 AC 491 ms
36,492 KB
testcase_44 AC 500 ms
36,336 KB
testcase_45 AC 494 ms
36,180 KB
testcase_46 AC 504 ms
36,216 KB
testcase_47 AC 503 ms
36,320 KB
testcase_48 AC 510 ms
36,612 KB
testcase_49 AC 492 ms
36,592 KB
testcase_50 AC 509 ms
36,212 KB
testcase_51 AC 511 ms
36,380 KB
testcase_52 AC 503 ms
36,072 KB
testcase_53 AC 519 ms
36,404 KB
testcase_54 AC 555 ms
37,364 KB
testcase_55 AC 576 ms
37,528 KB
testcase_56 AC 576 ms
37,440 KB
testcase_57 AC 534 ms
36,532 KB
testcase_58 AC 528 ms
36,740 KB
testcase_59 AC 508 ms
36,296 KB
testcase_60 AC 608 ms
37,088 KB
testcase_61 AC 529 ms
36,740 KB
testcase_62 AC 576 ms
37,792 KB
testcase_63 AC 561 ms
37,004 KB
testcase_64 AC 543 ms
37,040 KB
testcase_65 AC 532 ms
37,168 KB
testcase_66 AC 507 ms
36,160 KB
testcase_67 AC 499 ms
36,156 KB
testcase_68 AC 567 ms
37,444 KB
testcase_69 AC 584 ms
37,496 KB
testcase_70 AC 575 ms
37,460 KB
testcase_71 AC 555 ms
37,400 KB
testcase_72 AC 540 ms
37,140 KB
testcase_73 AC 635 ms
38,112 KB
testcase_74 AC 633 ms
38,452 KB
testcase_75 AC 503 ms
36,148 KB
testcase_76 AC 578 ms
37,648 KB
testcase_77 AC 524 ms
36,632 KB
testcase_78 AC 600 ms
37,920 KB
testcase_79 AC 598 ms
37,508 KB
testcase_80 AC 600 ms
37,816 KB
testcase_81 AC 593 ms
37,352 KB
testcase_82 AC 517 ms
36,176 KB
testcase_83 AC 529 ms
36,592 KB
testcase_84 AC 529 ms
36,644 KB
testcase_85 AC 572 ms
37,256 KB
testcase_86 AC 599 ms
37,792 KB
testcase_87 AC 676 ms
38,480 KB
testcase_88 AC 712 ms
38,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

#include <complex>
struct FFT {
    using Complex = complex<double>;
    const double PI = acos(-1);
    const int sz = 1 << 19;     // 524288
    void dft(vector<Complex> &x, bool inv) {
    	for (int i = sz >> 1; i >= 1; i >>= 1) {
    		Complex bs = polar(1.0, 2.0 * PI * i / sz * (inv ? -1 : 1)), w = 1;
            vector<Complex> y(sz);
    		for (int j = 0; 2 * j < sz; j += i) {
    			for (int k = 0; k < i; k++) {
    				y[j + k]             = x[2 * j + k] + w * x[2 * j + i + k];
    				y[j + k + (sz >> 1)] = x[2 * j + k] - w * x[2 * j + i + k];
    			}
    			w *= bs;
    		}
    		x = y;
    	}
    	if (inv) for (int i = 0; i < sz; i++) x[i] /= sz;
    }
    vector<long long> multiply(vector<long long> &a, vector<long long> &b) {
        vector<Complex> x(sz), y(sz);
        for (int i = 0; i < (int)a.size(); i++) x[i] = Complex(a[i], 0.0);
        for (int i = 0; i < (int)b.size(); i++) y[i] = Complex(b[i], 0.0);
        dft(x, false), dft(y, false);
        for (int i = 0; i < sz; i++) x[i] *= y[i];
        dft(x, true);
        vector<long long> c(sz);
        for (int i = 0; i < sz; i++) c[i] = (long long)(x[i].real() + 0.5);
        return c;
    }
} fft;

bool is_prime(long long x) {
    if (x <= 1) return false;
    for (long long i = 2; i * i <= x; i++) if (x % i == 0) return false;
    return true;
}

int main() {
    int n; cin >> n;
    vector<long long> v(n + 1), w(n * 2 + 1);
    for (int i = 0; i <= n; i++) v[i] = w[i * 2] = is_prime(i);
    auto x = fft.multiply(v, v);
    auto y = fft.multiply(v, x);
    auto z = fft.multiply(v, w);
    long long ans = 0;
    for (int i = 0; i <= n * 3; i++) if (is_prime(i)) ans += y[i] - z[i] * 3;
    cout << ans / 6 << endl;
	return 0;
}
0