提交记录 17232


用户 题目 状态 得分 用时 内存 语言 代码长度
RiverHamster 1002. 测测你的多项式乘法 Accepted 100 389.891 ms 106128 KB C++11 1.50 KB
提交时间 评测时间
2021-12-21 16:52:16 2021-12-21 16:52:19
#include <complex>
#include <algorithm>
#pragma GCC optimize("fast-math")
using namespace std;

const int N = 1 << 21;

using cp = complex<double>;
const double pi = acos(-1);
 
cp root[N];
 
cp MUL(const cp &a, const cp &b) {
  double x = a.real(), y = a.imag(), u = b.real(), v = b.imag();
  return cp(x * u - y * v, x * v + y * u);
}
 
void init_roots() {
  for (int i = N / 2; i; i >>= 1) {
    double theta = pi / i;
    for (int j = 0; j < i; ++j)
      root[i + j] = polar(1.0, j * theta);
  }
}
void DIF(cp *a, int n) {
  for (int l = n, k = l / 2; k; l >>= 1, k >>= 1) {
    cp *w = root + k;
    for (cp *i = a, *t = a + n; i != t; i += l)
      for (int j = 0; j < k; ++j) {
        cp t = i[j];
        i[j] += i[j + k];
        i[j + k] = (t - i[j + k]) * w[j];
      }
  }
}
void DIT(cp *a, int n) {
  for (int l = 2, k = 1; k < n; l <<= 1, k <<= 1) {
    cp *w = root + k;
    for (cp *i = a, *t = a + n; i != t; i += l)
      for (int j = 0; j < k; ++j) {
        cp t = i[j + k] * w[j];
        i[j + k] = i[j] - t;
        i[j] += t;
      }
  }
  double iv = 1.0 / n;
  reverse(a + 1, a + n);
  for (int i = 0; i < n; ++i)
    a[i] *= iv;
}

cp A[N], B[N];

void poly_multiply(unsigned *a, int n, unsigned *b, int m, unsigned *c) {
  init_roots();
  for (int i = 0; i <= n; ++i)
    A[i].real(a[i]);
  for (int i = 0; i <= m; ++i)
    B[i].real(b[i]);
  DIF(A, N); DIF(B, N);
  for (int i = 0; i < N; ++i)
    A[i] = A[i] * B[i];
  DIT(A, N);
  for (int i = 0; i <= n + m; ++i)
    c[i] = lround(A[i].real());
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #1389.891 ms103 MB + 656 KBAcceptedScore: 100


Judge Duck Online | 评测鸭在线
Server Time: 2026-03-17 22:26:36 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠