Sort by

recency

|

13 Discussions

|

  • + 0 comments

    haskell

    main = do
      getLine
      la <- getLine
      getLine
      lb <- getLine
      let xs = map read (words la) :: [Integer]
          ys = map read (words lb) :: [Integer]
      print $ gcd (product xs) (product ys) `mod` 1000000007
    
  • + 0 comments

    Haskell:

    main :: IO ()
    main = f <$> readN <*> readN >>= print
    
    f :: Integer -> Integer -> Integer
    f = ((`mod` k) .) . gcd
    
    k :: Integer
    k = 1000000007
    
    readN :: IO Integer
    readN = getLine >> product . map read . words <$> getLine
    
  • + 0 comments

    One-liner in Haskell (no focus on performance though):

    solve :: [Integer] -> [Integer] -> Integer
    solve ns ms = flip mod (10^9 + 7) 
                $ gcd (product ns) (product ms)
    
  • + 0 comments

    Scala solution:

    import scala.io.StdIn
    
    object Solution extends App {
      def gcd(x: BigInt, y: BigInt): BigInt = {
        if (x == 0) y else gcd(y % x, x)
      }
    
      def product(nums: String): BigInt = {
        nums.split(" ").map(BigInt(_)).product
      }
    
      val f = () => StdIn.readLine()
      val (_, a, _, b) = (f(), product(f()), f(), product(f()))
      println(gcd(a, b) % 1000000007)
    }
    
  • + 1 comment

    My OCaml code works fine with the first 3 TC, but on #3 it starts to break... First I get a list of prime factors, I generate a Set of factors and intersect them with each other so the (Prime, Exponent) lists can have the same length and items. Then I count each number in the common set and make a List of (Prime, Exponent). Last, I get the least Exponent and multiply to get the total number and apply the modulo operation.

    The result given in the output is a negative number, so I guess I ran out of Int64 values while multiplying...

    I'm running into a lot of timeout/memory issues with these kinds of exercises and OCaml, should I be using another language?