Dp:sumsets (POJ 2229)

Source: Internet
Author: User

                 

The set problem of the number

Topic: Given you an integer m, you can only use the 2 K power to combine this number, ask you how many combinations?

Look at this one, God, it's too simple, a full backpack? Isn't it?

  But it is true that the question can be thought of with a complete backpack, but the problem is definitely tle, if it is a complete backpack, then I do not have to wait so many days to send this hole, this problem really need to use some wonderful thought.

First, we ignore the most important condition of this problem, we use the number is 2 power, then 2 power of the number can do? This is a math problem.

But don't be afraid, this math problem is also very good to think,

    First of all: any one odd number must have a composition, inference: Any even numbers can be composed only of 1

Second, any even number can be left by one digit (refer to binary)

  Let's use these two mathematical conclusions to think about how to simplify recursion.

Back to the question, the reason why the full backpack will be tle is that there is too much enumeration of J in the second loop, so we must avoid this when we use these two conclusions, preferably one step, so we have to put the number all on the previous case, then we can first use the conclusion 1, For any one odd, we can use an even +1 (the combination number is the same, because only this 1), and this set can not be directly obtained by other sets, then we get the first recursive formula

   Dp[j]=dp[j-1] When j= odd

  Now use the second conclusion, because our even numbers can be obtained from the odd number, or can be obtained from the number of even, then the first part can be obtained from dp[j-1], the other part is to think of the conclusion 2, because we just left, the combination is constant, so we can also from dp[j>>1] To get the other part of the combo number so that you avoid a backpack that looks for enumeration I

Therefore, the state transfer equation is:

Dp[i]=dp[i-1] when I is odd

DP[I]=DP[I-1]+DP[I>>1] When I is an even number

    (Note that this topic only shows 9 numbers)

1#include <stdio.h>2#include <stdlib.h>3 #defineM 10000000004 5 Long Longcombinatories[1000001];6 7 intMainvoid)//This problem can not be used full backpack, will be timed out8 {9     intN, I;Tencombinatories[1] =1;//This place is going to be set to 1 . One  A      for(i =2; I <1000001; i++) -     { -         ifI2==1) theCombinatories[i] = combinatories[i-1]; -         Else -Combinatories[i] = combinatories[i-1] + combinatories[i >>1]; -Combinatories[i]%=M; +     }     -  +      while(~SCANF ("%d", &N)) Aprintf"%d\n", Combinatories[n]%M); at  -     return 0; -}

Dp:sumsets (POJ 2229)

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.