• Gym100947E || codeforces 559c 组合数取模


    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
    2 3
    1 2
    Output
    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

    `

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int N = 2e5 + 10;
    const int MOD = 1e9 + 7;
    typedef long long ll;
    ll fac[N], afac[N];
    ll powm(ll a, ll b) {
        ll ans = 1;
        a = a % MOD;
        while(b) {
            if(b & 1) ans = ans * a % MOD;
            a = a * a % MOD;
            b >>= 1;
        }
        return ans;
    }
    void pre() {
        fac[0] = 1;
        for(int i = 1; i < N; ++i) fac[i] = fac[i - 1] * (ll)i % MOD;
        afac[N - 1] = powm(fac[N - 1], MOD - 2);
        for(int i = N - 1; i >= 1; --i) afac[i - 1] = afac[i] * i % MOD;
    }
    ll get(int x, int y) {
        ll ans = 1;
        ans = ((fac[x] * afac[x - y]) % MOD * afac[y]) % MOD;
        return ans;
    }
    int main() {
        int _; scanf("%d", &_);
        pre();
        while(_ --) {
            ll n, m, x, y;
            scanf("%I64d%I64d%I64d%I64d", &n, &m, &x, &y);
            ll res1 = get(n + m - 2, m - 1);
            ll res2 = get(x + y - 2, y - 1);
            ll res3 = get(n + m - x - y, m - y);
            printf("%I64d
    ", (res1 + MOD - res2 * res3 % MOD) % MOD);
        }
        return 0;
    }
    View Code
    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
    3 4 2
    2 2
    2 3
    Output
    2
    Input
    100 100 3
    15 16
    16 15
    99 88
    Output
    545732279

    题意:这题是上面的复杂版,有n个障碍格,n《= 2000
    思路:我们设dp[i]表示到达第i个障碍格,而不经过(1,1)到(xi,yi)中其他障碍格的方案数,那么有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] 就是答案
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int N = 2005;
    const int X = 1e5 + 10;
    const int M = 2e5 + 10;
    const int MOD = 1e9 + 7;
    int h, w, n;
    ll fac[M], afac[M], bc[N], dp[N];
    
    ll pow_mod(ll a, ll b) {
        ll ans = 1;
        a %= MOD;
        while(b) {
            if(b & 1) ans = (ans * a) % MOD;
            a = (a * a) % MOD;
            b >>= 1;
        }
        return ans;
    }
    void pre() {
        fac[0] = 1;
        for(int i = 1; i < M; ++i) fac[i] = fac[i - 1] * (ll)i % MOD;
        afac[M - 1] = pow_mod(fac[M - 1], MOD - 2);
        for(int i = M - 1; i >= 1; --i) afac[i - 1] = afac[i] * (ll)i % MOD;
    }
    ll C(int n, int m) {
        if(m > n) return 0;
        return ((fac[n] * afac[n - m] % MOD) * afac[m]) % MOD;
    }
    int main() {
        pre();
        cin >> h >> w >> n;
        ll x, y;
        for(int i = 1; i <= n; ++i) {
            cin >> x >> y;
            bc[i] = (ll)x * X + y;
        }
        bc[++n] = (ll)h * X + w;
        sort(bc + 1, bc + n + 1);
        for(int i = 1; i <= n; ++i) {
            int xi = bc[i] / X, yi = bc[i] % X;
            dp[i] = C(xi + yi - 2, yi - 1);
            for(int j = 1; j < i; ++j) {
                int xj = bc[j] / X, yj = bc[j] % X;
                if(xj > xi || yj > yi) continue;
                dp[i] = (MOD + dp[i] - (dp[j] * C(xi - xj + yi - yj, yi - yj) % MOD)) % MOD;
            }
        }
        cout << dp[n] << endl;
        return 0;
    }
    View Code
  • 相关阅读:
    JSP引擎的工作原理
    Hibernate缓存配置
    理解LinkedHashMap
    如何在CMD下运用管理员权限
    sun.misc.BASE64Encoder找不到jar包的解决方法
    访问WEB-INF目录中的JSP文件
    Servlet Filter(过滤器)、Filter是如何实现拦截的、Filter开发入门
    message from server: "Host 'xxx' is not allowed to connect to this MySQL server的解决
    深入Java单例模式
    (八)路径(面包屑导航)分页标签和徽章组件
  • 原文地址:https://www.cnblogs.com/orchidzjl/p/5434651.html
Copyright © 2020-2023  润新知