mujtaba1747's blog

By mujtaba1747, history, 4 years ago, In English

Hi, I was trying to solve this 102501H - Pseudo-Random Number Generator. I tried to find a pattern but couldn't due to the large constraints. The editorial claims that we need to find the Period of the sequence but I am unable to do so. Any help / suggestions are welcome.

  • Vote: I like it
  • +1
  • Vote: I do not like it

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by mujtaba1747 (previous revision, new revision, compare).

»
4 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Can you share a link to the editorial? I'd like to see if my solution works correctly (if I've found the right periodicity length). The period and the start of the period I've found is in the spoiler below.

What you basically need to do is preprocess a little and try finding a period. A period is basically the cycle length after which you start to observe the same numbers. I did so using a hash map and loops (which is slightly slow). There might be better ways of doing it but they aren't something I'm aware off yet. This periodic property can be proven using some math, I guess, but I assumed periodicity (otherwise it's probably impossible) because of having solved a similar problem. Once you've found a period, you can answer in O(1) time like a $$$y = mx + c$$$ question.

I've solved a similar problem from HP CodeWars 2019 NA Sample paper (I guess it was in this one, but I'm not sure). It was this problem called "Wonka Factory Password" and it's the 29th (last) problem on the problem set. I initially started out with a "working" brute force solution. Because I wasn't able to think about how I'd about it, I had opened a problem discussion thread on a different website (let me know if you want the link to the entire discussion. It's not actually the entire discussion (but it's most of it) because the person who helped me solve it continued discussion in private chats but I can share you some code from there if you'd like).

Period:
Period starts at:

HP CodeWars Problem Link: http://www.hpcodewars.org/past/cw22/problems/2019NAFullProblemSet.pdf

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    This is the contest link. This is it's editorial. Thanks for sharing your approach, much appreciated.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Could you share your code please....

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      Well, I haven't written a complete solution for this problem. I just used some memory saving techniques to calculate the period and start of period using an unordered_map. It's similar idea like the code below for Wonka Factory Password.

      Understanding the editorial idea: Youtube

      Code
»
4 years ago, # |
  Vote: I like it +2 Vote: I do not like it

Hi! I had a lot of fun upsolving this problem. It uses "Floyd's Tortoise and Hare" algorithm, which finds a cycle in a functional graph. I recommend that you read about it here.

Basically, since the given function in the problem uses modulo to calculate values, we know it repeats itself at some point. That means there is a fixed number of values the generator can output: $$$p_{len} + c_{len}$$$, where $$$p_{len}$$$ is the length of the sequence previous to the cycle and $$$c_{len}$$$ is the length of the cycle.

Knowing this, you need to precalculate blocks of answer for the two parts: before the cycle and during the cycle. If you know the answer at some fixed points, the amount of work your code needs to do is greatly reduced and you can calculate the rest during execution.

In my code (pastebin link) I called precomputed values as "PCA" (Pre-cycle answer) and "CA" (Cycle answer). I hope I was able to give you a clear idea of the solution. Feel free to write back if you need any help!

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Wow, I didn't know about this technique. Thanks for sharing.