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 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 165 |
2 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
4 | adamant | 160 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
8 | Dominater069 | 154 |
8 | nor | 154 |
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
Название |
---|
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