Count Path Pair


Time Limit: 3 Seconds      Memory Limit: 65536 KB

You are given four positive integers m,n,p,q(p < m and q < n). There are four points A(0,0),B(p,0),C(m,q),D(m,n). Consider the path f from A to D and path g from B to Cf and g are always towards and parallel to the positive direction of one axis, and they can only change their direction on integer points(whose coordinates are both integers).

You are asked to count the number(mod 100000007) of pair (f,g) that and g have no intersection.

Input

There are multiple cases(less than 100). Each case is a line containing four integers m,n,p,q(m ≤ 100000 and n ≤ 100000).

Output

For each case, output a single line containing the right answer.

Sample Input

2 2 1 1
3 2 1 1

Sample Output

3
6

题目大意:从A到D与从B到C的路径中不相交的有多少条。

思路:

这道题比较恶心,读题目大约找了2位大佬读,看了大约有1个多小时才搞懂的题意。(ORZ)

唉。这道题我们要求不相交的条数,不过不想交直接求太复杂,所以转化为补集思想:不想交=所有-相交,我们求出相交的,然后用总条数减去相交的条数就好了!

但是问题来了,我们要怎样求总条数与相交的条数呢?!

总路径数是C(M+N,M)*C(Q+M-P,Q)。

然后相交的地方肯定是B与C构成的矩形当中。

我们可以考虑A到C以及B到D,他们的路径必定有相交的,而且相交的位置必定也在矩形当中。

而且相交之后你可以考虑成从A到C的路径改为从交点到D,就样就完成了转化。

结果便是C(M+N,M)*C(Q+M-P,Q)-C(N+M-P,N)*C(M+Q,M);(为什么,背公式!)

然而,这还不够,因为这道题让着取模啊!!!(靠,变态的题目、、、、、、)

没办法,还是要搞一搞的,据学长说在遇到排列组合取模的问题的时候可以求乘法逆元(模数为质数)(还记不记得我们之前在做乘法逆元的题的时候遇到过这样的题,这里粘一下那道题:http://www.cnblogs.com/z360/p/7327322.html)我们这里上公式:

(a/b)mod p=a*c mod p = (a mod p * c mod p)mod p(定义 c为b在mod p意义下的逆元)

这个地方好像可以用卢卡斯定理做、、、、、(然而蒟蒻并不会,做完这道题再学吧、、、、、、、(⊙o⊙)…)

好了,好像这样就差不多了。(然而蒟蒻表示做了一天啊!~~~~(>_<)~~~~)

mdzz 取模的地方写错了,硬是找了一天、、、、、

代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 1000005
#define mod 100000007
#define ll long long
using namespace std;
ll ans,m,n,q,p,jie[N];
ll read()
{
    ll x=,f=; char ch=getchar();
    ; ch=getchar();}
    +ch-'; ch=getchar();}
    return x*f;
}
ll ins(ll n)
{
    jie[]=;
    ;i<n;i++)
     jie[i]=(jie[i-]*i)%mod;
}
ll exgcd(ll a,ll b,ll &x,ll &y)
{
    )
    {
        x=,y=;
        return a;
    }
    ll r=exgcd(b,a%b,x,y),tmp;
    tmp=x,x=y,y=tmp-a/b*y;
    return r;
}
ll c(ll a,ll b)
{
    ll sum;
    ll x,y;b=jie[b]%mod*jie[a-b]%mod;
    sum=jie[a];
    exgcd(b,mod,x,y);
    x=(mod+x%mod)%mod;
    sum=(sum%mod*x%mod)%mod;
    return sum;
}
int main()
{
    ins(N);
    while(~scanf("%lld",&m))
    {
        n=read(),p=read(),q=read();
        ans=((c(m+n,m)%mod*c((q+m-p),q)%mod)%mod-(c((n+m-p),n)%mod*c((m+q),m)%mod)+mod)%mod;
        printf("%lld\n",ans);
    }
    ;
}

zoj——3624 Count Path Pair的更多相关文章

  1. ZOJ 3624 Count Path Pair 排列组合

    思路:在没有限制条件时,很容易知道结果为C(m+n,n)*C(m+q-p,q). 然后再把相交的情况去除就可以了.而如果想到了就是水题了…… 求A->D,B->C相交的情况可以转化为求A- ...

  2. ZOJ 1610.Count the Colors-线段树(区间染色、区间更新、单点查询)-有点小坑(染色片段)

    ZOJ Problem Set - 1610 Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting s ...

  3. zoj 1610 Count the Colors

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=610  Count the Colors Time Limit:2000MS   ...

  4. ZOJ 1610——Count the Colors——————【线段树区间替换、求不同颜色区间段数】

    Count the Colors Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Subm ...

  5. zoj 1610 Count the Colors 【区间覆盖 求染色段】

    Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on ...

  6. ZOJ 1610 Count the Colors (线段树区间更新与统计)

    Painting some colored segments on a line, some previously painted segments may be covered by some th ...

  7. ZOJ 1610 Count the Colors (线段树区间更新)

    题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...

  8. zoj 1610 Count the Colors(线段树延迟更新)

    所谓的懒操作模板题. 学好acm,英语很重要.做题的时候看不明白题目的意思,我还拉着队友一块儿帮忙分析题意.最后确定了是线段树延迟更新果题.我就欣欣然上手敲了出来. 然后是漫长的段错误.... 第一次 ...

  9. ZOJ - 1610 Count the Colors(线段树区间更新)

    https://cn.vjudge.net/problem/ZOJ-1610 题意 给一个n,代表n次操作,接下来每次操作表示把[l,r]区间的线段涂成k的颜色其中,l,r,k的范围都是0到8000. ...

随机推荐

  1. KMP POJ 2406 Power Strings

    题目传送门 /* 题意:一个串有字串重复n次产生,求最大的n KMP:nex[]的性质应用,感觉对nex加深了理解 */ /************************************** ...

  2. A Python example for HiveServer2

    要做一个通过调用python来实现对hive server2 的连接.在网上搜索了很多资料,有些说的hive sever的,但是由于认证方式发生改变,行不通. 最后,找到了权威的说明(PS: 还是应该 ...

  3. struct结构的一些内容

    srtuct结构的定义: 访问修饰符 struct  结构名{ //方法体 } 结构定义的特点: 1.结构中可以有字段(属性),也可以有方法 2.定义时,结构的字段不能被赋初值 3.结构和类一样都有默 ...

  4. Python(1)-第一天

    PTVS下载地址:https://pytools.codeplex.com/releases/view/109707 Python下载地址:https://www.python.org/downloa ...

  5. js实现点击上下按钮,图片向上向下循环滚动切换

    //popup.js //jquery.1.4.2-min.js (function(p,j){function u(){if(!c.isReady){try{v.documentElement.do ...

  6. 前端面试题HTML

    浏览器页面有哪三层构成,分别是什么,作用是什么?

  7. python __slots__ 详解(上篇)

    转自:http://blog.csdn.net/sxingming/article/details/52892640 python中的new-style class要求继承Python中的一个内建类型 ...

  8. Python 将中文转拼音

    文字转拼音 import os.path class PinYin(object): def __init__(self): self.word_dict = {} def load_word(sel ...

  9. 【alert(1) to win】不完全攻略

    alert(1) to win 一个练习XSS的平台,地址:https://alf.nu/alert1 Warmup 给出了一段JavaScript代码 function escape(s) { re ...

  10. java protostuff 序列化反序列化工具

    protostuff是由谷歌开发的一个非常优秀的序列化反序列化工具 maven导入包: <dependency> <groupId>io.protostuff</grou ...