题意##

给出\(n*n\)网格\((n<=10^5)\)

顶部有\(K\)个起点,底部有\(K\)个相对应的终点

每次只能向下或向右走

求有多少种从各个起点出发到达对应终点且路径不相交的路径?

对\(10^9 + 7\)取模

题解

按照组合数的套路

二维空间从一个h * w的矩形左上角走到右下角只向下或向右走的方案数为\(C_{h + w}^{h}\)

这题直接求不好求,我们考虑容斥

可以发现,如果两个路径相交,就相当于交换了终点

那么我们枚举每个起点的终点,乘上一个容斥系数\((-1)^t\),其中\(t\)表示相交的路径个数,也可以看做选择的终点序列的逆序对个数

按照每个起点择每一个终点构造出一个组合数的矩阵,按照矩阵的的定义你会发现其实答案就等于这个矩阵的行列式

我们求一个行列式就做完了

这题太神了

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 105,maxm = 200005,INF = 1000000000,P = 1000000007;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
LL fac[maxm],inv[maxm],fv[maxm];
void init(){
fac[0] = 1;
for (int i = 1; i <= 200000; i++) fac[i] = fac[i - 1] * i % P;
inv[0] = inv[1] = 1;
for (int i = 2; i <= 200000; i++) inv[i] = (P - P / i) * inv[P % i] % P;
fv[0] = 1;
for (int i = 1; i <= 200000; i++) fv[i] = fv[i - 1] * inv[i] % P;
}
LL qpow(LL a,LL b){
LL ans = 1;
for (; b; b >>= 1,a = a * a % P)
if (b & 1) ans = ans * a % P;
return ans;
}
LL C(int n,int m){
if (m > n) return 0;
return fac[n] * fv[m] % P * fv[n - m] % P;
}
int n,K,a[maxn],b[maxn];
LL A[maxn][maxn],ans;
void gause(){
ans = 1;
for (int i = 1; i <= K; i++){
int pos = i;
while (A[pos][i] == 0 && pos <= K) pos++;
if (pos > K){ans = 0; return;}
if (pos != i) for (int j = i; j <= K; j++) swap(A[i][j],A[pos][j]),ans = -ans;
for (int j = i + 1; j <= K; j++){
LL t = A[j][i] * qpow(A[i][i],P - 2) % P;
for (int k = i; k <= K; k++)
A[j][k] = (A[j][k] - A[i][k] * t % P + P) % P;
}
ans = ans * A[i][i] % P;
}
ans = (ans % P + P) % P;
}
int main(){
ios::sync_with_stdio(false);
init();
int T = read();
while (T--){
n = read(); K = read();
REP(i,K) a[i] = read();
REP(i,K) b[i] = read();
REP(i,K) REP(j,K) A[i][j] = C(n - 1 + b[j] - a[i],n - 1);
gause();
cout << ans << endl;
}
return 0;
}

hdu5852 Intersection is not allowed! 【矩阵行列式】的更多相关文章

  1. HDU5852 Intersection is not allowed!

    There are K pieces on the chessboard. The size of the chessboard is N*N. The pieces are initially pl ...

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

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

  3. 【原创】开源Math.NET基础数学类库使用(15)C#计算矩阵行列式

                   本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新  开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 上个月 ...

  4. c++实现矩阵类矩阵行列式,伴随矩阵,逆矩阵

    //Matrix ver1.0 //只支持矩阵内部(方阵)的运算 #include<iostream> #include<math.h> using namespace std ...

  5. 开源Math.NET基础数学类库使用(15)C#计算矩阵行列式

    原文:[原创]开源Math.NET基础数学类库使用(15)C#计算矩阵行列式                本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p ...

  6. BZOJ 4031 HEOI2015 小Z的房间 基尔霍夫矩阵+行列式+高斯消元 (附带行列式小结)

    原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4031 Description 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可 ...

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

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

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

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

  9. 矩阵&行列式

    # 代数 排列 对换,对于一个排列操作,对于一个偶排列一次对换之后变为奇排列 反之变为偶排列 行列式 N阶行列式室友N^2个数aij(i,j = 1,2,3,...n) 行列式的数=\(\sum_ { ...

随机推荐

  1. 监控服务端口状态python脚本

    #!/usr/bin/python import socket,os,time data={ 8080:"tomcat9", 18080:"tomcat_hjgdmj&q ...

  2. 用函数求lnx,lgx等

    https://blog.csdn.net/liujian20150808/article/details/50628061

  3. C++调用C语言编译的so文件

    参考链接:https://blog.csdn.net/chenjinlong126/article/details/78990350 一.制作so文件:libadd_c.so或libadd_cpp.s ...

  4. flowvisor连接ovs

    1 开启flowvisor $ sudo -u flowvisor fvconfig generate /etc/flowvisor/config.json $ sudo /etc/init.d/fl ...

  5. CF-1013 (2019/02/09 补)

    CF-1013 A. Piles With Stones 比较两个序列的和,因为只能拿走或者不拿,所以总数不能变大. B. And 答案只有 -1,0,1,2几种可能,所以对于每一种答案都暴力扫一次是 ...

  6. PHP 日常开发过程中的bug集合(持续更新中。。。)

    PHP 日常开发过程中的bug集合(持续更新中...) 在日常php开发过程中,会遇到一些意想不到的bug,所以想着把这些bug记录下来,以免再犯! 1.字符串 '0.00'.'0.0'.'0'  是 ...

  7. 【jquery】 选中复选框 和 return false 的影响

    $('id').attr('checked',true); return false;   如果后面接上return false 的话,复选框的钩钩不会改变,但是.is(':checked')仍然能检 ...

  8. pycharm-install scipy

    懒得装双系统,所以在win7下用pycharm,python2.7 虽然机子本身是64位,但是安装包的时候,我居然需要下载32位的??迷:) 这次装的是scipy.在pycharm里添加不了,根据网上 ...

  9. Java-basic-3-运算符-修饰符-循环

    运算符: 与C++类似,特殊的有: 1)按位右移补零操作符: 2)instanceof运算符:判断一个实例是否是某类/接口类型 如果是/类型兼容,则返回true // superclass class ...

  10. LeetCode(307) Range Sum Query - Mutable

    题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...