Sort by

recency

|

12 Discussions

|

  • + 0 comments

    Simple bottom up DP solution in Haskell using arrays:

    import Control.Monad (replicateM_)
    import Data.Array
    
    main :: IO ()
    main = readLn >>= flip replicateM_ (do
      l <- getLine
      let [n, k] = fmap read $ words l
      print $ combs ! n ! k)
    
    combs :: Array Int (Array Int Int)
    combs = listArray (0, 1000) $ rowToArray <$> combinations
      where rowToArray l = listArray (0, length l - 1) l
    
    combinations :: [[Int]]
    combinations = [1] : (nextRow <$> combinations)
      where
        nextRow l@(_:t) = 1 : (zipWith f l t) ++ [1]
        f x y = (x + y) `mod` (10^8 + 7)
    
  • + 0 comments

    n,m = map(int,input().split()) a1 = [[0]*m]*n for i in range(n): a1[i] = list(map(int,input().split())) a = numpy.array(a1) s = numpy.sum(a,axis = 0) p = numpy.prod(s) print(p) I'm using this track scirpt with my Neoprene Bags page.

  • + 0 comments

    haskell without DP

    import Data.List (intercalate)
    
    choose (n, k) =
      let k' = min k (n-k)
        in product [n-k'+1..n] `div` product [1..k'] `mod` 100000007
    
    intpair l = (head z, z !! 1)
      where z = map read $ words l
    
    main = do
      cont <- getContents
      let ls = tail $ lines cont
      putStrLn $ intercalate "\n" $ map (show . choose . intpair) ls
    
  • + 0 comments

    This might be useful: https://blog.plover.com/math/choose.html

    unsigned choose(unsigned n, unsigned k) {
        unsigned r = 1;
        unsigned d;
        if (k > n) return 0;
        for (d=1; d <= k; d++) {
        r *= n--;
        r /= d;
        }
        return r;
    }
    

    I translated it to Haskell (foldr) and passed all tests.

  • + 0 comments

    I used the Extended Euclidean Algorithm to calculate the necessary modular inverses for this problem. Solution is in Haskell.

    -- Code assumes prime modulus
    -- Using the Extended Euclidean Algorithm to solve Bezout's Identity
    extEuclid ra rb sa sb ta tb
        | rb == 0   = sa
        | otherwise = extEuclid rb (ra - quot * rb) sb (sa - quot * sb) tb (ta - quot * tb)
        where quot = div ra rb
    -- Calculating the modular inverse using the Extended Euclidean Algorithm
    invmod modulus a = extEuclid a modulus 1 0 0 1
    -- n choose k = n / k * (n-1 choose k-1)
    choose modulus n k
        | k == 0    = 1
        | otherwise = mod (n * invmod modulus k * choose modulus (n-1) (k-1)) modulus