题解:CF2187D Cool Problem

· · 题解

这道题怎么这么强。

把式子改一改得:

r=0$ 时 $c_i-\dfrac{x+y}{2}=c_{i-1}+\dfrac{x-y}{2} r=1$ 时 $c_i-\dfrac{x+y}{2}=\dfrac{y-x}{2}-c_{i-1}

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

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

所以:

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

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

\mathscr{Code:}
#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;
}