【组合计数】CF1866 H

Problem - H - Codeforces

题意

思路

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

设 f[x] 为恰好有一个最大值为 x 的方案数,我们要求这个,那就设 g[x] 为 至少有一个最大值为 x 的方案数,那么答案就是 f[x] = g[x] - g[x - 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;
}
相关推荐
君义_noip12 小时前
信息学奥赛一本通 1661:有趣的数列 | 洛谷 P3200 [HNOI2009] 有趣的数列
c++·算法·组合数学·信息学奥赛·csp-s
程序员:钧念12 小时前
深度学习与强化学习的区别
人工智能·python·深度学习·算法·transformer·rag
英英_13 小时前
MATLAB数值计算基础教程
数据结构·算法·matlab
一起养小猫13 小时前
LeetCode100天Day14-轮转数组与买卖股票最佳时机
算法·leetcode·职场和发展
hele_two14 小时前
快速幂算法
c++·python·算法
l1t14 小时前
利用DeepSeek将python DLX求解数独程序格式化并改成3.x版本
开发语言·python·算法·数独
jllllyuz14 小时前
基于子集模拟的系统与静态可靠性分析及Matlab优化算法实现
算法·matlab·概率论
程序员-King.15 小时前
day143—递归—对称二叉树(LeetCode-101)
数据结构·算法·leetcode·二叉树·递归
BlockChain88815 小时前
字符串最后一个单词的长度
算法·go
爱吃泡芙的小白白15 小时前
深入解析:2024年AI大模型核心算法与应用全景
人工智能·算法·大模型算法