Blue Bridge Cup Algorithm Practice - YBH Counting

Topic requirements:

resource constraints

Time limit: 1.0s Memory limit: 256.0MB

Problem Description

  YBH is poor at mathematics, and she can't distinguish 4, 5, and 8 when counting; we define YBH[i] as the value of i corresponding to YBH's notation.
  Regulations: YBH[4] = 5, YBH[5] = 8; YBH[i] operation rules are as follows:
  ① YBH[i+j] = YBH[i] + YBH[j]
  ② YBH[i*j] = YBH [i] * YBH[j]
  We will find that the values ​​of YBH[i] calculated by different methods are different, for example: when i=20,
  YBH[20] = 5*YBH[4] = 25;
  YBH [20] = 4*YBH[5] = 32;
  YBH[20] = YBH[4] * YBH[5] = 40;
  ......
  We stipulate: F[i] is the maximum of YBH[i] Value, now please write a program, input a number n, output the value of F[n], if F[n] is meaningless, output -1.

input format

  A number n(8 <= n <= 1000)

output format

  Outputs the value of F[n].

sample input

20

sample output

40

sample input

11

sample output

-1

Data Size and Conventions

  8 <= n <= 1000

analyze:

The label given to the topic is DP, and you can analyze it according to the general problem-solving ideas of DP.

1. Define the array and determine the meaning of its elements.

Whether to define a one-dimensional or two-dimensional array depends on the specific topic and then analyze it. According to this question, we can define a one-dimensional array, YBH[i], and the value stored in the array is the maximum value sought by the question.

2. Find the relationship between the elements of the array.

First of all, the title points out how to get YBH[i]: YBH[i+j] = YBH[i] + YBH[j], YBH[i*j] = YBH[i] * YBH[j], and the initial YBH There are only YBH[4] and YBH[5], so the condition for the existence of YBH[i] is i = 4*x + 5*y. Or can it be found that all YBHs make sense after a bound. The subscripts 4 and 5 in YBH are meaningful, then we subtract 4 from 4 and 5, so that 4 = 0, 5 = 1 can be obtained, and with 1, any number can be obtained by addition. So we can find 4 consecutive numbers that YBH exists, so that we can add 4 or 5 to these 4 numbers to get all the following numbers, so that we don't need to judge whether the following YBH is meaningful. This group of 4 consecutive numbers can be obtained by simple calculation: 16, 17, 18, 19. So after i>16, we don't need to judge whether YBH is meaningful.

Next, let's get the maximum value of YBH[i]. There are two ways to get YBH[i].

1: i cannot be obtained by multiplication, only by addition, so YBH[i] = YBH[i-4]+YBH[4], or YBH[i] = YBH[i-5]+YBH[5], we only The maximum value is required.

2: Multiplication can get i. So YBH[i] adds a way to get it, that is, YBH[i] = YBH[i / 4] * YBH[4] or YBH[i] = YBH[i / 5] * YBH[5]. Of course, multiplication can be obtained by addition, and we can judge and take the maximum value together.

3. Determine the initial value. The title has given two initial values, YBH[4] = 5, YBH[5] = 8

write the code:

n = int(input())
YBH = [0 for i in range(n + 1)]
if n >= 8 and n <= 1000:
    for i in range(1, n + 1):
        if i < 16:
            if i < 6:
                if i == 4:
                    YBH[i] = 5
                elif i == 5:
                    YBH[i] = 8
                else:
                    YBH[i] = -1
            else:
                if i % 4 == 1 or i % 5 == 4:
                    YBH[i] = max(YBH[i - 4] + 5, YBH[i - 5] + 8)
                else:
                    YBH[i] = -1
        else:
            if i % 4 == 0 and i % 5 == 0:
                YBH[i] = max(YBH[i // 4] * YBH[4], YBH[i // 5] * YBH[5], YBH[i - 4] + 5, YBH[i - 5] + 8)
            elif i % 4 == 0 and i % 5 != 0:
                YBH[i] = max(YBH[i // 4] * YBH[4], YBH[i - 4] + 5, YBH[i - 5] + 8)
            elif i % 4 != 0 and i % 5 == 0:
                YBH[i] = max(YBH[i // 5] * YBH[5], YBH[i - 4] + 5, YBH[i - 5] + 8)
            else:
                YBH[i] = max(YBH[i - 4] + 5, YBH[i - 5] + 8)
print(YBH[n])

Guess you like

Origin blog.csdn.net/youngwyj/article/details/122328627