input
stringlengths 20
127k
| target
stringlengths 20
119k
| problem_id
stringlengths 6
6
|
---|---|---|
from itertools import product
n,k = list(map(int, input().split()))
d = list(map(int, input().split()))
nums = []
for i in range(10):
if i not in d: nums.append(i)
m = str(n)
for i in range(len(m), len(m)+2):
for j in product(nums, repeat=i):
ans = ""
for k in j: ans += str(k)
if int(ans) >= n:
print(ans)
exit()
|
from itertools import product
n,k = list(map(int, input().split()))
d = list(map(int, input().split()))
nums = []
for i in range(10):
if i not in d: nums.append(i)
m = len(str(n))
for i in range(m, m+2):
for j in product(nums, repeat=i):
ans = ""
for k in j: ans += str(k)
if int(ans) >= n:
print(ans)
exit()
|
p04045
|
N, K = list(map(int,input().split()))
D = list(map(int,input().split()))
OK = []
for i in range(10):
if i not in D:
OK.append(str(i))
#print(OK)
sn = str(N)
l = len(sn)
ans = N*1
Flag = 0
while Flag == 0:
sans = str(ans)
if all(sans[i] in OK for i in range(len(sans))):
Flag = 1
else:
ans += 1
print(ans)
|
N,K = list(map(int,input().split()))
A = set(map(int,input().split()))
#A = set(A)
ok = []
for i in range(10):
if i not in A:
ok.append(i)
#print(ok)
while True:
nstr = str(N)
nstr = list(nstr)
#print(nstr)
Flag = True
for x in nstr:
x = int(x)
if x in A:
Flag = False
break
#print(Flag)
if Flag:
print(N)
exit()
else:
N += 1
|
p04045
|
N,K=list(map(int,input().split()))
D=set([int(i) for i in input().split()])
while any(int(j) in D for j in str(N)):
N+=1
print(N)
|
N, K = list(map(int, input().split()))
D = [i for i in input().split()]
while True:
if all(n not in D for n in str(N)):
print(N)
exit()
else:
N += 1
|
p04045
|
n,k= list(map(int, input().split()))
d= list(input().split())
for i in range(n,10*n+1):
x=str(i)
ans=True
for j in range(len(x)):
if x[j] in d:
ans=False
if ans:
print(i)
break
|
n,k=list(map(int,input().split()))
d=list(input().split())
for i in range(n,10*n):
a=str(i)
b=len(a)
ans=True
for j in a:
if j in d:
ans=False
break
if ans:
print(i)
exit()
|
p04045
|
# 24
import sys
def input(): return sys.stdin.readline().strip()
def I(): return int(eval(input()))
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return eval(input())
def LS(): return input().split()
INF = float('inf')
n, k = LI()
d = set(LI())
# 最小金額から条件を満たすまで増やしていく
while True:
if all([int(j) not in d for j in str(n)]): # 全ての桁の数字が d に属さない
print(n)
exit()
n += 1
|
# 24
import sys
def input(): return sys.stdin.readline().strip()
def I(): return int(eval(input()))
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return eval(input())
def LS(): return input().split()
INF = float('inf')
n, k = LI()
d = set(LI())
# 最小金額から条件を満たすまで増やしていく
while True:
if all(int(j) not in d for j in str(n)): # 全ての桁の数字が d に属さない
print(n)
exit()
n += 1
|
p04045
|
import itertools
n, k = input().split()
d = list(map(int, input().split()))
number = list(range(10))
number = [str(x) for x in number if x not in d]
ans = []
for v in itertools.product(number, repeat=len(n)):
if v[0] == "0": continue
if int("".join(v)) >= int(n): ans.append(int("".join(v)))
if not ans:
for v in itertools.product(number, repeat=len(n)+1):
if v[0] == "0": continue
if int("".join(v)) >= int(n): ans.append(int("".join(v)))
print((min(ans)))
|
import itertools
n,k = list(map(int, input().split()))
d = list(input().split())
d = [str(i) for i in range(10) if str(i) not in d]
ans = float("INF")
for v in itertools.product(d, repeat=len(str(n))):
if v[0] != "0" and int("".join(v)) >= n:
if ans > int("".join(v)): ans = int("".join(v))
if ans == float("INF"):
for v in itertools.product(d, repeat=len(str(n))+1):
if v[0] != "0" and int("".join(v)) >= n:
if ans > int("".join(v)): ans = int("".join(v))
print(ans)
|
p04045
|
n, k = list(map(int, input().split()))
dlist = [s for s in input().split()]
ans = n
while True:
blnOk = True
for d in str(ans):
if d in dlist:
blnOk = False
if blnOk:
print(ans)
exit()
else:
ans += 1
|
n, k = list(map(int, input().split()))
dlist = [s for s in input().split()]
ans = n
while True:
blnOk = True
for d in str(ans):
if d in dlist:
blnOk = False
break
if blnOk:
print(ans)
exit()
else:
ans += 1
|
p04045
|
a,b = list(map(int,input().split()))
li1 = set(map(int,input().split()))
li2 = {1,2,3,4,5,6,7,8,9,0}
nu = list(li1^li2)
i = 1
while True:
aa = list(str(a))
aa = [int(x) for x in aa]
if set(aa).issubset(set(nu)):
print(a)
break
a += i
|
n,k = list(map(int,input().split()))
L = list(map(int,input().split()))
num = [1,2,3,4,5,6,7,8,9,0]
li = list(set(num).difference(set(L)))
flag = False
while True:
n = str(n)
for i in range(len(n)):
if int(n[i]) in li:
flag = True
else:
flag= False
break
if flag:
print(n)
exit()
n = int(n)
n +=1
|
p04045
|
N, K = list(map(int, input().split()))
D = list(map(int, input().split()))
#使うことのできるリストの作成
d = []
for i in range(10):
if i in D:
pass
else:
d += [i]
def check(i): #数値iがdのリストだけで書くことができるかを判定
I = str(i) #文字列に変換
# print (I)
n = len(I) #桁数の取得
for j in range(n): #全桁で確認
# print (I[j])
if int(I[j]) in d: #大丈夫ならスルー
pass
else: #一つでも不適ならFalseを返す
return False
return True
# print (check(2000))
for i in range(N, 10**5 + 1): #小さい数から全探索
if i >= N: #大きさを確認
if check(i): #dの中だけで書けるかをチェック
print (i) #書くことができるなら出力して終了
exit()
|
N, K = list(map(int, input().split()))
D = list(map(int, input().split()))
#使うことのできるリストの作成
d = []
for i in range(10):
if i in D:
pass
else:
d += [i]
def check(i): #数値iがdのリストだけで書くことができるかを判定
I = str(i) #文字列に変換
# print (I)
n = len(I) #桁数の取得
for j in range(n): #全桁で確認
# print (I[j])
if int(I[j]) in d: #大丈夫ならスルー
pass
else: #一つでも不適ならFalseを返す
return False
return True
# print (check(2000))
for i in range(N, 10**5 + 1): #小さい数から全探索
if check(i): #dの中だけで書けるかをチェック
print (i) #書くことができるなら出力して終了
exit()
|
p04045
|
N,K = list(map(int,input().split()))
D = set(input().split())
while True:
if not set(str(N)) & D:
break
N += 1
print(N)
|
N,K = list(map(int,input().split()))
D = set(input().split())
for i in range(N,100000):
# 禁止文字との共通集合がなければ良い
if not D & set(str(i)):
break
print(i)
|
p04045
|
N,K=list(map(int,input().split()))
D=list(map(int,input().split()))
C=[]
for i in range(0,10):
if i not in D:
C.append(i)
#print(C)
for i in range(N,10**10):
cnt=0
for j in list(map(int,list(str(i)))):
if j in C:
cnt+=1
if len(str(i))==cnt:
print(i)
exit()
|
N, K = list(map(int, input().split()))
D = list(map(int, input().split()))
for i in range(N, 10 * N + 1):
A = list(map(int, str(i)))
flag = 1
for a in A:
if a in D:
flag = 0
break
if flag:
print(i)
exit()
|
p04045
|
numbers = {str(i) for i in range(10)}
N, K = list(map(int, input().split()))
*D, = list(map(str, input().split()))
D = set(D)
available = numbers - D
ans = 0
for i in range(N, 100000):
nums = set(str(i))
if nums <= available:
print(i)
break
|
n, k = list(map(int, input().split()))
d = set(map(str, input().split()))
numbers = {str(i) for i in range(10)}
available = numbers - d
for i in range(n, 100000):
nums = set(str(i))
if nums <= available:
print(i)
break
|
p04045
|
N, K = list(map(int, input().split()))
D = list(map(int, input().split()))
n = N
while True:
flg = False
for Di in D:
if str(Di) in str(n):
flg = True
if not flg:
break
n += 1
print(n)
|
N, K = list(map(int, input().split()))
D = list(map(int, input().split()))
n = N
while True:
i = n
while i != 0:
if i % 10 in D:
break
i = i // 10
if i == 0:
break
n += 1
print(n)
|
p04045
|
N, K = list(map(int, input().split()))
D = list(map(int, input().split()))
n = N
while True:
i = n
while i != 0:
if i % 10 in D:
break
i = i // 10
if i == 0:
break
n += 1
print(n)
|
N, K = list(map(int, input().split()))
D = list(map(int, input().split()))
n = N
while True:
i = n
while i != 0:
if i % 10 in D:
break
i = int(i / 10)
if i == 0:
break
n += 1
print(n)
|
p04045
|
N, K = list(map(int, input().split()))
D = input().split()
n = N
while True:
for c in str(n):
if c in D:
break
else:
break
n += 1
print(n)
|
N, K = list(map(int, input().split()))
D = set(input().split())
n = N
while True:
for c in str(n):
if c in D:
break
else:
break
n += 1
print(n)
|
p04045
|
N, K = list(map(int, input().split()))
D = set(input().split())
n = N
while True:
for c in str(n):
if c in D:
break
else:
break
n += 1
print(n)
|
import sys
n, k = list(map(int, input().split()))
D = [int(x) for x in input().split()]
def dfs(s: str, m: int) -> None:
if s != "" and int(s) >= n:
print(s)
sys.exit()
if len(s) == m:
return
for i in range(10):
if i not in D:
dfs(str(i) + s, m)
for i in range(sys.maxsize):
dfs("", i)
|
p04045
|
N,K = list(map(int,input().split()))
D = set(input().split())
while 1:
E = set(str(N))
if not(D&E):
print(N)
break
N+=1
|
N,K = list(map(int,input().split()))
D = list(input().split())
for i in range(N,100001):
n = str(i)
k = 1
for j in n:
if j in D:
k=0
break
if k:
print(n)
break
|
p04045
|
n, k = list(map(int, input().split()))
d = list(map(int, input().split()))
use = [i for i in range(10)]
for _d in d:
use.remove(_d)
while True:
ok = True
s = str(n)
for i in range(len(s)):
if int(s[i]) not in use:
ok = False
break
if ok:
print(n)
exit()
else:
n += 1
|
n, k = list(map(int, input().split()))
D = list(map(str, input().split()))
money = n
ok = True
while True:
for d in D:
if d in str(money):
ok = False
break
if ok:
print(money)
break
money += 1
ok = True
|
p04045
|
n,k = list(map(int,input().split()))
d = list(map(int,input().split()))
while True:
a = []
m = n
while m > 0:
a.append(m % 10)
m = m // 10
j = 0
for i in range(len(a)):
if a[i] in d:
break
j += 1
if j == len(a):
print(n)
exit(0)
else:
n += 1
|
n,k = list(map(int,input().split()))
d = list(input().split())
flg = True
while flg:
flg = False
m = list(str(n))
for i in m:
if i in d:
flg = True
break
if flg:
n += 1
print(n)
|
p04045
|
import sys
input = sys.stdin.readline
#n = int(input())
#l = list(map(int, input().split()))
'''
a=[]
b=[]
for i in range():
A, B = map(int, input().split())
a.append(A)
b.append(B)'''
a=list(map(str, [0,1,2,3,4,5,6,7,8,9]))
n,k=list(map(int, input().split()))
d=list(map(str, input().split()))
for item in d:
a.remove(item)
#print(a)
from collections import deque
q=deque()
q.extend(a)
for i in range(len(a)):
if int(a[i])>=n:
print((a[i]))
sys.exit()
while True:
qq=q.popleft()
#print(q)
for item in a:
qqq=qq+item
#print(qqq[::-1])
if int(qqq[::-1])>=n:
print((qqq[::-1]))
sys.exit()
q.append(qqq[::-1])
|
import sys
input = sys.stdin.readline
#n = int(input())
#l = list(map(int, input().split()))
'''
a=[]
b=[]
for i in range():
A, B = map(int, input().split())
a.append(A)
b.append(B)'''
a=list(map(str, [0,1,2,3,4,5,6,7,8,9]))
n,k=list(map(int, input().split()))
d=list(map(str, input().split()))
for item in d:
a.remove(item)
#print(a)
from collections import deque
q=deque()
q.extend(a)
for i in range(len(a)):
if int(a[i])>=n:
print((a[i]))
sys.exit()
while True:
qq=q.popleft()
#print(q)
for item in a:
qqq=qq+item
#print(qqq[::-1])
if int(qqq)>=n:
print(qqq)
sys.exit()
q.append(qqq)
|
p04045
|
#!/usr/bin/env python3
import sys
def solve(N: int, K: int, D: "List[int]"):
while(True):
nl = list(map(int,list(str(N))))
f = True
for i in range(len(nl)):
if(nl[i] in D):
f = False
if(f):
print(N)
exit()
else:
N += 1
return
# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template)
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
N = int(next(tokens)) # type: int
K = int(next(tokens)) # type: int
D = [int(next(tokens)) for _ in range(K)] # type: "List[int]"
solve(N, K, D)
if __name__ == '__main__':
main()
|
#!/usr/bin/env python3
import sys
def solve(N: int, K: int, D: "List[int]"):
d = set(map(str, D))
while set(str(N))&d:
N += 1
print(N)
return
# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template)
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
N = int(next(tokens)) # type: int
K = int(next(tokens)) # type: int
D = [int(next(tokens)) for _ in range(K)] # type: "List[int]"
solve(N, K, D)
if __name__ == '__main__':
main()
|
p04045
|
from sys import stdin, setrecursionlimit
setrecursionlimit(10 ** 7)
N, K = [int(x) for x in stdin.readline().rstrip().split()]
D = [int(x) for x in stdin.readline().rstrip().split()]
L = [x for x in range(10) if x not in D]
ans = 10 ** 7
def dfs(n):
global ans
if n != '' and n[0] == '0':
return
if n != '' and int(n) >= ans:
return
if len(n) >= len(str(N)):
if int(n) >= N:
ans = int(n)
for l in L:
dfs(n + str(l))
dfs('')
print(ans)
|
from sys import stdin, setrecursionlimit
setrecursionlimit(10 ** 7)
N, K = [int(x) for x in stdin.readline().rstrip().split()]
D = [int(x) for x in stdin.readline().rstrip().split()]
L = [x for x in range(10) if x not in D]
def dfs(n):
if n != '':
if n[0] == '0':
return 10 ** 7
elif int(n) >= N:
return int(n)
output = 10 ** 7
for l in L:
output = min(output, dfs(n + str(l)))
return output
print((dfs('')))
|
p04045
|
N,K=list(map(int,input().split()))
D=list(map(int,input().split()))
def solve():
for i in range(10*N+2):
if i>=N:
s=str(i)
for j in range(len(s)):
if int(s[j]) in D: break
else:
print(i)
return 0
solve()
|
n, k = list(map(int, input().split()))
d = list(map(int, input().split()))
def solve():
for i in range(10*n+2):
if i >= n:
s = str(i)
for j in range(len(s)):
if int(s[j]) in d:
break
else:
print(i)
return 0
solve()
|
p04045
|
n,k = list(map(int,input().split()))
D = list(map(int,input().split()))
ans = n
while True:
for i in list(map(int, str(ans))):
if i in D:
break
else:
print (ans)
exit ()
ans += 1
|
import sys
sys.setrecursionlimit(500000)
n,k = list(map(int,input().split()))
D = list(map(int,input().split()))
ans = []
def dfs(p:str):
if int(p)>=n:
ans.append(int(p))
return
if len(p)>len(str(n))+1:
return
for i in range(10):
if not i in D:
dfs(p+str(i))
for i in range(10):
if not i in D:
dfs(str(i))
print((min(ans)))
|
p04045
|
# coding: utf-8;
def main():
num = [0,1,2,3,4,5,6,7,8,9]
n, k = list(map(int, input().split()))
d = list(map(int, input().split()))
usable_num = list(set(num) - set(d))
while True:
if len(set(map(int, str(n))) - set(usable_num)) == 0:
break
else:
n += 1
print(n)
if __name__ == '__main__':
main()
|
# coding: utf-8;
def main():
num = [0,1,2,3,4,5,6,7,8,9]
n, k = list(map(int, input().split()))
d = list(map(int, input().split()))
usable_num = list(set(num) - set(d))
while True:
f = True
for a in str(n):
if int(a) not in usable_num:
f = False
break
if f: break
n += 1
print(n)
if __name__ == '__main__':
main()
|
p04045
|
N, K = list(map(int, input().split())) # 標準入力
D = list(map(int, input().split())) # 嫌いな数字のリスト
D_str = list(map(str, D))
m = N
while True:
for n in str(m):
if n in D_str:
break
else: # breakされなかったら実行される
print(m)
break
m += 1
|
N, K = list(map(int, input().split()))
D=input().split()
ans=N
while True:
for i in str(ans):
if i in D:
break
else:
print(ans)
exit()
ans +=1
|
p04045
|
def main():
n,k=list(map(int,input().split(' ')))
d = list(input().split(' '))
for i in range(n,10*n+1):
if not set(list(str(i)))&set(d):
print(i)
break
main()
|
def main():
n,k=list(map(int,input().split(' ')))
d = input().split(' ')
for i in range(n,10*n+1):
if not set(list(str(i)))&set(d):
print(i)
break
if __name__=='__main__':
main()
|
p04045
|
def main():
from sys import stdin
n,k = list(map(int,stdin.readline().rstrip().split()))
Ds = set(map(int,stdin.readline().rstrip().split()))
ans = n
while True:
num = ans
numset = set()
while num != 0:
r = num%10
num = num//10
numset.add(r)
if numset & Ds == set({}):
break;
else:
ans+=1
print(ans)
main()
|
import io,sys
sys.setrecursionlimit(10**6)
def main():
n,k = list(map(int,input().split()))
D = list(map(int,input().split()))
while True:
temp = n
num = []
while temp > 0:
num.append(temp%10)
temp//=10
flag = True
for i in num:
if i in D:
flag = False
break
if flag:
print(n)
sys.exit()
else:
n+=1
main()
|
p04045
|
#
# abc042 c
#
import sys
from io import StringIO
import unittest
class TestClass(unittest.TestCase):
def assertIO(self, input, output):
stdout, stdin = sys.stdout, sys.stdin
sys.stdout, sys.stdin = StringIO(), StringIO(input)
resolve()
sys.stdout.seek(0)
out = sys.stdout.read()[:-1]
sys.stdout, sys.stdin = stdout, stdin
self.assertEqual(out, output)
def test_入力例_1(self):
input = """1000 8
1 3 4 5 6 7 8 9"""
output = """2000"""
self.assertIO(input, output)
def test_入力例_2(self):
input = """9999 1
0"""
output = """9999"""
self.assertIO(input, output)
def resolve():
N, K = list(map(int, input().split()))
D = list(input().split())
for i in range(100000):
if i < N:
continue
for d in D:
if str(i).count(d) != 0:
break
else:
print(i)
break
if __name__ == "__main__":
# unittest.main()
resolve()
|
#
# abc042 c
#
import sys
from io import StringIO
import unittest
class TestClass(unittest.TestCase):
def assertIO(self, input, output):
stdout, stdin = sys.stdout, sys.stdin
sys.stdout, sys.stdin = StringIO(), StringIO(input)
resolve()
sys.stdout.seek(0)
out = sys.stdout.read()[:-1]
sys.stdout, sys.stdin = stdout, stdin
self.assertEqual(out, output)
def test_入力例_1(self):
input = """1000 8
1 3 4 5 6 7 8 9"""
output = """2000"""
self.assertIO(input, output)
def test_入力例_2(self):
input = """9999 1
0"""
output = """9999"""
self.assertIO(input, output)
def resolve():
N, K = list(map(int, input().split()))
D = list(input().split())
for i in range(N, 10*N+1):
for d in D:
if str(i).count(d) != 0:
break
else:
print(i)
break
if __name__ == "__main__":
# unittest.main()
resolve()
|
p04045
|
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint
sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007
n, k = LI()
D = set(range(10)) - set(LI())
for i in range(n, 100000):
if set([int(j) for j in str(i)]) <= D:
print(i)
break
|
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint
sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007
n, k = LI()
D = set(LI())
for i in range(n, 100000):
if not set([int(j) for j in str(i)]) & D:
print(i)
break
|
p04045
|
N, K = list(map(int, input().split()))
D = set(input().split())
frag = True
ans = 0
N = N-1
for i in range(0, 1000000):
N += 1
if len(set(str(N)) & D) == 0:
ans = N
frag = False
break
print(N)
|
import sys
N, K = list(map(int, input().split()))
D = [str(i) for i in input().split()]
ans = 0
now = N
for j in range(10**6):
for i in list(str(now)):
if i in D:
now += 1
break
else:
print(now)
sys.exit()
|
p04045
|
N, K = list(map(int, input().split()))
D = [int(i) for i in input().split()]
num = N
flag = True
while flag:
for i in D:
if str(i) in str(num):
num += 1
break
else:
print(num)
flag = False
|
N, K = list(map(int, input().split()))
D = [str(i) for i in input().split()]
ans = N
for i in range(N, 100001):
if set(list(str(i))) & set(D):
gg = 1
else:
print(i)
break
|
p04045
|
numbers = {str(i) for i in range(10)}
N, K = list(map(int, input().split()))
*D, = list(map(str, input().split()))
D = set(D)
available = numbers - D
ans = 0
for i in range(N, 100000):
nums = set(str(i))
if nums <= available:
print(i)
break
|
def problem_c():
N, K = list(map(int, input().split()))
D = list(map(str, input().split()))
numbers = {str(i) for i in range(10)}
availables = set(numbers) - set(D)
for i in range(N, 100000):
nums = set(str(i))
if nums <= availables:
print(i)
return
##########
problem_c()
|
p04045
|
N, K = list(map(int, input().split()))
D = input().split()
for n in range(N, 10 * N):
if all([c not in D for c in str(n)]):
print(n)
break
|
N, K = input().split()
D = input().split()
E = [str(x) for x in range(10) if str(x) not in D]
n = len(N)
next_digit = [(str(d), 0) if str(d) not in D else None for d in range(10)]
for i in range(9, -1, -1):
if next_digit[i] is None:
j = (i + 1) % 10
while next_digit[j] is None:
j = (j + 1) % 10
if i < j:
next_digit[i] = next_digit[j]
else:
next_digit[i] = (next_digit[j][0], 1)
for i in range(n):
if N[i] in D:
carry = 0
j = i
reverse_mid = ""
while j >= 0:
d = N[j]
if carry == 1:
if d == '9':
d = '0'
else:
d = chr(ord(d) + 1)
carry = 0
nd, carry0 = next_digit[int(d)]
reverse_mid += nd
carry += carry0
if carry == 0:
break
j -= 1
if carry == 1:
head = next_digit[1][0]
else:
head = N[:j]
N = head + reverse_mid[::-1] + (E[0] * (n - 1 - i))
break
print(N)
|
p04045
|
def inpl(): return input().split()
n, k = list(map(int, inpl()))
d = inpl()
x = n
while True:
flg = True
for c in str(x):
if c in d:
flg = False
break
if flg:
break
x += 1
print(x)
|
def main():
import sys
sys.setrecursionlimit(10 ** 7)
N, K = list(map(int, input().split()))
D = set(map(int, input().split()))
digits = {x for x in range(10)}
digits -= D
# print(digits)
digits = sorted(digits)
def dfs(pay):
if pay >= N:
return pay
np = pay * 10
ret = 1 << 60
if np == 0 and 0 in digits:
for d in digits:
if d == 0: continue
ret = min(ret, dfs(np + d))
else:
for d in digits:
ret = min(ret, dfs(np + d))
return ret
print((dfs(0)))
if __name__ == '__main__':
main()
|
p04045
|
n,k=list(map(int,input().split()))
d=list(map(int,input().split()))
nmax=100000
for nn in range(n,nmax):
ns=list(str(nn))
yn="yes"
for nsi in ns:
# print(nn,nsi,(nsi in d))
if int(nsi) in d:
yn="no"
break
if yn=="no":
continue
else:
break
if yn=="yes":
print(nn)
|
n,k=list(map(int,input().split()))
d=set(input().split())
for nn in range(n,10*n+1):
if len(set(str(nn)) & d)==0:
print(nn)
break
|
p04045
|
n,k = list(map(int,input().split()))
ng = set(input().split())
while len(set(str(n))&ng):
n+=1
print(n)
|
n,k = list(map(int,input().split()))
ng = set(list(input().split()))
while True:
if len(set(str(n))&ng)==0:
break
else:
n+=1
print(n)
|
p04045
|
N, K = list(map(int, input().split()))
D = set(input().split())
while True:
l = list(str(N))
for x in l:
if x in D:
N += 1
break
else:
print(N)
exit()
|
N, K = list(map(int, input().split()))
D = set(map(int, input().split()))
safe = set(range(10)) - D
def test(N):
return set((int(x) for x in str(N))) <= safe
def F(N):
while N % 10:
if test(N):
return N
N += 1
N //= 10
x = F(N)
y = min(safe)
return 10 * x + y
answer = F(N)
print(answer)
|
p04045
|
N, K = list(map(int,input().split()))
D = list(map(str, input().split()))
while True:
S = str(N)
t = 0
for x in S:
if x not in D:
t += 1
if t == len(S):
print(S)
break
else:
N += 1
|
import sys, math
def input():
return sys.stdin.readline()[:-1]
def main():
N, K = list(map(int,input().split()))
D = set(list(input().split()))
for k in range(N,100001):
s = str(k)
f = 0
for e in s:
if e in D:
f = 1
break
if f == 0:
print(s)
exit(0)
if __name__ == '__main__':
main()
|
p04045
|
import sys, math
def input():
return sys.stdin.readline()[:-1]
def main():
N, K = list(map(int,input().split()))
D = set(list(input().split()))
for k in range(N,100001):
s = str(k)
f = 0
for e in s:
if e in D:
f = 1
break
if f == 0:
print(s)
exit(0)
if __name__ == '__main__':
main()
|
import sys, math
def input():
return sys.stdin.readline()[:-1]
def main():
N, K = list(map(int,input().split()))
D = set(input().split())
for ans in range(N,1000000):
f = 0
for e in str(ans):
if e in D:
f = 1
break
if f == 0:
print(ans)
exit(0)
if __name__ == '__main__':
main()
|
p04045
|
#n=int(input())
ans=10**6
n,k=list(map(int,input().split()))
p=[int(i) for i in input().split()]
s=[]
for i in range(10):
if i not in p:
s.append(str(i))
for a in s+['0']:
for b in s:
for c in s:
for d in s:
for e in s:
x=list(map(int,[a+b+c+d+e,b+c+d+e,c+d+e,d+e,e]))
for i in x:
if i>=n:
ans=min(i,ans)
print(ans)
|
n,k=list(map(int,input().split()))
p=[int(i) for i in input().split()]
s=[]
for i in range(10):
if i not in p:
s.append(str(i))
for x in range(n,10*n):
flg=True
for d in str(x):
if d not in s:
flg=False
if flg:
print(x)
break
|
p04045
|
N, K = list(map(int, input().split()))
D = list(map(int, input().split()))
while True:
flag = 1
N_str = list(str(N))
for s in N_str:
if int(s) in D:
flag = 0
break
if flag:
print(N)
break
N += 1
|
N,K = list(map(int, input().split()))
D = set(list(map(int, input().split())))
ans = 0
for i in range(N, 10*N+1):
res = i
while res > 0:
digit = res % 10
if digit in D:
break
res //= 10
if res <= 0:
ans = i
break
print(ans)
|
p04045
|
n,k=list(map(int,input().split()))
d=input().split()
while 1:
t=0
p=list(str(n))
q=len(p)
for c in range(q):
if t==1:
break
for b in range(k):
if p[c]==d[b]:
n=n+10**c
t=1
break
if t==0:
break
print(n)
|
n,k=list(map(int,input().split()))
d=input().split()
while 1:
t=0
p=list(str(n))
q=len(p)
for c in range(q):
if t==1:
break
for b in range(k):
if p[c]==d[b]:
n=n+1
t=1
break
if t==0:
break
print(n)
|
p04045
|
n,k=list(map(int,input().split()))
d=input().split()
while 1:
t=0
p=list(str(n))
q=len(p)
for c in range(q):
if t==1:
break
for b in range(k):
if p[c]==d[b]:
n=n+1
t=1
break
if t==0:
break
print(n)
|
n,k=list(map(int,input().split()))
d=input().split()
while 1:
t=0
p=list(str(n))
q=len(p)
for c in range(q):
if t==1:
break
for b in range(k):
if p[c]==d[b]:
r=10**(q-c-1)
n=int(n/r+1)*r
t=1
break
if t==0:
break
print(n)
|
p04045
|
N,K = [int(i) for i in input().split()]
#D = [int(i) for i in input().split()]は数値代入
#文字列を複数代入
D = input().split()
#集合に置き換える(型はset)
D = set(D)
use = set(['0','1','2','3','4','5','6','7','8','9'])
use -= D
for i in range(N,100*N):
if use >= set(str(i)):
print(i)
break
|
N,K = [int(i) for i in input().split()]
#D = [int(i) for i in input().split()]は数値代入
#文字列を複数代入
D = input().split()
#集合に置き換える(型はset)
D = set(D)
use = set(['0','1','2','3','4','5','6','7','8','9'])
use -= D
for i in range(N,88889):
if use >= set(str(i)):
print(i)
break
|
p04045
|
import itertools
N,K = list(map(int,input().split()))
D = list(map(int,input().split()))
A = []
for i in range(10):
if i not in D:
A.append(i)
cmin = 100000
k = len(str(N))
for i in range(k,6):
for x in itertools.product(A,repeat=i):
cnt = 0
for j in range(i):
cnt += x[j]*10**(i-1-j)
if cnt>=N:
cmin = min(cmin,cnt)
print(cmin)
|
N,K = list(map(int,input().split()))
D = list(map(int,input().split()))
E = []
for i in range(10):
if i not in D:
E.append(i)
E = sorted(E)
N = str(N)
ind = len(N)
for i in range(len(N)):
if int(N[i]) not in E:
ind = i
break
if ind==len(N):
print(N)
else:
flag = 0
x = ""
for i in range(ind,-1,-1):
n = int(N[i])
for e in E:
if e>n:
x = N[:i]+str(e)+str(E[0])*(len(N)-i-1)
flag = 1
break
if flag==1:break
if flag==0:
if E[0]>0:
a = E[0]
else:
a = E[1]
x = str(a)+str(E[0])*len(N)
print(x)
|
p04045
|
def main():
num = list(map(int,input().split()))
bad = list(map(int,input().split()))
while True :
flg=0
for i in range(0,len(str(num[0]))):
stnum=str(num[0])
if int(stnum[i]) in bad:
flg=1
break
if flg==1:
num[0]+=1
continue
else:
break
print((num[0]))
main()
|
def main():
num = list(map(int,input().split()))
bad = list(map(str,input().split()))
while True :
flg=0
for i in range(0,len(str(num[0]))):
stnum=str(num[0])
if stnum[i] in bad:
flg=1
break
if flg==1:
num[0]+=1
continue
else:
break
print((num[0]))
main()
|
p04045
|
n, k=list(map(int, input().split()))
d = list(input().split())
i=n
while True:
count = 0
for j in range(k):
if d[j] in str(i):
count += 1
if count == 0:
print(i)
break
else:
i += 1
|
n,k = list(map(int, input().split()))
d = list(input().split())
while True:
c=0
for i in range(k):
if d[i] in str(n):
c+=1
if c==0:
print(n)
break
else:
n+=1
|
p04045
|
def abc042_c():
price, len_hate_set = list(map(int, input().split()))
hate_set = set([int(i) for i in input().split()])
while True:
if all([int(x) in set(range(0, 10)) - hate_set for x in [int(str(price)[i]) for i in range(len(str(price)))]]) and price >= price:
return price
price += 1
if __name__ == '__main__':
print((abc042_c()))
|
def abc042_c():
price, len_hate_set = list(map(int, input().split()))
hate_set = set([int(i) for i in input().split()])
usable_set = set(range(0, 10)) - hate_set
while True:
if all([int(x) in usable_set for x in [int(str(price)[i]) for i in range(len(str(price)))]]) and price >= price:
return price
price += 1
if __name__ == '__main__':
print((abc042_c()))
|
p04045
|
def no_dislike_number(num, D):
for dislike in D:
if dislike in num:
return False
return True
MAX = 10000
N, K = list(map(int, input().split()))
D = input().split()
num = N
while(True):
if no_dislike_number(str(num), D):
print(num)
break
num += 1
|
def no_dislike_number(num, D):
for digit in num:
if digit in D:
return False
return True
# for dislike in D:
# if dislike in num:
# return False
# return True
MAX = 10000
N, K = list(map(int, input().split()))
D = input().split()
num = N
while(True):
if no_dislike_number(str(num), D):
print(num)
break
num += 1
|
p04045
|
# encoding: utf-8
N, K = list(map(int, input().split()))
D = list(map(int, input().split()))
Db = []
for Di in range(10):
if Di not in D: Db.append(Di)
# print("#", Db)
for level in range(6):
if N // (10 ** level) < 1: break
# print("#", level)
memo = [None] * ((level + 1) * 10000)
def pay(pos, tmp):
# print("##", pos, tmp)
if pos > level or tmp >= N:
if tmp >= N:
# print("###", tmp)
return tmp
else: return 10000
elif memo[pos * 10000 + tmp] != None: return memo[pos * 10000 + tmp]
rets = []
for Dbi in Db:
add = Dbi * (10 ** pos)
rets.append(pay(pos + 1, tmp + add))
ret = min(rets)
if memo[pos * 10000 + tmp] == None: memo[pos * 10000 + tmp] = ret
return ret
print((pay(0, 0)))
|
# encoding: utf-8
N, K = list(map(int, input().split()))
D = list(map(int, input().split()))
Db = []
for Di in range(10):
if Di not in D: Db.append(Di)
# print("#", Db)
for level in range(6):
if N // (10 ** level) < 1: break
# print("#", level)
# memo = [None] * ((level + 1) * 10000)
def pay(pos, tmp):
# print("##", pos, tmp)
if pos > level or tmp >= N:
if tmp >= N:
# print("###", tmp)
return tmp
else: return 10000
# elif memo[pos * 10000 + tmp] != None: return memo[pos * 10000 + tmp]
rets = []
for Dbi in Db:
add = Dbi * (10 ** pos)
rets.append(pay(pos + 1, tmp + add))
ret = min(rets)
# if memo[pos * 10000 + tmp] == None: memo[pos * 10000 + tmp] = ret
return ret
print((pay(0, 0)))
|
p04045
|
#!/use/bin/env python3
(n, k) = [int(s) for s in input().split()]
a = [int(s) for s in input().split()]
like = []
ans = n
while (1):
c = True
b = n
while (b != 0):
b = b // 10
d = b % 10
if d in a:
c = False
break
if (c):
ans = n
break
n += 1
print(ans)
|
#!/use/bin/env python3
(n, k) = [int(s) for s in input().split()]
a = [int(s) for s in input().split()]
like = []
ans = n
b = n
while (1):
c = True
b = n
while (b != 0):
d = b % 10
b = b // 10
if d in a:
c = False
break
if (c):
ans = n
break
n += 1
print(ans)
|
p04045
|
n,k = list(map(int,input().split()))
dls = list(map(int,input().split()))
while 1:
s = list(map(int,list(str(n))))
for i in dls:
if i in s:
break
else:
print(n)
exit()
n += 1
|
n,k = list(map(int,input().split()))
dls = list(map(str,input().split()))
while 1:
s = str(n)
for i in dls:
if i in s:
break
else:
print(n)
exit()
n += 1
|
p04045
|
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return list(map(int, sys.stdin.readline().split()))
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]
import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from itertools import accumulate #list(accumulate(A))
#from collections import deque
N, K = mi()
D = li()
r = [i for i in range(10) if i not in D]
if r[0]==0:
#que = deque(r[1:])
que = r[1:]
else:
#que = deque(r)
que = r
i = 0
while True:
for num in r:
new = que[i]*10 + num
que.append(new)
if que[-1] >= 10**6:
break
i += 1
ind = bisect.bisect_left(que, N)
#print(que)
#print(ind)
print((que[ind]))
|
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return list(map(int, sys.stdin.readline().split()))
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]
import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from itertools import accumulate #list(accumulate(A))
#from collections import deque
N, K = mi()
D = li()
r = [i for i in range(10) if i not in D]
if r[0]==0:
#que = deque(r[1:])
que = r[:][1:]
else:
#que = deque(r)
que = r[:]
for num in que:
if N <= num:
print(num)
exit()
i = 0
while True:
base = que[i]*10
for num in r:
new = base + num
if new >= N:
print(new)
exit()
que.append(new)
#if que[-1] >= 10**6:
#break
i += 1
#ind = bisect.bisect_left(que, N)
#print(que)
#print(ind)
#print(que[ind])
|
p04045
|
def slove():
import sys
import collections
input = sys.stdin.readline
n, k = list(map(int, input().rstrip('\n').split()))
d = collections.defaultdict(int)
for i in list(map(int, input().rstrip('\n').split())):
d[i]
for i in range(n, 10 ** 10):
s = str(i)
b = True
for j in range(len(s)):
if int(s[j]) in d:
b = False
break
if b:
print(i)
exit()
if __name__ == '__main__':
slove()
|
import sys
import collections
def solve():
input = sys.stdin.readline
mod = 10 ** 9 + 7
n, k = list(map(int, input().rstrip('\n').split()))
d = collections.defaultdict(int)
for v in list(map(int, input().rstrip('\n').split())):
d[str(v)]
for i in range(n, 10 ** 10):
s = str(i)
b = True
for v in s:
if v in d:
b = False
break
if b:
print(i)
exit()
if __name__ == '__main__':
solve()
|
p04045
|
N,K = list(map(int,input().split()))
D = set(input().split())
while len(set(str(N))&D)>0:
N+=1
print(N)
|
N,K = list(map(int,input().split()))
D = set(input().split())
for n in range(N,100000):
if len(set(str(n))&D)==0:
print(n)
break
|
p04045
|
N,K = list(map(int,input().split()))
D = set(input().split())
for n in range(N,100000):
if len(set(str(n))&D)==0:
print(n)
break
|
N,K = list(map(int,input().split()))
D = set(input().split())
while len(set(str(N))&D):
N+=1
print(N)
|
p04045
|
N,K = list(map(int,input().split()))
D = set(input().split())
while True:
for c in str(N):
if c in D:
break
else:
print(N)
exit()
N += 1
|
N,K = list(map(int,input().split()))
D = set(input().split())
while True:
for c in str(N):
if c in D:
break
else:
print(N)
exit()
N += 1
print(N)
|
p04045
|
N, K = list(map(int, input().split(" ")))
list_num = ["0","1","2","3","4","5","6","7","8","9"]
D = input().split(" ")
use_num = list(set(list_num) - set(D))
ok = []
for a in use_num:
ok.append(a)
for b in use_num:
ok.append(a+b)
for c in use_num:
ok.append(a+b+c)
for d in use_num:
ok.append(a+b+c+d)
for e in use_num:
ok.append(a+b+c+d+e)
ans = []
for i in ok:
if int(i) >= N:
ans.append(int(i))
print((min(ans)))
|
def main():
N, K = list(map(int, input().split()))
D = list(input().split())
for i in range(N,100000):
a = str(i)
flag = True
for d in D:
if d in a:
flag = False
break
if flag:
print(i)
exit()
if __name__ == "__main__":
main()
|
p04045
|
N, K = list(map(int, input().split()))
Dset = set(input().split())
while True:
if Dset & set(str(N)) == set():
print(N)
break
else:
N += 1
|
def check(num, ng):
for c in str(num):
if int(c) in ng:
return False
return True
N, K = list(map(int, input().split()))
D = set(list(map(int, input().split())))
while True:
if check(N, D):
print(N)
break
else:
N += 1
|
p04045
|
# -*- coding: utf-8 -*-
"""
https://beta.atcoder.jp/contests/abc042/tasks/arc058_a
"""
import sys
from sys import stdin
input = stdin.readline
def solve(forbiddens, N):
forbiddens.sort()
ans = N
repeat = True
while repeat:
repeat = False
num_s = str(ans)
for s in num_s:
if int(s) in forbiddens:
repeat = True
ans += 1
break
return ans
def main(args):
# N, K = 9999, 1
# forbiddens = [9]
N, K = list(map(int, input().split()))
forbiddens = [int(x) for x in input().split()]
ans = solve(forbiddens, N)
print(ans)
if __name__ == '__main__':
main(sys.argv[1:])
|
# -*- coding: utf-8 -*-
"""
https://beta.atcoder.jp/contests/abc042/tasks/arc058_a
AC
"""
import sys
from sys import stdin
input = stdin.readline
def solve(forbiddens, N):
forbiddens = set(forbiddens)
ans = N
repeat = True
while repeat:
repeat = False
num_s = str(ans)
for s in num_s:
if int(s) in forbiddens:
repeat = True
ans += 1
break
return ans
def main(args):
# N, K = 9999, 1
# forbiddens = [9]
N, K = list(map(int, input().split()))
forbiddens = [int(x) for x in input().split()]
ans = solve(forbiddens, N)
print(ans)
if __name__ == '__main__':
main(sys.argv[1:])
|
p04045
|
n, k, *D = map(int, open(0).read().split())
D = list({0,1,2,3,4,5,6,7,8,9} - set(D))
N = list(map(int, str(n)))
def dfs(N, M, i, f):
if i == len(N):
print(*M, sep='')
exit()
if f:
dfs(N, M + [D[0]], i+1, 1)
for d in D:
if d == N[i]:
dfs(N, M+[d], i+1, 0)
elif d > N[i]:
dfs(N, M+[d], i+1, 1)
break
if D[0] == 0:
dfs(N, [D[1], D[0]], 1, 1)
else:
dfs(N, [D[0], D[0]], 1, 1)
dfs(N, [], 0, 0)
|
n, k, *D = map(int, open(0).read().split())
D = list({0,1,2,3,4,5,6,7,8,9} - set(D))
N = list(map(int, str(n)))
d0 = D[0]
def dfs(M, i, f):
if i == len(N):
print(*M, sep='')
exit()
if f:
dfs(M+[d0], i+1, 1)
for d in D:
if d == N[i]:
dfs(M+[d], i+1, 0)
elif d > N[i]:
dfs(M+[d], i+1, 1)
break
if d0:
dfs([d0], 0, 1)
else:
dfs([D[1]], 0, 1)
dfs([], 0, 0)
|
p04045
|
S, K = list(map(int, input().split()))
Bad = list(input().split())
while 1:
if all(d not in str(S) for d in Bad):
print(S)
break
S += 1
|
import sys
S, K = input().split()
Bad = list(map(int, input().split()))
OK = sorted(set(range(10)) - set(Bad))
NotZero = sorted(set(OK) - set([0]))
if any(d > str(OK[-1]) for d in S):
print((str(NotZero[0]) + str(OK[0]) * len(S)))
sys.exit()
count = [0] * 10
for c in str(S):
count[ord(c) - ord('0')] += 1
greater = 0
ans = []
def can(i, d):
c = str(d)
if i == 0 and d == 0 and len(S) > 1:
return 0
if not greater and c < S[i]:
return 0
if c > S[i]:
return 1
for b in range(10):
if count[b] > 0 and b > OK[-1]:
return 0
return 1
for i in range(len(S)):
count[ord(S[i]) - ord('0')] -= 1
for d in OK:
if can(i, d):
if str(d) > S[i]:
greater = 1
ans.append(d)
break
print(("".join(str(a) for a in ans)))
|
p04045
|
def main():
N, K = list(map(int, input().split()))
D = input().split()
for i in range(N, N*10):
if len(set(list(str(i))) & set(D)) == 0:
print(i)
break
main()
|
n, k = list(map(int, input().split()))
d = [ int(x) for x in input().split() ]
def is_ok(x):
while x != 0:
if x % 10 in d:
return False
x //= 10
return True
money = n
while True:
if is_ok(n):
print(n)
break
n += 1
|
p04045
|
n, k = list(map(int, input().split()))
l = list(map(int, input().split()))
nums = [i for i in range(10) if not i in l]
while True:
total = 0
for num in nums:
total += list(str(n)).count(str(num))
if total == len(list(str(n))):
print(n)
break
n += 1
|
n, k = list(map(int, input().split()))
like = list(map(str, set(range(10)) - set(map(int, input().split()))))
while True:
count = 0
for c in str(n):
if c in like:
count += 1
if count == len(str(n)):
print(n)
break
n += 1
|
p04045
|
n, k = list(map(int, input().split()))
d = list(map(int, input().split()))
if 0 in d:
zero = True
else:
zero = False
n_ok = {0} | set([0,1,2,3,4,5,6,7,8,9]) - set(d)
n_oks = sorted(n_ok)
#print(n_oks)
def lowest():
for i in n_oks:
for j in n_oks:
for k in n_oks:
for l in n_oks:
for m in n_oks:
money = 10000*i +1000*j + 100*k + 10*l + m
if money >= n:
if zero:
if 0 in [i,j,k,l,m] and i != 0:
continue
elif 0 in [j,k,l,m] and j != 0:
continue
if 0 in [k,l,m] and k != 0:
continue
if 0 in [l,m] and l != 0:
continue
print(money)
return
lowest()
|
n, k = list(map(int, input().split()))
d = list(map(int, input().split()))
if 0 in d:
zero = True
else:
zero = False
n_ok = {0} | set([0,1,2,3,4,5,6,7,8,9]) - set(d)
n_oks = sorted(n_ok)
#print(n_oks)
def lowest():
for i in n_oks:
for j in n_oks:
for k in n_oks:
for l in n_oks:
for m in n_oks:
money = 10000*i +1000*j + 100*k + 10*l + m
if money >= n:
if zero:
if 0 in [i,j,k,l,m] and i != 0:
continue
elif 0 in [j,k,l,m] and j != 0:
continue
elif 0 in [k,l,m] and k != 0:
continue
elif 0 in [l,m] and l != 0:
continue
print(money)
return
lowest()
|
p04045
|
n,k = list(map(int,input().split()))
d = list(map(int,input().split()))
while 1:
for c in str(n):
if int(c) in d:
break
else:
print(n)
exit()
n += 1
|
n,k=list(map(int,input().split()));d=input().split()
while 1:
for c in str(n):
if c in d:break
else:
print(n)
exit()
n += 1
|
p04045
|
from collections import deque
N,K=list(map(int,input().split()))
D=list(map(int,input().split()))
like=list(range(1,10))
dislike0=False
for s in D:
if s==0:
dislike0=True
else:
like.remove(s)
like.sort()
q=deque(like)
if dislike0==False:
like.insert(0,0)
while q:
num=q.popleft()
if num>=N:
print(num)
exit()
for i in range(len(like)):
q.append((int(str(num)+str(like[i]))))
|
N,K=list(map(int,input().split()))
D=list(input().split())
while True:
for s in str(N):
if s in D:
N+=1
break
else:
print(N)
exit()
|
p04045
|
n,k=list(map(int,input().split()))
d=list(map(int,input().split()))
a=[]
for i in range(10):
sum=0
for x in d:
if x==i:
break
else:
sum+=1
if sum==k:
a.append(i)
a=set(a)
for i in range(n,10*n+1):
b=[]
x=len(str(i))
y=str(i)
for j in range(x):
b.append(int(y[j]))
b=set(b)
if a>=b:
print(i)
break
|
n,k=list(map(int,input().split()))
d=set(input().split())
while len(d&set(str(n)))!=0:
n+=1
print(n)
|
p04045
|
N,K = list(map(int,input().split()))
D = list(map(str,list(map(int,input().split()))))
n = N
while True:
s = str(n)
S = set(list(s))
for c in S:
if c in D:
break
else:
break
n += 1
print(n)
|
N,K=list(map(int, input().split()))
D=set(input().split())
def check(x):
for c in str(x):
if c in D: return False
return True
ans = N
while not check(ans): ans += 1
print(ans)
|
p04045
|
n,k = list(map(int,input().split()))
d = input().split()
f = True
while(f):
for s in d:
if s in str(n):
_ = n
n += 1
break
else:
break
print(n)
|
n,k = list(map(int,input().split()))
d = list(map(int,input().split()))
while(True):
x = n
while(x):
if x%10 in d:
break
x//=10
else:
print(n)
quit(0)
n += 1
|
p04045
|
N, K = list(map(int, input().split()))
D = {i for i in range(10)} - set(map(int, input().split()))
for i in range(N, 100000):
if all(j in D for j in map(int, str(i))):
print(i)
break
|
N, K, *D = list(map(int, open(0).read().split()))
D = set(str(d) for d in D)
for i in range(N, 100_000):
if set(str(i)) & D:
continue
print(i)
break
|
p04045
|
lst_1 = input().split()
N = lst_1[0]
K = int(lst_1[1])
lst_N = list(N)
lst_hate = input().split()
flg = True
while 1:
for i in range(len(lst_N)):
for j in range(len(lst_hate)):
if lst_N[i] == lst_hate[j]:
flg = False
break
if flg == True:
break
N = ''.join(lst_N)
N = int(N)
N += 1
lst_N = list(str(N))
flg = True
N = ''.join(lst_N)
print(N)
|
N, K = list(map(int, input().split()))
D = list(map(int, input().split()))
k = N
"""while k > 0:
lst.append(k % 10)
k //= 10
#lst.reverse()"""
#print(lst)
while 1:
flg = True
lst = []
N = k
while k > 0:
lst.append(k % 10)
k //= 10
lst.reverse()
# print(lst)
for i in range(len(lst)):
if lst[i] in D:
flg = False
break
if flg == False:
k = N
k += 1
else:
break
print(N)
|
p04045
|
n,k=list(map(int,input().split()))
D=set(list(map(int,input().split())))
for i in range(n,10*n):
num=set(list(map(int,str(i))))
if i>=n and len(D&num)==0:
print(i)
exit()
|
n,k=list(map(int,input().split()))
D=set(list(map(str,input().split())))
for i in range(n,10*n):
num=set(list(str(i)))
if i>=n and len(D&num)==0:
print(i)
exit()
|
p04045
|
n,k=list(map(int,input().split()))
D = list(map(str,input().split()))
x = n
while True:
y = str(x)
for i in range(len(y)):
if y[i] in D:
break
else:
ans = x
break
x += 1
print(ans)
|
n,k=list(map(int,input().split()))
D = list(map(str,input().split()))
x = n
while True:
y = str(x)
for i in y:
if i in D:
break
else:
ans = x
break
x += 1
print(ans)
|
p04045
|
# 0〜9
l=list(range(10))
# 入力
N,K=list(map(int,input().split()))
l2=list(map(int,input().split()))
ans=''
f=False
# 使用可能な数字を求める
for x in l2:
l.remove(x)
# 集合型に変換
l=set(l)
for x in range(1,1000000):
y=set(map(int,list(str(x))))
if N<=x and l.issuperset(y):
print(x)
break
|
# 0〜9
l=list(range(10))
# 入力
N,K=list(map(int,input().split()))
l2=list(map(int,input().split()))
# 使用可能な数字を求める
for x in l2:
l.remove(x)
# 集合型に変換
l=set(l)
# 1<=N<=10000 なので、答えは最大でも100000まで
for x in range(1,100001):
# 集合型に変換
y=set(map(int,list(str(x))))
# Nを上回る数字かつ、使える数字のみを使用している場合
if N<=x and l.issuperset(y):
print(x)
break
|
p04045
|
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time
sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
def LI(): return list(map(int,input().split()))
def I(): return int(eval(input()))
def LS(): return input().split()
def S(): return eval(input())
def main():
n,k=LI()
l=LI()
a=0
while True:
a+=1
if a>=n:
f=True
for x in list(str(a)):
if int(x) in l:
f=False
break
if f:
return a
print((main()))
|
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time
sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
def LI(): return list(map(int,input().split()))
def I(): return int(eval(input()))
def LS(): return input().split()
def S(): return eval(input())
def main():
n,k=LI()
l=LS()
ans=1
while True:
if ans>=n:
s=str(ans)
f=True
for x in s:
if x in l:
f=False
break
if f:
return ans
ans+=1
print((main()))
|
p04045
|
amount, num = list(map(int, input().split()))
hate_num_list = list(map(str, input().split()))
while(True):
check = 0
for digit in str(amount):
if digit in hate_num_list:
check += 1
if check == 0:
print(amount)
break
else:
amount += 1
|
amount, num = list(map(int, input().split()))
hated_digit = list(map(str, input().split()))
while True:
check = 0
for hd in hated_digit:
if hd not in str(amount):
check += 1
else:
amount += 1
if check == num:
print(amount)
break
|
p04045
|
N, L = list(map(int, input().split()))
D = input().split()
ans = N
while 1:
s = str(ans)
f = 1
for i in D:
if i in s : f = 0
if f : break
ans += 1
print(ans)
|
def main():
N, L = list(map(int, input().split()))
D = input().split()
ans = N
while 1:
s = str(ans)
if not [1 for i in D if i in s] : break
ans += 1
print(ans)
if __name__ == '__main__':
main()
|
p04045
|
import sys
readline = sys.stdin.readline
def main():
N, K = list(map(int, readline().rstrip().split()))
D = list(map(int, readline().rstrip().split()))
nums = [str(i) for i in range(10)]
for d in D:
nums.remove(str(d))
for i in range(N, N*10+1):
d = str(i)
flg = True
for e in d:
if e not in nums:
flg = False
break
if flg:
print(i)
return
if __name__ == '__main__':
main()
|
import sys
readline = sys.stdin.readline
def main():
N, K = list(map(int, readline().rstrip().split()))
D = set(readline().rstrip().split())
for i in range(N, N*10+1):
d = str(i)
flg = True
for e in d:
if e in D:
flg = False
break
if flg:
print(i)
return
if __name__ == '__main__':
main()
|
p04045
|
n, k = list(map(int, input().split()))
A = list(input().split())
for i in range(10 * n):
for a in A:
if a in str(n + i):
break
else:
ans = n + i
break
print(ans)
|
n, k = list(map(int, input().split()))
A = list(input().split())
for i in range(n * 10):
for j in str(n + i):
if j in A:
break
else:
print((n + i))
break
|
p04045
|
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()
def resolve():
N,K=list(map(int,input().split()))
N= str(N)
S={i for i in range(10)}
D=set(map(int,input().split()))
D=S-D
def search(N):
for i in range(len(N)):
if int(N[i]) not in D:
return True
elif i==len(N)-1:
return False
while True:
if search(N) ==True:
N=str(int(N)+1)
else:
print(N)
break
resolve()
|
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()
def resolve():
def main():
N,K=list(map(int,input().split()))
D=set(map(int,input().split()))
L={i for i in range(10)}
U=L-D
while True:
sn = str(N)
ln = len(sn)
for i in range(ln):
if int(sn[i]) not in U:
N+=1
break
elif i==ln-1:
return int(N)
print((main()))
resolve()
|
p04045
|
from collections import deque
n, m = list(map(int, input().split()))
e = [int(eval(input())) for _ in range(m)]
d = deque(i for i in range(1, n+1))
for v in e[::-1]:
try:
d.remove(v)
print(v)
except ValueError:
pass
while d:
print((d.popleft()))
|
n, m = list(map(int, input().split()))
e = [int(eval(input())) for _ in range(m)]
b = [1] * (n+1)
for v in e[::-1]:
if b[v]:
b[v] = 0
print(v)
for i in range(1, n+1):
if b[i]:
print(i)
|
p00946
|
n = int(eval(input()))
G = [[] for i in range(n)]
root = 0
color_num = 0
keys = []
def get_key(n1, n2):
if n1 > n2:
return str(n2) + '-' + str(n1)
return str(n1) + '-' + str(n2)
for _ in range(n - 1):
a, b = list(map(int, input().split()))
keys.append(get_key(a - 1, b - 1))
G[a - 1].append(b - 1)
G[b - 1].append(a - 1)
if color_num < len(G[a - 1]):
root = a - 1
color_num = len(G[a - 1])
elif color_num < len(G[b - 1]):
root = b - 1
color_num = len(G[b - 1])
# print(G)
# [[1], [0, 2, 3, 4], [1], [1, 6], [1, 5], [4, 7], [3], [5]]
# print(root) # 最も多い色を持つノードの番号
print(color_num) # 必要な最小色数
queue = [root]
checked = set()
result = {}
min_colors = [set(range(color_num)) for i in range(n)]
while len(queue) > 0:
node = queue.pop(0)
# print(node + 1)
# ks = list(range(color_num))
# print(node)
# print(ks)
# for node2 in G[node]:
# key = get_key(node, node2)
# if key in result:
# ks.pop(result[key])
for node2 in G[node]:
if node2 not in checked:
key = get_key(node, node2)
# result[key] = ks.pop(0)
result[key] = min_colors[node].pop()
min_colors[node2].discard(result[key])
queue.append(node2)
checked.add(node)
for k in keys:
print((result[k] + 1))
|
n = int(eval(input()))
G = [[] for i in range(n)]
root = 0
color_num = 0
for i in range(n - 1):
a, b = list(map(int, input().split()))
G[a - 1].append((b - 1, i))
G[b - 1].append((a - 1, i))
if color_num < len(G[a - 1]):
root = a - 1
color_num = len(G[a - 1])
elif color_num < len(G[b - 1]):
root = b - 1
color_num = len(G[b - 1])
# print(G)
# [[1], [0, 2, 3, 4], [1], [1, 6], [1, 5], [4, 7], [3], [5]]
# print(root) # 最も多い色を持つノードの番号
print(color_num) # 必要な最小色数
queue = [(root, -1)]
checked = set()
answer = [-1] * (n - 1)
while len(queue) > 0:
node, c = queue.pop(0)
color = 1
for node2, index in G[node]:
if node2 not in checked:
if color == c:
color = c + 1
answer[index] = color
color += 1
queue.append((node2, answer[index]))
checked.add(node)
print(('\n'.join(map(str, answer))))
|
p02850
|
from collections import deque
import sys
input = sys.stdin.readline
def bfs(v, N, G, E):
visited = [0]*N
queue = deque()
new_color = 1
used_color = set()
num_used_color = 0
node2colors = [set() for _ in range(N)]
queue.append(v)
visited[v] = 1
while queue:
q = queue.popleft()
# print("q",q)
num_color_from_q = 0
while G[q]:
nex = G[q].popleft()
if visited[nex]:
continue
visited[nex] = 1
rest = num_used_color - num_color_from_q
# print(rest)
if rest > 0:
usd_c = used_color - node2colors[q]
if usd_c:
# print(used_color, node2colors[q])
usd_c = min(usd_c)
E[(q, nex)][1] = usd_c
node2colors[q].add(usd_c)
num_color_from_q += 1
node2colors[nex].add(usd_c)
else:
# print(num_used_color, num_color_from_q)
new_c = new_color
new_color += 1
num_used_color += 1
E[(q, nex)][1] = new_c
node2colors[q].add(new_c)
num_color_from_q += 1
node2colors[nex].add(new_c)
used_color.add(new_c)
else:
new_c = new_color
new_color += 1
num_used_color += 1
E[(q, nex)][1] = new_c
node2colors[q].add(new_c)
num_color_from_q += 1
node2colors[nex].add(new_c)
used_color.add(new_c)
queue.append(nex)
return num_used_color
def main():
N = int(eval(input()))
G = [deque() for _ in range(N)]
E = dict() # (a, b) -> [idx, color]
for i in range(N-1):
a, b = [int(x)-1 for x in input().split()]
G[a].append(b)
G[b].append(a)
E[(a, b)] = [i, 0]
K = bfs(0, N, G, E)
# ans_list = [value for value in E.values()]
print(K)
for value in list(E.values()):
print((value[1]))
main()
|
from collections import deque
import sys
input = sys.stdin.readline
def bfs(v, N, G, E):
visited = [0]*N
queue = deque()
K = -1
node2color = [-1 for _ in range(N)]
queue.append(v)
visited[v] = 1
while queue:
q = queue.popleft()
color = 0
for nex in G[q]:
if visited[nex]:
continue
visited[nex] = 1
color += 1
if color == node2color[q]:
color += 1
node2color[nex] = color
E[(min(q, nex), max(q, nex))] = color
queue.append(nex)
K = max(K, color)
return K
def main():
N = int(eval(input()))
G = [deque() for _ in range(N)]
E = dict() # (a, b) -> [idx, color]
for i in range(N-1):
a, b = [int(x)-1 for x in input().split()]
G[a].append(b)
G[b].append(a)
E[(a, b)] = 0
K = bfs(0, N, G, E)
# ans_list = [value for value in E.values()]
print(K)
for value in list(E.values()):
print(value)
main()
|
p02850
|
N = int(eval(input()))
ABs = []
for i in range(N - 1):
ABs.append(tuple(map(int, input().split())))
Cs = [0] * (N + 1)
for i in range(N - 1):
Cs[ABs[i][0]] += 1
Cs[ABs[i][1]] += 1
K = max(Cs)
print(K)
ABsc = [[False] * (K + 1) for _ in range(N + 1)]
Tr = [[] for _ in range(N + 1)]
for i in range(N - 1):
p = min(ABs[i][0], ABs[i][1])
c = max(ABs[i][0], ABs[i][1])
Tr[p].append((c, i))
import collections
d = collections.deque()
d.append(1)
def minC(i):
for j in range(1, K + 1):
if not ABsc[i][j]:
return j
Cis = [1] * N
while (len(d) > 0):
p = d.popleft()
for (c, i) in Tr[p]:
d.append(c)
col = minC(p)
ABsc[p][col] = True
ABsc[c][col] = True
Cis[i] = col
for i in range(N - 1):
print((Cis[i]))
|
N = int(eval(input()))
ABs = []
for i in range(N - 1):
ABs.append(tuple(map(int, input().split())))
Cs = [0] * (N + 1)
Tr = [[] for _ in range(N + 1)]
for i in range(N - 1):
Cs[ABs[i][0]] += 1
Cs[ABs[i][1]] += 1
p = min(ABs[i][0], ABs[i][1])
c = max(ABs[i][0], ABs[i][1])
Tr[p].append((c, i))
K = max(Cs)
print(K)
import collections
d = collections.deque()
d.append(1)
Pc = [0] * (N + 1)
Nc = [1] * (N + 1)
ABsc = [0] * (N + 1)
Cis = [1] * N
while (len(d) > 0):
p = d.popleft()
for (c, i) in Tr[p]:
d.append(c)
col = Nc[p]
if Pc[p] == col:
col += 1
Nc[p] = col + 1
Pc[c] = col
Cis[i] = col
for i in range(N - 1):
print((Cis[i]))
|
p02850
|
#!/usr/bin/env python3
import sys
from collections import deque
def solve(N: int, a: "List[int]", b: "List[int]"):
h = [[] for _ in range(N+1)]
edges = [[-1]*(N+1) for _ in range(N+1)]
## 全てのNodeは親は一つだけ
for i in range(N-1):
h[a[i]].append((b[i],i))
h[b[i]].append((a[i],i))
## node二つ与えたときにその間の辺
for i in range(N-1):
edges[a[i]][b[i]] = i
edges[b[i]][a[i]] = i
queue = deque([1])
## 頂点iの親を格納
parents = [-1]*(N+1)
parents[1] = 0
while queue:
q = queue.pop()
for child,_ in h[q]:
if parents[child] == -1:
parents[child] = q
queue.append(child)
answer = [0]*(N-1)
max_color = max(len(a) for a in h)
color = list(range(1,max_color+1))
queue = deque([1])
while queue:
color_index = 0
q = queue.popleft()
parent = parents[q]
parent_edge = edges[parent][q]
for node, j in h[q]:
##もう埋まっているか、親と一緒
if answer[j] > 0 or q == node:
continue
if answer[parent_edge] != color[color_index] or q == 1:
answer[j] = color[color_index]
else:
color_index+=1
answer[j] = color[color_index]
color_index+=1
queue.append(node)
print(max_color)
for a in answer:
print(a)
return
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
N = int(next(tokens)) # type: int
a = [int()] * (N - 1) # type: "List[int]"
b = [int()] * (N - 1) # type: "List[int]"
for i in range(N - 1):
a[i] = int(next(tokens))
b[i] = int(next(tokens))
solve(N, a, b)
if __name__ == '__main__':
main()
|
#!/usr/bin/env python3
import sys
from collections import deque
def solve(N: int, a: "List[int]", b: "List[int]"):
h = [[] for _ in range(N+1)]
edges = [[-1]*(N+1) for _ in range(N+1)]
## 全てのNodeは親は一つだけ
for i in range(N-1):
h[a[i]].append((b[i],i))
h[b[i]].append((a[i],i))
## node二つ与えたときにその間の辺
for i in range(N-1):
edges[a[i]][b[i]] = i
edges[b[i]][a[i]] = i
queue = deque([1])
## 頂点iの親を格納
parents = [-1]*(N+1)
parents[1] = 0
while queue:
q = queue.pop()
for child,_ in h[q]:
if child != q and parents[child] == -1:
parents[child] = q
queue.append(child)
answer = [0]*(N-1)
max_color = max(len(a) for a in h)
queue = deque([1])
while queue:
color = 1
q = queue.popleft()
parent_edge = edges[parents[q]][q]
ng_color = answer[parent_edge]
for node, j in h[q]:
##もう埋まっているか、親と一緒
if answer[j] > 0 or q == node:
continue
if color!=ng_color or q == 1:
answer[j] = color
else:
color+=1
answer[j] = color
color+=1
queue.append(node)
print(max_color)
for a in answer:
print(a)
return
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
N = int(next(tokens)) # type: int
a = [int()] * (N - 1) # type: "List[int]"
b = [int()] * (N - 1) # type: "List[int]"
for i in range(N - 1):
a[i] = int(next(tokens))
b[i] = int(next(tokens))
solve(N, a, b)
if __name__ == '__main__':
main()
|
p02850
|
import sys
from collections import deque
sys.setrecursionlimit(10 ** 7)
N = int(eval(input()))
count = [[] for _ in range(N)]
inputs = []
for _ in range(N - 1):
a, b = list(map(int, input().split()))
count[a - 1].append(b - 1)
count[b - 1].append(a - 1)
inputs.append(sorted([a - 1, b - 1]))
K = 0
for c in count:
K = max(K, len(c))
print(K)
def dfs(root, count):
done = [-1 for _ in range(N)]
done[root] = 0
info = []
d = deque([root])
r = d.pop()
childs = count[r]
tmp = 1
for c in childs:
if done[c] == -1:
d.append([c, r, tmp])
tmp += 1
done[c] += 1
while d:
data = d.pop()
info.append(data)
childs = count[data[0]]
tmp = 1
for c in childs:
if done[c] == -1:
if tmp != data[2]:
d.append([c, data[0], tmp])
else:
tmp += 1
d.append([c, data[0], tmp])
tmp += 1
done[c] += 1
return info
info = dfs(0, count)
info_new = [[sorted([i[0], i[1]]), i[2]] for i in info]
for i in inputs:
for k in info_new:
if i == k[0]:
print((k[1]))
|
import sys
from collections import deque, OrderedDict
sys.setrecursionlimit(10 ** 7)
N = int(eval(input()))
count = [[] for _ in range(N)]
inputs = OrderedDict()
for _ in range(N - 1):
a, b = list(map(int, input().split()))
count[a - 1].append(b - 1)
count[b - 1].append(a - 1)
inputs["-".join(list(map(str, sorted([a - 1, b - 1]))))] = 0
K = 0
for c in count:
K = max(K, len(c))
print(K)
def dfs(root, count, inputs):
done = [-1 for _ in range(N)]
done[root] = 0
d = deque([root])
r = d.pop()
childs = count[r]
tmp = 1
for c in childs:
if done[c] == -1:
d.append([c, r, tmp])
tmp += 1
done[c] += 1
while d:
data = d.pop()
inputs["-".join(list(map(str, sorted(data[:2]))))] = data[2]
childs = count[data[0]]
tmp = 1
for c in childs:
if done[c] == -1:
if tmp != data[2]:
d.append([c, data[0], tmp])
else:
tmp += 1
d.append([c, data[0], tmp])
tmp += 1
done[c] += 1
return inputs
inputs = dfs(0, count, inputs)
for i in list(inputs.values()):
print(i)
|
p02850
|
import sys
input = sys.stdin.readline
n = int(eval(input()))
GRAPH = {i:[] for i in range(1, n + 1)}
EDGES = []
COLORS = {(0, 1):0}
for _ in range(n - 1):
a, b = list(map(int, input().split()))
GRAPH[a].append(b)
GRAPH[b].append(a)
EDGES.append((a, b))
COLORS[(a, b)] = 0
PREV = {1:0}
Q = [1]
while Q:
now = Q.pop(0)
for vertex in GRAPH[now]:
if vertex == PREV[now]:
continue
next = vertex
PREV[next] = now
Q.append(next)
Q = [1]
while Q:
now = Q.pop(0)
prev = PREV[now]
a, b = min(now, prev), max(now, prev)
used = COLORS[(a, b)]
color = 1
for vertex in GRAPH[now]:
if vertex == prev:
continue
next = vertex
a, b = min(now, next), max(now, next)
if color == used:
color += 1
COLORS[(a, b)] = color
color += 1
else:
COLORS[(a, b)] = color
color += 1
Q.append(next)
print((max(COLORS.values())))
for edge in EDGES:
print((COLORS[edge]))
|
import sys
input = sys.stdin.readline
n = int(eval(input()))
GRAPH = {i:[] for i in range(1, n + 1)}
EDGES = []
for _ in range(n - 1):
a, b = list(map(int, input().split()))
GRAPH[a].append(b)
GRAPH[b].append(a)
EDGES.append((a, b))
PREV = {1:0}
Q = [1]
while Q:
now = Q.pop(0)
for vertex in GRAPH[now]:
if vertex == PREV[now]:
continue
next = vertex
PREV[next] = now
Q.append(next)
COLORS = {(0, 1):0}
Q = [1]
while Q:
now = Q.pop(0)
prev = PREV[now]
a, b = min(now, prev), max(now, prev)
used = COLORS[(a, b)]
color = 1
for vertex in GRAPH[now]:
if vertex == prev:
continue
next = vertex
a, b = min(now, next), max(now, next)
if color == used:
color += 1
COLORS[(a, b)] = color
color += 1
else:
COLORS[(a, b)] = color
color += 1
Q.append(next)
print((max(COLORS.values())))
for edge in EDGES:
print((COLORS[edge]))
|
p02850
|
#キューとdictionaryを利用したBFS
from collections import deque
n = int(eval(input()))
ab = [list(map(int,input().split())) for i in range(n-1)]
ver = [[] for i in range(n)]
for i in range(n-1):
a,b = ab[i][0],ab[i][1]
ver[a-1].append(b-1)
ver[b-1].append(a-1)
#print(ver)
color = [dict() for i in range(n)]
q = deque()
i = 1
for v in ver[0]:
color[0][v] = i
color[v][0] = i
q.append((0,v))
i += 1
#print(q)
while q:
f,t = q.popleft()
i = 1
for v in ver[t]:
if v == f:
continue
if i == color[f][t]:
i += 1
color[t][v] = i
color[v][t] = i
q.append((t,v))
i += 1
#print(color)
#辞書のリストで辺の色を管理している
k = 0
for i in range(n):
k = max(k,len(ver[i]))
print(k)
for i in range(n-1):
a,b = ab[i][0],ab[i][1]
print((color[a-1][b-1]))
|
#d3と同一解法
n=int(eval(input()))
import sys
#再帰関数の再帰回数を変更
sys.setrecursionlimit(10**6)
s=[[] for _ in range(n)]
for i in range(n-1):
a,b=list(map(int,input().split()))
s[a-1].append((b-1,i))
s[b-1].append((a-1,i))
l=[0]*(n-1)
def dfs(node,pre_col,par):
col=1
for nei,i in s[node]:
if nei==par:
continue
if col==pre_col:
col+=1
l[i]=col
dfs(nei,col,node)
col+=1
dfs(0,-1,-1)
print((max(l)))
for i in l:
print(i)
|
p02850
|
#d3と同一解法
n=int(eval(input()))
import sys
#再帰関数の再帰回数を変更
sys.setrecursionlimit(10**6)
s=[[] for _ in range(n)]
for i in range(n-1):
a,b=list(map(int,input().split()))
s[a-1].append((b-1,i))
s[b-1].append((a-1,i))
l=[0]*(n-1)
def dfs(node,pre_col,par):
col=1
for nei,i in s[node]:
if nei==par:
continue
if col==pre_col:
col+=1
l[i]=col
dfs(nei,col,node)
col+=1
dfs(0,-1,-1)
print((max(l)))
for i in l:
print(i)
|
import sys
sys.setrecursionlimit(500000)
N = int(eval(input()))
E = [[] for _ in range(N+1)]
for i in range(N-1):
a, b = list(map(int, input().split()))
E[a].append((b, i))
E[b].append((a, i))
K = max(len(e) for e in E)
print(K)
Ans = [-1] * (N-1)
def dfs(v=1, p=0, p_col=-1):
col = 1
for u, idx in E[v]:
if u!=p: #木構造の逆向きの矢印を除くための条件
if col == p_col:
col += 1
Ans[idx] = col
#print(p_col)
dfs(u, v, col) #u;調べる頂点 v;直前に調べた辺で、調べる頂点と結ばれている頂点 col;直前に調べた辺の色番号
col += 1 #最深部まで調べたら デフォルトの色=最後に調べた辺の色+1とする
#最深部に繋がった辺の色+1してやれば、次の辺の色は必ず条件を満たしてくれる
dfs()
print(("\n".join(map(str, Ans))))
|
p02850
|
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit
INF = 1 << 50
EPS = 1e-8
mod = 10 ** 9 + 7
def mapline(t = int):
return list(map(t, sysread().split()))
def mapread(t = int):
return list(map(t, read().split()))
def dfs(c, to, cols, max_counts, pre_col = 0):
colors = set()
colors.add(pre_col)
for idx, n in to[c]:
if cols[idx]:continue
num = 1
for i in range(num, max_counts+1):
if not i in colors:
num = i
cols[idx] = i
colors.add(i)
dfs(n, to, cols, max_counts, i)
break
def run():
N = int(sysread())
to = [[] for _ in range(N+1)]
cols = [0] * (N-1)
counts = [0 for i in range(N+1)]
for i in range(N-1):
a,b = mapline()
to[a].append((i, b))
to[b].append((i, a))
counts[a] += 1
counts[b] += 1
max_counts = max(counts)
print(max_counts)
dfs(1, to, cols, max_counts)
for c in cols:
print(c)
#print(a ^ b)
if __name__ == "__main__":
run()
|
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit
INF = 1 << 50
EPS = 1e-8
mod = 10 ** 9 + 7
def mapline(t = int):
return list(map(t, sysread().split()))
def mapread(t = int):
return list(map(t, read().split()))
def dfs(c, to, cols, max_counts, pre_col = 0):
colors = set()
colors.add(pre_col)
num = 1
for idx, n in to[c]:
if cols[idx]:continue
for i in range(num, max_counts+1):
if not i in colors:
num = i
cols[idx] = i
colors.add(i)
dfs(n, to, cols, max_counts, i)
break
def run():
N = int(sysread())
to = [[] for _ in range(N+1)]
cols = [0] * (N-1)
counts = [0 for i in range(N+1)]
for i in range(N-1):
a,b = mapline()
to[a].append((i, b))
to[b].append((i, a))
counts[a] += 1
counts[b] += 1
max_counts = max(counts)
print(max_counts)
dfs(1, to, cols, max_counts)
for c in cols:
print(c)
#print(a ^ b)
if __name__ == "__main__":
run()
|
p02850
|
from collections import deque
def solve(string):
n, *ab = list(map(int, string.split()))
next_ = [dict([]) for _ in range(n + 1)]
queue = deque([1])
for a, b in zip(*[iter(ab)] * 2):
next_[a][b] = 0
next_[b][a] = 0
while len(queue) > 0:
c = queue.popleft()
_next = next_[c]
use = set(range(1, len(list(_next.keys())) + 1)) - set(next_[c].values())
for _n, u in zip((k for k, v in list(_next.items()) if v == 0), use):
queue.append(_n)
next_[c][_n] = u
next_[_n][c] = u
return str("{}\n{}".format(max([len(list(x.keys())) for x in next_]),
"\n".join(str(next_[a][b]) for a, b in zip(*[iter(ab)] * 2))))
if __name__ == '__main__':
import sys
print((solve(sys.stdin.read().strip())))
|
from collections import deque
def solve(string):
n, *ab = list(map(int, string.split()))
e = [[] for _ in range(n + 1)]
q = deque([1])
c = [0] * (n + 1)
for a, b in zip(*[iter(ab)] * 2):
e[a].append(b)
while q:
s = q.popleft()
d = 0
for t in e[s]:
d += 1 + (d + 1 == c[s])
c[t] = d
q.append(t)
return str("{}\n{}".format(max(c), "\n".join(str(c[b]) for _, b in zip(*[iter(ab)] * 2))))
if __name__ == '__main__':
import sys
print((solve(sys.stdin.read().strip())))
|
p02850
|
from collections import deque
N = int(input())
X = [list(map(int, input().split())) for _ in range(N - 1)]
graph = [[] for _ in range(N)]
tree = [[] for _ in range(N)]
for n, (i, j) in enumerate(X):
graph[i - 1].append(j - 1)
graph[j - 1].append(i - 1)
tree[i - 1].append((n, j - 1))
K = max(len(x) for x in graph)
q = deque()
q.append((0, 0))
ans = [0] * (N - 1)
while len(q) > 0:
# u: node number, c: color
u, c = q.popleft()
cnt = 1
for i, (n, v) in enumerate(tree[u]):
if cnt == c:
cnt += 1
ans[n] = cnt
q.append((v, cnt))
cnt += 1
print(K)
print(*ans, sep="\n")
|
from collections import deque
N = int(input())
X = [list(map(int, input().split())) for _ in range(N - 1)]
tree = [[] for _ in range(N)]
for n, (i, j) in enumerate(X):
tree[i - 1].append((n, j - 1))
q = deque()
q.append((0, 0))
ans = [0] * (N - 1)
while len(q) > 0:
# u: node number, c: color
u, c = q.popleft()
cnt = 1
for i, (n, v) in enumerate(tree[u]):
if cnt == c:
cnt += 1
ans[n] = cnt
q.append((v, cnt))
cnt += 1
print(max(ans))
print(*ans, sep="\n")
|
p02850
|
from collections import defaultdict
N = int(input())
tree = [[] for i in range(N)] # 入力で与えられる木: [[to, 辺のindex],]
degrees = defaultdict(int) # 各頂点の次数を記録
for i in range(N - 1):
a, b = map(int, input().split())
a, b = a - 1, b - 1
tree[a].append([b, i])
tree[b].append([a, i])
degrees[a] += 1
degrees[b] += 1
# 最大次数色使えば必ず塗り分けられる
ans_K = max(degrees.values())
ans_c = [-1] * (N - 1)
# [現在見ている頂点, 直前を何色で塗ったか]
stack = [[0, -1]]
visited = [False] * N
while stack:
fr, pre_color = stack.pop()
visited[fr] = True
painting = 1 # これから何色で塗ろうとするか
for to, index in tree[fr]:
if visited[to]:
continue
if painting == pre_color:
painting += 1
ans_c[index] = painting
stack.append([to, painting])
# 塗ったら違う色に変える
painting += 1
print(ans_K)
print(*ans_c, sep='\n')
|
N = int(input())
T = [[] for i in range(N)]
for i in range(N - 1):
a, b = map(int, input().split())
a, b = a - 1, b - 1
# 辺のindexを持っておく
T[a].append([b, i])
T[b].append([a, i])
stack = [[0, 0]] # [次の頂点番号, 親に続く辺の色]
visited = [0] * N
colors = [0] * (N - 1)
while stack:
n, parent_color = stack.pop()
visited[n] = 1
color = 1
for to, i in T[n]:
if visited[to]:
continue
if color == parent_color:
color += 1
colors[i] = color
stack.append([to, color])
color += 1
print(len(set(colors)))
print(*colors, sep='\n')
|
p02850
|
import sys
input = sys.stdin.readline
def main():
N = int(eval(input()))
G = [[] for _ in range(N)]
for i in range(N-1):
a, b = list(map(int, input().split()))
G[a-1].append((b-1, i))
G[b-1].append((a-1, i))
V = [[-1, -1] for _ in range(N)] # 頂点の[親, 親への辺の色]
ans = [-1]*(N-1)
import collections
Q = collections.deque()
Q.append(0)
while Q:
i = Q.popleft()
color = 0
for c, ei in G[i]:
if c == V[i][0]:
continue
V[c][0] = i
color += 1
if color == V[i][1]:
color += 1
V[c][1] = ans[ei] = color
Q.append(c)
print((max(ans)))
print(('\n'.join(map(str, ans))))
if __name__ == '__main__':
main()
|
import sys
input = sys.stdin.readline
def main():
N = int(eval(input()))
G = [[] for _ in range(N)]
for i in range(N-1):
a, b = list(map(int, input().split()))
G[a-1].append((b-1, i))
G[b-1].append((a-1, i))
V = [[-1, -1] for _ in range(N)] # 頂点の[親, 親への辺の色]
ans = [-1]*(N-1)
stack = [0] # dfsになるはず(キューだとbfs)
while stack:
i = stack.pop()
color = 0
for c, ei in G[i]:
if c == V[i][0]:
continue
V[c][0] = i
color += 1
if color == V[i][1]:
color += 1
V[c][1] = ans[ei] = color
stack.append(c)
print((max(ans)))
print(('\n'.join(map(str, ans))))
if __name__ == '__main__':
main()
|
p02850
|
from operator import mul
from functools import reduce
# from collections import Counter
# from itertools import combinations as comb
# from itertools import permutations as perm
# from copy import copy
# 配列二分法アルゴリズム
# https://docs.python.jp/3/library/bisect.html
# import bisect
# 桁数指定
# print('{:.3f}'.format(X))
# from collections import defaultdict
# dic = defaultdict(lambda: ...)
# 値で辞書をソート
# sorted(dic.items(), key=lambda x:x[1])
# ヒープキュー
# https://docs.python.org/ja/3/library/heapq.html
# import heapq
# 正規表現(regular expression)のためのモジュール
# import re
import sys
sys.setrecursionlimit(10**6)
def inpl():
return list(map(int, input().split()))
# 重複組み合わせは nHr = (n + r - 1)Cn
def cmb(n, r):
# combination
if n < r:
return 0
r = min(n - r, r)
if r == 0:
return 1
over = reduce(mul, list(range(n, n - r, -1)))
under = reduce(mul, list(range(1, r + 1)))
return over // under
def gcd(a, b):
# greatest common divisor
while b > 0:
a, b = b, a % b
return a
def lcm(a, b):
# least common multiple
return a * b // gcd(a, b)
def dfs(visit, a, v_b, v_color):
visit[a] = True
t_list = [t for t in tmp_list[a] if t != v_b]
t_color = [c for c in range(ans) if v_color != c]
# print(t_list, t_color)
for b, c in zip(t_list, t_color):
# print(a, b, c)
color[a][c] = True
color[b][c] = True
ans_c[min(a, b), max(a, b)] = c
dfs(visit, b, a, c)
return
N = int(eval(input()))
AB = [inpl() for i in range(N - 1)]
ans_c = {}
tmp_list = [[] for i in range(N + 1)]
for a, b in AB:
tmp_list[a].append(b)
tmp_list[b].append(a)
ans = max(len(a) for a in tmp_list)
# print(ans, "ans")
color = [[False for j in range(ans)] for i in range(N + 1)]
visit = [False for i in range(N + 1)]
dfs(visit, 1, -1, -1)
print(ans)
for a, b in AB:
print((ans_c[a, b] + 1))
|
from operator import mul
from functools import reduce
# from collections import Counter
# from itertools import combinations as comb
# from itertools import permutations as perm
# from copy import copy
# 配列二分法アルゴリズム
# https://docs.python.jp/3/library/bisect.html
# import bisect
# 桁数指定
# print('{:.3f}'.format(X))
# from collections import defaultdict
# dic = defaultdict(lambda: ...)
# 値で辞書をソート
# sorted(dic.items(), key=lambda x:x[1])
# ヒープキュー
# https://docs.python.org/ja/3/library/heapq.html
# import heapq
# 正規表現(regular expression)のためのモジュール
# import re
import sys
sys.setrecursionlimit(10**6)
def inpl():
return list(map(int, input().split()))
# 重複組み合わせは nHr = (n + r - 1)Cn
def cmb(n, r):
# combination
if n < r:
return 0
r = min(n - r, r)
if r == 0:
return 1
over = reduce(mul, list(range(n, n - r, -1)))
under = reduce(mul, list(range(1, r + 1)))
return over // under
def gcd(a, b):
# greatest common divisor
while b > 0:
a, b = b, a % b
return a
def lcm(a, b):
# least common multiple
return a * b // gcd(a, b)
def dfs(visit, a, v_b, v_color):
visit[a] = True
# t_list = [t for t in tmp_list[a] if t != v_b]
# t_color = [c for c in range(ans) if v_color != c]
# print(t_list, t_color)
flag = True
i = 0
for b in tmp_list[a]:
if b == v_b:
continue
# print(a, b, c)
# color[a][c] = True
# color[b][c] = True
if flag is True and i >= v_color:
flag = False
i += 1
c = i
ans_c[min(a, b), max(a, b)] = c
dfs(visit, b, a, c)
i += 1
return
N = int(eval(input()))
AB = [inpl() for i in range(N - 1)]
ans_c = {}
tmp_list = [[] for i in range(N + 1)]
for a, b in AB:
tmp_list[a].append(b)
tmp_list[b].append(a)
ans = max(len(a) for a in tmp_list)
# print(ans, "ans")
# color = [[False for j in range(ans)] for i in range(N + 1)]
visit = [False for i in range(N + 1)]
dfs(visit, 1, N + 1, ans + 1)
print(ans)
for a, b in AB:
print((ans_c[a, b] + 1))
|
p02850
|
from collections import deque
from copy import deepcopy
N=int(eval(input()))
a=[0]*(N-1)
b=[0]*(N-1)
c=[set() for _ in range(N)]
d=[set() for _ in range(N)] # 隣接リスト
e=[-1 for _ in range(N)] #訪問ずみかどうか
g=[[ 0 for _ in range(N)] for _ in range(N)]
for i in range(N-1):
a[i],b[i] = list(map(int,input().split()))
d[a[i]-1].add(b[i]-1)
d[b[i]-1].add(a[i]-1)
f=deepcopy(d)
#print(f)
#x=0としてBFSする
v=deque([0])
e[0]=0
maxi=0
while len(v)>0:
k=v.popleft()
maxi=max(maxi,len(d[k]))
all=set(i+1 for i in range(len(d[k])))
pos=deepcopy(all)
pos -= c[k]
for item in d[k]:
if e[item]==-1:
s=min(pos)
c[k].add(s)
c[item].add(s)
pos.remove(s)
e[item] = e[k]+1
v.append(item)
g[k][item]=s
g[item][k]=s
print(maxi)
for i in range(N-1):
print(( g[a[i]-1][b[i]-1] ))
|
from collections import deque
from copy import deepcopy
N=int(eval(input()))
a=[0]*(N-1)
b=[0]*(N-1)
c=[set() for _ in range(N)]
d=[set() for _ in range(N)] # 隣接リスト
e=[-1 for _ in range(N)] #訪問ずみかどうか
di=dict()
for i in range(N-1):
a[i],b[i] = list(map(int,input().split()))
d[a[i]-1].add(b[i]-1)
d[b[i]-1].add(a[i]-1)
f=deepcopy(d)
v=deque([0])
e[0]=0
maxi=0
while len(v)>0:
k=v.popleft()
maxi=max(maxi,len(d[k]))
all=set(i+1 for i in range(len(d[k])))
pos=deepcopy(all)
pos -= c[k]
for item in d[k]:
if e[item]==-1:
s=min(pos)
c[k].add(s)
c[item].add(s)
pos.remove(s)
e[item] = e[k]+1
v.append(item)
st=str(k)+","+str(item)
di[st] = s
print(maxi)
for i in range(N-1):
t_str = str(a[i]-1)+","+str(b[i]-1)
print((di[t_str]))
|
p02850
|
from collections import deque
n=int(eval(input()))
l=[list(map(int,input().split())) for i in range(n-1)]
tree=[[]for _ in range(n)]
for a,b in l:
a-=1
b-=1
tree[a].append(b)
tree[b].append(b)
ans_num=0
for i in tree:
ans_num=max(ans_num,len(i))
dq=deque()
dq.append((0,0))
seen=set()
seen.add(0)
ans_dict={}
while dq:
x,color=dq.popleft()
i=0
for nx in tree[x]:
if nx in seen:
continue
i+=1
if i==color:
i+=1
seen.add(nx)
dq.append((nx,i)),
ans_dict[x+1,nx+1]=i
ans_dict[nx+1,x+1]=i
print(ans_num)
for i in l:
print((ans_dict[tuple(i)]))
|
from collections import deque
from sys import stdin
nii=lambda:list(map(int,stdin.readline().split()))
n=int(eval(input()))
l=[list(nii()) for i in range(n-1)]
tree=[[]for _ in range(n)]
for a,b in l:
a-=1
b-=1
tree[a].append(b)
tree[b].append(b)
ans_num=0
for i in tree:
ans_num=max(ans_num,len(i))
dq=deque()
dq.append((0,0))
seen=set()
seen.add(0)
ans_dict={}
while dq:
x,color=dq.pop()
i=0
for nx in tree[x]:
if nx in seen:
continue
i+=1
if i==color:
i+=1
seen.add(nx)
dq.append((nx,i)),
ans_dict[x+1,nx+1]=i
ans_dict[nx+1,x+1]=i
print(ans_num)
for i in l:
print((ans_dict[tuple(i)]))
|
p02850
|
n=int(eval(input()))
ab=[]
p=[-1 for i in range(n+1)]
edge=[[0 for i in range(n+1)] for j in range(n+1)]
G=[[] for i in range(n+1)]
used=[0 for i in range(n+1)]
for i in range(n-1):
ab.append(list(map(int,input().split())))
G[ab[i][0]].append(ab[i][1])
G[ab[i][1]].append(ab[i][0])
max_n=0
for i in range(n+1):
tmp=len(G[i])
if tmp>max_n:
max_n=tmp
top=i
queue=[]
queue.append(top)
while len(queue)>0:
num=queue.pop(0)
used[num]=1
color=1
for i in range(len(G[num])):
if used[G[num][i]]==0:
if p[num]==color:
color+=1
edge[num][G[num][i]]=color
edge[G[num][i]][num]=color
p[G[num][i]]=color
queue.append(G[num][i])
else:
edge[num][G[num][i]]=color
edge[G[num][i]][num]=color
p[G[num][i]]=color
queue.append(G[num][i])
color+=1
print(max_n)
for i in range(n-1):
print((edge[ab[i][0]][ab[i][1]]))
|
n=int(eval(input()))
ab=[]
p=[-1 for i in range(n+1)]
edge={}
G=[[] for i in range(n+1)]
used=[0 for i in range(n+1)]
for i in range(n-1):
ab.append(list(map(int,input().split())))
G[ab[i][0]].append(ab[i][1])
G[ab[i][1]].append(ab[i][0])
max_n=0
for i in range(n+1):
tmp=len(G[i])
if tmp>max_n:
max_n=tmp
top=i
queue=[]
queue.append(top)
while len(queue)>0:
num=queue.pop(0)
used[num]=1
color=1
for i in range(len(G[num])):
if used[G[num][i]]==0:
if p[num]==color:
color+=1
edge["{},{}".format(num,G[num][i])]=color
edge["{},{}".format(G[num][i],num)]=color
p[G[num][i]]=color
queue.append(G[num][i])
else:
edge["{},{}".format(num,G[num][i])]=color
edge["{},{}".format(G[num][i],num)]=color
p[G[num][i]]=color
queue.append(G[num][i])
color+=1
print(max_n)
for i in range(n-1):
print((edge["{},{}".format(ab[i][0],ab[i][1])]))
|
p02850
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.