There are K pieces on the chessboard.

The size of the chessboard is N*N.

The pieces are initially placed on the top cells of the board.

A piece located on (r, c) can be moved by one cell right to (r, c + 1) or one cell down to (r+1, c).

Your task is to count how many different ways to move all pieces to the given positions at the bottom of the board.

Furthermore, the paths of the pieces mustn’t intersect each other.

InputThe first line of input contains an integer T-the number of test cases.

Each test case begins with a line containing two
integers-N(1<=N<=100000) and K(1<=K<=100) representing the
size of the chessboard and the number of pieces respectively.

The second line contains K integers: 1<=a1<a2<
…<aK<=N representing the initial positions of the pieces. That is,
the pieces are located at (1, a1), (1, a2), …, (1, aK).

Next line contains K integers: 1<=b1<b2<…<bK<=N
representing the final positions of the pieces. This means the pieces
should be moved to (N, b1), (N, b2), …, (N, bK).

OutputPrint consecutive T lines, each of which represents the number of different ways modulo 1000000007.Sample Input

1
5 2
1 2
3 4

Sample Output

50

数学问题 容斥 矩阵行列式 脑洞题

假如只有一个起点一个终点,显然是一个基本的组合数问题,从所有步数中选n-1步向下走,方案为 $ ans = C(b-a+n-1,n-1) $

如果有两个起点两个终点,则是总方案数减去路径交叉的方案数。路径相交可以理解为两人交换了目的地,所以方案为

  $ ans = C(b_1-a_1+n-1,n-1)*C(b_2-a_2+n-1,n-1) - C(b_1-a_2+n-1,n-1)*C(b_2-a_1+n-1,n-1)$

显然枚举起点和终点有几对逆序对,可以容斥计算出答案,显然这样TLE了

但这个逆序对数量决定加还是减的容斥可以让我们联想到另一个东西——矩阵行列式。

从矩阵的每一行选一列,将选的每一个位置的值乘起来,乘以(-1)^(逆序对数),即是这一部分对行列式值的贡献,所有的选法的贡献叠加起来,就是矩阵行列式的值。

那么我们只要将每个(u to v)的关系用矩阵表示,再求矩阵行列式的值就是答案了。

 /*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#define LL long long
using namespace std;
const int mxn=;
const int mod=1e9+;
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;
}
int fac[mxn],inv[mxn];
void init(){
fac[]=fac[]=;inv[]=inv[]=;
for(int i=;i<mxn;i++){
fac[i]=(LL)fac[i-]*i%mod;
inv[i]=((-mod/i*(LL)inv[mod%i])%mod+mod)%mod;
}
for(int i=;i<mxn;i++)inv[i]=(LL)inv[i]*inv[i-]%mod;
return;
}
int ksm(int a,int k){
int res=;
while(k){
if(k&)res=(LL)res*a%mod;
a=(LL)a*a%mod;
k>>=;
}
return res;
}
int f[][];
int Gauss(int n){
int ft=;
for(int i=;i<=n;i++){
if(!f[i][i]){
int p=i;
for(int j=i+;j<=n;j++)if(f[j][i]){p=j;break;}
if(p==i)return ;
for(int j=i;j<=n;i++)swap(f[i][j],f[p][j]);
ft=-ft;
}
int IV=ksm(f[i][i],mod-);
for(int j=i+;j<=n;j++){
int tmp=(LL)f[j][i]*IV%mod;
for(int k=i;k<=n;k++){
f[j][k]=((LL)f[j][k]-(LL)tmp*f[i][k])%mod;
}
}
}
int res=ft;
for(int i=;i<=n;i++){
res=(LL)res*f[i][i]%mod;
}
return (res+mod)%mod;
}
int n,K;
int a[mxn],b[mxn];
int C(int n,int m){
if(n<m)return ;
return (LL)fac[n]*inv[m]%mod*inv[n-m]%mod;
}
int path(int x,int y){
return (LL)C(b[y]-a[x]+n-,n-);
}
void solve(){
memset(f,,sizeof f);
for(int i=;i<=K;i++){
for(int j=;j<=K;j++){
f[i][j]=path(i,j);
}
}
int ans=Gauss(K);
printf("%d\n",ans);
return;
}
int main(){
// freopen("in.txt","r",stdin);
init();
int T=read(),i;
while(T--){
n=read();K=read();
for(i=;i<=K;i++)a[i]=read();
for(i=;i<=K;i++)b[i]=read();
solve();
}
return ;
}

HDU5852 Intersection is not allowed!的更多相关文章

  1. hdu5852 Intersection is not allowed! 【矩阵行列式】

    题意 给出\(n*n\)网格\((n<=10^5)\) 顶部有\(K\)个起点,底部有\(K\)个相对应的终点 每次只能向下或向右走 求有多少种从各个起点出发到达对应终点且路径不相交的路径? 对 ...

  2. HDU 5852 Intersection is not allowed!(LGV定理行列式求组合数)题解

    题意:有K个棋子在一个大小为N×N的棋盘.一开始,它们都在棋盘的顶端,它们起始的位置是 (1,a1),(1,a2),...,(1,ak) ,它们的目的地是 (n,b1),(n,b2),...,(n,b ...

  3. hdu 5852 :Intersection is not allowed! 行列式

    有K个棋子在一个大小为N×N的棋盘.一开始,它们都在棋盘的顶端,它们起始的位置是 (1,a1),(1,a2),...,(1,ak) ,它们的目的地是 (n,b1),(n,b2),...,(n,bk). ...

  4. HDU 5852 Intersection is not allowed! ( 2016多校9、不相交路径的方案、LGV定理、行列式计算 )

    题目链接 题意 : 给定方格中第一行的各个起点.再给定最后一行与起点相对应的终点.问你从这些起点出发到各自的终点.不相交的路径有多少条.移动方向只能向下或向右 分析 : 首先对于多起点和多终点的不相交 ...

  5. LGV 引理

    (其实是贺的:https://www.luogu.com.cn/paste/whl2joo4) 目录 LGV 引理 不相交路径计数 例题 Luogu6657. [模板]LGV 引理 CF348D Tu ...

  6. FJNU2018低程A 逃跑路线(Lucas + 中国剩余定理 + LGV定理)题解

    题目描述 n个人在w*h的监狱里面想要逃跑,已知他们的同伙在坐标(bi,h)接应他们,他们现在被关在(ai,1)现在他们必须要到同伙那里才有逃出去的机会,这n个人又很蠢只会从(x,y)->(x+ ...

  7. 2016 Multi-University Training Contest 9 solutions BY 金策工业综合大学

    A Poor King Tag: Reversed BFS Preprocessing is needed to calculate answers for all positions (states ...

  8. Lindström–Gessel–Viennot lemma定理 行列式板子

    https://blog.csdn.net/qq_37025443/article/details/86537261 博客 下面是wiki上的讲解,建议耐心地看一遍...虽然看了可能还是不懂 http ...

  9. IIS7.5上的REST服务的Put,Delete操作发生HTTP Error 405.0 - Method Not Allowed 解决方法

    WebDAV 是超文本传输协议 (HTTP) 的一组扩展,为 Internet 上计算机之间的编辑和文件管理提供了标准.利用这个协议用户可以通过Web进行远程的基本文件操作,如拷贝.移动.删除等.在I ...

随机推荐

  1. iOS- iPad里有趣的UIPopoverController

    效果: 1.对UIPopoverController的简单概述 1.1 UIPopoverController是在iPad开发中常用的一个组件(在iPhone上不允许使用),使用非常简单   1.2 ...

  2. TCP系列15—重传—5、Linux中RTO的计算

    之前我们介绍的都是协议中给出的RTO计算方法,下面我们看一下linux实现中RTO的计算方法.在linux中维护了srtt.mdev.mdev_max.rttvar.rtt_seq几个状态变量用来计算 ...

  3. Gitkraken系列-Gitkraken使用操作

    一个优秀的团队合作离不开git,一个优秀的程序员也离不开git.gitkraken是我在进行软工实践这门课接触到的git的UI界面的工具,它给我留下的印象就是非常好用和方便 怎么个方便法呢? 方便的安 ...

  4. C# 压缩组件介绍与入门

    1.前言 作为吉日嘎拉权限管理系统最早的一批学习版用户,学了不少东西,在群里面也结识了很多朋友,更重要的是闲余时间,大家都发布很多可靠的外包工作.这次也是由于吉日嘎拉发布了一个有关“压缩文件损坏检测” ...

  5. [剑指Offer] 48.不用加减乘除做加法

    题目描述 写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. [思路] 首先看十进制是如何做的: 5+7=12,三步走第一步:相加各位的值,不算进位,得到2.第二步:计算进 ...

  6. [计算机网络-应用层] P2P应用

    首先我们要先来区分一下下面的几种体系结构: CS:Client/Server 客户-服务器结构BS:Browser/Server  浏览器-服务器结构 P2P:Peer to Peer 对等结构 BS ...

  7. MATLAB中mat2gray的用法【转】

    函数简介 函数功能:实现图像矩阵的归一化操作.所谓"归一化"就是使矩阵的每个元素的值都在0和1之间.该函数在数字图像处理中经常用到. 调用格式: I = mat2gray(A, [ ...

  8. html的body内标签之input系列1

    1. Form的作用:提交当前的表单. 类似于去了银行提交的纸质单子,递到后台去办理相关业务. text,password只有输入的功能:button,submit只有点击的功能.想要把这些信息提交, ...

  9. [HNOI2010]合唱队 区间DP

    ---题面--- 题解: 偶然翻到这道题,,,就写了. 观察到一个数被插在哪里只受前一个数的影响,如果明确了前一个数是哪个,那么我们就可以确定大小关系,就可以知道当前这个数插在哪里,而上一个插入的数就 ...

  10. BZOJ3631:[JLOI2014]松鼠的新家——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=3631 https://www.luogu.org/problemnew/show/P3258 松鼠的 ...