跳到正文

题解

题解:AT_tenka1_2019_f Banned X

鲤鱼江约 5 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 5 分钟

版本

  1. v1当前

萌萌题但是我更萌。

首先经典结论,对于只有 0,1,20,1,2 的序列,若一个子区间和为 kk,则对于所有 x∈[1,⌊k2⌋]x\in[1,\lfloor\frac k2\rfloor],k−2xk-2x 都是可以凑出来的。

考虑拿出这个子区间,若有一端是 00,可以直接丢掉这个 00。

若有一端是 22,丢掉这个 22,就凑出来了 k−2k-2。

否则两端都是 11,一起丢掉,也能凑出 k−2k-2,故此得证。

设序列之和 ss,分三类讨论:

  1. s<Xs<X,直接就是合法的。
  2. s≥Xs\ge X 且 s≡X(mod2)s\equiv X\pmod 2,明显不合法。
  3. 否则可能合法也可能不合法。

发现第三类讨论是模糊不清的,从此入手。

首先我们可以直接枚举 00 的个数 xx,因为 00 放在哪里都不影响答案,只需要最后将答案乘上 (nx){n\choose x} 就行了,现在我们认为序列中只存在 1,21,2。

顺势枚举 1,21,2 的个数 y,zy,z。

令 i,ji,j 表示序列的最前面和最后面有多少个 22,正式开始讨论充要条件。

我们声称,s−2min⁡(i,j)−1<Xs-2\min(i,j)-1<X 是不存在子区间和为 XX 的充要条件。

理由:我们肯定是要找到所以一个和最大的子区间,使得其和 sumsum 与 XX 奇偶性相同,显然 sum<Xsum<X 是合法的充要条件,不难发现拿出这个子区间的方法是从序列的一端扣掉若干个 22 和一个 11。

所以若 s−2min⁡(i,j)−1<Xs-2\min(i,j)-1<X,则我们无法找到一个子区间和为 XX。

移项发现 min⁡(i,j)≥⌊s−X−12⌋+1\min(i,j)\ge\lfloor\frac{s-X-1}{2}\rfloor+1。

说明序列前后都至少有 ⌊s−X−12⌋+1\lfloor\frac{s-X-1}{2}\rfloor+1 个 22,剩下的 1,21,2 可以随便放,就做完了。

注意分讨 y=1y=1 和 y=0y=0 的情况,是容易的。

C++
#include<bits/stdc++.h>

using namespace std;

const int N=6010;
const int mod=998244353;
struct modint {
	int val;
	static int norm(const int& x) { return x < 0 ? x + mod : x; }
	inline int Mod(int x){return x>=mod?x-mod:x;}
	modint inv() const {
	    int a = val, b = mod, u = 1, v = 0, t;
	    while (b > 0) t = a / b, swap(a -= t * b, b), swap(u -= t * v, v);
	    return modint(u);
	}
	modint() : val(0) {}
	modint(const int& m) : val(norm(m)) {}
	modint(const long long& m) : val(norm(m % mod)) {}
	modint operator-() const { return modint(norm(-val)); }
	modint& operator+=(const modint& o) { return val = Mod(val + o.val), *this; }
	modint& operator-=(const modint& o) { return val = norm(1ll * val - o.val), *this; }
	modint& operator*=(const modint& o) { return val = static_cast<int>(1ll * val * o.val % mod), *this; }
	modint operator-(const modint& o) const { return modint(*this) -= o; }
	modint operator+(const modint& o) const { return modint(*this) += o; }
	modint operator*(const modint& o) const { return modint(*this) *= o; }
	friend std::ostream& operator<<(std::ostream& os, const modint& a) { return os << a.val; }
}c[N][N],ans,tot;

int n,X;
inline modint C(int n,int m){return (n<m||m<0)?0:c[n][m];}
inline modint C3(int x,int y,int z){return c[x+y+z][x]*c[y+z][y];}
inline void init(int n=3001){
	c[0][0]=1;
	for(int i=1;i<=n;++i){
		c[i][0]=c[i][i]=1;
		for(int j=1;j<i;++j) c[i][j]=c[i-1][j]+c[i-1][j-1];
	}
}

int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n>>X;init();
	for(int x=0;x<=n;++x){
		modint ws=0;
		for(int y=0;y<=n-x;++y){
			int z=n-x-y,sum=y+2*z,w=(sum-X-1)/2+1;
			if(sum<X){ans+=C3(x,y,z);continue;}
			if((sum&1)==(X&1)) continue;
			if(!y){ans+=C3(x,y,z);continue;}
			if(z<2*w) continue;
			if(y==1){ans+=C(x+y+z,x)*(z-2*w+1);continue;}
			ws+=C(y+z-2*w,y);
		}
		ans+=ws*c[n][x];
	}
	cout<<ans<<'\n';
	return 0;
}
原文