LOJ 3160: "NOI2019 'main fighting ground

Topic Portal: LOJ # 3160 .

Briefly meaning of the questions:

Has a length \ (n-\) sequence \ (A \) , the initial \ (a_i = i \) or \ (= a_i I ^ 2 \) , depending on the \ (\ mathrm {type} \ ) of value.

This sequence \ (m \) operations, each operation given a value \ (A_i \) , this sequence is divided into two parts: \ (A [. 1: A_i] \) and \ (a [A_i + . 1: n-] \) , then the limit without changing the order of two opposing internal sequence, the two sequences are uniformly mixed to form a new sequence, the new sequence \ (a \) is the mixture of the the new sequence.

\ (Q \) times after this query \ (m \) After the operations, the value of a certain position \ (a_ {c_i} \) desired.

answer:

Because it is uniform mixing, so all \ (\ displaystyle binom {n} {A_i} \ \) admixture, the probability of occurrence of all the ways are equal.

First make a \ (30 \) min \ (\ mathcal {O} ( m \ cdot n ^ 2) \) violent, it can be found in the sample or observation: After the operation done any number of times, the sequence \ (\ mathbb { E} [a_i] \) is still a function or a quadratic function.

This conclusion can be so emotional understanding:

  • First, the initial function is a quadratic function or just to prove a linear function or quadratic function after a number of operations to remain unchanged.
  • Consider \ (\ mathbb {E} [a_i] = F (I) \) , where \ (f (x) \) is about \ (X \) is a linear function or a quadratic function.
  • After a given value \ (K \) after the operation, the left section \ (I \) term is equal to \ (F (I) \) , the right side of \ (I \) term is equal to \ (f (k I +) \) , both sides are the same number of functions.
  • Sequences of the two linear function or quadratic function of the form corresponding to the function of uniformly mixing, the number should not be increased, but since the same symbols highest order coefficient, the highest order term will not be canceled, so that the number does not decrease.

Just then each of the first three values ​​can be obtained, only the first three may be left by the first three sequences (which has been obtained) and the right of the first three sequences (obtained require interpolation) combination, as long as the push Some simple formula you can find the new previous value of the three.

The following is the code, the complexity of the \ (\ mathcal {O} (m) \) :

#include <cstdio>

typedef long long LL;
const int Mod = 998244353;
const int Inv2 = (Mod + 1) / 2;

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 x) { return qPow(x, Mod - 2); }

int N, M, A, Q, Typ;
LL iN0, iN1, iN2;
int E1, E2, E3;
inline int GetX(int i) {
    if (i == 1) return E1;
    if (i == 2) return E2;
    if (i == 3) return E3;
    int SE1 = (LL)E1 * (i - 2) % Mod * (i - 3) % Mod;
    int SE2 = (LL)E2 * (2 - i - i + Mod) % Mod * (i - 3) % Mod;
    int SE3 = (LL)E3 * (i - 1) % Mod * (i - 2) % Mod;
    return ((LL)SE1 + SE2 + SE3) * Inv2 % Mod;
}

int main() {
    freopen("landlords.in", "r", stdin);
    freopen("landlords.out", "w", stdout);
    scanf("%d%d%d", &N, &M, &Typ), --Typ;
    iN0 = gInv(N), iN1 = gInv((LL)N * (N - 1) % Mod), iN2 = gInv((LL)N * (N - 1) % Mod * (N - 2) % Mod);
    E1 = 1, E2 = Typ ? 4 : 2, E3 = Typ ? 9 : 3;
    while (M--) {
        scanf("%d", &A);
        LL F1 = E1, F2 = E2, F3 = E3;
        LL F4 = GetX(A + 1), F5 = GetX(A + 2), F6 = GetX(A + 3);
        E1 = (F1 * A + F4 * (N - A)) % Mod * iN0 % Mod;
        E2 = (F2 * A % Mod * (A - 1) + (F1 + F4) * A % Mod * (N - A) + F5 * (N - A) % Mod * (N - A - 1)) % Mod * iN1 % Mod;
        E3 = (F3 * A % Mod * (A - 1) % Mod * (A - 2) + (F4 + F2 + F2) * A % Mod * (A - 1) % Mod * (N - A) + (F5 + F5 + F1) * A % Mod * (N - A) % Mod * (N - A - 1) % Mod + F6 * (N - A) % Mod * (N - A - 1) % Mod * (N - A - 2)) % Mod * iN2 % Mod;
    }
    scanf("%d", &Q);
    for (int X; Q--; ) {
        scanf("%d", &X);
        printf("%d\n", GetX(X));
    }
    return 0;
}

Guess you like

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