Qwerty78 Trip(组合数,规律,逆元)
2 seconds
64 megabytes
standard input
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 .
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 T lines , The answer for each testcase which is the number of ways Qwerty78 can reach Dreamoon's house modulo 109 + 7.
1 2 3 1 2
1
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(组合数,规律,逆元)的更多相关文章
- 牛客网 Wannafly挑战赛11 B.白兔的式子-组合数阶乘逆元快速幂
链接:https://www.nowcoder.com/acm/contest/73/B来源:牛客网 B.白兔的式子 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 262144K, ...
- 【Gym 100947E】Qwerty78 Trip(组合数取模/费马小定理)
从(1,1)到(n,m),每次向右或向下走一步,,不能经过(x,y),求走的方案数取模.可以经过(x,y)则相当于m+n步里面选n步必须向下走,方案数为 C((m−1)+(n−1),n−1) 再考虑其 ...
- hdu5698瞬间移动-(杨辉三角+组合数+乘法逆元)
瞬间移动 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- (light oj 1102) Problem Makes Problem (组合数 + 乘法逆元)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1102 As I am fond of making easier problems, ...
- 牛客网 牛客小白月赛1 I.あなたの蛙が帰っています-卡特兰数,组合数阶乘逆元快速幂
I.あなたの蛙が帰っています 链接:https://www.nowcoder.com/acm/contest/85/I来源:牛客网 这个题有点意思,是卡特兰数,自行百度就可以.卡特兰数用处 ...
- 2018icpc南京现场赛-G Pyramid(打标找规律+逆元)
题意: 求n行三角形中等边三角形个数,图二的三角形也算. n<=1e9 思路: 打表找下规律,打表方法:把所有点扔坐标系里n^3爆搜即可 打出来为 1,5,15,35,70,126,210.. ...
- 组合数处理(逆元求解)...Orz
网上发现了不错的博客讲解... 熊猫的板子:http://blog.csdn.net/qq_32734731/article/details/51484729 组合数的预处理(费马小定理|杨辉三角|卢 ...
- 51nod 1119 组合数,逆元
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1119 1119 机器人走方格 V2 基准时间限制:1 秒 空间限制:13 ...
- hdu5967数学找规律+逆元
Detachment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
随机推荐
- 我所理解的设计模式(C++实现)——中介者模式(Mediator Pattern)
概述: 假设我们开发一个图片处理软件,里面肯定包括很多相关功能,比如说剪切,旋转,滤镜,美化等等,而我们这些功能所要处理的对象是固定的,就是我们所显示的那张图片.但是我们不能把所有的功能罗列到一个ta ...
- 在线C语言编译器/解释器
在线C语言编译器/解释器 本文介绍两个C语言在线解释器/编译器,这些工具可以提高代码片段检测方便的工作效率,并可以保证这些代码的正确性,而且还可以和别人一起编辑/分享之间的代码,这样可以共同分析代码并 ...
- hdu 3917 最大重量封闭图
/*最大重量封闭图: 意甲冠军:一些城市要建路需要负责一些公司,每家公司都需要缴纳个税.该公司将需要花费每路,另一个限制条件,如果那家公司a既定a-b.公司b既定b-c然后选择 公司a 你必须选择一个 ...
- js方法调用
<!DOCTYPE html> <html> <head> <title>测试</title> </head> <body ...
- ant学习(1)
路径:/home/framework_Study/springinAction/webRoot/WEB-INF <?xml version="1.0" encoding=&q ...
- asp Eval()函数的一些使用总结
一.函数的原型: Eval(string s); s可以是变量,表达式,语句: 二.使用: 1.s为变量时,返回该变量的值,如:string s = "sss";Eval(s);/ ...
- javascript系统时间
<div> <%--系统时间--%> 当前时间是: <script type=& ...
- MS-SQL数据库备份方法
一.手动备份 打开企业管理器 --> 右键点击需要备份的数据库 --> 所有任务 --> 备份数据库 或者: 查询分析器: use master backup database 数 ...
- REST 相关
REST 相关 REST:Representational State Transfer,表现层状态转化(出现在阮一峰的博客 理解RESTful架构 中,但是,很明显,Representational ...
- 路由转发(curl)
<?php ini_set('memory_limit', '640M'); ini_set('default_charset', 'utf-8'); define('webroot', 'ht ...