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 ...
随机推荐
- P1559 运动员最佳匹配问题
题目描述 羽毛球队有男女运动员各n人.给定2 个n×n矩阵P和Q.P[i][j]是男运动员i和女运动员j配对组成混合双打的男运动员竞赛优势:Q[i][j]是女运动员i和男运动员j配合的女运动员竞赛优势 ...
- Java23个设计模式的简明教程
设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. 毫无疑问,设计模式于 ...
- 【题解】ZJOI2007报表统计
洛谷传送门 主要思路大概也是差不多的,对于两种询问分别用线段树与平衡树来维护. 1.MIN_SORT_GAP:显然平衡树简单操作,来一发前驱.后继即可. 2.MIN_GAP:这一个我用的是线段树:可以 ...
- HTML5用canvas绘制五星红旗
在HTML5一览中,我们提到html 5被冠以很多高帽,其中最高的一顶.备受争议的就是"Flash杀手".IT评论界老喜欢用这个词了,杀手无处不在.不管是不是杀手,HTML 5引进 ...
- [codeforces gym Matrix God]随机矩阵乘法
题目链接:http://codeforces.com/gym/101341/problem/I 随机真是一个神奇的方法.原本矩阵乘法是n^3的复杂度,但是这个题是让判断两个矩阵是否相等,只需要在两个矩 ...
- codeforces2015ICL,Finals,Div.1#J Ceizenpok’s formula 扩展Lucas定理 扩展CRT
默默敲了一个下午,终于过了, 题目传送门 扩展Lucas是什么,就是对于模数p,p不是质数,但是不大,如果是1e9这种大数,可能没办法, 对于1000000之内的数是可以轻松解决的. 题解传送门 代码 ...
- docker公司测试环境搭建总结
1.防火墙转发规则: [root@docker ~]# firewall-cmd --list-allpublic (active) target: default icmp-block-invers ...
- Javascript 的addEventListener()及attachEvent()区别分析
大家都知道事件的用法就是当某个事件(状况)被触发了之后就会去执行某个Function, 尤其是Javascript, 在当红AJAX的催化下, 了解Javascript的Event用法更加重要, 在这 ...
- C++构造函数重载以及默认参数引起的二义性
大家都知道当我们声明一个类时,系统会提供一个默认构造函数.当我们需要提供参数进行对类数据成员进行初始化时,就需要对类的带参构造函数进行重载.同时,如果我们需要调用默认构造函数进行类数据成员的初始化时, ...
- jsp中路径的问题。。。
刚刚学jsp的时候都是从看别人的代码,模仿着做,但是这样也有看不懂的地方,这个相对路径和绝对路径就让我纠结了好久..所以我自己弄了一个demo实验了一下,试验出结果了,但是不明白原理,纠结了一天,终于 ...