codeforces 869C The Intriguing Obsession【组合数学+dp+第二类斯特林公式】
1 second
256 megabytes
standard input
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 a, b and 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. 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 isat 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. 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.
The first and only line of input contains three space-separated integers a, b and c (1 ≤ a, b, c ≤ 5 000) — the number of islands in the red, blue and purple clusters, respectively.
Output one line containing an integer — the number of different ways to build bridges, modulo 998 244 353.
1 1 1
8
1 2 2
63
1 3 5
3264
6 2 9
813023575
In the first example, there are 3 bridges that can possibly be built, and no setup of bridges violates the restrictions. Thus the answer is 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.
【题意】:给出三种颜色岛屿的数量,问有多少种建桥方法。限制是:对于相同颜色的岛屿,要么不能直接相连,要么最少相距为3。
【分析】:知识清单:http://blog.csdn.net/sr_19930829/article/details/40888349
dp的转移方程其实挺好想的,我们每次加入一个岛时,我们可以选择与另一个群岛上的一个岛造桥,或者选择不造,就可以很快想到,f[i][j] = f[i-1][j]+f[i-1][j-1]*j,初始状态是f[0][1]=f[0][i]=1(1≤i≤5000易得)。
对于3个群岛,我们分别考虑每两个群岛之间不与第三个群岛造桥的方案数,即第一个群岛与第二个群岛之间的造桥方案,第一个群岛与第三个群岛之间的造桥方案,第三个群岛与第二个群岛之间的造桥方案,最后把他们乘起来就好了,复杂度O(5000*5000),dp完后ans=f[a][b]*f[a][c]%mod*f[b][c]%mod。
由于同岛群的任意两岛最短距离至少为 3 或不能有路径。则可知,非法路径的连接方案为:
- 同岛群两岛直接连接。
- 同岛群两岛均与另一岛群的某岛连接。
故反向条件为:任意两岛群之间取任意 k (岛群数岛群数k∈[0,min(岛群数1,岛群数2)]) 个点,两两建桥均为合法。三个岛群的总方案数即认为是 (a岛群, b岛群) * (a岛群, c岛群) * (b岛群, c岛群) 。
【代码】:
//327ms/1s
#include<bits/stdc++.h>
using namespace std;
const int base = , nmax=;
typedef long long ll;
ll f[nmax][nmax]; int main()
{
for (int i=;i<nmax;++i)
for (int j=;j<nmax;++j)
f[i][j]=(i==||j==)?:(f[i][j-]+i*f[i-][j-])%base; int a,b,c;
cin>>a>>b>>c;
cout<<f[a][b]*f[b][c]%base*f[c][a]%base;
}
codeforces 869C The Intriguing Obsession【组合数学+dp+第二类斯特林公式】的更多相关文章
- Codeforces 869C The Intriguing Obsession:组合数 or dp
题目链接:http://codeforces.com/problemset/problem/869/C 题意: 红色.蓝色.紫色的小岛分别有a,b,c个. 你可以在两个不同的岛之间架桥,桥的长度为1. ...
- Codeforces 869C The Intriguing Obsession
题意:有三种颜色的岛屿各a,b,c座,你可以在上面建桥.联通的点必须满足以下条件:1.颜色不同.2.颜色相同且联通的两个点之间的最短路径为3 其实之用考虑两种颜色的即可,状态转移方程也不难推出:F[i ...
- CodeForces - 869C The Intriguing Obsession(组合数)
题意:有三个集合,分别含有a.b.c个点,要求给这些点连线,也可以全都不连,每两点距离为1,在同一集合的两点最短距离至少为3的条件下,问有多少种连接方案. 分析: 1.先研究两个集合,若每两个集合都保 ...
- Codeforces Round #100 E. New Year Garland (第二类斯特林数+dp)
题目链接: http://codeforces.com/problemset/problem/140/E 题意: 圣诞树上挂彩球,要求从上到下挂\(n\)层彩球.已知有\(m\)种颜色的球,球的数量不 ...
- 【hdu4045】Machine scheduling(dp+第二类斯特林数)
传送门 题意: 从\(n\)个人中选\(r\)个出来,但每两个人的标号不能少于\(k\). 再将\(r\)个人分为不超过\(m\)个集合. 问有多少种方案. 思路: 直接\(dp\)预处理出从\(n\ ...
- codeforces 659 G. Fence Divercity 组合数学 dp
http://codeforces.com/problemset/problem/659/G 思路: f(i,0/1,0/1) 表示到了第i个,要被切的块开始了没有,结束了没有的状态的方案数 递推看代 ...
- cf 869c The Intriguing Obsession
题意:有三种三色的岛,用a,b,c来标识这三种岛.然后规定,同种颜色的岛不能相连,而且同种颜色的岛不能和同一个其他颜色的岛相连.问有多少种建桥的方法. 题解:em....dp.我们先看两个岛之间怎么个 ...
- CF869C The Intriguing Obsession(组合数学瞎搞,O(n)莫名过)
— This is not playing but duty as allies of justice, Nii-chan! — Not allies but justice itself, Onii ...
- BZOJ 2159: Crash 的文明世界(树形dp+第二类斯特林数+组合数)
题意 给定一棵 \(n\) 个点的树和一个常数 \(k\) , 对于每个 \(i\) , 求 \[\displaystyle S(i) = \sum _{j=1} ^ {n} \mathrm{dist ...
随机推荐
- P1717 钓鱼
题目描述 话说发源于小朋友精心设计的游戏被电脑组的童鞋们藐杀之后非常不爽,为了表示安慰和鼓励,VIP999决定请他吃一次“年年大丰收”,为了表示诚意,他还决定亲自去钓鱼,但是,因为还要准备2013NO ...
- win7中输入文件夹首字母跳到相应的文件或者文件夹,却在搜索栏出现输入的字母
组织->文件夹和搜索选项->查看->在视图中选择键入项
- 【CF MEMSQL 3.0 C. Pie Rules】
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- VC++使用CImage在内存中Jpeg转换Bmp图片
VC++中Jpeg与Bmp图片格式互转应该是会经常遇到,Jpeg相比Bmp在图片大小上有很大优势. 本文重点介绍使用现有的CImage类在内存中进行转换,不需要保存为文件,也不需要引入第三方库. Li ...
- Centos7下redis设置密码、开放远程访问权限
redis的安装与启动可参考前一篇文章:http://www.cnblogs.com/zuidongfeng/p/8032505.html redis安装成功后,默认是没有设置密码的启动redis-c ...
- lesson 4 再谈继承多态,抽象类和接口
再谈多态,抽象类和接口 上一次博客已经概念性的概述了继承多态,抽象类和接口,这次来具体的谈一谈他们之间的联系和需要注意的地方. 一.继承和多态:Inheritance (继承) & Polym ...
- Tomcat启动报错:java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addServlet
测试tomcat的comet Java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addServlet 异常 因为工程 ...
- loadView不需要调用super view,原因:loadView方法的作用就是自定义view.[super loadView]会耗性能.
https://www.evernote.com/shard/s227/sh/423fd81d-ab1c-4e6c-997d-39359472a4a5/f220ade8bd9be149ad70 ...
- js三层引号嵌套
··· 参考:https://blog.csdn.net/feiyangbaxia/article/details/49681131 第一层用双引号,第二层转义双引号,第三层单引号
- python3 面向过程编程思想,函数综合应用
应用:grep -rl 'root' /etc 实现过滤文件的功能 import os def init(func): def wrapper(*args,**kwargs): g=func(*arg ...