当前位置: 首页 > article >正文

【组合计数】CF1866 H

Problem - H - Codeforces

题意

思路

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

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

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

Code:

#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;
}

 


http://www.kler.cn/a/105075.html

相关文章:

  • 【Qt之QtConcurrent】描述及使用
  • 【2024秋招】2023-10-9 同花顺后端笔试题
  • ubuntu下Docker的简单使用并利用主机显示
  • 笔记43:ResNet 结构详解
  • 关于集群和分布式部署
  • 渗透测试工具-sqlmap使用
  • vue源码分析(二)——vue的入口发生了什么
  • 【C++笔记】C++继承
  • 【python海洋专题S2】找范围的语句进阶版本V2
  • Python循环语句(二)
  • Proteus仿真--量程自动切换数字电压表(仿真+程序)
  • UE4/5 竖排文字文本
  • asp.net core webapi signalR通信
  • Spring Boot + EasyUI 创建第一个项目(一)
  • 聊聊“JVM 调优JVM 性能优化”是怎么个事?
  • Server Name Indication(SNI),HTTP/TLS握手过程解析
  • Hafnium安全分区管理器和示例参考软件栈
  • DB-GPT发布:用私有LLM技术彻底改革数据库互动
  • 【软件测试】了解JUnit单元测试框架常用注解
  • 导致爬虫无法使用的原因有哪些?