Drink with Double or Fold
Time limit1sMemory limit512 MB
Each term from position k+1 on is the sum of the previous k terms mod P; find the N-th term with N up to 1e9.
- Level
Medium7 of 10
- Topics
- Matrix, Math, Dynamic programming, Divide and conquer
- Solved
- No attempts yet
Problem
Youngsu and his friends invented a new drinking game called "Drink with Double or Fold". The game proceeds as follows.
- At the start of the game, the first, second, , -th person each decide how much to drink.
- Starting from the -th person (), that person drinks an amount equal to the sum of what the -th people drank, taken modulo .
Given the amounts that the first people drink, and given that Youngsu drinks last, find the amount of alcohol Youngsu will drink. Youngsu and his friends have no limit on how much they can drink, so there is no need to worry about their health.
Input
The first line gives and . (, )
The second line gives the amounts that the first people drink, in order. ()
The last line gives the integer . ()
Output
Print the amount of alcohol Youngsu will drink.