ZOJ3874 Permutation Graph
Time Limit: 2 Seconds Memory Limit: 65536 KB
Edward has a permutation {a1, a2, … an}. He finds that if he connects each pair (ai, aj) 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 u, v 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 n, m (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,1, vi,2, … vi,ci which denotes the connected component (1 ≤ ci, vi,1, vi,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的更多相关文章
- ZOJ3874 Permutation Graph 【分治NTT】
题目链接 ZOJ3874 题意简述: 在一个序列中,两点间如果有边,当且仅当两点为逆序对 给定一个序列的联通情况,求方案数对\(786433\)取模 题解 自己弄了一个晚上终于弄出来了 首先\(yy\ ...
- ZOJ3874 Permutation Graph(NTT&&cdq分治)
最近在看几道整体二分还有cdq分治的东西,突然间想起前几个礼拜的ZOJ题,然后看了一下代码,经过了一些深思熟虑之后,发现自己终于看懂了,下面就用别人的代码来剖析一下整个解题的思路吧,具体的内容我再看看 ...
- ZOJ 3874 Permutation Graph 分治NTT
Permutation Graph Time Limit: 2 Seconds Memory Limit: 65536 KB Edward has a permutation {a1, a2 ...
- ZOJ 3874 Permutation Graph ——分治 NTT
发现每一块一定是按照一定的顺序的. 然后与标号无关,并且相同大小的对答案的影响相同. 然后列出递推式,上NTT+分治就可以了. 然后就可以与输入同阶处理答案了. #include <map> ...
- ZOJ 3874 Permutation Graph (分治NTT优化DP)
题面:vjudge传送门 ZOJ传送门 题目大意:给你一个排列,如果两个数构成了逆序对,就在他们之间连一条无向边,这样很多数会构成一个联通块.现在给出联通块内点的编号,求所有可能的排列数 推来推去容易 ...
- 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 ...
- 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 ...
- codeforces 340D Bubble Sort Graph(dp,LIS)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Bubble Sort Graph Iahub recently has lea ...
- 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 ...
随机推荐
- Alpha 冲刺2
队名:日不落战队 安琪(队长) 今天完成的任务 组织第二次站立式会议. 完成40%个人信息前端界面. 明天的计划 完成剩下的60%个人信息前端界面. 还剩下的任务 个人信息修改前端界面. 遇到的困难 ...
- lintcode-162-矩阵归零
162-矩阵归零 给定一个m×n矩阵,如果一个元素是0,则将其所在行和列全部元素变成0. 需要在原矩阵上完成操作. 样例 给出一个矩阵 [ [1, 2], [0, 3] ] 返回 [ [0, 2], ...
- iOS- 移动端Socket UDP协议广播机制的实现
1.前言 什么是UDP协议广播机制? 举一个例, 例如在一群人群中,一个人要找张三,于是你向人群里大喊一声(广播):“谁是张三” 如果它是张三,它就会回应你,在网络中也是一样的. ...
- Window命令行工具操作文件
1,cd 命令用来切换目录 2,mkdir用来创建文件夹 3,rmdir用来删除空文件夹 4,创建指定类型的文件 type nul>"文件名和后缀" 5,打开指定文件用sta ...
- Jdk1.7 与 jdk1.8的区别,最新的特征有哪些(美团,360,京东面试题目)
在jdk7的新特性方面主要有下面几方面的增强: 1.1二进制变量的表示,支持将整数类型用二进制来表示,用0b开头. 所有整数int.short.long.byte都可以用二进制表示: byte aBy ...
- 第51天:封装可视区域大小函数client
一.client 可视区域 offsetWidth: width + padding + border (披着羊皮的狼) clientWidth: width + ...
- (转)Ubuntu 12.04 LTS安装VMware Tools实现linux和window 互相复制:无法找到kernel header path的问题
Ubuntu 12.04 LTS安装VMware Tools无法找到kernel header path的问题 ubuntuvmware Ubuntu 12.04 安装 VMware Tools, ...
- 【刷题】BZOJ 2539 [Ctsc2000]丘比特的烦恼
Description 随着社会的不断发展,人与人之间的感情越来越功利化.最近,爱神丘比特发现,爱情也已不再是完全纯洁的了.这使得丘比特很是苦恼,他越来越难找到合适的男女,并向他们射去丘比特之箭.于是 ...
- POJ2142:The Balance——题解
http://poj.org/problem?id=2142 题目大意:有一天平和两种数量无限的砝码(重为a和b),天平左右都可以放砝码,称质量为c的物品,要求:放置的砝码数量尽量少:当砝码数量相同时 ...
- 从零开始学Linux系统(五)用户管理和权限管理
权限管理: 常识: chmod U-所有者 g-所属组 O-其他人r-4-可读 w-2-可写 x-1-可执行 s-4-SetUID s-2-SetGID t-1-粘着位 注:目 ...