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
C:
1#include <stdio.h>2#include <string.h>3 #defineINF 1<<304 inta[ -],s[ -],m,n;5 intdp1[ -][ the],dp2[ -][ the];6 intMaxn,minn;7InlineintMaxintAintb)8 {9 returnA>b?a:b;Ten } OneInlineintMinintAintb) A { - returnA<b?a:b; - } the intMain () - { - //freopen ("A.txt", "R", stdin); - inti,j,k,l,tmp; + while(SCANF ("%d%d", &m,&n) = =2) - { + for(i=1; i<=m;i++) A { atscanf"%d",&a[i]); -a[m+i]=A[i]; - } -s[0]=0; - for(i=1; i<=2*m;i++) - { ins[i]=s[i-1]+A[i]; - } tomaxn=-inf,minn=INF; + for(l=1; l<=m;l++) - { the for(i=1; i<=2*m;i++) * for(j=1; j<=n;j++) $ {Panax Notoginsengdp1[i][j]=INF; -dp2[i][j]=-INF; the } + for(i=l;i<=m+l-1; i++) A { theTmp= (s[i]-s[l-1])%Ten; + if(tmp<0) tmp+=Ten; -dp1[i][1]=tmp;dp2[i][1]=dp1[i][1]; $ } $ for(j=2; j<=n;j++) - for(i=j+l-1; i<=m+l-1; i++) - for(k=j+l-2; k<i;k++) the { -Tmp= (S[i]-s[k])%Ten;Wuyi if(tmp<0) tmp+=Ten; theDp1[i][j]=min (dp1[i][j],dp1[k][j-1]*tmp); -Dp2[i][j]=max (dp2[i][j],dp2[k][j-1]*tmp); Wu } -Minn=min (dp1[l+m-1][n],minn); AboutMaxn=max (dp2[l+m-1][N],MAXN); $ } -printf"%d\n%d\n", MINN,MAXN); - } - return 0; A}
View Code
C++:
1#include <cstdio>2#include <algorithm>3 using namespacestd;4 5 intn,m,max,min,s[ -],d[ -][Ten],dp[ -][Ten];6 voidDP (inta[])7 {8 inti,j,k;9 for(i=1; i<=n;i++)Tens[i]=s[i-1]+A[i]; One for(i=0; i<=n;i++) A for(j=0; j<=m;j++) - { -d[i][j]=0; thedp[i][j]=-1u>>1; - } -d[0][0]=dp[0][0]=1; - for(i=1; i<=n;i++) +d[i][1]=dp[i][1]= (s[i]%Ten+Ten)%Ten; - for(j=2; j<=m;j++) + for(i=j;i<=n;i++) A for(k=j-1; k<i;k++) at { -D[i][j]=max (d[i][j],d[k][j-1]* ((s[i]-s[k)%Ten+Ten)%Ten));//Number of k+1 to number I to part J -Dp[i][j]=min (dp[i][j],dp[k][j-1]* ((s[i]-s[k)%Ten+Ten)%Ten)); - } -max=Max (max,d[n][m]); -min=min (min,dp[n][m]); in } - intMain () to { + //freopen ("A.txt", "R", stdin); - while(SCANF ("%d%d", &n,&m) = =2) the { * inti,j,a[ the]; $max=0;Panax Notoginsengmin=-1u>>1; - for(i=1; i<=n;i++) the { +scanf"%d", A +i); Aa[n+i]=A[i]; the } + for(j=0; j<n;j++) -DP (A +j); $printf"%d\n%d\n", Min,max); $ } -}
View Code
Number games (two great gods written)