Sort by

recency

|

8 Discussions

|

  • + 0 comments

    no bfs, no dp. Just simple math. Also, handles more stricter test cases.

    solve :: ([Int], Int) -> [Int]
    solve (s,n) = foldl (\r partS -> 
                                    let 
                                        f = factors partS n [] 
                                        lenf = length f
                                        lenr = length r
                                        check = mod n (head partS) == 0
                                    in 
                                        if not check then 
                                            r 
                                        else if not (null f) && (lenr > lenf || null r) then f else if lenr == lenf then min r f else r
                        ) [] $ init $ tails rs
        where
            rs = sortBy (flip compare) s
    
            factors _ 1 acc = 1:acc
            factors [] _ _ = []
            factors s@(a:as) n acc
                | mod n a == 0 = factors s (div n a) (n:acc)
                | otherwise = factors as n acc
    
  • + 0 comments

    seems like most of the 50pt solutions fail the following test case

    64 3
    2 8 16
    

    the answer should be

    1 8 64
    

    instead of

    1 2 4 64
    
  • [deleted]
    + 0 comments

    I meant to submit my solutions until I found there is no C++ option in the languages combo box. Is there a way to submit a C++ coded solution?

  • + 1 comment

    Hi, I'm trying to use BFS to solve this, but I'm getting timeouts for test case 11 onwards. Can someone please help me make my solution more efficient? (I'm coding in Scala)

    def main(args: Array[String]) {
        val sc = new java.util.Scanner(System.in)
        val n = sc.nextLong()
        val k = sc.nextInt()
        val a: Array[Int] = Array.ofDim(k)
        for (i <- 0 until k) {
          a(i) = sc.nextInt()
        }
        val s = a.toStream.sorted
    
        val sol = broaden[(List[Int], Long)](Stream((Nil, n)),
                                             currTarget => s.filter(ai => (currTarget._2 % ai) == 0)
                                                           .map(ai => (ai :: currTarget._1, currTarget._2 / ai)))
                  .find(_._2 == 1)
                  .map(_._1.reverse)
    
        if (sol.isEmpty) print("-1")
        else printStates(sol.get, 1)
      }
    
      def printStates(l: List[Int], current: Long): Unit = l match {
        case Nil => print(current + " ")
        case x :: xs =>
          print(current + " ")
          printStates(xs, current * x)
      }
    
      def broaden[T](s: Stream[T], f: T => Stream[T]): Stream[T] = {
        if (s.isEmpty) s
        else s.head #:: broaden(s.tail append f(s.head), f)
      }
    
  • + 1 comment

    Since with the actual test case, this challenge is quite easy, would it possible to downgrade the difficulty of this challenge and add a new challenge that would be the same, but with more advanced test-cases as the one mentioned here ?