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):
When minimum value is required, ((2-1) mod x ((4+3) mod) =1x7=7, when maximum is required, is ((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 multiple test cases, processing to the end of the file, each test the first line 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. Outputs each test case output 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. Sample Input
4 243-12
Sample Output
781
HINT: Classic DP
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intINF =43333;Const intMAX = -;intDp1[max][max],dp2[max][max],sum[max];intn,m;intmin1,max1;intDP (int*a) { for(inti =1; I <= N; i++) Sum[i]= sum[i-1] +A[i]; for(inti =0; I <= N; i++){ for(intj =0; J <= M; j + +) {Dp1[i][j]=0; DP2[I][J]=inf; }} dp1[0][0] = dp2[0][0] =1; for(inti =1; I <= N; i++) dp1[i][1] = dp2[i][1] = (sum[i]%Ten+Ten)%Ten; for(intj =2; J <= M; j + +){ for(intK = J; K <= N; k++){ for(intp = J1; P < K; p++) {Dp1[k][j]= Max (dp1[k][j],dp1[p][j-1]* ((sum[k]-sum[p)%Ten+Ten)%Ten)); DP2[K][J]= Min (dp2[k][j],dp2[p][j-1]* ((sum[k]-sum[p)%Ten+Ten)%Ten)); } }} max1=Max (max1,dp1[n][m]); Min1=min (min1,dp2[n][m]);}intMain () {inta[ -]; while(~SCANF ("%d%d",&n,&m)) {Min1=inf; Max1=0; memset (SUM,0,sizeof(sum)); for(inti =1; I <= N; i++) {scanf ("%d", A +i); A[i+n] =A[i]; } for(inti =0; I < n;i++) DP (a+i); printf ("%d\n%d\n", MIN1,MAX1); } return 0;}
View Code
dp--Digital Games