For two given integers n and k, How to find
(Zn + Zn - 1 - 2Zn - 2) mod 10000007,
where Zn = Sn + Pn and
Sn = 1k + 2k + 3k + ….. + nk and
Pn = 11 + 22 + 33 + …… + nn
Constraint : (1 < n < 2 * 108, 0 < k < 106)
I got this problem on link
For two given integers n and k, How to find
(Zn + Zn - 1 - 2Zn - 2) mod 10000007,
where Zn = Sn + Pn and
Sn = 1k + 2k + 3k + ….. + nk and
Pn = 11 + 22 + 33 + …… + nn
Constraint : (1 < n < 2 * 108, 0 < k < 106)
I got this problem on link
| № | Пользователь | Рейтинг |
|---|---|---|
| 1 | Benq | 3676 |
| 2 | jiangly | 3670 |
| 3 | Kevin114514 | 3657 |
| 4 | maroonrk | 3355 |
| 5 | Um_nik | 3353 |
| 6 | squareOf105 | 3346 |
| 7 | tourist | 3343 |
| 8 | strapple | 3324 |
| 9 | heuristica | 3322 |
| 10 | jiangbowen | 3291 |
| Страны | Города | Организации | Всё → |
| № | Пользователь | Вклад |
|---|---|---|
| 1 | Qingyu | 158 |
| 2 | nik_exists | 150 |
| 2 | maspy | 150 |
| 4 | AmShZ | 143 |
| 5 | Um_nik | 141 |
| 6 | Errichto | 139 |
| 7 | adamant | 137 |
| 8 | BledDest | 134 |
| 9 | maroonrk | 133 |
| 10 | qwexd | 130 |
| Название |
|---|



It's a simple math problem, i think.
If we write Zn = Sn + Pn = nk + Sn - 1 + nn + Pn - 1
So we can write Zn + Zn - 1 = nk + nn + 2 * Zn - 1
As a result, we can write Zn + Zn - 1 - 2 * Zn - 2 = nn + nk + 2 * (n - 1)n - 1 + 2 * (n - 1)k.
This can be calculated in logarithmic time with fast exponentiation.
I solved it with the following thought process (In my opinion, giving thoughts is more important than the solution itself).
First notice the coefficients Zn + Zn - 1 - 2Zn - 2)
1 + 1 - 2 = 0
That looks suspicious!
Then we take a look at Sn and Pn.
Hmm ... Have you noticed something interesting?
If not, write down S3 S4 S5 and so on
and you'll notice:
Sn = Sn - 1 + nk = Sn - 2 + (n - 1)k + nk
Similarly with Pn
Pn = Pn - 1 + nn = Pn - 2 + (n - 1)n - 1 + nn
Then we substitute Sn, Sn - 1, Pn, Pn - 1 in the original sequence,
we get Zn + Zn - 1 - 2Zn - 2 = 2(n - 1)k + nk + 2(n - 1)n - 1 + nn
which leads to the solution