跳到正文

题解

题解:CF2187D Cool Problem

RainbowQAQ约 5 分钟
原文
文章信息与版本

文章信息

发布
2026年2月26日
收录
2026年6月15日
更新
2026年11月14日
篇幅
约 5 分钟
获赞
2
题目
CF2187D

版本

  1. v1当前

这道题怎么这么强。


把式子改一改得:

r=0r=0 时 ci−x+y2=ci−1+x−y2c_i-\dfrac{x+y}{2}=c_{i-1}+\dfrac{x-y}{2}

r=1r=1 时 ci−x+y2=y−x2−ci−1c_i-\dfrac{x+y}{2}=\dfrac{y-x}{2}-c_{i-1}

有:(ci−x+y2)2=(y−x2−ci−1)2(c_{i}-\dfrac{x+y}{2})^2=(\dfrac{y-x}{2}-c_{i-1})^2

推一下式子就有了:(ci2−ci−12)−y(ci−ci−1)+xy=x(ci+ci−1)(c_i^2-c_{i-1}^2)-y(c_i-c_{i-1})+xy=x(c_i+c_{i-1})

所以:

Ans=cn2+(x−y)cn+nxy2xAns=\frac{c_n^2+(x-y)c_n+nxy}{2x}

求出 cnc_n 的种类就行,设 fi,j,k=0/1f_{i,j,k=0/1} 表示对于前 ii 个数,xx 的系数为 jj,yy 的系数为 k=0/1k=0/1(不难发现 yy 的系数只能是这个)用 bitset 优化。

Code:\mathscr{Code:}

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

#define LL long long
#define int LL
#define pb push_back
#define PII pair<int, int>
#define i64 __int128
#define YY puts("Yes"), exit(0)
#define NN puts("No"), exit(0)
using namespace std;
const int Mod = 998244353;
const int Inf = 0x3f3f3f3f;
const LL InfLL = 0x3f3f3f3f3f3f3f3f;
char buf[1 << 21], *p1 = buf, *p2 = buf;
//#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
//#define getchar() gc()
LL read() {
	LL s = 0; char ch = getchar(); bool fu = false;
	while (ch < '0' || ch > '9') ch == '-' ? fu = true : 0, ch = getchar();
	while (ch >= '0' && ch <= '9') s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
	return fu ? -s : s;
}

const int N = 1e5 + 10;
int n, x, y;

i64 calc(i64 t) {
	return (t * t + (x - y) * t + (i64)n * x * y) / (2 * x);
}

inline void Main() {
	n = read(), x = read(), y = read();
	string s; cin >> s;
	bitset<N> f1, f0, g1, g0; // f 存正数,g 存负数
	f0.set(0), g0.set(0);
	for (auto ch : s) {
		bitset<N> nf1, nf0, ng1, ng0;
		if (ch != '0') {
			nf1 |= g0;
			nf0 |= g1;
			ng1 |= f0;
			ng0 |= f1;
		}
		if (ch != '1') {
			nf1 |= f1 << 1;
			nf0 |= f0 << 1;
			ng1 |= g1 >> 1;
			ng0 |= g0 >> 1;
		}
		nf0[0] = nf0[0] | ng0[0];
		ng0[0] = nf0[0] | ng0[0];
		nf1[0] = nf1[0] | ng1[0];
		ng1[0] = nf1[0] | ng1[0];
		f0 = nf0, g0 = ng0;
		f1 = nf1, g1 = ng1;
	}
	vector<i64> ans;
	for (int i = 0; i <= n; ++i) {
		if (f0[i]) ans.push_back(calc(i * x));
		if (f1[i]) ans.push_back(calc(i * x + y));
	}
	for (int i = 1; i <= n; ++i) {
		if (g0[i]) ans.push_back(calc(-i * x));
		if (g1[i]) ans.push_back(calc(-i * x + y));
	}
	sort(ans.begin(), ans.end());
	ans.erase(unique(ans.begin(), ans.end()), ans.end());
	LL Ans = 0;
	for (auto it : ans)
		Ans = (Ans + (it % Mod + Mod)) % Mod;
	printf("%lld\n", Ans);
}

signed main() {
//	freopen("input.in", "r", stdin);
//	ios::sync_with_stdio(false);
//	cin.tie(0), cout.tie(0);
	int T = read();
	while (T--)
		Main();
	return 0;
}
原文
存档评论1 条抓取于