結果

問題 No.732 3PrimeCounting
ユーザー kyunakyuna
提出日時 2020-03-08 11:44:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 603 ms / 3,000 ms
コード長 1,810 bytes
コンパイル時間 785 ms
コンパイル使用メモリ 85,812 KB
実行使用メモリ 38,520 KB
最終ジャッジ日時 2024-04-24 14:41:22
合計ジャッジ時間 47,177 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 453 ms
36,424 KB
testcase_01 AC 458 ms
36,168 KB
testcase_02 AC 453 ms
36,292 KB
testcase_03 AC 465 ms
36,296 KB
testcase_04 AC 459 ms
36,168 KB
testcase_05 AC 458 ms
36,168 KB
testcase_06 AC 453 ms
36,168 KB
testcase_07 AC 463 ms
36,172 KB
testcase_08 AC 465 ms
36,172 KB
testcase_09 AC 471 ms
36,168 KB
testcase_10 AC 456 ms
36,168 KB
testcase_11 AC 462 ms
36,168 KB
testcase_12 AC 461 ms
36,168 KB
testcase_13 AC 464 ms
36,164 KB
testcase_14 AC 462 ms
36,168 KB
testcase_15 AC 476 ms
36,296 KB
testcase_16 AC 480 ms
36,168 KB
testcase_17 AC 484 ms
36,292 KB
testcase_18 AC 476 ms
36,292 KB
testcase_19 AC 467 ms
36,168 KB
testcase_20 AC 473 ms
36,180 KB
testcase_21 AC 469 ms
36,352 KB
testcase_22 AC 470 ms
36,224 KB
testcase_23 AC 462 ms
36,172 KB
testcase_24 AC 472 ms
36,168 KB
testcase_25 AC 465 ms
36,404 KB
testcase_26 AC 470 ms
36,372 KB
testcase_27 AC 487 ms
36,188 KB
testcase_28 AC 473 ms
36,316 KB
testcase_29 AC 466 ms
36,340 KB
testcase_30 AC 477 ms
36,340 KB
testcase_31 AC 477 ms
36,232 KB
testcase_32 AC 468 ms
36,256 KB
testcase_33 AC 469 ms
36,256 KB
testcase_34 AC 470 ms
36,256 KB
testcase_35 AC 461 ms
36,200 KB
testcase_36 AC 470 ms
36,360 KB
testcase_37 AC 466 ms
36,304 KB
testcase_38 AC 469 ms
36,304 KB
testcase_39 AC 472 ms
36,236 KB
testcase_40 AC 484 ms
36,360 KB
testcase_41 AC 480 ms
36,132 KB
testcase_42 AC 478 ms
36,232 KB
testcase_43 AC 454 ms
36,344 KB
testcase_44 AC 459 ms
36,348 KB
testcase_45 AC 456 ms
36,320 KB
testcase_46 AC 447 ms
36,196 KB
testcase_47 AC 455 ms
36,200 KB
testcase_48 AC 463 ms
36,284 KB
testcase_49 AC 448 ms
36,412 KB
testcase_50 AC 448 ms
36,220 KB
testcase_51 AC 442 ms
36,224 KB
testcase_52 AC 445 ms
36,320 KB
testcase_53 AC 462 ms
36,532 KB
testcase_54 AC 529 ms
37,576 KB
testcase_55 AC 515 ms
37,452 KB
testcase_56 AC 514 ms
37,456 KB
testcase_57 AC 492 ms
36,904 KB
testcase_58 AC 510 ms
36,644 KB
testcase_59 AC 466 ms
36,420 KB
testcase_60 AC 478 ms
36,916 KB
testcase_61 AC 479 ms
36,912 KB
testcase_62 AC 533 ms
37,800 KB
testcase_63 AC 505 ms
37,212 KB
testcase_64 AC 487 ms
36,900 KB
testcase_65 AC 491 ms
37,024 KB
testcase_66 AC 455 ms
36,176 KB
testcase_67 AC 461 ms
36,300 KB
testcase_68 AC 524 ms
37,684 KB
testcase_69 AC 536 ms
37,560 KB
testcase_70 AC 521 ms
37,592 KB
testcase_71 AC 527 ms
37,460 KB
testcase_72 AC 502 ms
37,044 KB
testcase_73 AC 588 ms
38,152 KB
testcase_74 AC 576 ms
38,272 KB
testcase_75 AC 462 ms
36,400 KB
testcase_76 AC 537 ms
37,656 KB
testcase_77 AC 481 ms
36,812 KB
testcase_78 AC 548 ms
37,844 KB
testcase_79 AC 520 ms
37,700 KB
testcase_80 AC 540 ms
37,900 KB
testcase_81 AC 529 ms
37,476 KB
testcase_82 AC 463 ms
36,180 KB
testcase_83 AC 494 ms
36,796 KB
testcase_84 AC 488 ms
36,832 KB
testcase_85 AC 516 ms
37,348 KB
testcase_86 AC 547 ms
37,796 KB
testcase_87 AC 603 ms
38,520 KB
testcase_88 AC 599 ms
38,516 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