# Quotient-State DP and Ordered Meet-in-the-Middle Problem setter's solution: combine quotient-state DP and ordered meet-in-the-middle according to the parameters. Codex (GPT-6): application and derivation of both methods, the multiple-query specification, modular probability implementation, overflow handling, and verification data. Quotient decomposition and meet-in-the-middle are established techniques, not claimed as original inventions. The preferred program is the self-contained [std.cpp](std.cpp). A [four-list streaming solution](solution-alternative-streaming.en.md) and its [program](alternative-streaming.cpp) trade time for memory. ## 1. Probabilities and preprocessing Let $D=10^6$ , $P=998244353$ , and $a_i=p_iD^{-1}\bmod P$ . Use modular arithmetic for probabilities and exact integer arithmetic for multipliers; the two must remain separate. - Remove swords with $p_i=0$ or $m_i=1$ . - A sword with $p_i=D$ always contributes multiplier $m_i$ . Remove it and replace every threshold by $\lceil K_j/m_i\rceil$ . - A threshold of $1$ always has answer $1$ . The deterministic reduction requires ceiling division. For example, after removing a guaranteed multiplier of $3$ , a threshold of $5$ requires the remaining multiplier to be at least $2$ . Below, $n$ denotes the remaining number of swords. Each remaining sword satisfies $01$ , set $T=K-1$ . Let $F_i(t)$ be the probability that the first $i$ swords produce a multiplier at most the integer $t$ . Splitting on whether sword $i$ triggers gives: $$ F_i(t)=(1-a_i)F_{i-1}(t)+a_iF_{i-1}(\lfloor t/m_i\rfloor) $$ Initially, $F_0(0)=0$ and $F_0(t)=1$ for $t\ge1$ . The answer is $1-F_n(T)$ , with subtraction also performed modulo $P$ . Maintaining every threshold takes $O(nK)$ time. Starting from $T$ , however, every required threshold belongs to: $$ \mathcal Q_T=\{0\}\cup\{\lfloor T/d\rfloor:1\le d\le T\} $$ The closure property follows from: $$ \left\lfloor\frac{\lfloor T/d\rfloor}{m_i}\right\rfloor=\left\lfloor\frac{T}{dm_i}\right\rfloor $$ For $d\le\sqrt T$ , there are only $O(\sqrt T)$ denominators. For $d>\sqrt T$ , the quotient is smaller than $\sqrt T$ . Hence there are only $O(\sqrt K)$ states, and every transition stays within the set. ### Array indexing without hashing or binary search Let $s=\lfloor\sqrt T\rfloor$ and $r=\lfloor T/(s+1)\rfloor$ . Store $0,1,\ldots,s$ in increasing order, followed by the quotients for denominators decreasing from $r$ to $1$ . The index of a nonzero state is: $$ \operatorname{index}(t)= \begin{cases} t,&t\le s,\\ s+r-\lfloor T/t\rfloor+1,&t>s \end{cases} $$ Each transition finds its destination in constant time. Process thresholds in decreasing order for each sword, so smaller thresholds retain their previous-layer values. Only one probability array is needed. Correctness follows by induction. The initial array represents the empty collection. Each transition adds the probabilities of the two mutually exclusive triggering outcomes, and the state set includes every dependency. Thus the final array represents the required cumulative probabilities. Processing all queries separately takes $O(n\sum_j\sqrt{K_j})$ time and $O(\sqrt{K_{\max}})$ extra state space, where $K_{\max}=\max_jK_j$ . ## 3. Few swords: ordered meet-in-the-middle Let $M=\max_jK_j$ . Compute the probability of falling below the threshold, then subtract it from $1$ . Split the swords into two nearly equal groups and enumerate each group's triggering subsets, multipliers, and probability weights. ### A common probability factor Extract the probability that no sword triggers: $$ C=\prod_i(1-a_i),\qquad w_i=\frac{a_i}{1-a_i}=\frac{p_i}{D-p_i}\pmod P $$ A triggering set $S$ has probability $C\prod_{i\in S}w_i$ . During construction, non-triggering keeps the weight unchanged, while triggering multiplies it by $w_i$ . Apply $C$ after combining both halves. Removing certain triggers beforehand makes every denominator invertible. ### Linear total construction time Maintain a list sorted by multiplier. Inserting a sword combines the existing list and the list with each multiplier multiplied by $m_i$ . Both are sorted, so merge them in linear time. Keep only products smaller than $M$ . All multipliers are at least $1$ , so a partial product reaching $M$ can never contribute to any query's failure event. Before merging, only states whose multiplier is at most $\lfloor(M-1)/m_i\rfloor$ may trigger. This both prunes irrelevant states and guarantees that every multiplication actually performed produces a value smaller than $M$ . For a group of $h$ swords, list lengths are at most $1,2,4,\ldots,2^h$ , totaling $O(2^h)$ merge work. The implementation performs just one binary search per level to locate the eligible triggering prefix. Its total $O(h^2)$ cost is also bounded by $O(2^h)$ . There is no binary search for each state and no comparison sort after enumeration. Induction shows that every retained state represents a triggering subset, and every subset with product below $M$ is retained. Different subsets with equal products may remain separate; all their weights count. ### Prefix weights and two pointers Write the lists as $(x_i,u_i)$ and $(y_j,v_j)$ . The failure probability for a query $K$ is: $$ C\sum_i u_i\sum_{j:x_iy_j