AtCoder Beginner Contest 254题解
AtCoder Beginner Contest 254\(A \sim E\)题解
A - Last Two Digits
题目描述:给你一个三位数,输出其十位和个位。
思路:根据题意模拟即可。
时间复杂度:\(O(1)\)
参考代码:
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 7 21:54:10 2022
@author: cherish
"""
a = input()
print(a[1:3])
B - Practical Computing
题目描述:给定整数\(N\),输出杨辉三角的前\(N\)行。
思路:根据题意模拟即可
时间复杂度:\(O(n^2)\)
参考代码:
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 7 22:00:31 2022
@author: cherish
"""
n = int(input())
res = [[0 for j in range(n + 1)] for i in range(n + 1)]
res[1][1] = 1
for i in range(2 , n + 1):
for j in range(1 , i + 1):
res[i][j] = res[i - 1][j] + res[i - 1][j - 1]
for i in range(1 , n + 1):
for j in range(1 , i + 1):
print(res[i][j] , end = ' ')
print()
C - K Swap
题目描述:给你一个长度为\(n\)的数组\(A\)和一个整数\(K\),你每次可以选择一个下标\(i (1 \leq i \leq n - k)\),然后交换\(a_i\)和\(a_{i + k}\),问你是否可以通过该方式将数组变有序。
思路:显然将每间隔\(k\)个的分为一组,然后组内排序,然后再还原到原数组中,若原数组变有序输出Yes
,否则输出No
。
时间复杂度:\(O(nlogn)\)
参考代码:
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 7 22:11:47 2022
@author: cherish
"""
n , k = map(int , input().strip().split())
a = list(map(int , input().strip().split()))
arr = [[] for i in range(k)]
idx = [[] for i in range(k)]
def solve():
for i in range(k):
for j in range(i , n , k):
arr[i].append(a[j])
idx[i].append(j)
for i in range(k):
arr[i].sort()
for i in range(k):
for j in range(len(arr[i])):
a[idx[i][j]] = arr[i][j]
for i in range(1 , n):
if a[i] < a[i - 1]:
print('No')
return
print('Yes')
return
solve()
D - Together Square
题目描述:给你一个正整数\(n(1 \leq n \leq 2 \times 10^5)\),求数对\((i , j)(1 \leq i , j \leq n)\)满足\(i \times j\)是平方数的数量。
思路:因为题目要求\(x = i \times j\)且\(x\)为平方数的数对\((i , j)\)的数量,显然此时\(x\)可以表示成\(p * p * q * q\),我们显然可以枚举\(i = p\),那么此时的\(j = \frac{p * q * q}{y}\),其中\(y\)为\(p\)中出现次数为奇数的质因子的乘积。
时间复杂度:\(O(n\sqrt{n})\)
参考代码:
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 7 22:31:44 2022
@author: cherish
"""
import bisect
n = int(input())
def solve(x: int)->int:
ans, i = 1 , 2
while i * i <= x:
if x % i == 0:
cnt = 0
while x % i == 0:
cnt += 1
x = x // i
if cnt % 2 == 1:
ans *= i
i += 1
if x > 1:
ans *= x
res , i = 0 , 1
while i * i * ans <= n:
res += 1
i += 1
return res
res = 0
for i in range(1 , n + 1):
res += solve(i)
print(res)
E - Small d and k
题目描述:给你\(n\)个顶点\(m\)条边的无向图,每个顶点的度不超过\(3\),给你\(Q\)次询问,每次询问给你一个顶点\(x\)和一个\(k\),表示求距离顶点\(x\)的长度不超过\(k\)的顶点数量。
思路:考虑到每个顶点的度不超过\(3\),且每次询问的距离不超过\(3\),故每次询问访问的点不超过\(40\)个,所以对于每个询问暴力\(BFS\)即可。
时间复杂度:\(O(Q + M)\)
参考代码:
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 8 10:17:18 2022
@author: cherish
"""
import collections
import sys
input = sys.stdin.readline
n , m = map(int , input().rstrip('\n').split())
graph = [[] for each in range(n + 1)]
for i in range(m):
u , v = map(int , input().rstrip('\n').split())
graph[u].append(v)
graph[v].append(u)
dis = [-1 for each in range(n + 1)]
Q = int(input())
for each in range(Q):
x , k = map(int , input().rstrip('\n').split())
q = collections.deque()
vec = [x]
dis[x] = 0
q.append(x)
while q:
u = q.popleft()
if dis[u] == k:
continue
for v in graph[u]:
if dis[v] != -1:
continue
dis[v] = dis[u] + 1
vec.append(v)
if dis[v] < k:
q.append(v)
res = 0
for val in vec:
res += val
dis[val] = -1
print(res)
作者:cherish.
出处:https://home.cnblogs.com/u/cherish-/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。