博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Qwerty78 Trip(组合数,规律,逆元)
阅读量:5172 次
发布时间:2019-06-13

本文共 2896 字,大约阅读时间需要 9 分钟。

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
#include
#include
#include
#include
using namespace std;const int MOD = 1e9 + 7;const int MAXN = 2e5 + 100;typedef __int64 LL;LL fac[MAXN];void init(){ fac[0] = 1; for(int i = 1; i < MAXN; i++){ fac[i] = fac[i - 1] * i % MOD; }}LL quick_mul(LL a, LL n){ LL ans = 1; while(n){ if(n & 1){ ans = ans * a % MOD; } n >>= 1; a = a * a % MOD; } return ans;}LL C(int n, int m){ return fac[n] * quick_mul(fac[m], MOD - 2) % MOD * quick_mul(fac[n - m], MOD - 2) % 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 - 2, n - 1) - C(n+m-r-c, n-r)*C(r+c-2, r-1)%MOD + MOD) % MOD); } return 0;}

 

 

 

转载于:https://www.cnblogs.com/handsomecui/p/5531654.html

你可能感兴趣的文章
C# 3.0 LINQ的准备工作
查看>>
静态代码审查工具FxCop插件开发(c#)
查看>>
创建代码仓库
查看>>
理解裸机部署过程ironic
查看>>
Django 组件-ModelForm
查看>>
zabbix 二 zabbix agent 客户端
查看>>
大数据分析中,有哪些常见的大数据分析模型?
查看>>
如何防止Arp攻击
查看>>
ClassList 标签的用法
查看>>
小细节:Java中split()中的特殊分隔符 小数点
查看>>
【编程思想】【设计模式】【行为模式Behavioral】中介者模式Mediator
查看>>
后端接口时间戳或者随机数的作用
查看>>
tomcat docBase 和 path
查看>>
java默认语法、EL、JSTL表达式,JSTL和struts Tag标签的使用总结
查看>>
Vue笔记:使用 axios 发送请求
查看>>
富文本编辑器 - RichEditor
查看>>
java webcontroller访问时报415错误
查看>>
qcow2、raw、vmdk等镜像格式
查看>>
Jzoj5455【NOIP2017提高A组冲刺11.6】拆网线
查看>>
特定字符序列的判断(1028)
查看>>