The 15th Lanqiao Cup Simulation Competition (Phase 1) Python

Creation is not easy, friends are welcome to follow, like and collect!

Fill in the blanks

Problem Description

  Please find a minimum number greater than 2022. After converting this number to hexadecimal, all digits (excluding leading 0) are letters (A to F).
  Please submit the decimal form of this number as your answer.

answer submission

  This is a fill-in-the-blank question. You only need to calculate the result and submit it. The result of this question is an integer. Only fill in this integer when submitting the answer. No points will be awarded if you fill in excess content.

Given the Python code for the calculation:

current_year = 2023

# 找到大于 2022 的最小数,使得其十六进制表示中所有数位都是字母
while True:
    current_year += 1
    hex_representation = hex(current_year)[2:].upper()  # 转换为十六进制并去掉前缀0x,然后转换为大写
    if all(char.isalpha() for char in hex_representation):  # 检查是否所有字符都是字母
        break

# 输出结果
print(current_year)

Answer: 2730

Problem Description

  In Excel, column names use a combination of English letters. The first 26 columns use one letter, from A to Z, and the next 26*26 columns use a combination of two letters, from AA to ZZ.
  What is the name of column 2022?

answer submission

  This is a fill-in-the-blank question. You only need to calculate the result and submit it. The result of this question is a string composed of uppercase letters. Only fill in this string when submitting the answer. No points will be awarded if you fill in extra content.

Given the Python code for the calculation:

def get_excel_column_name(column_number):
    result = ''
    while column_number > 0:
        column_number, remainder = divmod(column_number - 1, 26)
        result = chr(65 + remainder) + result  # 65 是字符 'A' 的 ASCII 值
    return result

# 获取第 2022 列的名称
column_2022_name = get_excel_column_name(2022)

# 输出结果
print(column_2022_name)

Answer: BYT

Problem Description

  For a date, we can calculate the sum of the digits of the year, or we can calculate the sum of the digits of the month and day respectively. How many days are there in total from January 1, 1900 to December 31, 9999? The sum of the digits of the year is equal to the sum of the digits of the month plus the sum of the digits of the day.
  For example, November 13, 2022 meets the requirement because 2+0+2+2=(1+1)+(1+3).
  Please submit the total quantity on the date that meets the conditions.

answer submission

  This is a fill-in-the-blank question. You only need to calculate the result and submit it. The result of this question is an integer. Only fill in this integer when submitting the answer. No points will be awarded if you fill in excess content.

Given the Python code for the calculation:

def get_digit_sum(n):
    # 计算一个数字的各位数字之和
    digit_sum = 0
    while n > 0:
        digit_sum += n % 10
        n //= 10
    return digit_sum

def count_dates():
    count = 0
    for year in range(1900, 10000):
        for month in range(1, 13):
            days_in_month = 31
            if month == 2:
                if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
                    days_in_month = 29
                else:
                    days_in_month = 28
            elif month in [4, 6, 9, 11]:
                days_in_month = 30
            for day in range(1, days_in_month + 1):
                year_sum = get_digit_sum(year)
                month_sum = get_digit_sum(month)
                day_sum = get_digit_sum(day)
                if year_sum == month_sum + day_sum:
                    count += 1
    return count

# 统计满足条件的日期数量
num_dates = count_dates()

# 输出结果
print(num_dates)

Answer: 70910

Problem Description

  Xiaolan has 30 numbers, namely: 99, 22, 51, 63, 72, 61, 20, 88, 40, 21, 63, 30, 11, 18, 99, 12, 93, 16 , 7, 53, 64, 9, 28, 84, 34, 96, 52, 82, 51, 77.
  Xiao Lan can take out two numbers with different serial numbers from these numbers. There are 30*29/2=435 ways in total.
  ​Among these 435 methods, how many methods have the product of two numbers greater than or equal to 2022?

answer submission

  This is a fill-in-the-blank question. You only need to calculate the result and submit it. The result of this question is an integer. Only fill in this integer when submitting the answer. No points will be awarded if you fill in excess content.

Given the Python code for the calculation:

numbers = [99, 22, 51, 63, 72, 61, 20, 88, 40, 21, 63, 30, 11, 18, 99, 12, 93, 16, 7, 53, 64, 9, 28, 84, 34, 96, 52, 82, 51, 77]

count = 0

for i in range(len(numbers)):
    for j in range(i + 1, len(numbers)):
        product = numbers[i] * numbers[j]
        if product >= 2022:
            count += 1

# 输出结果
print(count)

Answer: 189

Problem Description

  小蓝个个30线60结构的数矩阵、柩阵中中每个个数数为0或1.
  110010000011111110101001001001101010111011011011101001111110
  0100000000010100011011000000100 10110001111100010101100011110
  1011 00001101011101101011011001000110111111010000000110110000   01010110010001000011100010011110011000111011110101001 1001011   010011011010011110111101111001001001010111110001101000100011   1010010110001101000011010110000 00110110110100100110111101011   101111000000101000111001100010110000100110001001000101011001   0011 10111010001011110000001111100001010101001110011010101110 11101010   011111100011001110100101001011110011000101011000100111001011   01101000110101111001101111101 0111110010100101000110111010110   001110000111100100101110001011101010001100010111110111011011   111100001000001100010110101100111001001111100100110000001101   001110010000000111011110000011000010101000111000 000110101101   100100011101011111001101001010011111110010111101000010000111   11001010011010110000110111110 1010011000110101100000110001010   110101101100001110000100010001001010100010110100100001000011   100100000100001101010101001101000101101000000101111110001010   111100110010   101111000100000100011000010001011111001010010001010110001010   00101000111010101000010001001 1101001010101101101010111100101   001111110000101100010111111100000100101010000001011101100001   101011110010000010010110000100001010011111100011011000110010   010001110011   000101000101000010010010110111000010101111001101100110011100   10001110011001111100011001100 1111100001110110111001001000111   011110011110110110011011001011010000100100101010110000010011   101110011101   Run up, down, left and right racing up, down, left and right 标 1 个 个 1 标 为 两 两 两 两 两 两A possession position (comprehensive self) that is connected to a certain position is a combination of one person and one person.   请 请 请 请 阵 阵 阵 阵 阵 最 最 最 最 最 最 最 最 最 最 最 最 最 最 最 最 最 最




























answer submission

  This is a fill-in-the-blank question. You only need to calculate the result and submit it. The result of this question is an integer. Only fill in this integer when submitting the answer. No points will be awarded if you fill in excess content.

Give the Python code for the calculation

def dfs(matrix, i, j, visited):
    if i < 0 or i >= len(matrix) or j < 0 or j >= len(matrix[0]) or matrix[i][j] == 0 or visited[i][j]:
        return 0
    
    visited[i][j] = True
    size = 1

    directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
    for direction in directions:
        new_i, new_j = i + direction[0], j + direction[1]
        size += dfs(matrix, new_i, new_j, visited)

    return size

def find_largest_connected_block(matrix):
    rows, cols = len(matrix), len(matrix[0])
    visited = [[False for _ in range(cols)] for _ in range(rows)]
    max_size = 0

    for i in range(rows):
        for j in range(cols):
            if matrix[i][j] == 1 and not visited[i][j]:
                block_size = dfs(matrix, i, j, visited)
                max_size = max(max_size, block_size)

    return max_size

# Example matrix
matrix = [
    [1, 1, 0, 0, 1, 0],
    [0, 1, 1, 0, 1, 1],
    [0, 0, 1, 0, 0, 0],
    [1, 1, 0, 0, 0, 1],
    [0, 0, 1, 1, 0, 0],
    [1, 1, 1, 0, 1, 1]
]

# Find the size of the largest connected block
largest_block_size = find_largest_connected_block(matrix)

# Output the result
print(largest_block_size)

Tip:You can use depth-first search (DFS) or breadth-first search (BFS) to find connected partitions in a matrix and calculate their sizes.

Answer: 5

Problem Description

  The sum of the digits of a number refers to the sum of the digits in each digit of the number. For example, the digit sum of 2023 is 2+0+2+3=7.
  For the following numbers (8 rows, 8 in each row, 64 in total), what is the digit sum of the smallest number? (If there are multiple, please answer the one that appears earliest) 454771 329157 801601 580793 755604 931703 529875 361797 604358 529564 574776 821517 1955 63 688516 223321 607845   376981 910643 413754 945725 817853 651778 350775 676550   743580 857864 529622 320921 595409 486860 951114 558787< /span>   316935 487808 939526 900568 423326 298936 927671 539773 136326 717022 886675 466684 436470 558644 2672 31 902422   896582 670074 912386 702393 722092 834842 12634 6 606526   284772 603562 543328 707484 533688 380468 233733 257995







answer submission

  This is a fill-in-the-blank question. You only need to calculate the result and submit it. The result of this question is an integer. Only fill in this integer when submitting the answer. No points will be awarded if you fill in excess content.

n = 8
numbers = [
    [454771, 329157, 801601, 580793, 755604, 931703, 529875, 361797],
    [604358, 529564, 574776, 821517, 195563, 688516, 223321, 607845],
    [284772, 603562, 543328, 707484, 533688, 380468, 233733, 257995],
    [896582, 670074, 912386, 702393, 722092, 834842, 126346, 606526],
    [376981, 910643, 413754, 945725, 817853, 651778, 350775, 676550],
    [316935, 487808, 939526, 900568, 423326, 298936, 927671, 539773],
    [136326, 717022, 886675, 466684, 436470, 558644, 267231, 902422],
    [743580, 857864, 529622, 320921, 595409, 486860, 951114, 558787]
]

min_sum = float('inf')
min_number = -1

for i in range(n):
    for j in range(n):
        number = numbers[i][j]
        digit_sum = sum(map(int, str(number))) # 计算数位和

        if digit_sum < min_sum: # 更新最小数位和和对应的数
            min_sum = digit_sum
            min_number = number

print(min_number) # 输出结果

 Answer: 223321

Code question

Problem Description

  Given a day of the week, what day of the week will it be n days later?

Input format

  The first line of input contains an integer w, indicating which day of the week the given day is. w is 1 to 6, which represents Monday to Saturday respectively, and w is 7, which represents Sunday.
  The second line contains an integer n.

Output format

  The output line contains an integer, indicating the day of the week after n days. 1 to 6 represent Monday to Saturday respectively, and 7 represents Sunday.

Sample input

6
10

Sample output

2

Evaluation use case scale and conventions

  ​For all evaluation cases, 1 <= n <= 1000000.

w = int(input())  # 输入给定的天是一周中的哪一天
n = int(input())  # 输入天数间隔 n

day = (w + n - 1) % 7  # 计算 n 天后是一周中的哪一天

if day == 0:
    day = 7

print(day)

Problem Description

  Xiao Lan is responsible for the installation of signal towers in an area. The entire area is a rectangular area. After establishing the coordinate axis, the coordinates of the southwest corner are (0, 0), the coordinates of the southeast corner are (W, 0), and the coordinates of the northwest corner The coordinates are (0, H), and the coordinates of the northeast corner are (W, H). Where W, H are all integers.
  He set up signal towers at n locations. Each signal tower can cover a circle with itself as the center and a radius of R (including the edge).
  In order to check the signal coverage, Xiaolan plans to test all points with integer horizontal and vertical coordinates in the area to check the signal status. The abscissa range is from 0 to W, the ordinate range is from 0 to H, and a total of (W+1) * (H+1) points are tested.
  Given the location of the signal tower, how many of these (W+1)*(H+1) points are covered by the signal.

Input format

  The first line of input contains four integers W, H, n, R. Adjacent integers are separated by a space.
  Next n lines, each line contains two integers x, y, representing the coordinates of a signal tower. The signal towers may overlap, indicating that two signal transmitters are installed in the same location.

Output format

  The output line contains an integer representing the answer.

Sample input

10 10 2 5
0 0
7 0

Sample output

57

Evaluation use case scale and conventions

  For all evaluation cases, 1 <= W, H <= 100, 1 <= n <= 100, 1 <= R <= 100, 0 <= x <= W, 0 < = y <= H.

def is_covered(x, y, towers, R):
    for tower in towers:
        tower_x, tower_y = tower
        if (x - tower_x) ** 2 + (y - tower_y) ** 2 <= R ** 2:
            return True
    return False

def count_covered_points(W, H, n, R, towers):
    count = 0
    for x in range(W + 1):
        for y in range(H + 1):
            if is_covered(x, y, towers, R):
                count += 1
    return count

# 读取输入
W, H, n, R = map(int, input().split())
towers = [tuple(map(int, input().split())) for _ in range(n)]

# 计算答案
result = count_covered_points(W, H, n, R, towers)

# 输出结果
print(result)

Problem Description

  Xiaolan has a rectangular water area of ​​size n * m. Xiaolan divides this water area into n rows and m columns. The rows are numbered from 1 to n, and the columns are numbered from 1 to m. The width of each row and column is unit 1 .
  Now, this water area is full of aquatic plants, and Xiao Lan wants to clean up the aquatic plants.
  Each time, Xiaolan can clean a rectangular area, from row r1 (inclusive) to row r2 (inclusive) and column c1 (inclusive) to column c2 (inclusive).
  After a period of cleaning, how many places have not been cleaned?

Input format

  The first line of input contains two integers n, m, separated by a space.
  The second line contains an integer t, indicating the number of cleanups.
  Next t lines, each line contains four integers r1, c1, r2, c2. Adjacent integers are separated by a space, indicating a cleanup. Please pay attention to the order of entry.

Output format

  The output line contains an integer, indicating the area that has not been cleaned.

Sample input

2 3
2
1 1 1 3
1 2 2 2

Sample output

2

Sample input

30 20
2
5 5 10 15
6 7 15 9

Sample output

519

Evaluation use case scale and conventions

  For all evaluation cases, 1 <= r1 <= r2 <= n <= 100, 1 <= c1 <= c2 <= m <= 100, 0 <= t <= 100 .

def read_area(n, m, t):  
    # 计算矩形的总面积  
    total_area = n * m  
  
    # 存储每次清理的面积  
    cleaned_area = 0  
  
    # 遍历所有的清理记录  
    for _ in range(t):  
        r1, c1, r2, c2 = map(int, input().split())  
        # 计算本次清理的面积  
        cleaned_area += (r2 - r1 + 1) * (c2 - c1 + 1)  
  
    # 计算未清理的面积  
    uncleaned_area = total_area - cleaned_area  
  
    # 返回结果  
    return uncleaned_area  
  
n, m = map(int, input().split())  
t = int(input())  
print(read_area(n, m, t))

Problem Description

  Xiao Lan is preparing to slide in an open field. The height of this field is different. Xiao Lan uses a matrix with n rows and m columns to represent the field. The values ​​in the matrix represent the height of the field.
  If Xiao Lan is in a certain position, and the height of one of his upper, lower, left, and right positions is (strictly) lower than the current height, Xiao Lan can slide over, and the sliding distance is 1.
  If Xiao Lan is at a certain position, and the height of all his upper, lower, left, and right positions is greater than or equal to the current height, Xiao Lan's sliding is over.
  Xiao Lan cannot slide out of the field represented by the matrix.
  Xiao Lan can choose a position to start gliding. What is the maximum distance that Xiao Lan can glide?

Input format

  The first line of input contains two integers n, m, separated by a space.
  Next n lines, each line contains m integers, separated by a space between adjacent integers, indicating the height of each position in turn.

Output format

  The output line contains an integer representing the answer.

Sample input

4 5
1 4 6 3 1
11 8 7 3 1
9 4 5 2 1
1 3 2 2 1

Sample output

7

Sample description

  The sliding positions are (2, 1), (2, 2), (2, 3), (3, 3), (3, 2), (4, 2), (4, 3) at a time.

Evaluation use case scale and conventions

  For 30% of the evaluation cases, 1 <= n <= 20, 1 <= m <= 20, 0 <= height <= 100.
  For all evaluation cases, 1 <= n <= 100, 1 <= m <= 100, 0 <= height <= 10000.

# 检查一个位置是否在矩阵范围内
def is_valid(matrix, row, col):
    return 0 <= row < len(matrix) and 0 <= col < len(matrix[0])

# 定义上、下、左、右四个方向
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]

# DFS函数,在当前位置进行DFS
def dfs(matrix, row, col, visited):
    height = matrix[row][col]
    max_distance = 0

    for d in directions:
        new_row = row + d[0]
        new_col = col + d[1]

        if is_valid(matrix, new_row, new_col) and not visited[new_row][new_col] and matrix[new_row][new_col] < height:
            visited[new_row][new_col] = True
            distance = dfs(matrix, new_row, new_col, visited)
            max_distance = max(max_distance, distance)

    return max_distance + 1

# 读取输入
n, m = map(int, input().split())
matrix = [list(map(int, input().split())) for _ in range(n)]

# 计算答案
result = 0

for i in range(n):
    for j in range(m):
        visited = [[False] * m for _ in range(n)]
        visited[i][j] = True
        distance = dfs(matrix, i, j, visited)
        result = max(result, distance)

# 输出结果
print(result)

Hint:This problem can be solved by depth-first search (DFS). We can perform DFS starting from each position, find adjacent positions where we can continue gliding, calculate the distance of gliding, and then choose the result with the longest gliding distance as the answer.

Problem Description

  Xiaolan has a sequence a[1], a[2], ..., a[n].
  Given a positive integer k, for each serial number i between 1 and n, a[i-k], a[i-k+1], ..., a[i +k] What is the minimum value among these 2k+1 numbers? When a subscript exceeds the range of 1 to n, the number does not exist, and only those values ​​that exist are used to find the minimum value.

Input format

  The first line of input contains the integer n.
  The second line contains n integers, representing a[1], a[2], ..., a[n] respectively.
  The third line contains an integer k.

Output format

  Output a line containing n integers, representing the minimum value obtained for each sequence number.

Sample input

5
5 2 7 4 3
1

Sample output

2 2 2 3 3

Evaluation use case scale and conventions

  For 30% of the evaluation cases, 1 <= n <= 1000, 1 <= a[i] <= 1000.
  For 50% of the evaluation cases, 1 <= n <= 10000, 1 <= a[i] <= 10000.
  For all evaluation cases, 1 <= n <= 1000000, 1 <= a[i] <= 1000000.

from collections import deque

def find_min_in_window(n, arr, k):
    result = []
    window = deque()

    # 处理前 k 个元素
    for i in range(k):
        while window and arr[i] < arr[window[-1]]:
            window.pop()
        window.append(i)

    # 处理剩余的元素
    for i in range(k, n):
        result.append(arr[window[0]])

        # 移除不在当前窗口范围内的元素
        while window and window[0] <= i - k:
            window.popleft()

        # 移除比当前元素大的元素
        while window and arr[i] < arr[window[-1]]:
            window.pop()

        window.append(i)

    # 处理最后一个窗口
    result.append(arr[window[0]])

    return result

# 读取输入
n = int(input())
arr = list(map(int, input().split()))
k = int(input())

# 计算答案
result = find_min_in_window(n, arr, k)

# 输出结果
print(" ".join(map(str, result)))

Problem Description

  Xiao Lan has a rope of length L. Every time it is folded in half, the length becomes half of the original length. How many times will the length be less than 1 after being folded in half?
  For example, when L=6, the length of folding in half once is 3, the length of folding in half twice is 1.5, and the length of folding in half three times is 0.75, so the length after 3 times does not exceed 1.

Input format

  The input line contains an integer L.

Output format

  The output line contains an integer representing the answer.

Sample input

6

Sample output

3

Sample input

255

Sample output

8

Sample input

256

Sample output

8

Sample input

257

Sample output

9

Evaluation use case scale and conventions

  For 50% of the evaluation cases, 1 < L <= 10**9 (10 to the 9th power).
  For all evaluation cases, 1 < L <= 10**18 (10 to the 18th power).

L = int(input())

count = 0
while L > 1:
    L /= 2
    count += 1

print(count)

Problem Description

  Given a string of length n consisting of uppercase letters, delete m characters from the string so that the remaining string has the smallest lexicographic order.

Input format

  The first line of input contains two integers n, m separated by a space.
  The second line contains a string of length n.

Output format

  Output a line containing a string of length n-m, representing the answer.

Sample input

7 3
LANQIAO

Sample output

AIAO

Evaluation use case scale and conventions

  For 30% of the evaluation cases, 1 <= m < n <= 30.
  For 60% of the evaluation cases, 1 <= m < n <= 1000.
  For all evaluation cases, 1 <= m < n <= 1000000.

def remove_characters(s, m):  
    # 将字符串排序  
    s = sorted(s)  
    n = len(s)  
    # 删除尽可能多的字符  
    i = 0  
    while i < n and m > 0:  
        if s[i] != 'A':  
            i += 1  
        else:  
            s.pop(i)  
            m -= 1  
    # 如果删除的字符数量小于 m,则将删除的字符数增加到 m  
    if m < n - i:  
        s = s[:-i]  
    return ''.join(s)

hint:

This problem can be solved by greedy algorithm. We can start from the beginning of the string and delete as many characters as possible to get the lexicographically smallest string.

Specifically, we can follow these steps:

  1. Sort the string so that you can easily remove characters and get the lexicographically smallest string.
  2. Starting from the beginning of the string, delete as many characters as possible so that the remaining string has the smallest lexicographic order. The number of deleted characters should not exceed m.
  3. If the number of deleted characters is less than m, increase the number of deleted characters to m.
  4. Return the remaining string.
  5. The time complexity of this algorithm is O(n log n), where n is the length of the string. This is because we need to sort the strings, and the time complexity of sorting is usually O(n log n).

Guess you like

Origin blog.csdn.net/m0_66827057/article/details/134347103