码迷,mamicode.com
首页 > 其他好文 > 详细

HackerRank(FP) - The Sums of Powers

时间:2015-03-03 06:20:33      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!