您的位置:首页 > 娱乐 > 八卦 > 蓝桥·算法赛——第05场

蓝桥·算法赛——第05场

2024/12/23 12:19:54 来源:https://blog.csdn.net/qq_52652401/article/details/141259338  浏览:    关键词:蓝桥·算法赛——第05场

文章目录

  • 小白入门赛
    • 1-十二生肖
    • 2-欢迎参加福建省大学生程序设计竞赛
    • 3-匹配二元组的数量
    • 4-元素交换
    • 5-下棋的贝贝
    • 6-方程
  • 强者挑战赛
    • 1-元素交换
    • 2-下棋的贝贝
    • 3-方程
    • 4-学《博弈论》的贝贝
    • 5-学《数论》的贝贝
    • 6-学《树论》的贝贝

小白入门赛

1-十二生肖

2-欢迎参加福建省大学生程序设计竞赛

3-匹配二元组的数量

N = int(input())
lst = [0] + list(map(int, input().split()))
s = set()
for i in range(1, N + 1):s.add(i * lst[i])
print(N - len(s)) # 少的数目即存在匹配

4-元素交换

N = int(input())
lst = list(map(int, input().split()))
cnt = 0 # 记录奇数位上1的个数
for i in range(0, N << 1, 2):if lst[i] == 1:cnt += 1
print(min(cnt, N - cnt))

5-下棋的贝贝

from math import sqrtn = int(input())
L = int(sqrt(n)) # 最大正方形边长
ans = 4 * (L - 2) * (L - 2) + 4 * 3 * (L - 2) + 4 * 2
diff = n - L * L # 剩下的棋子数(<= 2L)
if diff > 0 and diff <= L:# 若剩余棋子数(0, L] (边缘棋子 + 3 * 剩余棋子 - 2)ans += 4 * diff - 2
elif diff > L: # diff == 0则输出结果即可# 若剩余棋子数(L, 2L] (两条边的棋子) L + 3 * L - 2 + (diff - L) + 3 * (diff - L) - 2ans += 4 * diff - 4
print(ans)

6-方程

MOD = pow(10, 9) + 7
def multiply(arr1, arr2):arr = [[0, 0], [0, 0]]for i in range(2):for j in range(2):arr[i][j] = arr1[i][0] * arr2[0][j] + arr1[i][1] * arr2[1][j]if arr[i][j] >= MOD: # 存在负数取余数问题arr[i][j] %= MODreturn arrdef qpow(Mat, n): # 计算 Mat 矩阵的 n 次方ans = [[1, 0], [0, 1]] # 单位阵while n > 0:if n & 1 == 1: # 累积ans = multiply(ans, Mat)Mat = multiply(Mat, Mat) # 倍增n >>= 1return ansT = int(input())
for _ in range(T):n, k = map(int, input().split())temp = qpow([[k, -1], [1, 0]], n - 1)print((temp[0][0] * k + temp[0][1] * 2) % MOD)

强者挑战赛

1-元素交换

N = int(input())
lst = list(map(int, input().split()))
cnt = 0 # 记录奇数位上1的个数
for i in range(0, N << 1, 2):if lst[i] == 1:cnt += 1
print(min(cnt, N - cnt))

2-下棋的贝贝

from math import sqrtn = int(input())
L = int(sqrt(n)) # 最大正方形边长
ans = 4 * (L - 2) * (L - 2) + 4 * 3 * (L - 2) + 4 * 2
diff = n - L * L # 剩下的棋子数(<= 2L)
if diff > 0 and diff <= L:# 若剩余棋子数(0, L] (边缘棋子 + 3 * 剩余棋子 - 2)ans += 4 * diff - 2
elif diff > L: # diff == 0则输出结果即可# 若剩余棋子数(L, 2L] (两条边的棋子) L + 3 * L - 2 + (diff - L) + 3 * (diff - L) - 2ans += 4 * diff - 4
print(ans)

3-方程

MOD = pow(10, 9) + 7
def multiply(arr1, arr2):arr = [[0, 0], [0, 0]]for i in range(2):for j in range(2):arr[i][j] = arr1[i][0] * arr2[0][j] + arr1[i][1] * arr2[1][j]if arr[i][j] >= MOD: # 存在负数取余数问题arr[i][j] %= MODreturn arrdef qpow(Mat, n): # 计算 Mat 矩阵的 n 次方ans = [[1, 0], [0, 1]] # 单位阵while n > 0:if n & 1 == 1: # 累积ans = multiply(ans, Mat)Mat = multiply(Mat, Mat) # 倍增n >>= 1return ansT = int(input())
for _ in range(T):n, k = map(int, input().split())temp = qpow([[k, -1], [1, 0]], n - 1)print((temp[0][0] * k + temp[0][1] * 2) % MOD)

4-学《博弈论》的贝贝

5-学《数论》的贝贝

6-学《树论》的贝贝