标签:
In Haskell. Two points: 1. pruning 2. Int suffers from overflow. Integer it is.
getPowerSum :: Integer -> [Integer] -> Integer -> Integer getPowerSum _ [] _ = 0 getPowerSum tgt cand start = case compare tgt start of EQ -> 1 LT -> 0 GT -> let newCand = filter (>start) cand in let newTgt = tgt - start in sum $ map (getPowerSum newTgt newCand) newCand getPowerSumWays :: Integer -> Integer -> [Integer] getPowerSumWays x n = let cand = [i^n | i <- [1..x], (i ^ n) <= x] in map (getPowerSum x cand) cand -- Main -- main = do sx <- getLine sn <- getLine let x = read sx :: Integer let n = read sn :: Integer print $ sum $ getPowerSumWays x n
HackerRank(FP) - The Sums of Powers
标签:
原文地址:http://www.cnblogs.com/tonix/p/4310074.html