LOJ 3045: Luo Gu P5326: "ZJOI2019" switch

Topic Portal: LOJ # 3043 .

Description of the meaning of problems

slightly.

answer

From the Gaussian elimination seems to need some knowledge set power series, I do not think from this perspective.


\(\displaystyle \dot p = \sum_{i = 1}^{n} p_i\)

We consider a sequence of operations \ (\ {A_1, A_2, \ ldots, a_k \} \) , where \ (. 1 \ Le a_j \ n-Le \) , it represents \ (I \) times the switch is pressed \ ( a_j \) .

Then press \ (K \) After exactly twice the probability to obtain a sequence is \ (\ DisplayStyle \ prod_. 1} = {J ^ {K} ({P_ a_j} / \ DOT P) \) .

If we consider this sequence just been pressed target state \ (S \) :
if and only if for every \ (I \) ( \ (. 1 \ I Le \ n-Le \) ) satisfy the switch is pressed \ (I \) parity is exactly equal to the number \ (S_I \) .
Formally, is that for each \ (I \) have \ (\ DisplayStyle \ left (\ sum_ {J} = ^ {K}. 1 [a_j = I] \ right) \ 2 = S_I BMOD \) .


Then we each \ (i \) are considered separately, for \ (s_i = 0 \) is required by the even number of times, for \ (s_i = 1 \) is required by several odd.

  • For a \ (s_i = 0 \) a \ (I \) , we present this series: \ (F_i = \ {. 1, 0, {(P_i / \ P DOT)} ^ 2, 0, {( P_i / \ P DOT)} ^. 4, 0, {(P_i / \ P DOT)}. 6 ^, 0, \ ldots \} \) .
  • For a \ (s_i = 1 \) a \ (I \) , we present this series: \ (F_i = \ {0, P_i / \ DOT P, 0, {(P_i / \ P DOT)} ^ . 3, 0, {(P_i / \ P DOT)} ^. 5, 0, {(P_i / \ P DOT)}. 7 ^, \ ldots \} \) .

Can be found, all the \ (i \) the number of columns all two convolution up, you get a new series \ (f \) , the series is satisfied:
for \ (f \) first \ (k \) term \ (F_k \) , it means that when the press \ (K \) when the switch happens to get the state \ (S \) probability.

Because it is two convolution , so we write this procedure exponential probability generating function of the form:

Defined \ (\ hat F_i (x) = \ mathbf {EGF} \ left ({\ left \ {[j \ bmod 2 = s_i] {(p_i / \ dot p)} ^ j \ right \}} _ {j = 0} ^ {\ infty} \ right) \) ,
i.e. each \ (I \) corresponding to the number of columns \ (F_i \) exponential generating function,
written as a closed form, is \ (\ displaystyle \ hat f_i (x) = \ frac { e ^ {(p_i / \ dot p) x} + {(-1)} ^ {s_i} e ^ {- (p_i / \ dot p) x}} {2} \) .

Therefore, the resulting \ (F \) of EGF is \ (\ displaystyle \ hat F ( x) = \ prod_ {i = 1} ^ {n} \ frac {e ^ {(p_i / \ dot p) x} + {(-1)} ^ {^ {E} S_I - (P_i / \ P DOT)} X {2}} \) .

It looks very sick, but not finished! What's wrong?


First, we must be clear: to get \ (f \) can be doing?

Found \ (F \) properties is: \ (F_k \) represented by exactly \ (K \) switching operations to obtain status \ (S \) probability, that depending on the desired definition, the answer is \ (\ displaystyle \ sum_ { = {0} ^ I \ I F_i infty} \) .

This is what, is \ (F \) corresponding to the normal generation function \ (\ DisplayStyle F. (X) = \ sum_ {I} = 0 {^ \ infty F_i} X ^ I \) , it \ (1 \ ) derivative at, i.e. \ (F. '(. 1) \) .
(Recalling formal power series derivative, and the definition of the evaluation)

But wrong , then look, the subject of the request is the first to reach the state \ (s \) expected number of steps, rather than what it was now.
(Because it may not be the first time, but had been through many times. In fact, if this request directly, or even not converge)

So how to do it? We find it necessary to exclude first arrived \ (s \) later, and after several steps to return \ (s \) case, that is a return to the original state.

Thus, we consider the calculated number of columns \ (G \) , where \ (g_k \) indicates \ (k \) after the step just returns the probability that the original state.

It can be found, if made final answer number as \ (H \) , there \ (H \ AST G = f \) ( \ (H \) volume \ (g \) is equal to \ (f \) , is the normal volume product is not a two convolution).

The \ (g \) how to obtain it? In fact, when all \ (s_i = 0 \) when \ (f \) you, because it is the thing to return to the original state.


It says a bunch of things in theory, we now consider how to implement.

First discovered when demand is EGF, but when the operator answers that OGF, this strange. We look see if you can convert the form.

For \ (\ Hat F. \) , Has the form \ (\ displaystyle \ hat F ( x) = \ prod_ {i = 1} ^ {n} \ frac {e ^ {(p_i / \ dot p) x} + { (-1)} ^ {^ {E} S_I - (P_i / \ P DOT)} X {2}} \) .

We each of the form \ (a_w e ^ {(w / \ dot p) x} \) is regarded as an equation, it can be found in the final \ (W \) values in \ ([- \ dot p , \ the p-DOT] \) .

Therefore, the \ (\ hat F (x) \) expressed as \ (\ displaystyle \ sum_ {w = - \ dot p} ^ {\ dot p} a_w e ^ {(w / \ dot p) x} \) of after the form, we have \ (\ DisplayStyle F_k = \ sum_ {W = - \ DOT P ^ {} \} P DOT a_w {(W / \ P DOT)} ^ K \) .

This form then converted into OGF, to give \ (\ displaystyle \ mathbf {OGF } (f) = F (x) = \ sum_ {w = - \ dot p} ^ {\ dot p} \ frac {a_w} {1 - (W / \ P DOT)} X \) .

This calculated time to consider each \ (a_w \) , can be found on the line to make a backpack, complexity is \ (\ mathcal O (the n-\ the p-DOT) \) .
(Transfer coefficient is observed backpack \ (\. 1 PM / 2 \) , the polynomial may be used to optimize Exp \ (\ mathcal O (n-+ \ DOT P \ log \ DOT P) \) , but not necessary)

For \ (\ displaystyle \ mathbf {OGF } (g) = G (x) = \ sum_ {w = - \ dot p} ^ {\ dot p} \ frac {b_w} {1 - (w / \ dot p) x} \) Similarly, we need to find every \ (B_W \) .

Find all \ (a_w, b_w \) after we mastered \ (f, g \) some properties, and then to answer \ (H \) , so that it is the ordinary generating function \ (H \) .

According to the above explanation, there are \ (H = F. / G \) , and finally we need to find \ (H '(. 1) \) .

Because there \ (F, G, H \ ) may have unlimited entries, so to be considered by \ (a_w, b_w \) to find the answer.

Consider dividing derivation method: \ (\ DisplayStyle H '= {(F. / G)}' = \ {F'G FRAC - G'F G ^ {2}} \) .

So long as the determined \ (F (1), G (1), F '(1), G' (1) \) can.

However Unfortunately, we found that because of \ (\ displaystyle \ frac {a _ {\ dot p}} {1 - (\ dot p / \ dot p) x} \) this one, so \ (F, G, F ', G' \) in \ (x = 1 \) at the non-convergence.

We know the answer to some convergence, so consider Luo Luo can do a bit of transformation: the \ (F \) and \ (G \) multiplied by the \ ((1 - the X-) \) . Then there is:

  • \(F(1) = a_{\dot p}\)
  • \(\displaystyle F'(1) = \sum_{w = -\dot p}^{\dot p - 1} \frac{a_w}{w / \dot p - 1}\)
  • \(G(1) = b_{\dot p}\)
  • \(\displaystyle G'(1) = \sum_{w = -\dot p}^{\dot p - 1} \frac{b_w}{w / \dot p - 1}\)

The calculation process is omitted, is calculated according to the equation derivation of it. and so:

\[\begin{aligned} H'(1) &= \frac{F'(1) G(1) - G'(1) F(1)}{G^2(1)} \\ &= \frac{\displaystyle \left( \sum_{w = -\dot p}^{\dot p - 1} \frac{a_w}{w / \dot p - 1} \right) b_{\dot p} - \left( \sum_{w = -\dot p}^{\dot p - 1} \frac{b_w}{w / \dot p - 1} \right) a_{\dot p}}{b_{\dot p}^2} \\ &= \sum_{w = -\dot p}^{\dot p} \frac{a_w b_{\dot p} - b_w a_{\dot p}}{(w / \dot p - 1) b_{\dot p}^2} \end{aligned} \]

Determine all \ (a_w, b_w \) after the calculation according to this formula, the time complexity is \ (\ mathcal O (n-\ DOT + P \ P DOT \ log MOD) \) , code is as follows:

#include <cstdio>
#include <algorithm>

typedef long long LL;
const int Mod = 998244353, Inv2 = (Mod + 1) / 2;
const int MN = 105, MP = 50005;

inline void Add(int &x, LL y) { x = (x + y) % Mod; }
inline int qPow(int b, int e) {
	int a = 1;
	for (; e; e >>= 1, b = (LL)b * b % Mod)
		if (e & 1) a = (LL)a * b % Mod;
	return a;
}
inline int gInv(int b) { return qPow(b, Mod - 2); }

int N, s[MN], p[MN], sump;
int _a[2][MP * 2], _b[2][MP * 2], *a[2] = {_a[0] + MP, _a[1] + MP}, *b[2] = {_b[0] + MP, _b[1] + MP};
int Ans;

int main() {
	scanf("%d", &N);
	for (int i = 1; i <= N; ++i) scanf("%d", &s[i]), s[i] = s[i] ? -1 : 1;
	b[0][0] = a[0][0] = 1;
	for (int i = 1; i <= N; ++i) {
		scanf("%d", &p[i]);
		for (int j = -sump - p[i]; j <= sump + p[i]; ++j) b[1][j] = a[1][j] = 0;
		for (int j = -sump; j <= sump; ++j)
			Add(a[1][j + p[i]], (LL)Inv2 * a[0][j]),
			Add(a[1][j - p[i]], s[i] * (LL)Inv2 * a[0][j]),
			Add(b[1][j + p[i]], (LL)Inv2 * b[0][j]),
			Add(b[1][j - p[i]], (LL)Inv2 * b[0][j]);
		sump += p[i];
		std::swap(a[0], a[1]), std::swap(b[0], b[1]);
	}
	int isump = gInv(sump), *A = a[0], *B = b[0];
	for (int j = -sump; j < sump; ++j)
		Add(Ans, ((LL)A[j] * B[sump] - (LL)B[j] * A[sump]) % Mod * gInv((LL)j * isump % Mod - 1));
	Ans = (LL)Ans * qPow(B[sump], Mod - 3) % Mod;
	printf("%d\n", (Ans + Mod) % Mod);
	return 0;
}

Guess you like

Origin www.cnblogs.com/PinkRabbit/p/ZJOI2019D2T1.html