結果

問題 No.931 Multiplicative Convolution
ユーザー jelljell
提出日時 2020-07-08 01:40:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 132 ms / 2,000 ms
コード長 6,775 bytes
コンパイル時間 2,670 ms
コンパイル使用メモリ 207,900 KB
実行使用メモリ 9,208 KB
最終ジャッジ日時 2023-07-25 05:46:52
合計ジャッジ時間 6,599 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,384 KB
testcase_07 AC 15 ms
4,384 KB
testcase_08 AC 132 ms
9,184 KB
testcase_09 AC 81 ms
9,000 KB
testcase_10 AC 125 ms
9,208 KB
testcase_11 AC 81 ms
9,040 KB
testcase_12 AC 80 ms
6,256 KB
testcase_13 AC 128 ms
8,948 KB
testcase_14 AC 132 ms
9,116 KB
testcase_15 AC 131 ms
8,976 KB
testcase_16 AC 128 ms
8,992 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:141:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-Wreturn-type]
  141 | main()
      | ^~~~

ソースコード

diff #

#ifndef number_theoretic_transform_hpp
#define number_theoretic_transform_hpp

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>

namespace number_theoretic_transform
{
    constexpr int mod = 998244353;
    constexpr int primitive = 3;

    class modint
    {
        int val;
    public:
        constexpr modint() noexcept : val{0} {}
        constexpr modint(long long x) noexcept : val((x %= mod) < 0 ? mod + x : x) {}
        constexpr long long value() const noexcept { return val; }
        constexpr modint operator++(int) noexcept { modint t = *this; return ++val, t; }
        constexpr modint operator--(int) noexcept { modint t = *this; return --val, t; }
        constexpr modint &operator++() noexcept { return ++val, *this; }
        constexpr modint &operator--() noexcept { return --val, *this; }
        constexpr modint operator-() const noexcept { return modint(-val); }
        constexpr modint &operator+=(const modint &other) noexcept { return (val += other.val) < mod ? 0 : val -= mod, *this; }
        constexpr modint &operator-=(const modint &other) noexcept { return (val += mod - other.val) < mod ? 0 : val -= mod, *this; }
        constexpr modint &operator*=(const modint &other) noexcept { return val = (long long)val * other.val % mod, *this; }
        constexpr modint &operator/=(const modint &other) noexcept { return *this *= inverse(other); }
        constexpr modint operator+(const modint &other) const noexcept { return modint(*this) += other; }
        constexpr modint operator-(const modint &other) const noexcept { return modint(*this) -= other; }
        constexpr modint operator*(const modint &other) const noexcept { return modint(*this) *= other; }
        constexpr modint operator/(const modint &other) const noexcept { return modint(*this) /= other; }
        constexpr bool operator==(const modint &other) const noexcept { return val == other.val; }
        constexpr bool operator!=(const modint &other) const noexcept { return val != other.val; }
        constexpr bool operator!() const noexcept { return !val; }
        friend constexpr modint operator+(long long x, modint y) noexcept { return modint(x) + y; }
        friend constexpr modint operator-(long long x, modint y) noexcept { return modint(x) - y; }
        friend constexpr modint operator*(long long x, modint y) noexcept { return modint(x) * y; }
        friend constexpr modint operator/(long long x, modint y) noexcept { return modint(x) / y; }
        static constexpr modint inverse(const modint &other) noexcept
        {
            assert(other != 0);
            int a{mod}, b{other.val}, u{}, v{1}, t{};
            while(b) t = a / b, a ^= b ^= (a -= t * b) ^= b, u ^= v ^= (u -= t * v) ^= v;
            return {u};
        }
        static constexpr modint pow(modint other, long long e) noexcept
        {
            if(e < 0) e = e % (mod - 1) + mod - 1;
            modint res{1};
            while(e) { if(e & 1) res *= other; other *= other, e >>= 1; }
            return res;
        }
        friend std::ostream &operator<<(std::ostream &os, const modint &other) noexcept { return os << other.val; }
        friend std::istream &operator>>(std::istream &is, modint &other) noexcept { long long val; other = {(is >> val, val)}; return is; }
    }; // class modint

    class zeta_calc
    {
        static constexpr size_t n = __builtin_ctz(mod - 1);
        modint _zeta[n + 1];
    public:
        constexpr zeta_calc() : _zeta{}
        {
            _zeta[n] = modint::pow(modint(primitive), (mod - 1) / (1 << n));
            for(size_t i{n}; i; --i) _zeta[i - 1] = _zeta[i] * _zeta[i];
        }
        constexpr modint operator[](size_t k) const { return _zeta[k]; }
    }; // class zeta_calc
    constexpr zeta_calc zeta;

    class inv_calc
    {
        static constexpr size_t n = __builtin_ctz(mod - 1);
        modint _inv[n + 1];
    public:
        constexpr inv_calc() : _inv{1, (mod + 1) / 2} { for(size_t i{1}; i < n; ++i) _inv[i + 1] = _inv[i] * _inv[1]; }
        constexpr modint operator[](size_t k) const { return _inv[k]; }
    }; // class inv_calc
    constexpr inv_calc inv;

    using poly_t = std::vector<modint>;

    void discrete_Fourier_transform(poly_t &f)
    {
        const size_t n{f.size()}, mask{n - 1};
        assert(__builtin_popcount(n) == 1); // degree of f must be a power of two.
        static poly_t g; g.resize(n);
        for(size_t i{n >> 1}, ii{1}; i; i >>= 1, ++ii, swap(f, g))
        {
            modint powzeta{1};
            for(size_t j{}; j < n; powzeta *= zeta[ii])
            {
                for(size_t k{}, x{mask & j << 1}, y{mask & (i + (j << 1))}; k < i; ++k, ++j, ++x, ++y)
                {
                    g[j] = f[x] + powzeta * f[y];
                }
            }
        }
    }

    void inverse_discrete_Fourier_transform(poly_t &f)
    {
        discrete_Fourier_transform(f), reverse(next(f.begin()), f.end());
        const size_t k = __builtin_ctz(f.size()); for(modint &e : f) e *= inv[k];
    }

    poly_t convolute(poly_t f, poly_t g)
    {
        if(f.empty() || g.empty()) return poly_t();
        const size_t deg_f{f.size() - 1}, deg_g{g.size() - 1}, deg_h{deg_f + deg_g}, n(1u << (32 - __builtin_clz(deg_h)));
        static poly_t h;
        f.resize(n, 0), g.resize(n, 0), h.resize(n);
        discrete_Fourier_transform(f), discrete_Fourier_transform(g);
        for(size_t i{}; i < n; ++i) h[i] = f[i] * g[i];
        inverse_discrete_Fourier_transform(h); h.resize(deg_h + 1);
        return h;
    }
} // namespace Number_theoretic_transform

#endif // number_theoretic_transform_hpp


#include <bits/stdc++.h>
using namespace std;
using i64=int64_t;

i64 modpow(i64 x, i64 y, i64 p)
{
    i64 res=1;
    while(y)
    {
        if(y&1) res=res*x%p;
        x=x*x%p;
        y>>=1;
    }
    return res;
}

main()
{
    int P; cin>>P;
    i64 pr=1;
    for(;;)
    {
        int ord=P-1;
        for(int d=1; d*d<P; d++)
        {
            if((P-1)%d) continue;
            if(modpow(pr,d,P)==1) ord=min(ord,d);
            if(modpow(pr,(P-1)/d,P)==1) ord=min(ord,(P-1)/d);
        }
        if(ord==P-1) break;
        pr++;
    }
    // cerr<<pr<<endl;

    vector<int> e(P);
    for(i64 i=0,x=1; i<P-1; i++)
    {
        e[x]=i;
        x=x*pr%P;
    }

    using namespace number_theoretic_transform;
    poly_t a(P-1),b(P-1);
    for(int i=1;i<P;i++)
    {
        cin>>a[e[i]];
    }
    for(int i=1;i<P;i++)
    {
        cin>>b[e[i]];
    }
    poly_t c=convolute(a,b);
    for(int i=P-1;i<(int)c.size();i++) c[i-P+1]+=c[i];
    poly_t res(P);
    for(i64 i=0,j=1;i<P-1;i++)
    {
        res[j]=c[i];
        j=j*pr%P;
    }
    for(int i=1;i<P;i++) cout<<res[i]<<" ";
    cout<<endl;
}
0