首页 > 编程知识 正文

HDU 6064 RXD and numbersBEST theorem

时间:2023-05-03 21:00:14 阅读:223042 作者:1605

RXD and numbers

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 40 Accepted Submission(s): 16

Problem Description
RXD has a sequence A1,A2,A3,…An, which possesses the following properties:
1≤Ai≤m
A1=An=1
for all 1≤x≤m, there is at least one position p where Ap=x.
for all x,y, the number of i(1 ≤ i < n) which satisfies Ai=x and Ai+1=y is Dx,y.
One day, naughty boy DXR clear the sequence.
RXD wants to know, how many valid sequences are there.
Output the answer module 998244353.
0≤Di,j<500,1≤m≤400
n≥2

Input
There are several test cases, please keep reading until EOF.
There are about 10 test cases, but only 1 of them satisfies m>50
For each test case, the first line consists of 1 integer m, which means the range of the numbers in sequence.
For the next m lines, in the i-th line, it consists of m integers, the j-th integer means Di,j.
We can easily conclude that n=1+∑mi=1∑gxddb=1Di,j.

Output
For each test case, output “Case #x: y”, which means the test case number and the answer.

Sample Input
2
1 2
2 1
4
1 0 0 2
0 3 0 1
2 1 0 0
0 0 3 1
4
0 1 0 0
1 0 0 0
0 0 0 1
0 0 1 0

Sample Output
Case #1: 6
Case #2: 18
Case #3: 0

Source
2017 Multi-University Training Contest - Team 3

Recommend
liuyiding

题目大意:

 给出一个m个节点的有向图中,每种起点终点边的条数,求有多少条从1号节点起始的欧拉回路。

解题思路:

 由于是要求有向图的欧拉回路数,很自然想到BEST theorem解决。
 BEST theorem的介绍引用wiki:

 这里要用到matrix tree的有向图版本,表达能力有限(:з」∠),同样引用wiki:

 首先利用BEST theorm求得的欧拉回路数是不定起点的,这里固定起点为1,那么就需要把方案数乘上deg(1),表示同一条欧拉回路,在这里起点不同算作不同的欧拉回路。由于BEST theorm会把重边看作不同的边,而本题会看作相同的边,所以还需要对答案除以 ∏mi=1∏gxddb=1(Di,j!)
 所以最终答案就是 tw(G)∗(deg(1)!)∗∏mi=2(deg(i)−1)!/∏mi=1∏gxddb=1(Di,j)!
 总复杂度为 O(m3) 。
 

AC代码 #include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <ctime>#include <vector>#include <queue>#include <stack>#include <deque>#include <string>#include <map>#include <set>#include <list>using namespace std;#define INF 0x3f3f3f3f#define LL long long#define fi first#define se second#define mem(a,b) memset((a),(b),sizeof(a))const LL MOD=998244353;const int MAXV=400+1;int V;LL D[MAXV][MAXV];//从i,到j的边的数目LL in[MAXV],out[MAXV];//每个结点的入度,出度struct Matrix{ LL a[MAXV][MAXV]; Matrix() { memset(a,0,sizeof(a)); } LL det(int n)//求前n行n列的行列式的值 { for(int i=0;i<n;++i) for(int j=0;j<n;++j) a[i][j]=(a[i][j]%MOD+MOD)%MOD; LL ret=1; for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) while(a[j][i]) { LL t=a[i][i]/a[j][i]; for(int k=i;k<n;++k) a[i][k]=((a[i][k]-a[j][k]*t)%MOD+MOD)%MOD; for(int k=i;k<n;++k) swap(a[i][k],a[j][k]); ret=-ret; } if(!a[i][i]) return 0; ret=ret*a[i][i]%MOD; } ret=(ret%MOD+MOD)%MOD; return ret; }};LL get_fac(LL x)//计算阶乘{ LL res=1; for(LL i=2;i<=x;++i) res=(res*i)%MOD; return res;}LL exgcd(LL a, LL b, LL &x, LL &y){ LL d=a; if(b) { d=exgcd(b, a%b, y, x); y-=(a/b)*x; } else { x=1; y=0; } return d;}LL inv(LL a)//计算逆元{ LL x, y; exgcd(a, MOD, x, y); return (MOD+x%MOD)%MOD;}void init()//初始化{ for(int i=0;i<=V;++i) in[i]=out[i]=0;}int main(){ int cas=1; while(~scanf("%d",&V)) { init(); Matrix mat; for(int i=0;i<V;++i) for(int j=0;j<V;++j) { scanf("%lld", &D[i][j]); mat.a[i][j]-=D[i][j]; mat.a[j][j]+=D[i][j]; in[j]+=D[i][j]; out[i]+=D[i][j]; } //如果存在点入度不等于出度,则不存在欧拉回路直接输出0 bool ok=true; for(int i=0;i<V;++i) if(in[i]!=out[i]) { ok=false; break; } if(!ok) { printf("Case #%d: 0n", cas++); continue; } //把根节点移到最后,方便去掉它求行列式 for(int i=0;i<V;++i) swap(mat.a[0][i], mat.a[V-1][i]); for(int i=0;i<V;++i) swap(mat.a[i][0], mat.a[i][V-1]); LL ans=mat.det(V-1); for(int i=0;i<V;++i) ans=(ans*get_fac(in[i]-(i!=0)))%MOD; for(int i=0;i<V;++i) for(int j=0;j<V;++j) ans=(ans*inv(get_fac(D[i][j])))%MOD; printf("Case #%d: %lldn", cas++, ans); } return 0;}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。