HDU 6064 RXD and numbers(BEST theorem)
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∑mj=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會把重邊看作不同的邊,而本題會看作相同的邊,所以還需要對答案除以
所以最終答案就是
總複雜度為
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: 0\n", 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: %lld\n", cas++, ans);
}
return 0;
}