Submission #34072002
Source Code Expand
/*
[x^A y^B z^C] (1 - x*y*z) / (1 - x - y - z + 2*x*y*z)
[x^A y^B z^C] \sum_{i} (A-i+B-i+C-i+i)! /(i! (A-i)!(B-i)!(C-i)!) (-2)^i
*/
#include <iostream>
using i32 = int;
using u32 = unsigned;
using i64 = long long;
template <i32 MOD>
struct Mint {
i32 n;
constexpr Mint(i32 n = 0): n(n) {}
constexpr Mint operator-() const { return Mint(n ? MOD - n: 0); }
constexpr Mint &operator+=(const Mint &rhs){ n += rhs.n; if(n >= MOD) n -= MOD; return *this; }
constexpr Mint &operator-=(const Mint &rhs){ if(rhs.n > n) n += MOD; n -= rhs.n; return *this; }
constexpr Mint &operator*=(const Mint &rhs){ n = (i64) n * rhs.n % MOD; return *this; }
constexpr Mint inv() const {
i32 x = MOD;
i32 y = n;
i32 b = 0, d = 1;
while(y){
i32 q = x / y;
x = x % y;
b -= q * d;
std::swap(x, y);
std::swap(b, d);
}
if(b < 0) b += MOD;
return b;
}
constexpr Mint &operator/=(const Mint &rhs){ n = (i64) n * rhs.inv().n % MOD; return *this; }
friend constexpr Mint operator+(const Mint &lhs, const Mint &rhs){ return Mint(lhs) += rhs; }
friend constexpr Mint operator-(const Mint &lhs, const Mint &rhs){ return Mint(lhs) -= rhs; }
friend constexpr Mint operator*(const Mint &lhs, const Mint &rhs){ return Mint(lhs) *= rhs; }
friend constexpr Mint operator/(const Mint &lhs, const Mint &rhs){ return Mint(lhs) /= rhs; }
friend constexpr bool operator==(const Mint &lhs, const Mint &rhs){ return lhs.n == rhs.n; }
friend constexpr bool operator!=(const Mint &lhs, const Mint &rhs){ return lhs.n != rhs.n; }
friend std::ostream &operator<<(std::ostream &os, const Mint &rhs){ return os << rhs.n; }
};
constexpr i32 mod = 998244353;
using mint = Mint<mod>;
mint fact[3000001];
mint ifact[3000001];
int main(){
i32 A, B, C;
std::cin >> A >> B >> C;
const i32 d = std::min(std::min(A, B), C);
const i32 k = A + B + C;
fact[0] = 1;
for(i32 i = 1; i <= k; i++) fact[i] = i * fact[i-1];
ifact[k] = fact[k].inv();
for(i32 i = k-1; i >= 0; i--) ifact[i] = (i+1) * ifact[i+1];
mint ans = 0;
mint p = 1;
for(i32 i = 0; i <= d; i++){
ans += fact[A+B+C-2*i] * ifact[i] * ifact[A-i] * ifact[B-i] * ifact[C-i] * p;
p *= mod-2;
}
p = 1;
--A, --B, --C;
for(i32 i = 0; i <= d; i++){
ans -= fact[A+B+C-2*i] * ifact[i] * ifact[A-i] * ifact[B-i] * ifact[C-i] * p;
p *= mod-2;
}
std::cout << ans << std::endl;
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | D - Yet Another ABC String |
| User | ryuhei |
| Language | C++ (GCC 9.2.1) |
| Score | 1000 |
| Code Size | 2507 Byte |
| Status | AC |
| Exec Time | 67 ms |
| Memory | 27020 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 1000 / 1000 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt, 00-sample-004.txt |
| All | 00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt, 00-sample-004.txt, 01-001.txt, 01-002.txt, 01-003.txt, 01-004.txt, 01-005.txt, 01-006.txt, 01-007.txt, 01-008.txt, 01-009.txt, 01-010.txt, 01-011.txt, 01-012.txt, 01-013.txt, 01-014.txt, 01-015.txt, 01-016.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00-sample-001.txt | AC | 10 ms | 3476 KiB |
| 00-sample-002.txt | AC | 2 ms | 3516 KiB |
| 00-sample-003.txt | AC | 3 ms | 3584 KiB |
| 00-sample-004.txt | AC | 12 ms | 6436 KiB |
| 01-001.txt | AC | 30 ms | 12696 KiB |
| 01-002.txt | AC | 23 ms | 12784 KiB |
| 01-003.txt | AC | 41 ms | 17248 KiB |
| 01-004.txt | AC | 25 ms | 13184 KiB |
| 01-005.txt | AC | 28 ms | 14428 KiB |
| 01-006.txt | AC | 59 ms | 25516 KiB |
| 01-007.txt | AC | 61 ms | 26104 KiB |
| 01-008.txt | AC | 62 ms | 25812 KiB |
| 01-009.txt | AC | 64 ms | 26064 KiB |
| 01-010.txt | AC | 61 ms | 25500 KiB |
| 01-011.txt | AC | 67 ms | 26808 KiB |
| 01-012.txt | AC | 65 ms | 27020 KiB |
| 01-013.txt | AC | 65 ms | 26988 KiB |
| 01-014.txt | AC | 64 ms | 26792 KiB |
| 01-015.txt | AC | 67 ms | 26956 KiB |
| 01-016.txt | AC | 63 ms | 26916 KiB |