s_id
stringlengths
10
10
p_id
stringlengths
6
6
u_id
stringlengths
10
10
date
stringlengths
10
10
language
stringclasses
1 value
original_language
stringclasses
11 values
filename_ext
stringclasses
1 value
status
stringclasses
1 value
cpu_time
stringlengths
1
5
memory
stringlengths
1
7
code_size
stringlengths
1
6
code
stringlengths
1
539k
s110089845
p01274
u509278866
1535515958
Python
Python3
py
Runtime Error
0
0
2325
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**13 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def main(): rr = [] def f(n): h = LI() hm = max(h) m = I() ms = [] ma = [] for _ in range(m): nama,mp,target,damege = LS() mp = int(mp) damege = int(damege) if damege == 0: continue if mp == 0: return 0 if target == 'All': ma.append((mp,damege)) else: ms.append((mp,damege)) ds = [inf] * (hm+1) ds[0] = 0 for mp, dm in ms: for i in range(dm,hm+1): if ds[i] > mp + ds[i-dm]: ds[i] = mp + ds[i-dm] t = min(ds[-dm+1:]) if ds[-1] > t + dm: ds[-1] = t + dm da = [inf] * (hm+1) da[0] = 0 for mp, dm in ma: for i in range(dm,hm+1): if da[i] > mp + da[i-dm]: da[i] = mp + da[i-dm] t = min(da[-dm+1:]) if da[-1] > t + dm: da[-1] = t + dm for i in range(hm-1,-1,-1): if ds[i] > ds[i+1]: ds[i] = ds[i+1] if da[i] > da[i+1]: da[i] = da[i+1] mm = r = inf for i in range(hm,-1,-1): if mm <= da[i]: continue mm = tp = da[i] for hi in h: if hi > i: tp += ds[hi-i] if r > tp: r = tp return r while True: n = I() if n == 0: break rr.append(f(n)) return '\n'.join(map(str,rr)) print(main())
s015821806
p01283
u633068244
1423406128
Python
Python
py
Runtime Error
19930
4448
627
import math def calcH(O): n = len(O) return sum(-O.count(x)*1.0/n*math.log(O.count(x)*1.0/n) for x in set(O)) M = 256 while 1: n = int(raw_input()) if n == 0: break I = map(int,raw_input().split()) H = 10**10 for S in range(16): for A in range(16): for C in range(16): O = [] R = S for i in range(n): R = (A*R+C)%M O.append((I[i]+R)%M) tH = calcH(O) if tH < H: H = tH ans = [S,A,C] print " ".join(map(str,ans))
s947855374
p01283
u633068244
1423406354
Python
Python
py
Runtime Error
19930
4448
623
import math def calcH(O): n = len(O) return sum(-O.count(x)*1.0/n*math.log(O.count(x)*1.0/n) for x in set(O)) M = 256 while 1: n = int(raw_input()) if n == 0: break I = map(int,raw_input().split()) H = 10**10 for S in range(16): for A in range(16): for C in range(16): O = [0]*n R = S for i in range(n): R = (A*R+C)%M O[i] = (I[i]+R)%M tH = calcH(O) if tH < H: H = tH ans = [S,A,C] print " ".join(map(str,ans))
s899995885
p01283
u633068244
1423582029
Python
Python
py
Runtime Error
19930
4452
623
import math def calcH(O): n = len(O) return sum(-O.count(x)*1.0/n*math.log(O.count(x)*1.0/n) for x in set(O)) M = 256 while 1: n = int(raw_input()) if n == 0: break I = map(int,raw_input().split()) H = 10**10 for S in range(16): for A in range(16): for C in range(16): O = [0]*n R = S for i in range(n): R = (A*R+C)%M O[i] = (I[i]+R)%M tH = calcH(O) if tH < H: H = tH ans = [S,A,C] print " ".join(map(str,ans))
s956835244
p01283
u633068244
1423683665
Python
Python
py
Runtime Error
19930
4444
576
import math M = 256 while 1: N = int(raw_input()) if N == 0: break H = 10**10 l = map(int,raw_input().split()) for S in range(16): for A in range(16): for C in range(16): R = S O = [0]*N for i in range(N): R = (A*R+C)%M O[i] = (l[i]+R)%M tmp = -sum(1.0*O.count(x)/N*math.log(1.0*O.count(x)/N) for x in set(O)) if tmp < H: H = tmp ans = [S,A,C] print " ".join(map(str,ans))
s917761598
p01283
u633068244
1423684110
Python
Python
py
Runtime Error
0
0
598
import math M = 256 while 1: N = int(raw_input()) if N == 0: break H = 1 l = map(int,raw_input().split()) for S in range(16): for A in range(16): for C in range(16): R = S O = [0]*N for i in range(N): R = (A*R+C)%M O[i] = (l[i]+R)%M X = [O.count(i) for i in O] tmp = -sum(float(x)/N*math.log(float(x)/N) for x in X) if tmp < H: H = tmp ans = [S,A,C] print " ".join(map(str,ans))
s698212365
p01283
u633068244
1423684194
Python
Python
py
Runtime Error
0
0
603
import math M = 256 while 1: N = int(raw_input()) if N == 0: break H = 1 l = map(int,raw_input().split()) for S in range(16): for A in range(16): for C in range(16): R = S O = [0]*N for i in range(N): R = (A*R+C)%M O[i] = (l[i]+R)%M X = [O.count(i) for i in set(O)] tmp = -sum(float(x)/N*math.log(float(x)/N) for x in X) if tmp < H: H = tmp ans = [S,A,C] print " ".join(map(str,ans))
s329585870
p01283
u633068244
1423684234
Python
Python
py
Runtime Error
19930
4452
606
import math M = 256 while 1: N = int(raw_input()) if N == 0: break H = 1e32 l = map(int,raw_input().split()) for S in range(16): for A in range(16): for C in range(16): R = S O = [0]*N for i in range(N): R = (A*R+C)%M O[i] = (l[i]+R)%M X = [O.count(i) for i in set(O)] tmp = -sum(float(x)/N*math.log(float(x)/N) for x in X) if tmp < H: H = tmp ans = [S,A,C] print " ".join(map(str,ans))
s460160136
p01283
u633068244
1423684399
Python
Python
py
Runtime Error
19930
4456
608
import math M = 256 while 1: N = int(raw_input()) if N == 0: break H = 1e10 l = map(int,raw_input().split()) for S in range(16): for A in range(16): for C in range(16): R = S O = [0]*N for i in range(N): R = (A*R+C)%M O[i] = (l[i]+R)%M X = [O.count(i) for i in set(O)] tmp = -sum(float(x)/N*math.log(float(x)/N,2) for x in X) if tmp < H: H = tmp ans = [S,A,C] print " ".join(map(str,ans))
s663478804
p01283
u506554532
1514525929
Python
Python3
py
Runtime Error
0
0
753
import math from collections import Counter M = 256 def entropy(ctr): ret = 0 for v in ctr.values(): ret -= (v / N) * math.log2(v / N) return ret while True: N = int(input()) if N == 0: break src = list(map(int,input().split())) ans_h = float('inf') ans_sac = None for s in range(16): for a in range(16): for c in range(16): ctr = Counter() r = s for i in src: r = (a*r + c) % M o = (i + r) % M ctr.update([o]) h = entropy(ctr) if h < ans_h: ans_h = h ans_sac = (s,a,c) print(' '.join(map(str,ans_sac)))
s207398163
p01283
u506554532
1514526354
Python
Python3
py
Runtime Error
0
0
850
import math from collections import Counter M = 256 def entropy(ctr): ret = 0 for v in ctr.values(): ret -= (v / N) * math.log2(v / N) return ret def solve(src): ans_h = float('inf') ans = None for s in range(16): for a in range(16): for c in range(16): ctr = Counter() r = s for i in src: r = (a*r + c) % M o = (i + r) % M ctr.update([o]) h = entropy(ctr) if h < ans_h: ans = (s,a,c) ans_h = h if ans_h == 0: return ans return ans while True: N = int(input()) if N == 0: break src = list(map(int,input().split())) print(' '.join(map(str,solve(src))))
s884760562
p01283
u506554532
1514527275
Python
Python3
py
Runtime Error
0
0
939
import math from collections import Counter M = 256 def entropy_if_smallest(ctr,bound): ret = 0 for v in ctr.values(): ret -= (v / N) * math.log2(v / N) if ret >= bound: return None return ret def solve(src): ans_h = float('inf') ans = None for s in range(16): for a in range(16): for c in range(16): ctr = Counter() r = s for i in src: r = (a*r + c) % M o = (i + r) % M ctr.update([o]) h = entropy_if_smallest(ctr,ans_h) if h is not None: ans = (s,a,c) ans_h = h if ans_h == 0: return ans return ans while True: N = int(input()) if N == 0: break src = list(map(int,input().split())) print(' '.join(map(str,solve(src))))
s768989767
p01283
u109084363
1364107899
Python
Python
py
Runtime Error
20000
5376
796
import sys import collections import math while True: N = int(raw_input()) if N == 0: break I = map(int, raw_input().split()) M = 256 result = [] for S in xrange(16): for A in xrange(16): for C in xrange(16): R = [S] for i in xrange(N): R.append((A * R[i] + C) % M) O = [] for i in xrange(N): O.append((I[i] + R[i + 1]) % M) count = collections.Counter() count.update(O) H = 0 for c in count.values(): H -= c / float(N) * math.log(c / float(N)) result.append((H, S, A, C)) result.sort() print ' '.join(map(str, result[0][1:]))
s097259227
p01283
u109084363
1364108098
Python
Python
py
Runtime Error
20000
5372
734
import sys import collections import math while True: N = int(raw_input()) if N == 0: break I = map(int, raw_input().split()) M = 256 result = [] for S in xrange(16): for A in xrange(16): for C in xrange(16): R = [S] for i in xrange(N): R.append((A * R[i] + C) % M) O = [] for i in xrange(N): O.append((I[i] + R[i + 1]) % M) count = collections.Counter() count.update(O) H = 0 for c in count.values(): H -= c / float(N) * math.log(c / float(N)) result.append((H, S, A, C))
s306908574
p01283
u109084363
1364108172
Python
Python
py
Runtime Error
20000
5376
595
import sys import collections import math while True: N = int(raw_input()) if N == 0: break I = map(int, raw_input().split()) M = 256 result = [] for S in xrange(16): for A in xrange(16): for C in xrange(16): R = [S] for i in xrange(N): R.append((A * R[i] + C) % M) O = [] for i in xrange(N): O.append((I[i] + R[i + 1]) % M) count = collections.Counter() count.update(O) H = 0 for c in count.values(): H -= c / float(N) * math.log(c / float(N)) result.append((H, S, A, C)) result.sort() print ' '.join(map(str, result[0][1:]))
s464343296
p01283
u109084363
1364108425
Python
Python
py
Runtime Error
20000
5360
770
import sys import collections import math while True: N = int(raw_input()) if N == 0: break I = map(int, raw_input().split()) M = 256 result = [] for S in xrange(16): for A in xrange(16): for C in xrange(16): R = [S] for i in xrange(N): R.append((A * R[i] + C) % M) count = [0 for i in xrange(M)] for i in xrange(N): count[(I[i] + R[i + 1]) % M] += 1 H = 0 for c in count: if c != 0: H -= c / float(N) * math.log(c / float(N)) result.append((H, S, A, C)) result.sort() print ' '.join(map(str, result[0][1:]))
s289214529
p01283
u104911888
1372862233
Python
Python
py
Runtime Error
0
0
705
import math def prodR(s,a,c,N): R=[s] for i in range(1,N+1): R.append((a*R[i-1]+c)%256) return R[1:] def entropy(R,I,N): O=[(I[i]+R[i])%256 for i in range(N)] L=[0]*256 for i in O: L[i]+=1 H=-sum(float(L[i])/N*math.log(float(L[i])/N,2) for i in range(256) if L[i]!\ =0) return H while True: N=input() if N==0:break I=map(int,raw_input().split()) maxInt=1e10 for s in range(16): for a in range(16): for c in range(16): R=prodR(s,a,c,N) H=entropy(R,I,N) if maxInt>H: maxInt=H ans=s,a,c print "%d %d %d"%(ans[0],ans[1],ans[2])
s522259215
p01288
u408260374
1519050377
Python
Python3
py
Runtime Error
0
0
1727
class UnionFindTree: """Disjoint-Set Data Structure Union-Find Tree complexity: init: O(n) find, unite, same: O(alpha(n)) used in SRM505 div.2 900, ATC001 A, DSL1A(AOJ) """ def __init__(self, n): self.par = list(range(n)) # parent self.rank = [0] * n # depth of tree def find(self, x): if self.par[x] == x: return x else: self.par[x] = self.find(self.par[x]) return self.par[x] def unite(self, x, y): x, y = self.find(x), self.find(y) if x == y: return if self.rank[x] < self.rank[y]: self.par[x] = y else: self.par[y] = x if self.rank[x] == self.rank[y]: self.rank[x] += 1 def same(self, x, y): return self.find(x) == self.find(y) while True: N, Q = map(int, input().split()) if N == Q == 0: break parents = [-1] + [int(input()) - 1 for _ in range(N - 1)] queries = [] marked = set() for _ in range(Q): k, v = input().split() v = int(v) - 1 queries.append((k, v)) if k == "M": marked.add(v) uf = UnionFindTree(N) for i in range(1, N): if i not in marked: p_root = uf.find(parents[i]) uf.par[i] = p_root uf.rank[p_root] = max(uf.rank[p_root], uf.par[i] + 1) ans = 0 for k, v in reversed(queries): if k == "Q": ans += uf.find(v) + 1 elif not uf.same(v, parents[v]): p_root = uf.find(parents[v]) uf.par[v] = p_root uf.rank[p_root] = max(uf.rank[p_root], uf.par[v] + 1) print(ans)
s062456607
p01288
u072053884
1519139511
Python
Python3
py
Runtime Error
0
0
763
def solve(): import sys file_input = sys.stdin def marked_ancestor(v): if v == ancestor[v]: return v else: ancestor[v] = marked_ancestor(parent[v]) return ancestor[v] def line_ans(line): v = int(line[2:]) if line[0] == 'M': ancestor[v] = v return 0 else: return marked_ancestor(v) while True: N, Q = map(int, file_input.readline().split()) if N == 0: break parent = [None, None] parent += [int(file_input.readline()) for _ in range(N - 1)] ancestor = [1] * (N + 1) ans = (line_ans(file_input.readline()) for _ in range(Q)) print(sum(ans)) solve()
s227298360
p01295
u615353970
1420714231
Python
Python
py
Runtime Error
0
0
351
# -*- coding: utf-8 -*- import sys import math while True: # a,b = map(int,raw_input().split(' ')) a = int(raw_input()) b = int(raw_input()) if (a == 0 and b == 0): sys.exit() res = "" for i in range(a,a+b): res += str(i); ans = "" for i in range(a,a+b): ans+=res[i-a] print ans
s740879705
p01295
u615353970
1420767582
Python
Python
py
Runtime Error
0
0
321
# -*- coding: utf-8 -*- import sys import math while True: a,b = map(int,raw_input().split(' ')) if (a == 0 and b == 0): sys.exit() res = "" for i in range(a,min(a+b,100/len(str(a)))): res += str(i); ans = "" for i in range(a,a+b): ans+=res[i-a] print ans
s663511304
p01296
u509278866
1531051555
Python
Python3
py
Runtime Error
0
0
2862
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def main(): rr = [] while True: n = I() if n == 0: break a = [LS() for _ in range(n)] d = collections.defaultdict(int) for i in range(n): x, y, di = a[i] x = int(x) y = int(y) d[(x,y)] = i + 1 if di == 'x': d[(x+1,y)] = i + 1 else: d[(x,y+1)] = i + 1 v = collections.defaultdict(bool) f = True for k in list(d.keys()): if v[k]: continue s1 = set() s2 = set() ns1 = set([k]) ns2 = set() while ns1: while ns1: ns1 -= s1 s1 |= ns1 for ak in list(ns1): for di,dj in dd: nk = (ak[0]+di, ak[1]+dj) if d[nk] == 0: continue if d[nk] == d[ak]: ns2.add(nk) else: ns1.add(nk) if s1 & s2: f = False break while ns2: ns2 -= s2 s2 |= ns2 for ak in list(ns2): for di,dj in dd: nk = (ak[0]+di, ak[1]+dj) if d[nk] == 0: continue if d[nk] == d[ak]: ns1.add(nk) else: ns2.add(nk) if s1 & s2: f = False break if s1 & s2: f = False break if not f: break for c in s1: v[c] = 1 for c in s2: v[c] = 2 if f: rr.append('Yes') else: rr.append('No') return '\n'.join(map(str,rr)) print(main())
s343941255
p01296
u509278866
1531280586
Python
Python3
py
Runtime Error
0
0
2832
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def main(): rr = [] while True: n = I() if n == 0: break a = [LS() for _ in range(n)] d = collections.defaultdict(int) ed = collections.defaultdict(lambda: None) for i in range(n): x, y, di = a[i] x = int(x) y = int(y) d[(x,y)] = i + 1 if di == 'x': d[(x+1,y)] = i + 1 ed[(x,y)] = (x+1,y) ed[(x+1,y)] = (x,y) else: d[(x,y+1)] = i + 1 ed[(x,y)] = (x,y+1) ed[(x,y+1)] = (x,y) ee = collections.defaultdict(set) dka = list(d.keys()) for x,y in list(d.keys()): dt = d[(x,y)] for di,dj in dd: ni = x + di nj = y + dj if d[(ni,nj)] > 0 and d[(ni,nj)] != dt: ee[(x,y)].add((ni,nj)) v = collections.defaultdict(bool) f = True for k in dka: if v[k]: continue s1 = set() s2 = set() ns1 = set([k]) ns2 = set() while ns1: na = list(ns1) s1 |= ns1 ns1 = set() for k in na: ns1 |= ee[k] ns2.add(ed[k]) ns2 -= s2 while ns2: na = list(ns2) s2 |= ns2 ns2 = set() for k in na: ns2 |= ee[k] ns1.add(ed[k]) ns2 -= s2 ns1 -= s1 if s1 & s2: f = False break # print('k', k) # print('s1', s1) # print('s2', s2) if f: for k in s1: v[k] = 1 for k in s2: v[k] = 1 else: break if f: rr.append('Yes') else: rr.append('No') return '\n'.join(map(str,rr)) print(main())
s640568490
p01298
u947762778
1509275494
Python
Python3
py
Runtime Error
0
0
956
import def check(h, slist, l): last = l t = 0 for s in slist: last += (s[0] - t) * h last = min(last, l) last -= (s[1] - s[0]) * (s[2]-h) t = s[1] if last < 0: return False last = min(last, l) d1last = last t -= 86400 for s in slist: last += (s[0] - t) * h last = min(last, l) last -= (s[1] - s[0]) * (s[2]-h) t = s[1] if last < 0: return False last = min(last, l) if last < d1last: return False return True while True: N, L = map(int, input().split()) if N == 0 and L == 0: break s = [list(map(int, input().split())) for _ in range(N)] left = 0 right = 1e6 mid = (right + left)/2 while (right - left) > 1e-7: mid = (right + left)/2 if check(mid, s, L): right = mid else: left = mid print(mid)
s627931259
p01300
u798803522
1471594019
Python
Python3
py
Runtime Error
40000
8744
565
targ = input() while True: ans = 0 length = len(targ) for o in range(length-1): disp = [0 for n in range(length)] for i in range(o+1,length): if targ[o] == '0': disp[i] = ans elif i == length - 1: disp[i] = int((int(targ[o] + targ[o+1:i+1])) % 11 == 0) + disp[i-1] + ans else: disp[i] = int((int(targ[o] + targ[o+1:i+1])) % 11 == 0) + disp[i-1] ans = disp[-1] del disp print(ans) targ = input() if targ == '0': break
s302135327
p01300
u467175809
1529581102
Python
Python
py
Runtime Error
0
0
447
#!/usr/bin/env python from collections import deque import itertools as it import sys sys.setrecursionlimit(1000000) while True: S = reversed(raw_input()) if S == '0': break m = [1] + [0 for i in range(10)] diff = ans = 0 even = 1 for c in S: num = int(c) diff += num * even diff %= 11 if num != 0: ans += m[diff] m[diff] += 1 even *= -1 print ans
s618666116
p01300
u467175809
1529582315
Python
Python
py
Runtime Error
0
0
218
#!/usr/bin/env python from collections import deque import itertools as it import sys sys.setrecursionlimit(1000000) def fact(num): if num <= 1: return 1 return fact(num - 1) + 1 print fact(200000)
s731195987
p01300
u633068244
1398661062
Python
Python
py
Runtime Error
39860
9376
188
while 1: n = raw_input() if n == "0": break c = 0 for i in range(2,len(n) + 1): for sp in range(len(n) - i + 1): if n[sp] != "0" and int(n[sp:sp + i])%11 == 0: c += 1 print c
s891105972
p01300
u633068244
1398661625
Python
Python
py
Runtime Error
39860
9356
295
def isEle(n): s = 0 for i in range(len(n)): s += (-1)**i * int(n[i]) return True if s%11 == 0 else False while 1: n = raw_input() if n == "0": break c = 0 for i in range(2,len(n) + 1): for sp in range(len(n) - i + 1): if n[sp] != "0" and isEle(n[sp:sp + i]): c += 1 print c
s182667109
p01300
u633068244
1398662613
Python
Python
py
Runtime Error
39860
10604
325
while 1: n = raw_input() if n == "0": break size = len(n) s = 0 diff = [0]*(size + 1) for i in range(size): s += (-1)**i * int(n[i]) diff[i + 1] = s c = 0 for sp in range(size - 1): if n[sp] == "0": continue for l in range(2,size - sp + 1): if abs((diff[sp + l] - diff[sp])%11) == 0: c += 1 print c
s685935034
p01300
u633068244
1398662696
Python
Python
py
Runtime Error
39860
10620
363
while 1: try: n = raw_input() if n == "0": break size = len(n) s = 0 diff = [0]*(size + 1) for i in range(size): s += (-1)**i * int(n[i]) diff[i + 1] = s c = 0 for sp in range(size - 1): if n[sp] == "0": continue for l in range(2,size - sp + 1): if abs((diff[sp + l] - diff[sp])%11) == 0: c += 1 print c except: pass
s865217099
p01304
u798803522
1470328641
Python
Python3
py
Runtime Error
0
0
1495
trial = int(input()) for t in range(trial): targ = [int(n) for n in f.readline().split(' ')] root = [[0 for n in range(targ[0] + 1)] for m in range(targ[1] + 1)] matanum = int(f.readline()) for m in range(matanum): matax,matay,secx,secy = (int(n) for n in f.readline().split(' ')) if matax == secx: root[max(matay,secy)][matax] = 'y' else: root[matay][max(secx,matax)] = 'x' for yaxis in range(targ[1] + 1): for xaxis in range(targ[0] + 1): if xaxis == 0: if root[yaxis][xaxis] == 'y': root[yaxis][xaxis] = 0 else: if yaxis == 0: root[0][0] = 1 else: root[yaxis][xaxis] = root[yaxis- 1][xaxis] elif yaxis == 0: if root[yaxis][xaxis] == 'x': root[yaxis][xaxis] = 0 else: root[yaxis][xaxis] = root[yaxis][xaxis-1] else: if root[yaxis][xaxis] == 'y': root[yaxis][xaxis] = root[yaxis][xaxis - 1] elif root[yaxis][xaxis] == 'x': root[yaxis][xaxis] = root[yaxis - 1][xaxis] else: root[yaxis][xaxis] = root[yaxis - 1][xaxis] + root[yaxis][xaxis - 1] if root[targ[1]][targ[0]] == 0: print("Miserable Hokusai!") else: print(root[targ[1]][targ[0]])
s097846838
p01305
u724548524
1528800621
Python
Python3
py
Runtime Error
0
0
494
import itertools from decimal import Decimal, ROUND_HALF_UP for _ in range(int(input())): a, b = tuple(map(int, input().split())), tuple(map(int, input().split())) c = 0 for i in itertools.permutations(b): s = 0 for j in range(9):s += (a[j] + i[j]) * (a[j] > i[j]) if s > 85:c += 1 c /= 362880 print("{} {}".format(Decimal(c).quantize(Decimal('0.00001'), rounding=ROUND_HALF_UP), Decimal(1 - c).quantize(Decimal('0.00001'), rounding=ROUND_HALF_UP)))
s509432950
p01306
u724548524
1528858866
Python
Python3
py
Runtime Error
0
0
836
m = {"yotta":24, "zetta":21, "exa":18, "peta":15, "tera":12, "giga":9, "mega":6, "kilo":3, "hecto":2, "deca":1, "deci":-1, "centi":-2, "milli":-3, "micro":-6, "nano":-9, "pico":-12, "femto":-15, "ato":-18, "zepto":-21, "yocto":-24} for _ in range(int(input())): v, *b = input().split() if len(b) == 2: k, b = b[0], b[1] a = m[k] else:b = b[0];a = 0 s = 0 for i in range(len(v)): if v[i] in "123456789": s = len(v) - i if "." in v[i + 1:]:s -= 1 break x = float(v) while x < 1: x *= 10 a -= 1 while x >= 10: x /= 10 a += 1 while 1: if v[0] in "123456789": if len(v) != 1: v = v[0] + "." + v[1:] if "." in v[2:]: a = 2 + v[2:].index(".")
s738681870
p01308
u352394527
1545418277
Python
Python3
py
Runtime Error
0
0
790
dic = {"C":0, "C#":1, "D":2, "D#":3, "E":4, "F":5, "F#":6, "G":7, "G#":8, "A":9, "A#":10, "B":11} t = int(input()) for _ in range(t): n, m = map(int, input().split()) t_lst = [-100] + list(map(lambda x:dic[x],input().split())) s_lst = list(map(lambda x:dic[x],input().split())) s_lst.reverse() def search(t_index, s_index): if s_index == m: return t_index == 0 if t_index <= 0 or t_index > n: return False base = t_lst[t_index] proc = s_lst[s_index] diff = (proc - base) % 12 if diff == 1 and search(t_index - 2, s_index + 1) or\ diff == 0 and search(t_index - 1, s_index + 1) or\ diff == 11 and search(t_index + 1, s_index + 1): return True return False print("Yes" if search(n, 0) or search(n - 1, 0) else "No")
s274283675
p01308
u266872031
1435980102
Python
Python
py
Runtime Error
19920
8248
1295
def notetonum(notestr): numlist=[] nn=["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"] for c in notestr: numlist.append(nn.index(c)) return numlist Q=int(raw_input()) for q in range(Q): [n,m]=[int(x) for x in raw_input().split()] stairlist=notetonum(raw_input().split()) songlist=notetonum(raw_input().split()) #dp=[[0 for x in range(n)] for y in range(m)] stateold=[0 for x in range(n)] statenew=[0 for x in range(n)] isoneold=[] isonenew=[] if songlist[0]==stairlist[0]: stateold[0]=1 isoneold.append(0) if songlist[0]==(stairlist[1]+1)%12: statenew[1]=1 isoneold.append(1) for i in range(m-1): isonenew=[] for j in isoneold: if j<n-1 and songlist[i+1]==stairlist[j+1]: statenew[j+1]=1 isonenew.append(j+1) if j<n-2 and songlist[i+1]==(stairlist[j+2]+1)%12: statenew[j+2]=1 isonenew.append(j+2) if j>0 and songlist[i+1]==(stairlist[j-1]-1)%12: statenew[j-1]=1 isonenew.append(j-1) stateold=statenew[:] isoneold=set(isonenew) if statenew[n-1] or statenew[n-2]: print "YES" else: print "NO"
s578138117
p01314
u352394527
1531252104
Python
Python3
py
Runtime Error
0
0
401
#include <cstdio> int main(void){ int n, i, j, cnt, tmp; while(scanf("%d", &n)){ if(n == 0){ break; } cnt = 0; for(i=1; i<n/2+1; i++){ for(j=i; j<n; j++){ tmp = (i + j) * (j - i + 1) / 2; if(tmp == n){ cnt++; break; } if(tmp > n){ break; } } } printf("%d\n", cnt); } return 0; }
s666490564
p01314
u509775126
1529400222
Python
Python3
py
Runtime Error
0
0
210
while True: n = int(input()) if n == 0: break c = 0 for i in range(2, 1001): for a in range(1, 1001): if i*a+(0.5*(i-1)*i) == n: c += 1 print(c)
s449139744
p01314
u509775126
1529400832
Python
Python3
py
Runtime Error
0
0
249
while True: try: n = int(input()) except: break if n == 0: break c = 0 for i in range(2, 1001): for a in range(1, 1001): if i*a+(0.5*(i-1)*i) == n: c += 1 print(c)
s914879601
p01314
u509775126
1529401353
Python
Python3
py
Runtime Error
0
0
260
while True: try: n = int(input()) except ValueError: break if n == 0: break c = 0 for i in range(2, 1001): for a in range(1, 1001): if i*a+(0.5*(i-1)*i) == n: c += 1 print(c)
s731952836
p01314
u136916346
1529981572
Python
Python3
py
Runtime Error
0
0
219
while 1: n=int(input()) if not n:break t=0 cnt=0 for i in range(2,n+1): o=n//i t+=(i-1) p=n-(i*o-t) if o+p//i+1-i<1:break if p%i==0:cnt+=1 print cnt
s965537061
p01315
u824193546
1503320935
Python
Python3
py
Runtime Error
0
0
1024
import numpy as np import sys def firster(former, latter): com = [str(former), str(latter)] com.sort() #print(former) #print(latter) #print(com) if com[0] == former: return 1 return 0 def one_turn(N): effs = [] #name effs_val = {} #value for i in range(N): data = input().split() period = 0 name = data[0] p = int(data[1]) period = int(data[2])+int(data[3])+int(data[4])+int(data[5])+int(data[6]) f = int(data[7]) s = int(data[8]) m = int(data[9]) eff = (m*f*s-p)/(period+(m-1)*(int(data[5])+int(data[6]))) effs_val[name] = eff if i == 0: effs.insert(0,name) continue for k in range(i): if effs_val[effs[k]] < eff: effs.insert(k,name) break elif (effs_val[effs[k]] == eff and firster(name,effs[k])): effs.insert(k,name) break elif k == i-1: effs.append(name) #print(effs) for l in range(N): print(effs[l]) #print(effs_val[effs[l]]) N = int(sys.stdin.readline()) while N > 0: one_turn(N) print('#') N = int(sys.stdin.readline()) print('#')
s898835776
p01316
u731235119
1423240200
Python
Python
py
Runtime Error
19930
5088
498
while 1: N, M = map(int,raw_input().split(" ")) if M == N == 0 : break y0 = 128 C = [int(raw_input()) for _ in range(M)] x = [int(raw_input()) for _ in range(N)] dp = {y0:0} for i in range(N): tmpdp = dict() for k,v in dp.iteritems(): for c in C: if k + c > 255: y = 255 elif k + c < 0: y = 0 else : y = k + c sub_sq = (x[i]-y)**2 if not(y in tmpdp and v + sub_sq >= tmpdp[y]): tmpdp[y] = v + sub_sq dp = tmpdp print sorted(dp.values())[0]
s138518044
p01316
u731235119
1423289494
Python
Python
py
Runtime Error
19920
95072
554
INF = float("inf") while 1: N, M = map(int,raw_input().split(" ")) if M == N == 0 : break y0 = 128 C = [int(raw_input()) for _ in range(M)] x = [int(raw_input()) for _ in range(N)] dp = [[INF for _ in range(256)] for _ in range(N+1)] dp[0][128] = 0 for i in range(N): for k in range(256): v = dp[i][k] if v == INF : continue for c in C: if k + c > 255: y = 255 elif k + c < 0: y = 0 else : y = k + c sub_sq = (x[i]-y)**2 if v + sub_sq < dp[i+1][y]: dp[i+1][y] = v + sub_sq print sorted(dp[N])[0]
s455659395
p01316
u509278866
1528443190
Python
Python3
py
Runtime Error
0
0
1207
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 998244353 def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def main(): rr = [] while True: n,m = LI() if n == 0 and m == 0: break cs = [I() for _ in range(m)] xs = [I() for _ in range(n)] d = collections.defaultdict(int) d[128] = 0 for x in xs: n = collections.defaultdict(lambda: inf) for k,v in d.items(): for c in cs: nk = max(min(k+c,255),0) if n[nk] > v + (x-nk) ** 2: n[nk] = v + (x-nk) ** 2 d = n rr.append(min(d.values())) return '\n'.join(map(str, rr)) print(main())
s294437711
p01316
u855199458
1530192706
Python
Python3
py
Runtime Error
0
0
698
# -*- coding: utf-8 -*- def inpl(): return list(map(int, input().split())) def main(): N, M = inpl() INF = 10**9 while N: C = [int(input()) for _ in range(M)] X = [int(input()) for _ in range(N)] Y = [[INF]*256 for i in range(N+1)] for i in range(256): Y[0][i] = 0 for i, x in enumerate(X): for j in range(256): if Y[i][j] == INF: continue for c in C: k = min(255, max(0, j+c)) Y[i+1][k] = min(Y[i+1][k], Y[i][j] + (x - k)**2) print(min(Y[-1])) N, M = inpl() if __name__ == "__main__": main()
s358240800
p01316
u779627195
1354704947
Python
Python
py
Runtime Error
19930
73220
1066
import sys R = lambda:map(int,raw_input().split()) yNum = 256 MAX_VAL = 10**6 while 1: n,m = R() if not (n or m): break c = [] x = [128] for i in xrange(m): c.append(int(raw_input())) for i in xrange(n): x.append(int(raw_input())) ## sub = [[0 for j in xrange(yNum)] for i in xrange(n+1)] ## for i in xrange(1,n+1): ## for j in xrange(yNum): ## sub[i][j] = (j-x[i])**2 values = [[MAX_VAL for j in xrange(yNum)] for i in xrange(n+1)] values[0][128] = 0 for i in xrange(n): for j in xrange(yNum): for k in xrange(m): if 0 <= j+c[k] <= 255: values[i+1][j+c[k]] = min(values[i+1][j+c[k]], values[i][j]+(j+c[k]-x[i+1])**2) elif 0 > j+c[k]: values[i+1][0] = min(values[i+1][0], values[i][j]+(0-x[i+1])**2) else: values[i+1][255] = min(values[i+1][255], values[i][j]+(255-x[i+1])**2) #if values[i][j] < 10**5: print values[i][j] print min(values[n])
s603762297
p01317
u266872031
1428164242
Python
Python
py
Runtime Error
19920
6720
2374
while(1): [N,M]=[int(x) for x in raw_input().split()] if N==0: break else: landr=[[99999999 for x in range(N+1)] for y in range(N+1)] sear=[[99999999 for x in range(N+1)] for y in range(N+1)] islandname1=[i for i in range(N+1)] #ex.[0,0,2,3,3] islandname2=[[i] for i in range(N+1)] #ex.[[0,1],[],[2],[3,4],[]] for i in range(1,N+1): landr[i][i]=0 sear[i][i]=0 for i in range(M): dd=raw_input().split() [a,b]=sorted([int(x) for x in dd[:2]]) d=int(dd[2]) t=dd[3] if t=='S': if sear[a][b]>d: sear[a][b]=d sear[b][a]=d else: if landr[a][b]>d: landr[a][b]=d landr[b][a]=d #check if same island if islandname1[a]!=islandname1[b]: #a<b islandnameold=islandname1[b] for port in islandname2[islandname1[b]]: islandname1[port]=islandname1[a] islandname2[islandname1[a]].extend(islandname2[islandnameold]) #floyd algorhythm for k in range(1,N+1): for i in range(1,N+1): for j in range(i,N+1): if landr[i][j]>landr[i][k]+landr[k][j]: landr[i][j]=landr[i][k]+landr[k][j] landr[j][i]=landr[i][k]+landr[k][j] if sear[i][j]>sear[i][k]+sear[k][j]: sear[i][j]=sear[i][k]+sear[k][j] sear[j][i]=sear[i][k]+sear[k][j] R=int(raw_input()) dest=[int(x) for x in raw_input().split()] loc=dest[0] portold=[[dest[0],0]] for i in range(R-1): portnew=[] newloc=dest[i+1] newport_temp=islandname2[islandname1[newloc]] for np in newport_temp: mintime=999999999 for opc in portold: op=opc[0] opt=opc[1] mintime=min(mintime,opt+landr[loc][op]+sear[op][np]+landr[np][newloc]) portnew.append([np,mintime]) portold=portnew loc=dest[i+1] i=i+1 ans=sorted(portold,key=lambda x:x[1]) print ans[0][1]
s262743167
p01317
u266872031
1428168679
Python
Python
py
Runtime Error
19920
6732
2473
while(1): [N,M]=[int(x) for x in raw_input().split()] if N==0: break else: landr=[[99999999 for x in range(N+1)] for y in range(N+1)] sear=[[99999999 for x in range(N+1)] for y in range(N+1)] islandname1=[i for i in range(N+1)] #ex.[0,0,2,3,3] islandname2=[[i] for i in range(N+1)] #ex.[[0,1],[],[2],[3,4],[]] for i in range(1,N+1): landr[i][i]=0 sear[i][i]=0 for i in range(M): dd=raw_input().split() [a,b]=sorted([int(x) for x in dd[:2]]) d=int(dd[2]) t=dd[3] if t=='S': if sear[a][b]>d: sear[a][b]=d sear[b][a]=d else: if landr[a][b]>d: landr[a][b]=d landr[b][a]=d #check if same island if islandname1[a]!=islandname1[b]: #a<b islandnameold=islandname1[b] for port in islandname2[islandname1[b]]: islandname1[port]=islandname1[a] islandname2[islandname1[a]].extend(islandname2[islandnameold]) #floyd algorhythm for k in range(1,N+1): for i in range(1,N+1): for j in range(i,N+1): if landr[i][j]>landr[i][k]+landr[k][j]: landr[i][j]=landr[i][k]+landr[k][j] landr[j][i]=landr[i][k]+landr[k][j] if sear[i][j]>sear[i][k]+sear[k][j]: sear[i][j]=sear[i][k]+sear[k][j] sear[j][i]=sear[i][k]+sear[k][j] R=int(raw_input()) dest=[int(x) for x in raw_input().split()] loc=dest[0] portold=[[dest[0],0]] for i in range(R-1): portnew=[] newloc=dest[i+1] newport_temp=islandname2[islandname1[newloc]] for np in newport_temp: mintime=999999999 for opc in portold: op=opc[0] opt=opc[1] mintime=min(mintime,opt+landr[loc][op]+sear[op][np]+landr[np][newloc]) if op==np: mintime=min(mintime,opt+landr[loc][newloc]) portnew.append([np,mintime]) portold=portnew loc=dest[i+1] i=i+1 ans=sorted(portold,key=lambda x:x[1]) print ans[0][1]
s080762166
p01317
u469154576
1501691224
Python
Python3
py
Runtime Error
40000
10776
1717
from queue import PriorityQueue inf = 10**9 while True: n, m = map(int, input().split()) if n == 0: break edgel = [[] for i in range(n)] edges = [[] for i in range(n)] for i in range(m): x, y, t, sl = input().split() x = int(x) - 1 y = int(y) - 1 t = int(t) if sl == 'L': edgel[x].append([y, t]) edgel[y].append([x, t]) else: edges[x].append([y, t]) edges[y].append([x, t]) r = int(input()) z = list(map(int, input().split())) for i in range(r): z[i] -= 1 dp = [[[inf] * n for i in range(n)] for j in range(r)] dp[0][z[0]][z[0]] = 0 for i in range(1, r): pq = PriorityQueue() for j in range(n): if dp[i - 1][z[i - 1]][j] != inf: pq.put([-dp[i - 1][z[i - 1]][j], z[i - 1], j]) dp[i][z[i - 1]][j] = dp[i - 1][z[i - 1]][j] while not pq.empty(): d, pos, f = pq.get() d *= -1 if d > dp[i][pos][f]: continue for j in range(len(edgel[pos])): to, add = edgel[pos][j] if dp[i][to][f] > dp[i][pos][f] + add: dp[i][to][f] = dp[i][pos][f] + add pq.put([-dp[i][to][f], to, f]) if pos == f: for j in range(len(edges[pos])): to, add = edges[pos][j] if dp[i][to][to] > dp[i][pos][f] + add: dp[i][to][to] = dp[i][pos][f] + add pq.put([-dp[i][to][to], to, to]) ret = inf for i in range(n): ret = min(ret, dp[r - 1][z[r - 1]][i]) print(ret)
s893255020
p01317
u469154576
1502476883
Python
Python3
py
Runtime Error
0
0
2088
inf = 10**9 dp = [[inf] * 200 for i in range(200)] ldist = [[inf] * 200 for i in range(200)] sdist = [[inf] * 200 for i in range(200)] while True: n, m = map(int, input().split()) if n == 0: break for i in range(n): for j in range(n): if i == j: ldist[i][j] = sdist[i][j] = 0 else: ldist[i][j] = sdist[i][j] = inf for i in range(m): x, y, t, sl = input().split() x = int(x) - 1 y = int(y) - 1 t = int(t) if sl == 'L': ldist[x][y] = ldist[y][x] = min(ldist[x][y], t) elif sl == 'S': sdist[x][y] = sdist[y][x] = min(sdist[x][y], t) for k in range(n): for i in range(n): for j in range(n): ldist[i][j] = min(ldist[i][j], ldist[i][k] + ldist[k][j]) sdist[i][j] = min(sdist[i][j], sdist[i][k] + sdist[k][j]) for i in range(n): for j in range(n): dp[i][j] = 0 if i == j else inf for i in range(n): for j in range(n): if i != j: dp[i][j] = min(dp[i][j], dp[j][j] + ldist[j][i]) r = int(input()) z = list(map(int, input().split())) for i in range(n): z[i] -= 1 for i in range(n): if i != z[0]: for j in range(n): dp[i][j] = inf for i in range(1, r): for j in range(n): for k in range(n): if j != z[i - 1]: dp[j][k] = min(dp[j][k], dp[z[i - 1]] [k] + ldist[z[i - 1]][j]) for j in range(n): for k in range(n): dp[k][k] = min(dp[k][k], dp[j][j] + sdist[j][k]) for j in range(n): for k in range(n): if j != k: dp[j][k] = min(dp[j][k], dp[k][k] + ldist[k][j]) for j in range(n): if j != z[i]: for k in range(n): dp[j][k] = inf ret = inf for i in range(n): ret = min(ret, dp[z[r - 1]][i]) print(ret)
s542728166
p01317
u469154576
1502477069
Python
Python3
py
Runtime Error
0
0
2052
inf = 10**9 dp = [[inf] * 200 for i in range(200)] ldist = [[inf] * 200 for i in range(200)] sdist = [[inf] * 200 for i in range(200)] while True: n, m = map(int, input().split()) if n == 0: break for i in range(n): for j in range(n): if i == j: ldist[i][j] = sdist[i][j] = 0 else: ldist[i][j] = sdist[i][j] = inf for i in range(m): x, y, t, sl = input().split() x = int(x) - 1 y = int(y) - 1 t = int(t) if sl == 'L': ldist[x][y] = ldist[y][x] = min(ldist[x][y], t) elif sl == 'S': sdist[x][y] = sdist[y][x] = min(sdist[x][y], t) for k in range(n): for i in range(n): for j in range(n): ldist[i][j] = min(ldist[i][j], ldist[i][k] + ldist[k][j]) sdist[i][j] = min(sdist[i][j], sdist[i][k] + sdist[k][j]) for i in range(n): for j in range(n): dp[i][j] = 0 if i == j else inf for i in range(n): for j in range(n): if i != j: dp[i][j] = min(dp[i][j], dp[j][j] + ldist[j][i]) r = int(input()) z = list(map(int, input().split())) for i in range(n): z[i] -= 1 for i in range(n): if i != z[0]: for j in range(n): dp[i][j] = inf for i in range(1, r): for j in range(n): for k in range(n): if j != z[i - 1]: dp[j][k] = min(dp[j][k], dp[z[i - 1]][k] + ldist[z[i - 1]][j]) for j in range(n): for k in range(n): dp[k][k] = min(dp[k][k], dp[j][j] + sdist[j][k]) for j in range(n): for k in range(n): if j != k: dp[j][k] = min(dp[j][k], dp[k][k] + ldist[k][j]) for j in range(n): if j != z[i]: for k in range(n): dp[j][k] = inf ret = inf for i in range(n): ret = min(ret, dp[z[r - 1]][i]) print(ret)
s892620062
p01317
u469154576
1502477226
Python
Python3
py
Runtime Error
0
0
2088
inf = 10**9 dp = [[inf] * 210 for i in range(210)] ldist = [[inf] * 210 for i in range(210)] sdist = [[inf] * 210 for i in range(210)] while True: n, m = map(int, input().split()) if n == 0: break for i in range(n): for j in range(n): if i == j: ldist[i][j] = sdist[i][j] = 0 else: ldist[i][j] = sdist[i][j] = inf for i in range(m): x, y, t, sl = input().split() x = int(x) - 1 y = int(y) - 1 t = int(t) if sl == 'L': ldist[x][y] = ldist[y][x] = min(ldist[x][y], t) elif sl == 'S': sdist[x][y] = sdist[y][x] = min(sdist[x][y], t) for k in range(n): for i in range(n): for j in range(n): ldist[i][j] = min(ldist[i][j], ldist[i][k] + ldist[k][j]) sdist[i][j] = min(sdist[i][j], sdist[i][k] + sdist[k][j]) for i in range(n): for j in range(n): dp[i][j] = 0 if i == j else inf for i in range(n): for j in range(n): if i != j: dp[i][j] = min(dp[i][j], dp[j][j] + ldist[j][i]) r = int(input()) z = list(map(int, input().split())) for i in range(n): z[i] -= 1 for i in range(n): if i != z[0]: for j in range(n): dp[i][j] = inf for i in range(1, r): for j in range(n): for k in range(n): if j != z[i - 1]: dp[j][k] = min(dp[j][k], dp[z[i - 1]] [k] + ldist[z[i - 1]][j]) for j in range(n): for k in range(n): dp[k][k] = min(dp[k][k], dp[j][j] + sdist[j][k]) for j in range(n): for k in range(n): if j != k: dp[j][k] = min(dp[j][k], dp[k][k] + ldist[k][j]) for j in range(n): if j != z[i]: for k in range(n): dp[j][k] = inf ret = inf for i in range(n): ret = min(ret, dp[z[r - 1]][i]) print(ret)
s937344939
p01317
u469154576
1502502653
Python
Python3
py
Runtime Error
40000
10720
2088
inf = 10**9 dp = [[inf] * 210 for i in range(210)] ldist = [[inf] * 210 for i in range(210)] sdist = [[inf] * 210 for i in range(210)] while True: n, m = map(int, input().split()) if n == 0: break for i in range(n): for j in range(n): if i == j: ldist[i][j] = sdist[i][j] = 0 else: ldist[i][j] = sdist[i][j] = inf for i in range(m): x, y, t, sl = input().split() x = int(x) - 1 y = int(y) - 1 t = int(t) if sl == 'L': ldist[x][y] = ldist[y][x] = min(ldist[x][y], t) elif sl == 'S': sdist[x][y] = sdist[y][x] = min(sdist[x][y], t) for k in range(n): for i in range(n): for j in range(n): ldist[i][j] = min(ldist[i][j], ldist[i][k] + ldist[k][j]) sdist[i][j] = min(sdist[i][j], sdist[i][k] + sdist[k][j]) for i in range(n): for j in range(n): dp[i][j] = 0 if i == j else inf for i in range(n): for j in range(n): if i != j: dp[i][j] = min(dp[i][j], dp[j][j] + ldist[j][i]) r = int(input()) z = list(map(int, input().split())) for i in range(r): z[i] -= 1 for i in range(n): if i != z[0]: for j in range(n): dp[i][j] = inf for i in range(1, r): for j in range(n): for k in range(n): if j != z[i - 1]: dp[j][k] = min(dp[j][k], dp[z[i - 1]] [k] + ldist[z[i - 1]][j]) for j in range(n): for k in range(n): dp[k][k] = min(dp[k][k], dp[j][j] + sdist[j][k]) for j in range(n): for k in range(n): if j != k: dp[j][k] = min(dp[j][k], dp[k][k] + ldist[k][j]) for j in range(n): if j != z[i]: for k in range(n): dp[j][k] = inf ret = inf for i in range(n): ret = min(ret, dp[z[r - 1]][i]) print(ret)
s738947288
p01317
u469154576
1502506490
Python
Python3
py
Runtime Error
40000
10780
1641
inf = 10**9 dp = [[inf] * 210 for i in range(210)] ldist = [[inf] * 210 for i in range(210)] sdist = [[inf] * 210 for i in range(210)] while True: n, m = map(int, input().split()) if n == 0: break for i in range(n): for j in range(n): if i == j: ldist[i][j] = sdist[i][j] = 0 else: ldist[i][j] = sdist[i][j] = inf for i in range(m): x, y, t, sl = input().split() x = int(x) - 1 y = int(y) - 1 t = int(t) if sl == 'L': ldist[x][y] = ldist[y][x] = min(ldist[x][y], t) elif sl == 'S': sdist[x][y] = sdist[y][x] = min(sdist[x][y], t) for k in range(n): for i in range(n): for j in range(n): ldist[i][j] = min(ldist[i][j], ldist[i][k] + ldist[k][j]) sdist[i][j] = min(sdist[i][j], sdist[i][k] + sdist[k][j]) for i in range(n): for j in range(n): dp[i][j] = 0 if i == j else inf for i in range(n): for j in range(n): if i != j: dp[i][j] = min(dp[i][j], dp[j][j] + ldist[j][i]) r = int(input()) z = list(map(int, input().split())) for i in range(r): z[i] -= 1 for i in range(n): if i != z[0]: for j in range(n): dp[i][j] = inf for i in range(1, r): for j in range(n): for k in range(n): if j != z[i - 1]: dp[j][k] = min(dp[j][k], dp[z[i - 1]] [k] + ldist[z[i - 1]][j]) for j in range(n): for k in range(n): dp[k][k] = min(dp[k][k], dp[j][j] + sdist[j][k]) for j in range(n): for k in range(n): if j != k: dp[j][k] = min(dp[j][k], dp[k][k] + ldist[k][j]) for j in range(n): if j != z[i]: for k in range(n): dp[j][k] = inf ret = inf for i in range(n): ret = min(ret, dp[z[r - 1]][i]) print(ret)
s245785168
p01317
u266872031
1530190602
Python
Python3
py
Runtime Error
0
0
1318
# -*- coding: utf-8 -*- def wf(A,N): for k in range(1,N+1): for i in range(1,N+1): for j in range(1,N+1): if A[i][j] > A[i][k]+A[k][j]: A[i][j] = A[i][k]+A[k][j] if __name__=="__main__": while(1): N,M=map(int, input().split()) if N==0: break bignum=999999999999 L=[[bignum if x!=y else 0 for x in range(N+1)] for y in range(N+1)] S=[[bignum if x!=y else 0 for x in range(N+1)] for y in range(N+1)] for i in range(M): l=input().split() a,b,c = map(int, l[:-1]) if l[-1]=="S": S[a][b]=min(S[a][b],c) S[b][a]=min(S[b][a],c) else: L[b][a]=min(L[b][a],c) L[a][b]=min(L[a][b],c) wf(L,N) wf(S,N) R=int(input()) Z=list(map(int, input().split())) A=[[bignum for x in range(N+1)] for y in range(R)] A[0][Z[0]]=0 for i in range(1,R): src=Z[i-1] dst=Z[i] for n in range(1,N+1): if L[n][dst]!=bignum: for k in range(1,N+1): A[i][n]=min(A[i][n],A[i-1][k]+L[src][k]+S[k][n]+L[n][dst]) print (min(A[R-1]))
s956023974
p01321
u647766105
1389864672
Python
Python
py
Runtime Error
0
0
1728
def toBinary(block): return [sum(1 << i for i,bb in enumerate(b) if bb == "#") for b in block] def toStr(bfield): return ["".join("." if f & (1<<i) == 0 else "#" for i in xrange(W)) for f in bfield] def rotated(block): return map(lambda x:"".join(x),zip(*[b[:] for b in block]))[::-1] def striped(block): ret = [b[:] for b in block] while ret[-1] == "." * len(ret[0]): ret.pop(-1) while ret[0] == "." * len(ret[0]): ret.pop(0) return ret def canPut(x, y): if not(0 < x + w <= W or 0 < y + h <= H): return False for f,b in zip(bfield[y:y+h], bblock): if (f & (b << x)) != 0: return False return True def puted(x, y): ret = bfield[:] for i,b in zip(xrange(y,y+h), bblock): ret[i] = ret[i] | (b << x) #print "\n".join(toStr(ret)),"\n" return ret def count(bfield): return sum(1 for f in bfield if f == (1 << W) -1) for _ in xrange(input()): h, w = map(int, raw_input().split()) block = striped(rotated(striped([raw_input() for _ in xrange(h)]))) h, w = len(block), len(block[0]) H, W = map(int, raw_input().split()) field = [raw_input() for _ in xrange(H)] bfield = toBinary(field) FLAG = False ans = 0 for _ in xrange(4): block = rotated(block) h, w = w, h #print h,w,H,W #print "\n".join(field), "\n" #print "\n".join(block), "\n" bblock = toBinary(block) for y in xrange(0, H - h + 1): for x in xrange(0, W - w + 1): if canPut(x, y): FLAG = True #print x,y ans = max(ans, count(puted(x, y))) print ans if FLAG else -1
s917393934
p01322
u078042885
1486866335
Python
Python3
py
Runtime Error
0
0
169
while 1: n,a,b=map(int,input().split()) if n==a==b==0:break c=0;d=[1]*(n+1) for x in range(a): for i in range(x*b,n+1,a):d[i]=0 print(sum(d))
s328825772
p01325
u724548524
1528854456
Python
Python3
py
Runtime Error
0
0
750
while 1: n = int(input()) if n == 0:break an, bn = n, n a, b = list(map(int, input().split())), list(map(int, input().split())) while 1: ad = [] for i in range(an): if a[i] == bn: ad.append(i) for j in range(bn):b[j] -= 1 elif a[i] == 0: ad.append(i) an -= len(ad) for i in ad[::-1]:a.pop(i) bd = [] for i in range(bn): if b[i] == an: bd.append(i) for j in range(an):a[j] -= 1 elif b[i] == 0: bd.append(i) bn -= len(bd) for i in bd[::-1]:b.pop(i) if ad == bd == []:break print("Yes" if a == b == [] else "No")
s911288431
p01325
u724548524
1528856554
Python
Python3
py
Runtime Error
0
0
789
import bisect while 1: n = int(input()) if n == 0:break a0, b0 = 0, 0 an, bn = n, n p = (0, n, 0, n) f = 1 a, b = list(sorted(map(int, input().split()))), list(sorted(map(int, input().split()))) if sum(a) != sum(b):print("No") else: while 1: a0 = bisect.bisect_right(a, b0, a0, an) an = bisect.bisect_left(a, bn, a0, an) if min(a[a0], a[a0 - 1]) < n - bn:f = 0;break b0 = bisect.bisect_right(b, a0, b0, bn) bn = bisect.bisect_left(b, an, b0, bn) if min(b[b0], b[b0 - 1]) < n - an:f = 0;break print(b[b0 - 1]) print(an) if (a0, an, b0, bn) == p:break else:p = (a0, an, b0, bn) print("Yes" if a0 == an and f else "No")
s680164507
p01329
u352394527
1546025214
Python
Python3
py
Runtime Error
0
0
2255
from heapq import heappush, heappop from collections import deque def main(): while True: n, m = map(int, input().split()) if n == 0:break mp = ["#" * (m + 2)] + ["#" + input() + "#" for _ in range(n)] + ["#" * (m + 2)] for y in range(1, n + 1): for x in range(1, m + 1): if mp[y][x] == "S": sx, sy = x, y if mp[y][x] == "G": gx, gy = x, y p = int(input()) bans = [input() for _ in range(p)] dist = [[None] * (m + 2) for _ in range(n + 2)] dist[gy][gx] = 0 vec = ((1, 0), (0, -1), (-1, 0), (0, 1)) que = deque() que.append((0, gx, gy)) while que: score, x, y = que.popleft() for dx, dy in vec: nx, ny = x + dx, y + dy if dist[ny][nx] == None and mp[ny][nx] != "#": dist[ny][nx] = score + 1 que.append((score + 1, nx, ny)) def check(com): for ban in bans: if ban in com:return False return True que = [] heappush(que, (0 + dist[sy][sx], 0, sx, sy, "")) save = set() save.add((sx, sy, "")) while que: total, turn, x, y, com = heappop(que) if (x, y) == (gx, gy): print(turn) break nx, ny = x + 1, y if mp[ny][nx] != "#" and check(com + "R") and (nx, ny, (com + "R")[-9:]) not in save: heappush(que, (turn + 1 + dist[ny][nx], turn + 1, nx, ny, (com + "R")[-9:])) save.add((nx, ny, (com + "R")[-9:])) nx, ny = x - 1, y if mp[ny][nx] != "#" and check(com + "L") and (nx, ny, (com + "L")[-9:]) not in save: heappush(que, (turn + 1 + dist[ny][nx], turn + 1, nx, ny, (com + "L")[-9:])) save.add((nx, ny, (com + "L")[-9:])) nx, ny = x, y + 1 if mp[ny][nx] != "#" and check(com + "D") and (nx, ny, (com + "D")[-9:]) not in save: heappush(que, (turn + 1 + dist[ny][nx], turn + 1, nx, ny, (com + "D")[-9:])) save.add((nx, ny, (com + "D")[-9:])) nx, ny = x, y - 1 if mp[ny][nx] != "#" and check(com + "U") and (nx, ny, (com + "U")[-9:]) not in save: heappush(que, (turn + 1 + dist[ny][nx], turn + 1, nx, ny, (com + "U")[-9:])) save.add((nx, ny, (com + "U")[-9:])) else: print(-1) main()
s010445846
p01333
u099826363
1529581211
Python
Python3
py
Runtime Error
0
0
322
A,B = input().split() A = int(A) B = int(B) if A == 0: return oturi = B - A B1 = 0 C5 = 0 C1 = 0 if oturi/1000 >= 1: B1+= int(oturi/1000) oturi = oturi-(B1*1000) if oturi % 500 == 0: oturi = int(oturi) while oturi >= 500: oturi-=500 C5+=1 while oturi > 0: oturi-=100 C1+=1 print(C1,C5,B1)
s448210387
p01333
u099826363
1529581231
Python
Python3
py
Runtime Error
0
0
322
A,B = input().split() A = int(A) B = int(B) if A == 0: return oturi = B - A B1 = 0 C5 = 0 C1 = 0 if oturi/1000 >= 1: B1+= int(oturi/1000) oturi = oturi-(B1*1000) if oturi % 500 == 0: oturi = int(oturi) while oturi >= 500: oturi-=500 C5+=1 while oturi > 0: oturi-=100 C1+=1 print(C1,C5,B1)
s585293474
p01333
u099826363
1529581244
Python
Python3
py
Runtime Error
0
0
324
A,B = input().split() A = int(A) B = int(B) if A == 0: return 0 oturi = B - A B1 = 0 C5 = 0 C1 = 0 if oturi/1000 >= 1: B1+= int(oturi/1000) oturi = oturi-(B1*1000) if oturi % 500 == 0: oturi = int(oturi) while oturi >= 500: oturi-=500 C5+=1 while oturi > 0: oturi-=100 C1+=1 print(C1,C5,B1)
s946735865
p01336
u266872031
1528596738
Python
Python
py
Runtime Error
0
0
991
while(1): try: N,M=map( int,raw_input().split()) W=[[] for i in range(3)] for i in range(N): name=raw_input() inp=map( int,raw_input().split()) c=inp[0] for i in range(3): W[i].append([c, inp[1+i]]) ans=0 for i in range(3): DP=[[0 for x in range(M+1)] for y in range(N)] for t in range(M): ci=W[i][0][0]*t if ci <= M: DP[0][ci]=W[i][0][1]*t else: break for y in range(1,N): for x in range(M+1): for t in range(M): ci=W[i][y][0]*t if ci <= x: DP[y][x] = max(DP[y][x], DP[y-1][x-ci]+W[i][y][1]*t ) else: break ans=max(ans, max(DP[-1])) print ans except: break
s867736311
p01336
u266872031
1528597405
Python
Python
py
Runtime Error
0
0
1061
while(1): try: N,M=map( int,raw_input().split()) W=[[] for i in range(3)] for i in range(N): name=raw_input() inp=map( int,raw_input().split()) c=inp[0] for i in range(3): W[i].append([c, inp[1+i]]) for i in range(3): W[i].sort(key = lambda x:x[0]) ans=0 for i in range(3): DP=[[0 for x in range(M+1)] for y in range(N)] for t in range(M+1): ci=W[i][0][0]*t if ci <= M: DP[0][ci]=W[i][0][1]*t else: break for y in range(1,N): for x in range(M+1): for t in range(M): ci=W[i][y][0]*t if ci <= x: DP[y][x] = max(DP[y][x], DP[y-1][x-ci]+W[i][y][1]*t ) else: break ans=max(ans, max(DP[-1])) print ans except: break
s306346574
p01337
u506537276
1559541255
Python
Python3
py
Runtime Error
0
0
815
import math w1 = 1 w2 = 1/2 * (-1 + math.sqrt(3)*1j) w3 = 1/2 * (-1 - math.sqrt(3)*1j) n = int(input()) for i in range(n): aa, bb, cc, dd = map(int, input().split()) a = bb / aa b = cc / aa c = dd / aa p = -1/54 * (27 * c + 2 * (a ** 3) - 9 * a * b) q = 1/9 * (3 * b - a ** 2) r = 1/3 * a x1 = w1 * (p + math.sqrt(p ** 2 + q ** 3)) ** (1/3) + w1 * (p - math.sqrt(p ** 2 + q ** 3)) ** (1/3) - r x2 = w2 * (p + math.sqrt(p ** 2 + q ** 3)) ** (1/3) + w3 * (p - math.sqrt(p ** 2 + q ** 3)) ** (1/3) - r x3 = w3 * (p + math.sqrt(p ** 2 + q ** 3)) ** (1/3) + w2 * (p - math.sqrt(p ** 2 + q ** 3)) ** (1/3) - r plus = 0 minus = 0 for i in [x1, x2, x3]: if i.imag == 0: i = i.real if i > 0: plus += 1 elif i < 0: minus += 1 print(plus, minus)
s028153929
p01337
u506537276
1559541446
Python
Python3
py
Runtime Error
0
0
815
import math w1 = 1 w2 = 1/2 * (-1 + math.sqrt(3)*1j) w3 = 1/2 * (-1 - math.sqrt(3)*1j) n = int(input()) for i in range(n): aa, bb, cc, dd = map(int, input().split()) a = bb / aa b = cc / aa c = dd / aa p = -1/54 * (27 * c + 2 * (a ** 3) - 9 * a * b) q = 1/9 * (3 * b - a ** 2) r = 1/3 * a x1 = w1 * (p + math.sqrt(p ** 2 + q ** 3)) ** (1/3) + w1 * (p - math.sqrt(p ** 2 + q ** 3)) ** (1/3) - r x2 = w2 * (p + math.sqrt(p ** 2 + q ** 3)) ** (1/3) + w3 * (p - math.sqrt(p ** 2 + q ** 3)) ** (1/3) - r x3 = w3 * (p + math.sqrt(p ** 2 + q ** 3)) ** (1/3) + w2 * (p - math.sqrt(p ** 2 + q ** 3)) ** (1/3) - r plus = 0 minus = 0 for i in [x1, x2, x3]: if i.imag == 0: i = i.real if i > 0: plus += 1 elif i < 0: minus += 1 print(plus, minus)
s025118805
p01339
u572790226
1466496141
Python
Python3
py
Runtime Error
0
0
433
def combi(n): if n == 1: return [[1],[0]] else: s = combi(n-1) ret = [[t] + x for x in s for t in [1, 0]] return ret N, M = map(int, input().split()) whole = combi(N) for i in range(M): select = [] s, d = map(int, input().split()) for c in whole: if c[s-1] == 1 and c[d-1] ==0: select.append(c) for c in select: whole.remove(c) print(len(whole))
s246584653
p01359
u010611609
1558954675
Python
Python3
py
Runtime Error
0
0
488
from bisect import bisect_left while True: N, Q = map(int, input().split(' ')) if N == 0: break era = {} # Like {1877: ('Meiji', 10)} years = [] # 西暦 for _ in range(N): name, j, w = input().split(' ') j, w = int(j), int(w) era[w] = (name, j) years.append(w) years.sort() for _ in range(Q): q = int(input()) y = years[bisect_left(years, q)] name, length = era[y] if q - y + length <= 0: print('Unknown') else: print(name + ' ' + str(q-y+length))
s002735211
p01359
u010611609
1558954686
Python
Python3
py
Runtime Error
0
0
488
from bisect import bisect_left while True: N, Q = map(int, input().split(' ')) if N == 0: break era = {} # Like {1877: ('Meiji', 10)} years = [] # 西暦 for _ in range(N): name, j, w = input().split(' ') j, w = int(j), int(w) era[w] = (name, j) years.append(w) years.sort() for _ in range(Q): q = int(input()) y = years[bisect_left(years, q)] name, length = era[y] if q - y + length <= 0: print('Unknown') else: print(name + ' ' + str(q-y+length))
s160327754
p01359
u010611609
1558954717
Python
Python3
py
Runtime Error
0
0
488
from bisect import bisect_left while True: N, Q = map(int, input().split(' ')) if N == 0: break era = {} # Like {1877: ('Meiji', 10)} years = [] # 西暦 for _ in range(N): name, j, w = input().split(' ') j, w = int(j), int(w) era[w] = (name, j) years.append(w) years.sort() for _ in range(Q): q = int(input()) y = years[bisect_left(years, q)] name, length = era[y] if q - y + length <= 0: print('Unknown') else: print(name + ' ' + str(q-y+length))
s548488560
p01359
u633068244
1398929316
Python
Python
py
Runtime Error
0
0
365
while 1: n,m = map(int,raw_input().split()) if n == 0: break E,B,Y = [],[],[] for i in range(n): era,base,year = raw_input().split() E.append(era) B.append(int(base)) Y.append(int(year)) for i in range(m): year = int(raw_input()) for j in range(n): if Y[j]-B[j] < year <= Y[j]: print E[i], year-Y[j]+B[i] break else: print "Unknown"
s095476133
p01359
u633068244
1398929360
Python
Python
py
Runtime Error
0
0
365
while 1: n,m = map(int,raw_input().split()) if n == 0: break E,B,Y = [],[],[] for i in range(n): era,base,year = raw_input().split() E.append(era) B.append(int(base)) Y.append(int(year)) for i in range(m): year = int(raw_input()) for j in range(n): if Y[j]-B[j] < year <= Y[j]: print E[i], year-Y[j]+B[j] break else: print "Unknown"
s494068016
p01360
u509278866
1528687171
Python
Python3
py
Runtime Error
0
0
1695
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 998244353 dd = [(0,-1),(1,0),(0,1),(-1,0)] ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def main(): rr = [] while True: s = S() if s == '#': break l = True lr = 0 for i in range(1,len(s)): c = s[i] p = s[i-1] cl = int(c) % 3 if cl == 0: cl = 3 pl = int(p) % 3 if pl == 0: pl = 3 if l and cl > pl: lr += 1 elif not l and cl < pl: lr += 1 else: l = not l l = False tr = 0 for i in range(1,len(s)): c = s[i] p = s[i-1] cl = int(c) % 3 if cl == 0: cl = 3 pl = int(p) % 3 if pl == 0: pl = 3 if l and cl > pl: tr += 1 elif not l and cl < pl: tr += 1 else: l = not l rr.append(min(lr,tr)) return '\n'.join(map(str, rr)) print(main())
s229146165
p01369
u352394527
1531260054
Python
Python3
py
Runtime Error
0
0
545
#include <iostream> using namespace std; int main(void){ string s, left; int pos, tmp, cnt, i; left = "qwertasdfgzxcvb"; while(1){ cin >> s; if(s == "#"){ break; } if(left.find(s[0]) != std::string::npos){ pos = 0; }else{ pos = 1; } cnt = 0; for(i=1;i<s.length();i++){ if(left.find(s[i]) != std::string::npos){ tmp = 0; }else{ tmp = 1; } if(pos != tmp){ cnt++; pos = tmp; } } cout << cnt << endl; } return 0; }
s427617304
p01369
u910106747
1497695535
Python
Python
py
Runtime Error
0
0
459
def solve(s): dic = {} ll = 'qwertasdfgzxcvb' rr = 'yuiophjklnm' for l_i in list(ll): dic[l_i] = -1 for r_i in list(rr): dic[r_i] = 1 ans = 0 prev = 0 for s_i in s: current = dic[s_i] if current != prev: if abs(current - prev) == 2: ans += 1 prev = current return ans while 1: s = input() if s == '#': break print(solve(s))
s112054116
p01369
u412356196
1505819596
Python
Python
py
Runtime Error
0
0
869
using System; using System.Collections.Generic; class Program { static void Main(string[] args) { HashSet<char> s = new HashSet<char> { }; string yukio = "yuiophjklnm"; for (int i = 0; i < yukio.Length; i++) { s.Add(yukio[i]); } while (true) { string a = Console.ReadLine(); if (a == "#") { break; } int ans = 0; for (int i = 1; i < a.Length; i++) { bool o = false; if (s.Add(a[i])) { o = true;s.Remove(a[i]); } bool o1 = false; if (s.Add(a[i - 1])) { o1 = true;s.Remove(a[i-1]); } if (o != o1) { ans++; } } Console.WriteLine(ans); } Console.ReadLine(); } }
s108190563
p01370
u633068244
1422281817
Python
Python
py
Runtime Error
19920
4604
599
while 1: t,n = map(int,raw_input().split()) if t == n == 0: break block = [map(int,raw_input().split()) for _ in range(n)] sx,sy = map(int,raw_input().split()) visited = [] visited.append([sx,sy]) que = [[sx,sy,t]] ans = 1 while que: x,y,tt = que.pop(0) if tt == 0: continue for dx,dy in zip([1,1,0,-1,-1,0],[0,1,1,0,-1,-1]): nx,ny = x+dx,y+dy if [nx,ny] not in visited and [nx,ny] not in block: que.append([nx,ny,tt-1]) visited.append([nx,ny]) ans += 1 print ans
s839353510
p01370
u617183767
1424151118
Python
Python
py
Runtime Error
19930
75532
1174
#! /usr/bin/env python # -*- coding: utf-8 -*- import os import sys import itertools import math from collections import Counter, defaultdict class Main(object): def __init__(self): pass def solve(self): ''' insert your code ''' while True: t, n = map(int, raw_input().split()) if t == n == 0: break obs = [] for i in range(n): x, y = map(int, raw_input().split()) obs.append((x, y)) sx, sy = map(int, raw_input().split()) q = [(sx, sy, 0)] visited = set([(sx, sy)]) d = [(1, 0), (1, 1), (0, 1), (-1, 0), (-1, -1), (0, -1)] while q: x, y, turn = q.pop(0) if turn >= t: break for dx, dy in d: if not (x + dx, y + dy) in obs: visited.add((x + dx, y + dy)) q.append((x + dx, y + dy, turn + 1)) # print visited print len(visited) return None if __name__ == '__main__': m = Main() m.solve()
s726726705
p01370
u316268279
1426010217
Python
Python3
py
Runtime Error
0
0
920
#!/usr/bin/env python # -*- coding: utf-8 -*- import heapq as hq def bfs(x,y,t,Map,depth): queue = [(depth,(x,y))] while len(queue) != 0: p = hq.heappop(queue) x,y = p[1] if p[0] < t and Map[y][x] == 0: Map[y][x] = 1 dxdy = [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1)] for dx,dy in dxdy: if 0 <= x + dx < 60 and 0 <= y + dy < 60: hq.heappush(queue,(p[0] + 1, (x + dx, y + dy))) while True: t,n = map(int,input().split()) if t == 0 and n == 0: break Map = [[0 for j in range(60)] for i in range(60)] for _ in range(n): x,y = map(lambda x:int(x) + 30,input().split()) Map[y][x] = -1 x,y = map(lambda x:int(x) + 30,input().split()) bfs(x,y,t + 1,Map,0) print(sum([0 if Map[i][j] == -1 else Map[i][j] for i in range(60) for j in range(60)]))
s590621074
p01370
u316268279
1426010327
Python
Python3
py
Runtime Error
0
0
894
#!/usr/bin/env python # -*- coding: utf-8 -*- import heapq as hq def bfs(x,y,t,Map,depth): queue = [(depth,(x,y))] while len(queue) != 0: p = hq.heappop(queue) x,y = p[1] if p[0] < t and Map[y][x] == 0: Map[y][x] = 1 dxdy = [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1)] for dx,dy in dxdy: if 0 <= x + dx < 60 and 0 <= y + dy < 60: hq.heappush(queue,(p[0] + 1, (x + dx, y + dy))) while True: t,n = map(int,input().split()) if t == 0 and n == 0: break Map = [[0 for j in range(60)] for i in range(60)] for _ in range(n): x,y = map(lambda x:int(x) + 30,input().split()) Map[y][x] = -1 x,y = map(lambda x:int(x) + 30,input().split()) bfs(x,y,t + 1,Map,0) print(sum([0 if Map[i][j] == -1 else Map[i][j] for i in range(60) for j in range(60)]))
s771206258
p01370
u120360464
1434962263
Python
Python
py
Runtime Error
0
0
829
#! /usr/bin/python # -*- coding: utf-8 -*- (t, n) = map(int, raw_input().split()) while t != 0 or n != 0: M = [[False]*100 for i in range(100)] for i in range(n): (obx, oby) = map(int, raw_input().split()) M[oby+50][obx+50] = True (sx, sy) = map(int, raw_input().split()) ST = [] #0:y 1:x 2:turn num cnt = 0 #----above init----# #print s ST.append([sy+50, sx+50, 0]) while len(ST) > 0: y = ST[0][0] x = ST[0][1] c = ST[0][2] ST.pop(0) if c <= t and M[y][x] == False: M[y][x] = True cnt += 1 for dy in [-1, 0, 1]: for dx in [-1, 0, 1]: if dx+dy != 0: ST.append([y+dy, x+dx, c+1]) print cnt (t, n) = map(int, raw_input().split())
s286229144
p01370
u120360464
1434962300
Python
Python
py
Runtime Error
0
0
829
#! /usr/bin/python # -*- coding: utf-8 -*- (t, n) = map(int, raw_input().split()) while t != 0 or n != 0: M = [[False]*100 for i in range(100)] for i in range(n): (obx, oby) = map(int, raw_input().split()) M[oby+50][obx+50] = True (sx, sy) = map(int, raw_input().split()) ST = [] #0:y 1:x 2:turn num cnt = 0 #----above init----# #print s ST.append([sy+50, sx+50, 0]) while len(ST) > 0: y = ST[0][0] x = ST[0][1] c = ST[0][2] ST.pop(0) if c <= t and M[y][x] == False: M[y][x] = True cnt += 1 for dy in [-1, 0, 1]: for dx in [-1, 0, 1]: if dx+dy != 0: ST.append([y+dy, x+dx, c+1]) print cnt (t, n) = map(int, raw_input().split())
s036616488
p01370
u120360464
1434962430
Python
Python
py
Runtime Error
0
0
856
#! /usr/bin/python # -*- coding: utf-8 -*- (t, n) = map(int, raw_input().split()) while t != 0 or n != 0: M = [[False]*100 for i in range(100)] for i in range(n): (obx, oby) = map(int, raw_input().split()) M[oby+50][obx+50] = True (sx, sy) = map(int, raw_input().split()) ST = [] #0:y 1:x 2:turn num cnt = 0 #----above init----# #print s ST.append([sy+50, sx+50, 0]) while len(ST) > 0: y = ST[0][0] x = ST[0][1] c = ST[0][2] ST.pop(0) if c <= t and M[y][x] == False: M[y][x] = True cnt += 1 for dy in [-1, 0, 1]: for dx in [-1, 0, 1]: if dx+dy != 0 and M[y+dy][x+dx] == False: ST.append([y+dy, x+dx, c+1]) print cnt (t, n) = map(int, raw_input().split())
s681857036
p01371
u731235119
1423155828
Python
Python
py
Runtime Error
19920
15600
694
while 1: N = int(raw_input()) if N == 0: break stage = set(range(1,N+1)) time = [None] + [map(int,raw_input().split(" ")) for _ in range(N)] dp = dict([[tuple([i]),time[i][0]] for i in range(1,N+1)]) count = 1 while count < N: tmp_dp = dict() for k, v in dp.iteritems(): cleared = set(k) for st in stage - cleared: min_time = float("inf") for i in range(1,N+1): if i in cleared and time[st][i] < min_time: min_time = time[st][i] new_cleared = tuple(sorted(cleared | set([st]))) if not (new_cleared in tmp_dp and min_time + v >= tmp_dp[new_cleared]): tmp_dp[new_cleared] = min_time + v dp = tmp_dp count += 1 print dp[tuple(sorted(stage))]
s129883039
p01371
u731235119
1423196071
Python
Python
py
Runtime Error
19930
9340
515
INF = float("inf") while 1: N = int(raw_input()) if N == 0: break time = [map(int,raw_input().split(" ")) for _ in range(N)] dp = [INF for i in range(2**N)] dp[0] = 0 for cleared in range(2**N): for i in range(0,N): now = 2 ** i if now & cleared != 0: continue tmp_time = time[i][0] for j in range(1,N+1): if cleared & 2 ** (j-1) == 0: continue tmp_time = min(tmp_time, time[i][j]) clear = cleared | now dp[clear] = min(dp[clear], dp[cleared] + tmp_time) print dp[2**N-1]
s132813653
p01371
u633068244
1424356118
Python
Python
py
Runtime Error
0
0
964
#include <iostream> #include <algorithm> #include <string.h> #include <stdio.h> #define REP(i,a,b) for (int i = (a); i < (b); i++) #define rep(i,a) REP(i,0,a) using namespace std; const int INF = 1 << 28; int sp, N; int t[16][17]; int dp[1 << 16][16]; int rec(int S, int v) { if (dp[S][v] > 0) return dp[S][v]; if (S == (1 << N) - 1 && v == sp) return dp[S][v] = 0; int res = INF; rep (u, N) { if (!(S >> u & 1)) { int cost = t[u][0]; rep(i, N) if (S & 1 << i) cost = min(cost, t[u][i+1]); res = min(res, rec(S | 1 << u, u) + cost); } } return dp[S][v] = res; } int main(void){ while (1) { cin >> N; if (N == 0) break; rep(i, N) rep(j, N+1) cin >> t[i][j]; int ans = INF; for (sp = 0; sp < N; sp++) { memset(dp, -1, sizeof(dp)); ans = min(ans, rec(0,sp)); } cout << ans << endl; } return 0; }
s340427608
p01371
u981364540
1471355446
Python
Python3
py
Runtime Error
40000
10748
507
import sys while 1: N = int(input()) if N == 0 : break t = [list(map(int, input().split())) for i in range(N)] dp = [sys.maxsize]*(1<<N) dp[0] = 0 for i in range(1<<N): for j in range(N): if (i & (1<<j)) != 0: continue cost = t[j][0] for k in range(N): if (i & (1<<k)) == 0: continue cost = min(t[j][k+1], cost) n = (i | (1<<j)) dp[n] = min(dp[n], dp[i]+cost) print(dp[(1<<N)-1])
s311897101
p01371
u981364540
1471355873
Python
Python3
py
Runtime Error
40000
10692
507
import sys while 1: N = int(input()) if N == 0 : break t = [list(map(int, input().split())) for i in range(N)] dp = [sys.maxsize]*(1<<N) dp[0] = 0 for i in range(1<<N): for j in range(N): if (i & (1<<j)) != 0: continue cost = t[j][0] for k in range(N): if (i & (1<<k)) == 0: continue cost = min(t[j][k+1], cost) n = (i | (1<<j)) dp[n] = min(dp[n], dp[i]+cost) print(dp[(1<<N)-1])
s942287952
p01371
u981364540
1471356327
Python
Python3
py
Runtime Error
40000
10860
512
import sys while 1: N = int(input()) if N == 0 : sys.exit() t = [list(map(int, input().split())) for i in range(N)] dp = [sys.maxsize]*(1<<N) dp[0] = 0 for i in range(1<<N): for j in range(N): if (i & (1<<j)) != 0: continue cost = t[j][0] for k in range(N): if (i & (1<<k)) == 0: continue cost = min(t[j][k+1], cost) n = (i | (1<<j)) dp[n] = min(dp[n], dp[i]+cost) print(dp[(1<<N)-1])
s455211174
p01371
u981364540
1471356440
Python
Python3
py
Runtime Error
40000
10696
507
import sys while 1: N = int(input()) if N == 0 : sys.exit() t = [list(map(int, input().split())) for i in range(N)] dp = [500000]*(1<<N) dp[0] = 0 for i in range(1<<N): for j in range(N): if (i & (1<<j)) != 0: continue cost = t[j][0] for k in range(N): if (i & (1<<k)) == 0: continue cost = min(t[j][k+1], cost) n = (i | (1<<j)) dp[n] = min(dp[n], dp[i]+cost) print(dp[(1<<N)-1])
s159442751
p01371
u981364540
1471356979
Python
Python3
py
Runtime Error
40000
10700
515
import sys while True: N = int(input()) if N == 0 : sys.exit() t = [list(map(int, input().split())) for i in range(N)] dp = [sys.maxsize]*(1<<N) dp[0] = 0 for i in range(1<<N): for j in range(N): if (i & (1<<j)) != 0: continue cost = t[j][0] for k in range(N): if (i & (1<<k)) == 0: continue cost = min(t[j][k+1], cost) n = (i | (1<<j)) dp[n] = min(dp[n], dp[i]+cost) print(dp[(1<<N)-1])
s687571056
p01371
u685815919
1474616666
Python
Python
py
Runtime Error
40000
8488
727
import sys def bitdp(N, costs): dp = [sys.maxint] * 2**N dp[0] = 0 # min cost of state i is calcurated by minimum of # before state i-2**k + cost of minimum for clear k for i in xrange(1, 2 ** N): for k in xrange(N): if i & 2**k > 0: mincost = costs[k][0] for x in xrange(0, N): if (i-2**k) & 2**x > 0: mincost = min(mincost, costs[k][x+1]) dp[i] = min(dp[i], dp[i-2**k]+mincost) print dp[2**N-1] while True: N = int(raw_input()) if N==0: break # cost for clear stage i with item j costs = [[0]*(N+1) for _ in xrange(N)] for i in xrange(N): t = map(int, raw_input().split()) for j in xrange(N+1): costs[i][j] = t[j] bitdp(N, costs)
s647109603
p01371
u672443148
1515145101
Python
Python3
py
Runtime Error
0
0
912
while True: N=int(input()) if N==0: break stage=[] for i in range(N): stage.append(list(map(int,input().split()))) INF=int(1e8) dp=[INF for i in range(1<<N)] dp[0]=0 for bit in range(1<<N): #ステージのクリア状況分だけループを回す for i in range(N): if bit & 1<<i: continue #iをクリアしている場合ループを抜ける cost=stage[i][0] #iをクリアするのに装備無しでかかる時間 for j in range(N): if (bit & 1<<j)==0: continue cost=min(stage[i][j+1],cost) #jの装備を使ったときに速いほうを選択する now=(bit|(1<<i)) #iをクリアしたとする dp[now]=min(dp[now],dp[bit]+cost) #クリア状況bitの時にiをクリアした状態 print(dp[(1<<N)-1])
s503495011
p01371
u855199458
1530101847
Python
Python3
py
Runtime Error
0
0
777
# -*- coding: utf-8 -*- from itertools import combinations N = int(input()) while N: T = [] B = [] for i in range(N): base, *other = list(map(int, input().split())) B.append(base) T.append(other) cost = [10**7]*(2**N) X = [2**i for i in range(N)] for i, x in enumerate(X): cost[x] = B[i] for i in range(1, N): for j in combinations(range(N), i): tmp = 0 for k in j: tmp += X[k] for l in range(N): if tmp & X[l]: continue else: c = min([B[l]] + [T[l][m] for m in j]) cost[tmp + X[l]] = min(cost[tmp] + c, cost[tmp + X[l]]) print(cost[-1]) N = int(input())
s081314435
p01371
u352394527
1530574783
Python
Python3
py
Runtime Error
0
0
695
INF = 10 ** 20 def minimum_cost(now, visited): if (now, visited) in dp: return dp[(now, visited)] if visited == end: return 0 ret = INF for to in range(1, n + 1): if to not in visited: ret = min(ret, minimum_cost(to, tuple(sorted(list(visited) + [to]))) + cost(to, visited)) dp[(now, visited)] = ret return ret def cost(to, visited): return min(tlst[to - 1][i] for i in visited) while True: n = int(input()) if n == 0: break tlst = [list(map(int, input().split())) for _ in range(n)] dp = {} end = tuple(i for i in range(n + 1)) ans = INF for i in range(1, n + 1): ans = min(ans, minimum_cost(i, (0, i)) + tlst[i - 1][0]) print(ans)
s748093039
p01371
u352394527
1530580014
Python
Python3
py
Runtime Error
0
0
769
INF = 10 ** 20 def minimum_cost(rest, dp, tlst, end, n): if rest in dp: return dp[rest] if rest == end: return 0 ret = INF for ind, to in enumerate(rest): if to == "0": tmp = minimum_cost(rest[:ind] + "1" + rest[ind + 1:], dp, tlst, end, n) + cost(ind, rest, tlst, n) if tmp < ret: ret = tmp dp[rest] = ret return ret def cost(ind, rest, tlst, n): ts = tlst[ind - 1] ret = INF for c, v in zip(rest, ts): if v < ret and c == "1": ret = v return ret def main(): while True: n = int(input()) if n == 0: break tlst = [list(map(int, input().split())) for _ in range(n)] dp = {} end = "1" * (n + 1) ans = minimum_cost("1" + "0" * n, dp, tlst, end, n) print(ans) main()
s685313398
p01371
u109084363
1361955394
Python
Python
py
Runtime Error
19930
4616
718
import sys import collections while True: N = int(raw_input()) if N == 0: break time = [] for i in xrange(N): time.append(map(int, raw_input().split())) def dp(state): L = len(state) if L <= 1: return time[state[0]][0] else: temp = [] for i in state: copy = state[:] copy.remove(i) min_time = time[i][0] for j in xrange(len(time[i])): if j in copy: min_time = min(min_time, time[i][j + 1]) temp.append(dp(copy) + min_time) return min(temp) state = range(N) print dp(state)
s960730773
p01371
u109084363
1361955533
Python
Python
py
Runtime Error
19930
4252
516
import sys while True: N = int(raw_input()) if N == 0: break time = [] for i in xrange(N): time.append(map(int, raw_input().split())) def dp(state): L = len(state) if L <= 1: return time[state[0]][0] else: temp = [] for i in state: copy = state[:] copy.remove(i) min_time = time[i][0] for j in xrange(len(time[i])): if j in copy: min_time = min(min_time, time[i][j + 1]) temp.append(dp(copy) + min_time) return min(temp) state = range(N) print dp(state)