zoukankan      html  css  js  c++  java
  • Qwerty78 Trip(组合数,规律,逆元)

    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<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    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
    ", (C(n + m - 2, n - 1) - C(n+m-r-c, n-r)*C(r+c-2, r-1)%MOD + MOD) % MOD);
        }
        return 0;
    }
  • 相关阅读:
    IntelliJ IDEA常用的快捷键积累总结
    Linux命令(六)之防火墙iptables的相关操作以及端口的开放
    Linux命令(五)之service服务查找、启动/停止等相关操作
    Linux命令(四)之常用文件拷贝/移动,文件解压缩,文件查找等相关的操作
    Linux命令(三)vim编辑器的常用命令
    Linux命令(一)之目录结构、Linux终端操作、关机重启等一些基本操作
    zookeeper核心知识与投票机制详解
    zuul开发实战(限流,超时解决)
    IO多路复用技术详解
    Linux五大网络IO模型图解
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5531654.html
Copyright © 2011-2022 走看看