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. [codecademy]html&css

    1. HTML is the language used to create the web pages you visit everyday. It provides a logical way t ...

  2. 第三章——供机器读取的数据(XML)

    本书使用的文件.代码:https://github.com/huangtao36/data_wrangling 机器可读(machine readable)文件格式: 1.逗号分隔值(Comma-Se ...

  3. Swift-函数的理解

    /* 函数(Function) 函数是为执行特定功能的自包含的代码块.函数需要给定一个特定标识符(名字),然后当需要的时候, 就调用此函数来执行功能. */ // 函数的定义与调用 // 定义函数时, ...

  4. 3dContactPointAnnotationTool开发日志(三)

      今天的目的是把obj文件导到场景里.具体将制定路径的obj文件导进去我用的是这个方法.导进去后呈现的是一个黑色的影子.   导入后还想实现一下缩放功能,请看这个方法.缩放实现起来也很简单.   光 ...

  5. WebKit 源码分析 -- loader

    原文地址: http://peirenlei.iteye.com/blog/1718569 摘要:本文介绍 WebCore 中 Loader 模块是如何加载资源的,分主资源和派生资源分析 loader ...

  6. 发生dev_queue_xmit的时候,全部都是从ip_finish_output中来的吗

    也就是说啊,内核中的收发包的路径,很可能是经理driver_recv --> tcp -->driver_send这样一个过程,是个很长的路径呢...... 从dev_queue_xmit ...

  7. poj1065-Wooden Sticks

    题目 有很多小木棍需要机器处理.每个小木棍有重量和长度两个属性.不断把小木棍放入机器中,如果小木棍\(a\)放完后放入小木棍\(b\),那么如果\(a.weight<=b.weight\ and ...

  8. Go语言【第二篇】:Go语法和数据类型

    Go语言基础语法 Go标记 Go程序可以由多个标记组成,可以是关键字,标识符,常量,字符串,符号.如以下Go语句由6个标记组成: fmt.PrintIn("Hello, World!&quo ...

  9. CentOS expr和let

    1.expr,用于计算变量等 用法:expr 表达式 用例1: #运算符号和参数之间要有空格分开:[es@bigdata-senior01 ~]$ expr 2 + 3 5 #乘号(*)需要用 \ , ...

  10. wmware的vmnet0、vmnet1、vmnet8

    用vmware安装虚拟机后会出现三种网卡: 1.vmnet0:桥接网卡,虚拟机相当于一台实体机,可以自用访问与被访问及上网. 在桥接模式下,VMware虚拟出来的操作系统就像是局域网中的一独立的主机, ...