HDU2709 sumsets "Recursion"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.