[AGC035F]Two Histograms

Description

Do you have a \ (N \) line, \ (M \) columns, each grid filled out the form 0. You were the following:

  • For each row \ (I \) , a natural number selected \ (r_i \ (0 ≤ M ≤ RI) \)
    , this line leftmost \ (r_i \) lattices
    number \ (1 + \) .

  • For each column \ (I \) , a natural number selected \ (C_i \ (0 ≤ N ≤ CI) \)
    , this column will be the uppermost \ (C_i \) lattices
    number \ (1 + \) .

In this way, according to your chosen \ (R_1, R_2,..., R_n, c_1, c_2,..., C_M \) , you get a grid each either \ (0 \) , either \ (1 \) , either \ (2 \) in a final form.

The different nature of the final table to ask how many. When the two essentially different forms, and if they have a number into a corresponding different lattice.

\(n, m\le 5\times 10^5\)

Solution

Csy of problem solution:

2019-09-02 15-04-49 screenshot .png

Where the number of combinations down to write backwards, multiplied by \ (k! \) Is to allow elected k rows and k columns eleven on match can be a fixed, another arrangement is \ (k! \) .

Code

#include <iostream>
#include <cstdio>

#define LL long long

using namespace std;

const int maxN = (int) 5e5;
const int mod = (int) 998244353;

LL qpow(LL a, LL b)
{
    LL ans = 1;
    while (b)
    {
        if (b & 1) ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}

int n, m, ans;
int fac[maxN + 2], ifac[maxN + 2];

void init(int n)
{
    fac[0] = 1;
    for (int i = 1; i <= n; ++i) fac[i] = 1ll * fac[i - 1] * i % mod;
    ifac[n] = qpow(fac[n], mod - 2);
    for (int i = n - 1; i >= 0; --i) ifac[i] = 1ll * ifac[i + 1] * (i + 1) % mod;
}

int C(int n, int m)
{
    if (n < 0 || m < 0 || m > n) return 0;
    return 1ll * fac[n] * ifac[m] % mod * ifac[n - m] % mod;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("AGC035F.in", "r", stdin);
    freopen("AGC035F.out", "w", stdout);
#endif

    scanf("%d%d", &n, &m);
    if (n < m) swap(n, m);
    init(n);
    for (int k = 0; k <= m; ++k)
    {
        ans += 1ll * qpow(-1, k) * C(n, k) % mod * C(m, k) % mod * fac[k] % mod * qpow(m + 1, n - k) % mod * qpow(n + 1, m - k) % mod;
        ans %= mod;
        (ans += mod) %= mod;
    }
    printf("%d\n", (1ll * ans + mod) % mod);
}

Guess you like

Origin www.cnblogs.com/cnyali-Tea/p/11446695.html