传送门:【ZOJ】3874 Permutation Graph
题目分析:
容易知道一个个连通块内部的标号都是连续的,否则一定会有另一个连通块向这个连通块建边,或者这个连通块向另一个连通块建边。而且从左到右左边的连通块内最大的标号小于右边连通块内最小的标号。
然后我们可以构造dp方程:
dp[n]=n!−i!∗dp[n−i]
容易发现右边存在一个卷积,由于给的素数正好是费马素数,所以可以用 NTT 来求卷积,保证精度。因为我们要求所有的dp值,因此我们要用 CDQ 分治优化。
my code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std ;
typedef long long LL ;
#define clr( a , x ) memset ( a , x , sizeof a )
#define cpy( a , x ) memcpy ( a , x , sizeof a )
const int P = 786433 ;
const int MAXN = 262144 ;
const int g = 10 ;
LL power ( int a , int b , LL res = 1 ) {
for ( LL tmp = a ; b ; b >>= 1 , tmp = tmp * tmp % P ) if ( b & 1 ) res = res * tmp % P ;
return res ;
}
void NTT ( LL y[] , int n , int rev ) {
LL inv = power ( n , P - 2 ) , wn , w , t ;
for ( int i = 1 , j , k , t ; i < n ; ++ i ) {
for ( j = 0 , k = n >> 1 , t = i ; k ; k >>= 1 , t >>= 1 ) j = j << 1 | t & 1 ;
if ( i < j ) swap ( y[i] , y[j] ) ;
}
for ( int s = 2 , ds = 1 , k , i ; s <= n ; ds = s , s <<= 1 ) {
wn = power ( g , ( P - 1 ) / s ) ;
if ( rev == -1 ) wn = power ( wn , P - 2 ) ;
for ( k = 0 ; k < n ; k += s ) {
for ( i = k , w = 1 ; i < k + ds ; ++ i , w = w * wn % P ) {
y[i + ds] = ( y[i] - ( t = w * y[i + ds] % P ) + P ) % P ;
y[i] = ( y[i] + t ) % P ;
}
}
}
if ( rev == -1 ) for ( int i = 0 ; i < n ; ++ i ) y[i] = y[i] * inv % P ;
}
LL x1[MAXN] , x2[MAXN] ;
LL f[MAXN] , dp[MAXN] ;
void cdq ( int l , int r ) {
if ( l + 1 == r ) return ;
int m = ( l + r ) >> 1 , n = 1 ;
cdq ( l , m ) ;
while ( n < r - l + 1 ) n <<= 1 ;
for ( int i = 0 ; i < m - l ; ++ i ) x1[i] = dp[i + l] ;
for ( int i = m - l ; i < n ; ++ i ) x1[i] = 0 ;
for ( int i = 0 ; i < n ; ++ i ) x2[i] = f[i] ;
NTT ( x1 , n , 1 ) ;
NTT ( x2 , n , 1 ) ;
for ( int i = 0 ; i < n ; ++ i ) x1[i] = x1[i] * x2[i] % P ;
NTT ( x1 , n , -1 ) ;
for ( int i = m ; i < r ; ++ i ) dp[i] = ( dp[i] - x1[i - l] + P ) % P ;
cdq ( m , r ) ;
}
void preprocess () {
f[0] = 1 ;
for ( int i = 1 ; i < MAXN ; ++ i ) {
f[i] = f[i - 1] * ( LL ) i % P ;
dp[i] = f[i] ;
}
cdq ( 1 , 100001 ) ;
//for ( int i = 1 ; i <= 10 ; ++ i ) printf ( "%lld\n" , dp[i] ) ;
}
void solve () {
int x , c , n ;
int ans = 1 ;
scanf ( "%*d%d" , &n ) ;
for ( int i = 1 ; i <= n ; ++ i ) {
scanf ( "%d" , &x ) ;
int minv = 0x3f3f3f3f , maxv = 0 ;
for ( int j = 0 ; j < x ; ++ j ) {
scanf ( "%d" , &c ) ;
minv = min ( c , minv ) ;
maxv = max ( c , maxv ) ;
}
if ( maxv - minv + 1 != x ) ans = 0 ;
ans = ans * dp[x] % P ;
}
printf ( "%d\n" , ans ) ;
}
int main () {
int T ;
preprocess () ;
scanf ( "%d" , &T ) ;
for ( int i = 1 ; i <= T ; ++ i ) solve () ;
return 0 ;
}