From be8d6eacd2a3d62060dc33a3ad58d2b5bb2d91b6 Mon Sep 17 00:00:00 2001 From: Debashis Nandi Date: Thu, 9 Jul 2026 01:32:39 +0530 Subject: [PATCH] feat: POTD 2026-07-08 solved --- src/3700-3799/3756.cpp | 145 +++++++++++++++++++++++++++++++++++++++ tests/3700-3799/3756.txt | 26 +++++++ 2 files changed, 171 insertions(+) create mode 100644 src/3700-3799/3756.cpp create mode 100644 tests/3700-3799/3756.txt diff --git a/src/3700-3799/3756.cpp b/src/3700-3799/3756.cpp new file mode 100644 index 0000000..4a27e2c --- /dev/null +++ b/src/3700-3799/3756.cpp @@ -0,0 +1,145 @@ +/* +Tags +level-medium +math, string, prefix-sum + +Problem Description +3756. Concatenate Non-Zero Digits and Multiply by Sum II + +You are given a string s of length m consisting of digits. You are also given a 2D integer array queries, where queries[i] = [li, ri]. +For each queries[i], extract the substring s[li..ri]. Then, perform the following: +Form a new integer x by concatenating all the non-zero digits from the substring in their original order. If there are no non-zero digits, x = 0. +Let sum be the sum of digits in x. The answer is x * sum. +Return an array of integers answer where answer[i] is the answer to the ith query. +Since the answers may be very large, return them modulo 109 + 7. + +Example 1: +Input: s = "10203004", queries = [[0,7],[1,3],[4,6]] +Output: [12340, 4, 9] +Explanation: +s[0..7] = "10203004" +x = 1234 +sum = 1 + 2 + 3 + 4 = 10 +Therefore, answer is 1234 * 10 = 12340. +s[1..3] = "020" +x = 2 +sum = 2 +Therefore, the answer is 2 * 2 = 4. +s[4..6] = "300" +x = 3 +sum = 3 +Therefore, the answer is 3 * 3 = 9. + +Example 2: +Input: s = "1000", queries = [[0,3],[1,1]] +Output: [1, 0] +Explanation: +s[0..3] = "1000" +x = 1 +sum = 1 +Therefore, the answer is 1 * 1 = 1. +s[1..1] = "0" +x = 0 +sum = 0 +Therefore, the answer is 0 * 0 = 0. + +Example 3: +Input: s = "9876543210", queries = [[0,9]] +Output: [444444137] +Explanation: +s[0..9] = "9876543210" +x = 987654321 +sum = 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 = 45 +Therefore, the answer is 987654321 * 45 = 44444444445. +We return 44444444445 modulo (109 + 7) = 444444137. + +Constraints: +1 <= m == s.length <= 1^05 +s consists of digits only. +1 <= queries.length <= 10^5 +queries[i] = [li, ri] +0 <= li <= ri < m +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../core/core.h" +using namespace std; + +const int mod = 1e9 + 7; +const int maxN = 100001; +long long pow10[maxN]; + +int init = []() + { + pow10[0] = 1; + for (int i = 1; i < maxN; i++) + { + pow10[i] = (pow10[i - 1] * 10) % mod; + } + return 0; + }(); + +class Solution +{ +public: + vector sumAndMultiply(string s, vector>& queries) + { + int n = s.size(); + vector sum(n + 1, 0); + vector x(n + 1, 0); + vector count(n + 1, 0); + for (int i = 0; i < n; i++) + { + int d = s[i] - '0'; + sum[i + 1] = sum[i] + d; + x[i + 1] = (d > 0) ? (x[i] * 10 + d) % mod : x[i]; + count[i + 1] = count[i] + (d > 0); + } + int m = queries.size(); + vector result(m, 0); + for (int i = 0; i < m; i++) + { + int l = queries[i][0]; + int r = queries[i][1] + 1; + int length = count[r] - count[l]; + long long valX = (x[r] - x[l] * pow10[length] % mod + mod) % mod; + long long valSum = sum[r] - sum[l]; + result[i] = (valX * valSum) % mod; + } + + return result; + } +}; + +int main() +{ + int m; + string s; + cin >> s >> m; + vector> queries(m, vector(2, 0)); + for (int i = 0; i < m; i++) + { + cin >> queries[i][0] >> queries[i][1]; + } + Solution sol; + vector result = sol.sumAndMultiply(s, queries); + Core::printVectorResult(result); + return 0; +} \ No newline at end of file diff --git a/tests/3700-3799/3756.txt b/tests/3700-3799/3756.txt new file mode 100644 index 0000000..a5e3bbd --- /dev/null +++ b/tests/3700-3799/3756.txt @@ -0,0 +1,26 @@ +Test Case 1: +Input: +10203004 +3 +0 7 +1 3 +4 6 +Output: +12340 4 9 + +Test Case 2: +Input: +1000 +2 +0 3 +1 1 +Output: +1 0 + +Test Case 3: +Input: +9876543210 +1 +0 9 +Output: +444444137 \ No newline at end of file