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. 1 2 3 1 2
output
  1. 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)

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

代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<algorithm>
  6. using namespace std;
  7. const int MOD = 1e9 + ;
  8. const int MAXN = 2e5 + ;
  9. typedef __int64 LL;
  10. LL fac[MAXN];
  11. void init(){
  12. fac[] = ;
  13. for(int i = ; i < MAXN; i++){
  14. fac[i] = fac[i - ] * i % MOD;
  15. }
  16. }
  17. LL quick_mul(LL a, LL n){
  18. LL ans = ;
  19. while(n){
  20. if(n & ){
  21. ans = ans * a % MOD;
  22. }
  23. n >>= ;
  24. a = a * a % MOD;
  25. }
  26. return ans;
  27. }
  28. LL C(int n, int m){
  29. return fac[n] * quick_mul(fac[m], MOD - ) % MOD * quick_mul(fac[n - m], MOD - ) % MOD;
  30. }
  31. int main(){
  32. int T, n, m, r, c;
  33. scanf("%d", &T);
  34. init();
  35. while(T--){
  36. scanf("%d%d%d%d", &n, &m, &r, &c);
  37. printf("%I64d\n", (C(n + m - , n - ) - C(n+m-r-c, n-r)*C(r+c-, r-)%MOD + MOD) % MOD);
  38. }
  39. return ;
  40. }

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. sublime怎么实现函数之间的跳转

    1.安装ctags应用程序. 到CTags的官方站点下载最新版本号,将解压后的ctags.exe放到系统环境变量的搜索路径中.通常是C:\windows\system32. 假设你想放到其它目录中,记 ...

  2. android-用xml自定义背景(可自定义显示具体那一边)

    常见的描边都是闭合的.四个边都有.如下: <?xml version="1.0" encoding="UTF-8"?> <layer-list ...

  3. 安装软件配置VC++环境时常出现的问题--Error 1935.安装程序集

    装很多软件是都要配置VC++环境的,但由于系统注册表限制,很多时候软件安装过程中会报如下错误 安装 vc++2005 运行库 Error 1935.安装程序集 Microsoft.vc80.atl,t ...

  4. IL(Intermediate Language)

    释义: IL是.NET框架中中间语言(Intermediate Language)的缩写.使用.NET框架提供的编译器可以直接将源程序编译为.exe或.dll文件,但此时编译出来的程序代码并不是CPU ...

  5. 全世界最详细的图形化VMware中linux环境下oracle安装(三)【weber出品必属精品】

    数据库软件和数据库都建好了,基本上可以说完成90%的工作,但是美中不足的就是企业管理器还没有安装好,现在我们就开始安装企业管理器吧. 安装之前我们先将补丁给补上.补丁我们也是采用禁默安装.补丁:p83 ...

  6. iOS的Bundle资源束制作

    在静态库的制作中,很多时候我们的静态库也是带着文件,图片和多媒体资源的. 若只是直接加入到项目中也是可以,但是,考虑到方便管理(方便插件使用者的管理),我们希望把插件的资源文件打成一个包来管理. 当然 ...

  7. UI基础视图----UIWebView总结

    UIWebView是UIKit框架中继承于UIView的一个常用的基础视图,和UILabel,UIImageView是兄弟类,用于展示一个网页. UIWebView是一个可以设置代理的类,在加载的不同 ...

  8. redis基础操作

    /** * redis的Java客户端Jedis测试验证 * * @author */ public class Test { /** * 非切片客户端链接 */ private Jedis jedi ...

  9. 寻找子串位置 codevs 1204

    题目描述 Description 给出字符串a和字符串b,保证b是a的一个子串,请你输出b在a中第一次出现的位置. 输入描述 Input Description 仅一行包含两个字符串a和b 输出描述  ...

  10. Codeforces Round #302 (Div. 1)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud A. Writing Code Programmers working on a ...