Codeforces Round 439 (Div. 2) C. The Intriguing Obsession

The Intriguing Obsession

time limit per test: 1 second
memory limit per test: 512 megabytes
input: standard input
output: standard output

With hands joined, go everywhere at a speed faster than our thoughts! This time, the Fire Sisters — Karen and Tsukihi — is heading for somewhere they’ve never reached — water-surrounded islands!

There are three clusters of islands, conveniently coloured red, blue and purple. The clusters consist of a a a, b b b and c c c distinct islands respectively.

Bridges have been built between some (possibly all or none) of the islands. A bridge bidirectionally connects two different islands and has length 1 1 1. For any two islands of the same colour, either they shouldn’t be reached from each other through bridges, or the shortest distance between them is at least 3, apparently in order to prevent oddities from spreading quickly inside a cluster.

The Fire Sisters are ready for the unknown, but they’d also like to test your courage. And you’re here to figure out the number of different ways to build all bridges under the constraints, and give the answer modulo 998   244   353 998\ 244\ 353 998 244 353. Two ways are considered different if a pair of islands exist, such that there’s a bridge between them in one of them, but not in the other.

Input

The first and only line of input contains three space-separated integers a a a, b b b and c c c ( 1   ≤   a ,   b ,   c   ≤   5000 ) (1 \leq a, b, c \leq 5000) (1 a,b,c 5000) — the number of islands in the red, blue and purple clusters, respectively.

Output

Output one line containing an integer — the number of different ways to build bridges, modulo 998   244   353 998\ 244\ 353 998 244 353.

Example

i n p u t \tt input input
1 1 1
o u t p u t \tt output output
8
i n p u t \tt input input
1 2 2
o u t p u t \tt output output
63
i n p u t \tt input input
1 3 5
o u t p u t \tt output output
3257
i n p u t \tt input input
1 3 5
o u t p u t \tt output output
813023575

Note

In the first example, there are 3 3 3 bridges that can possibly be built, and no setup of bridges violates the restrictions. Thus the answer is 2 3   =   8 2^3 = 8 23= 8.

In the second example, the upper two structures in the figure below are instances of valid ones, while the lower two are invalid due to the blue and purple clusters, respectively.

Tutorial

由题意得,两个相邻颜色的岛要么不通过桥梁相连接,要么它们最短的距离至少为 3 3 3,则表示以下两种情况:

{ 两个相同颜色的岛不直接相连 一个岛不被两个同色岛相连接 \left\{\begin{matrix} 两个相同颜色的岛不直接相连 \\ 一个岛不被两个同色岛相连接 \end{matrix}\right. {两个相同颜色的岛不直接相连一个岛不被两个同色岛相连接

所以可以把所有情况分为三类:

{ 红色岛 与 蓝色岛 相连 红色岛 和 紫色岛 相连 蓝色岛 和 紫色岛 相连 \left\{\begin{matrix} 红色岛\ 与\ 蓝色岛\ 相连 \\ 红色岛\ 和\ 紫色岛\ 相连 \\ 蓝色岛\ 和\ 紫色岛\ 相连\end{matrix}\right. 红色岛  蓝色岛 相连红色岛  紫色岛 相连蓝色岛  紫色岛 相连

对于任意两种颜色的岛(以红色岛和蓝色岛为例),设红色岛的数量为 x x x,蓝色岛的数量为 y y y,两种颜色的岛中间连了 k k k 座桥,此时在红色岛中选 k k k 个岛有 C x k C_x^k Cxk 种选择,在蓝色岛中选 k k k 个岛有 C y k C_y^k Cyk 种选择,最后把两种颜色的岛相连接,共有 k ! k! k! 种连法,所以此时共有 C x k C y k k ! C_x^k C_y^k k! CxkCykk! 种方案

综上所述,对于每两类岛,共有 ∑ k = 0 min ⁡ ( x , y ) C x k C y k k ! \sum_{k = 0}^{\min(x, y)} C_x^k C_y^k k! k=0min(x,y)CxkCykk! 种方案,则总共有 ∑ k = 0 min ⁡ ( a , b ) C a k C b k k ! × ∑ k = 0 min ⁡ ( a , c ) C a k C c k k ! × ∑ k = 0 min ⁡ ( b , c ) C b k C c k k ! \sum_{k = 0}^{\min(a, b)} C_a^k C_b^k k! \times \sum_{k = 0}^{\min(a, c)} C_a^k C_c^k k! \times \sum_{k = 0}^{\min(b, c)} C_b^k C_c^k k! k=0min(a,b)CakCbkk!×k=0min(a,c)CakCckk!×k=0min(b,c)CbkCckk! 种方案 (注意取余)

此解法时间复杂度为 O ( max ⁡ ( a , b , c ) ) \mathcal O(\max(a, b, c)) O(max(a,b,c))

Solution

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define int long long
const int mod = 998244353;

// C a 取 b = fact[a] * infact[b - a] * infact[b],注意取余
struct Comb {
    vector<int> fact, infact;

    int ksm(int a, int b, int mod) {
        a %= mod;
        int ans = 1;
        while (b) {
            if (b & 1) {
                ans = (ans * a) % mod;
            }
            a = (a * a) % mod;
            b >>= 1;
        }
        return ans;
    }
    
    Comb() {}
    Comb(int n) {
        fact.resize(n + 1);
        infact.resize(n + 1);
        fact[0] = infact[0] = 1;
        for (int i = 1; i <= n; ++i) {
            fact[i] = fact[i - 1] * i % mod;
            infact[i] = infact[i - 1] * ksm(i, mod - 2, mod) % mod;
        }
    }

    int C(int a, int b, int mod) { // C a 取 b,对 mod 取余
        if (a < b) {
            return 0;
        }
        return fact[a] * infact[a - b] % mod * infact[b] % mod;
    }
};

// x 的阶乘
int factorial(int x, int mod) {
    if (x < 2) {
        return 1;
    }
    return x * factorial(x - 1, mod) % mod;
}

signed main() {
    int a, b, c, ans = 0;
    cin >> a >> b >> c;
    Comb comb(5000);

    function<int(int, int, int)> f = [&](int x, int y, int mod) -> int {
        int ans = 0;
        for (int i = 0; i <= min(x, y); ++i) {
            ans = (ans + (comb.C(x, i, mod)) % mod * comb.C(y, i, mod) % mod * factorial(i, mod) % mod) % mod;
        }
        return ans;
    };
    
    cout << f(a, b, mod) * f(a, c, mod) % mod * f(b, c, mod) % mod;
    return 0;
}
  • 23
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值