Short Summary:
Given a list of numbers $a_k$ and $r_1, r_2, c_1, c_2$,
calculate
$\sum_{x=r_1}^{r_2} \sum_{y=c_1}^{c_2} a_{(x + y) mod N }$
Although this is a computer programming problem, I believe there is math involved in the question to simplify it. When I make a 'brute-force' solution, it run too slow.
Suppose $N = 3 \times 10^5, r_1 = c_1 = 0$ and $ r_2 = c_2 = N$ With straight-forward brute force I would need to do near $10^11$ Calculations vs around 10^8 Calculations per second.
Is there a better algorithm/way to simplify the calculation?
Thanks.
(P.S. If this should be transferred to StackOverflow, I would remove this.)
