POJ 2229 Complete Backpack deformation (solving integer splitting problems)

Source: Internet
Author: User

Integer split problem: Given a positive integer n, divide n into several numbers and ask how many methods?

This is called the sub-Problem of integer splitting, and the number required to split is the power of 2.

Definition Dp[i][k] Represents the number of split schemes for the number I when enumerated to the K number.

Then there is the state transition equation:

DP[I][K] = Dp[i][k-1] + dp[i-num[k]][k];

A friend who is familiar with the full backpack can see that this equation is the same as the state transfer equation of the complete backpack, the second dimension can be omitted, as long as I from small to large enumeration can be.

1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 6 Const intMOD =1000000000;7 Const intN =1000001;8 intDp[n];9 Ten voidInit () One { Adp[0] =1; -      for(intj =0; (1<< j) < N; J + + ) -     { the          for(inti = (1<< j); i < N; i++ ) -         { -Dp[i] + = Dp[i-(1<<j)]; -             if(Dp[i] >= MOD) dp[i]-=MOD; +         } -     } + } A  at intMain () - { - init (); -     intN; -      while(SCANF ("%d", &n)! =EOF) -     { inprintf"%d\n", Dp[n]); -     } to     return 0; +}

In addition, there are recursive practices:

1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 6 Const intMOD =1000000000;7 Const intN =1000001;8 intDp[n];9 Ten voidInit () One { Adp[1] =1; -dp[2] =2; -      for(inti =3; i < N; i++ ) the     { -         if(I &1 ) -         { -Dp[i] = dp[i-1]; +         } -         Else +         { ADp[i] = dp[i-1] + dp[i >>1]; at             if(Dp[i] >= MOD) dp[i]-=MOD; -         } -     } - } -  - intMain () in { - init (); to     intN; +      while(SCANF ("%d", &n)! =EOF) -     { theprintf"%d\n", Dp[n]); *     } $     return 0;Panax Notoginseng}

POJ 2229 Complete Backpack deformation (solving integer splitting problems)

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.