結果
| 問題 | No.732 3PrimeCounting |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-09-07 21:48:34 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 2,180 ms / 3,000 ms |
| コード長 | 4,264 bytes |
| 記録 | |
| コンパイル時間 | 2,679 ms |
| コンパイル使用メモリ | 203,816 KB |
| 最終ジャッジ日時 | 2025-01-06 12:57:06 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 89 |
ソースコード
//=================================
// Created on: 2018/09/07 21:33:10
//=================================
#include <bits/stdc++.h>
#define show(x) std::cerr << #x << " = " << x << std::endl
using ll = long long;
using ull = unsigned long long;
using ld = long double;
constexpr std::size_t PC(const ull v)
{
ull count = (v & 0x5555555555555555ULL) + ((v >> 1) & 0x5555555555555555ULL);
count = (count & 0x3333333333333333ULL) + ((count >> 2) & 0x3333333333333333ULL);
count = (count & 0x0f0f0f0f0f0f0f0fULL) + ((count >> 4) & 0x0f0f0f0f0f0f0f0fULL);
count = (count & 0x00ff00ff00ff00ffULL) + ((count >> 8) & 0x00ff00ff00ff00ffULL);
count = (count & 0x0000ffff0000ffffULL) + ((count >> 16) & 0x0000ffff0000ffffULL);
return static_cast<std::size_t>((count & 0x00000000ffffffffULL) + ((count >> 32) & 0x00000000ffffffffULL));
}
constexpr std::size_t LG(ull v) { return v == 0 ? 0 : (v--, v |= (v >> 1), v |= (v >> 2), v |= (v >> 4), v |= (v >> 8), v |= (v >> 16), v |= (v >> 32), PC(v)); }
constexpr ull SZ(const ull v) { return 1ULL << LG(v); }
class FastFourierTransformation
{
private:
using T = ll;
using F = ld;
static constexpr F PI = static_cast<F>(3.141592653589793238462643383279502884);
using P = std::pair<F, F>;
static P plus(const P& c1, const P& c2) { return P{c1.first + c2.first, c1.second + c2.second}; }
static P minus(const P& c1, const P& c2) { return P{c1.first - c2.first, c1.second - c2.second}; }
static P mul(const P& c1, const P& c2) { return P{c1.first * c2.first - c1.second * c2.second, c1.first * c2.second + c1.second * c2.first}; }
public:
FastFourierTransformation() = delete;
static std::vector<P> fft(std::vector<P>& a, const bool rev = false)
{
const std::size_t size = a.size(), height = LG(size);
for (std::size_t i = 0; i < size; i++) {
std::size_t j = 0;
for (std::size_t k = 0; k < height; k++) { j |= (i >> k & 1) << (height - 1 - k); }
if (i < j) { swap(a[i], a[j]); }
}
for (std::size_t i = 1; i < size; i <<= 1) {
for (std::size_t j = 0; j < i; j++) {
const F theta = 2 * PI / (F)(i << 1) * (F)(rev ? -1 : 1) * (F)j;
const P w{std::cos(theta), std::sin(theta)};
for (std::size_t k = 0; k < size; k += (i << 1)) {
const P s = a[j + k + 0], t = mul(a[j + k + i], w);
a[j + k + 0] = P{s.first + t.first, s.second + t.second}, a[j + k + i] = P{s.first - t.first, s.second - t.second};
}
}
}
if (not rev) { return a; }
for (std::size_t i = 0; i < size; i++) { a[i].first /= (F)size, a[i].second /= (F)size; }
return a;
}
static std::vector<T> convolute(const std::vector<T>& a, const std::vector<T>& b) // ans[i] = \sum_{A+B = i} a[A]*b[B]
{
const std::size_t size = a.size() + b.size() - 1, t = (std::size_t)SZ(size);
std::vector<P> A(t), B(t);
for (std::size_t i = 0; i < a.size(); i++) { A[i].first = (F)a[i]; }
for (std::size_t i = 0; i < b.size(); i++) { B[i].first = (F)b[i]; }
A = fft(A), B = fft(B);
for (std::size_t i = 0; i < t; i++) { A[i] = mul(A[i], B[i]); }
A = fft(A, true);
std::vector<T> ans(size);
for (std::size_t i = 0; i < size; i++) { ans[i] = (T)std::round(A[i].first); }
return ans;
}
};
int main()
{
ll MAX;
std::cin >> MAX;
std::vector<ll> a(3 * MAX + 1, 1);
for (ll i = 2; i <= 3 * MAX; i++) {
if (not a[i]) { continue; }
for (ll j = 2; i * j <= 3 * MAX; j++) { a[i * j] = false; }
}
a[0] = a[1] = 0;
const auto A = a;
a.resize(MAX + 1);
const auto b = FastFourierTransformation::convolute(a, a);
const auto c = FastFourierTransformation::convolute(a, b);
ll ans = 0;
for (ll i = 0; i <= 3 * MAX; i++) {
if (A[i]) { ans += c[i]; }
}
std::vector<ll> B(2 * MAX + 1, 0);
for (ll i = 0; i <= MAX; i++) { B[2 * i] = A[i]; }
const auto C = FastFourierTransformation::convolute(B, a);
for (ll i = 0; i <= 3 * MAX; i++) {
if (A[i]) { ans -= 3 * C[i]; }
}
std::cout << ans / 6 << std::endl;
return 0;
}