結果

問題 No.1757 Many Many Cards
ユーザー torisasami4torisasami4
提出日時 2021-11-20 14:13:15
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 88 ms / 2,000 ms
コード長 7,424 bytes
コンパイル時間 4,233 ms
コンパイル使用メモリ 223,476 KB
実行使用メモリ 18,916 KB
最終ジャッジ日時 2023-09-02 08:16:34
合計ジャッジ時間 7,153 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
18,588 KB
testcase_01 AC 29 ms
18,392 KB
testcase_02 AC 29 ms
18,568 KB
testcase_03 AC 29 ms
18,704 KB
testcase_04 AC 29 ms
18,568 KB
testcase_05 AC 28 ms
18,816 KB
testcase_06 AC 28 ms
18,720 KB
testcase_07 AC 29 ms
18,816 KB
testcase_08 AC 30 ms
18,792 KB
testcase_09 AC 29 ms
18,788 KB
testcase_10 AC 29 ms
18,868 KB
testcase_11 AC 30 ms
18,820 KB
testcase_12 AC 30 ms
18,640 KB
testcase_13 AC 29 ms
18,744 KB
testcase_14 AC 28 ms
18,640 KB
testcase_15 AC 28 ms
18,848 KB
testcase_16 AC 29 ms
18,580 KB
testcase_17 AC 28 ms
18,400 KB
testcase_18 AC 29 ms
18,812 KB
testcase_19 AC 30 ms
18,792 KB
testcase_20 AC 29 ms
18,700 KB
testcase_21 AC 37 ms
18,692 KB
testcase_22 AC 66 ms
18,572 KB
testcase_23 AC 70 ms
18,744 KB
testcase_24 AC 52 ms
18,776 KB
testcase_25 AC 63 ms
18,388 KB
testcase_26 AC 30 ms
18,780 KB
testcase_27 AC 43 ms
18,376 KB
testcase_28 AC 73 ms
18,836 KB
testcase_29 AC 60 ms
18,444 KB
testcase_30 AC 31 ms
18,400 KB
testcase_31 AC 31 ms
18,744 KB
testcase_32 AC 48 ms
18,372 KB
testcase_33 AC 36 ms
18,772 KB
testcase_34 AC 37 ms
18,792 KB
testcase_35 AC 41 ms
18,916 KB
testcase_36 AC 88 ms
18,744 KB
testcase_37 AC 87 ms
18,568 KB
testcase_38 AC 29 ms
18,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb(...) emplace_back(__VA_ARGS__)
#define mp(a, b) make_pair(a, b)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define lscan(x) scanf("%I64d", &x)
#define lprint(x) printf("%I64d", x)
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, n) for (ll i = (ll)n - 1; i >= 0; i--)
#define REP(i, l, r) for (ll i = l; i < (r); i++)
#define REP2(i, l, r) for (ll i = (ll)r - 1; i >= (l); i--)
#define siz(x) (ll) x.size()
template <class T>
using rque = priority_queue<T, vector<T>, greater<T>>;

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

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

__int128_t gcd(__int128_t a, __int128_t b) {
    if (a == 0)
        return b;
    if (b == 0)
        return a;
    __int128_t cnt = a % b;
    while (cnt != 0) {
        a = b;
        b = cnt;
        cnt = a % b;
    }
    return b;
}

long long extGCD(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extGCD(b, a % b, y, x);
    y -= a / b * x;
    return d;
}

struct UnionFind {
    vector<ll> data;
    int num;

    UnionFind(int sz) {
        data.assign(sz, -1);
        num = sz;
    }

    bool unite(int x, int y) {
        x = find(x), y = find(y);
        if (x == y)
            return (false);
        if (data[x] > data[y])
            swap(x, y);
        data[x] += data[y];
        data[y] = x;
        num--;
        return (true);
    }

    int find(int k) {
        if (data[k] < 0)
            return (k);
        return (data[k] = find(data[k]));
    }

    ll size(int k) {
        return (-data[find(k)]);
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }
};

template <int mod>
struct Mod_Int {
    int x;

    Mod_Int() : x(0) {
    }

    Mod_Int(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {
    }

    static int get_mod() {
        return mod;
    }

    Mod_Int &operator+=(const Mod_Int &p) {
        if ((x += p.x) >= mod)
            x -= mod;
        return *this;
    }

    Mod_Int &operator-=(const Mod_Int &p) {
        if ((x += mod - p.x) >= mod)
            x -= mod;
        return *this;
    }

    Mod_Int &operator*=(const Mod_Int &p) {
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }

    Mod_Int &operator/=(const Mod_Int &p) {
        *this *= p.inverse();
        return *this;
    }

    Mod_Int &operator++() {
        return *this += Mod_Int(1);
    }

    Mod_Int operator++(int) {
        Mod_Int tmp = *this;
        ++*this;
        return tmp;
    }

    Mod_Int &operator--() {
        return *this -= Mod_Int(1);
    }

    Mod_Int operator--(int) {
        Mod_Int tmp = *this;
        --*this;
        return tmp;
    }

    Mod_Int operator-() const {
        return Mod_Int(-x);
    }

    Mod_Int operator+(const Mod_Int &p) const {
        return Mod_Int(*this) += p;
    }

    Mod_Int operator-(const Mod_Int &p) const {
        return Mod_Int(*this) -= p;
    }

    Mod_Int operator*(const Mod_Int &p) const {
        return Mod_Int(*this) *= p;
    }

    Mod_Int operator/(const Mod_Int &p) const {
        return Mod_Int(*this) /= p;
    }

    bool operator==(const Mod_Int &p) const {
        return x == p.x;
    }

    bool operator!=(const Mod_Int &p) const {
        return x != p.x;
    }

    Mod_Int inverse() const {
        assert(*this != Mod_Int(0));
        return pow(mod - 2);
    }

    Mod_Int pow(long long k) const {
        Mod_Int now = *this, ret = 1;
        for (; k > 0; k >>= 1, now *= now) {
            if (k & 1)
                ret *= now;
        }
        return ret;
    }

    friend ostream &operator<<(ostream &os, const Mod_Int &p) {
        return os << p.x;
    }

    friend istream &operator>>(istream &is, Mod_Int &p) {
        long long a;
        is >> a;
        p = Mod_Int<mod>(a);
        return is;
    }
};

ll mpow2(ll x, ll n, ll mod) {
    ll ans = 1;
    while (n != 0) {
        if (n & 1)
            ans = ans * x % mod;
        x = x * x % mod;
        n = n >> 1;
    }
    return ans;
}
ll modinv2(ll a, ll mod) {
    ll b = mod, u = 1, v = 0;
    while (b) {
        ll t = a / b;
        a -= t * b;
        swap(a, b);
        u -= t * v;
        swap(u, v);
    }
    u %= mod;
    if (u < 0)
        u += mod;
    return u;
}

// constexpr int mod = 1000000007;
constexpr int mod = 998244353;
// constexpr int mod = 31607;
using mint = Mod_Int<mod>;

mint mpow(mint x, ll n) {
    mint ans = 1;
    while (n != 0) {
        if (n & 1)
            ans *= x;
        x *= x;
        n = n >> 1;
    }
    return ans;
}

// ----- library -------
template <typename T>
struct Combination {
    vector<T> _fac, _ifac;

    Combination() {
        init();
    }
    Combination(int n) {
        init(n);
    }

    void init(int n = 2000010) {
        _fac.resize(n + 1), _ifac.resize(n + 1);
        _fac[0] = 1;
        for (int i = 1; i <= n; i++)
            _fac[i] = _fac[i - 1] * i;
        _ifac[n] = _fac[n].inverse();
        for (int i = n; i >= 1; i--)
            _ifac[i - 1] = _ifac[i] * i;
    }

    T fac(int k) {
        return _fac[k];
    }

    T ifac(int k) {
        return _ifac[k];
    }

    T inv(int k) {
        return fac(k - 1) * ifac(k);
    }

    T P(int n, int k) {
        if (k < 0 || n < k)
            return 0;
        return fac(n) * ifac(n - k);
    }

    T C(int n, int k) {
        if (k < 0 || n < k)
            return 0;
        return fac(n) * ifac(n - k) * ifac(k);
    }

    T H(int n, int k) { // k個の区別できない玉をn個の区別できる箱に入れる場合の数
        if (n < 0 || k < 0)
            return 0;
        return k == 0 ? 1 : C(n + k - 1, k);
    }

    T second_stirling_number(int n, int k) { // n個の区別できる玉を、k個の区別しない箱に、各箱に1個以上玉が入るように入れる場合の数
        T ret = 0;
        for (int i = 0; i <= k; i++) {
            T tmp = C(k, i) * T(i).pow(n);
            ret += ((k - i) & 1) ? -tmp : tmp;
        }
        return ret * ifac(k);
    }

    T bell_number(int n, int k) { // n個の区別できる玉を、k個の区別しない箱に入れる場合の数
        if (n == 0)
            return 1;
        k = min(k, n);
        vector<T> pref(k + 1);
        pref[0] = 1;
        for (int i = 1; i <= k; i++) {
            if (i & 1)
                pref[i] = pref[i - 1] - ifac(i);
            else
                pref[i] = pref[i - 1] + ifac(i);
        }
        T ret = 0;
        for (int i = 1; i <= k; i++)
            ret += T(i).pow(n) * ifac(i) * pref[k - i];
        return ret;
    }
};

using comb = Combination<mint>;
// ----- library -------

int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    cout << fixed << setprecision(15);

    comb comb;
    int n, m;
    cin >> n >> m;
    mint ans = (mpow(m, 2 * n) - mpow(m - 1, 2 * n)) * m;
    REP(k, 1, min(m, n + 1)) ans -= comb.C(m, k + 1) * mpow(k + 1, k - 1) * comb.P(n, k) * mpow(2, k) * mpow(m - k - 1, 2 * (n - k));
    cout << ans << endl;
}
0