cf 1312 D 逆元模板

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

解析:
num2 * inv(num1) % mod 表示C mn-1, (n - 2) 表示最大的数不能重复,所以只有n-1种可能,qpow(2, n - 3))表示2n-2/2,因为总共2n-2种可能,其中一半是重复的,所以要除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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值