codeforces 869C The Intriguing Obsession【组合数学+dp+第二类斯特林公式】
— 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 = 998244353, nmax=5002; typedef long long ll; ll f[nmax][nmax]; int main() { for (int i=0;i<nmax;++i) for (int j=0;j<nmax;++j) f[i][j]=(i==0||j==0)?1:(f[i][j-1]+i*f[i-1][j-1])%base; int a,b,c; cin>>a>>b>>c; cout<<f[a][b]*f[b][c]%base*f[c][a]%base; }