跳到正文

个人记录

容斥原理

gala_xk约 9 分钟
原文
文章信息与版本

文章信息

发布
2025年5月5日
收录
2025年12月31日
更新
2026年9月9日
篇幅
约 9 分钟

版本

  1. v1当前

前言

容斥原理其实是 OI 的常见思想,二维前缀和、DP等都要用到它。但是,当它单独作为数学题出现时,就能创飞一堆人。

普通容斥

二项式反演

f(n)=∑i=0n(ni)g(i)⇔g(n)=∑i=0n(ni)(−1)if(i)f(i)=∑j>i(ji)g(j)⇔g(i)=∑j>i(ji)(−1)j−if(j) f(n)=\sum^n_{i=0} \begin{pmatrix} n\\i \end{pmatrix}g(i) \Leftrightarrow g(n)=\sum^n_{i=0} \begin{pmatrix} n\\i \end{pmatrix} (-1)^{i}f(i)\\ f(i)=\sum_{j>i} \begin{pmatrix} j\\i \end{pmatrix}g(j) \Leftrightarrow g(i)=\sum_{j>i} \begin{pmatrix} j\\i \end{pmatrix}(-1)^{j-i}f(j)\\

反射容斥

min-max 容斥

其它技巧(奇技淫巧)

AT_arc120_f [ARC120F] Wine Thief

题解

直接做是困难的,所以我们尝试进行转化。

我们考虑给原题目加上限制,此时答案比原答案小,再把剩下的部分容斥进去就可以了。

我们使 AA 数组内的所有数等价,再组合求解。

于是,我们可以将 A1A_1 与 AnA_n 之间也加上限制,这样就变成环上染色,相邻两个颜色距离至少为 11。

有一个显然的插板:这个问题的方案数 f(n,k)=(kn−k+1)f(n,k)=\begin{pmatrix} k\\n-k+1\end{pmatrix}。

然后对于一个方案,我们钦定一个点必选,这个点会贡献 f(n−3,k−1)∗a[i]f(n-3,k-1)*a[i]。

那些方案重复统计了呢?

当点 11 与点 nn 都选的时候,要加上统计答案。此时,点 11 与点 nn 都选,点 22 与点 n−1n-1 都不选,其他任意。答案增加 f(n−4,k−2)∗(a[1]+a[n])f(n-4,k-2)*(a[1]+a[n])

发现了吗?这就转化成 n−4,k−2n-4,k-2 的情况了。

递归求解即可。

std

代码
C++
/* Use the slash-star style comments or the system won't see your
   identification information */
/*
ID: zhuheng1
TASK:
LANG: C++
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=998244353;
const int N=100005;

//Debug Part
#undef _GLIBCXX_DEBUG
template <typename A, typename B>string to_string(pair<A, B> p);
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {  return '"' + s + '"';}
string to_string(const char* s) {  return to_string((string) s);}
string to_string(bool b) {  return (b ? "true" : "false");}
string to_string(vector<bool> v) {  bool first = true;  string res = "{";  for (int i = 0; i < static_cast<int>(v.size()); i++) {  if (!first) {  res += ", ";  }  first = false;  res += to_string(v[i]);  }  res += "}";  return res;}
template <size_t N> string to_string(bitset<N> v) {  string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; }
template <typename A> string to_string(A v) {bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res;}
template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; }
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; }
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
//Debug Part End

ll qpow(ll x,ll y){
	if(y==0)return 1;
	ll z=qpow(x,y/2);
	z=z*z%mod;
	if(y&1)z=z*x%mod;
	return z;
}
ll inv(ll x){
	return qpow(x,mod-2);
}
ll a[300005],s[300005],fac[300005],ifac[300005];
//unordered_map<int,unordered_map<int,int> >mp;
ll C(ll n,ll m){
	return fac[n]*ifac[m]%mod*ifac[n-m]%mod;
}
ll calc_cir(int n,int k){
//	if(mp[n][k])return mp[n][k];
//	if(k==0)return 0;
//	if(k==1)return n;
//	mp[n][k]=calc_cir(n-4,k-2);
//	mp[n][k]%=mod;
	if(k<0||n<2*k-1)return 0;
	return C(n-k+1,k);
}
ll calc(int n,int k,int l,int r){
	if(k==0)return 0;
	if(k==1)return (s[r]-s[l-1]+mod)%mod;
	ll ret=calc_cir(n-3,k-1)*(s[r]-s[l-1])%mod%mod;
	ret=(ret+mod)%mod;
	ret+=calc(n-4,k-2,l+2,r-2)+calc_cir(n-4,k-2)*(a[l]+a[r])%mod;
	ret%=mod;
	return ret;
}
int n,k,d;
void work(){
	/*fill your code here*/
	fac[0]=1;
	for(int i=1;i<=300001;i++)fac[i]=fac[i-1]*i%mod;
	ifac[300001]=inv(fac[300001]);
	for(int i=300000;i>=0;i--)ifac[i]=ifac[i+1]*(i+1)%mod;
	cin>>n>>k>>d;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		s[i]=s[i-1]+a[i];
		s[i]%=mod;
	}
	cout<<calc(n,k,1,n);
	return;
}

#ifdef int
	 signed main()
#else
	 int main()
#endif
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	int T=1;
//	cin>>T;  /*If there's only one test case, comment this line.*/
	while(T--){
		work();
	}
    return 0;
}



/* Code written by
		gala_xk (MarchWind)*/



原文