Optimising Prefix Sum

I have a question: Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in the array.

Given some queries in the form (a,b,k):

((1,5,3),(4,8,7),(6,9,1))

Add the values of k between the indices a and b inclusive:

(0,0,0, 0, 0,0,0,0,0, 0)
(3,3,3, 3, 3,0,0,0,0, 0)
(3,3,3,10,10,7,7,7,0, 0)
(3,3,3,10,10,8,8,8,1, 0)

And then return the max value which is 10

My attempt is:

from itertools import accumulate

def Operations(size, Array):

    values = (0) * (size+2)
    for a,b,k in Array:
        values(a) += k
        values(b+1) -= k

    values = list(accumulate(values))

    Result = max(values)
    return Result




def main():
    nm = input().split()

    n = int(nm(0))

    m = int(nm(1))

    queries = ()

    for _ in range(m):
        queries.append(list(map(int, input().rstrip().split())))

    result = Operations(n, queries)

if __name__ == "__main__":
    main()

This code works, however, hits runtime errors when the arrays get too large. I have no idea how to lower the runtime further.

Example input is as follows:

5 3
1 2 100
2 5 100
3 4 100

algorithm – Find all of the paths that lead to the minimum sum

“Find minimum path sum” in a 2D grid, is a very popular question which is solved by dynamic programming. But what if instead of the minimum sum value, we return all the actual paths that lead to the minimum sum path? This was an interview question which has been puzzling me.

The original problem is :

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.

Ex:

Input: grid = 
((1,3,1)
,(1,5,1)
,(4,2,1))

output: 7
Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.

But instead of 7 I want to return (1,3,1,1,1) and all other paths that lead to sum of sum (in this case there is only one path). My current solution has exponential time complexity which is not good. Does anyone has a good solution to this problem?

algorithms – Express polynomial as sum of two lower-degree polynomials, modulo another

Suppose I have a polynomial $p(x)$, and a “modulus” polynomial $q(x)$ of degree $d$. I want to find two polynomials $r_1(x),r_2(x)$ of degree $d_1,d_2$ such that
$$p(x) equiv r_1(x) x^t + r_2(x) pmod{q(x)}$$
Assume $d_1+d_2le d$, and that we are working over some finite field.

Given $p(x),q(x),d,d_1,d_2,t$, is there an algorithm to find $r_1(x),r_2(x)$ that meets the conditions above?

In the special case where $t=d_2$, this is handled by reducing $p(x)$ mod $q(x)$, but I’m wondering if there is an efficient algorithm for the more general case. One algorithm is to express the coefficients of $r_1(x),r_2(x)$ as unknowns, write down $n$ linear equations in terms of these $d_1+d_2$ unknowns, and then solve the resulting linear system; this will take $O(d^3)$ time. Is there a more direct solution exploiting that these are polynomials, perhaps one achieving faster performance, such as $O(d)$ time?

sequences and series – Sum of elements of $A cap(B cup C)=400 times 274$. Find the closed form of $C$.

Question:

$A={x: x=3$ digit natural number $}$
$B={x: x=9 k+2 ; k in N}$
${C}={{x}: {x}=9 {k}+I ; {k} in {Z}}, $ $0 leq {I}<9$
Sum of elements of $A cap(B cup C)=400 times 274 .$
Find the value of $I$

To be honest, I don’t know how to start without making an assumption.
$$A cap(B cup C)=(A cap B)cup (A cap C)$$
Assuming that $(A cap B)$ and $(A cap C)$ doesn’t have any element common, one can find the sum of elements of $(A cap B)$ and $(A cap C)$ which would be equal to $400 times 274 $.

Sum of elements of $(A cap B)$:
$$sum_{k=11}^{110}(9k+2)=54450+200=54650$$

Sum of elements of $(A cap C)$:
Elements of $C$ are $$cdotscdots90+I,99+I,108+Icdots990+I,999+Icdotscdots$$
Elements of $A$ are $$100,101,102cdots 999$$
So possible elements of $(A cap C)$ (By keeping $0 leq {I}<9$ in mind) must be:$$99+I,108+Icdots990+Itag1label{eq1}$$
So
$$sum_{k=11}^{110}(9k+I)=54450+100I$$
Then after adding and solving $$I=5$$
And by luck $9k+2$ and $9k+5$ does not have any element common from $k=11$ to $k=110$.

How can this be solved without making that risky assumption? Any alternate.
Seeing range in $eqref{eq1}$ and guessing the elements of $(A cap C)$ worked well in this question but there is a single value of $I$ so that won’t work every time.

real analysis – Prove that the outer measure of an arbitrary sum of disjoint intervals is the infinite sum of the lengths of the intervals

The following is my HW Exercise:

Prove that if $I_1, I_2, dots$ is a disjoint sequence of open intervals, then $$left|bigcup_{k=1}^infty I_kright| = sum_{k=1}^infty mathscr{l}(I_k)$$ where $|cdot|$ denotes outer measure.

I found another, presumably simpler exercise:

Suppose $a, b ,c ,d$ are real numbers with $a<b$ and $c<d$. Prove that
$$|(a, b)cup(c, d)| = (b-a)+(d-c)iff (a, b)cap (c, d) = emptyset$$

I tried proving the simpler (second) exercise first as a stepping stone to proving the first exercise, but I didn’t get anywhere. Can someone please provide some hints on how the second exercise can be proven and perhaps also how the result of the second exercise can be used to prove the first exercise? Also, I haven’t learned about measurable sets yet because I am just getting started in Measure Theory.

How to sum a range columns from another google sheet

I get this daily data consumption spreadsheet report from a vendor that looks like this

userid    feb1    feb2    feb3     .    feb29 
u1        100     34      23       .     4
u2        0       24      21             62
u3        300     25      5              1
u4        50      5       6      
.
.
un        23      52      3        .     42

where n is my total number of users.

What I care about is simply tracking the daily consumption of all users.. so my final sheet should look like this

date        daily consumption
feb1        14,971     
feb2        6,898       
feb3        10,666     
.
.

feb29        10,543     

Currently I’m doing this by writing this in each line in my final sheet, for example to get the 14,971 for feb1 I’m putting

=sum(importrange("<sheet_ref>","<sheet_name>!I2:I"))

Naturally this is very manual and slow work. I want to know how to do this using a single formula or pivot table etc. I tried using array formulas, queries, pivot tables but I keep on getting stuck. Any suggestions?

formulas – Google Spreadsheet: Adding together multiple Sum Filter or Sumifs with multiple conditions across multiple sheets?

So I have a budgeting spread sheet. I have an overview page that I need to link to a “Checkings” sheet and a “CC-Chase” sheet.

To calculate the amount spent on a specific category that month I currently use:
=sum(filter(Checkings!$B:$B,Checkings!$C:$C=("Grocery"),Checkings!$A:$A>=("1/1/21")+0,Checkings!$A:$A<=("1/31/21")+0))

I want to add in the same data from my credit card statments so I tried:
=sum(filter(Checkings!$B:$B,Checkings!$C:$C=("Grocery"),Checkings!$A:$A>=("1/1/21")+0,Checkings!$A:$A<=("1/31/21")+0))+(FILTER('CC-Chase'!$F:$F,'CC-Chase'!$D:$D=("Grocery", 'CC-Chase'!$B:$B>=("1/1/21")+0,'CC-Chase'!$B:$B<=("1/31/21")+0)))

But this gives me an error. I’ve tried a few other combinations of this but always get an error.

Does anyone know what I’m doing wrong or have another way I could do this?

nt.number theory – Sum of inverse squares of numbers divisible only by primes in the kernel of a quadratic character

Let $chi$ be a primitive quadratic Dirichlet character of d modulus $m$, and consider the product
$$prod_{substack{p text{ prime} \ chi(p) = 1}} (1-p^{-2})^{-1}.$$

What can we say about the value of this product? Do we have good upper or lower bounds?

Some observations, ideas, and auxiliary questions

  • When $chi$ is trivial, it has value $zeta(2)$.
  • In general, since Chebotarev density theorem (CDT) tells us that $chi(p)$ is equidistributed in the limit, I would “want” the value to be something like

$$Big(zeta(2)prod_{p | m} (1-p^{-2})Big)^{frac{1}{2}}.$$

However, if I’m not mistaken, it seems that the error terms in effective forms of CDT may cause this to be very far from the truth. We can’t ignore what happens before we are close to equidistribution as the tail and the head are both $O(1)$. We can’t even control the error term well (without GRH) because of Siegel zeroes.

  • I don’t think we can appeal to Dirichlet density versions of CDT since those only tell us things in the limit as $s$ goes to $1$ and here $s = 2$.
  • Is there a way to “Dirichlet character”-ify a proof of $zeta(2) = pi^2/6$ to get a formula for this more general case? At least with Euler’s proof via Weierstrass factorization, it seems that we would need some holomorphic function which has zeroes whenever $chi(n) = 1$.

I had a few other ideas but they all seem to run into the same basic problem of “can’t ignore the stuff before the limit”… am I missing something?

Arithmetically weighted sum of half of the combinatorial numbers

I was looking for a closed formula for the left-hand side below. The oeis (https://oeis.org/A002457) suggested the right-hand side expression but I haven’t been able to prove it:

$$sum_{k=1}^n kbinom{2n}{n+k} = frac{n}{2}binom{2n}{n}$$

Thanks in advance!

Confusions about definition of the sum of two or several random variables

Please see the following pictures for detailed descriptions:
enter image description here

enter image description here