【C++组合数学】P8106 [Cnoi2021] 数学练习|普及+

本文涉及知识点

组合数学汇总

[Cnoi2021] 数学练习

题目背景

「Cnoi2021」Cirno's Easy Round II 热身赛开始了。

题目描述

为了让选手们重视文化课,Cirno 特意加入了一道 Kamishirasawa Keine 老师的数学练习:

求将一个集合 U = { 1 , 2 , 3 , ⋯   , n } \texttt{U}=\{1,2,3,\cdots,n\} U={1,2,3,⋯,n} 划分成两个子集 S , T S,T S,T,使得 ∣ S ∣ ∉ S , ∣ T ∣ ∉ T |S|\notin S,|T|\notin T ∣S∣∈/S,∣T∣∈/T 的方案数。

由于选手都不会高精度,所以答案只需要对 998244353 998244353 998244353 取模即可。

输入格式

一行一个整数 n n n。

输出格式

一行,一个整数,表示答案。

样例 #1

样例输入 #1

复制代码
3

样例输出 #1

复制代码
2

样例 #2

样例输入 #2

复制代码
6

样例输出 #2

复制代码
10

样例 #3

样例输入 #3

复制代码
65535

样例输出 #3

复制代码
459810767

提示

样例解释

#1: 两种合法的划分方案为 { 1 , 3 } , { 2 } \{1,3\},\{2\} {1,3},{2} 与 { 2 } , { 1 , 3 } \{2\},\{1,3\} {2},{1,3} 。

数据范围

对于 100 % 100\% 100% 的数据,保证 1 ≤ n ≤ 10 5 1 \le n \le 10^5 1≤n≤105。

组合数学

s,t不能为空,否则对方会包括自己的长度。

枚举s的长度为i,i ∈ \in ∈[1,n-1]。

s必须选择n-i,否则t包括|t|。

s不能选择i,否则s包括|s|。

故从n-2选择i-1。

累加并取模。

直接用组合会超时,其含义是:n-2个数,选择0到n-2的方案数。 即2n-2
注意

如果是偶数,则i = n/2的时候,要扣掉。n/2放到s或t都不行。

代码

核心代码

cpp 复制代码
#include <iostream>
#include <sstream>
#include <vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<string>
#include<algorithm>
#include<functional>
#include<queue>
#include <stack>
#include<iomanip>
#include<numeric>
#include <math.h>
#include <climits>
#include<assert.h>
#include<cstring>

#include <bitset>
using namespace std;



template<class T = int>
vector<T> Read(int n,const char* pFormat = "%d") {
	vector<T> ret;
	T d ;
	while (n--) {
		scanf(pFormat, &d);
		ret.emplace_back(d);
	}
	return ret;
}

template<class T = int>
vector<T> Read( const char* pFormat = "%d") {
	int n;
	scanf("%d", &n);
	vector<T> ret;
	T d;
	while (n--) {
		scanf(pFormat, &d);
		ret.emplace_back(d);
	}
	return ret;
}

string ReadChar(int n) {
	string str;
	char ch;
	while (n--) {
		do
		{
			scanf("%c", &ch);
		} while (('\n' == ch));
			str += ch;
	}
	return str;
}

template<int MOD = 1000000007>
class C1097Int
{
public:
	C1097Int(long long llData = 0) :m_iData(llData% MOD)
	{

	}
	C1097Int  operator+(const C1097Int& o)const
	{
		return C1097Int(((long long)m_iData + o.m_iData) % MOD);
	}
	C1097Int& operator+=(const C1097Int& o)
	{
		m_iData = ((long long)m_iData + o.m_iData) % MOD;
		return *this;
	}
	C1097Int& operator-=(const C1097Int& o)
	{
		m_iData = (m_iData + MOD - o.m_iData) % MOD;
		return *this;
	}
	C1097Int  operator-(const C1097Int& o)
	{
		return C1097Int((m_iData + MOD - o.m_iData) % MOD);
	}
	C1097Int  operator*(const C1097Int& o)const
	{
		return((long long)m_iData * o.m_iData) % MOD;
	}
	C1097Int& operator*=(const C1097Int& o)
	{
		m_iData = ((long long)m_iData * o.m_iData) % MOD;
		return *this;
	}
	C1097Int  operator/(const C1097Int& o)const
	{
		return *this * o.PowNegative1();
	}
	C1097Int& operator/=(const C1097Int& o)
	{
		*this /= o.PowNegative1();
		return *this;
	}
	bool operator==(const C1097Int& o)const
	{
		return m_iData == o.m_iData;
	}
	bool operator<(const C1097Int& o)const
	{
		return m_iData < o.m_iData;
	}
	C1097Int pow(long long n)const
	{
		C1097Int iRet = 1, iCur = *this;
		while (n)
		{
			if (n & 1)
			{
				iRet *= iCur;
			}
			iCur *= iCur;
			n >>= 1;
		}
		return iRet;
	}
	C1097Int PowNegative1()const
	{
		return pow(MOD - 2);
	}
	int ToInt()const
	{
		return (m_iData + MOD) % MOD;
	}
private:
	int m_iData = 0;;
};

template<class T >
class CFactorial
{
public:
	CFactorial(int n) :m_res(n + 1) {
		m_res[0] = 1;
		for (int i = 1; i <= n; i++) {
			m_res[i] = m_res[i - 1] * i;
		}
	}
	T Com(int iSel, int iCanSel)const {
		return m_res[iCanSel] / m_res[iSel] / m_res[iCanSel - iSel];
	}
	T Com(const vector<int>& cnt)const {
		T biRet = 1;
		int iCanSel = std::accumulate(cnt.begin(), cnt.end(), 0);
		for (int j = 0; j < cnt.size(); j++) {
			biRet *= Com(cnt[j], iCanSel);
			iCanSel -= cnt[j];
		}
		return biRet;
	}
	vector<T> m_res;
};
class Solution {
		public:
			int Ans(int n ) {
				if (n <= 2) { return 0; }
				typedef C1097Int< 998244353> BI;
				BI ans = BI(2).pow(n-2);
				CFactorial<BI> fac(n - 2);
				if (0 == n % 2) {
					ans -= fac.Com(n / 2-1, n - 2);
				}
				return ans.ToInt();
			}
		};	
int main() {
#ifdef _DEBUG
	freopen("a.in", "r", stdin);
#endif // DEBUG
	int n;
	scanf("%d", &n);
#ifdef _DEBUG
	//Out(a, "a=");
#endif
	
	auto res = Solution().Ans(n);
	cout << res << endl;
	return 0;
}

单元测试

cpp 复制代码
TEST_METHOD(TestMethod1)
		{	
			auto res = Solution().Ans(3);
			AssertEx(2, res);
		}
		TEST_METHOD(TestMethod2)
		{
			auto res = Solution().Ans(6);
			AssertEx(10, res);
		}
		TEST_METHOD(TestMethod3)
		{
			auto res = Solution().Ans(65535);
			AssertEx(459810767, res);
		}

扩展阅读

我想对大家说的话
工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。
学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作
有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注
闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。
子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。
如果程序是一条龙,那算法就是他的是睛
失败+反思=成功 成功+反思=成功

视频课程

先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771

如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176

测试环境

操作系统:win7 开发环境: VS2019 C++17

或者 操作系统:win10 开发环境: VS2022 C++17

如无特殊说明,本算法用**C++**实现。

相关推荐
ALex_zry2 小时前
C++ const成员函数详解:原理、应用与最佳实践
c++
hetao17338373 小时前
2025-12-22 hetao1733837的笔记
c++·笔记·算法
charlee443 小时前
一种基于 SQLite3 的半自动 C++ ORM 实现
c++·sql·sqlite·orm·crud
DeltaTime3 小时前
三 视图变换, 投影变换, 正交投影, 透视投影
c++·图形渲染
superman超哥3 小时前
仓颉Result类型的错误处理模式深度解析
c语言·开发语言·c++·python·仓颉
八月的雨季 最後的冰吻3 小时前
FFmepg-- 38-ffplay源码-缓冲区 audio_buf调试
c++·ffmpeg·音视频
会思考的猴子3 小时前
UE5 C++ 笔记 GameplayAbilitySystem人物角色
c++·笔记·ue5
ht巷子3 小时前
Qt:信号与槽
开发语言·c++·qt
千里马-horse3 小时前
Checker Tool
c++·node.js·napi