Submission #25084524
Source Code Expand
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <utility>
#include <vector>
template <int M>
struct MInt {
unsigned int val;
MInt(): val(0) {}
MInt(long long x) : val(x >= 0 ? x % M : x % M + M) {}
static constexpr int get_mod() { return M; }
static void set_mod(int divisor) { assert(divisor == M); }
static void init(int x = 10000000) { inv(x, true); fact(x); fact_inv(x); }
static MInt inv(int x, bool init = false) {
// assert(0 <= x && x < M && std::__gcd(x, M) == 1);
static std::vector<MInt> inverse{0, 1};
int prev = inverse.size();
if (init && x >= prev) {
// "x!" and "M" must be disjoint.
inverse.resize(x + 1);
for (int i = prev; i <= x; ++i) inverse[i] = -inverse[M % i] * (M / i);
}
if (x < inverse.size()) return inverse[x];
unsigned int a = x, b = M; int u = 1, v = 0;
while (b) {
unsigned int q = a / b;
std::swap(a -= q * b, b);
std::swap(u -= q * v, v);
}
return u;
}
static MInt fact(int x) {
static std::vector<MInt> f{1};
int prev = f.size();
if (x >= prev) {
f.resize(x + 1);
for (int i = prev; i <= x; ++i) f[i] = f[i - 1] * i;
}
return f[x];
}
static MInt fact_inv(int x) {
static std::vector<MInt> finv{1};
int prev = finv.size();
if (x >= prev) {
finv.resize(x + 1);
finv[x] = inv(fact(x).val);
for (int i = x; i > prev; --i) finv[i - 1] = finv[i] * i;
}
return finv[x];
}
static MInt nCk(int n, int k) {
if (n < 0 || n < k || k < 0) return 0;
if (n - k > k) k = n - k;
return fact(n) * fact_inv(k) * fact_inv(n - k);
}
static MInt nPk(int n, int k) { return n < 0 || n < k || k < 0 ? 0 : fact(n) * fact_inv(n - k); }
static MInt nHk(int n, int k) { return n < 0 || k < 0 ? 0 : (k == 0 ? 1 : nCk(n + k - 1, k)); }
static MInt large_nCk(long long n, int k) {
if (n < 0 || n < k || k < 0) return 0;
inv(k, true);
MInt res = 1;
for (int i = 1; i <= k; ++i) res *= inv(i) * n--;
return res;
}
MInt pow(long long exponent) const {
MInt tmp = *this, res = 1;
while (exponent > 0) {
if (exponent & 1) res *= tmp;
tmp *= tmp;
exponent >>= 1;
}
return res;
}
MInt &operator+=(const MInt &x) { if((val += x.val) >= M) val -= M; return *this; }
MInt &operator-=(const MInt &x) { if((val += M - x.val) >= M) val -= M; return *this; }
MInt &operator*=(const MInt &x) { val = static_cast<unsigned long long>(val) * x.val % M; return *this; }
MInt &operator/=(const MInt &x) { return *this *= inv(x.val); }
bool operator==(const MInt &x) const { return val == x.val; }
bool operator!=(const MInt &x) const { return val != x.val; }
bool operator<(const MInt &x) const { return val < x.val; }
bool operator<=(const MInt &x) const { return val <= x.val; }
bool operator>(const MInt &x) const { return val > x.val; }
bool operator>=(const MInt &x) const { return val >= x.val; }
MInt &operator++() { if (++val == M) val = 0; return *this; }
MInt operator++(int) { MInt res = *this; ++*this; return res; }
MInt &operator--() { val = (val == 0 ? M : val) - 1; return *this; }
MInt operator--(int) { MInt res = *this; --*this; return res; }
MInt operator+() const { return *this; }
MInt operator-() const { return MInt(val ? M - val : 0); }
MInt operator+(const MInt &x) const { return MInt(*this) += x; }
MInt operator-(const MInt &x) const { return MInt(*this) -= x; }
MInt operator*(const MInt &x) const { return MInt(*this) *= x; }
MInt operator/(const MInt &x) const { return MInt(*this) /= x; }
friend std::ostream &operator<<(std::ostream &os, const MInt &x) { return os << x.val; }
friend std::istream &operator>>(std::istream &is, MInt &x) { long long val; is >> val; x = MInt(val); return is; }
};
namespace std { template <int M> MInt<M> abs(const MInt<M> &x) { return x; } }
namespace fast_fourier_transform {
using Real = double;
struct Complex {
Real re, im;
Complex(Real re = 0, Real im = 0) : re(re), im(im) {}
inline Complex operator+(const Complex &x) const { return Complex(re + x.re, im + x.im); }
inline Complex operator-(const Complex &x) const { return Complex(re - x.re, im - x.im); }
inline Complex operator*(const Complex &x) const { return Complex(re * x.re - im * x.im, re * x.im + im * x.re); }
inline Complex mul_real(Real r) const { return Complex(re * r, im * r); }
inline Complex mul_pin(Real r) const { return Complex(-im * r, re * r); }
inline Complex conj() const { return Complex(re, -im); }
};
std::vector<int> butterfly{0};
std::vector<std::vector<Complex>> zeta{{{1, 0}}};
void init(int n) {
const int prev_n = butterfly.size();
if (n <= prev_n) return;
butterfly.resize(n);
const int prev = zeta.size(), lg = __builtin_ctz(n);
for (int i = 1; i < prev_n; ++i) butterfly[i] <<= lg - prev;
for (int i = prev_n; i < n; ++i) butterfly[i] = (butterfly[i >> 1] >> 1) | ((i & 1) << (lg - 1));
zeta.resize(lg);
for (int i = prev; i < lg; ++i) {
zeta[i].resize(1 << i);
Real angle = -3.14159265358979323846 * 2 / (1 << (i + 1));
for (int j = 0; j < (1 << (i - 1)); ++j) {
zeta[i][j << 1] = zeta[i - 1][j];
Real theta = angle * ((j << 1) + 1);
zeta[i][(j << 1) + 1] = {std::cos(theta), std::sin(theta)};
}
}
}
void dft(std::vector<Complex> &a) {
const int n = a.size();
assert(__builtin_popcount(n) == 1);
init(n);
const int shift = __builtin_ctz(butterfly.size()) - __builtin_ctz(n);
for (int i = 0; i < n; ++i) {
const int j = butterfly[i] >> shift;
if (i < j) std::swap(a[i], a[j]);
}
for (int block = 1, den = 0; block < n; block <<= 1, ++den) {
for (int i = 0; i < n; i += (block << 1)) for (int j = 0; j < block; ++j) {
Complex tmp = a[i + j + block] * zeta[den][j];
a[i + j + block] = a[i + j] - tmp;
a[i + j] = a[i + j] + tmp;
}
}
}
template <typename T>
std::vector<Complex> real_dft(const std::vector<T> &a) {
const int n = a.size();
int lg = 1;
while ((1 << lg) < n) ++lg;
std::vector<Complex> c(1 << lg);
for (int i = 0; i < n; ++i) c[i].re = a[i];
dft(c);
return c;
}
void idft(std::vector<Complex> &a) {
const int n = a.size();
dft(a);
std::reverse(a.begin() + 1, a.end());
Real r = 1.0 / n;
for (int i = 0; i < n; ++i) a[i] = a[i].mul_real(r);
}
template <typename T>
std::vector<Real> convolution(const std::vector<T> &a, const std::vector<T> &b) {
const int a_size = a.size(), b_size = b.size(), c_size = a_size + b_size - 1;
int lg = 1;
while ((1 << lg) < c_size) ++lg;
const int n = 1 << lg, half = n >> 1, quarter = half >> 1;
std::vector<Complex> c(n);
for (int i = 0; i < a_size; ++i) c[i].re = a[i];
for (int i = 0; i < b_size; ++i) c[i].im = b[i];
dft(c);
c[0] = Complex(c[0].re * c[0].im, 0);
for (int i = 1; i < half; ++i) {
Complex i_square = c[i] * c[i], j_square = c[n - i] * c[n - i];
c[i] = (j_square.conj() - i_square).mul_pin(0.25);
c[n - i] = (i_square.conj() - j_square).mul_pin(0.25);
}
c[half] = Complex(c[half].re * c[half].im, 0);
c[0] = (c[0] + c[half] + (c[0] - c[half]).mul_pin(1)).mul_real(0.5);
const int den = __builtin_ctz(half);
for (int i = 1; i < quarter; ++i) {
const int j = half - i;
Complex tmp1 = c[i] + c[j].conj(), tmp2 = ((c[i] - c[j].conj()) * zeta[den][j]).mul_pin(1);
c[i] = (tmp1 - tmp2).mul_real(0.5);
c[j] = (tmp1 + tmp2).mul_real(0.5).conj();
}
if (quarter > 0) c[quarter] = c[quarter].conj();
c.resize(half);
idft(c);
std::vector<Real> res(c_size);
for (int i = 0; i < c_size; ++i) res[i] = (i & 1 ? c[i >> 1].im : c[i >> 1].re);
return res;
}
} // fast_fourier_transform
template <int T>
std::vector<MInt<T>> mod_convolution(const std::vector<MInt<T>> &a, const std::vector<MInt<T>> &b, const int pre = 15) {
using ModInt = MInt<T>;
const int a_size = a.size(), b_size = b.size(), c_size = a_size + b_size - 1;
int lg = 1;
while ((1 << lg) < c_size) ++lg;
const int n = 1 << lg, mask = (1 << pre) - 1;
std::vector<fast_fourier_transform::Complex> A(n), B(n);
for (int i = 0; i < a_size; ++i) {
const int a_i = a[i].val;
A[i] = fast_fourier_transform::Complex(a_i & mask, a_i >> pre);
}
for (int i = 0; i < b_size; ++i) {
const int b_i = b[i].val;
B[i] = fast_fourier_transform::Complex(b_i & mask, b_i >> pre);
}
fast_fourier_transform::dft(A);
fast_fourier_transform::dft(B);
const int half = n >> 1;
fast_fourier_transform::Complex tmp_a = A[0], tmp_b = B[0];
A[0] = {tmp_a.re * tmp_b.re, tmp_a.im * tmp_b.im};
B[0] = {tmp_a.re * tmp_b.im + tmp_a.im * tmp_b.re, 0};
for (int i = 1; i < half; ++i) {
const int j = n - i;
fast_fourier_transform::Complex a_l_i = (A[i] + A[j].conj()).mul_real(0.5), a_h_i = (A[j].conj() - A[i]).mul_pin(0.5);
fast_fourier_transform::Complex b_l_i = (B[i] + B[j].conj()).mul_real(0.5), b_h_i = (B[j].conj() - B[i]).mul_pin(0.5);
fast_fourier_transform::Complex a_l_j = (A[j] + A[i].conj()).mul_real(0.5), a_h_j = (A[i].conj() - A[j]).mul_pin(0.5);
fast_fourier_transform::Complex b_l_j = (B[j] + B[i].conj()).mul_real(0.5), b_h_j = (B[i].conj() - B[j]).mul_pin(0.5);
A[i] = a_l_i * b_l_i + (a_h_i * b_h_i).mul_pin(1);
B[i] = a_l_i * b_h_i + a_h_i * b_l_i;
A[j] = a_l_j * b_l_j + (a_h_j * b_h_j).mul_pin(1);
B[j] = a_l_j * b_h_j + a_h_j * b_l_j;
}
tmp_a = A[half]; tmp_b = B[half];
A[half] = {tmp_a.re * tmp_b.re, tmp_a.im * tmp_b.im};
B[half] = {tmp_a.re * tmp_b.im + tmp_a.im * tmp_b.re, 0};
fast_fourier_transform::idft(A);
fast_fourier_transform::idft(B);
std::vector<ModInt> res(c_size);
ModInt tmp1 = 1 << pre, tmp2 = 1LL << (pre << 1);
for (int i = 0; i < c_size; ++i) {
res[i] = std::llround(A[i].re);
res[i] += tmp1 * std::llround(B[i].re);
res[i] += tmp2 * std::llround(A[i].im);
}
return res;
}
int main() {
using ModInt = MInt<1000000001>;
int n;
std::cin >> n;
std::vector<ModInt> a(n + 1, 0), b(n + 1, 0);
for (int i = 1; i <= n; ++i) std::cin >> a[i] >> b[i];
std::vector<ModInt> ans = mod_convolution(a, b);
for (int i = 1; i <= n * 2; ++i) std::cout << ans[i] << '\n';
return 0;
}
Submission Info
| Submission Time |
|
| Task |
C - 高速フーリエ変換 |
| User |
emthrm |
| Language |
C++ (GCC 9.2.1) |
| Score |
100 |
| Code Size |
10489 Byte |
| Status |
AC |
| Exec Time |
91 ms |
| Memory |
18716 KiB |
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
100 / 100 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
00_sample_01 |
| All |
00_sample_01, 01_00_01, 01_01_19, 01_02_31, 01_03_22, 01_04_31, 01_05_40, 01_06_15, 01_07_39, 01_08_28, 01_09_30, 01_10_23, 01_11_33, 01_12_11, 01_13_28, 01_14_41, 01_15_26, 01_16_49, 01_17_34, 01_18_02, 01_19_33, 01_20_29, 02_00_51254, 02_01_82431, 02_02_17056, 02_03_34866, 02_04_6779, 02_05_65534, 02_06_65535, 02_07_65536, 02_08_65537, 02_09_65538, 02_10_100000 |
| Case Name |
Status |
Exec Time |
Memory |
| 00_sample_01 |
AC |
8 ms |
3912 KiB |
| 01_00_01 |
AC |
2 ms |
3936 KiB |
| 01_01_19 |
AC |
2 ms |
3796 KiB |
| 01_02_31 |
AC |
3 ms |
3952 KiB |
| 01_03_22 |
AC |
2 ms |
3804 KiB |
| 01_04_31 |
AC |
1 ms |
3820 KiB |
| 01_05_40 |
AC |
2 ms |
4016 KiB |
| 01_06_15 |
AC |
2 ms |
3828 KiB |
| 01_07_39 |
AC |
2 ms |
3776 KiB |
| 01_08_28 |
AC |
2 ms |
3912 KiB |
| 01_09_30 |
AC |
2 ms |
3896 KiB |
| 01_10_23 |
AC |
2 ms |
3884 KiB |
| 01_11_33 |
AC |
2 ms |
3896 KiB |
| 01_12_11 |
AC |
2 ms |
3936 KiB |
| 01_13_28 |
AC |
2 ms |
3864 KiB |
| 01_14_41 |
AC |
2 ms |
3868 KiB |
| 01_15_26 |
AC |
2 ms |
3892 KiB |
| 01_16_49 |
AC |
2 ms |
3916 KiB |
| 01_17_34 |
AC |
2 ms |
3892 KiB |
| 01_18_02 |
AC |
2 ms |
3908 KiB |
| 01_19_33 |
AC |
3 ms |
3912 KiB |
| 01_20_29 |
AC |
3 ms |
3780 KiB |
| 02_00_51254 |
AC |
52 ms |
11192 KiB |
| 02_01_82431 |
AC |
84 ms |
18320 KiB |
| 02_02_17056 |
AC |
27 ms |
7136 KiB |
| 02_03_34866 |
AC |
44 ms |
10812 KiB |
| 02_04_6779 |
AC |
15 ms |
4480 KiB |
| 02_05_65534 |
AC |
58 ms |
11172 KiB |
| 02_06_65535 |
AC |
57 ms |
11304 KiB |
| 02_07_65536 |
AC |
77 ms |
17976 KiB |
| 02_08_65537 |
AC |
75 ms |
18188 KiB |
| 02_09_65538 |
AC |
75 ms |
18028 KiB |
| 02_10_100000 |
AC |
91 ms |
18716 KiB |