ZOJ - 3624 Count Path Pair 组合计数

题目:4个点A(0,0),B(p,0),C(m,q),D(m,n),保证p<m,q<n,求从A走到D和从B走到C两条路径不相交的走法的种数。

思路:用总的路径数-相交的路径数

相交的路径数就是A到C的路径数*B到D的路径数,因为他们之间必有交点E,A->E->C,B->E->D

代码:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<algorithm>
#include<ctime>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<numeric>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define mm(a,b) memset(a,b,sizeof(a))
#define PP puts("*********************");
template<class T> T f_abs(T a){ return a > 0 ? a : -a; }
template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
// 0x3f3f3f3f3f3f3f3f


const LL MOD=100000007;
const int maxn=3e5+50;
LL fac[maxn],Inv[maxn];
LL pow_mod(LL a,LL b){
    LL res=1;
    while(b){
        if(b&1) res=res*a%MOD;
        a=a*a%MOD;
        b/=2;
    }
    return res;
}
LL C(int n,int m){
    return fac[n]*Inv[n-m]%MOD*Inv[m]%MOD;
}
int main(){

    int m,n,p,q;
    fac[0]=Inv[0]=1;
    for(int i=1;i<maxn;i++){
        fac[i]=fac[i-1]*(LL)i%MOD;
        Inv[i]=pow_mod(fac[i],(LL)(MOD-2));
    }
    while(~scanf("%d%d%d%d",&m,&n,&p,&q)){
        LL ans1=C(m+n,m)*C(m+q-p,q)%MOD;
        LL ans2=C(m+q,m)*C(m+n-p,n)%MOD;
        ans1=(ans1-ans2)%MOD;
        ans1=(ans1+MOD)%MOD;
        printf("%lld\n",ans1);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值