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
|
351 Discussions
|
Please Login in order to post a comment
The most intuitive solution you will ever find in this entire discussion. Plus, the run time speed surpassed editorial solution.
include
using namespace std;
int ipow(const int& b, const int& e){ if(e == 0) return 1; return b * pow(b, e - 1); }
int recursion_ans(const vector& vec, const int& index, const int& remaining){ if(remaining == 0) return 1; if(index < 0 || remaining < 0) return 0;
}
int main(){ int X, N; cin >> X >> N;
}
Haskell
Function wwo -- "with or without"
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 :'( ).