結果
| 問題 | No.2206 Popcount Sum 2 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-02-09 21:52:24 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,130 ms / 4,000 ms |
| コード長 | 4,118 bytes |
| コンパイル時間 | 1,905 ms |
| コンパイル使用メモリ | 206,092 KB |
| 最終ジャッジ日時 | 2025-02-10 11:50:11 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 18 |
ソースコード
#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;
}