Solution P3791

· · 题解

晕,位运算尽然要写 1ll << s。

给定 n,m,x,求 \sum_{i=0}^n \sum_{j=0}^m d(i\bigoplus j\bigoplus x)。

考虑换个枚举顺序,变成对于每个不同的 c=i\bigoplus j\bigoplus x,求其出现次数 cnt 再乘上对应的 d(c),总贡献 cnt \times d(c)。

因为涉及到位运算,所以考虑拆位,然后发现很想数位 dp,但偏偏 dp 不了。于是我们来拆分一下数位 dp 的具体过程,其中很关键的参量就是 lim 判断是否达上限和 lead 是否为前导零。前导零在这道题中是没地位的,直接扔了,而是否达上线,我们可以发现,是统计 cnt 的关键。

于是我们枚举一个 i 和 j,不妨假设 j \le i,分别代表 n 和 m 在什么位置打破上限,即接下来的数可以随便选。而我们发现由于进行的是位运算,所以区间 [1,j-1] 显然可以覆盖所以可能的值,共 2^{j-1},n 和 m 的取值共 4^{j-1} 种组合,所以每种值有 2^{j-1} 个。同理区间 [j,i-1] 因为 m 这边定死了,所以每种值只有 1 个,但仍然能覆盖到所有可能的数。

于是我们得出了一种可行的做法:枚举 i 和 j,满足 n_i=1,m_i=1,不妨假设 j \le i,可以发现从第 i 位开始全部定死,前面可以选任何数,每种不同的数的 cnt 为 2^{j-1}。只需求 2^j\sum\limits_{i=L}^R d(i)(L、R 具体见代码),显然可以差分然后整除分块,应为本质不同的数量很少所以可以记忆化。

设 suf_i 为后 i 位三个数异或起来的后缀和,那么 L 就是 suf_i,R 则是 suf_i + 2^{i-1}-1,因为显然区间 [1,i-1] 全取 1 就这么大,注意因为在 n_i=1 且 m_i=1,又在此处打破限制,所以实际上计算时 n_i=0,m_i=0,要从后缀和中去掉。

复杂度 O(\log^2 n+\sqrt n \log n)。

#include <bits/stdc++.h>
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#define int long long
using namespace std;

namespace IO{
    char ibuf[(1 << 20) + 1], *iS, *iT;
    #if ONLINE_JUDGE
    #define gh() (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, (1 << 20) + 1, stdin), (iS == iT ? EOF : *iS++) : *iS++)
    #else
    #define gh() getchar()
    #endif
    #define reg register
    inline long long read () {
        reg char ch = gh();
        reg long long x = 0;
        reg char t = 0;
        while (ch < '0' || ch > '9')   t |= ch == '-', ch = gh();
        while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gh();
        return t ? -x : x;
    }
    inline void write(long long x) {
        if (x < 0) {
            x = ~(x - 1);
            putchar('-');
        }
        if (x > 9)
            write(x / 10);
        putchar(x % 10 + '0');
    }
}

using IO::read;
using IO::write;

const int N = 80, mod = 998244353;
int n, m, ct, A, B, X, a[N], b[N], x[N], mi[N], h[N], ans;
__gnu_pbds::gp_hash_table <int, int> sd;

int D (int x) {
    if (!(~x) || !x) return 0;
    if (sd.find(x) != sd.end()) return sd[x];
    int res = 0;
    for (int l = 1, r; l <= x; l = r + 1) {
        r = x / (x / l);
        res = (res + (r - l + 1) % mod * (x / l) % mod) % mod;
    }
    return sd[x] = res;
}

signed main () {
    A = read() + 1, B = read() + 1, X = read();
    for (; A; A >>= 1) a[++n] = A & 1;
    for (; B; B >>= 1) b[++m] = B & 1;
    for (; X; X >>= 1) x[++ct] = X & 1;
    mi[0] = 1; for (int i = 1; i < N; i++) mi[i] = mi[i - 1] * 2 % mod;
    for (int i = max(max(n, m), ct); i; i--) h[i] = h[i + 1] + ((long long)(a[i] ^ b[i] ^ x[i]) << (i - 1));
    for (int i = 1; i <= n; i++) {
        if (!a[i]) continue;
        for (int j = 1; j <= m; j++) {
            if (!b[j]) continue;
            int s = max(i, j), t = min(i, j), H = h[s] ^ (1ll << (s - 1));
            if (s == t) H ^= (1ll << (s - 1));
            ans = (ans + (D(H + (1ll << (s - 1)) - 1) - D(H - 1) + mod) % mod * mi[t - 1] % mod) % mod;
        }
    }
    write(ans);
}