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 ...
随机推荐
- Redhat6.4 配置本地网络的FTP YUM源
Redhat6.4 配置本地网络的FTP YUM源 如果本机IP: 192.168.8.47 (一) 配置本机的yum源 使用以下的方法能够配置本机的yum源: 1) scp命令上传ISO文件到: / ...
- Java 反射 方法调用
在使用Java 反射时,对方法的调用,可能碰到最多的问题是,方法的变量如何使用.其实,调用方法的变量全部在参数数组里,不管有多少个参数,你都要把它放在参数数组里,如果是单个非数组参数,则可不使用参数数 ...
- js计算日期天数差-2013-9-26
function daymath(sdate, edate) { var startdate = sdate; var enddate = edate; ...
- DEV SIT UAT
DEV环境:DEV顾名思义就是develop,即代码开发的环境.SIT环境:System Integration Test系统集成测试,开发人员自己测试流程是否走通.UAT环境:User Accept ...
- 如何让div横向排列
方法一: 一般情况,默认的div是写一个换一行,那么如何定义两个div横向排列而不换行呢? div默认的display属性是block.所以每一个div都是新的一行,现在把display换成inlin ...
- 修改EF的默认约定模型的方式
EF默认使用约定的模型,但是有时类不遵从约定,我们需要能够执行进一步的配置.对此有两种方法:数据注释和Fluent API 注释 class User { [Key] public int Key{ ...
- C#中Property和Attribute的区别
C#中Property和Attribute的区别 Attribute 字段Property 属性(get;set;) 属性的正常写: private string name; public strin ...
- 使用java对sql server进行增删改查
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...
- 一个支持实时预览的在线 Markdown 编辑器 - Markdoc
最近组内需要为一些项目和系统写文档,发表在公司内的文档平台上,这个平台并不支持markdown,所以打算做一个在线markdown编辑器,支持实时预览,并且可以很容易的迁移发表到公司文档平台上,所以就 ...
- 递归:汉诺塔 - 零基础入门学习Python024
递归:汉诺塔 让编程改变世界 Change the world by program 似乎谈到递归算法就要拿汉诺塔来举例,没办法,因为小甲鱼小时候太笨了,这个游戏老是玩不过关,好不容易在自学编程的时候 ...