提交记录 21120


用户 题目 状态 得分 用时 内存 语言 代码长度
lyreqwq 1002. 测测你的多项式乘法 Accepted 100 670.188 ms 55868 KB C++17 2.61 KB
提交时间 评测时间
2024-01-31 14:50:45 2024-01-31 14:50:49
# include <bits/stdc++.h>

using namespace std;

namespace lyre {
  constexpr int P(998'244'353);
  constexpr int pow(int a, int n) {
    auto r(1);
    while (n) {
      r = 1l * r * (n & 1 ? a : 1) % P;
      n >>= 1, a = 1l * a * a % P;
    }
    return r;
  }
  constexpr int inv(int a)
  { return pow(a, P - 2); }
  class poly : public vector<int> {
    public:
    using vector<int>::vector;
    void reserve() {
      while (size() != (size() & -size()))
        emplace_back();
    }
    void dif() {
      int const n(size());
      vector<int> trans(n);
      for (int i(0); i < n; ++i)
        trans[i] = (trans[i >> 1] >> 1) + (i & 1) * (n >> 1);
      for (int i(0); i < n; ++i)
        if (i < trans[i]) std::swap(at(i), at(trans[i]));
      for (int mid(1); mid < n; mid *= 2) {
        auto w1(pow(3, (P - 1) / (mid * 2)));
        for (int i(0); i < n; i += mid * 2) {
          auto wk(1);
          for (int j(0), k(mid); j < mid; ++j, ++k) {
            auto x(at(i + j));
            auto y(1l * wk * at(i + k) % P);
            at(i + j) = (x + y) % P;
            at(i + k) = (x + P - y) % P;
            wk = 1l * wk * w1 % P;
          }
        }
      }
    }
    void dit() {
      int const n(size());
      vector<int> trans(n);
      for (int i(0); i < n; ++i)
        trans[i] = (trans[i >> 1] >> 1) + (i & 1) * (n >> 1);
      for (int i(0); i < n; ++i)
        if (i < trans[i]) std::swap(at(i), at(trans[i]));
      for (int mid(1); mid < n; mid *= 2) {
        auto w1(pow(inv(3), (P - 1) / (mid * 2)));
        for (int i(0); i < n; i += mid * 2) {
          auto wk(1);
          for (int j(0), k(mid); j < mid; ++j, ++k) {
            auto x(at(i + j));
            auto y(1l * wk * at(i + k) % P);
            at(i + j) = (x + y) % P;
            at(i + k) = (x + P - y) % P;
            wk = 1l * wk * w1 % P;
          }
        }
      }
      for (int i(0); i < n; ++i)
        at(i) = 1l * at(i) * inv(n) % P;
    }
    poly operator*(const poly &o) const {
      auto f(*this), g(o);
      poly h(f.size() + g.size() - 1);
      h.reserve();
      f.resize(h.size()), f.dif();
      g.resize(h.size()), g.dif();
      for (auto i(0u); i < h.size(); ++i)
        h[i] = 1l * f[i] * g[i] % P;
      h.dit();
      return h;
    }
  };
  void poly_multiply(unsigned *a, int n, unsigned *b, int m, unsigned *c) {
    poly f(n + 1), g(m + 1);
    for (int i(0); i <= n; ++i) f[i] = a[i];
    for (int i(0); i <= m; ++i) g[i] = b[i];
    poly const h(f * g);
    for (int i(0); i <= n + m; ++i) c[i] = h[i];
  }
}

void poly_multiply(unsigned *a, int n, unsigned *b, int m, unsigned *c)
{ return lyre::poly_multiply(a, n, b, m, c); }

CompilationN/AN/ACompile OKScore: N/A

Testcase #1670.188 ms54 MB + 572 KBAcceptedScore: 100


Judge Duck Online | 评测鸭在线
Server Time: 2024-11-23 05:49:01 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠