We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Recursion
- The Power Sum
- Discussions
The Power Sum
The Power Sum
Sort by
recency
|
349 Discussions
|
Please Login in order to post a comment
int findPows(int X, int num, int N) {
}
int powerSum(int X, int N) { return findPows(X, 1, N); }
knapsack, O(X * X^(1/N))
I noticed X was capped to 1000 and I'd never need to try any number >sqrt(1000), which happens to be just under 32. So, what I did was precompute the Nth powers for [1..32], store my current set as a bitmap in a
u32
. and then add when the bit was on. I just iterated until the first power of 2 (which means it's a single element set) for which the sum (the actual Nth power, in this case) was > X, since that means it's the Nth root of X and thus no point trying anything else. I'm somewhat proud of how dumb my solution was. No recursion, no algorithms concepts (although that's what I'm here to train :'( ).Simple C++ solution using Back Tracking:
I coded in Java, using backtracking as the core algorithm in the solution. This can be improved. I recommend this problem for anyone who just learn backtracking.