arXiv:2609.30992cs.DSSep 2026Cited by 1 paper on PaperVantage

Manuscript Offprint

arXiv:2609.30992

Practical Deterministic Linear-Time Modular Subset Sum

Phuoc Dinh Le, Kha Le

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

We give a deterministic algorithm for exact modular subset sum that, for every modulus mm, computes all reachable residues and one requested witness in O(m)\mathcal{O}(m) time and O(m)\mathcal{O}(m) auxiliary words. The input is a compact list of distinct residues with multiplicities, and the word-RAM supports constant-time modular arithmetic.

The algorithm represents reachable residues as intervals along cycles of repeated addition, charging work on partial cycles to newly reached residues. Processing prime factors in increasing order keeps the cost of rebuilding and changing cycles linear. A classical theorem on subset sums of distinct invertible residues bounds the number of boundary lists by O(m3/4)\mathcal{O}(m^{3/4}); together they contain O(m)\mathcal{O}(m) interval endpoints. Comparison sorting the short lists and radix sorting the long ones then takes O(m)\mathcal{O}(m) total time. The algorithm is fast in practice, using arrays and interval lists rather than heavy data structures.

1 Introduction and main result

Given a multiset of residues in Zm=Z/mZ\mathbb{Z}_m = \mathbb{Z}/m\mathbb{Z}, exact modular subset sum asks which residues are reachable as sums of subsets of the input, using each copy at most once and allowing the empty subset. We use the compact input convention of Potepa [Pot21]: at most mm records (x,cx)(x, c_x), where the residues xx are distinct and cxc_x is the nonnegative number of available copies of xx. For an explicit list of all copies, the bound also includes the time needed to read the list.

Theorem 1.1. For every m≥1m \geq 1, exact modular subset sum on compact input can be solved deterministically in O(m)\mathcal{O}(m) time and O(m)\mathcal{O}(m) auxiliary words under the arithmetic model of section 1. The algorithm returns the Boolean array of all reachable residues. Within the same time and space bounds, it also answers a query for a target tt: it reports that tt is unreachable or returns witness counts bxb_x satisfying

0≤bx≤cx,∑xbxx≡t(modm),∑xbx≤m−1.0\leq b_{x}\leq c_{x},\qquad\sum_{x}b_{x}x\equiv t\pmod{m},\qquad\sum_{x}b_{x}\leq m-1.

The witness may instead identify each selected copy by its input record and its copy number within that record.

Corollary 1.2. For an explicit, rereadable list of n reduced residues, the time is O(n+m)\mathcal{O}(n + m) and the auxiliary space is O(m)\mathcal{O}(m) words, excluding the original input storage. This includes one requested witness using distinct original list indices.

Prior work and attribution. [ABJ +^{+}19] give a randomized near-linear algorithm by accelerating Bellman's recurrence with linear sketches. Its O~(m)\widetilde{O}(m) bound hides logarithmic factors: the explicit bound is O(mlog⁡7m)\mathcal{O}(m\log^7 m) [ABB +^{+}21]. Potépa [Pot21, Theorem 2] gives deterministic O(mlog⁡mα(m))\mathcal{O}(m\log m\alpha(m)) time and O(m)\mathcal{O}(m) space for all attainable sums on compact input, where α\alpha is the inverse Ackermann function. Cardinal and Iacono give an expected O(mlog⁡m)\mathcal{O}(m\log m) algorithm; we use the expected-time statement of their revised full version [CI21]. [ABB +^{+}21] give further algorithms through dynamic strings, including a deterministic bound that depends on the number of reachable residues.

Grouping residues by divisors also appears in the work of Koiliaris and Xu [KX19]. Recording a parent when a residue is first reached is used to construct witnesses in earlier modular algorithms [CI21]. The theorem on subset sums of distinct invertible residues used here is due to DeVos, Goddyn, Mohar, and Šámal [DGMŠ07], following Vu [Vu05]. Our analysis combines these ingredients with classical comparison and radix sorting to bound the cost of maintaining cyclic intervals and ordering their endpoints. From our review of the literature, this is the first deterministic O(m)\mathcal{O}(m)-time algorithm for unrestricted exact modular subset sum on compact input over arbitrary moduli.

Model and input conventions. We work with residues in [0,m)[0, m) on a word-RAM supporting constant-time comparisons, bit operations, array access, and arithmetic, including integer quotient, remainder, and modular multiplication. Compact records and intermediate products occupy a constant number of words. For nonzero records we use the safe cap c^x=min⁡{cx,m/gcd⁡(x,m)−1}\widehat{c}_x = \min\{c_x, m / \gcd(x, m) - 1\}; zero records are discarded (theorem 6.1).

Organization. We begin with the Bellman update and a single cycle at prime modulus (section 2), then extend the same operations to several cycles (section 3). The remaining costs have three bounds:

run scans⏟new residues+cycle changes⏟prime−ordered stages+boundary sorting⏟few batches=O(m).\underbrace{\mathrm{r u n~s c a n s}}_{\mathrm{n e w~r e s i d u e s}}+\underbrace{\mathrm{c y c l e~c h a n g e s}}_{\mathrm{p r i m e-o r d e r e d~s t a g e s}}+\underbrace{\mathrm{b o u n d a r y~s o r t i n g}}_{\mathrm{f e w~b a t c h e s}}=\mathcal{O}(m).

Sections 4 and 5 analyze stages and sorting; sections 6 and 7 give preprocessing and witness reconstruction.