|
| 1 | +## Explanation |
| 2 | + |
| 3 | +### Strategy |
| 4 | + |
| 5 | +**Constraints & Edge Cases** |
| 6 | +We are dealing with string lengths ($n$) and query counts ($q$) up to 100,000. |
| 7 | + |
| 8 | + * **Time Complexity:** A solution that loops through the substring for every query will be $O(n \cdot q)$, which is far too slow. We need an $O(1)$ solution per query. |
| 9 | + * **Large Numbers:** The resulting numbers can be massive. We must apply modulo $10^9 + 7$ at every addition or multiplication step. |
| 10 | + * **Zeros:** The problem asks us to ignore zeros. If a range contains only zeros, our logic needs to handle returning 0 gracefully. |
| 11 | + |
| 12 | +**High-level approach** |
| 13 | +We will use the **Prefix Sum** technique, adapted for string concatenation. Instead of just summing numbers, we will maintain a "rolling hash" or "prefix value" that represents the integer formed by the string up to index $i$. To get a specific substring's value, we take the total value up to the end of the range and mathematically "subtract" the prefix before the range. |
| 14 | + |
| 15 | +**Brute force vs. optimized strategy** |
| 16 | + |
| 17 | + * **Brute Force:** For each query, slice the string `s[l:r+1]`, filter out '0', parse to int, and multiply. This involves heavy string manipulation inside a loop. |
| 18 | + * **Optimized:** Precompute three arrays: one for the concatenated value, one for the count of non-zero digits, and one for the simple sum of digits. We can then solve any query instantly using modular arithmetic. |
| 19 | + |
| 20 | +**Decomposition** |
| 21 | + |
| 22 | +1. **Map the Data:** Create prefix arrays that track the state of the string (value, count, sum) at every index. |
| 23 | +2. **Precompute Powers:** Calculate powers of 10 beforehand so we can "shift" numbers left efficiently. |
| 24 | +3. **Query Logic:** For each query, use the prefix arrays to isolate the target number and calculate the final result. |
| 25 | + |
| 26 | +### Steps |
| 27 | + |
| 28 | +1. **Initialize Prefix Arrays** |
| 29 | + We need three arrays of size $N+1$. We use $N+1$ to make 1-based indexing easier (index $i$ represents the state *after* processing character $i-1$). |
| 30 | + |
| 31 | + * `p_val`: The integer value of all non-zero digits concatenated so far. |
| 32 | + * `p_cnt`: The total count of non-zero digits encountered so far. |
| 33 | + * `p_sum`: The simple arithmetic sum of non-zero digits so far. |
| 34 | + |
| 35 | +2. **Build the Prefixes** |
| 36 | + Iterate through the string `s`. |
| 37 | + |
| 38 | + * If the character is `'0'`, the state doesn't change. Copy the values from index $i$ to $i+1$. |
| 39 | + * If it's a non-zero digit $d$: |
| 40 | + * Update sum: `new_sum = old_sum + d`. |
| 41 | + * Update count: `new_cnt = old_count + 1`. |
| 42 | + * Update value: Append the digit by shifting the old value left (multiply by 10) and adding $d$. Formula: `(old_val * 10 + d) % MOD`. |
| 43 | + |
| 44 | +3. **Precompute Powers of 10** |
| 45 | + To perform the "subtraction" logic later, we need to know what $10^k$ is for any $k$. Since we are working with modular arithmetic, we cannot just use `10 ** k` inside the loop (it's too slow). We precompute an array `pow10` where `pow10[k]` holds $10^k \pmod M$. |
| 46 | + |
| 47 | +4. **Calculate Query Results** |
| 48 | + For each query `[l, r]`: |
| 49 | + |
| 50 | + * **Step A (Simple Stats):** Calculate the number of non-zero digits (`cnt`) and their sum (`d_sum`) by subtracting the value at index $l$ from the value at index $r+1$. |
| 51 | + * **Step B (Extract Number):** We have the full prefix ending at `r` (`full`) and the prefix ending before `l` (`prev`). To isolate the middle part, we must subtract `prev` from `full`. |
| 52 | + * *Crucial Logic:* To align them correctly, `prev` must be shifted left by `cnt` positions. |
| 53 | + * Formula: `target = (full - (prev * pow10[cnt])) % MOD`. |
| 54 | + * **Step C:** Multiply `target` by `d_sum` and store it in `res`. |
0 commit comments