結果

問題 No.1761 Sequence Distance
ユーザー Ricky_ponRicky_pon
提出日時 2022-04-03 19:20:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5,888 ms / 8,000 ms
コード長 5,470 bytes
コンパイル時間 2,121 ms
コンパイル使用メモリ 210,704 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-15 17:17:17
合計ジャッジ時間 54,786 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1,388 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 11 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 11 ms
4,376 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 10 ms
4,376 KB
testcase_11 AC 16 ms
4,376 KB
testcase_12 AC 4 ms
4,380 KB
testcase_13 AC 6 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 10 ms
4,376 KB
testcase_16 AC 10 ms
4,380 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 16 ms
4,380 KB
testcase_19 AC 7 ms
4,376 KB
testcase_20 AC 7 ms
4,380 KB
testcase_21 AC 1,037 ms
4,376 KB
testcase_22 AC 61 ms
4,376 KB
testcase_23 AC 4,684 ms
4,384 KB
testcase_24 AC 1,363 ms
4,380 KB
testcase_25 AC 3,769 ms
4,376 KB
testcase_26 AC 4,077 ms
4,376 KB
testcase_27 AC 1,320 ms
4,376 KB
testcase_28 AC 1,122 ms
4,380 KB
testcase_29 AC 3,312 ms
4,376 KB
testcase_30 AC 567 ms
4,376 KB
testcase_31 AC 1,797 ms
4,376 KB
testcase_32 AC 96 ms
4,380 KB
testcase_33 AC 2,290 ms
4,380 KB
testcase_34 AC 11 ms
4,376 KB
testcase_35 AC 188 ms
4,376 KB
testcase_36 AC 2,990 ms
4,376 KB
testcase_37 AC 313 ms
4,380 KB
testcase_38 AC 617 ms
4,376 KB
testcase_39 AC 1,668 ms
4,384 KB
testcase_40 AC 514 ms
4,376 KB
testcase_41 AC 5,140 ms
4,376 KB
testcase_42 AC 5,865 ms
4,376 KB
testcase_43 AC 5,888 ms
4,380 KB
testcase_44 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <atcoder/modint>


#include <vector>

template <class T = int>
struct Edge {
    int from, to;
    T cost;
    int idx;

    Edge() = default;

    Edge(int from, int to, T cost = 1, int idx = 0)
        : from(from), to(to), cost(cost), idx(idx) {}
};

template <class T = int>
struct Graph {
    std::vector<std::vector<Edge<T>>> es;
    int edge_num;

    Graph(int n) : edge_num(0) { es.resize(n); }

    int size() { return es.size(); }

    void add_edge(int from, int to, T cost, int idx) {
        es[from].emplace_back(from, to, cost, idx);
        ++edge_num;
    }

    std::vector<Edge<T>> edges() {
        std::vector<Edge<T>> res(edge_num);
        for (int v = 0; v < (int)es.size(); ++v) {
            for (auto& e : es[v]) {
                res[e.idx] = e;
            }
        }
        return res;
    }

    std::vector<Edge<T>>& operator[](int i) { return es[i]; }
};


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

template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
T div_floor(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a >= 0 ? a / b : (a + 1) / b - 1;
}

template <class T>
T div_ceil(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a > 0 ? (a - 1) / b + 1 : a / b;
}

template <typename T>
struct CoordComp {
    std::vector<T> v;
    bool sorted;

    CoordComp() : sorted(false) {}

    int size() { return v.size(); }

    void add(T x) { v.push_back(x); }

    void build() {
        std::sort(v.begin(), v.end());

        v.erase(std::unique(v.begin(), v.end()), v.end());
        sorted = true;
    }

    int get_idx(T x) {
        assert(sorted);
        return lower_bound(v.begin(), v.end(), x) - v.begin();
    }

    T &operator[](int i) { return v[i]; }
};


struct HeavyLightDecomposition {
   public:
    std::vector<int> idx, par, head, out;

    HeavyLightDecomposition(Graph<> gr, int root = 0) {
        idx.resize(gr.size());
        par.resize(gr.size());
        head.resize(gr.size());
        out.resize(gr.size());

        sz_dfs(gr, root, -1);
        int cur = 0;
        idx[root] = cur++;
        head[root] = root;
        hld_dfs(gr, root, -1, root, cur);
    }

    int lca(int u, int v) {
        while (true) {
            if (idx[u] > idx[v]) std::swap(u, v);
            if (head[u] == head[v]) return u;
            v = par[head[v]];
        }
    }

    std::pair<std::vector<std::pair<int, int>>,
              std::vector<std::pair<int, int>>>
    get_path(int u, int v) {
        std::vector<std::pair<int, int>> up, down;
        while (true) {
            if (head[u] == head[v]) {
                if (idx[u] < idx[v])
                    down.emplace_back(idx[u], idx[v] + 1);
                else
                    up.emplace_back(idx[v], idx[u] + 1);
                std::reverse(up.begin(), up.end());
                std::reverse(down.begin(), down.end());
                return {up, down};
            } else {
                if (idx[u] < idx[v]) {
                    down.emplace_back(idx[head[v]], idx[v] + 1);
                    v = par[head[v]];
                } else {
                    up.emplace_back(idx[head[u]], idx[u] + 1);
                    u = par[head[u]];
                }
            }
        }
    }

   private:
    int sz_dfs(Graph<> &gr, int v, int pv) {
        int ret = 1, max_sz = 0;
        for (int i = 0; i < (int)gr[v].size(); ++i) {
            if (gr[v][i].to == pv) continue;
            int tmp = sz_dfs(gr, gr[v][i].to, v);
            ret += tmp;
            if (chmax(max_sz, tmp)) std::swap(gr[v][0], gr[v][i]);
        }
        return ret;
    }

    void hld_dfs(Graph<> &gr, int v, int pv, int h, int &cur) {
        for (auto &e : gr[v]) {
            if (e.to == pv) continue;
            idx[e.to] = cur++;  // バグっている気がする
            par[e.to] = v;
            head[e.to] = (gr[v][0].to == e.to ? h : e.to);
            hld_dfs(gr, e.to, v, head[e.to], cur);
        }
        out[v] = cur;
    }
};


#define For(i, a, b) for (int i = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int i = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For(i, 0, n)
#define rrep(i, n) rFor(i, n, 0)
#define fi first
#define se second

using namespace std;

using lint = long long;
using pii = pair<int, int>;
using pll = pair<lint, lint>;

using mint = atcoder::modint998244353;

int main() {
    int n, m;
    scanf("%d%d", &n, &m);

    constexpr int OFFSET = 50;
    vector<vector<mint>> dp(OFFSET * 2, vector<mint>(m + 1, 0));
    dp[OFFSET][0] = 1;
    rep(i, n) {
        vector<vector<mint>> nxt(OFFSET * 2, vector<mint>(m + 1, 0));
        rep(j, OFFSET * 2) rep(k, m + 1) if (dp[j][k] != 0) {
            if (k + abs(j - OFFSET) <= m) {
                nxt[j][k + abs(j - OFFSET)] += dp[j][k] * 2;
            }
            if (j + 1 < OFFSET * 2 && k + abs(j + 1 - OFFSET) <= m) {
                nxt[j + 1][k + abs(j + 1 - OFFSET)] += dp[j][k];
            }
            if (j - 1 >= 0 && k + abs(j - 1 - OFFSET) <= m) {
                nxt[j - 1][k + abs(j - 1 - OFFSET)] += dp[j][k];
            }
        }
        swap(dp, nxt);
    }
    mint ans = dp[OFFSET][m];
    printf("%u\n", ans.val());
}
0