【组合数】Codeforces-869C. The Intriguing Obsession
题目链接(https://codeforces.com/problemset/problem/869/C)
C. The Intriguing Obsession
time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output
— This is not playing but duty as allies of justice, Nii-chan!
— Not allies but justice itself, Onii-chan!
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 , and 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 . 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 . 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 , and — 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 .
Examples
input
1 1 1
output
8
input
1 2 2
output
63
input
1 3 5
output
3264
input
6 2 9
output
813023575
Note
In the first example, there are bridges that can possibly be built, and no setup of bridges violates the restrictions. Thus the answer is .
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.
题意
岛上有三种颜色的岛屿,分别是红色、蓝色和紫色。岛群分别由 ,和个不同的岛组成。
在一些(可能全部或没有)岛屿之间建立了桥梁。一座桥双向连接两个不同的岛,长度为。对于任意两个相同颜色的岛,要么不能通过桥相互到达,要么它们之间的最短距离至少为 。
Fire Sisters 已准备好迎接未知,但他们也想测试您的勇气。你来这里是为了找出在约束条件下建造所有桥梁的不同方法的数量,并给出模 的答案。如果存在一对岛,它们之间有一座桥,但另一种没有桥,则两种方法被认为是不同的。
思路
由题意可知,同岛不可建桥,如果一个岛中两个不同点连了另一个岛的同一个点也是不合法的。
根据抽屉原理,两岛中要想连边,两岛中选择的点的数量必须要相同。
然后从两岛中每次取个点,取法共。两岛分别找出来的个点之间可以任意相连,共种。故两岛的合法答案有种。
同理三个岛的建桥操作,不进行不合法操作均可成立,易得三岛连线都是互不干扰的,可以分别计算,相乘取模即得答案。预处理可以用杨辉三角,也可以用阶乘的逆元。
(本题可以用dp写,我dp学的属实垃圾,想不来)
AC代码
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3f
#define int long long
#define ull unsigned long long
#define PII pair<int,int>
using namespace std;
typedef long long ll;
const int N = 5e3 + 10;
const int mod = 998244353;
int a, b, c;
int C[N][N],fact[N];
inline int read() {//快读
int f = 1, x = 0; char ch;
do { ch = getchar(); if (ch == '-') f = -1; } while (ch<'0' || ch>'9');
do { x = x * 10 + ch - '0'; ch = getchar(); } while (ch >= '0'&&ch <= '9');
return x * f;
}
void init() {//阶乘,组合数预处理
fact[0] = 1;
for (int i = 0; i <= 5001; i++) {
if (i) fact[i] = i * fact[i - 1] % mod;
for (int j = 0; j <= i; j++) {
if (!j) C[i][j] = 1;
else C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
}
}
}
int fun(int x, int y) {//合法答案求法
int res = 0;
for (int i = 0; i <= min(x, y); i++) {
res = (res + (C[x][i] * C[y][i] % mod * fact[i]) % mod) % mod;
}
return res;
}
void solve() {
int ans1 = fun(a, b);
int ans2 = fun(c, b);
int ans3 = fun(a, c);
int ans = ans1 * ans2 % mod * ans3 % mod;//相乘取模,输出
printf("%lld\n", ans);
return;
}
signed main() {
// ios::sync_with_stdio(false);
// cin.tie(0); cout.tie(0);
a = read(); b = read(); c = read();
init();
solve();
return 0;
}
/*
i raised a cute kitty in my code,
my friend who pass by can touch softly on her head:)
/l、
Meow~(゚、 。7
|、 ~ヽ
じしf_,)ノ
*/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用