HDU-5852 Intersection is not allowed!(Lindström–Gessel–Viennot lemma+行列式)

                                     Intersection is not allowed!

                                     Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                       Total Submission(s): 475    Accepted Submission(s): 162


 

Problem Description

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.

 

 

Input

The 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
Next line contains K integers: 1<=b1

 

 

Output

Print 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*n的矩阵,初始有k个东西,在(1,x1)...(1,xk),他们要到(n,x1)...(n,xk),问不交叉的路径方案.

思路:Lindström–Gessel–Viennot引理,题目类型貌似很单一,通常都是上面有点集A,下面有点集B,然后求每个A到对应B的不相交路径总数,可能还会有一些限制.

首先考虑两个棋子的情况,即一个棋子从a1到b1,另一个棋子从a2到b2,两条路径不交叉的方案数,首先不考虑交叉方案数显然是C(b1-a1+n-1,n-1)*C(b2-a2+n-1,n-1),对于一个a1->b1,a2->b2且路径交叉的方案,如果我们把最下面一个交叉点之后的两条路径交换那么就对应了一个a1->b2,a2->b1的方案;对于一个a1->b2,a2->b1的方案,显然这两条路径必然会相交,那么我们把最后一个交叉点之后的两条路径交换就又对应一个a1->b1,a2->b2的路径交叉的方案,故我们建立了a1->b1,a2->b2交叉路径与a1->b2,a2->b1的方案的一一对应,那么不合法方案数就是C(b2-a1+n-1,n-1)*C(b1-a2+n-1,n-1) 
对于多个棋子的情况,由容斥原理,假设某些棋子路径发生了交叉,那么我们采用两个棋子的分析方法,把这些交叉的路径从最后下面一个交叉点之后交换,那么就变成了一个1~n序列的重排,我们不妨设其为c序列,表示第i个棋子从(1,ai)出发到(n,ci),那么这个排列对答案的贡献就取决于c序列的逆序对数,逆序对数为奇则做负贡献,为偶则做正贡献,那么就有 
这里写图片描述 
故问题转化为求一个n阶方阵行列式,用高斯消元O(n^3)即可解决(以上为转载)

代码:

#include
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn = 2e5+5;
const double eps = 1e-12;
const int inf = 0x3f3f3f3f;
const ll mod = 1e9+7;
map::iterator it;

int n,m;
int a[133],b[133];
ll f[maxn],inv[maxn];
ll mp[133][133];

ll quick_pow(ll x,int y)
{
	ll ans = 1;
	while(y)
	{
		if(y&1)
			ans = ans*x%mod;
		x = x*x%mod;
		y>>= 1;
	}
	return ans;
}

void init()//阶乘和逆元打表
{
	f[0] = inv[0] = 1;
	for(int i = 1;i<= 200000;i++)
	{
		f[i] = f[i-1]*i%mod;
		inv[i] = quick_pow(f[i],mod-2);
	}
}

ll C(int x,int y)
{
	if(y> x)
		return 0;
	ll ans;
	ans = (f[x]*inv[y]%mod)*inv[x-y]%mod;	
	return ans;
}

ll solve(int n)//求行列式的值
{
	ll ans=1;
    for(int k=1;k<=n;k++)
    {
        ll pos=-1;
        for(int i=k;i<=n;i++)
            if(mp[i][k])
            {
                pos=i;
                break;
            }
        if(pos==-1)return 0;
        if(pos!=k)
            for(int j=k;j<=n;j++)swap(mp[pos][j],mp[k][j]);
        ll tmp=quick_pow(mp[k][k],mod-2);
        for(int i=k+1;i<=n;i++)
            if(mp[i][k])
            {
                ans=ans*tmp%mod;
                for(int j=k+1;j<=n;j++)
                    mp[i][j]=((mp[i][j]*mp[k][k]%mod-mp[k][j]*mp[i][k]%mod)%mod+mod)%mod;
                mp[i][k]=0;
            }
    }
    
    for(int i=1;i<=n;i++)
        ans=ans*mp[i][i]%mod;
    return ans;
}

int main()
{
	init();
	int t;
	cin>>t;
	
	while(t--)
	{
		mem(mp,0);
		scanf("%d %d",&n,&m);
		for(int i = 1;i<= m;i++)
			scanf("%d",&a[i]);
		for(int i = 1;i<= m;i++)
			scanf("%d",&b[i]);
		
		for(int i = 1;i<= m;i++)
		{
			for(int j = 1;j<= m;j++)
			{
				mp[i][j] = C(n-1+b[i]-a[j],n-1);
			}
		}
		
		ll ans = solve(m);
		printf("%I64d\n",ans);
	}
	
	return 0;
}

 

你可能感兴趣的:(数论相关,高斯消元)