結果

問題 No.931 Multiplicative Convolution
ユーザー noshi91noshi91
提出日時 2019-11-22 22:24:55
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 5,631 bytes
コンパイル時間 1,063 ms
コンパイル使用メモリ 101,192 KB
実行使用メモリ 13,192 KB
最終ジャッジ日時 2024-04-19 11:55:18
合計ジャッジ時間 2,765 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 8 ms
6,944 KB
testcase_08 AC 64 ms
12,976 KB
testcase_09 AC 50 ms
12,992 KB
testcase_10 AC 59 ms
13,100 KB
testcase_11 AC 53 ms
12,920 KB
testcase_12 AC 37 ms
8,428 KB
testcase_13 AC 63 ms
12,992 KB
testcase_14 AC 63 ms
13,192 KB
testcase_15 AC 64 ms
13,184 KB
testcase_16 AC 64 ms
13,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#define NDEBUG
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <vector>

namespace n91 {

using i8 = std::int_fast8_t;
using i32 = std::int_fast32_t;
using i64 = std::int_fast64_t;
using u8 = std::uint_fast8_t;
using u32 = std::uint_fast32_t;
using u64 = std::uint_fast64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

struct rep {
  struct itr {
    usize i;
    constexpr itr(const usize i) noexcept : i(i) {}
    void operator++() noexcept { ++i; }
    constexpr usize operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
  };
  const itr f, l;
  constexpr rep(const usize f, const usize l) noexcept
      : f(std::min(f, l)), l(l) {}
  constexpr auto begin() const noexcept { return f; }
  constexpr auto end() const noexcept { return l; }
};
struct revrep {
  struct itr {
    usize i;
    constexpr itr(const usize i) noexcept : i(i) {}
    void operator++() noexcept { --i; }
    constexpr usize operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
  };
  const itr f, l;
  constexpr revrep(const usize f, const usize l) noexcept
      : f(l - 1), l(std::min(f, l) - 1) {}
  constexpr auto begin() const noexcept { return f; }
  constexpr auto end() const noexcept { return l; }
};
template <class T> auto md_vec(const usize n, const T &value) {
  return std::vector<T>(n, value);
}
template <class... Args> auto md_vec(const usize n, Args... args) {
  return std::vector<decltype(md_vec(args...))>(n, md_vec(args...));
}
template <class T> constexpr T difference(const T &a, const T &b) noexcept {
  return a < b ? b - a : a - b;
}
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
template <class T> T scan() {
  T ret;
  std::cin >> ret;
  return ret;
}

} // namespace n91
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <vector>

namespace n91 {

using usize = std::size_t;
using u64 = std::uint_fast64_t;

static constexpr u64 Modulus = 998244353;
static constexpr u64 PrimitiveRoot = 3;

void ntt(std::vector<u64> &a) {
  const usize size = a.size();
  const usize m = size - 1;
  std::vector<u64> b(size);
  u64 root = 1;
  {
    u64 base = PrimitiveRoot;
    u64 exp = (Modulus - 1) / size;
    while (exp != 0) {
      if (exp % 2 != 0) {
        root = root * base % Modulus;
      }
      exp /= 2;
      base = base * base % Modulus;
    }
  }
  for (usize i = size; (i /= 2) != 0;) {
    std::swap(a, b);
    u64 c = 1;
    u64 d = root;
    for (usize j = 1; j != i; j *= 2) {
      d = d * d % Modulus;
    }
    for (usize j = 0; j != size; j += i) {
      const usize l = j * 2 & m, r = l + i;
      for (usize k = 0; k != i; ++k) {
        a[j + k] = (b[l + k] + b[r + k] * c) % Modulus;
      }
      c = c * d % Modulus;
    }
  }
}

void inv_ntt(std::vector<u64> &a) {
  ntt(a);
  std::reverse(a.begin() + 1, a.end());
  const u64 inv = Modulus - (Modulus - 1) / a.size();
  for (auto &e : a) {
    e = e * inv % Modulus;
  }
}

} // namespace n91

#include <algorithm>
#include <chrono>
#include <iostream>
#include <random>
#include <utility>

namespace n91 {

u64 find_primroot(const u64 p) {
  std::vector<u64> factor;
  {
    u64 q = p - 1;
    for (u64 i = 2; i * i <= q; i += 1) {
      if (q % i == 0) {
        factor.push_back(i);
        do {
          q /= i;
        } while (q % i == 0);
      }
    }
    if (q != 1) {
      factor.push_back(q);
    }
  }
  for (u64 &e : factor) {
    e = (p - 1) / e;
  }
  std::mt19937_64 rnd(std::chrono::duration_cast<std::chrono::milliseconds>(
                          std::chrono::system_clock::now().time_since_epoch())
                          .count());
  while (true) {
    const u64 cand = rnd() % (p - 1) + 1;
    if (std::none_of(factor.cbegin(), factor.cend(), [&](u64 exp) {
          u64 base = cand;
          u64 a = 1;
          while (exp != 0) {
            if (exp % 2 == 1) {
              a = a * base % p;
            }
            base = base * base % p;
            exp /= 2;
          }
          return a == 1;
        })) {
      return cand;
    }
  }
}

void main_() {
  //*
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  //*/
  const u64 p = scan<u64>();
  const auto is_pr = [&](const u64 a) {
    u64 t = 1;
    for (const usize i : rep(0, p - 2)) {
      t = t * a % p;
      if (t == 1) {
        return false;
      }
    }
    return true;
  };
  const u64 proot = find_primroot(p);
  std::vector<u64> A(p), B(p);
  for (const usize i : rep(1, p)) {
    std::cin >> A[i];
  }
  for (const usize i : rep(1, p)) {
    std::cin >> B[i];
  }
  const usize s = [&]() {
    usize s = 1;
    while (s < p - 1) {
      s *= 2;
    }
    return s * 2;
  }();
  std::vector<u64> a(s), b(s);
  {
    u64 t = 1;
    for (const usize i : rep(0, p - 1)) {
      a[i] = A[t];
      b[i] = B[t];
      t = t * proot % p;
    }
  }
  ntt(a);
  ntt(b);
  std::vector<u64> c(s);
  for (const usize i : rep(0, s)) {
    c[i] = a[i] * b[i] % Modulus;
  }
  inv_ntt(c);
  for (const usize i : rep(p - 1, s)) {
    c[i % (p - 1)] = (c[i % (p - 1)] + c[i]) % Modulus;
  }
  std::vector<u64> C(p);
  {
    u64 t = 1;
    for (const usize i : rep(0, p - 1)) {
      C[t] = c[i];
      t = t * proot % p;
    }
  }
  for (const usize i : rep(1, p)) {
    std::cout << C[i] << (i != p - 1 ? ' ' : '\n');
  }
}

} // namespace n91

int main() {
  n91::main_();
  return 0;
}
0