結果

問題 No.931 Multiplicative Convolution
ユーザー PachicobuePachicobue
提出日時 2019-11-22 22:30:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 13,194 bytes
コンパイル時間 2,878 ms
コンパイル使用メモリ 220,800 KB
実行使用メモリ 12,544 KB
最終ジャッジ日時 2024-10-11 04:14:27
合計ジャッジ時間 4,892 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 3 ms
5,248 KB
testcase_07 AC 17 ms
5,248 KB
testcase_08 AC 140 ms
12,544 KB
testcase_09 AC 88 ms
12,332 KB
testcase_10 AC 135 ms
12,420 KB
testcase_11 AC 87 ms
12,232 KB
testcase_12 AC 107 ms
11,752 KB
testcase_13 AC 135 ms
12,336 KB
testcase_14 AC 140 ms
12,412 KB
testcase_15 AC 139 ms
12,356 KB
testcase_16 AC 136 ms
12,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
// created [2019/11/22] 22:11:34
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wsign-conversion"

using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;
using uint = unsigned int;
using usize = std::size_t;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template<typename T> constexpr T popcount(const T u) { return u ? static_cast<T>(__builtin_popcountll(static_cast<u64>(u))) : static_cast<T>(0); }
template<typename T> constexpr T log2p1(const T u) { return u ? static_cast<T>(64 - __builtin_clzll(static_cast<u64>(u))) : static_cast<T>(0); }
template<typename T> constexpr T msbp1(const T u) { return log2p1(u); }
template<typename T> constexpr T lsbp1(const T u) { return __builtin_ffsll(u); }
template<typename T> constexpr T clog(const T u) { return u ? log2p1(u - 1) : static_cast<T>(u); }
template<typename T> constexpr bool ispow2(const T u) { return u and (static_cast<u64>(u) & static_cast<u64>(u - 1)) == 0; }
template<typename T> constexpr T ceil2(const T u) { return static_cast<T>(1) << clog(u); }
template<typename T> constexpr T floor2(const T u) { return u == 0 ? static_cast<T>(0) : static_cast<T>(1) << (log2p1(u) - 1); }
template<typename T> constexpr bool btest(const T mask, const usize ind) { return static_cast<bool>((static_cast<u64>(mask) >> ind) & static_cast<u64>(1)); }
template<typename T> void bset(T& mask, const usize ind) { mask |= (static_cast<T>(1) << ind); }
template<typename T> void breset(T& mask, const usize ind) { mask &= ~(static_cast<T>(1) << ind); }
template<typename T> void bflip(T& mask, const usize ind) { mask ^= (static_cast<T>(1) << ind); }
template<typename T> void bset(T& mask, const usize ind, const bool b) { (b ? bset(mask, ind) : breset(mask, ind)); }
template<typename T> constexpr T bcut(const T mask, const usize ind) { return ind == 0 ? static_cast<T>(0) : static_cast<T>((static_cast<u64>(mask) << (64 - ind)) >> (64 - ind)); }
template<typename T> bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); }
template<typename T> bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); }
constexpr unsigned int mod                  = 1000000007;
template<typename T> constexpr T inf_v      = std::numeric_limits<T>::max() / 4;
template<typename Real> constexpr Real pi_v = Real{3.141592653589793238462643383279502884};

template<typename T>
T read()
{
    T v;
    return std::cin >> v, v;
}
template<typename T, typename... Args>
auto read(const usize size, Args... args)
{
    std::vector<decltype(read<T>(args...))> ans(size);
    for (usize i = 0; i < size; i++) { ans[i] = read<T>(args...); }
    return ans;
}
template<typename... Types>
auto reads() { return std::tuple<std::decay_t<Types>...>{read<Types>()...}; }
#    define SHOW(...) static_cast<void>(0)

template<typename T>
std::vector<T> make_v(const usize size, const T v) { return std::vector<T>(size, v); }
template<typename... Args>
auto make_v(const usize size, Args... args) { return std::vector<decltype(make_v(args...))>(size, make_v(args...)); }


template<typename T> T gcd(const T& a, const T& b) { return a < 0 ? gcd(-a, b) : b < 0 ? gcd(a, -b) : (a > b ? gcd(b, a) : a == 0 ? b : gcd(b % a, a)); }
template<typename T> T lcm(const T& a, const T& b) { return a / gcd(a, b) * b; }
template<typename T>
constexpr std::pair<T, T> extgcd(const T a, const T b)
{
    if (b == 0) { return std::pair<T, T>{1, 0}; }
    const auto g = gcd(a, b), da = std::abs(b) / g;
    const auto p = extgcd(b, a % b);
    const auto x = (da + p.second % da) % da, y = (g - a * x) / b;
    return {x, y};
}
template<typename T>
constexpr T inverse(const T a, const T mod) { return extgcd(a, mod).first; }
template<uint mod_value, bool dynamic = false>
class modint_base
{
public:
    template<typename UInt = uint>
    static std::enable_if_t<dynamic, const UInt> mod() { return mod_ref(); }
    template<typename UInt = uint>
    static constexpr std::enable_if_t<not dynamic, const UInt> mod() { return mod_value; }
    template<typename UInt = uint>
    static void set_mod(const std::enable_if_t<dynamic, const UInt> mod) { mod_ref() = mod, inv_ref() = {1, 1}; }
    modint_base() : v{0} {}
    modint_base(const ll val) : v{norm(static_cast<uint>(val % static_cast<ll>(mod()) + static_cast<ll>(mod())))} {}
    modint_base(const modint_base& n) : v{n()} {}
    explicit operator bool() const { return v != 0; }
    bool operator!() const { return not static_cast<bool>(*this); }
    modint_base& operator=(const modint_base& m) { return v = m(), (*this); }
    modint_base& operator=(const ll val) { return v = norm(uint(val % static_cast<ll>(mod()) + static_cast<ll>(mod()))), (*this); }
    friend modint_base operator+(const modint_base& m) { return m; }
    friend modint_base operator-(const modint_base& m) { return make(norm(mod() - m.v)); }
    friend modint_base operator+(const modint_base& m1, const modint_base& m2) { return make(norm(m1.v + m2.v)); }
    friend modint_base operator-(const modint_base& m1, const modint_base& m2) { return make(norm(m1.v + mod() - m2.v)); }
    friend modint_base operator*(const modint_base& m1, const modint_base& m2) { return make(static_cast<uint>(static_cast<ll>(m1.v) * static_cast<ll>(m2.v) % static_cast<ll>(mod()))); }
    friend modint_base operator/(const modint_base& m1, const modint_base& m2) { return m1 * inv(m2.v); }
    friend modint_base operator+(const modint_base& m, const ll val) { return modint_base{static_cast<ll>(m.v) + val}; }
    friend modint_base operator-(const modint_base& m, const ll val) { return modint_base{static_cast<ll>(m.v) - val}; }
    friend modint_base operator*(const modint_base& m, const ll val) { return modint_base{static_cast<ll>(m.v) * (val % static_cast<ll>(mod()))}; }
    friend modint_base operator/(const modint_base& m, const ll val) { return modint_base{static_cast<ll>(m.v) * inv(val)}; }
    friend modint_base operator+(const ll val, const modint_base& m) { return modint_base{static_cast<ll>(m.v) + val}; }
    friend modint_base operator-(const ll val, const modint_base& m) { return modint_base{-static_cast<ll>(m.v) + val}; }
    friend modint_base operator*(const ll val, const modint_base& m) { return modint_base{static_cast<ll>(m.v) * (val % static_cast<ll>(mod()))}; }
    friend modint_base operator/(const ll val, const modint_base& m) { return modint_base{val * inv(static_cast<ll>(m.v))}; }
    friend modint_base& operator+=(modint_base& m1, const modint_base& m2) { return m1 = m1 + m2; }
    friend modint_base& operator-=(modint_base& m1, const modint_base& m2) { return m1 = m1 - m2; }
    friend modint_base& operator*=(modint_base& m1, const modint_base& m2) { return m1 = m1 * m2; }
    friend modint_base& operator/=(modint_base& m1, const modint_base& m2) { return m1 = m1 / m2; }
    friend modint_base& operator+=(modint_base& m, const ll val) { return m = m + val; }
    friend modint_base& operator-=(modint_base& m, const ll val) { return m = m - val; }
    friend modint_base& operator*=(modint_base& m, const ll val) { return m = m * val; }
    friend modint_base& operator/=(modint_base& m, const ll val) { return m = m / val; }
    friend modint_base operator^(const modint_base& m, const ll n) { return power(m.v, n); }
    friend modint_base& operator^=(modint_base& m, const ll n) { return m = m ^ n; }
    friend bool operator==(const modint_base& m1, const modint_base& m2) { return m1.v == m2.v; }
    friend bool operator!=(const modint_base& m1, const modint_base& m2) { return not(m1 == m2); }
    friend bool operator==(const modint_base& m, const ll val) { return m.v == norm(static_cast<uint>(static_cast<ll>(mod()) + val % static_cast<ll>(mod()))); }
    friend bool operator!=(const modint_base& m, const ll val) { return not(m == val); }
    friend bool operator==(const ll val, const modint_base& m) { return m.v == norm(static_cast<uint>(static_cast<ll>(mod()) + val % static_cast<ll>(mod()))); }
    friend bool operator!=(const ll val, const modint_base& m) { return not(m == val); }
    friend std::istream& operator>>(std::istream& is, modint_base& m)
    {
        ll v;
        return is >> v, m = v, is;
    }
    friend std::ostream& operator<<(std::ostream& os, const modint_base& m) { return os << m(); }
    uint operator()() const { return v; }
    static modint_base small_inv(const usize n)
    {
        auto& in = inv_ref();
        if (n < in.size()) { return in[n]; }
        for (usize i = in.size(); i <= n; i++) { in.push_back(-in[modint_base::mod() % i] * (modint_base::mod() / i)); }
        return in.back();
    }

private:
    template<typename UInt = uint>
    static std::enable_if_t<dynamic, UInt&> mod_ref()
    {
        static UInt mod = 0;
        return mod;
    }
    static uint norm(const uint x) { return x < mod() ? x : x - mod(); }
    static modint_base make(const uint x)
    {
        modint_base m;
        return m.v = x, m;
    }
    static modint_base power(modint_base x, ull n)
    {
        modint_base ans = 1;
        for (; n; n >>= 1, x *= x) {
            if (n & 1) { ans *= x; }
        }
        return ans;
    }
    static modint_base inv(const ll v) { return v < 1000000 ? small_inv(static_cast<usize>(v)) : modint_base{inverse(v, static_cast<ll>(mod()))}; }
    static std::vector<modint_base>& inv_ref()
    {
        static std::vector<modint_base> in{1, 1};
        return in;
    }
    uint v;
};
template<uint mod>
using modint = modint_base<mod, false>;
template<uint id>
using dynamic_modint = modint_base<id, true>;

template<typename T>
std::vector<std::pair<T, usize>> prime_factors(T n)
{
    std::vector<std::pair<T, usize>> ans;
    for (T i = 2; i * i <= n; i++) {
        usize cnt = 0;
        for (; n % i == 0; n /= i, cnt++) {}
        if (cnt > 0) { ans.push_back({i, cnt}); }
    }
    if (n > 1) { ans.push_back({n, 1}); }
    return ans;
}

template<uint mod = 924844033, uint root = 5>
class ntt
{
private:
    using value_type             = modint<mod>;
    static constexpr usize depth = 30;
    static void transform(std::vector<value_type>& a, const usize lg, const bool rev)
    {
        const usize N = a.size();
        assert(1UL << lg == N);
        static std::vector<value_type> R[depth];
        if (R[lg].empty()) {
            R[lg].reserve(N), R[lg].resize(N, value_type(1));
            const value_type r = value_type(root) ^ ((mod - 1) / N);
            for (usize i = 1; i < N; i++) { R[lg][i] = R[lg][i - 1] * r; }
        }
        std::vector<value_type> tmp(N);
        for (usize w = (N >> 1); w > 0; w >>= 1) {
            for (usize y = 0; y < (N >> 1); y += w) {
                const value_type r = rev ? R[lg][y == 0 ? 0 : N - y] : R[lg][y];
                for (usize x = 0; x < w; x++) {
                    const auto u = a[y << 1 | x], v = a[y << 1 | x | w]() * r;
                    tmp[y | x] = u + v, tmp[y | x | (N >> 1)] = u - v;
                }
            }
            std::swap(tmp, a);
        }
        if (rev) {
            for (usize i = 0; i < N; i++) { a[i] /= value_type(N); }
        }
    }

public:
    ntt() = delete;
    static std::vector<value_type> convolute(const std::vector<value_type>& a, const std::vector<value_type>& b)
    {
        const usize need = a.size() + b.size() - 1, lg = clog(need), sz = 1UL << lg;
        std::vector<value_type> A(sz, 0), B(sz, 0);
        for (usize i = 0; i < a.size(); i++) { A[i] = a[i](); }
        for (usize i = 0; i < b.size(); i++) { B[i] = b[i](); }
        transform(A, lg, false), transform(B, lg, false);
        for (usize i = 0; i < sz; i++) { A[i] *= B[i]; }
        transform(A, lg, true);
        std::vector<value_type> ans(need);
        for (usize i = 0; i < need; i++) { ans[i] = int(A[i]()); }
        return ans;
    }
};
int main()
{
    using mint   = dynamic_modint<0>;
    const auto P = read<uint>();
    mint::set_mod(P);
    std::vector<uint> A(P, 0), B(P, 0);
    for (int i = 1; i < P; i++) { std::cin >> A[i]; }
    for (int i = 1; i < P; i++) { std::cin >> B[i]; }
    uint g        = 2;
    const auto fs = prime_factors(P - 1);
    for (; g < P; g++) {
        bool ok = true;
        for (const auto& p : fs) {
            const uint q   = p.first;
            const uint per = (P - 1) / q;
            if ((mint(g) ^ per) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) { break; }
    }
    SHOW(g);
    std::vector<mint> ps(P - 1, 1);
    for (int i = 1; i < P - 1; i++) { ps[i] = ps[i - 1] * g; }
    constexpr uint mod = 998244353;
    std::vector<modint<mod>> a(P), b(P);
    for (int i = 0; i < P - 1; i++) { a[i] = A[ps[i]()]; }
    for (int i = 0; i < P - 1; i++) { b[i] = B[ps[i]()]; }
    SHOW(a, b);
    auto c = ntt<mod>::convolute(a, b);
    SHOW(c);
    std::vector<modint<mod>> d(P - 1, 0);
    for (int i = 0; i < c.size(); i++) { d[i % (P - 1)] += c[i]; }
    SHOW(d);
    std::vector<modint<mod>> C(P, 0);
    for (int i = 0; i < P - 1; i++) { C[ps[i]()] = d[i]; }
    for (int i = 1; i < P; i++) { std::cout << C[i] << " "; }
    std::cout << std::endl;
    return 0;
}
0