A. Sequence of Strings
import sys
def solve():
input = sys.stdin.readline
mod = 10 ** 9 + 7
n = int(input().rstrip('\n'))
s = [str(input().rstrip('\n')) for _ in range(n)]
s.reverse()
print(*s, sep="\n")
if __name__ == '__main__':
solve()
B. Multi Test Cases
import sys
def solve():
input = sys.stdin.readline
mod = 10 ** 9 + 7
t = int(input().rstrip('\n'))
for i in range(t):
n = int(input().rstrip('\n'))
print(sum([1 if v % 2 != 0 else 0 for v in list(map(int, input().rstrip('\n').split()))]))
if __name__ == '__main__':
solve()
C. Count Connected Components
import sys
class AlgUnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
return -self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
def members(self, x):
root = self.find(x)
return [i for i in range(self.n) if self.find(i) == root]
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
def group_count(self):
return len(self.roots())
def all_group_members(self):
return {r: self.members(r) for r in self.roots()}
def __str__(self):
return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
def solve():
input = sys.stdin.readline
mod = 10 ** 9 + 7
n, m = list(map(int, input().rstrip('\n').split()))
uf = AlgUnionFind(n)
for i in range(m):
v1, v2 = list(map(int, input().rstrip('\n').split()))
uf.union(v1-1, v2-1)
print(len(uf.roots()))
if __name__ == '__main__':
solve()
D. Happy New Year 2023
import collections
import sys
def solve():
input = sys.stdin.readline
mod = 10 ** 9 + 7
t = int(input().rstrip('\n'))
ls = [True] * int(pow(pow(10, 18) * 9, 1/3) + 1)
d = collections.defaultdict(int)
for i in range(2, len(ls)):
if ls[i]:
d[i]
for j in range(i, len(ls), i):
ls[j] = False
for i in range(t):
n = int(input().rstrip('\n'))
for k in d.keys():
dv, md = divmod(n, k)
if md == 0:
p = pow(n / k, 0.5)
if p == int(p):
print(int(p), k)
break
dv, md = divmod(n, pow(k, 2))
if md == 0:
print(k, n // pow(k, 2))
break
if __name__ == '__main__':
solve()