【组合计数】CF1866 H

Problem - H - Codeforces

题意

思路

不知道这种trick叫什么,昨天VP刚遇到过

设 fx 为恰好有一个最大值为 x 的方案数,我们要求这个,那就设 gx 为 至少有一个最大值为 x 的方案数,那么答案就是 fx = gx - gx - 1

这里也一样,不过要稍微变一下

Code:

cpp 复制代码
#include <bits/stdc++.h>

#define int long long

constexpr int N = 1e6 + 10;
constexpr int mod = 998244353;
constexpr int Inf = 0x3f3f3f3f;

int n, k;
int Fac[N];
int inv[N];
int g[N];

int qpow(int a, int b) {
	int res = 1;
	while(b) {
		if (b & 1) res = (res * a) % mod;
		a = (a * a) % mod;
		b >>= 1;
	}
	return res;
}
void Fac_init() {
	Fac[0] = 1;
	for (int i = 1; i < N; i ++) {
		Fac[i] = (Fac[i - 1] * i) % mod;
	}
	inv[N - 1] = qpow(Fac[N - 1], mod - 2);
	for (int i = N - 2; i >= 0; i --) {
		inv[i] = (inv[i + 1] * (i + 1)) % mod;
	}
}
void solve() {
	std::cin >> n >> k;
	for (int i = std::max(0ll, n - k); i <= n; i ++) {
		g[i] = Fac[n - i + 1] * qpow(n - i + 1, k - (n - i)) % mod;
	}
	int ans = 0;
	for (int i = n; i >= std::max(0ll, n - k); i --) {
		int res = g[i] - g[i + 1];
		res *= Fac[n];
		res %= mod;
		res *= inv[i];
		res %= mod;
		ans += res;
		ans %= mod;
	}
	std::cout << ((ans % mod) + mod) % mod << "\n";
}
signed main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);

	int t = 1;
	Fac_init();
	while (t--) {
		solve();
	}
	return 0;
}
相关推荐
geovindu3 小时前
python:Logistic Regression
开发语言·后端·python·算法
geovindu4 小时前
python: Random Forest
开发语言·后端·python·算法·随机森林
凌冰_5 小时前
数据结构 - 线性结构(三)
java·数据结构·算法·软件工程
拂拉氏5 小时前
【知识讲解】 数据结构与算法-树状数组的认识
数据结构·算法·树状数组
饮水桶_T8 小时前
C++ STL00 | 六大核心组件协作框架 笔记
开发语言·算法·百度·facebook·新浪微博
饮水桶_T10 小时前
Java调用大语言模型、多轮对话 笔记
笔记·算法·设计模式·电脑·ai编程
AAA修煤气灶刘哥10 小时前
QQ 音乐怎么在 25 毫秒内,决定一句话该交给谁?
人工智能·算法·llm
开开心心就好11 小时前
电脑禁止自动休眠工具,还能按负载定时
算法·智能手机·pdf·电脑·word·音视频·lua
geovindu1 天前
python:DecisionTree
开发语言·后端·python·算法