We are summing basically
seq * i^k
but for different ranges [a,b]. How to reduce this to run
in time? I cannot see how to precompute for this. Thanks!
11444 - Sum
Moderator: Board moderators
-
- A great helper
- Posts: 383
- Joined: Mon Oct 18, 2004 8:25 am
- Location: Bangladesh
- Contact:
-
- New poster
- Posts: 33
- Joined: Fri Jan 06, 2006 5:51 pm
Re: 11444 - Sum
I understood how to deduce the answer from the partial sums when K = 1, but I failed to generalize it for K > 1.
How can the binomial expansion help me here?
How can the binomial expansion help me here?
Re: 11444 - Sum
Use binomial formula to expand (i-a+1)^k in terms of powers of i.
For K=2:
sum(seq * (i + (1-a))^2) =
sum(seq*i^2 + 2(1-a)*seq*i + (1-a)^2*seq) = sum(seq*i^2) + 2(1-a)*sum(seq*i) + (1-a)^2 * sum(seq).
Sums like sum(seq*i^2), sum(seq*i), etc can be done with a precomputation.
For K=2:
sum(seq * (i + (1-a))^2) =
sum(seq*i^2 + 2(1-a)*seq*i + (1-a)^2*seq) = sum(seq*i^2) + 2(1-a)*sum(seq*i) + (1-a)^2 * sum(seq).
Sums like sum(seq*i^2), sum(seq*i), etc can be done with a precomputation.