Codeforces 869 C The Intriguing Obsession
題目描述
— 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 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 998244353 . 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 aa , bb and cc ( 1<=a,b,c<=5000 ) — 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 998244353.
輸入輸出樣例
輸入樣例#1:1 1 1輸出樣例#1:
8輸入樣例#2:
1 2 2輸出樣例#2:
63輸入樣例#3:
1 3 5輸出樣例#3:
3264輸入樣例#4:
6 2 9輸出樣例#4:
813023575
說明
In the first example, there are 33 bridges that can possibly be built, and no setup of bridges violates the restrictions. Thus the answer is 2^{3}=823=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,所以同色島之間不能有邊並且一個島不能連兩個同色島。
於是我們可以分別求一下2色到1色的方案數,3色到1色的方案數,3色到2色的方案數,然後把它們乘起來就好啦。
求一個搭配的方案數要用一下組合數。。。。
#include<bits/stdc++.h> #define ll long long #define maxn 5005 const int ha=998244353; using namespace std; int jc[maxn],ni[maxn]; int ans=0,a,b,c; inline int ksm(int x,int y){ int an=1; for(;y;y>>=1,x=x*(ll)x%ha) if(y&1) an=an*(ll)x%ha; return an; } inline int add(int x,int y){ x+=y; return x>=ha?x-ha:x; } inline void init(){ jc[0]=1; for(int i=1;i<=5000;i++) jc[i]=jc[i-1]*(ll)i%ha; ni[5000]=ksm(jc[5000],ha-2); for(int i=5000;i;i--) ni[i-1]=ni[i]*(ll)i%ha; } inline int P(int x,int y){ return x<y?0:jc[x]*(ll)ni[x-y]%ha; } inline int C(int x,int y){ return x<y?0:P(x,y)*(ll)ni[y]%ha; } int main(){ init(); cin>>a>>b>>c; for(int i=0;i<=b;i++) ans=add(ans,C(b,i)*(ll)P(a,b-i)%ha); int an[2]; an[0]=an[1]=0; for(int i=0;i<=c;i++){ an[0]=add(an[0],C(c,i)*(ll)P(a,c-i)%ha); an[1]=add(an[1],C(c,i)*(ll)P(b,c-i)%ha); } ans=ans*(ll)an[0]%ha*(ll)an[1]%ha; printf("%d\n",ans); return 0; }
Codeforces 869 C The Intriguing Obsession