Title Description
Tintin recently indulged in a number game. The game seems simple, but after many days of research, Tintin found that it was not easy to win the game under simple rules. The game is like this, in front of you there is a circle of integers (total n), you have to divide it into m parts in order, the number of parts within the sum of the sum of the m results of 10 modulo and then multiply, and finally get a number k. The requirement of the game is to make your K max or Min.
For example, for the following lap number (n=4,m=2):
Minimum required, ((2-1) mod x ((4+3) mod) =1x7=7, when maximum value is required ((2+4+3) mod x ( -1 mod) =9x9=81. It is particularly noteworthy that the results of the 10 modulo are non-negative, either negative or positive.
Tintin asked you to write a program to help him win the game.
Input/output format
Input format:
The first line of the input file has two integers, n (1≤n≤50) and M (1≤m≤9). The following n rows have an integer in each row, with an absolute value not greater than 104, given in order of the number in the circle, and end to end.
Output format:
The output file has two lines, each containing a non-negative integer. The first line is the minimum value your program gets, and the second row is the maximum value.
Input/Output sample
Input Sample # #:
4 243-12
Sample # # of output:
781
------------------------------------------------------------
Copy to Back
F[i][j][k] indicates that I to j is divided into K-segments, and the transfer and product are maximal as
The mold is taken when the mold is taken
WARN: Be careful m==1
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespacestd;Const intn= -, m= A;intn,m,a[n<<1],s[n<<1];intf[n<<1][n<<1][m],g[n<<1][n<<1][m],mx=-1e9,mn=1e9;inlineintSumintIintj) { return(s[j]-s[i-1]+Ten)%Ten;}voiddp () { for(intI=1; i<=n*2; i++) f[i][i][1]=g[i][i][1]= (a[i]+Ten)%Ten; for(intI=1; i<=n*2; i++) for(intj=i+1; j<=n*2&&j<=i+n-1; j + +) {f[i][j][1]=g[i][j][1]=sum (I,J);if(j-i+1==n&&m==1) Mx=max (mx,f[i][j][1]), Mn=min (mn,g[i][j][1]); intNum=min (m,j-i+1); for(intk=2; k<=num;k++) {F[i][j][k]=-1e9;g[i][j][k]=1e9; for(intt=1; t<=j-i;t++) {F[i][j][k]=max (f[i][j][k],f[i][j-t][k-1]*sum (j-t+1, J)); G[I][J][K]=min (g[i][j][k],g[i][j-t][k-1]*sum (j-t+1, J)); } if(j-i+1==n&&k==m) Mx=max (Mx,f[i][j][k]), mn=min (mn,g[i][j][k]); } }}intMain () {scanf ("%d%d",&n,&m); for(intI=1; i<=n;i++) scanf ("%d", &a[i]), a[i+n]=a[i]= (a[i]%Ten+Ten)%Ten; for(intI=1; i<=n*2; i++) s[i]= (s[i-1]+a[i]+Ten)%Ten; DP (); printf ("%d\n%d", MN,MX);}
NOIP2003PJ Digital game [ring DP]