【组合计数】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;
}
相关推荐
wadesir5 小时前
Rust中的条件变量详解(使用Condvar的wait方法实现线程同步)
开发语言·算法·rust
yugi9878386 小时前
基于MATLAB实现协同过滤电影推荐系统
算法·matlab
TimberWill6 小时前
哈希-02-最长连续序列
算法·leetcode·排序算法
Morwit6 小时前
【力扣hot100】64. 最小路径和
c++·算法·leetcode
leoufung6 小时前
LeetCode 373. Find K Pairs with Smallest Sums:从暴力到堆优化的完整思路与踩坑
java·算法·leetcode
wifi chicken7 小时前
数组遍历求值,行遍历和列遍历谁更快
c语言·数据结构·算法
胡楚昊7 小时前
NSSCTF动调题包通关
开发语言·javascript·算法
Gold_Dino7 小时前
agc011_e 题解
算法
bubiyoushang8888 小时前
基于蚁群算法的直流电机PID参数整定 MATLAB 实现
数据结构·算法·matlab
风筝在晴天搁浅8 小时前
hot100 240.搜索二维矩阵Ⅱ
算法·矩阵