結果

問題 No.2206 Popcount Sum 2
ユーザー jianglyjiangly
提出日時 2023-02-09 21:52:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,354 ms / 4,000 ms
コード長 4,118 bytes
コンパイル時間 2,378 ms
コンパイル使用メモリ 210,656 KB
実行使用メモリ 10,020 KB
最終ジャッジ日時 2023-09-21 03:58:56
合計ジャッジ時間 17,871 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 329 ms
6,656 KB
testcase_03 AC 336 ms
6,744 KB
testcase_04 AC 333 ms
6,632 KB
testcase_05 AC 1,347 ms
9,736 KB
testcase_06 AC 1,350 ms
10,020 KB
testcase_07 AC 1,350 ms
9,796 KB
testcase_08 AC 1,354 ms
9,808 KB
testcase_09 AC 1,347 ms
9,868 KB
testcase_10 AC 593 ms
9,732 KB
testcase_11 AC 592 ms
9,720 KB
testcase_12 AC 591 ms
10,016 KB
testcase_13 AC 488 ms
9,792 KB
testcase_14 AC 489 ms
9,772 KB
testcase_15 AC 488 ms
9,796 KB
testcase_16 AC 694 ms
9,808 KB
testcase_17 AC 699 ms
9,864 KB
testcase_18 AC 698 ms
9,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using i64 = long long;
template<class T>
T power(T a, i64 b) {
    T res = 1;
    for (; b; b /= 2, a *= a) {
        if (b % 2) {
            res *= a;
        }
    }
    return res;
}

template<int P>
struct MInt {
    int x;
    MInt() : x{} {}
    MInt(i64 x) : x{norm(x % P)} {}
    
    int norm(int x) {
        if (x < 0) {
            x += P;
        }
        if (x >= P) {
            x -= P;
        }
        return x;
    }
    int val() const {
        return x;
    }
    MInt operator-() const {
        MInt res;
        res.x = norm(P - x);
        return res;
    }
    MInt inv() const {
        assert(x != 0);
        return power(*this, P - 2);
    }
    MInt &operator*=(const MInt &rhs) {
        x = 1LL * x * rhs.x % P;
        return *this;
    }
    MInt &operator+=(const MInt &rhs) {
        x = norm(x + rhs.x);
        return *this;
    }
    MInt &operator-=(const MInt &rhs) {
        x = norm(x - rhs.x);
        return *this;
    }
    MInt &operator/=(const MInt &rhs) {
        return *this *= rhs.inv();
    }
    friend MInt operator*(const MInt &lhs, const MInt &rhs) {
        MInt res = lhs;
        res *= rhs;
        return res;
    }
    friend MInt operator+(const MInt &lhs, const MInt &rhs) {
        MInt res = lhs;
        res += rhs;
        return res;
    }
    friend MInt operator-(const MInt &lhs, const MInt &rhs) {
        MInt res = lhs;
        res -= rhs;
        return res;
    }
    friend MInt operator/(const MInt &lhs, const MInt &rhs) {
        MInt res = lhs;
        res /= rhs;
        return res;
    }
    friend std::istream &operator>>(std::istream &is, MInt &a) {
        i64 v;
        is >> v;
        a = MInt(v);
        return is;
    }
    friend std::ostream &operator<<(std::ostream &os, const MInt &a) {
        return os << a.val();
    }
};

constexpr int P = 998244353;
using Z = MInt<P>;

struct Comb {
    int n;
    std::vector<Z> _fac;
    std::vector<Z> _invfac;
    std::vector<Z> _inv;
    
    Comb() : n{0}, _fac{1}, _invfac{1}, _inv{0} {}
    Comb(int n) : Comb() {
        init(n);
    }
    
    void init(int m) {
        if (m <= n) return;
        _fac.resize(m + 1);
        _invfac.resize(m + 1);
        _inv.resize(m + 1);
        
        for (int i = n + 1; i <= m; i++) {
            _fac[i] = _fac[i - 1] * i;
        }
        _invfac[m] = _fac[m].inv();
        for (int i = m; i > n; i--) {
            _invfac[i - 1] = _invfac[i] * i;
            _inv[i] = _invfac[i] * _fac[i - 1];
        }
        n = m;
    }
    
    Z fac(int m) {
        if (m > n) init(2 * m);
        return _fac[m];
    }
    Z invfac(int m) {
        if (m > n) init(2 * m);
        return _invfac[m];
    }
    Z inv(int m) {
        if (m > n) init(2 * m);
        return _inv[m];
    }
    Z binom(int n, int m) {
        if (n < m || m < 0) return 0;
        return fac(n) * invfac(m) * invfac(n - m);
    }
} comb;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int t;
    std::cin >> t;
    
    std::vector<std::array<int, 3>> qry(t);
    for (int i = 0; i < t; i++) {
        int n, m;
        std::cin >> n >> m;
        qry[i] = {n, m, i};
    }
    
    constexpr int B = 500;
    std::sort(qry.begin(), qry.end(), [&](auto a, auto b) {
        if (a[0] / B != b[0] / B) {
            return a[0] < b[0];
        } else {
            return a[1] < b[1];
        }
    });
    
    int N = 0, M = 0;
    Z res = 1;
    
    std::vector<Z> ans(t);
    for (auto [n, m, i] : qry) {
        while (N < n - 1) {
            res = 2 * res - comb.binom(N, M);
            N++;
        }
        while (M > m - 1) {
            res -= comb.binom(N, M);
            M--;
        }
        while (N > n - 1) {
            N--;
            res = (res + comb.binom(N, M)) * ((P + 1) / 2);
        }
        while (M < m - 1) {
            M++;
            res += comb.binom(N, M);
        }
        
        ans[i] = res * (power(Z(2), n) - 1);
    }
    
    for (int i = 0; i < t; i++) {
        std::cout << ans[i] << "\n";
    }
    
    return 0;
}
0