Blue Bridge Cup official website question record python

Blue Bridge Cup official website question record python

Since many questions will appear in the 2020, 2021, and 2022 provincial competitions, some of them have been done in previous articles and will not be discussed here.

1. Space

Xiaolan plans to use 256MB of memory space to open an array. Each element of the array is a 32-bit binary integer. If the space occupied by the program and the auxiliary space required for memory maintenance are not considered, how many 32-bit binary integers can be stored in 256MB of space? Integer?
import os
import sys

# 请在此输入您的代码
## 32位占32bit
## 1MB = 1024KB
## 1KB = 1024byte
## 1byte = 8bits
print(256*1024*1024*8//32)

2. Increasing sequence

For a matrix of letters, we call an increasing sequence in the matrix when we find two letters in the matrix that are in the same row, the same column, or on the same 45-degree diagonal, looking from left to right. , or increasing from top to bottom. For example, LANN QIAO
in the following matrix has 13 increasing sequences such as LN, LN, AN, AN, IO, AO, LQ, AI, NO, NO, AQ, IN, and AN . Note that when two letters are arranged from bottom left to top right, the order is different when looking from left to right and looking from top to bottom. For the following matrix of 30 rows and 50 columns, how many increasing sequences are there in total?





VLPWJVVNNZSWFGHSFRBCOIJTPYNEURPIGKQGPSXUGNELGRVZAG
SDLLOVGRTWEYZKKXNKIRWGZWXWRHKXFASATDWZAPZRNHTNNGQF
ZGUGXVQDQAEAHOQEADMWWXFBXECKAVIGPTKTTQFWSWPKRPSMGA
BDGMGYHAOPPRRHKYZCMFZEDELCALTBSWNTAODXYVHQNDASUFRL
YVYWQZUTEPFSFXLTZBMBQETXGXFUEBHGMJKBPNIHMYOELYZIKH
ZYZHSLTCGNANNXTUJGBYKUOJMGOGRDPKEUGVHNZJZHDUNRERBU
XFPTZKTPVQPJEMBHNTUBSMIYEGXNWQSBZMHMDRZZMJPZQTCWLR
ZNXOKBITTPSHEXWHZXFLWEMPZTBVNKNYSHCIQRIKQHFRAYWOPG
MHJKFYYBQSDPOVJICWWGGCOZSBGLSOXOFDAADZYEOBKDDTMQPA
VIDPIGELBYMEVQLASLQRUKMXSEWGHRSFVXOMHSJWWXHIBCGVIF
GWRFRFLHAMYWYZOIQODBIHHRIIMWJWJGYPFAHZZWJKRGOISUJC
EKQKKPNEYCBWOQHTYFHHQZRLFNDOVXTWASSQWXKBIVTKTUIASK
PEKNJFIVBKOZUEPPHIWLUBFUDWPIDRJKAZVJKPBRHCRMGNMFWW
CGZAXHXPDELTACGUWBXWNNZNDQYYCIQRJCULIEBQBLLMJEUSZP
RWHHQMBIJWTQPUFNAESPZHAQARNIDUCRYQAZMNVRVZUJOZUDGS
PFGAYBDEECHUXFUZIKAXYDFWJNSAOPJYWUIEJSCORRBVQHCHMR
JNVIPVEMQSHCCAXMWEFSYIGFPIXNIDXOTXTNBCHSHUZGKXFECL
YZBAIIOTWLREPZISBGJLQDALKZUKEQMKLDIPXJEPENEIPWFDLP
HBQKWJFLSEXVILKYPNSWUZLDCRTAYUUPEITQJEITZRQMMAQNLN
DQDJGOWMBFKAIGWEAJOISPFPLULIWVVALLIIHBGEZLGRHRCKGF
LXYPCVPNUKSWCCGXEYTEBAWRLWDWNHHNNNWQNIIBUCGUJYMRYW
CZDKISKUSBPFHVGSAVJBDMNPSDKFRXVVPLVAQUGVUJEXSZFGFQ
IYIJGISUANRAXTGQLAVFMQTICKQAHLEBGHAVOVVPEXIMLFWIYI
ZIIFSOPCMAWCBPKWZBUQPQLGSNIBFADUUJJHPAIUVVNWNWKDZB
HGTEEIISFGIUEUOWXVTPJDVACYQYFQUCXOXOSSMXLZDQESHXKP
FEBZHJAGIFGXSMRDKGONGELOALLSYDVILRWAPXXBPOOSWZNEAS
VJGMAOFLGYIFLJTEKDNIWHJAABCASFMAKIENSYIZZSLRSUIPCJ
BMQGMPDRCPGWKTPLOTAINXZAAJWCPUJHPOUYWNWHZAKCDMZDSR
RRARTVHZYYCEDXJQNQAINQVDJCZCZLCQWQQIKUYMYMOVMNCBVY
ABTCRRUXVGYLZILFLOFYVWFFBZNFWDZOADRDCLIRFKBFBHMAXX

First of all, for each point, the answer will be +1 only if the other point is to its right, below or diagonally. When it is diagonal, it cannot be on its upper left, that is If you use that point to draw the axis, the fourth quadrant will not work. Because he said that for the diagonal line y = -x, going to the right and going down are the same direction, so the fourth quadrant does not work, and for the diagonal line y = x, going to the right and going down are different directions. , so both quadrants 1.3 and 3 are OK, so only the first quadrant is not.

Then we only need to traverse all the points once, and then traverse all the points another time. When the requirements are met, the answer will be +1. 50 30 50*30 The time complexity will not be very high.

import os
import sys

Please enter your code here

data = ["VLPWJVVNNZSWFGHSFRBCOIJTPYNEURPIGKQGPSXUGNELGRVZAG",\
"SDLLOVGRTWEYZKKXNKIRWGZWXWRHKXFASATDWZAPZRNHTNNGQF",\
"ZGUGXVQDQAEAHOQEADMWWXFBXECKAVIGPTKTTQFWSWPKRPSMGA",\
"BDGMGYHAOPPRRHKYZCMFZEDELCALTBSWNTAODXYVHQNDASUFRL",\
"YVYWQZUTEPFSFXLTZBMBQETXGXFUEBHGMJKBPNIHMYOELYZIKH",\
"ZYZHSLTCGNANNXTUJGBYKUOJMGOGRDPKEUGVHNZJZHDUNRERBU",\
"XFPTZKTPVQPJEMBHNTUBSMIYEGXNWQSBZMHMDRZZMJPZQTCWLR",\
"ZNXOKBITTPSHEXWHZXFLWEMPZTBVNKNYSHCIQRIKQHFRAYWOPG",\
"MHJKFYYBQSDPOVJICWWGGCOZSBGLSOXOFDAADZYEOBKDDTMQPA",\
"VIDPIGELBYMEVQLASLQRUKMXSEWGHRSFVXOMHSJWWXHIBCGVIF",\
"GWRFRFLHAMYWYZOIQODBIHHRIIMWJWJGYPFAHZZWJKRGOISUJC",\
"EKQKKPNEYCBWOQHTYFHHQZRLFNDOVXTWASSQWXKBIVTKTUIASK",\
"PEKNJFIVBKOZUEPPHIWLUBFUDWPIDRJKAZVJKPBRHCRMGNMFWW",\
"CGZAXHXPDELTACGUWBXWNNZNDQYYCIQRJCULIEBQBLLMJEUSZP",\
"RWHHQMBIJWTQPUFNAESPZHAQARNIDUCRYQAZMNVRVZUJOZUDGS",\
"PFGAYBDEECHUXFUZIKAXYDFWJNSAOPJYWUIEJSCORRBVQHCHMR",\
"JNVIPVEMQSHCCAXMWEFSYIGFPIXNIDXOTXTNBCHSHUZGKXFECL",\
"YZBAIIOTWLREPZISBGJLQDALKZUKEQMKLDIPXJEPENEIPWFDLP",\
"HBQKWJFLSEXVILKYPNSWUZLDCRTAYUUPEITQJEITZRQMMAQNLN",\
"DQDJGOWMBFKAIGWEAJOISPFPLULIWVVALLIIHBGEZLGRHRCKGF",\
"LXYPCVPNUKSWCCGXEYTEBAWRLWDWNHHNNNWQNIIBUCGUJYMRYW",\
"CZDKISKUSBPFHVGSAVJBDMNPSDKFRXVVPLVAQUGVUJEXSZFGFQ",\
"IYIJGISUANRAXTGQLAVFMQTICKQAHLEBGHAVOVVPEXIMLFWIYI",\
"ZIIFSOPCMAWCBPKWZBUQPQLGSNIBFADUUJJHPAIUVVNWNWKDZB",\
"HGTEEIISFGIUEUOWXVTPJDVACYQYFQUCXOXOSSMXLZDQESHXKP",\
"FEBZHJAGIFGXSMRDKGONGELOALLSYDVILRWAPXXBPOOSWZNEAS",\
"VJGMAOFLGYIFLJTEKDNIWHJAABCASFMAKIENSYIZZSLRSUIPCJ",\
"BMQGMPDRCPGWKTPLOTAINXZAAJWCPUJHPOUYWNWHZAKCDMZDSR",\
"RRARTVHZYYCEDXJQNQAINQVDJCZCZLCQWQQIKUYMYMOVMNCBVY",\
"ABTCRRUXVGYLZILFLOFYVWFFBZNFWDZOADRDCLIRFKBFBHMAXX"]

ans = 0
for i in range(30):
    for j in range(50):
        for k in range(30):
            for l in range(50):
                if data[i][j] < data[k][l] and ((i == k and l > j) or (j == l and k > i) or (abs(i-k) == abs(j-l) and not (k <= i and l <= j))):
                    ans += 1
print(ans)

3. Arithmetic prime sequence

2,3,5,7,11,13,… is a sequence of prime numbers. Similar: 7,37,67,97,127,157.
7,37,67,97,127,157 Such an arithmetic sequence composed entirely of prime numbers is called an arithmetic prime number sequence.
The sequence above has a tolerance of 30 and a length of 6.
In 2004, Green collaborated with the Chinese Terence Tao to prove that there is an arithmetic sequence of prime numbers of any length. This is an amazing result in the field of number theory!
Based on this theory, please use the computer in your hand to search with confidence:
What is the minimum tolerance of an arithmetic prime sequence of length 10?

This question is a fill-in-the-blank question, as long as you can calculate it, and the time complexity should not be exaggerated. The idea is to traverse the tolerance from small to large. Then iterate through all the prime numbers. I feel that the prime numbers and the tolerance will not be very large, so the range is from 2 to 1000. If I can't find it, I can expand it. It’s easy to understand by looking at the code

import os
import sys
import math
### 反正是填空题,能算出来就行
# 请在此输入您的代码
def check(n):
  for i in range(2,int(math.sqrt(n)) + 1):
    if n % i == 0: return False 
  return True
find = False
for d in range(2,1000): # 公差
  if find : break
  for i in range(2,1000): # 素数
    if find: break
    n = i
    res = 0
    while check(n):
      res += 1
      n += d
    if res == 10:
      print(d)
      find = True

4. Form a team

As the coach of the basketball team, you need to select one player from positions 1 to 5 from the following list to form the team's starting lineup. Each player's rating when playing positions 1 to 5 is shown in the table below. Please calculate the maximum possible sum of ratings for positions 1 to 5 of the starting lineup?
insert image description here

This problem is easy to solve if you use Lingo, haha, linear programming, but you can also do it if you traverse all possibilities in python. I even think that you can use Monte Carlo to find the optimal value, but here is a lesson This is a method that won't time out. After all, there were only two fill-in-the-blank questions last year, so I still need to practice more procedural questions.

Use dynamic programming to do it, f[i][j] represents the maximum score of the first i students who participated in the first j positions. w[i][j] represents the score of the i-th student in the j-th position. If the i-th student does not participate in any position, then f[i][j] = f[i-1][j], if The i-th student wants to participate in the j-th position, then f[i][j] = f[i-1][j-1] + w[i][j].

PS: I thought about it carefully and found that there was something wrong with my approach, but I happened to be able to pass this question. Because if the 20th person chooses the first four positions, then there is no way to solve this question. So when filling in the blanks, it might be better to use brute force or Monte Carlo.

import os
import sys

# 请在此输入您的代码
w = [[0]*30 for _ in range(30)]
f = [[0]* 30 for _ in range(30)]
# f[i][j] 表示前i个学生参加j个位置
w[1][1] = 97;
w[1][2] = 90;
w[2][1] = 92;
w[2][2] = 85;
w[2][3] = 96;
w[3][5] = 93;
w[4][4] = 80;
w[4][5] = 86;
w[5][1] = 89;
w[5][2] = 83;
w[5][3] = 97;
w[6][1] = 82;
w[6][2] = 86;
w[7][4] = 87;
w[7][5] = 90;
w[8][2] = 97;
w[8][3] = 96;
w[9][3] = 89;
w[10][1] = 95;
w[10][2] = 99;
w[11][3] = 96;
w[11][4] = 97;
w[12][4] = 93;
w[12][5] = 98;
w[13][1] = 94;
w[13][2] = 91;
w[14][2] = 83;
w[14][3] = 87;
w[15][3] = 98;
w[15][4] = 97;
w[15][5] = 98;
w[16][4] = 93;
w[16][5] = 86;
w[17][1] = 98;
w[17][2] = 83;
w[17][3] = 99;
w[17][4] = 98;
w[17][5] = 81;
w[18][1] = 93;
w[18][2] = 87;
w[18][3] = 92;
w[18][4] = 96;
w[18][5] = 98;
w[19][4] = 89;
w[19][5] = 92;
w[20][2] = 99;
w[20][3] = 96;
w[20][4] = 95;
w[20][5] = 81;
for i in range(1,21):
  for j in range(6):
      f[i][j] = f[i-1][j]
      if j >= 1:
        f[i][j] = max(f[i][j],f[i-1][j-1] + w[i][j])
print(f[20][5])

The following is a brute force method, which is not bad and the time complexity is not very high.

import os
import sys

# 请在此输入您的代码
w = [[0]*30 for _ in range(30)]
f = [[0]* 30 for _ in range(30)]
w[1][1] = 97;
w[1][2] = 90;
w[2][1] = 92;
w[2][2] = 85;
w[2][3] = 96;
w[3][5] = 93;
w[4][4] = 80;
w[4][5] = 86;
w[5][1] = 89;
w[5][2] = 83;
w[5][3] = 97;
w[6][1] = 82;
w[6][2] = 86;
w[7][4] = 87;
w[7][5] = 90;
w[8][2] = 97;
w[8][3] = 96;
w[9][3] = 89;
w[10][1] = 95;
w[10][2] = 99;
w[11][3] = 96;
w[11][4] = 97;
w[12][4] = 93;
w[12][5] = 98;
w[13][1] = 94;
w[13][2] = 91;
w[14][2] = 83;
w[14][3] = 87;
w[15][3] = 98;
w[15][4] = 97;
w[15][5] = 98;
w[16][4] = 93;
w[16][5] = 86;
w[17][1] = 98;
w[17][2] = 83;
w[17][3] = 99;
w[17][4] = 98;
w[17][5] = 81;
w[18][1] = 93;
w[18][2] = 87;
w[18][3] = 92;
w[18][4] = 96;
w[18][5] = 98;
w[19][4] = 89;
w[19][5] = 92;
w[20][2] = 99;
w[20][3] = 96;
w[20][4] = 95;
w[20][5] = 81;
score = [0]*5
res = 0
for i in range(1,21):
    for j in range(1,21):
        for k in range(1,21):
            for l in range(1,21):
                for h in range(1,21):
                    if i != j and i != k and i != l and i != h and j != k and j != l and j != h and k != l and k != h and l != h :
                        for o in range(1,6): # 打第几个位置
                            score[0],score[1],score[2],score[3],score[4] = w[i][o],w[j][(o+1)%5],w[k][(o+2)%5],w[l][(o+3)%5],w[h][(o+4)%5]
                            res = max(res,sum(score))
print(res)

5. Seven-segment code

Xiao Lan wants to use a seven-segment digital tube to represent a special text.
insert image description here

insert image description here

This requires the use of some basic libraries of python.

itertools.combiations('abcdefg',3) can use the seven letters abcdefg to take out three of them, and the combination of all three (the order is not considered)
itertools.permutations('abc') can take all the sequences of abc The combinations are output
, then we only need to traverse all the combinations and sequences to see if they can be connected.

Why do we need to traverse the order? Because it is possible that abcd a is connected to c, and c is connected to b. When judging in this way, because a is not connected to b, we judge that it is not connected, but in fact it is connected. So when you want to traverse to acbd, you are right, and then just jump out of the loop.

import os
import sys
import itertools
# 请在此输入您的代码
W = {"a": ["b", "f"],
     "b": ["a", "g", "c"],
     "c": ["b", "d", "g"],
     "d": ["c", "e"],
     "e": ["d", "f", "g"],
     "f": ["a", "e", "g"],
     "g": ["b", "c", "e", "f"]
     }  # 字典,存放距离为1的字符,用来判别
s1 = []
count = 0
for i in range(1,8):
  a = list(itertools.combinations('abcdefg',i))
  for j in a:
    s1.append(''.join(j))  # 元组转化为字符串存入列表中
for i in s1:
  a = itertools.permutations(i)
  for j in a:
    for k in range(len(j) - 1):
      if j[k+1] not in W[j[k]]:break
    else:
      count += 1
      break
print(count)

6. Reduced fractions

insert image description here

It’s traversal, there’s not much to say, but if you run it directly on the official website of the Blue Bridge Cup, it will time out. If you use your own fill-in-the-blank questions during the competition, you won’t care whether it will time out or not.

import os
import sys

# 请在此输入您的代码
# def gcd(a,b):
#   if b == 0: return a
#   else: return gcd(b,a%b)
# res = 0
# for i in range(1,2021): # 分母
#   for j in range(1,2021):
#     if gcd(i,j) == 1: res += 1
# print(res)

print(2481215)

7. Jump

insert image description here
insert image description here

This question can be done with dp without even thinking, that is, the maximum value to this step is equal to the maximum value to the previous step plus the weight of this step.

import os
import sys

# 请在此输入您的代码
n,m = map(int,input().split())
map1 = []
for i in range(n):
  a = [int(x) for x in input().split()]
  map1.append(a)
dp = [[-200]*(m+3) for _ in range(n+3)] # 多出来三个为了防止最开始的时候会溢出
for i in range (3,n+3):
  for j in range(3,m+3):
    if i == 3 and j == 3 :
      dp[i][j] = map1[i-3][j-3]
    else:
      dp[i][j] = map1[i-3][j-3] + max(dp[i][j-1],dp[i][j-2],dp[i][j-3],dp[i-1][j],dp[i-1][j-1],dp[i-1][j-2],dp[i-2][j],dp[i-2][j-1],dp[i-3][j])
print(dp[n+2][m+2])

8. Sequence evaluation

insert image description here

The question requires the last 4 digits. We don’t need to consider how the first digits change, as it will not affect the next four digits. Just ask for help one by one. My i is 20190318. It is 6 digits less than 20190324 because the first three 1s are no longer needed. Then our a, b, and c are 4, 5, and 6 digits. When I cycle through it for the first time, I ask for help. is the seventh

a,b,c = 3,5,9 
res = 0
for _ in range(20190318):
  res = (a + b + c) % 10000
  a,b,c = b,c,res
print(res)

Continuously updating...

Guess you like

Origin blog.csdn.net/abc1234564546/article/details/128907797