Describe
http://codevs.cn/problem/1017/
Given an n-digit number, add K multiplication sign to the middle of the number, making the final product maximum.
1017 Product Maximum
2000 Noip National League popularization Group Noip National League Improvement Group
time limit: 1 sspace limit: 128000 KBtitle level: Golden GoldTitle Description
Description
This year is the "2000--World Mathematics Year" established by the International Mathematical Union, and coincides with the 90 anniversary of the birth of our famous mathematician Mr. Hua. In Mr. Hua's hometown of Jintan, Jiangsu, organized a wonderful mathematical intelligence contest activities, a good friend of your XZ is also fortunate to participate. During the event, the host gives all the participants a title:
There is a number string of length n, requiring the player to use K multiplication sign to divide it into k+1 parts, to find a division, so that this k+1 part of the product can be the largest.
At the same time, in order to help the player to understand test instructions correctly, the moderator also gave the following example:
There is a number string: 312, when N=3,k=1 will have the following two kinds of methods:
1) 3*12=36
2) 31*2=62
At this point, the result that meets the requirements of the question is: 31*2=62
Now, please help your good friend XZ design a program to get the right answer.
Enter a description
Input Description
There are two lines of input to the program:
The first line has a total of 2 natural numbers n,k (6≤n≤40,1≤k≤6)
The second line is a number string with a length of N.
Output description
Output Description
The result is displayed on the screen, and the maximum product (a natural number) should be output relative to the input.
Sample input
Sample Input
4 2
1231
Sample output
Sample Output
62
Data range and Tips
Data Size & Hint
Because older, the data is actually relatively small, with a long long can pass
Analysis
The crux of the matter is whether we can see how to divide it. The problem can be seen as the use of K-multiplication sign in the first n number to find the optimal solution. So the first n number of K multiplication sign is used by the number of pre-J (J<n) to use the k-1 multiplication sign, the result is multiplied by the number of [J+1,n]. If dp[i][ K] indicates the optimal solution obtained using K-multiplication sign in the first I-number, then Dp[i][k]=max{dp[j][k-1]*[j+1,i]} (j<i). Here we need to preprocess an array of a, where a[i][j] represents the number represented by [i,j].
Attention:
1.dp[i][k] from dp[j][k-1], so to take advantage of the state of k-1, so the cycle of k should be in the outer layer.
2. Use k-multiplication sign, at least the first k+1 digits.
Dynamic planning
1#include <bits/stdc++.h>2 using namespacestd;3 4typedefLong Longll;5 Const intmaxn= -, maxk=Ten;6 intn,k;7 CharSTR[MAXN];8 ll A[maxn][maxn],dp[maxn][maxk];9 Ten voidsolve () { One for(intI=1; i<=n;i++) dp[i][0]=a[1][i]; A for(intk=1; k<=k;k++) - for(inti=k+1; i<=n;i++) - for(intj=k;j<i;j++) theDp[i][k]=max (dp[i][k],dp[j][k-1]*a[j+1][i]); -printf"%lld\n", Dp[n][k]); - } - voidinit () { +scanf"%d%d%s", &n,&k,str+1); - for(intI=1; i<=n;i++){ +a[i][i]=str[i]-'0'; A for(intj=i+1; j<=n;j++) ata[i][j]=a[i][j-1]*Ten+ (str[j]-'0'); - } - } - intMain () { - init (); - solve (); in return 0; -}
View Code
Memory Search
1#include <bits/stdc++.h>2 using namespacestd;3 4typedefLong Longll;5 Const intmaxn= -, maxk=Ten;6 intn,k;7 CharSTR[MAXN];8 ll A[maxn][maxn],dp[maxn][maxk];9 Tenll Dfs (intMintk) { One if(Dp[m][k])returnDp[m][k]; A if(k==0)returndp[m][k]=a[1][m]; - for(inti=k;i<m;i++) -Dp[m][k]=max (Dp[m][k],dfs (i,k-1) *a[i+1][m]); the returnDp[m][k]; - } - voidinit () { -scanf"%d%d%s", &n,&k,str+1); + for(intI=1; i<=n;i++){ -a[i][i]=str[i]-'0'; + for(intj=i+1; j<=n;j++) Aa[i][j]=a[i][j-1]*Ten+ (str[j]-'0'); at } - } - intMain () { - init (); -printf"%lld\n", DFS (n,k)); - return 0; in}
View Code
Codevs_1017_ Product Max _ (partitioned dynamic programming/memory Search)