Integer partitioning (interval DP) dynamic programming

Source: Internet
Author: User

Topic Link: point here ....


For two integers n, m, we need to add M-1 in N and divide n into m segments to find out the maximum product of this m segment. (Number of digits of 1<=n<=1e19,m<=n)


Exercises

1, set Dp[i][j][k] is the maximum product of k in the interval [i,j] multiplication number.

2, note that when merging the 22 merge into Dp[i][j][0], it is directly merged into an integer without multiplication.

3, 22 when merging into Dp[i][j][k] (k!=0), we use multiplication to merge them together, and there is no omission.


Code:

#include <cstdio> #include <cstring> #include <iostream> #include <sstream> #include < algorithm> #include <vector> #include <bitset> #include <set> #include <queue> #include < stack> #include <map> #include <cstdlib> #include <cmath> #define LL long #define PB push_back #de Fine PA pair<int,int> #define CLR (a,b) memset (A,b,sizeof (a)) #define Lson lr<<1,l,mid #define Rson lr<< 1|1,mid+1,r #define BUG (x) printf ("%d++++++++++++++++++++%d\n", x,x) #define Key_value ch[ch[root][1]][0] #pragma
Comment (linker, "/stack:102400000000,102400000000") const LL MOD = 1000000007;
const int N = 100+15;
const int MAXN = 1E5+15;
const int letter = 130;
Const LL INF = 1e7;
Const double Pi=acos (-1.0);
const double EPS=1E-10;
using namespace Std;
    inline int read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | |
    Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; ch=geTCHAR ();}
return x*f;
int m;
LL n,b[25],dp[25][25][25],bin[25];
    Dp[i][j][k] represents the maximum value of k multiplication in the i-j interval int main () {int TC;
    scanf ("%d", &AMP;TC);
    Bin[0]=1;
        while (tc--) {cin>>n>>m;
        m--;
        CLR (dp,0);
        int k=0;
        while (n) {b[++k]=n%10,n/=10;}
        Reverse (b+1,b+k+1);
        for (int i=1;i<=k;i++) dp[i][i][0]=b[i];
                for (int len=2;len<=k;len++) {for (int i=1;i<=k;i++) {int j=i+len-1;
                if (j>k) break;
                DP[I][J][0]=DP[I][J-1][0]*10+DP[J][J][0]; for (int x=1;x<=min (j-i,m); x + +) {for (int y=i;y<j;y++) {for (int z=0;z<=
                        Min (x-1,min (m,y-i)); z++) {Dp[i][j][x]=max (dp[i][j][x],dp[i][y][z]*dp[y+1][j][x-z-1]); }}}} cout<<dp[1][k][m]<<
    Endl
return 0;
 }



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.