[Blue Bridge Cup Python Group] Detailed explanation of Python problem-solving ideas in Group B of the 13th Blue Bridge Cup Provincial Competition in 2022

Detailed explanation of Python problem-solving ideas in Group B of the 13th Blue Bridge Cup Provincial Competition

Because the competition is held online this year, the organizing committee has made some adjustments to the questions, changing the original 5 fill-in-the-blanks + 5 programming questions into 2 fill-in-the-blanks + 8 programming questions, which is said to prevent plagiarism. In fact, the adjustment of the questions is one aspect. More importantly, the difficulty of the questions has also increased. Several questions have to be derived through the axioms or theorems in number theory. I only did the seven questions of ABCDGHJ during the exam, and the remaining questions were either selectively skipped, or I had thought about it for a long time but had no idea. After the game, I asked Brother Duan and Brother Bing, who played ACM, and rethought a few questions. Although they have not been implemented in the code, they also understand the general problem-solving ideas. Next, I will write in this blog Generally write down your own simple understanding.

QA: arrange letters

insert image description here
It is relatively convenient to use Python for this question. The general idea is to convert the string into a list, use the list's built-in quick sort + merge to sort and then convert it back to the string. The code is as follows:

x=input()
x=list(x)
x.sort()
print(''.join(x))

QB: looking for integers

insert image description here

答案就是蓝桥杯省赛日期:2022040920220409

Although this question is in the front position, it cannot be solved violently. Instead, it needs to be transformed and solved by using the Chinese remainder theorem in several rounds.

System of linear congruential equations

insert image description here

Chinese remainder theorem

Unknown question:

有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二。问物几何?

Resolving it into a system of linear equations means that
insert image description here
for this system of equations, we can construct a number for each equation so that its modulo other modulus is 0, and the modulus of its own modulus is the answer. That is to say, the above equation can be constructed:
insert image description here
adding all of them together can get a set of special solutions, x0=128. Get the general solution x=x0+Mt (M is the least common multiple of the modulus, and t belongs to all integers), so that the smallest positive integer solution x=23 can be obtained.
insert image description here
So we can directly set the formula for this problem, in the words of Brother Bing, it is a set of boards. However, before this, you can use the operation of the Chinese remainder theorem to select all prime numbers from 2 to 49 as the modulus m, find the corresponding remainder a, and bring it into the solution. The specific code is as follows:

import gmpy2

mod_num=[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47]
rem_num=[1,2,4,4,0,10,0,18,15,16,27,22,1,11,5]

def crt(b,m):

    #乘积
    M = 1
    for i in range(len(m)):
        M *= m[i]
    #求M/mi
    Mm = []
    for i in range(len(m)):
        Mm.append(M // m[i])
    #求Mm[i]的乘法逆元
    Mm_ = []
    for i in range(len(m)):
        _,a,_ = gmpy2.gcdext(Mm[i],m[i])
        Mm_.append(int(a % m[i]))
    #求MiM'ibi的累加
    y = 0
    for i in range(len(m)):

        y += (Mm[i] * Mm_[i] * b[i])
    y = y % M
    return y

print(crt(rem_num,mod_num))

QC: paper size

insert image description here
This question is also simple, so I won't go into details.

index=int(input()[-1])
h,w=1189,841
i=0
while i<index:
    h=int(h/2)
    h, w = max(h, w), min(h, w)
    i+=1
print(h)
print(w)

QD: numerical order

insert image description here
The general idea: use the list x to store the n numbers 1-n, store the digit sum of its corresponding elements in the list value, sort x according to the value, and use zip to pack and unpack the two lists. code show as below:

n=int(input())
m=int(input())
x=[i for i in range(1,n+1)]
index=[]
for item in x:
    value = list(map(int, list(str(item))))
    index.append(sum(value))
result_list = [i for _,i in sorted(zip(index,x))]
print(result_list[m-1])

QE: Honeycomb

insert image description here
jumped up

QF: Elimination Game

insert image description here
General idea: Don't stick to the details, but give an overview of the overall arrangement mode. Divide the repetition patterns of edge characters into two patterns, odd repetition and even repetition. Even repetition must be eliminated, and odd repetition may remain. Eliminate judging one edge character at a time until there is no edge character.

QG: Value of full permutation

insert image description here
The full permutation in the title is a smoke bomb. At the beginning, I also tried to use backtracking, recursion and other methods to find the full permutation, and then find its value, but for the full permutation of the scale of 2022, it is absolutely time-out and out-of-memory, so I use the backtracking algorithm The points that can be cheated are very limited, after all, the memory consumption is too large.
This topic should correspond to a certain theorem of number theory. I mainly talk about my own problem-solving ideas.
For the list of 1 2 3, the full arrangement and its scores are as follows
insert image description here
Use the recursive method to find this value. Let n be the full permutation value of 1-n, leicheng(n) calculates the class product of 1-n, and leijia() calculates the cumulative sum of 1-n, then: f[n]=leijia(n-1)· leicheng (n-1)+f[n-1]·n
simple derivation process
insert image description here
code:

def leicheng(num):
    if num==1:
        return 1
    if num==2:
        return 2
    res=1
    for i in range(num,1,-1):
        res*=i
        res%=998244353
    return res

def leijia(num):
    res=0
    for i in range(num+1):
        res+=i
    return res%998244353

n=int(input())
ans=[0 for i in range(n+1)]
ans[1]=0
ans[2]=1
ans[3]=9

for i in range(4,n+1):
    ans[i]=(leijia(i-1)*leicheng(i-1)+ ans[i - 1]* i)%998244353
print(ans[n])

QH: skill upgrade

insert image description here
Each time, the one with the highest income is selected for upgrading, and after the upgrade, it is attenuated, and then sorted after the attenuation. A total of M times are upgraded.

import math


n, m = list(map(int, input().split()))
mp = {
    
    }
for i in range(0, n):
    Ai, Bi = list(map(int, input().split()))
    mp[i + 1] = [Ai, Bi, 0]
# print(mp)

temp = sorted(mp.items(), key=lambda mp: mp[1][0], reverse=True)
# print(temp)

res = 0
for i in range(0, m):
    index = temp[0][0]
    A = temp[0][1][0]
    B = temp[0][1][1]
    times = temp[0][1][2]
    # print(index,A,B,times)

    if times >= math.ceil(A / B):
        A = 0
        B = 0

    res += A
    A -= B
    times += 1
    mp[index] = [A, B, times]
    temp = sorted(mp.items(), key=lambda mp: mp[1][0], reverse=True)

print(res)

QI: longest non-decreasing subsequence

insert image description here

QJ: Optimal clearing scheme

insert image description here
code:

n,k=list(map(int,input().split()))
a=list(map(int,input().split()))

index=0
sum_num=0
op=[-1 for i in range(k)]
while index<n-k+1:

    if 0 in a[index:index+k]:

        index+=1
    else:
        # print(index)
        for i in range(index,index+k):
            a[i]-=1
        sum_num+=1

print(sum(a)+sum_num)

Guess you like

Origin blog.csdn.net/weixin_43427721/article/details/124072813