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.
- All Contests
- ProjectEuler+
- Project Euler #169: Exploring the number of different ways a number can be expressed as a sum of powers of 2.
- Discussions
Project Euler #169: Exploring the number of different ways a number can be expressed as a sum of powers of 2.
Project Euler #169: Exploring the number of different ways a number can be expressed as a sum of powers of 2.
Sort by
recency
|
30 Discussions
|
Please Login in order to post a comment
PyPy3 solution:
The recurrence comes from generating functionology. E.g. suppose N = 10. int(log(N, 2)) = 3. Thus, the generating function of this is (1 + x + x^2)(1 + x^2 + x^4)(1 + x^4 + x^8). In general, when int(log(N, 2)) = k, we have: Pk = (1 + x + x^2)...(1 + x^(2^k) + x^(2^(k + 1)) ).
The solution is the coefficient in front of x^N Now, to obtain the reccurence, consider the following two cases: 2N + 1 and 2N. coeff(Pk, 2N + 1) = coeff(x * P_{k - 1}(x^2), 2N + 1) = coeff(P_{k - 1}, N) coeff(Pk, 2N) = coeff((1 + x^2)P_{k - 1}(x^2), 2N) = coeff(P_{k - 1}, N - 1) + coeff(P_{k - 1}, N)
It follows that f(2N + 1) = f(N) and f(2N) = f(N - 1) + f(N).
Python: just figure out // is floor division and / is float division. When n are very large, / will round your result and // will result in integer. Try the following code to see the difference print(10 ** 25 / 2, 10 ** 25 // 2)
Can be solved more efficiently by dynamic programming. https://www.geeksforgeeks.org/perfect-sum-problem-print-subsets-given-sum/
public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
what is the problem..showing:unsafe operations