UCC Coding Competition '21 P4 - Trampoline Jump

View as PDF

Submit solution

Points: 7 (partial)
Time limit: 2.0s
Memory limit: 256M

Author:
Problem type

The Fibonacci Sequence is a mathematical sequence where the first and second terms are 1, and each term after that is the sum of the two previous terms. More formally, if fi denotes the ith term of the Fibonacci Sequence, then f1=f2=1, and fk=f(k1)+f(k2) for all integer k>2.

The modulo (or mod) operation is an operation represented by the % symbol. The expression a%b denotes the remainder when the positive integer a is divided by the positive integer b. For example, 7%2=1, because the remainder when 7 is divided by 2 is 1. The modulo operator is available in most programming languages using the operator % - for example, the line int a = 7%2; in C++ will set a to be 1.


You have found yourself at the first house of a long street. There are a total of N houses along this street, numbered 1 to N. You are trying to travel from the first house to the last house as fast as possible.

You can always walk from one house to an adjacent house (i.e. from house w to house w1 or house w+1, if they exist). This takes one minute.

At each house, there is also a trampoline that allows you to travel a larger distance. In particular, at the ith house, there is a trampoline with strength ai. Using the trampoline also takes one minute, and from house i, you can use the trampoline to jump either backwards to house iai (as long as iai1) or forwards to house i+ai (as long as i+aiN).

You have been able to reverse-engineer a pattern of the values of ai for all the houses:

ai=(fi%2021)+(i%50) for all 1iN,

where fi represents the ith term in the Fibonacci Sequence and % represents the modulo operation, as defined at the beginning of the problem.

For reference purposes, a1=2, a2=3, a3=5, a4=7, a5=10, a6=14, a7=20, and a125=356.

Please find the minimum number of minutes you need to travel from house 1 to house N.

NOTE FOR PYTHON USERS: If your program receives TLE (time limit exceeded), you should try submitting using the PyPy interpreter: when you are submitting your code, try using "PyPy 3" or "PyPy 2" as the language, instead of "Python 3" or "Python 2".

Input Specification

The input will consist of one integer, N, the number of houses on the street.

Output Specification

Please output the minimum number of minutes you need to travel from house 1 to house N.

Constraints and Partial Marks

For 3 of the 10 available marks, N400.

For the remaining 7 marks, N5105.

Sample Input 1

Copy
9

Sample Output 1

Copy
3

Explanation for Sample Output 1

The first 9 terms of the sequence ai are 2,3,5,7,10,14,20,29,43. To travel from house 1 to house 9, the optimal pattern of moves is:

  • Move 1: Jump from house 1 to house 3. This is possible as 1+a1=1+2=3.
  • Move 2: Jump from house 3 to house 8. This is possible as 3+a3=3+5=8.
  • Move 3: Walk from house 8 to house 9. This is possible as 8+1=9.

Every move takes 1 minute, so the output is 3.

Sample Input 2

Copy
27

Sample Output 2

Copy
4

Explanation for Sample Output 2

One way to reach house 27 in 4 moves is as follows:

  • Move 1: Jump from house 1 to house 3. This is possible as 1+a1=1+2=3.
  • Move 2: Jump from house 3 to house 8. This is possible as 3+a3=3+5=8.
  • Move 3: Walk backwards from house 8 to house 7. This is possible as 81=7.
  • Move 4: Jump from house 7 to house 27. This is possible as 7+a7=7+20=27.

Comments

There are no comments at the moment.