Qwerty78 Trip
time limit per test

2 seconds

memory limit per test

64 megabytes

input

standard input

output

standard output

Qwerty78 is a well known programmer (He is a member of the ICPC WF winning team in 2015, a topcoder target and one of codeforces top 10).

He wants to go to Dreamoon's house to apologize to him, after he ruined his plans in winning a Div2 contest (He participated using the handle"sorry_Dreamoon") so he came first and Dreamoon came second.

Their houses are presented on a grid of N rows and M columns. Qwerty78 house is at the cell (1, 1) and Dreamoon's house is at the cell (N, M).

If Qwerty78 is standing on a cell (r, c) he can go to the cell (r + 1, c) or to the cell (r, c + 1). Unfortunately Dreamoon expected Qwerty78 visit , so he put exactly 1 obstacle in this grid (neither in his house nor in Qwerty78's house) to challenge Qwerty78. Qwerty78 can't enter a cell which contains an obstacle.

Dreamoon sent Qwerty78 a message "In how many ways can you reach my house?". Your task is to help Qwerty78 and count the number of ways he can reach Dreamoon's house. Since the answer is too large , you are asked to calculate it modulo 109 + 7 .

Input

The first line containts a single integer T , the number of testcases.

Then T testcases are given as follows :

The first line of each testcase contains two space-separated N , M ( 2 ≤ N, M ≤ 105)

The second line of each testcase contains 2 space-separated integers OR, OC - the coordinates of the blocked cell (1 ≤ OR ≤ N) (1 ≤ OC ≤ M).

Output

Output T lines , The answer for each testcase which is the number of ways Qwerty78 can reach Dreamoon's house modulo 109 + 7.

Examples
input
1 2 3 1 2
output
1
Note

Sample testcase Explanation :

The grid has the following form:

Q*.

..D

Only one valid path:

(1,1) to (2,1) to (2,2) to (2,3).

题解:

组合数,一个矩形只能往右或者下走,中间一个格子有石头,问有多少中走法;

C(n + m - 2, n - 1) - C(n+m-r-c, n-r)*C(r+c-2, r-1)

总的减去经过格子的方法就是所要结果,但是存在取模,所以要用到逆元;

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MOD = 1e9 + ;
const int MAXN = 2e5 + ;
typedef __int64 LL;
LL fac[MAXN];
void init(){
fac[] = ;
for(int i = ; i < MAXN; i++){
fac[i] = fac[i - ] * i % MOD;
}
}
LL quick_mul(LL a, LL n){
LL ans = ;
while(n){
if(n & ){
ans = ans * a % MOD;
}
n >>= ;
a = a * a % MOD;
}
return ans;
}
LL C(int n, int m){
return fac[n] * quick_mul(fac[m], MOD - ) % MOD * quick_mul(fac[n - m], MOD - ) % MOD;
}
int main(){
int T, n, m, r, c;
scanf("%d", &T);
init();
while(T--){
scanf("%d%d%d%d", &n, &m, &r, &c);
printf("%I64d\n", (C(n + m - , n - ) - C(n+m-r-c, n-r)*C(r+c-, r-)%MOD + MOD) % MOD);
}
return ;
}

Qwerty78 Trip(组合数,规律,逆元)的更多相关文章

  1. 牛客网 Wannafly挑战赛11 B.白兔的式子-组合数阶乘逆元快速幂

    链接:https://www.nowcoder.com/acm/contest/73/B来源:牛客网 B.白兔的式子   时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 262144K, ...

  2. 【Gym 100947E】Qwerty78 Trip(组合数取模/费马小定理)

    从(1,1)到(n,m),每次向右或向下走一步,,不能经过(x,y),求走的方案数取模.可以经过(x,y)则相当于m+n步里面选n步必须向下走,方案数为 C((m−1)+(n−1),n−1) 再考虑其 ...

  3. hdu5698瞬间移动-(杨辉三角+组合数+乘法逆元)

    瞬间移动 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  4. (light oj 1102) Problem Makes Problem (组合数 + 乘法逆元)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1102 As I am fond of making easier problems, ...

  5. 牛客网 牛客小白月赛1 I.あなたの蛙が帰っています-卡特兰数,组合数阶乘逆元快速幂

    I.あなたの蛙が帰っています   链接:https://www.nowcoder.com/acm/contest/85/I来源:牛客网     这个题有点意思,是卡特兰数,自行百度就可以.卡特兰数用处 ...

  6. 2018icpc南京现场赛-G Pyramid(打标找规律+逆元)

    题意: 求n行三角形中等边三角形个数,图二的三角形也算. n<=1e9 思路: 打表找下规律,打表方法:把所有点扔坐标系里n^3爆搜即可 打出来为 1,5,15,35,70,126,210.. ...

  7. 组合数处理(逆元求解)...Orz

    网上发现了不错的博客讲解... 熊猫的板子:http://blog.csdn.net/qq_32734731/article/details/51484729 组合数的预处理(费马小定理|杨辉三角|卢 ...

  8. 51nod 1119 组合数,逆元

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1119 1119 机器人走方格 V2 基准时间限制:1 秒 空间限制:13 ...

  9. hdu5967数学找规律+逆元

    Detachment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

随机推荐

  1. CFGYM 2013-2014 CT S01E03 D题 费用流模版题

    题意: n行, a房间的气球,b房间的气球 i行需要的气球,与a房的距离,b房的距离 求最小距离 #include <stdio.h> #include <string.h> ...

  2. Zedboard甲诊opencv图像处理(四)

    接着上一篇博客,继续改进,现在为了是图像处理结果更加稳定,我实在没有办法了,只好先提取手指,再提取指甲. 把手指从背景里面提出来还是挺简单的,可惜的是我研究这么半天还是这结果,好沮丧. 怎么办呢,时间 ...

  3. maven pom.xml具体解释(整理)

    pom作为项目对象模型. 通过xml表示maven项目,使用pom.xml来实现.主要描写叙述了项目:包含配置文件.开发人员须要遵循的规则,缺陷管理系统.组织和licenses,项目的url,项目的依 ...

  4. 高性能MySql进化论【转】

    高性能MySql进化论(十二):Mysql中分区表的使用总结 http://binary.duapp.com/category/sql 当数据量非常大时(表的容量到达GB或者是TB),如果仍然采用索引 ...

  5. [Redux] Redux: Extracting Container Components -- AddTodo

    Code to be refactored: const AddTodo = ({ onAddClick }) => { let input; return ( <div> < ...

  6. FLEX中Tree默认展开全部节点

    这里分两种情况,一种是数据源在MXML文件里,如: <mx:XML id="treeXML" format="e4x"> <root> ...

  7. Unity 对象池的使用

    在游戏开发过程中,我们经常会遇到游戏发布后,测试时玩着玩着明显的感觉到有卡顿现象.出现这种现象的有两个原因:一是游戏优化的不够好或者游戏逻辑本身设计的就有问题,二是手机硬件不行.好吧,对于作为程序员的 ...

  8. C# 创建验证码图片

    using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; us ...

  9. mysql的锁--行锁,表锁,乐观锁,悲观锁

    一 引言--为什么mysql提供了锁 最近看到了mysql有行锁和表锁两个概念,越想越疑惑.为什么mysql要提供锁机制,而且这种机制不是一个摆设,还有很多人在用.在现代数据库里几乎有事务机制,aci ...

  10. POJ 1001 Exponentiation 模拟小数幂

    模拟小数幂 小数点位 pos 非零末位 e 长度 len 只有三种情况 pos > len pos < e e < pos < len #include <iostrea ...