Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=2709
Main topic:
An integer n is decomposed into the form of 2^i addition, and there are many kinds of division methods. Example: 7 = 1+1+1+1+1+1+1
= 1+1+1+1+1+2 = 1+1+1+2+2 = 1+1+1+4 = 1+2+2+2 = 1+2+4, a total of 6 different methods.
Ideas:
Set A[n] as the integer n decomposition into the number of 2^i addition form.
When n is odd, n-1 is an even number, n = 1 + n-1, decomposition of a 1, and then decomposition of even n-1, that is, the a[n-1] Species division method.
When n is an even number, there are two methods of decomposition.
1): The added 2^i contains 1. Because n is even, there are at least two 1, i.e. n = 1 + 1 + n-2, the total is a[n-2].
2): The added 2^i does not contain 1. The decomposition factor is even, dividing each decomposed 2^i by 2, just the decomposition result of the N/2,
Total is A[N/2].
To sum up is:
If n is odd, a[n] = a[n-1], if n is even, a[n] = A[n-2] + A[N/2]. This is a recursive process.
AC Code:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace Std;__int64 A[1000100];int Main () { a[0] = 1,a[1] = 1; for (int i = 2; I <= 1000000; ++i) { if (i&1) a[i] = a[i-1]; else a[i] = (a[i>>1] + a[i-2])%1000000000; } __int64 N; while (CIN >> N) { cout << a[n] << Endl; } return 0;}
HDU2709 sumsets "Recursion"