You are given two arrays of integers a, b both with length n <= 10^5. For each 0 <= x <= n print the sum of a[i]*b[x-i] for all 0 <= i <= x.
It's obvious this can be done in quadratic time, but can we do any better?
# | User | Rating |
---|---|---|
1 | tourist | 3856 |
2 | jiangly | 3747 |
3 | orzdevinwang | 3706 |
4 | jqdai0815 | 3682 |
5 | ksun48 | 3591 |
6 | gamegame | 3477 |
7 | Benq | 3468 |
8 | Radewoosh | 3462 |
9 | ecnerwala | 3451 |
10 | heuristica | 3431 |
# | User | Contrib. |
---|---|---|
1 | cry | 168 |
2 | -is-this-fft- | 162 |
3 | Dominater069 | 160 |
4 | Um_nik | 159 |
5 | atcoder_official | 156 |
6 | djm03178 | 153 |
6 | adamant | 153 |
8 | luogu_official | 149 |
9 | awoo | 148 |
10 | TheScrasse | 146 |
You are given two arrays of integers a, b both with length n <= 10^5. For each 0 <= x <= n print the sum of a[i]*b[x-i] for all 0 <= i <= x.
It's obvious this can be done in quadratic time, but can we do any better?
Name |
---|
If P(x) = a0 + a1*x + ... + an * x^n and Q(x) = b0 + b1*x + ... + bn*x^n then how can you obtain the requested sum? How can you compute it?
Let R(x) = P(x) * Q(x). Your sum is R(1).
FFT
Thanks for the reply. Your strategy is quite interesting, however that only solves the problem if we want the sum over a[i]*b[x-i] for all x and for all i. The problem was to isolate each x and calculate the sum over a[i]*b[x-i] for all i for this value of x only, then move on to the next value of x. I'm very sorry if this wasn't clear.
Oh, I didn't get that. I think that the same strategy works in this case though.
It seems that the answer will always be equal to
. (can't prove it, sorry)
.
Let p be the partial sum array of a. Then the answer is simply