George the Ghost returns home with bags full of candy. He lays them out on the table, opens his mouth, and... you didn't think he was gonna eat them, did you?
Of course not! Firstly, George is a ghost, and he cannot eat solid food. Secondly, George is a magic ghost - he was going to cast spells to duplicate his candy!
George has types of candy laid out in a row. Initially (at time ), he has () of the -th type of candy. Then, each second, he performs the following operation:
Set to for all at once, where
After () seconds, how much total candy he will have? This might be a big number, so print it modulo .
I'm not familiar with modulo
When solving problems that ask for an answer a large prime number, this often means that you have to use 64-bit numbers, such aslong
in Java or long long
in C++.
When performing addition and multiplication operations, , often denoted by , is distributive:
, and
When performing subtraction, you might need to do it as follows to avoid performing a negative number (i.e. in case is negative):
Finally, for division... things get complicated. Read the following articles below to better understand it.
CP-Algorithms - Modular Inverse using Binary Exponentiation
Geeks4Geeks - Modular Division
Input Specifications
On the first line, an integer (), the number of types of candy.
On the second line, space-separated integers: (), the starting number of each type of candy.
On the third and last line, an integer (), the number of seconds that pass.
Output Specifications
Print one integer: the total number of candy George will have after seconds, modulo .
Note for Python users: To pass this question, you must select the PyPy interpreter instead of the normal one.
Constraints
Subtask 1 [20%]:
Subtask 2 [80%]:
No further constraints.
Sample Input 1
4
3 2 2 1
3
Sample Output 1
89
Sample Output 1 Explanation
After second, the number of candies of each type is:
3 5 7 8
After seconds:
3 8 15 23
After seconds:
3 11 26 49
, so he has 89 pieces of candy.
Comments