結果
問題 | No.1307 Rotate and Accumulate |
ユーザー | Pachicobue |
提出日時 | 2020-12-04 00:14:06 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 151 ms / 5,000 ms |
コード長 | 10,512 bytes |
コンパイル時間 | 2,602 ms |
コンパイル使用メモリ | 216,856 KB |
実行使用メモリ | 21,148 KB |
最終ジャッジ日時 | 2024-09-14 09:40:41 |
合計ジャッジ時間 | 5,076 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 2 ms
6,944 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,944 KB |
testcase_08 | AC | 73 ms
11,636 KB |
testcase_09 | AC | 74 ms
11,752 KB |
testcase_10 | AC | 78 ms
11,616 KB |
testcase_11 | AC | 68 ms
11,332 KB |
testcase_12 | AC | 76 ms
11,744 KB |
testcase_13 | AC | 16 ms
6,940 KB |
testcase_14 | AC | 39 ms
7,440 KB |
testcase_15 | AC | 150 ms
20,636 KB |
testcase_16 | AC | 151 ms
21,148 KB |
testcase_17 | AC | 149 ms
19,996 KB |
testcase_18 | AC | 144 ms
20,764 KB |
testcase_19 | AC | 149 ms
20,636 KB |
testcase_20 | AC | 150 ms
20,252 KB |
testcase_21 | AC | 2 ms
6,940 KB |
ソースコード
#include <bits/stdc++.h> using ll = long long; using uint = unsigned int; using ull = unsigned long long; using ld = long double; template<typename T> using max_heap = std::priority_queue<T>; template<typename T> using min_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>; template<const uint& mod> class modint { public: modint() : m_val{0} {} modint(const ll v) : m_val{normll(v)} {} modint(const modint& m) = default; modint& operator =(const modint& m) { return m_val = m(), (*this); } modint& operator =(const ll v) { return m_val = normll(v), (*this); } modint operator+() const { return *this; } modint operator-() const { return modint{0} - (*this); } modint& operator+=(const modint& m) { return m_val = norm(m_val + m()), *this; } modint& operator-=(const modint& m) { return m_val = norm(m_val + mod - m()), *this; } modint& operator*=(const modint& m) { return m_val = normll((ll)m_val * (ll)m() % (ll)mod), *this; } modint& operator/=(const modint& m) { return *this *= m.inv(); } modint& operator+=(const ll val) { return *this += modint{val}; } modint& operator-=(const ll val) { return *this -= modint{val}; } modint& operator*=(const ll val) { return *this *= modint{val}; } modint& operator/=(const ll val) { return *this /= modint{val}; } modint operator+(const modint& m) const { return modint{*this} += m; } modint operator-(const modint& m) const { return modint{*this} -= m; } modint operator*(const modint& m) const { return modint{*this} *= m; } modint operator/(const modint& m) const { return modint{*this} /= m; } modint operator+(const ll v) const { return *this + modint{v}; } modint operator-(const ll v) const { return *this - modint{v}; } modint operator*(const ll v) const { return *this * modint{v}; } modint operator/(const ll v) const { return *this / modint{v}; } bool operator==(const modint& m) const { return m_val == m(); } bool operator!=(const modint& m) const { return not(*this == m); } friend modint operator+(const ll v, const modint& m) { return modint{v} + m; } friend modint operator-(const ll v, const modint& m) { return modint{v} - m; } friend modint operator*(const ll v, const modint& m) { return modint{v} * m; } friend modint operator/(const ll v, const modint& m) { return modint{v} / m; } friend std::istream& operator>>(std::istream& is, modint& m) { ll v; return is >> v, m = v, is; } friend std::ostream& operator<<(std::ostream& os, const modint& m) { return os << m(); } uint operator()() const { return m_val; } modint pow(ull n) const { modint ans = 1; for (modint x = *this; n > 0; n >>= 1, x *= x) { if (n & 1ULL) { ans *= x; } } return ans; } modint inv() const { return m_val <= 2000000 ? sinv(m_val) : pow(mod - 2); } static modint fact(const uint n) { static std::vector<modint> fs{1, 1}; for (uint i = (uint)fs.size(); i <= n; i++) { fs.push_back(fs.back() * i); } return fs[n]; } static modint ifact(const uint n) { static std::vector<modint> ifs{1, 1}; for (uint i = (uint)ifs.size(); i <= n; i++) { ifs.push_back(ifs.back() * sinv(i)); } return ifs[n]; } static modint perm(const int n, const int k) { return k > n or k < 0 ? modint{0} : fact(n) * ifact(n - k); } static modint comb(const int n, const int k) { return k > n or k < 0 ? modint{0} : fact(n) * ifact(n - k) * ifact(k); } private: static constexpr uint norm(const uint x) { return x < mod ? x : x - mod; } static constexpr uint normll(const ll x) { return norm(uint(x % (ll)mod + (ll)mod)); } static modint sinv(const uint n) { static std::vector<modint> is{1, 1}; for (uint i = (uint)is.size(); i <= n; i++) { is.push_back(-is[mod % i] * (mod / i)); } return is[n]; } uint m_val; }; constexpr int popcount(const ull v) { return v ? __builtin_popcountll(v) : 0; } constexpr int log2p1(const ull v) { return v ? 64 - __builtin_clzll(v) : 0; } constexpr int lsbp1(const ull v) { return __builtin_ffsll(v); } constexpr int clog(const ull v) { return v ? log2p1(v - 1) : 0; } constexpr ull ceil2(const ull v) { return 1ULL << clog(v); } constexpr ull floor2(const ull v) { return v ? (1ULL << (log2p1(v) - 1)) : 0ULL; } constexpr bool btest(const ull mask, const int ind) { return (mask >> ind) & 1ULL; } template<typename T> void bset(T& mask, const int ind) { mask |= ((T)1 << ind); } template<typename T> void breset(T& mask, const int ind) { mask &= ~((T)1 << ind); } template<typename T> void bflip(T& mask, const int ind) { mask ^= ((T)1 << ind); } template<typename T> void bset(T& mask, const int ind, const bool b) { (b ? bset(mask, ind) : breset(mask, ind)); } template<const auto& mod, uint root> class ntt { private: using mint = modint<mod>; static constexpr int D = 30; static void trans(std::vector<mint>& as, const int lg, const bool rev) { const int N = as.size(); static std::vector<mint> rs[D]; if (rs[lg].empty()) { rs[lg].resize(N, 1); const mint r = mint(root).pow((mod - 1) >> lg); for (int i = 1; i < N; i++) { rs[lg][i] = rs[lg][i - 1] * r; } } std::vector<mint> tmp(N); for (int w = (N >> 1); w > 0; w >>= 1) { for (int y = 0; y < (N >> 1); y += w) { const mint r = rev ? rs[lg][y == 0 ? 0 : N - y] : rs[lg][y]; for (int x = 0; x < w; x++) { const auto u = as[y << 1 | x], v = as[y << 1 | x | w]() * r; tmp[y | x] = u + v, tmp[y | x | (N >> 1)] = u - v; } } std::swap(tmp, as); } if (rev) { for (int i = 0; i < N; i++) { as[i] /= mint(N); } } } public: ntt() = delete; static std::vector<mint> convolute(std::vector<mint> as, std::vector<mint> bs) { const int need = as.size() + bs.size() - 1, lg = clog(need), size = 1UL << lg; as.resize(size), bs.resize(size); trans(as, lg, false), trans(bs, lg, false); for (int i = 0; i < size; i++) { as[i] *= bs[i]; } trans(as, lg, true); return std::vector<mint>(as.begin(), as.begin() + need); } }; 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); } 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> constexpr T TEN(const int n) { return n == 0 ? T{1} : TEN<T>(n - 1) * T{10}; } template<typename F> struct fix : F { fix(F&& f) : F{std::forward<F>(f)} {} template<typename... Args> auto operator()(Args&&... args) const { return F::operator()(*this, std::forward<Args>(args)...); } }; template<typename T, int n, int i = 0> auto nd_array(int const (&szs)[n], const T x = T{}) { if constexpr (i == n) { return x; } else { return std::vector(szs[i], nd_array<T, n, i + 1>(szs, x)); } } class printer { public: printer(std::ostream& os_ = std::cout) : m_os{os_} { m_os << std::fixed << std::setprecision(15); } template<typename... Args> int ln(const Args&... args) { return dump(args...), m_os << '\n', 0; } template<typename... Args> int el(const Args&... args) { return dump(args...), m_os << std::endl, 0; } private: template<typename T> void dump(const T& v) { m_os << v; } template<typename T> void dump(const std::vector<T>& vs) { for (int i = 0; i < (int)vs.size(); i++) { m_os << (i ? " " : ""), dump(vs[i]); } } template<typename T> void dump(const std::vector<std::vector<T>>& vss) { for (int i = 0; i < (int)vss.size(); i++) { m_os << (0 <= i or i + 1 < (int)vss.size() ? "\n" : ""), dump(vss[i]); } } template<typename T, typename... Args> int dump(const T& v, const Args&... args) { return dump(v), m_os << ' ', dump(args...), 0; } std::ostream& m_os; }; printer out; class scanner { public: scanner(std::istream& is_ = std::cin) : m_is{is_} { m_is.tie(nullptr), std::ios::sync_with_stdio(false); } void fastio(const bool on) { if (on) { m_is.tie(nullptr), std::ios::sync_with_stdio(false); } else { m_is.tie(&std::cout), std::ios::sync_with_stdio(true); } } template<typename T> T val() { T v; return m_is >> v, v; } template<typename T> T val(const T offset) { return val<T>() - offset; } template<typename T> std::vector<T> vec(const int n) { return make_v<T>(n, [this]() { return val<T>(); }); } template<typename T> std::vector<T> vec(const int n, const T offset) { return make_v<T>(n, [this, offset]() { return val<T>(offset); }); } template<typename T> std::vector<std::vector<T>> vvec(const int n0, const int n1) { return make_v<std::vector<T>>(n0, [this, n1]() { return vec<T>(n1); }); } template<typename T> std::vector<std::vector<T>> vvec(const int n0, const int n1, const T offset) { return make_v<std::vector<T>>(n0, [this, n1, offset]() { return vec<T>(n1, offset); }); } template<typename... Args> auto tup() { return std::tuple<std::decay_t<Args>...>{val<Args>()...}; } template<typename... Args> auto tup(const Args&... offsets) { return std::tuple<std::decay_t<Args>...>{val<Args>(offsets)...}; } private: template<typename T, typename F> std::vector<T> make_v(const int n, F f) { std::vector<T> ans; for (int i = 0; i < n; i++) { ans.push_back(f()); } return ans; } std::istream& m_is; }; scanner in; int main() { static constexpr uint mod = 998244353; static constexpr uint root = 3; using mint = modint<mod>; const auto [N, Q] = in.tup<int, int>(); const auto as = in.vec<int>(N); const auto rs = in.vec<int>(Q); std::vector<mint> vs(2 * N); for (int i = 0; i < 2 * N; i++) { vs[i] = as[i % N]; } std::vector<mint> cs(N + 1); for (int i = 0; i < Q; i++) { cs[N - rs[i]] += 1; } const auto ans = ntt<mod, root>::convolute(vs, cs); std::vector<mint> bs(N); for (int i = N; i < 2 * N; i++) { bs[i - N] = ans[i]; } out.ln(bs); return 0; }