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
# | User | Rating |
---|---|---|
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 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 156 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
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
Name |
---|
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