LogoLogo
  • CP Gymnasium
  • Week 3 Math / Number Theory
    • Week 3 Math / Number Theory
      • Problem 1: Tetration
      • Problem 2: Peragrams
      • Problem 3: Veci
      • Problem 4: Architecture
      • Problem 5: Joint Attack
      • Problem 6: How Many Digits?
      • Problem 7: Abstract Painting
  • Week 4 Array / Greedy
    • Week 4 Array / Greedy
      • Problem 1: Vaccine Efficacy
      • Problem 2: Frosh Week
      • Problem 3: Inquiry
      • Problem 4: Bank Queue
      • Problem 5: Log Land
  • Week 6 Sorting / Binary Search
    • Week 6 Sorting / Binary Search
      • Problem 1: Falling Apart
      • Problem 2: Synchronizing Lists
      • Problem 3: Distributing Ballot Boxes
      • Problem 4: Financial Planning
      • Problem 5: Big Boxes
  • Week 7 Dynamic Programming
    • Week 7 Dynamic Programming
      • Problem 1: Ocean's Anti-11
      • Problem 2: Batmanacci
      • Problem 3: Radio Commercials
      • Problem 4: Welcome to Code Jam (Hard)
      • Problem 5: Honeycomb Walk
  • Week 8 Graph Traversals
    • Week 8 Graph Traversals
      • Problem 1: Reachable Roads
      • Problem 2: Money Matters
      • Problem 3: Squawk Virus
      • Problem 4: Beehives
      • Problem 5: Running MoM
      • Problem 6: Amanda Lounges
Powered by GitBook
On this page
  1. Week 7 Dynamic Programming
  2. Week 7 Dynamic Programming

Problem 2: Batmanacci

https://open.kattis.com/problems/batmanacci

Given NNN and KKK, find the KthK^{th}Kth letter in the NthN^{th}Nth Batmanacci string.

We can store the length of each Batmanacci string in a list. (The length of theNthN^{th}Nthstring is simply the NthN^{th}NthFibonacci number.)

Recurrence relation for the lengths:

Length[i] = Length[i - 1] + Length[i - 2], with base cases Length[0] = 0 and Length[1] = 1.

We can build the list of lengths using DP.

Observe that given someNthN^{th}Nth string, the first part of this string is the (N−2)th(N-2)^{th}(N−2)th string.

  • If KKK is within the length of that (N−2)th(N-2)^{th}(N−2)th string, then we can check that (N−2)th(N-2)^{th}(N−2)th string to find the answer. (Denote the length of this (N−2)th(N-2)^{th}(N−2)th string as L.L.L.)

  • Otherwise,KKK is greater than the length of the (N−2)th(N-2)^{th}(N−2)thstring. Then the answer corresponds to the (K−L)th(K - L)^{th}(K−L)th letter in the(N−1)th(N-1)^{th}(N−1)thstring.

We can recursively obtain the desired KthK^{th}Kth letter in the NthN^{th}Nth string, taking advantage of the word lengths we previously stored.

Base cases in the recursion:

  • N = 1: answer is 'N'

  • N = 2: answer is 'A'

Recurrence relation:

  • If K <= Length[N-2], go to the case for N-2, K.

  • Otherwise, K > Length[N-2], so we go to the case for N-1, K-L.

After these recursive calls, we will arrive at one of the base cases (which gives us the final answer).

Note: make sure to use unsigned long long because the numbers can get very large.

For largeNNN, Length[N]Length[N]Length[N]may cause overflow. Thus, if Length[N]>1018Length[N] > 10^{18}Length[N]>1018 , we can just set it to101810^{18}1018since that's the maximum input value forKKK.

PreviousProblem 1: Ocean's Anti-11NextProblem 3: Radio Commercials

Last updated 4 years ago