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}.


Author: LIN, Xi
Source: The 12th Zhejiang Provincial Collegiate Programming Contest

动态规划 分治NTT优化

随意脑补一下可以发现构成连通块的数排序后一定是连续+1递增的一串,不然它们中肯定有数连到别的地方去。

设f[i]表示大小恰好为i的连通块的方案数首先列出一个简单粗暴的方程:

 $ f[i]=\sum_{j=1}^{i} A_{j}^{j} * f[i-j] $

 $ A_{j}^{j}=j!$

复杂度$O(n^2)$

注意到右边构成了一个卷积的形式,且每个f都只由比它小的f计算得到。

于是可以愉快地用分治NTT优化DP

(前置技能:CDQ分治)

分治NTT理解了以后其实挺简单的,利用CDQ分治计算整个序列,当处理一个区间时,该区间的前一半已经被算完了,只需要把前一半的项卷积一下,答案累加到下标对应的后一半位置去,如此递归分治即可。

注意NTT的数组空间要开到(mid-l+1)的两倍。刚开始想当然觉得for(N=1,len=0;N<=m;N<<=1)等价,实际上这样做的话N会停在某个比size大,比2*size小的2的幂上

786433的原根是10  ←不知为什么记成11,调了半天

 /*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#define LL long long
using namespace std;
const int mod=;
const int G=;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
LL fac[mxn];
void init(){
fac[]=;
for(int i=;i<mxn;i++)fac[i]=(LL)fac[i-]*i%mod;
return;
}
LL ksm(LL a,LL k){
int res=;
while(k){
if(k&)res=(LL)res*a%mod;
a=(LL)a*a%mod;
k>>=;
}
return res;
}
LL a[mxn<<],b[mxn<<];
int N,len,rev[mxn<<];
void NTT(LL *a,int flag){
for(int i=;i<N;i++)if(i<rev[i])swap(a[i],a[rev[i]]);
for(int i=;i<N;i<<=){
int p=i<<;
LL gn=ksm(G,(flag==)?(mod-)/p:(mod-)-(mod-)/p);
for(int j=;j<N;j+=p){
LL g=;
for(int k=;k<i;k++,g=(LL)g*gn%mod){
LL x=a[j+k],y=g*a[j+k+i]%mod;
a[j+k]=(x+y)%mod;
a[j+k+i]=(x-y+mod)%mod;
}
}
}
if(flag==-){
LL INV=ksm(N,mod-);
for(int i=;i<N;i++)a[i]=(LL)a[i]*INV%mod;
}
return;
}
LL f[mxn];
void solve(int l,int r){
if(l==r){
f[l]=((fac[l]-f[l])%mod+mod)%mod;
//当l==r时,比l小的f全都算完了,可以得出f[l]=fac[l]-f[l]
return;
}
int i,j,mid=(l+r)>>;
solve(l,mid);//先算前一半
int m=(mid-l+)<<;//空间开两倍
for(N=,len=;N<=m;N<<=)len++;
for(i=;i<N;i++)rev[i]=(rev[i>>]>>)|((i&)<<(len-));
for(i=;i<N;i++)a[i]=b[i]=;
for(i=l;i<=mid;i++)//为了节省空间,将每一项左移l位计算
a[i-l]=f[i];
for(i=l;i<=r;i++)b[i-l]=fac[i-l];
//
NTT(a,);NTT(b,);
for(i=;i<N;i++)a[i]=(LL)a[i]*b[i]%mod;
NTT(a,-);
for(i=mid+;i<=r;i++){//将贡献累加到右半边
(f[i]+=a[i-l])%=mod;
}
solve(mid+,r);
return;
}
int n;
int num[mxn];
bool check(int x){
sort(num+,num+x+);
for(int i=;i<=x;i++){
if(num[i]!=num[i-]+)return ;
}
return ;
}
int main(){
int i,j;
init();
f[]=;
solve(,);
int T=read(),m;
while(T--){
n=read();m=read();
bool flag=;
LL ans=;
for(i=;i<=m;i++){
int sz=read();
for(j=;j<=sz;j++)num[j]=read();
if(!check(sz))flag=;
ans=(ans*f[sz])%mod;
}
if(!flag)puts("");
else printf("%lld\n",ans);
}
return ;
}

ZOJ3874 Permutation Graph的更多相关文章

  1. ZOJ3874 Permutation Graph 【分治NTT】

    题目链接 ZOJ3874 题意简述: 在一个序列中,两点间如果有边,当且仅当两点为逆序对 给定一个序列的联通情况,求方案数对\(786433\)取模 题解 自己弄了一个晚上终于弄出来了 首先\(yy\ ...

  2. ZOJ3874 Permutation Graph(NTT&&cdq分治)

    最近在看几道整体二分还有cdq分治的东西,突然间想起前几个礼拜的ZOJ题,然后看了一下代码,经过了一些深思熟虑之后,发现自己终于看懂了,下面就用别人的代码来剖析一下整个解题的思路吧,具体的内容我再看看 ...

  3. ZOJ 3874 Permutation Graph 分治NTT

    Permutation Graph Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward has a permutation {a1, a2 ...

  4. ZOJ 3874 Permutation Graph ——分治 NTT

    发现每一块一定是按照一定的顺序的. 然后与标号无关,并且相同大小的对答案的影响相同. 然后列出递推式,上NTT+分治就可以了. 然后就可以与输入同阶处理答案了. #include <map> ...

  5. ZOJ 3874 Permutation Graph (分治NTT优化DP)

    题面:vjudge传送门 ZOJ传送门 题目大意:给你一个排列,如果两个数构成了逆序对,就在他们之间连一条无向边,这样很多数会构成一个联通块.现在给出联通块内点的编号,求所有可能的排列数 推来推去容易 ...

  6. 140 - The 12th Zhejiang Provincial Collegiate Programming Contest(第二部分)

    Floor Function Time Limit: 10 Seconds      Memory Limit: 65536 KB a, b, c and d are all positive int ...

  7. Codeforces Round #198 (Div. 2) D. Bubble Sort Graph (转化为最长非降子序列)

    D. Bubble Sort Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. codeforces 340D Bubble Sort Graph(dp,LIS)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud  Bubble Sort Graph Iahub recently has lea ...

  9. 2017ICPC南宁赛区网络赛 Minimum Distance in a Star Graph (bfs)

    In this problem, we will define a graph called star graph, and the question is to find the minimum d ...

随机推荐

  1. 【IdentityServer4文档】- 贡献

    贡献 我们非常乐于接受社区贡献,但您应遵循一些指导原则,以便我们可以很方便的解决这个问题. 如何贡献? 最简单的方法是打开一个问题并开始讨论.然后,我们可以决定如何实现一个特性或一个变更.如果您即将提 ...

  2. [经典贪心算法]Prim算法

    最小生成树的Prim算法也是贪心算法的一大经典应用.Prim算法的特点是时刻维护一棵树,算法不断加边,加的过程始终是一棵树. Prim算法过程: 一条边一条边地加, 维护一棵树. 初始 E = {}空 ...

  3. 可用于jquery animate()方法的css属性

    * backgroundPosition * borderWidth * borderBottomWidth * borderLeftWidth * borderRightWidth * border ...

  4. WPF绑定到父元素的属性的方法

    应用:绑定到父元素的属性上的方法,看图.

  5. 在Ubuntu系统下编译arcsim仿真器

    首先,用tar zxvf arcsim-0.2.1.tar.gz 将软件包解压 然后,打开里面的INSTALL文件,按照里面的步骤一步一步安装库.Ubuntu13.04下 1.BLAS sudo ap ...

  6. 《转》HTTP 协议入门

    HTTP 协议是互联网的基础协议,也是网页开发的必备知识,最新版本 HTTP/2 更是让它成为技术热点. 本文介绍 HTTP 协议的历史演变和设计思路. 一.HTTP/0.9 HTTP 是基于 TCP ...

  7. P1825 [USACO11OPEN]玉米田迷宫Corn Maze

    题目描述 This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn m ...

  8. Linux(ubuntu 12.04桌面版) 搭建Android开发环境

    因为一些工作上的原因,需要切换到Linux环境下做点开发,我选择的Linux发行版本为ubuntu(我不建议使用fedora,我最开始就是使用的fedora,但发现并不是特别好使,有些插件没办法安装, ...

  9. 【BZOJ1486】最小圈(分数规划)

    [BZOJ1486]最小圈(分数规划) 题面 BZOJ 洛谷 求图中边权和除以点数最小的环 题解 分数规划 二分答案之后将边权修改为边权减去二分值 检查有无负环即可 #include<iostr ...

  10. 洛谷4577 & LOJ2521:[FJOI2018]领导集团问题——题解

    https://www.luogu.org/problemnew/show/P4577 https://loj.ac/problem/2521 参考:https://www.luogu.org/blo ...