AtCoder Beginner Contest 220


A - Find Multiple

import sys
import heapq, math, itertools
from collections import defaultdict, deque
from bisect import bisect_left, bisect_right, insort_left, insort_right
inputs = sys.stdin.readline
mod = 10**9+7
inf = float('inf')
#sys.setrecursionlimit(10**7)

def main():
  a,b,c = map(int, inputs().split())
  ans = -1
  for x in range(a, b+1):
    if x%c==0:
      ans = x
      break
  print(ans)

if __name__ == '__main__':
  main()

B - Base K

import sys
import heapq, math, itertools
from collections import defaultdict, deque
from bisect import bisect_left, bisect_right, insort_left, insort_right
inputs = sys.stdin.readline
mod = 10**9+7
inf = float('inf')
#sys.setrecursionlimit(10**7)

def main():
  k = int(input())
  a,b = input().split()
  a,b = a[::-1], b[::-1]
  x,y = 0, 0
  for i in range(len(a)):
    x += int(a[i])*pow(k,i)
  for i in range(len(b)):
    y += int(b[i])*pow(k,i)
  print(x*y)

if __name__ == '__main__':
  main()

C - Long Sequence

import sys
import heapq, math, itertools
from collections import defaultdict, deque
from bisect import bisect_left, bisect_right, insort_left, insort_right
inputs = sys.stdin.readline
mod = 10**9+7
inf = float('inf')
#sys.setrecursionlimit(10**7)

def main():
  n = int(input())
  a = list(map(int, inputs().split()))
  x = int(input())
  s = sum(a)
  u,v = divmod(x, s)
  ans = 0
  for i in range(n):
    if ans+a[i]>v:
      break
    else:
      ans += a[i]
  print(u*n+i+1)

if __name__ == '__main__':
  main()

D - FG operation

import sys
import heapq, math, itertools
from collections import defaultdict, deque
from bisect import bisect_left, bisect_right, insort_left, insort_right
inputs = sys.stdin.readline
mod = 998244353
inf = float('inf')
#sys.setrecursionlimit(10**7)

def main():
  n = int(input())
  a = list(map(int, input().split()))
  dp = [[0 for _ in range(10)] for _ in range(n)]
  dp[0][a[0]] += 1
  for i in range(1, n):
    for j in range(10):
      dp[i][(j+a[i])%10] += dp[i-1][j]
      dp[i][(j*a[i])%10] += dp[i-1][j]
      dp[i][(j+a[i])%10] %= mod
      dp[i][(j*a[i])%10] %= mod
  for i in range(10):
    print(dp[-1][i])
if __name__ == '__main__':
  main()

F - Distance Sums 2

import sys
import heapq, math, itertools
from collections import defaultdict, deque
from bisect import bisect_left, bisect_right, insort_left, insort_right
inputs = sys.stdin.readline
mod = 10**9+7
inf = float('inf')
sys.setrecursionlimit(10**7)

def main():
  n = int(input())
  g = [[] for _ in range(n)]
  for _ in range(n-1):
    u,v = map(lambda x:int(x)-1, input().split())
    g[u].append(v)
    g[v].append(u)
  num_child = [1]*n
  dst = [0]*n

  def dfs1(pos, pre, d):
    dst[pos] = d
    for nx in g[pos]:
      if nx!=pre:
        num_child[pos] += dfs1(nx, pos, d+1)
    return num_child[pos]

  dfs1(0,-1,0)
  s = sum(dst)
  ans = [0]*n
  ans[0] = s

  def dfs2(pos, pre):
    for nx in g[pos]:
      if nx!=pre:
        ans[nx] = ans[pos] + n - 2*num_child[nx]
        dfs2(nx, pos)
  dfs2(0,-1)
  print(*ans, sep='\n')

if __name__ == '__main__':
  main()