# Grox's Sequence II Editorial **Preferred method: prefix sums by exponent residue, a linear algorithm by Codex (GPT-6) for the three requested totals.** The problem setter’s divide-and-conquer NTT method computes all coefficients; its complete approach and complexity are retained below. Since this task needs only three totals, `std.cpp` uses the linear method. ## 1. Retain the information needed by the output Let $f_i=(i!)^{-1}\bmod P$ . A nonnegative pair $(i,j)$ contributes exactly when $i+j\le n$ . Its coefficient is $f_if_j$ and its exponent is $i-j$ . For a fixed $i$ , group the values $f_j$ for $0\le j\le n-i$ by $j\bmod3$ . Define $$H_t(z)=\sum_{\substack{0\le j\le z\\j\equiv t\pmod3}}f_j$$ Then $$A_r=\sum_{i=0}^n f_iH_{(i-r)\bmod3}(n-i)\pmod P$$ Normalize all remainders to $0,1,2$ . ## 2. One scan Compute factorials, obtain $f_n$ by one modular exponentiation, and use $f_{i-1}=if_i$ downwards to obtain every inverse factorial. Scan $i$ from $n$ down to zero. Insert the newly available $j=n-i$ into its residue-class prefix sum, then add the contribution to each of the three answers. Neither all prefix arrays nor the entire Laurent polynomial need to be stored. When processing $i$ , the three running sums cover exactly $j=0,\ldots,n-i$ . Every legal pair is therefore counted once in the correct residue class, and no illegal pair enters the sums. This proves correctness. For $n=0$ , the process yields $1,0,0$ . Time is $O(n+\log P)$ and space is $O(n)$ . With the fixed modulus, exponentiation is constant with respect to $n$ , giving $O(n)$ time. ## 3. Problem setter’s solution: triangular divide and conquer with NTT To compute every coefficient, process the triangle $i+j\le n$ . Set $t=n-j$ , giving $0\le i\le t\le n$ . The corresponding exponent is $i+t-n$ and the coefficient is $f_i f_{n-t}$ . Recursively process pairs $l\le i\le t\le r$ . With midpoint $h$ : 1. Recurse on $l\le i\le t\le h$ . 2. Recurse on $h+1\le i\le t\le r$ . 3. Evaluate the cross rectangle $l\le i\le h