Lanqiao Cup 2022 Group A python

Lanqiao Cup 2022 Group A python

Group A has five different questions than Group B.

Question 1: Paper Knife

insert image description here

Just cut the four sides first, then line them first and then line them up. It’s pretty simple.

# 20行 横着19刀,竖着21*20
print(19+21*20+4)

Question 3: Number of prime factors

insert image description here

I feel like this question is the same as a template that approximates a number. Just change it a little bit. There is nothing to say. Just look at the code.

# 任何一个正整数,都可以拆成质因数的乘积,最多只有一个质因数大于根号N

if __name__ == '__main__':
    n = int(input())
    res = 0
    for i in range(2,n+1):  #寻找他的质因数
        if i > n/i: break # i就循环到根号N  
        if n % i == 0:  # 找到的一定是质数,因为约数在之前一定会被前面的质数给消掉
            res += 1
        while n % i == 0: 
            n = n // i
    if n > 1: res += 1
    print(res)

Question 4: Rectangular splicing

insert image description here

Once you find the pattern, it's easy to do

Quadrilaterals: three have a side of the same length, two have sides of the same length, and the sum of the other two sides is equal to a side of the third rectangle

Hexagon: Two sides with the same length, the sum of the sides of two rectangles is equal to one side of the third rectangle

Octagon: Other

Write according to the above rules, there are actually many repetitions after reading a lot. . Just look at it carefully

# 四边形:三个都有一条长度相同的边、两个有长度相同的边,且另外两条长度边之和等于第三个矩形的一条边
# 六边形:两个有长度相同的边、两个矩形的各自一条边之和等于第三个矩形的一条边
# 八边形: 其他

def find(A1,A2,A3):
    for i in A1:
        if A2.count(i) >= 1 and A3.count(i) >= 1: return 4
    
    for i in range(2):
        if A2.count(A1[i]) >= 1 and A3.count(A1[i]) == 0:  # 找到A1 和 A2 有相同长度的边
            for j in range(2):
                if A2[j] == A1[i]:
                    for k in range(2):
                        if A1[i ^ 1] + A2[j ^ 1] == A3[k]: return 4
    for i in range(2):
        if A2.count(A1[i]) == 0 and A3.count(A1[i]) >= 0:  # 找到A1 和 A3 有相同长度的边
            for j in range(2):
                if A3[j] == A1[i]:
                    for k in range(2):
                        if A1[i ^ 1] + A3[j ^ 1] == A2[k]: return 4
    
    for j in range(2):
        if A3.count(A2[j]) >= 1 and A1.count(A2[j]) == 0:
            for k in range(2):
                if A2[j] == A3[k]:
                    for i in range(2):
                        if A2[j ^ 1] + A3[k ^ 1] == A1[i] : return 4
## *******************************和上面基本一样,但return 6 不能在return 4 前面,所以我上面把return 6 删了,又复制了一份*********************************************** ##
    for i in range(2):
        if A2.count(A1[i]) >= 1 and A3.count(A1[i]) == 0:  # 找到A1 和 A2 有相同长度的边
            for j in range(2):
                if A2[j] == A1[i]:
                    for k in range(2):
                        if A1[i ^ 1] + A2[j ^ 1] == A3[k]: return 4
                    else : return 6
    for i in range(2):
        if A2.count(A1[i]) == 0 and A3.count(A1[i]) >= 0:  # 找到A1 和 A3 有相同长度的边
            for j in range(2):
                if A3[j] == A1[i]:
                    for k in range(2):
                        if A1[i ^ 1] + A3[j ^ 1] == A2[k]: return 4
                    else : return 6
    
    for j in range(2):
        if A3.count(A2[j]) >= 1 and A1.count(A2[j]) == 0:
            for k in range(2):
                if A2[j] == A3[k]:
                    for i in range(2):
                        if A2[j ^ 1] + A3[k ^ 1] == A1[i] : return 4
                    else: return 6
    for i in A1:
        for j in A2:
            for k in A3:
                if i + j == k or i + k == j or j + k == i: return 6
    return 8


if __name__ == '__main__':
    n = int(input())
    for _ in range(n):
        edge = [int(x) for x in input().split()]
        A1 = edge[0:2]
        A2 = edge[2:4]
        A3 = edge[4:6]
        print(find(A1,A2,A3))

Question 6: Reorder

insert image description here

I don’t need to say the initial sum, what we need is how to find the subsequent sum? It is to collect the requested order of each position. If a certain position is requested the most times, we put the maximum value in its position. Then we only need to ask for the order, and then sort the order array and the original A array respectively, and multiply them together.

Code 1

if __name__ == '__main__':
    n = int(input())
    A = [int(x) for x in input().split()]
    s = [0]*(n+1)
    m = int(input())
    C = [0]*(n+1) # 用来存储每个数被查询了几次
    sum0 = 0
    for i in range(m):
        a,b = map(int,input().split())
        for i in range(a-1,b):  # 数组中的下标要-1
            sum0 += A[i]
            C[i] += 1
    A.sort()
    C.sort()
    sum1 = 0
    for i in range(n):
        sum1 += C[i]*A[i]
    print(sum1 - sum0)

This code looks very simple, but you will find that for i in range(a-1,b) needs to be looped n times at worst, and the overall time complexity is O(mn), which is too big. We need to find a way. How to optimize this area

Then we thought of the prefix sum for summing, so that we don’t have to go through all the sums. We can also use a similar idea to find the number of times, and look at the final optimized code - all passed on ACWING

if __name__ == '__main__':
    n = int(input())
    A = [int(x) for x in input().split()]
    s = [0]*(n+1)
    for i in range(1,n+1):
        s[i] = s[i-1] + A[i-1]
    m = int(input())
    C = [0]*(n+1) # 用来存储每个数被查询了几次
    sum0 = 0
    for i in range(m):
        a,b = map(int,input().split())
        # for i in range(a-1,b):  # 数组中的下标要-1
        #     sum0 += A[i]
        #     C[i] += 1
        sum0 += (s[b] - s[a-1])
        C[a-1] += 1
        C[b] -=1
    for i in range(1,n):
        C[i] += C[i-1]
    C = C[:n]
    A.sort()
    C.sort()
    sum1 = 0
    for i in range(n):
        sum1 += C[i]*A[i]
    print(sum1 - sum0)

Question 10: Splitting of Numbers

insert image description here

I feel that this question is still a template of the approximate number. . Just change a little bit, maybe time complexity should be considered? I didn’t find the question test here, but the test data is no problem. It’s quite simple. Let’s just look at the code directly.

import math
if __name__ == '__main__':
    T = int(input())
    for _ in range(T):
        n = int(input())
        res = [0] * (int(math.sqrt(n))+1)
        for i in range(2,n):
            if i > n/i: break
            while n % i == 0:
                res[i] += 1
                n = n//i
        if n > 1: 
            print('no')
            continue
        if 1 not in res:print('yes')

Guess you like

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