cf 1312 D inverse template

Your task is to calculate the number of arrays such that:

each array contains n elements;
each element is an integer from 1 to m;
for each array, there is exactly one pair of equal elements;
for each array a, there exists an index i such that the array is strictly ascending before the i-th element and strictly descending after it (formally, it means that aj<aj+1, if j<i, and aj>aj+1, if j≥i).
Input
The first line contains two integers n and m (2≤n≤m≤2⋅105).

Output
Print one integer — the number of arrays that meet all of the aforementioned conditions, taken modulo 998244353.

Examples
input Copy
3 4
output Copy
6
input Copy
3 5
output Copy
10
input Copy
42 1337
output Copy
806066790
input Copy
100000 200000
output Copy
707899035

Analytical:
num2 * INV (num1)% MOD denotes C m n-1 , (n-- 2) represents the maximum number can not be repeated, so only n-1 possible, qpow (2, n - 3 )) 2 represents N- 2 /2, because a total of 2 n-2- possible, half of which is repeated, so that in addition to 2.

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <vector>
#include <map>

#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define ull unsigned long long
#define uint unsigned int
#define l(x) ((x)<<1)
#define r(x) ((x)<<1|1)
#define lowbit(x) ((x)&(-(x)))
#define ms(a,b) memset(a,b,sizeof(a))
#define NSYNC std::ios::sync_with_stdio(false);std::cin.tie(0);std::cout.tie(0);

using namespace std;

const int maxn = 200002;

ll mod = 998244353;
ll qpow(ll a, ll b) {
	ll res = 1;
	while (b) {
		if (b & 1) res = res * a % mod;
		b /= 2;
		a = a * a % mod;
	}
	return res % mod;
}
ll inv(ll m) {
	return  qpow(m, mod - 2);
}

int main() {
	ll n, m, num1 = 1, num2 = 1;

	cin >> n >> m;
	for (ll i = 1; i < n; i++) {
		num1 = (num1 * i) % mod;
	}

	for (ll i = m; i > m - n + 1; i--) {
		num2 = (num2 * i) % mod;
	}
	cout << (num2 * inv(num1) % mod * (n - 2) % mod * qpow(2, n - 3)) % mod << "\n";

	return 0;
}
Published 86 original articles · won praise 8 · views 2202

Guess you like

Origin blog.csdn.net/Fawkess/article/details/104774668