HackerRank(FP) - The Sums of Powers

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
时间: 2024-12-21 00:46:15

HackerRank(FP) - The Sums of Powers的相关文章

2017中国大学生程序设计竞赛 - 女生专场(重现)

A.Automatic Judge(模拟) Problem Description Welcome to HDU to take part in the second CCPC girls’ competition!A new automatic judge system is used for this competition. During the five-hour contest time, you can submit your code to the system, then the

HDU - 6027 Easy Summation(快速幂)解题

Easy Summation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1107    Accepted Submission(s): 443 Problem Description You are encountered with a traditional problem concerning the sums of po

hdu6027Easy Summation(快速幂取模)

Easy Summation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 4112    Accepted Submission(s): 1676 Problem Description You are encountered with a traditional problem concerning the sums of po

Good number(3进制)

https://codeforces.com/problemset/problem/1249/C2 C2. Good Numbers (hard version) time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The only difference between easy and hard versions is the

【PE512】Sums of totients of powers(欧拉函数)

点此看题面 大致题意: 求\(\sum_{n=1}^{5*10^8}((\sum_{i=1}^n\phi(n^i))(mod\ n+1))\). 大力推式子 单独考虑\((\sum_{i=1}^n\phi(n^i))(mod\ n+1)\). 由于\(\phi\)有一个显然的性质: \[\phi(x^y)=\phi(x)\cdot x^{y-1}\] 所以上面的式子就可以推成: \[(\phi(n)\sum_{i=1}^nn^{i-1})(mod\ n+1)\] 又由于\(n\equiv-1(m

uva:10487 - Closest Sums(二分查找)

题目:10487 - Closest Sums 题目大意:给出一组数据,再给出m个查询的数字.要求找到这组数据里的两个数据相加的和最靠近这个查询的数据,输出那两个数据的和. 解题思路:二分查找,这样找到的话,就输出查询的数值,但是要注意找不到的情况:这里最靠近的值不一定是在找不到的时刻的前一次数据,所以要维护最靠近的要查询数的数值. 代码: #include <stdio.h> #include <algorithm> #include <stdlib.h> using

LightOJ1125 Divisible Group Sums

Divisible Group Sums Given a list of N numbers you will be allowed to choose any M of them. So you can choose in NCM ways. You will have to determine how many of these chosen groups have a sum, which is divisible by D. Input Input starts with an inte

洛谷P1466 集合 Subset Sums

洛谷P1466 集合 Subset Sums这题可以看成是背包问题 用空间为 1--n 的物品恰好填充总空间一半的空间 有几种方案 01 背包问题 1.注意因为两个交换一下算同一种方案,所以最终 要 f [ v ] / 2 2.要开 long long 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <string&g

FP Tree算法原理总结

在Apriori算法原理总结中,我们对Apriori算法的原理做了总结.作为一个挖掘频繁项集的算法,Apriori算法需要多次扫描数据,I/O是很大的瓶颈.为了解决这个问题,FP Tree算法(也称FP Growth算法)采用了一些技巧,无论多少数据,只需要扫描两次数据集,因此提高了算法运行的效率.下面我们就对FP Tree算法做一个总结. 1. FP Tree数据结构 为了减少I/O次数,FP Tree算法引入了一些数据结构来临时存储数据.这个数据结构包括三部分,如下图所示: 第一部分是一个项