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)