C language dynamic planning-Sumsets (Hdu 2709)

Source: Internet
Author: User

C language dynamic planning-Sumsets (Hdu 2709)

 

Problem Description Farmer John commanded his cows to search for different sets of numbers that sum to a given number. the cows use only numbers that are an integer power of 2. here are the possible sets of numbers that sum to 7:

1) 1 + 1 + 1 + 1 + 1 + 1 + 1
2) 1 + 1 + 1 + 1 + 1 + 2
3) 1 + 1 + 1 + 2 + 2
4) 1 + 1 + 1 + 4
5) 1 + 2 + 2 + 2
6) 1 + 2 + 4

Help FJ count all possible representations for a given integer N (1 <=n <= 1,000,000 ). input A single line with a single integer, N. output The number of ways to represent N as the indicated sum. due to the potential huge size of this number, print only last 9 digits (in base 10 representation ). sample Input
7
Sample Output
6


 

Question:

Enter an integer to divide the number into an indefinite positive number and only the sum. The number must be the k power of 2 (k is a positive number greater than or equal to 0 ). number of output points. (when the output integer is too large, only the last 9 digits are output)

 

Thought 1:

 

SetA [n]ForNNumber of types;
According to the question, if n is an odd number, it is equal to the previous number.N-1Number of TypesA [n-1], IfNIs there an even number of time-based adders?1Discussion, that is, the key isNFor an even number:
1.NIs odd,A [n] = a [n-1]
2.NIs an even number:
(1) If the addition contains 1, there must be at least two 1, that isN-2After each addition+ 1 + 1, The total number of classes isA [N-2];
(2) If there is no 1 in the numberN/2Multiplied by 2, the total number of classes isA [n/2];
Therefore, the total number of types is as follows:A [n] = a [N-2] + a [n/2];


Code:

 

# Include
 
  
_ Int64 a [1000001]; int main () {_ int64 n; int I; a [1] = 1; a [2] = 2; for (I = 3; I <1000001; I ++) {if (I % 2 = 0) a [I] = a [I-2] + a [I/2]; else a [I] = a [I-1]; a [I] % = 1000000000; // The maximum number of digits is 9 .} while (scanf ("% I64d", & n )! = EOF) printf ("% I64d \ n", a [n]); return 0 ;}
 


 

Thought 2:

DP ideology,

If only 1 can be used, then the number of points for each number is 1.

If this time can be used2Is equal to or greater2NumberNYou canN-2And2Structure is convertedN-2IsD [n] = d [N-2] + d [n](FrontD [N-2]RepresentationNYou can2The number of components, followedD [n]Indicates that n can only be composed1Number of components .)

Then the state transition equation formula comes out (C [n] = 2 ^ n)

D [n] [k] = d [n] [k-1] + d [n-c [k] [k];

Cycle Dimension Reduction:

D [n] = d [n] + d [n-c [k];

 

Code:

 

#include
 
    #include 
  
   __int64 d[1000005],c[25],n,i,j;  int main()  {      while(scanf("%d",&n)!=EOF)     {        memset(d,0,sizeof(d));        c[0]=d[0]=1;      for(i=1;i<=24;i++)          c[i]=c[i-1]<<1;      for(i=0;i<=24&&c[i]<=n;i++)          for(j=c[i];j<=n;j++)              d[j]=(d[j]+d[j-c[i]])%1000000000;          printf("%d\n",d[n]);    }            return 0;}  
  
 


 

 

 

Related Article

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.