"ZJOI2019" switch (the probability of expected + FWT)

Address

Luogu # 5326

BIG # 3045

Solution

First, the target state and the initial state exchange, the answer is clearly unchanged.

Consider shaped pressure \ (\ DP {text} \) , now referred to \ (F [S] \) represents the initial state \ (S \) , the desired number of steps to reach the target state.

Next, assume \ (\ sum_ {I} = ^. 1. 1 np_i = \) , if the input is not satisfied, the \ (P_i \) all divided by \ (\ sum p_i \) can.

Then there is: \ [F. [\ EmptySet] = 0 \] \ [S \ NE \ emptySet, F. [S] = \ sum_ip_i × (F. [S \ Oplus \ left \ {I \ right \}] +. 1) \ ] where \ (\ Oplus \) represents an exclusive oR, \ (\ left \ {I \ right \} \) represents an only \ (I \) set this element.

Note \ (G [\ left \ {I \ right \}] = P_i \) , when the \ (| S | \ ne 1 \) when, \ (G [S] = 0 \) .

Exclusive or convolutional define two arrays: If \ (C = A \ B Times \) , then there is: \ [\ FORALL S, C [S] = \ sum_U \ sum_v [the U-\ Oplus V = S] A [ U] × B [V] \ ]

Definition of the dot product of two arrays: If \ (C = A \ B CDOT \) , then there is \ [\ forall S, C [ S] = A [S] \ times B [S] \]

Define two arrays together: if \ (C = A + B \) , then there is \ [\ forall S, C [ S] = A [S] + B [S] \]

Referred to a full \ (1 \) array \ (H \) , i.e., \ (\ S FORALL, H [S] = 1 \) .

Definition array \ (R & lt \) , where \ (R & lt [\ emptySet] = C \) , \ (\ S FORALL \ NE \ emptySet, R & lt [S] = 0 \) . \ (c \) value is not yet known, first with the letter.

It can be obtained: \ [F. = H + F. × G + R & lt \] \ (R & lt \) occurring due to \ (S = \ emptyset \) and \ (S \ ne \ emptyset \ ) when, \ (F. [ S] \) is not the same recursive formula.

Define transfer \ (\ tilde are C \) (this thing is \ (the FWT \) conversion, will have consciously skipped), where \ [\ tilde C [S] = \ sum_T (-1) ^ {| S \ cap T |} C [T] \ ]

We can get \ [C [S] = \ frac {1} {2 ^ n} \ sum_T (-1) ^ {| S \ cap T |} \ tilde C [T] \]

Wherein \ (n-\) is \ (S \) binary bits, proof of formula considerations.

Demonstrated equivalent: \ [C [S] = \ {FRAC. 1} ^ {n-2} \ sum_A \ sum_B (-1) ^ {| S \ CAP A |} (-. 1) ^ {| A \ CAP B | } C [B] \]

When \ (S = B \) , the formula into \ [C [S] = \ frac {1} {2 ^ n} \ sum_A (-1) ^ {2 | S \ cap A |} C [B ] \]

\[C[S]=\frac{1}{2^n}\sum_AC[B]\]

It is clearly established.

When \ (S \ ne B \) , we only grasp just a \ (S, B \) elements appear in one of which \ (I \) . For a set of \ (A '\) , if the \ (I \ notin A' \) , then the \ (A = A '\) and \ (A = A' + \ left \ {i \ right \} \) contribution number opposite to each other, because: \ [(-. 1) ^ {| S \ CAP a '|} (-. 1) ^ {| a' \ CAP B |} = - (-. 1) ^ {| S \ cap (A '+ \ left \ {i \ right \}) |} (- 1) ^ {| (A' + \ left \ {i \ right \}) \ cap B |} \]

And such \ (A '\) exactly \ (2 ^ {n-1 } \) a, that is to say all \ (A \) may be paired off all contribute canceled, then the \ (S \ ne B \) when there is: \ [\ {FRAC. 1} ^ {n-2} \ sum_A (-1) ^ {| S \ CAP a |} (-. 1) ^ {| a \ CAP B |} = 0 \ ]

QED.

There nature: if \ (C = A \ B Times \) , then the \ (\ tilde are C = \ A tilde are \ CDOT \ tilde are B \) , demonstrated as follows:

\[C[S]=\sum_L\sum_R[L\oplus R\oplus S=0]A[L]B[R]\]

Since \ (\ sum_T (-1) ^ {| S \ CAP T |} ^ n-2 = [S = \ emptySet] \) , so that:

\[C[S]=\frac{1}{2^n}\sum_L\sum_R\sum_T(-1)^{|T\cap(L\oplus R\oplus S)|}A[L]B[R]\]

Then, apparently \ [T \ cap (L \ oplus R \ oplus S) = (T \ cap L) \ oplus (T \ cap R) \ oplus (T \ cap S) \]

Then \ [(- 1) ^ { | T \ cap (L \ oplus R \ oplus S) |} = (- 1) ^ {| T \ cap L | + | T \ cap R | + | T \ cap S |} \]

Formula can be understood as follows: If \ (Z = X \ Oplus Y \) , then the \ (| Z | \ &. 1 = (| X | + | Y |) \ &. 1 \) , since the exclusive OR corresponding to binary not carry adder, so \ (x \ oplus y \) when, \ (X, Y \) in binary \ (1 \) will disappear with twenty-two, \ (1 \) parity does not become the total number of .

and so

\[C[S]=\frac{1}{2^n}\sum_L\sum_R\sum_T(-1)^{|T\cap L|}(-1)^{|T\cap R|}(-1)^{|T\cap S|}A[L]B[R]\]

\[C[S]=(\frac{1}{2^n}\sum_T(-1)^{|T\cap S|})(\sum_L(-1)^{|T\cap L|}A[L])(\sum_R(-1)^{|T\cap R|}B[R])\]

\[C[S]=(\frac{1}{2^n}\sum_T(-1)^{|T\cap S|})\tilde A[T]\times\tilde B[T]\]

The \ [C [S] = \ frac {1} {2 ^ n} \ sum_T (-1) ^ {| S \ cap T |} \ tilde C [T] \]

可得 \[\tilde C[T]=\tilde A[T]\times\tilde B[T]\]

QED.

Let us return to: \ [× F. F. = G + H + R & lt \]

Transposition, to give \ [F (1-G) = H + R \]

\(A=F(1-G)\),则 \[\tilde A[U]=\tilde F[U]×(1-\tilde G[U])\]

\(A=H+R\),则 \[\tilde A[U]=\sum_T(-1)^{|T\cap U|}+c\]

于是 \[\tilde F[U]×(1-\tilde G[U])=\sum_T(-1)^{|T\cap U|}+c\]

When \ (U = \ emptyset \) when there is: \ [\ tilde are G [the U-] = \ = SUM P_i. 1 \]

Into the above equation can be obtained \ (n-C = -2 ^ \) .

When \ (U \ ne \ emptyset \ ) when there is: \ [\ tilde are G [the U-] = \ sum_ {I} (-. 1) ^ {[i∈U] P_i} \]

At this time, \ (\ tilde are G [the U-] <. 1 \) , into the above equation can be obtained:

\[\tilde F[U]=\frac{c}{1-\tilde G[U]}=-\frac{2^n}{1-\tilde G[U]}\]

The expression \ [\ sum_T (-1) ^ {| S \ cap T |} = 2 ^ n [S = \ emptyset] \]

可得 \[\sum_T\tilde F[T]=2^nF[\emptyset]=0\]

那么 \[\tilde F[\emptyset]=-\sum_{T\ne \emptyset}\tilde F[T]=\sum_{T\ne \emptyset}\frac{2^n}{1-\tilde G[T]}\]

Can now be made \ (\ tilde F \) back \ (F. \) A, i.e. \ [F [S] = \ frac {1} {2 ^ n} \ sum_T (-1) ^ {| S \ cap T |} \ tilde F [T] \]

The above \ (\ tilde F \) expression, available \ [F [S] = \ sum_ {T} (1 - (- 1) ^ {| S \ cap T |}) \ frac {1} { 1- \ tilde G [T]} \]

The \ (\ tilde G \) expression can be obtained \ [1- \ tilde G [T ] = \ sum_ {i∈T} 2 × p_i \]

那么 \[F[S]=\sum_{T}(|S\cap T|\&1)\frac{1}{\sum_{i∈T}p_i}\]

Note \ (dp [i] [j ] [k] \) represents the former \ (I \) subset number, and \ (S \) intersection size parity is \ (K \) , a subset \ (P \) sum of \ (J \) , the number of subsets to satisfy these conditions.

Then \ [ans = \ sum_j \ frac {dp [n] [j] [1]} {j} \]

However, if the \ (P_i \) all divided by \ (\ sum_ = {I}. 1 np_i ^ \) , \ (P_i \) is not an integer, not \ (DP \) . So do not start the \ (p_i \) divided by \ (\ SUM P_i \) , direct \ (dp \) . Finally the answer multiplied by \ (\ sum p_i \) can be.

Time complexity \ (\ mathcal O (n-\ SUM P_i) \) .

Code

#include <bits/stdc++.h>

using namespace std;

#define ll long long

template <class t>
inline void read(t & res)
{
    char ch;
    while (ch = getchar(), !isdigit(ch));
    res = ch ^ 48;
    while (ch = getchar(), isdigit(ch))
    res = res * 10 + (ch ^ 48);
}

const int e = 105, o = 5e4 + 5, mod = 998244353;

int n, s[e], p[e], sum, inv[o], f[e][o][2], ans;

inline void add(int &x, int y)
{
    (x += y) >= mod && (x -= mod);
}

int main()
{
    read(n);
    int i, j;
    for (i = 1; i <= n; i++) read(s[i]);
    for (i = 1; i <= n; i++) read(p[i]), sum += p[i];
    inv[1] = 1;
    for (i = 2; i <= sum; i++) inv[i] = (ll)(mod - mod / i) * inv[mod % i] % mod;
    f[0][0][0] = 1;
    for (i = 1; i <= n; i++)
    for (j = 0; j <= sum; j++)
    {
        f[i][j][0] = f[i - 1][j][0];
        f[i][j][1] = f[i - 1][j][1];
        if (j >= p[i])
        {
            add(f[i][j][0], f[i - 1][j - p[i]][s[i]]);
            add(f[i][j][1], f[i - 1][j - p[i]][s[i] ^ 1]);
        }
    }
    for (i = 1; i <= sum; i++) ans = (ans + (ll)inv[i] * f[n][i][1]) % mod;
    ans = (ll)ans * sum % mod; 
    cout << ans << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/cyf32768/p/12235774.html