E - Qwerty78 Trip

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

standard input/output 
Announcement

 
  • Statements

    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.

Sample Input

Input
  1. 1
    2 3
    1 2
Output
  1. 1

Hint

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).

题意,给你一个矩阵,并且里面有且只有一个障碍格(x,y),求从(1,1)走到(n,m) 的方案数

思路:首先没有障碍格的方案数是C(n+m-2,m-1),有一个障碍格的方案数就是总数-经过障碍的方案数。经过障碍格的方案数应该是:走到障碍格的方法数×从障碍格走到终点的方案数, 那么就是C(n+m-2,m-1) - C(x+y-2,y-1) × C(n-x+1+m-y+1-2, m-y+1-1); 求组合数取模,可以根据C(n,m) = n! / ((n-m)! × m!)  由于涉及到除法,取余的时候要预处理出所有阶乘的逆元。可以根据快速密来求逆元,复杂度为nlog,也可以直接递推出所有的阶乘逆元,复杂度为On

`

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5. using namespace std;
  6. const int N = 2e5 + ;
  7. const int MOD = 1e9 + ;
  8. typedef long long ll;
  9. ll fac[N], afac[N];
  10. ll powm(ll a, ll b) {
  11. ll ans = ;
  12. a = a % MOD;
  13. while(b) {
  14. if(b & ) ans = ans * a % MOD;
  15. a = a * a % MOD;
  16. b >>= ;
  17. }
  18. return ans;
  19. }
  20. void pre() {
  21. fac[] = ;
  22. for(int i = ; i < N; ++i) fac[i] = fac[i - ] * (ll)i % MOD;
  23. afac[N - ] = powm(fac[N - ], MOD - );
  24. for(int i = N - ; i >= ; --i) afac[i - ] = afac[i] * i % MOD;
  25. }
  26. ll get(int x, int y) {
  27. ll ans = ;
  28. ans = ((fac[x] * afac[x - y]) % MOD * afac[y]) % MOD;
  29. return ans;
  30. }
  31. int main() {
  32. int _; scanf("%d", &_);
  33. pre();
  34. while(_ --) {
  35. ll n, m, x, y;
  36. scanf("%I64d%I64d%I64d%I64d", &n, &m, &x, &y);
  37. ll res1 = get(n + m - , m - );
  38. ll res2 = get(x + y - , y - );
  39. ll res3 = get(n + m - x - y, m - y);
  40. printf("%I64d\n", (res1 + MOD - res2 * res3 % MOD) % MOD);
  41. }
  42. return ;
  43. }
codeforces 559c- Gerald and Giant Chess

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Giant chess is quite common in Geraldion. We will not delve into the rules of the game, we'll just say that the game takes place on an h × w field, and it is painted in two colors, but not like in chess. Almost all cells of the field are white and only some of them are black. Currently Gerald is finishing a game of giant chess against his friend Pollard. Gerald has almost won, and the only thing he needs to win is to bring the pawn from the upper left corner of the board, where it is now standing, to the lower right corner. Gerald is so confident of victory that he became interested, in how many ways can he win?

The pawn, which Gerald has got left can go in two ways: one cell down or one cell to the right. In addition, it can not go to the black cells, otherwise the Gerald still loses. There are no other pawns or pieces left on the field, so that, according to the rules of giant chess Gerald moves his pawn until the game is over, and Pollard is just watching this process.

Input

The first line of the input contains three integers: h, w, n — the sides of the board and the number of black cells (1 ≤ h, w ≤ 105, 1 ≤ n ≤ 2000).

Next n lines contain the description of black cells. The i-th of these lines contains numbers ri, ci (1 ≤ ri ≤ h, 1 ≤ ci ≤ w) — the number of the row and column of the i-th cell.

It is guaranteed that the upper left and lower right cell are white and all cells in the description are distinct.

Output

Print a single line — the remainder of the number of ways to move Gerald's pawn from the upper left to the lower right corner modulo109 + 7.

Sample Input

Input
  1. 3 4 2
    2 2
    2 3
Output
  1. 2
Input
  1. 100 100 3
    15 16
    16 15
    99 88
Output
  1. 545732279
  2.  
  3. 题意:这题是上面的复杂版,有n个障碍格,n《= 2000
    思路:我们设dp[i]表示到达第i个障碍格,而不经过(1,1)到(xiyi)中其他障碍格的方案数,那么有dp[i] = C(xi+yi-2,yi-1) - sigma(dp[j] * C(xi-xj+yi-yj, yi-yj))
    其中,xj <= xi && yj <= yi 我们把第 (n,m)也看做是第n+1格障碍格,那么dp[n+1] 就是答案
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int N = ;
  5. const int X = 1e5 + ;
  6. const int M = 2e5 + ;
  7. const int MOD = 1e9 + ;
  8. int h, w, n;
  9. ll fac[M], afac[M], bc[N], dp[N];
  10.  
  11. ll pow_mod(ll a, ll b) {
  12. ll ans = ;
  13. a %= MOD;
  14. while(b) {
  15. if(b & ) ans = (ans * a) % MOD;
  16. a = (a * a) % MOD;
  17. b >>= ;
  18. }
  19. return ans;
  20. }
  21. void pre() {
  22. fac[] = ;
  23. for(int i = ; i < M; ++i) fac[i] = fac[i - ] * (ll)i % MOD;
  24. afac[M - ] = pow_mod(fac[M - ], MOD - );
  25. for(int i = M - ; i >= ; --i) afac[i - ] = afac[i] * (ll)i % MOD;
  26. }
  27. ll C(int n, int m) {
  28. if(m > n) return ;
  29. return ((fac[n] * afac[n - m] % MOD) * afac[m]) % MOD;
  30. }
  31. int main() {
  32. pre();
  33. cin >> h >> w >> n;
  34. ll x, y;
  35. for(int i = ; i <= n; ++i) {
  36. cin >> x >> y;
  37. bc[i] = (ll)x * X + y;
  38. }
  39. bc[++n] = (ll)h * X + w;
  40. sort(bc + , bc + n + );
  41. for(int i = ; i <= n; ++i) {
  42. int xi = bc[i] / X, yi = bc[i] % X;
  43. dp[i] = C(xi + yi - , yi - );
  44. for(int j = ; j < i; ++j) {
  45. int xj = bc[j] / X, yj = bc[j] % X;
  46. if(xj > xi || yj > yi) continue;
  47. dp[i] = (MOD + dp[i] - (dp[j] * C(xi - xj + yi - yj, yi - yj) % MOD)) % MOD;
  48. }
  49. }
  50. cout << dp[n] << endl;
  51. return ;
  52. }

Gym100947E || codeforces 559c 组合数取模的更多相关文章

  1. 2015 ICL, Finals, Div. 1 Ceizenpok’s formula(组合数取模,扩展lucas定理)

    J. Ceizenpok’s formula time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  2. 组合数取模Lucas定理及快速幂取模

    组合数取模就是求的值,根据,和的取值范围不同,采取的方法也不一样. 下面,我们来看常见的两种取值情况(m.n在64位整数型范围内) (1)  , 此时较简单,在O(n2)可承受的情况下组合数的计算可以 ...

  3. 排列组合+组合数取模 HDU 5894

    // 排列组合+组合数取模 HDU 5894 // 题意:n个座位不同,m个人去坐(人是一样的),每个人之间至少相隔k个座位问方案数 // 思路: // 定好m个人 相邻人之间k个座位 剩下就剩n-( ...

  4. hdu 3944 DP? 组合数取模(Lucas定理+预处理+帕斯卡公式优化)

    DP? Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0 ...

  5. [BZOJ 3129] [Sdoi2013] 方程 【容斥+组合数取模+中国剩余定理】

    题目链接:BZOJ - 3129 题目分析 使用隔板法的思想,如果没有任何限制条件,那么方案数就是 C(m - 1, n - 1). 如果有一个限制条件是 xi >= Ai ,那么我们就可以将 ...

  6. lucas定理解决大组合数取模

    LL MyPow(LL a, LL b) { LL ret = ; while (b) { ) ret = ret * a % MOD; a = a * a % MOD; b >>= ; ...

  7. BZOJ_2142_礼物_扩展lucas+组合数取模+CRT

    BZOJ_2142_礼物_扩展lucas+组合数取模 Description 一年一度的圣诞节快要来到了.每年的圣诞节小E都会收到许多礼物,当然他也会送出许多礼物.不同的人物在小E 心目中的重要性不同 ...

  8. 组合数取模&&Lucas定理题集

    题集链接: https://cn.vjudge.net/contest/231988 解题之前请先了解组合数取模和Lucas定理 A : FZU-2020  输出组合数C(n, m) mod p (1 ...

  9. 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1<p<=1e6,p必须为素数

    typedef long long ll; /********************************** 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1&l ...

随机推荐

  1. C#把DataTable中数据一次插入数据库的方法

    现在实际的情况是这样的: 客户有一台打卡机,员工打卡的信息全部储存在打卡机的Access数据库里面,现在客户引入了一种新的管理系统,需要将Access数据库中的打卡数据同步到SQL Server数据库 ...

  2. 如何自学 Java 开发

    如何自学 Java 开发? 568赞同反对,不会显示你的姓名 李艾米IT路上学习 568 人赞同 Java Web前端技术 HTML 入门视频课程 1 HTML 简介 2 HTML基本结构[ 3 HT ...

  3. ecshop 支付

    支付分成两部分 1.订单信息 2.支付日志ID 3.生成支付代码 一次性支付完成 // 支付信息 include_once('includes/lib_payment.php'); $order['l ...

  4. [java] 可视化日历的实现(基于Calendar类 )

    写在前面 博文安排顺序如下 1.写在前面 2.源码 3.思路 4.相关知识 该小程序是对Date类及其相关类的复习 要求如下图:实现可视化日历 实现思路 1.先从键盘输入指定格式的字符串(str)2. ...

  5. vijos1404 遭遇战

    描述 今天,他们在打一张叫DUSTII的地图,万恶的恐怖分子要炸掉藏在A区的SQC论坛服务器!我们SQC的人誓死不屈,即将于恐怖分子展开激战,准备让一个人守着A区,这样恐怖分子就不能炸掉服务器了.(一 ...

  6. 《UNIX网络编程(第3版)》unp.h等源码文件的编译安装

    操作系统:Mac OS X 10.11.5 1.下载书中的源代码:点击下载 2.切换到解压后的目录 unpv13e,先查看下 README,依次执行: ./configure cd lib make ...

  7. and or bool and a or b 原理解释

    python 中的and从左到右计算表达式,若所有值均为真,则返回最后一个值,若存在假,返回第一个假值. or也是从左到有计算表达式,返回第一个为真的值. 代码如下: IDLE 1.2.4>&g ...

  8. 倾力总结40条常见的移动端Web页面问题解决方案

    1.安卓浏览器看背景图片,有些设备会模糊.   用同等比例的图片在PC机上很清楚,但是手机上很模糊,原因是什么呢? 经过研究,是devicePixelRatio作怪,因为手机分辨率太小,如果按照分辨率 ...

  9. AngularJS 配置和运行phonecat错误

    安装node.js 就按照入门上的步骤来就行了 第一步:下载安装node.js,并将程序路径添加到环境变量中PATH中 第二步:下载git版本控制软件 第三步:在git目录下点击git-bash.ex ...

  10. mediastreamer使用教程

    mediastreamer使用教程 1.各个函数功能简介 ms_filter_destroy 释放filter资源 ms_ticker_destroy释放ticker 说明:ticker为定时器线程, ...