結果
| 問題 | No.732 3PrimeCounting |
| コンテスト | |
| ユーザー |
kyuna
|
| 提出日時 | 2020-03-08 11:44:34 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 761 ms / 3,000 ms |
| コード長 | 1,810 bytes |
| 記録 | |
| コンパイル時間 | 868 ms |
| コンパイル使用メモリ | 85,052 KB |
| 実行使用メモリ | 38,644 KB |
| 最終ジャッジ日時 | 2024-11-06 23:06:22 |
| 合計ジャッジ時間 | 56,972 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 89 |
ソースコード
#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;
}
kyuna