結果

問題 No.1241 Eternal Tours
ユーザー hitonanodehitonanode
提出日時 2020-08-25 22:32:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 536 ms / 6,000 ms
コード長 5,624 bytes
コンパイル時間 978 ms
コンパイル使用メモリ 85,324 KB
実行使用メモリ 34,404 KB
最終ジャッジ日時 2024-05-06 23:50:36
合計ジャッジ時間 10,291 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 125 ms
11,648 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 213 ms
8,064 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 54 ms
5,376 KB
testcase_17 AC 511 ms
34,404 KB
testcase_18 AC 442 ms
14,336 KB
testcase_19 AC 423 ms
12,044 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 10 ms
5,376 KB
testcase_22 AC 461 ms
22,912 KB
testcase_23 AC 14 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 257 ms
11,776 KB
testcase_29 AC 262 ms
11,776 KB
testcase_30 AC 327 ms
11,776 KB
testcase_31 AC 217 ms
7,552 KB
testcase_32 AC 536 ms
34,400 KB
testcase_33 AC 502 ms
13,020 KB
testcase_34 AC 413 ms
11,776 KB
testcase_35 AC 407 ms
11,776 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 487 ms
13,016 KB
testcase_39 AC 440 ms
12,264 KB
testcase_40 AC 439 ms
11,648 KB
testcase_41 AC 361 ms
34,400 KB
testcase_42 AC 293 ms
12,260 KB
testcase_43 AC 269 ms
11,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <iostream>
#include <vector>
#include <set>

template <int mod>
struct ModInt
{
    using lint = long long;
    static int get_mod() { return mod; }
    static int get_primitive_root() {
        assert(mod == 998244353);
        return 3;
    }
    int val;
    constexpr ModInt() : val(0) {}
    constexpr ModInt &_setval(lint v) { val = (v >= mod ? v - mod : v); return *this; }
    constexpr ModInt(lint v) { _setval(v % mod + mod); }
    explicit operator bool() const { return val != 0; }
    constexpr ModInt operator+(const ModInt &x) const { return ModInt()._setval((lint)val + x.val); }
    constexpr ModInt operator-(const ModInt &x) const { return ModInt()._setval((lint)val - x.val + mod); }
    constexpr ModInt operator*(const ModInt &x) const { return ModInt()._setval((lint)val * x.val % mod); }
    constexpr ModInt operator/(const ModInt &x) const { return ModInt()._setval((lint)val * x.inv() % mod); }
    constexpr ModInt operator-() const { return ModInt()._setval(mod - val); }
    constexpr ModInt &operator+=(const ModInt &x) { return *this = *this + x; }
    constexpr ModInt &operator-=(const ModInt &x) { return *this = *this - x; }
    constexpr ModInt &operator*=(const ModInt &x) { return *this = *this * x; }
    constexpr ModInt &operator/=(const ModInt &x) { return *this = *this / x; }
    friend constexpr ModInt operator+(lint a, const ModInt &x) { return ModInt()._setval(a % mod + x.val); }
    friend constexpr ModInt operator-(lint a, const ModInt &x) { return ModInt()._setval(a % mod - x.val + mod); }
    friend constexpr ModInt operator*(lint a, const ModInt &x) { return ModInt()._setval(a % mod * x.val % mod); }
    friend constexpr ModInt operator/(lint a, const ModInt &x) { return ModInt()._setval(a % mod * x.inv() % mod); }
    constexpr bool operator==(const ModInt &x) const { return val == x.val; }
    constexpr bool operator!=(const ModInt &x) const { return val != x.val; }
    friend std::istream &operator>>(std::istream &is, ModInt &x) { lint t; is >> t; x = ModInt(t); return is; }
    friend std::ostream &operator<<(std::ostream &os, const ModInt &x) { os << x.val;  return os; }
    constexpr lint power(lint n) const {
        lint ans = 1, tmp = this->val;
        while (n) {
            if (n & 1) ans = ans * tmp % mod;
            tmp = tmp * tmp % mod;
            n /= 2;
        }
        return ans;
    }
    constexpr lint inv() const { return this->power(mod - 2); }
};

template <typename MODINT>
void ntt(std::vector<MODINT> &a, bool is_inverse = false)
{
    int n = a.size();
    if (n == 1) return;
    static const int mod = MODINT::get_mod();
    static const MODINT root = MODINT::get_primitive_root();
    assert(__builtin_popcount(n) == 1 and (mod - 1) % n == 0);

    static std::vector<MODINT> w{1}, iw{1};
    for (int m = w.size(); m < n / 2; m *= 2)
    {
        MODINT dw = root.power((mod - 1) / (4 * m)), dwinv = 1 / dw;
        w.resize(m * 2), iw.resize(m * 2);
        for (int i = 0; i < m; i++) w[m + i] = w[i] * dw, iw[m + i] = iw[i] * dwinv;
    }

    if (!is_inverse) {
        for (int m = n; m >>= 1;) {
            for (int s = 0, k = 0; s < n; s += 2 * m, k++) {
                for (int i = s; i < s + m; i++) {
                    MODINT x = a[i], y = a[i + m] * w[k];
                    a[i] = x + y, a[i + m] = x - y;
                }
            }
        }
    }
    else {
        for (int m = 1; m < n; m *= 2) {
            for (int s = 0, k = 0; s < n; s += 2 * m, k++) {
                for (int i = s; i < s + m; i++) {
                    MODINT x = a[i], y = a[i + m];
                    a[i] = x + y, a[i + m] = (x - y) * iw[k];
                }
            }
        }
        int n_inv = MODINT(n).inv();
        for (auto &v : a) v *= n_inv;
    }
}

template <typename MODINT>
void ntt2d(std::vector<std::vector<MODINT>> &mat)
{
    for (auto &vec : mat) ntt(vec, false);
    int h = mat.size(), w = mat[0].size();
    for (int j = 0; j < w; j++)
    {
        std::vector<MODINT> v(h);
        for (int i = 0; i < h; i++) v[i] = mat[i][j];
        ntt(v, false);
        for (int i = 0; i < h; i++) mat[i][j] = v[i];
    }
}
template <typename MODINT>
void ntt2dinv(std::vector<std::vector<MODINT>> &mat)
{
    int h = mat.size(), w = mat[0].size();
    for (int j = 0; j < w; j++)
    {
        std::vector<MODINT> v(h);
        for (int i = 0; i < h; i++) v[i] = mat[i][j];
        ntt(v, true);
        for (int i = 0; i < h; i++) mat[i][j] = v[i];
    }
    for (auto &vec : mat) ntt(vec, true);
}

using namespace std;
using mint = ModInt<998244353>;

int main()
{
    int X, Y;
    long long T;
    int a, b, c, d;
    cin >> X >> Y >> T >> a >> b >> c >> d;
    assert(X >= 1 and Y >= 1);
    assert(X + Y <= 18);
    assert(T >= 1 and T <= 1'000'000'000'000'000'000LL);
    assert(a >= 1 and a < (1 << X));
    assert(b >= 1 and b < (1 << Y));
    assert(c >= 1 and c < (1 << X));
    assert(d >= 1 and d < (1 << Y));

    vector dp(1 << (X + 1), vector<mint>(1 << (Y + 1)));
    dp[a - 1][b - 1] = 1;
    dp[a - 1][dp[a - 1].size() - b - 1] = -1;
    dp[dp.size() - a - 1][b - 1] = -1;
    dp[dp.size() - a - 1][dp[a - 1].size() - b - 1] = 1;

    vector trans(1 << (X + 1), vector<mint>(1 << (Y + 1)));
    trans[0][0] = trans[0][1] = trans[0].back() = trans[1][0] = trans.back()[0] = 1;

    ntt2d(dp);
    ntt2d(trans);
    for (size_t i = 0; i < dp.size(); i++)
    {
        for (size_t j = 0; j < dp[i].size(); j++)
        {
            dp[i][j] *= trans[i][j].power(T);
        }
    }
    ntt2dinv(dp);
    cout << dp[c - 1][d - 1] << '\n';
}
0