Note that the answer is an integer greater than or equal to 0, so there is a trap in one digit. D i </ sub> ≤ 100, so if you calculate the answer straightforwardly with an integer, it will exceed the range of int64. Python, so nothing You can calculate without thinking.
N, *d = map(int, open(0).read().split())
for i in range(N):
if d[i] == 1:
print(10)
else:
print(9 * 10 ** (d[i] - 1))
Well, it's not difficult to calculate with a string.
N, *d = map(int, open(0).read().split())
for i in range(N):
if d[i] == 1:
print(10)
else:
print('9' + '0' * (d[i] - 1))
Consider where you can go from city i. If the city included in the set S of cities that can be reached from city i through the motorway i (including city i itself) is j, then the city that can go from city j to the pedestrian road The sum set of sets (including city j itself) is the set of cities that can be reached from city i. And, of course, all the cities that can be reached from the cities included in the same set S are the same. That is, for each set S, Since processing is performed for the number of cities included in the set S, it can be solved as * O * (* N *).
from sys import setrecursionlimit, stdin
def find(parent, i):
t = parent[i]
if t < 0:
return i
t = find(parent, t)
parent[i] = t
return t
def unite(parent, i, j):
i = find(parent, i)
j = find(parent, j)
if i == j:
return
parent[j] += parent[i]
parent[i] = j
readline = stdin.readline
setrecursionlimit(10 ** 6)
N, D, W = map(int, readline().split())
car = [-1] * N
for _ in range(D):
a, b = map(lambda x: int(x) - 1, readline().split())
unite(car, a, b)
walking = [-1] * N
for _ in range(W):
c, d = map(lambda x: int(x) - 1, readline().split())
unite(walking, c, d)
xs = [i for i in range(N) if car[i] < 0]
cs = {}
ss = {}
for x in xs:
cs[x] = 0
ss[x] = set()
for i in range(N):
a = find(car, i)
b = find(walking, i)
if b in ss[a]:
continue
ss[a].add(b)
cs[a] -= walking[b]
result = 0
for x in xs:
result -= car[x] * (cs[x] - 1)
print(result)
Recommended Posts