D. Count the Arrays (+ simple mathematical thinking)

Title: Portal

Meaning of the questions: Q How many sequences of length n, are met:

1, each element of the sequence ai satisfy 1 <= ai <= m

2, only one pair of elements exactly equal

3, there is one index i such that  A J < A J + 1'd, if j<i, and aj>aj+1, if ji

 

Ideas:

Sequence of length n, and happen to have a pair of equal number, then there are n - 1 number of different elements, each element is between 1 ~ m, and that there are C (n - 1, m) ways to take n - 1 different numbers.

Just a pair of equal elements, can not be equal to the number of maximum number, the other can, that there is an equal number (n - 2) types of borrowing.

Length of the n different n numbers consisting of the sequence satisfies the condition of total 3 2 (n-1) ^ species.

Certainly equal number on both sides of the maximum value, the other did not limit the number, the maximum value can not be on both sides, and different from each other n - 2 Position the Ruoyi number, equal to the number of positions that also with of OK.

3 is satisfied conditions of 2 ^ (n - 3) types, both as to free space, the number of other finalized, then fill the two numbers are equal.

 

#include <bits/stdc++.h>
#define LL long long
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF INT_MAX
#define inf LLONG_MAX
#define PI acos(-1)
#define fir first
#define sec second
using namespace std;

const int mod = 998244353;

LL ksm(LL a, int b) {
    LL res = 1;
    while(b) {
        if(b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

LL C(int n, int m) {
    LL resdw1 = 1;
    rep(i, 1, n) resdw1 = resdw1 * i % mod;
    LL resdw2 = 1;
    rep(i, 1, m - n) resdw2 = resdw2 * i % mod;
    LL resup = 1;
    rep(i, 1, m) resup = resup * i % mod;
    return resup * ksm(resdw1, mod - 2) % mod * ksm(resdw2, mod - 2) % mod;
}

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    if(n == 2) puts("0");
    else {
        LL ans = ksm(2LL, n - 3) * 1LL * (n - 2) % mod * C(n - 1, m) % mod;
        printf("%lld\n", ans);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Willems/p/12465248.html