【组合计数】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;
}
相关推荐
傅科摆 _ py22 分钟前
DP,贪心,堆和栈,二分力扣复习
开发语言·python·算法
Shaoshing1 小时前
LeetCode100 42.接雨水
java·算法·leetcode
域限奇点访客1 小时前
用AI生成问卷的网站怎么选?看题目录入结构、题型映射和生成后的校验
算法
not coder2 小时前
一文讲通--从树到红黑树
数据结构·算法
octopus_c2 小时前
C++ 入门
开发语言·c++·算法
ZhengEnCi2 小时前
2026年10月30日AI早报:Gemini 4 Carbon登顶、DeepSeek融资1055亿、智元机器人上岗实测
人工智能·算法
强哥聊算力3 小时前
《算力江湖·Intel帝国往事》第三集:安迪·格鲁夫的铁血时代:Intel为什么能把对手逼到墙角?
运维·人工智能·科技·算法·ai·gpu算力
Shaoshing3 小时前
LeetCode100 11. 盛最多水的容器
算法·leetcode
骑着蜗牛撵大象3273 小时前
558 万美元训出 671B 模型:DeepSeek-V3 算法与系统的协同拆解
分布式·算法·moe·deepseek-v3·mla·fp8训练
Eyfcom4 小时前
〖阶段性回顾〗物流系统架构与算法的 Day 1-5 知识图谱与实战答疑
算法·系统架构·知识图谱·物流管理系统·快递管理系统