2003 Number Games

Source: Internet
Author: User

Title Description 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):

2

4-1

3

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.

Enter a description Input Description

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 description Output Description

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.

Sample input Sample Input

4 2

4

3

-1

2

Sample output Sample Output

7

81

Data range and Tips Data Size & Hint

En

Exercises

The movement of the return.

First of all, consider the linear condition of the loop (maximum): F "i,j" means the maximum number of J for the first I number, the state transfer equation: F "i,j" = Max (f "i-k,j-1" * ((((Sum "i"-sum "I-k") (mod +10) mod 10)); Preprocessing: Sum "i" =sum "i-1" +a "I" (prefix and), f "i,1" = (sum "i" mod 10+10) mod 10. Answer: F "n,m", ring processing method: Each time the last number to move to the front of the line! Finally, note that the minimum answer is also for Max (0).

var n,m,i,ans2,ans1,ii,s,j,k:longint;

A,sum:array[0..51]of Longint;

F1,f2:array[0..51,0..10]of Longint;

function Max (x,y:longint): Longint;

Begin

If X>y then exit (x);

Exit (y);

End

function min (x,y:longint): Longint;

Begin

If X<y then exit (x);

Exit (y);

End

Begin

READLN (N,M);

For I:=1 to n do read (A[i]);

Ans2:=maxlongint;

For Ii:=1 to N do

Begin

Fillchar (f2,sizeof (F2), 10);

Fillchar (f1,sizeof (F1), 0);

S:=a[n];

For I:=n Downto 2 do a[i]:=a[i-1];

A[1]:=s;

SUM[1]:=A[1];

For i:=2 to n do sum[i]:=sum[i-1]+a[i];

For I:=1 to N do f2[i,1]:= (Sum[i] mod 10+10) mod 10;

For I:=1 to n do f1[i,1]:=f2[i,1];

For I:=1 to N do

For j:=2 to M do

Begin

If J>i then break;

For K:=1 to I-j+1 do

Begin

f1[i,j]:=

Max (F1[i,j],f1[i-k,j-1]* ((((sum[i]-sum[i-k) mod +10) mod 10));

f2[i,j]:=

Min (F2[i,j],f2[i-k,j-1]* ((((sum[i]-sum[i-k) mod) +10) mod 10));

End

End

Ans1:=max (Ans1,f1[n,m]);

Ans2:=min (Ans2,f2[n,m]);

End

Writeln (Max (0,ANS2));

Writeln (ANS1);

End.

2003 Number Games

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.