結果

問題 No.2272 多項式乗算 mod 258280327
ユーザー cutmdocutmdo
提出日時 2023-04-14 23:50:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 571 ms / 2,000 ms
コード長 7,553 bytes
コンパイル時間 4,089 ms
コンパイル使用メモリ 281,196 KB
実行使用メモリ 28,816 KB
最終ジャッジ日時 2024-04-27 04:43:18
合計ジャッジ時間 8,137 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 6 ms
6,940 KB
testcase_25 AC 23 ms
6,940 KB
testcase_26 AC 23 ms
6,944 KB
testcase_27 AC 49 ms
6,940 KB
testcase_28 AC 50 ms
6,940 KB
testcase_29 AC 258 ms
16,132 KB
testcase_30 AC 571 ms
28,816 KB
testcase_31 AC 562 ms
28,816 KB
testcase_32 AC 558 ms
28,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,avx512f")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <stack>
#include <queue>
#include <bitset>
#include <numeric>
#include <cassert>
#include <memory>
#include <random>
#include <functional>
#include <complex>
#include <immintrin.h>
#include <stdexcept>
#ifdef DEBUG
#include "./CompetitiveProgrammingCpp/Utils/debug.hpp"
#include "./CompetitiveProgrammingCpp/Utils/Timer.hpp"
#include "./CompetitiveProgrammingCpp/Utils/sample.hpp"
#else
#define dump(...)
template<class T>constexpr inline auto d_val(T a, T b) { return a; }
#endif

/* macro */
// #define FOR(i, b, e) for(ll i = (ll)(b); i < (ll)(e); ++i)
#define RFOR(i, b, e) for(ll i = (ll)((e)-1); i >= (ll)(b); --i)
#define REP(i, n) FOR(i, 0, (n))
#define RREP(i, n) RFOR(i, 0, (n))
#define REPC(x,c) for(const auto& x:(c))
#define REPI2(it,b,e) for(auto it = (b); it != (e); ++it)
#define REPI(it,c) REPI2(it, (c).begin(), (c).end())
#define RREPI(it,c) REPI2(it, (c).rbegin(), (c).rend())
#define REPI_ERACE2(it, b, e) for(auto it = (b); it != (e);)
#define REPI_ERACE(it, c) REPI_ERACE2(it, (c).begin(), (c).end())
#define ALL(x) (x).begin(),(x).end()
#define cauto const auto&
/* macro func */
template<class T>
inline auto sort(T& t) { std::sort(ALL(t)); }
template<class T>
inline auto rsort(T& t) { std::sort((t).rbegin(), (t).rend()); }
template<class T>
inline auto unique(T& t) { (t).erase(unique((t).begin(), (t).end()), (t).end()); }
template<class T, class S>
inline auto chmax(T& t, const S& s) { if(s > t) { t = s; return true; } return false; }
template<class T, class S>
inline auto chmin(T& t, const S& s) { if(s < t) { t = s; return true; } return false; }
inline auto BR() { std::cout << "\n"; }

/* type define */
using ll = long long;
using VS = std::vector<std::string>;
using VL = std::vector<long long>;
using VVL = std::vector<VL>;
using VVVL = std::vector<VVL>;
using VVVVL = std::vector<VVVL>;
using VVVVVL = std::vector<VVVVL>;
using VD = std::vector<double>;
template<class T>
using V = std::vector<T>;
template<class T = ll, class U = T>
using P = std::pair<T, U>;
using PAIR = P<ll>;

/* using std */
using std::cout;
constexpr char endl = '\n';
using std::cin;
using std::pair;
using std::string;
using std::stack;
using std::queue;
using std::deque;
using std::vector;
using std::list;
using std::map;
using std::unordered_map;
using std::multimap;
using std::unordered_multimap;
using std::set;
using std::unordered_set;
using std::unordered_multiset;
using std::multiset;
using std::bitset;
using std::priority_queue;

/* Initial processing  */
struct Preprocessing { Preprocessing() { std::cin.tie(0); std::ios::sync_with_stdio(0); }; }_Preprocessing;

/* define hash */
namespace std {
template <>	class hash<std::pair<ll, ll>> { public:	size_t operator()(const std::pair<ll, ll>& x) const { return hash<ll>()(1000000000 * x.first + x.second); } };
}

/* input */
template<class T> std::istream& operator >> (std::istream& is, vector<T>& vec) { for(T& x : vec) is >> x; return is; }

/* constant value */
// constexpr ll MOD = 1000000007;
constexpr ll MOD = 998244353;

//=============================================================================================


template<class T> T extgcd(T a, T b, T& x, T& y) { for(T u = y = 1, v = x = 0; a;) { T q = b / a; std::swap(x -= q * u, u); std::swap(y -= q * v, v); std::swap(b -= q * a, a); } return b; }
template<class T> T mod_inv(T a, T m) { T x, y; extgcd(a, m, x, y); return (m + x % m) % m; }
ll mod_pow(ll a, ll n, ll mod) { ll ret = 1; ll p = a % mod; while(n) { if(n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; }

#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)(c).size())
#define ten(x) ((int)1e##x)

template<int mod, int primitive_root>
class NTT {
public:
    int get_mod() const { return mod; }
    void _ntt(vector<ll>& a, int sign) {
        const int n = sz(a);
        assert((n ^ (n & -n)) == 0); //n = 2^k

        const int g = 3; //g is primitive root of mod
        int h = (int)mod_pow(g, (mod - 1) / n, mod); // h^n = 1
        if(sign == -1) h = (int)mod_inv(h, mod); //h = h^-1 % mod

        //bit reverse
        int i = 0;
        for(int j = 1; j < n - 1; ++j) {
            for(int k = n >> 1; k > (i ^= k); k >>= 1);
            if(j < i) std::swap(a[i], a[j]);
        }

        for(int m = 1; m < n; m *= 2) {
            const int m2 = 2 * m;
            const ll base = mod_pow(h, n / m2, mod);
            ll w = 1;
            FOR(x, m) {
                for(int s = x; s < n; s += m2) {
                    ll u = a[s];
                    ll d = a[s + m] * w % mod;
                    a[s] = u + d;
                    if(a[s] >= mod) a[s] -= mod;
                    a[s + m] = u - d;
                    if(a[s + m] < 0) a[s + m] += mod;
                }
                w = w * base % mod;
            }
        }

        for(auto& x : a) if(x < 0) x += mod;
    }
    void ntt(vector<ll>& input) {
        _ntt(input, 1);
    }
    void intt(vector<ll>& input) {
        _ntt(input, -1);
        const int n_inv = mod_inv(sz(input), mod);
        for(auto& x : input) x = x * n_inv % mod;
    }

    // 畳み込み演算を行う
    vector<ll> convolution(const vector<ll>& a, const vector<ll>& b) {
        int ntt_size = 1;
        while(ntt_size < sz(a) + sz(b)) ntt_size *= 2;

        vector<ll> _a = a, _b = b;
        _a.resize(ntt_size); _b.resize(ntt_size);

        ntt(_a);
        ntt(_b);

        FOR(i, ntt_size) {
            (_a[i] *= _b[i]) %= mod;
        }

        intt(_a);
        return _a;
    }
};

typedef NTT<167772161, 3> NTT_1;
typedef NTT<469762049, 3> NTT_2;
typedef NTT<1224736769, 3> NTT_3;

vector<ll> fast_int32mod_convolution(vector<ll> a, vector<ll> b, int mod) {
    for(auto& x : a) x %= mod;
    for(auto& x : b) x %= mod;

    typedef NTT<167772161, 3> NTT_1;

    NTT_1 ntt1; NTT_2 ntt2; NTT_3 ntt3;
    assert(ntt1.get_mod() < ntt2.get_mod() && ntt2.get_mod() < ntt3.get_mod());
    auto x = ntt1.convolution(a, b);
    auto y = ntt2.convolution(a, b);
    auto z = ntt3.convolution(a, b);

    const ll m1 = ntt1.get_mod(), m2 = ntt2.get_mod(), m3 = ntt3.get_mod();
    const ll m1_inv_m2 = mod_inv<ll>(m1, m2);
    const ll m12_inv_m3 = mod_inv<ll>(m1 * m2, m3);
    const ll m12_mod = m1 * m2 % mod;
    vector<ll> ret(x.size());
    FOR(i, x.size()) {
        ll v1 = (y[i] - x[i]) * m1_inv_m2 % m2;
        if(v1 < 0) v1 += m2;
        ll v2 = (z[i] - (x[i] + m1 * v1) % m3) * m12_inv_m3 % m3;
        if(v2 < 0) v2 += m3;
        ll constants3 = (x[i] + m1 * v1 + m12_mod * v2) % mod;
        if(constants3 < 0) constants3 += mod;
        ret[i] = constants3;
    }

    return ret;
}

auto solve(ll n, const VL& f, ll m, const VL& g) {
    if((n == 0 && f[0] == 0) || (m == 0 && g[0] == 0)) { return VL{0}; }
    constexpr ll mod = 258280327;
    auto ans = fast_int32mod_convolution(f, g, mod);
    ans.resize(n + m + 1);
    return ans;
}

signed main() {
    ll n;
    cin >> n;
    VL f(n + 1);
    cin >> f;
    ll m;
    cin >> m;
    VL g(m + 1);
    cin >> g;
    auto ans = solve(n, f, m, g);
    cout << ans.size() - 1 << endl;
    REPC(x, ans) { cout << x << " "; }BR();
}
0