Permutation Graph


Time Limit: 2 Seconds      Memory Limit: 65536 KB

Edward has a permutation {a1a2, … an}. He finds that if he connects each pair (aiaj) such that i < j and ai > aj, he will get a graph.

For example, if the permutation is {2, 3, 1, 4}, then 1 and 2 are connected and 1 and 3 are connected.

Edward lost his permutation, but he does know the connected components of the corresponding graph. He wants to know how many permutations will result in the same connected components.

Note that two vertices uv belong to the same connected component if there exists a sequence of vertices starting with u and ending with v such that every two subsequent vertices in the sequence are connected by an edge.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains two integers nm (1 ≤ m ≤ n ≤ 100000), indicating the length of the permutation and the number of connected components in the graph.

Each of the following m lines contains an integer ci which denotes the size of i-th connected component, followed by ci distinct integers vi,1vi,2, … vi,ci which denotes the connected component (1 ≤ civi,1vi,2, … vi,ci ≤ n).

It is guaranteed that every number will appear in exact one connected component and c1 + c2 + … + cm = n.

Output

For each case, output the answer modulo 786433.

Sample Input

2
4 4
1 1
1 2
1 3
1 4
4 2
3 1 2 3
1 4

Sample Output

1
3

Hint

For the second case, the three permutations is: {2, 3, 1, 4}, {3, 2, 1, 4}, {3, 1, 2, 4}.

题解:

  一个联通块的点必须是连续的

  构造一个dp方程,令dp[i] 表示 i 个连续的点,能形成联通块的 方案数

  那么 : dp[i] = n! - i*dp[n - i]    这里 i 取遍1~n-1

  发现 i * dp[n-i], 就是卷积,取的模又是 费马素数, 那就NTT求解了

  还要用cdq分治优化下

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 1e6+, M = 1e3+,inf = 2e9,mod = ; const LL G = , P = ; LL mul(LL x,LL y){
return (x*y-(LL)(x/(long double)P*y+1e-)*P+P)%P;
}
LL qpow(LL x,LL k,LL p){
LL ret=;
while(k){
if(k&) ret=mul(ret,x);
k>>=;
x=mul(x,x);
}
return ret;
}
LL wn[];
void getwn(){
for(int i=; i<=; ++i){
int t=<<i;
wn[i]=qpow(G,(P-)/t,P);
}
}int len;
void NTT_init() {
getwn();
} void NTT(LL y[],int op){
for(int i=,j=len>>,k; i<len-; ++i){
if(i<j) swap(y[i],y[j]);
k=len>>;
while(j>=k){
j-=k;
k>>=;
}
if(j<k) j+=k;
}
int id=;
for(int h=; h<=len; h<<=) {
++id;
for(int i=; i<len; i+=h){
LL w=;
for(int j=i; j<i+(h>>); ++j){
LL u=y[j],t=mul(y[j+h/],w);
y[j]=u+t;
if(y[j]>=P) y[j]-=P;
y[j+h/]=u-t+P;
if(y[j+h/]>=P) y[j+h/]-=P;
w=mul(w,wn[id]);
}
}
}
if(op==-){
for(int i=; i<len/; ++i) swap(y[i],y[len-i]);
LL inv=qpow(len,P-,P);
for(int i=; i<len; ++i) y[i]=mul(y[i],inv);
}
} int T,n,m;
LL y[N],yy[N],dp[N],f[N];
void cdq(int ll,int rr) {
if(ll == rr) return ;
cdq(ll,mid);
len = ;
while(len <= rr-ll+) len<<=;
for(int i = ; i < mid-ll+; ++i) y[i] = dp[ll+i];
for(int i = mid-ll+; i < len; ++i) y[i] = ;
for(int i = ; i < len; ++i) yy[i] = f[i+];
NTT(y,),NTT(yy,);
for(int i = ; i < len; ++i) y[i] = (y[i] * yy[i])%P;
NTT(y,-);
for(int i = mid; i < rr; ++i)
dp[i+] = ((dp[i+] - y[i - ll])%mod + mod) % mod;
cdq(mid+,rr);
}
int main() {
NTT_init();
f[] = ;
for(int i = ; i <= ; ++i) {
f[i] = 1LL* f[i-] * i % mod;
dp[i] = f[i];
}
cdq(,);
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&m);
int ans = ;
for(int i = ; i <= m; ++i) {
int x,y,mi = inf,mx = ;
scanf("%d",&x);
ans = (ans * dp[x]) % mod;
for(int j = ; j <= x; ++j) {
scanf("%d",&y);
mx = max(mx,y);
mi = min(mi,y);
}
if(mx - mi + != x) ans = ;
}
printf("%d\n",ans);
}
return ;
}

最新文章

  1. MySQL 配置
  2. Thrift架构~thrift中间语言的认识(只有它什么都不是,它才有可能什么都是)
  3. LoadRunner 常用C语言函数使用
  4. [杂谈] There is a Doctor in My Computer.
  5. clearfix清除浮动进化史
  6. Qt——QLineEdit使用总结
  7. div下的img标签中图片的大小设定
  8. Spring依赖注入 --- 模拟实现
  9. 示例:Servlet读取文件内容并在页面打印输出
  10. Flink Program Guide (3) -- Event Time (DataStream API编程指导 -- For Java)
  11. 201521123013 《Java程序设计》第6周学习总结
  12. NIO 多线程
  13. Sublime text 3 注册码激活码 版本号3143
  14. java.lang.IllegalStateException: Invalid use of BasicClientConnManager: connection still allocated.
  15. Scrapy 框架 配置文件
  16. qhfl-5 redis 简单操作
  17. (转)Springboot邮件服务
  18. CrackMe破解系列第一番Acid_burn
  19. Centos 7 系统安装(简单步骤)
  20. C do whlie 数数位

热门文章

  1. Caffe 不同版本之间layer移植方法
  2. System.TypeInitializationException
  3. SG博弈函数模板
  4. 算法复习——数位dp(不要62HUD2089)
  5. BZOJ3295 动态逆序对(树状数组套线段树)
  6. cf660E Different Subsets For All Tuples
  7. 【HDOJ5979】Convex(三角函数)
  8. 标准C程序设计七---07
  9. js-判断当前页面是否在微信浏览器中打开
  10. Executors