Editorial for Goofy Function
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Since , , and are provided in the input, and a formula for each following term is provided, we can use a loop to calculate all the values . Make sure to store at least the most recent answers in an array to be used in the formula and mod each answer by .
Afterwards, output the value for .
Time complexity: .
Sample solution written in Python
Please try implementing the solution yourself rather than copy-pasting the official solution.
a, b, c, n = map(int, input().split())
f = [None] * (n + 1) # Initialize empty array
f[1] = a; f[2] = b; f[3] = c
for i in range(4, n + 1): # calculate f(4) ... f(n)
f[i] = (f[i - 1] + f[i - 2] - f[i - 3]) % (1000000007) # use the given formula and mod
print(f[n])
Comments