We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Dynamic Programming
  4. P-sequences
  5. Discussions

P-sequences

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • brunopaulsen
    1 year ago+ 0 comments

    The algorithm is correct

    Still it will not work for very large number TestCases: 10,11,13,16,17 and 18. They exceed computational capacity of 53bit integers. Then, a special multiplication procedure will be needed in order to get the correct modulus without errors induced by those really really really really big numbers,,,,,

    function pSequences(n, p) {
        // Write your code here
        const MOD = 1000000000+7
        let root = parseInt(Math.sqrt(p)),
            last = root*2-((root*(root+1)>p)?2:1),
            qtty = [], val=[], calc=[], i=0, j=last, accum=0, size=last+1
        while (accum < p) calc[j--] = (accum += (val[i] = (qtty[i++] = (parseInt(p/parseInt(p/(accum+1))) - accum))))
        while(n-->=2) {
            let next = new Array(size)
            accum = 0
            for(i=0,j=last;i<=last;i++,j--) next[j] = (accum = (accum + (val[i] = ((qtty[i] * calc[i])%MOD)))%MOD)
            calc = next
        }
        return accum % MOD
    }
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy