• + 0 comments

    Haskell

    Function wwo -- "with or without"

    module Main where
    
    vals :: Int -> Int -> [Int]
    vals n k = reverse $ takeWhile (<= n) [x ^ k | x <- [1 ..]]
    
    wwo :: Int -> [Int] -> Int
    wwo n xs
      | n == 0 = 1
      | n < 0 = 0
      | null xs = 0
      | otherwise = wwo n (tail xs) + wwo (n - head xs) (tail xs)
    
    main :: IO ()
    main = do
      n <- readLn :: IO Int
      k <- readLn :: IO Int
      print $ wwo n (vals n k)