---
id: 2427
lang: en
translation_of: index.md
source_sha256: 53639fad77672f254e66092d86406a191a52fe96ae35923d456dd8b87f1c4202
title: "[Universal OJ] #310. [UNR #2] Chocolate Before Dawn"
description: "Instead of running FWT many times, one FWT of the summed array gives, at each position, the counts of -1 and 3 factors via a linear equation."
tags: ["OI","FWT"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "The source says each position becomes either 3 or 1 and calls the result a sum of a power of -1 and a power of 3; both are kept as written, not corrected."
---

## Main Idea

Basically, this is taking the XOR convolution of the $(1 + 2x^{a_i})$ terms to get the number of ways to make $0$. One idea is to run FWT many times, but that will definitely time out. Let's look at some properties.

Actually, the FWT transform is just:

$$
b_i = \sum_{j} (-1)^{cnt(i \& j)} a_j
$$

<!--more-->

After several FWTs, each position is either $3$ or $1$ (at position $0$, the $1$ contributes $1$ to every position, and at position $a_i$, the $2$ can contribute $2, -2$). So the final result is actually just a sum of a power of $-1$ and a power of $3$. We need to work out these exponents and then do a fast exponentiation.

We can let the exponent of $-1$ be $x$; then the exponent of $3$ is $n - x$. We need to know $x$ at each position, so we can try to find an equation.

One property is that the sum of the FWTs is the FWT of the sum. So we can take the sum and then do FWT, then set up the equation $b_i = -1x + 3(n - x)$; after solving for $x$, the real value after FWT is $c_i = (-1)^x + 3^{n - x}$.

## Code

```cpp
// UOJ310.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 2e6 + 200, mod = 998244353, poly_siz = 1048576;

int n, ai[MAX_N], bi[MAX_N], ci[MAX_N], pw[MAX_N];

int fpow(int bas, int tim)
{
    int ret = 1;
    while (tim)
    {
        if (tim & 1)
            ret = 1LL * ret * bas % mod;
        bas = 1LL * bas * bas % mod;
        tim >>= 1;
    }
    return ret;
}

const int inv2 = fpow(2, mod - 2);

void fwt_xor(int *arr, int dft)
{
    for (int step = 1; step < poly_siz; step <<= 1)
        for (int j = 0; j < poly_siz; j += (step << 1))
            for (int k = j; k < j + step; k++)
            {
                int A = arr[k], B = arr[k + step];
                arr[k] = (0LL + A + B) % mod, arr[k + step] = (0LL + A + mod - B) % mod;
                if (dft == -1)
                    arr[k] = 1LL * arr[k] * inv2 % mod, arr[k + step] = 1LL * arr[k + step] * inv2 % mod;
            }
}

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &ai[i]), bi[0]++, bi[ai[i]] += 2;
    for (int i = pw[0] = 1; i < poly_siz; i++)
        pw[i] = 3LL * pw[i - 1] % mod;
    fwt_xor(bi, 1);
    for (int i = 0; i < poly_siz; i++)
    {
        int x = (3LL * n + mod - bi[i]) % mod * inv2 % mod * inv2 % mod;
        ci[i] = (x & 1) ? (mod - pw[n - x]) % mod : (pw[n - x]);
    }
    fwt_xor(ci, -1), printf("%lld\n", (0LL + ci[0] + mod - 1) % mod);
    return 0;
}
```
