code1
stringlengths
16
24.5k
code2
stringlengths
16
24.5k
similar
int64
0
1
pair_id
int64
2
181,637B
โŒ€
question_pair_id
float64
3.71M
180,628B
โŒ€
code1_group
int64
1
299
code2_group
int64
1
299
X = int(input()) def is_prime(n): a = 2 while a*a < n: if n%a == 0: return False a += 1 return True while True: if is_prime(X): break X += 1 print(X)
H,W,K=map(int,input().split()) s=[] for i in range(H): S=input() s.append(S) a=[[0 for i in range(W)] for j in range(H)] tmp=-1 flag=False chk=[0 for _ in range(W)] for w in range(W): count=0 for h in range(H): if not flag: if s[h][w]=="#": tmp=w flag=True if s[h][w]==".": count+=1 if count==H: chk[w]=1 num=0 flag=True f=True for w in range(W): if w<tmp: continue if chk[w]==0: num+=1 f=True for h in range(H): if chk[w]==0: if s[h][w]=="#": if flag: num=1 flag=False if f: pass f=False else: num+=1 a[h][w]=num else: a[h][w]=num else: a[h][w]=a[h][w-1] if tmp!=0: for w in range(tmp): for h in range(H): if (tmp-1-w)<0: break a[h][tmp-1-w]=a[h][tmp-w] for i in range(H): print(*a[i])
0
null
124,816,754,019,750
250
277
def f(x): now = 0 for i in range(n): now += (a[i]-1)//x return now <= k n, k = map(int, input().split()) a = list(map(int, input().split())) ng = 0 ok= int(1e9) while ok - ng > 1: x = (ok + ng) // 2 if f(x): ok = x else: ng = x print(ok)
N, K = map(int, input().split()) A = [int(i) for i in input().split()] def f(length, ls): cur = 0 for a in ls: if a%length == 0: cur += a//length - 1 else: cur += a//length if cur <= K: return True else: return False ok, ng = max(A), 0 while abs(ok - ng) > 1: z = (ok+ng)//2 if f(z, A) == True: ok = z else: ng = z print(ok)
1
6,523,796,060,250
null
99
99
h, w = map(int, input().split()) h_odd = (h+1)//2 h_even = h//2 w_odd = (w+1)//2 w_even = w//2 if h == 1 or w ==1: print(1) else: ans = h_odd * w_odd + h_even *w_even print(ans)
h, w = map(int,input().split()) print((h*w+1)//2 if h!=1 and w!=1 else 1)
1
50,650,953,324,190
null
196
196
while True: h,w = map(int, raw_input().split()) if h == w == 0: break print (("#" * w + "\n") * h)
def calc_matrix(A, B, size): n, m, l = size # print(n, m, l) results = [] for i in range(n): row_data = [] for j in range(l): products = [] for k in range(m): products.append(A[i][k] * B[k][j]) # print('C[{0}][{1}] = {2}'.format(i, j, sum(products))) row_data.append(sum(products)) results.append(row_data) return results if __name__ == '__main__': # ??????????????\??? A = [] B = [] # A.append([1, 2]) # A.append([0, 3]) # A.append([4, 5]) # B.append([1, 2, 1]) # B.append([0, 3, 2]) n, m, l = [int(x) for x in input().split(' ')] for i in range(n): A.append([int(x) for x in input().split(' ')]) for i in range(m): B.append([int(x) for x in input().split(' ')]) # ??????????????? results = calc_matrix(A, B, (n, m, l)) # ??????????????? for row in results: print(' '.join(map(str, row)))
0
null
1,105,471,448,040
49
60
x1,y1,x2,y2=map(float,input().split()) d=((x1-x2)**2 + (y1-y2)**2)**(1/2) print(d)
x1, y1, x2, y2 = map(float, input().split()) X = (x2 - x1)**2 Y = (y2 - y1)**2 distance = (X + Y)**(1/2) print("%.8f" % (float(distance)))
1
155,081,372,992
null
29
29
s = input() t = input() len_s = len(s) len_t = len(t) ans = len_t for i in range(len_s - len_t + 1): tmp_cnt = 0 tmp_s = s[i:i + len_t] for c1, c2 in zip(tmp_s, t): if c1 != c2: tmp_cnt += 1 ans = min(ans, tmp_cnt) print(ans)
S = input() T = input() ans = 1e10 for i in range(len(S)): if i+len(T) > len(S): break cnt = 0 for j in range(len(T)): if S[i+j] != T[j]: cnt += 1 ans = min(cnt, ans) print(ans)
1
3,633,037,963,682
null
82
82
c = input() a = ord(c) a += 1 print(chr(a))
import sys read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines sys.setrecursionlimit(10 ** 9) INF = 1 << 60 MOD = 1000000007 def main(): a, b = map(int, readline().split()) s1 = str(a) * b s2 = str(b) * a if s1 < s2: print(s1) else: print(s2) return if __name__ == '__main__': main()
0
null
88,025,071,152,134
239
232
S = input() T = input() max_count = 0 for i in range(max(1, len(S) - len(T))): count = 0 for j in range(len(T)): if S[i+j] == T[j]: count += 1 max_count = max(max_count, count) print(len(T) - max_count)
s=input() t=input() n=len(s) n1=len(t) ans=10**10 for i in range(n-n1+1): diff=0 for j in range(n1): if(t[j]!=s[i+j]): diff+=1 ans=min(ans,diff) print(ans)
1
3,698,183,816,500
null
82
82
K=int(input()) for i in range(K): print("ACL",end="") print("")
cnt = int(input()) print("ACL" * cnt)
1
2,202,056,343,168
null
69
69
n = int(input()) s = [] t = [] for i in range(n): a,b = input().split() s.append(a) t.append(int(b)) x = s.index(input()) ans = 0 for i in range(x+1,n): ans += t[i] print(ans)
n = int(input()) a = list(map(int,input().split())) count = 0 for i in range(n-1): flag = 0 min_j = i for j in range(i,n): if a[j] < a[min_j]: min_j = j flag = 1 if flag: a[i], a[min_j] = a[min_j], a[i] count += 1 print(" ".join(map(str,a))) print(count)
0
null
48,425,460,127,198
243
15
s = input() t = input() n = len(s) - len(t) l = len(t) res = 0 for i in range(n+1): cnt = 0 for j in range(l): if s[i+j] == t[j]: cnt += 1 res = max(res, cnt) print(l - res)
s = input() t = input() ls = len(s) lt = len(t) min_count = 1000 for i in range(ls-lt+1): count = 0 for j in range(lt): if s[i:i+lt][j] != t[j]: count += 1 if min_count > count: min_count = count print(min_count)
1
3,713,139,737,110
null
82
82
n=int(input()) a=list(map(int,input().split())) mod=10**9+7 c=[0]*60 for aa in a: for i in range(60): if aa&(1<<i):c[i]+=1 ans=0 for i,cc in enumerate(c): ans+=cc*(n-cc)*2**i ans%=mod print(ans)
import numpy as np n=int(input()) a=np.array(list(map(int,input().split()))) mod=10**9+7 s=0 for i in range(60): bit = np.count_nonzero(a & 1) s += bit*(n-bit)*(2**i) a >>= 1 print(s % mod)
1
122,956,017,345,940
null
263
263
while True: H,W = map(int,input().split()) if H==0 and W==0: break idx = ('#'*W + '\n') con = "" if W > 2: con = ('#' + '.'*(W-2) + '#\n') * (H-2) if H >= 2: con =con + idx print(idx + con)
brank = '.' while 1: H, W = map(int, raw_input().split()) if H == 0 and W == 0: break for h in xrange(H): if (h == 0) or (h == H - 1): print '#' * W else: print ('#' + brank * (W - 2) + '#') print
1
836,366,560,448
null
50
50
K=int(input()) L=len(input()) from numba import* @njit(cache=1) def f(): m=10**9+7 max_n=2*10**6 fac=[1]*(max_n+1) inv=[1]*(max_n+1) ifac=[1]*(max_n+1) for n in range(2,max_n+1): fac[n]=(fac[n-1]*n)%m inv[n]=m-inv[m%n]*(m//n)%m ifac[n]=(ifac[n-1]*inv[n])%m d=[1]*(K+1) d2=[1]*(K+1) for i in range(K): d[i+1]=d[i]*25%m d2[i+1]=d2[i]*26%m a=0 for i in range(K+1): a=(a+fac[L+i-1]*ifac[i]%m*ifac[L-1]%m*d[i]%m*d2[K-i]%m)%m return a print(f())
def sol(s, p): n = len(s) cnt = 0 if p == 2 or p == 5: for i in range(n): if (s[i] % p == 0): cnt += i + 1 else: pre = [0] * (n+2) pre[n+1] = 0 b = 1 for i in range(n, 0, -1): pre[i] = (pre[i+1] + s[i-1] * b) % p b = (b * 10) % p rec = [0] * p rec[0] = 1 for i in range(n, 0, -1): cnt += rec[pre[i]] rec[pre[i]] += 1 return cnt if __name__ == "__main__": n, p = map(int, input().split()) s = input() print (sol([int(i) for i in s], p))
0
null
35,388,324,037,124
124
205
A=[list(map(int,input().split())) for i in range(3)] n=int(input()) b=[int(input()) for i in range(n)] for i in range(3): for j in range(3): for k in range(n): if A[i][j]==b[k]: A[i][j]=-1 if A[0][0]==-1 and A[1][1]==-1 and A[2][2]==-1: print("Yes") exit() elif A[0][2]==-1 and A[1][1]==-1 and A[2][0]==-1: print("Yes") exit() for i in range(3): total=0 if A[i].count(-1)==3: print("Yes") exit() for j in range(3): if A[j][i]==-1: total+=1 if total==3: print("Yes") exit() print("No")
board, dp = [], [] bingo = False for _ in range(3): board.append(list(map(int, input().split()))) dp.append([0, 0, 0]) N = int(input()) for _ in range(N): num = int(input()) for r in range(3): for c in range(3): if num == board[r][c]: dp[r][c] = 1 for i in range(3): if dp[i][0] == dp[i][1] == dp[i][2] == 1 or dp[0][i] == dp[1][i] == dp[2][i] == 1: bingo = True print("Yes") break if not bingo: if dp[0][0] == dp[1][1] == dp[2][2] == 1 or dp[0][2] == dp[1][1] == dp[2][0] == 1: print("Yes") else: print("No")
1
60,030,012,290,780
null
207
207
n, q = map(int , input().split()) queue = [] for _ in range(n): line = input().split() queue.append([line[0], int(line[1])]) class my_queue: def __init__(self, queue): self.queue = queue def enqueue(self, item): self.queue = self.queue + [item] self.queue.append(item) def dequeue(self): if len(self.queue) != 0: item = self.queue[0] self.queue = self.queue[1:] else : item = None return item time = 0 finish_task = [] while(len(queue) != 0): item = queue.pop(0) if item == None: break elif item[1] <= q: time += item[1] finish_task.append([item[0], time]) else : time += q queue.append([item[0], item[1] - q]) for item in finish_task: print(item[0], item[1])
import sys input = sys.stdin.readline n, k = map(int,input().split()) A = list(map(int,input().split())) S = [A[0] % k] for i in range(1, n): S.append((A[i]+S[-1]) % k) # print(S) D = {} S = [0] + S ans = 0 for i in range(len(S)): key = (S[i] - i) % k if i >= k: j = i-k key1 = (S[j] - j) % k D[key1] -= 1 try: ans += D[key1] # print(i, j, D[key1]) except: ans += 0 try: D[key] += 1 except: D[key] = 1 for j in range(max(0, len(S)-k), len(S)): key1 = (S[j] - j) % k D[key1] -= 1 try: ans += D[key1] # print(i, j, D[key1]) except: ans += 0 # print(D) print(ans)
0
null
68,483,206,314,016
19
273
s = raw_input() p = raw_input() slen = len(s) plen = len(p) flag = False for i in xrange(len(s)): j = 0 while(j < plen and s[(i+j)%slen] == p[j]): j += 1 if (j == plen): flag = True if(flag): print "Yes" else: print "No"
str1 = raw_input() str2 = raw_input() n = len(str1) for i in xrange(n): j = i s = 0 for k in xrange(len(str2)): if (j+k) > (n-1): j = -k if str1[j+k] != str2[k]: s += 1 if s == 0: print "Yes" break else: print "No"
1
1,742,802,489,022
null
64
64
import sys sys.setrecursionlimit(10**7) input = sys.stdin.readline n,k = map(int, input().split()) m = n % k m = 0 if m == 0 else abs(m - k) print(min(n, m))
from bisect import bisect_left from itertools import accumulate n, m = map(int, input().split()) hands = [int(x) for x in input().split()] increasing = sorted(hands) hands = list(reversed(increasing)) xs = [0] + list(accumulate(hands)) max_x = 2 * 10**5 + 1 min_x = 0 def canGet(x): count = 0 for hand in increasing: idx = bisect_left(increasing, x-hand) count += n - idx return count >= m while max_x - min_x > 1: # left mid = (max_x + min_x) // 2 if canGet(mid): min_x = mid else: max_x = mid ans = 0 count = 0 for Ai in hands: idx = bisect_left(increasing, min_x-Ai) ans += Ai*(n-idx) + xs[n-idx] count += n-idx print(ans-(count-m)*min_x)
0
null
73,709,324,061,464
180
252
h,w,m = map(int,input().split()) hw = [list(map(int,input().split())) for _ in range(m)] hlist = [0]*(h+1) wlist = [0]*(w+1) for x,y in hw: hlist[x]+=1 wlist[y]+=1 hmax=max(hlist) h_index = {n for n, v in enumerate(hlist) if v == hmax} wmax=max(wlist) w_index = {n for n, v in enumerate(wlist) if v == wmax} count = sum(x in h_index and y in w_index for x,y in hw) print(hmax+wmax-(len(h_index)*len(w_index)==count))
import sys H, W, M = map(int, sys.stdin.readline().strip().split()) h = [0] * H w = [0] * W # g = [[False] * W for _ in range(H)] S = set() for _ in range(M): Th, Tw = map(int, sys.stdin.readline().strip().split()) h[Th - 1] += 1 w[Tw - 1] += 1 # g[Th - 1][Tw - 1] = True S.add((Th - 1, Tw - 1)) max_h = max(h) max_w = max(w) ans = max_h + max_w index_h = [n for n, v in enumerate(h) if v == max_h] index_w = [n for n, v in enumerate(w) if v == max_w] sub = 1 # for ih in index_h: # for iv in index_w: # if g[ih][iv] is False: # sub = 0 # break # else: # continue # break # ans -= sub # print(ans) for ih in index_h: for iv in index_w: if not (ih, iv) in S: print(ans) exit() print(ans - 1)
1
4,719,143,168,282
null
89
89
# -*- coding: utf-8 -*- def main(): import sys input = sys.stdin.readline n, k = map(int, input().split()) a = list(map(int, input().split())) # See: # https://www.youtube.com/watch?v=ENSOy8u9K9I&feature=youtu.be # KeyInsight: # ็งปๅ‹•: ๅ‘จๆœŸ็š„ใงใฏใชใ„ + ๅ‘จๆœŸ็š„ใซ็นฐใ‚Š่ฟ”ใ™ใฎๅˆ่จˆ # ๅฎŸ่ฃ…ไธŠใฎใƒใ‚คใƒณใƒˆ # ็ตŒ่ทฏใจ้ †็•ชใ‚’ๅˆฅใ€…ใซ็ฎก็†ใ™ใ‚‹ path = list() order = [-1 for _ in range(n + 1)] # -1: ๅˆฐ้”ใ—ใฆใ„ใชใ„ place = 1 # ๅˆฐ้”ใ—ใฆใ„ใชใ„็‚นใŒใ‚ใ‚‹้™ใ‚Šใ€ใƒซใƒผใƒ—ใ‚’็นฐใ‚Š่ฟ”ใ™ # = ๅŒใ˜็‚นใ‚’2ๅ›ž้€šใ‚‹ใพใง็นฐใ‚Š่ฟ”ใ™ while order[place] == -1: order[place] = len(path) # ใ‚ใ‚‹ๅ ดๆ‰€ใ‚’้€šใฃใŸ้ †็•ชใ‚’็ฎก็† path.append(place) # ็ตŒ่ทฏใ‚’ๆ›ดๆ–ฐ place = a[place - 1] # ๆฌกใฎ่กŒใๅ…ˆ # ๅ‘จๆœŸ: ๅŒใ˜็‚นใ‚’2ๅ›ž้€šใ‚‹ใพใงใซ่ฆใ—ใŸ็งปๅ‹•ๅ›žๆ•ฐ - ๅ‘จๆœŸใซๅ…ฅใ‚‹ใพใงใฎ็งปๅ‹•ๅ›žๆ•ฐ cycle = len(path) - order[place] before_cycle_count = order[place] if (k < before_cycle_count): print(path[k]) else: k -= before_cycle_count k %= cycle # ๅ‘จๆœŸ: ้€”ไธญใฎ็นฐใ‚Š่ฟ”ใ—้ƒจๅˆ†ใ‚’็œใ„ใฆใ€้€”ไธญใฎ้ƒจๅˆ†ใ ใ‘่จˆ็ฎ—ใ™ใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ print(path[before_cycle_count + k]) if __name__ == '__main__': main()
n,k = list(map(int,input().split())) A = list(map(int,input().split())) if k == 1:exit(print(A[0])) t = 0 dp = [-1] * n dp[0] = 0 flg = 0 for i in range(1,k+1): t = A[t]-1 if dp[t] != -1: flg = 1 break dp[t] = i if flg == 0: exit(print(t+1)) elif k == 2: exit(print(t+1)) l = i - dp[t] K = (k-dp[t])%l + dp[t] for i in range(n): if dp[i] == K:exit(print(i+1))
1
22,874,040,557,900
null
150
150
MOD=10**9+7 def facinv(N): fac,finv,inv=[0]*(N+1),[0]*(N+1),[0]*(N+1)#้šŽไน—ใƒ†ใƒผใƒ–ใƒซใ€้€†ๅ…ƒใƒ†ใƒผใƒ–ใƒซใ€้€†ๅ…ƒ fac[0]=1;fac[1]=1;finv[0]=1;finv[1]=1;inv[1]=1 for i in range(2,N+1): fac[i]=fac[i-1]*i%MOD inv[i]=MOD-inv[MOD%i]*(MOD//i)%MOD finv[i]=finv[i-1]*inv[i]%MOD return fac,finv,inv def COM(n,r): if n<r or r<0: return 0 else: return ((fac[n]*finv[r])%MOD*finv[n-r])%MOD n,k=map(int,input().split()) fac,finv,inv=facinv(2*n) if k>=n-1: print(COM(2*n-1,n-1)) else: ans=COM(2*n-1,n-1) for i in range(1,n-k): ans=(ans-COM(n,i)*COM(n-1,i-1)%MOD+MOD)%MOD print(ans%MOD)
def main(): n,m=map(int,input().split()) s=input() route=[] i=n visited=n while True: if i-m<=0: route.append(i) break else: flag=False for j in range(i-m,visited): if flag: break if s[j]=='0': route.append(i-j) visited=i-m i=j flag=True if flag: continue else: print(-1) exit() print(*route[::-1]) if __name__=='__main__': main()
0
null
102,710,741,215,248
215
274
a ,b = map(int,input().split()) li = list(map(int,input().split())) sum = 0 for i in li: sum += i if a > sum: print(a - sum) elif a == sum: print(0) else: print(-1)
import sys K=input() if int(K)%2==0: print(-1) sys.exit() S=int('7'*(len(K))) ans=len(K) K=int(K) for i in range(10**6): if S%K==0: print(ans) break else: S=(S*10)%K+7 ans+=1 else: print(-1)
0
null
19,106,055,504,158
168
97
K=input() r='ACL' for i in range(1,int(K)): r='ACL'+ r print(r)
x = int(input()) if(x < 1 or x > 5): print("") else: for i in range(x): print("ACL",end='')
1
2,187,675,821,398
null
69
69
# C from collections import Counter N = int(input()) S = [input() for _ in range(N)] countS = Counter(S) maxS = max(countS.values()) ans = [] for key,value in countS.items(): if value == maxS: ans.append(key) for a in sorted(ans): print(a)
import sys import itertools # import numpy as np import time import math import heapq from collections import defaultdict from collections import Counter sys.setrecursionlimit(10 ** 7) INF = 10 ** 18 MOD = 10 ** 9 + 7 read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines # map(int, input().split()) import sys import itertools # import numpy as np import time import math import heapq from collections import defaultdict from collections import Counter sys.setrecursionlimit(10 ** 7) INF = 10 ** 18 MOD = 10 ** 9 + 7 read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines # map(int, input().split()) def my_pow(base, n, mod): if n == 0: return 1 x = base y = 1 while n > 1: if n % 2 == 0: x *= x n //= 2 else: y *= x n -= 1 x %= mod y %= mod return x * y % mod N, K = map(int, input().split()) cnt = [0] * (K + 1) ans = 0 total = 0 for i in range(K, 0, -1): t = K // i now = my_pow(t, N, MOD) for j in range(i*2, K + 1, i): now -= cnt[j] cnt[i] = now ans += (now * i) % MOD print(ans % MOD)
0
null
53,472,906,269,152
218
176
(N,) = [int(x) for x in input().split()] brackets = [input() for i in range(N)] delta = [] minDelta = [] for s in brackets: balance = 0 mn = 0 for c in s: if c == "(": balance += 1 else: balance -= 1 mn = min(mn, balance) delta.append(balance) minDelta.append(mn) if sum(delta) == 0: # Want to get balance as high as possible # Take any positive as long there's enough balance to absorb their minDelta # Can sort since anything that works will only increase balance and the ones with worse minDelta comes later posIndices = [i for i in range(N) if delta[i] > 0] posIndices.sort(key=lambda i: minDelta[i], reverse=True) # At the top can take all with zero delta, just need to absorb minDelta eqIndices = [i for i in range(N) if delta[i] == 0] # When going back down, want to preserve existing balance as much as possible but take a hit for stuff that does need to use the balance to absorb minDelta negIndices = [i for i in range(N) if delta[i] < 0] negIndices.sort(key=lambda i: delta[i] - minDelta[i], reverse=True) balance = 0 for i in posIndices + eqIndices + negIndices: if balance + minDelta[i] < 0 or balance + delta[i] < 0: print("No") exit() balance += delta[i] assert balance == 0 print("Yes") else: print("No")
N = int(input()) A = [int(s) for s in input().split()] print(' '.join([str(item) for item in A])) for i in range(1, N): key = A[i] j = i - 1 while j >= 0 and A[j] > key: A[j+1] = A[j] j = j - 1 A[j+1] = key print(' '.join([str(item) for item in A]))
0
null
11,917,999,442,870
152
10
ele_and_tar = [] rs = [] flag1 = 1 flag2 = 0 while flag1: data = [int(x) for x in input().split()] if data == [0,0]: flag1 = 0 else: ele_and_tar.append(data) for i in range(len(ele_and_tar)): rs.append(0) for math in ele_and_tar: for i in range(1,math[0]+1): for j in range(i+1,math[0]+1): for k in range(j+1,math[0]+1): if (i + j + k) == math[1]: rs[flag2] = rs[flag2] + 1 flag2 = flag2 + 1 for out in rs: print(out)
while True: [n, x] = map(int, input().split()) if n==0 and x==0: break ans = 0 for a in range(1,n-1): for b in range(a+1,n): for c in range(b+1,n+1): if a+b+c==x: ans += 1 print(ans)
1
1,290,442,878,730
null
58
58
import sys input = sys.stdin.readline ins = lambda: input().rstrip() ini = lambda: int(input().rstrip()) inm = lambda: map(int, input().split()) inl = lambda: list(map(int, input().split())) out = lambda x: print('\n'.join(map(str, x))) x, y, z = inm() print(z, x, y)
num = list(map(int,input().split())) print(num[2],num[0],num[1])
1
38,194,696,164,310
null
178
178
n=int(input()) a=list(map(int,input().split())) thing={} ans=0 for i in range(n): if i-a[i] in thing: ans+=thing[i-a[i]] if i+a[i] in thing: thing[i+a[i]]+=1 else: thing[i+a[i]]=1 print(ans)
N = int(input()) A = [int(i) for i in input().split()] L = [A[i]+i+1 for i in range(N)] R = [i + 1 - A[i] for i in range(N)] from collections import defaultdict l = defaultdict(int) for i in L: l[i] += 1 ans = 0 for r in R: ans += l.get(r, 0) print(ans)
1
26,113,994,017,880
null
157
157
# -*- coding: utf-8 -*- """ D - Knight https://atcoder.jp/contests/abc145/tasks/abc145_d """ import sys def modinv(a, m): b, u, v = m, 1, 0 while b: t = a // b a -= t * b a, b = b, a u -= t * v u, v = v, u u %= m return u if u >= 0 else u + m def solve(X, Y): MOD = 10**9 + 7 if (X + Y) % 3: return 0 n = (X - 2 * Y) / -3.0 m = (Y - 2 * X) / -3.0 if not n.is_integer() or not m.is_integer() or n < 0 or m < 0: return 0 n, m = int(n), int(m) x1 = 1 for i in range(2, n + m + 1): x1 = (x1 * i) % MOD x2 = 1 for i in range(2, n+1): x2 = (x2 * i) % MOD for i in range(2, m+1): x2 = (x2 * i) % MOD ans = (x1 * modinv(x2, MOD)) % MOD return ans def main(args): X, Y = map(int, input().split()) ans = solve(X, Y) print(ans) if __name__ == '__main__': main(sys.argv[1:])
def cmb(n, r, p): if (r < 0) or (n < r): return 0 r = min(r, n - r) return fact[n] * factinv[r] * factinv[n-r] % p p = 10**9 + 7 N = 10**6 # N ใฏๅฟ…่ฆๅˆ†ใ ใ‘็”จๆ„ใ™ใ‚‹ fact = [1, 1] # fact[n] = (n! mod p) factinv = [1, 1] # factinv[n] = ((n!)^(-1) mod p) inv = [0, 1] # factinv ่จˆ็ฎ—็”จ for i in range(2, N+1): fact.append((fact[-1] * i) % p) inv.append((-inv[p % i] * (p // i)) % p) factinv.append((factinv[-1] * inv[-1]) % p) x,y=map(int,input().split()) if ((x+y)%3!=0): print(0) else: xx=(2*x-y)//3 yy=(2*y-x)//3 if (xx<0 or yy<0): print(0) else: ans=cmb(xx+yy,xx,p) print(ans)
1
150,228,284,345,308
null
281
281
def read_int(): return int(input().strip()) def read_ints(): return list(map(int, input().strip().split(' '))) def solve(): S = input().strip() if S[2] == S[3] and S[4] == S[5]: return 'Yes' return 'No' if __name__ == '__main__': print(solve())
import bisect N = int(input()) numbers = [int(i) for i in input().split()] numbers = sorted(numbers) counter = 0 for a in range(N): for b in range(a+1, N): c = bisect.bisect_left(numbers, numbers[a] + numbers[b]) if c > b: counter += c - b -1 print(counter)
0
null
107,334,355,853,150
184
294
n = int(input()) p = tuple(map(int,input().split())) q = tuple(map(int,input().split())) import itertools li = [i for i in range(1,n+1)] li2 = list(itertools.permutations(li)) cnt = 0 for i in li2: cnt +=1 if i == p: a = cnt if i == q: b = cnt print(abs(a-b))
import itertools n=[x for x in range(1,int(input())+1)] P=list(map(int, input().split())) Q=list(map(int, input().split())) for i, a in enumerate(itertools.permutations(n), start=1): if list(a)==P: p=i if list(a)==Q: q=i print(abs(p-q))
1
101,002,417,826,652
null
246
246
import sys from scipy.sparse import csr_matrix from scipy.sparse.csgraph import floyd_warshall n,m,l = map(int, input().split()) d=[[sys.maxsize]*(n) for _ in range(n)] e=[[sys.maxsize]*(n) for _ in range(n)] for i in range(n): d[i][i] = 0 e[i][i] = 0 for _ in range(m): a,b,c = map(int, input().split()) d[a-1][b-1]=c d[b-1][a-1]=c d=floyd_warshall(csr_matrix(d)) for i in range(n): for j in range(n): if d[i][j] <= l: e[i][j] = 1 e=floyd_warshall(csr_matrix(e)) q=int(input()) for _ in range(q): s,t = map(int, input().split()) if e[s-1][t-1] == sys.maxsize: print(-1) else: print(int(e[s-1][t-1]-1))
import numpy as np from scipy.sparse.csgraph import floyd_warshall def main(): N, M, L = map(int, input().split()) graph = [[0] * N for _ in range(N)] for _ in range(M): s, t, w = map(int, input().split()) if w <= L: graph[s-1][t-1] = graph[t-1][s-1] = w graph = floyd_warshall(graph, directed=False) graph = floyd_warshall(graph <= L, directed=False) graph[np.isinf(graph)] = 0 Q = int(input()) ans = [] for _ in range(Q): s, t = map(int, input().split()) ans.append(int(graph[s-1][t-1]) - 1) print(*ans, sep='\n') if __name__ == '__main__': main()
1
173,341,720,446,078
null
295
295
n=int(input()) table=[[0]*n for i in range(60)] for i,v in enumerate(map(int,input().split())): for j in range(60): table[j][n-1-i]=v%2 v//=2 if v==0:break #print(*table,sep='\n') ans=0 mod1,mod2=10**9+7,998244353 mod=mod1 a=1 for t in table: o,z=0,0 for v in t: if v: o+=1 ans=(ans+z*a)%mod else: z+=1 ans=(ans+o*a)%mod a=a*2%mod print(ans)
from collections import deque q=deque() n=int(input()) for i in range(n): cmd=input() if cmd=="deleteFirst": try: q.popleft() except: pass elif cmd=="deleteLast": try: q.pop() except: pass else: cmd,number=cmd.split() if cmd=="delete": try: q.remove(number) except: pass elif cmd=="insert": q.appendleft(number) print(*q)
0
null
61,192,334,255,042
263
20
mod = 10**9 + 7 X, Y = map(int, input().split()) if (X+Y) % 3 != 0: print(0) exit() a = (2*Y-X)//3 b = (2*X-Y)//3 if a < 0 or b < 0: print(0) exit() m = min(a, b) ifact = 1 for i in range(2, m+1): ifact = (ifact * i) % mod ifact = pow(ifact, mod-2, mod) fact = 1 for i in range(a+b, a+b-m, -1): fact = (fact * i) % mod print(fact*ifact % mod)
n=int(input()) ans =0 for i in range(2,int(n**0.5)+5): cnt = 0 while n%i==0: n//=i cnt += 1 s=1 while cnt-s>=0: cnt -= s s +=1 ans += 1 if n!=1:ans+=1 print(ans)
0
null
83,738,994,129,250
281
136
n = int(input()) d = [] for i in range(n): d.append(list(map(int,input().split()))) cnt = 0 flag = 0 for i in range(n): if d[i][0] == d[i][1]: cnt = cnt + 1 else: cnt = 0 if cnt == 3: flag = 1 if flag == 1: print("Yes") else: print("No")
T1, T2 = map(int, input().split()) A1, A2 = map(int, input().split()) B1, B2 = map(int, input().split()) d1 = T1*(A1-B1) d2 = T2*(A2-B2) if d1>d2: d1 *= -1 d2 *= -1 if (d1>0 and d1+d2>0) or (d1<0 and d1+d2<0): print(0) elif d1+d2==0: print("infinity") else: v = (abs(d1)//(d1+d2)) ans = v*2 pos = d1+v*(d1+d2) if pos<0 and pos+d2>=0: ans += 1 print(ans)
0
null
67,117,893,907,350
72
269
X = int(input()) print("YNeos"[not(X>=30)::2])
X = int(input()) ans="No" if X>=30: ans='Yes' print(ans)
1
5,681,327,266,450
null
95
95
import sys def main(): for line in sys.stdin: print(int(line.strip())**3) if __name__ == "__main__": main()
n = int(input()) def dfs(s): if len(s) == n: print(s) else: max_list = [] for _ in s: max_list.append(ord(_)) for i in range(ord("a"),max(max_list) + 2): s = s + chr(i) dfs(s) s = s[:len(s) - 1] dfs("a")
0
null
26,289,969,095,182
35
198
from collections import deque def bfs(graph, n, s): visited = [0]*n # ็งปๅ‹•ๅ›žๆ•ฐใ‚’ไฟๆŒ visited[s] = 1 # ้ƒจๅฑ‹1ใฎๆŽข็ดขใฏๅฎŒไบ†ใจใ—ใฆใ„ใ‚‹ q = deque([s]) # ๆŽข็ดข็”จใซdequeใ‚’่จญๅฎšใ—ใ€ๅˆๆœŸไฝ็ฝฎใจใ—ใฆs(0)ใ‚’่จญๅฎšใ™ใ‚‹ while q: node = q.popleft() # ๅ…ˆ้ ญใ‹ใ‚‰้ †็•ชใซๅฎŸ่กŒ for i in graph[node]: # ๅ„้ƒจๅฑ‹ใ‹ใ‚‰่กŒใ‘ใ‚‹ๅˆฅใฎ้ƒจๅฑ‹ใ‚’็ขบ่ช if not visited[i]: # ๅ€คใŒ"0"ใฎๅ ดๅˆใซๅ‡ฆ็†ใ‚’ๅฎŸ่กŒ visited[i] = node + 1 # ้ƒจๅฑ‹1ใ‹ใ‚‰ใฎ็งปๅ‹•ๅ›žๆ•ฐใ‚’ไฟๆŒใ™ใ‚‹ q.append(i) # ็งปๅ‹•ๅ…ˆใฎ้ƒจๅฑ‹ใ‚’qใซๆ ผ็ดใ—ใ€ๆŽข็ดขใ‚’็ถ™็ถšใ™ใ‚‹ return visited n,m = map(int, input().split()) graph = [[] for _ in range(n)] for _ in range(m): a,b = map(lambda x: int(x)-1, input().split()) graph[a].append(b) graph[b].append(a) visited = bfs(graph, n, 0)[1:] # ๅน…ๅ„ชๅ…ˆๆŽข็ดขใ‚’่กŒใ„ใ€้ƒจๅฑ‹1ไปฅๅค–ใฎ็ตๆžœใ‚’ๅ–ๅพ—ใ™ใ‚‹ if all(visited): print("Yes") print(*visited, sep="\n") else: print("No")
tmp = [int(e) for e in input().split()] a = tmp[0] b = tmp[1] c = tmp[2] d = tmp[3] res = a * c tmp = a * d if tmp > res: res = tmp tmp = b * c if tmp > res: res = tmp tmp = b * d if tmp > res: res = tmp print(res)
0
null
11,890,830,267,810
145
77
from collections import deque n = int(input()) queue = deque([[1],[2],[3],[4],[5],[6],[7],[8],[9]]) # ๅˆๆœŸๅŒ–_ใ“ใ“ใงใฏๆญฃใฎไธ€ๆกๆ•ดๆ•ฐใจใ—ใฆ'1'๏ฝž'9'ใ‚’่จญ็ฝฎ ptns = [] # ใƒ‘ใ‚ฟใƒผใƒณๅ€™่ฃœใฎๅ™จ_ๅˆๆœŸๅŒ– while queue: # queueใŒ็ฉบใซใชใ‚‹ใพใงใƒซใƒผใƒ— tmp = queue.popleft() # ใƒ‘ใ‚ฟใƒผใƒณใฎๅ€™่ฃœใ‚’ popleft if len(ptns) < n: # ptnsใฎ่ฆ็ด ๆ•ฐใŒ'n'ๅ€‹ๆœชๆบ€ใชใ‚‰ใฐ append ptns.append(tmp) else: break # ไปฅไธ‹ใ€tmpใซๅ„ใ€…tmp[-1]-1ใ€tmp[-1]ใ€tmp[-1]+1ใ‚’appendใ™ใ‚‹ใŸใ‚ใฎๆกไปถ if tmp[-1] != 0: queue.append(tmp + [tmp[-1] -1]) queue.append(tmp + [tmp[-1]]) if tmp[-1] != 9: queue.append(tmp + [tmp[-1] +1]) # ใƒชใ‚นใƒˆ = map(str, ใƒชใ‚นใƒˆ) โ† intใฎใƒชใ‚นใƒˆใ‹ใ‚‰strใฎmapใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใธๅค‰ๆ› print(''.join(map(str, ptns[-1])))
def lu(n): L.append(n) a=n%10 if len(str(n))>11: return 1 if a==0: lu(n*10+1) lu(n*10) elif a==9: lu(n*10+9) lu(n*10+8) else: lu(n*10+1+a) lu(n*10+a-1) lu(n*10+a) L=list() lu(1) lu(2) lu(3) lu(4) lu(5) lu(6) lu(7) lu(8) lu(9) L=sorted(set(L)) k=int(input()) print(L[k-1])
1
39,957,432,322,078
null
181
181
while True: l=list(input()) if l==["0"]: break print(sum(map(int,l)))
while True: s = input() if len(s) == 1 and s[0] == '0': break total = 0 for i in range(0,len(s)): total += int(s[i]) print("%d" % total)
1
1,571,972,206,424
null
62
62
a,b,c=map(int,input().split()) d={"a":a,"b":b,"c":c} for i in range(int(input())+1): if d["a"]<d["b"]<d["c"]: print("Yes") exit() elif d["a"]>=d["c"]: d["c"]*=2 elif d["a"]>=d["b"]: d["b"]*=2 elif d["b"]>=d["c"]: d["c"]*=2 print("No")
# -*- coding: utf-8 -*- def main(): import sys input = sys.stdin.readline a, b, c = map(int, input().split()) k = int(input()) count = 0 while True: if a >= b: b *= 2 count += 1 else: break while True: if b >= c: c *= 2 count += 1 else: break if count <= k: print('Yes') else: print('No') if __name__ == '__main__': main()
1
6,887,487,725,810
null
101
101
S = input() if S[-1] == 's': print(S,'es',sep='') else: print(S,'s',sep='')
def main(): H, W, M = [int(s) for s in input().split()] cols = [0] * W rows = [0] * H bombs = set() for _ in range(M): x, y = [int(s)-1 for s in input().split()] bombs.add((x, y)) cols[y] += 1 rows[x] += 1 sc = sorted([(c, i) for i, c in enumerate(cols)], reverse=True) sr = sorted([(c, i) for i, c in enumerate(rows)], reverse=True) best = 0 for v1, c in sc: for v2, r in sr: if v1 + v2 <= best: break score = v1 + v2 if (r, c) in bombs: score -= 1 best = max(best, score) print(best) main()
0
null
3,519,929,330,340
71
89
from typing import List # ไบบiใฎ่จผ่จ€ใ‚’ไบบjใซๅฏพใ™ใ‚‹่จผ่จ€ใ‚’ใƒชใ‚นใƒˆใงๆ ผ็ดใ€‚1:ๆญฃ็›ด่€…, 0:ไธ่ฆชๅˆ‡, -1:่จ€ๅŠใชใ— def io_info() -> List[List[int]]: N = int(input()) res = [[-1] * N for _ in range(N)] for n in range(N): a = int(input()) for _ in range(a): x, y = map(int, input().split()) res[n][x-1] = y return res def main(): infos = io_info() n = len(infos) ans = 0 for i in range(1 << n): d = [0] * n for j in range(n): # iใฎj+1ใƒ“ใƒƒใƒˆ็›ฎใŒ1ใ‹ใฉใ†ใ‹,d[j]ใŒๆญฃ็›ดใชใ‚‰1ใ‚’ๅ‰ฒใ‚Šๅฝ“ใฆใ‚‹ if (i >> j) & 1: d[j] = 1 ok = True for j in range(n): if d[j]: for k in range(n): if infos[j][k] == -1: continue if infos[j][k] != d[k]: ok = False if ok: ans = max(ans, bin(i).count("1")) print(ans) if __name__ == '__main__': main()
n = int(input()) a = [] for i in range(n): temp = [] temp_temp = [] temp.append(int(input())) for j in range(temp[0]): temp_temp.append(list(map(int,input().split()))) temp.append(temp_temp) a.append(temp) ans = 0 for i in range(2**n): ans_t = 0 id_list =list(bin(i))[2:] id_list = [int(x) for x in id_list] while len(id_list) != n: id_list.insert(0,0) for j in range(n): flag = 0 if id_list[j] == 0: continue else: for k in range(a[j][0]): if id_list[a[j][1][k][0]-1] != a[j][1][k][1]: flag = 1 break if flag == 1: ans_t = 0 break else: ans_t += 1 if ans_t > ans: ans = ans_t print(ans)
1
122,232,850,353,120
null
262
262
a = [int(i) for i in input().split()] a.sort() print(*a,sep=" ")
a,b,c=map(int,input().split()) if a>b: n=a a=b b=n else: pass if b>c: n=b b=c c=n else: pass if a>b: n=a a=b b=n else: pass print(a,b,c)
1
412,327,951,460
null
40
40
def main(): n = int(input()) if n == 0: print("Yes") return slup = [] sldown = [] for i in range(n): height, mi = 0, 0 for si in input(): if si == "(": height += 1 else: height -= 1 mi = min(mi, height) if height >= 0: slup.append([mi, height]) else: sldown.append([mi-height, -height]) slup.sort(key = lambda x: -x[0]) sldown.sort(key = lambda x: -x[0]) if sum([si[1] for si in slup]) + sum([-si[1] for si in sldown]): print("No") return h = 0 for si in slup: if h+si[0] < 0: print("No") return h += si[1] h = 0 for si in sldown: if h+si[0] < 0: print("No") return h += si[1] print("Yes") if __name__ == "__main__": main()
import sys input = sys.stdin.readline def I(): return int(input()) def MI(): return map(int, input().split()) def LI(): return list(map(int, input().split())) def main(): mod=10**9+7 from math import gcd a,b=MI() print((a*b)//gcd(a,b)) main()
0
null
68,685,693,162,140
152
256
def main(): n, m = map(int, input().split()) a_lst = list(map(int, input().split())) total = sum(a_lst) if total > n: ans = -1 else: ans = n - total print(ans) if __name__ == "__main__": main()
from collections import deque h,w=map(int,input().split()) s=[input() for _ in range(h)] #ใƒžใƒƒใƒ— vi=[ [-1 for _ in range(w)] for _ in range(h)]#visit st=deque() d=[[0,1],[-1,0],[1,0],[0,-1]] mx=0 for i in range(h): for j in range(w): vi=[ [-1 for _ in range(w)] for _ in range(h)] st.append([i,j,0]) while st: h1,w1,k=st.popleft() if 0<=h1<h and 0<=w1<w and vi[h1][w1]==-1 and s[h1][w1]==".": vi[h1][w1]=k for m in d: st.append([h1+m[0],w1+m[1],k+1]) for m in vi: mx=max(mx,max(m)) print(mx)
0
null
63,466,526,217,472
168
241
if __name__ == "__main__": n = int(input()) ops = [] words = [] for _ in range(n): op, word = input().split() ops.append(op) words.append(word) db = set() for op, word in zip(ops, words): if op=='insert': db.add(word) else: if word in db: print("yes") else: print("no")
import math h=[int(input())for _ in range(2)] n=int(input()) print(math.ceil(n/max(h)))
0
null
44,361,872,555,628
23
236
n, x, m = map(int, input().split()) flag = [False]*m a = [] while not flag[x]: flag[x] = True a.append(x) x = pow(x, 2, m) loop_start_idx = a.index(x) loop_len = len(a) - loop_start_idx loop_count = (n - loop_start_idx)//loop_len loop_amari = (n-loop_start_idx)%loop_len ans = sum(a[:loop_start_idx]) ans += sum(a[loop_start_idx:])*loop_count ans += sum(a[loop_start_idx: loop_start_idx + loop_amari]) print(ans)
n = int(input()) taro = 0 hanako = 0 for i in range(n): animals = input().split() taro_animal = animals[0] hanako_animal = animals[1] if taro_animal > hanako_animal: taro += 3 elif taro_animal < hanako_animal: hanako +=3 else: taro += 1 hanako += 1 print(taro, hanako)
0
null
2,399,460,399,868
75
67
# coding: utf-8 N,K=map(int,input().split()) A=list(map(int,input().split())) MOD=10**9+7 ans=1 if K==N: for i in range(N): ans*=A[i] if ans>0: ans=ans%MOD elif max(A)<=0 and K%2==1: A.sort(reverse=True) for i in range(K): ans*=A[i] if ans>0: ans=ans%MOD else: Ap=[] Am=[] for i in range(N): if A[i]>0: Ap.append(A[i]) else: Am.append(A[i]) Ap.sort(reverse=True) Am.sort() if K%2==1 and Ap: ans*=Ap.pop(0) P=[] for i in range(len(Ap)//2): P.append(Ap[i*2]*Ap[i*2+1]) for i in range(len(Am)//2): P.append(Am[i*2]*Am[i*2+1]) P.sort(reverse=True) for i in range(K//2): ans*=P[i] ans=ans%MOD print(ans%MOD)
N = int(input()) S,T=input().split() ST = "".join([S[i]+T[i] for i in range(N)]) print(ST)
0
null
60,791,172,746,360
112
255
N = int(input()) X = list(map(int, input().split())) ans = 100 ** 100 for p in range(1, 101): now = 0 for x in X: now += (p - x) ** 2 ans = min(ans, now) print(ans)
x,y=map(int,input().split()) ans=0 if x<=3: ans+=(4-x)*100000 if y<=3: ans+=(4-y)*100000 if ans==600000: ans=1000000 print(ans)
0
null
103,063,631,312,490
213
275
n = int(input()) if n%2 == 0: ans = n//2-1 else: ans = n//2 print(ans)
n = int(input()) x = 0 if n%2 == 0: x = 1 print(n // 2 - x)
1
153,061,057,832,552
null
283
283
import sys input = sys.stdin.readline """ """ s, w = map(int, input().split()) if w >= s: print("unsafe") else: print("safe")
score = list(map(int,input().split())) if score[0] <= score[1]: print('unsafe') else: print('safe')
1
29,347,602,868,100
null
163
163
#create date: 2020-07-02 22:54 import sys stdin = sys.stdin from collections import deque def ns(): return stdin.readline().rstrip() def ni(): return int(ns()) def na(): return list(map(int, stdin.readline().split())) def main(): n, m = na() g = [[] for i in range(n)] for i in range(m): a, b = na() g[a-1].append(b-1) g[b-1].append(a-1) q = deque([0]) d = [-1] * n d[0] = 0 while q: v = q.popleft() for w in g[v]: if d[w] != -1: continue d[w] = v q.append(w) if -1 in d: print("No") else: print("Yes") for di in d[1:]: print(di+1) if __name__ == "__main__": main()
import copy N, M = map(int, input().split()) res = [[] for i in range(N+5)] for i in range(M) : a,b = map(int, input().split()) a -= 1 b -= 1 res[a].append(b) res[b].append(a) pre = [-1] * N dist = [-1] * N d = 1 dl = [] pre[0] = 0 dist[0] = 0 dl.append(0) while(len(dl) != 0) : a = dl[0] dl.pop(0) for i in res[a] : if(dist[i] != -1): continue dist[i] = dist[a] + 1 pre[i] = a dl.append(i) for i in range(N) : if(i == 0) : print("Yes") continue print(pre[i] + 1)
1
20,485,132,796,988
null
145
145
A=[0 for i in range(45)] A[0]=1 A[1]=1 for i in range(2,45): A[i]=A[i-1]+A[i-2] B=int(input()) print(A[B])
import numpy as np from numba import njit @njit def update(a): n = len(a) b = np.zeros_like(a) for i,x in enumerate(a): l = max(0, i-x) r = min(n-1, i+x) b[l] += 1 if r+1 < n: b[r+1] -= 1 b = np.cumsum(b) return b n,k = map(int, input().split()) a = np.array(list(map(int, input().split()))) for _ in range(k): a = update(a) if np.all(a==n): break print(' '.join(map(str, a)))
0
null
7,742,296,078,400
7
132
from math import factorial N, M = map(int, input().split()) if N <= 1: combN = 0 else: combN = factorial(N) // (factorial(N - 2) * factorial(2)) if M <= 1: combM = 0 else: combM = factorial(M) // (factorial(M - 2) * factorial(2)) print(combN + combM)
N, M = map(int, input().split()) print(int(N * (N - 1) / 2 + M * (M - 1) / 2))
1
45,368,951,464,032
null
189
189
def g(x): S=format(x,"22b") cnt=0 for i in range(20): if S[-1-i]=="1": cnt=cnt+1 return x%cnt n=int(input()) X=input() a=0 for i in range(n): if X[i]=="1": a=a+1 #ๆœ€ๅˆใซๅ‰ฒใ‚‹ใฎใฏ a-1 or a+1 -> a==0,1 ใซๆณจๆ„ if a==0: for i in range(n): print(1) exit() if a==1: if X[-1]=="1": for i in range(n-1): print(2) print(0) exit() else: for i in range(n-1): if X[i]=="1": print(0) else: print(1) print(2) exit() D=[0]*n E=[0]*n D[0]=1%(a-1) E[0]=1%(a+1) b=int(X[-1])%(a-1) c=int(X[-1])%(a+1) for i in range(n-1): D[i+1]=(D[i]*2)%(a-1) E[i+1]=(E[i]*2)%(a+1) if X[-2-i]=="1": b=(b+D[i+1])%(a-1) c=(c+E[i+1])%(a+1) for i in range(n): if X[i]=="1": x=(b+a-1-D[-1-i])%(a-1) ans=1 while x!=0: x=g(x) ans=ans+1 print(ans) else: x=(c+E[-1-i])%(a+1) ans=1 while x!=0: x=g(x) ans=ans+1 print(ans)
def search(n): res = 0 while n: n %= bin(n).count("1") res += 1 return res def main(): n = int(input()) x = list(input()) x_decimal = int("".join(map(str, x)), 2) first_mod = sum(map(int, list(x))) increase_mod = first_mod + 1 increase_base = x_decimal % increase_mod # ๅ่ปขใ•ใ›ใ‚‹ๆกใŒๅ…ƒใ€…0ใ ใฃใŸๅ ดๅˆ decrease_mod = first_mod - 1 if decrease_mod: decrease_base = x_decimal % decrease_mod # ๅ่ปขใ•ใ›ใ‚‹ๆกใŒๅ…ƒใ€…1ใ ใฃใŸๅ ดๅˆ for i in range(n): cnt = 0 if x[i] == "1" and decrease_mod: cnt = 1 + search((decrease_base - pow(2, n - i - 1, decrease_mod)) % decrease_mod) elif x[i] == "0": cnt = 1 + search((increase_base + pow(2, n - i - 1, increase_mod)) % increase_mod) print(cnt) if __name__ == '__main__': main()
1
8,262,901,820,480
null
107
107
N = int(input()) hash = {} end = int(N ** (1/2)) for i in range(2, end + 1): while N % i == 0: hash[i] = hash.get(i, 0) + 1 N = N // i if N != 1: hash[i] = hash.get(i, 0) + 1 count = 1 res = 0 for i in hash.values(): count = 1 while i >= count: res += 1 i -= count count += 1 print(res)
from collections import Counter def prime_fact(n): P = [] for i in range(2, int(n**0.5)+1): while n % i == 0: P.append(i) n //= i if n != 1: P.append(n) return P n = int(input()) cnt = Counter(prime_fact(n)) #print(cnt) ans = 0 for c in cnt.values(): tmp = 1 while c >= tmp: c -= tmp ans += 1 tmp += 1 print(ans)
1
16,827,544,748,366
null
136
136
N = int(input()) A = list(map(int, input().split())) manzoku = 1 hakai = 0 for i in range(N): if A[i] == manzoku: manzoku += 1 else: hakai += 1 if hakai == N: print(-1) else: print(hakai)
n = input() renga = list(map(int, input().split(" "))) ni = 1 remains = 0 for r in renga: if ni == r: ni += 1 remains += 1 if ni == 1: print(-1) else: print(len(renga) - remains)
1
114,931,125,755,528
null
257
257
a, b = map(int, input().split()) if a < 10 and b < 10: print(a * b) else: print('-1')
a, b = input().split() if (len(a) >1) or (len(b) >1): print(-1) else: print(int(a)*int(b))
1
157,973,643,942,468
null
286
286
ch = raw_input() print(chr(ord(ch) + 1))
c = input() d = ord(c) e = chr(d+1) print(e)
1
92,081,070,663,738
null
239
239
K = int(input()) A, B = map(int, input().split()) x = 1000//K for n in range(x+1): if (n * K >= A) and (n * K <= B): print('OK') break if n == x: print('NG')
a, b = int(input()), int(input()) if 1 not in [a, b]: print(1) elif 2 not in [a, b]: print(2) else: print(3)
0
null
69,023,894,183,510
158
254
def count_divisors(num, start, end): """ num: positive int start: positive int end: positive int(end > start) returns numbers of devisors of i between start and end >>> count_divisors(80, 5, 14) 3 >>> count_divisors(12, 1, 4) 4 """ count = 0 for i in range(start, end+1): if num % i == 0: count += 1 return count if __name__ == '__main__': #import doctest #doctest.testmod() (a, b, c) = [int(i) for i in input().split(' ')] print(count_divisors(c, a, b))
#!/usr/bin/python3 # -*- coding:utf-8 -*- def main(): h, w = map(int, input().split()) grid = [] for _ in range(h): grid.append(list(input().strip())) dp = [[0]*(w) for _ in range(h)] def cals_score(i, j): score = 10**9 + 7 if i >= 1: score = dp[i-1][j] + (1 if grid[i-1][j]!=grid[i][j] else 0) if j >= 1: score = min(score, dp[i][j-1] + (1 if grid[i][j-1]!=grid[i][j] else 0)) return score dp[0][0] = 1 for i in range(1, w): dp[0][i] = cals_score(0, i) for i in range(1, h): dp[i][0] = cals_score(i, 0) for i in range(1, h): for j in range(1, w): dp[i][j] = cals_score(i, j) print(dp[-1][-1]//2 + dp[-1][-1]%2 * (grid[0][0]=='#')) if __name__=='__main__': main()
0
null
25,062,599,451,940
44
194
# -*- coding: utf-8 -*- def main(): S = input() T = input() n = len(S) ans = 0 for i in range(n): if S[i] != T[i]: ans += 1 print(ans) if __name__ == "__main__": main()
n=int(input()) a=list(map(int,input().split())) ans = [] for i in range(n): ans.append(0) for i in range(n): ans[a[i]-1] = i +1 for i in range(len(ans)): print(ans[i], end=" ")
0
null
95,167,815,688,242
116
299
n, k = map(int, input().split()) a_i = list(map(int, input().split())) def f(n): cnt = 0 for a in a_i: cnt += (a - 1) // n if cnt > k: return False else: return True l, r = 0, max(a_i) while True: if r - l <= 1: break val = (l + r) // 2 if f(val) == True: r = val else: l = val print(r)
n,k = map(int,input().split()) A = list(map(int,input().split())) l = min(A)//(k+1) r = max(A) m = (l+r)//2+1 while 1: m = (l+r)//2 if m==0: m=1 break #mไปฅไธ‹ใฎ้•ทใ•ใซใงใใ‚‹ใ‹ๅˆคๅฎš # A_long = A[A>m] n= sum( [ int((A[i]-0.1)//(m)) for i in range(len(A)) ] ) # print(l,m,r,n) if n<=k: if r==m: break r=m-1 else: if l==m: m+=1 break l=m+1 print(m)
1
6,574,693,235,260
null
99
99
#!/usr/bin/python3 # -*- coding:utf-8 -*- import numpy def main(): n = int(input()) la, lb = [], [] for _ in range(n): a, b = map(int, input().split()) la.append(a) lb.append(b) la.sort(), lb.sort() if n % 2 == 0: s = (n-1)//2 e = s + 2 ma = sum(la[s:e]) / 2.0 mb = sum(lb[s:e]) / 2.0 print(int(2 * (mb - ma) + 1)) if n % 2 == 1: ma = la[n//2] mb = lb[n//2] print(mb - ma + 1) if __name__=='__main__': main()
# -*- coding: utf-8 -*- def main(): N = int(input()) case = [] for i in range(1, 10): for j in range(1, 10): case.append(i * j) if N in case: ans = 'Yes' else: ans = 'No' print(ans) if __name__ == "__main__": main()
0
null
88,201,958,599,580
137
287
from collections import Counter N = int(input()) A = list(map(int, input().split())) L = dict(Counter([i+A[i] for i in range(N)])) R = dict(Counter([i-A[i] for i in range(N)])) cnt = 0 for x in L: cnt += L[x]*R.get(x, 0) print(cnt)
import math def f(n): pop = 0 ntemp = n for i in range(int(math.log2(n)) + 1): pop += ntemp % 2 ntemp //= 2 return n % pop n = int(input()) S = input() popcount = 0 for i in range(n): popcount += int(S[i]) # 1ใŒ1ๅ€‹ใ—ใ‹ใชใ„ๅ ดๅˆใฎไพ‹ๅค–ๅ‡ฆ็† if popcount == 1: if S[-1] == 1: ans = [2] * n ans[-1] = 0 else: ans = [1] * n ans[-1] = 2 for i in range(n): if S[i] == '1': ans[i] = 0 for i in range(n): print(ans[i]) exit() Sint = int(S, 2) remminus = Sint % (popcount - 1) remplus = Sint % (popcount + 1) # 1,2,4,...ใฎไฝ™ใ‚Šใฎใƒชใ‚นใƒˆใ‚’ๆบ–ๅ‚™ remlistminus = [] remlistplus = [] temp = 1 for i in range(n): remlistminus.append(temp) temp = (temp * 2) % (popcount - 1) temp = 1 for i in range(n): remlistplus.append(temp) temp = (temp * 2) % (popcount + 1) remlistminus.reverse() remlistplus.reverse() ans = [] for i in range(n): count = 1 if S[i] == '0': rem = (remplus + remlistplus[i]) % (popcount + 1) while rem > 0: rem = f(rem) count += 1 else: rem = (remminus - remlistminus[i]) % (popcount - 1) while rem > 0: rem = f(rem) count += 1 ans.append(count) for i in range(n): print(ans[i])
0
null
17,182,500,501,738
157
107
import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline import sys # sys.stdout.write(str(n)+"\n") MODULO = 1000000007 # INF = 1e18+10 def gcd(a, b): while b != 0: a, b = b, a % b return a n = int(input()) a_b = [() for i in range(n)] m_b_a = [() for i in range(n)] # a_b_value_indices = {} # m_b_a_value_indices = {} a_b_value_count = {} m_b_a_value_count = {} a_b_value_rep = {} m_b_a_value_rep = {} used = [False for _ in range(n)] zeroes = 0 is_zero = [False for _ in range(n)] for i in range(n): a, b = map(int, input().split()) if a == 0 and b == 0: zeroes += 1 used[i] = True is_zero[i] = True else: if a == 0: a_b[i] = (0, 1) m_b_a[i] = (1, 0) elif b == 0: a_b[i] = (1, 0) m_b_a[i] = (0, 1) else: neg = False if a*b < 0: neg = True d = gcd(abs(a), abs(b)) a = abs(a) // d b = abs(b) // d if neg: a_b[i] = (-a, b) m_b_a[i] = (b, a) else: a_b[i] = (a, b) m_b_a[i] = (-b, a) a_b_value_count[a_b[i]] = a_b_value_count.get(a_b[i], 0) + 1 # a_b_value_indices[a_b[i]] = a_b_value_indices.get(a_b[i], []) + [i] m_b_a_value_count[m_b_a[i]] = m_b_a_value_count.get(m_b_a[i], 0) + 1 # m_b_a_value_indices[m_b_a[i]] = m_b_a_value_indices.get(m_b_a[i], []) + [i] if a_b[i] not in a_b_value_rep: a_b_value_rep[a_b[i]] = i if m_b_a[i] not in m_b_a_value_rep: m_b_a_value_rep[m_b_a[i]] = i ans = 1 for i in range(n): if not is_zero[i] and not used[a_b_value_rep[a_b[i]]]: # if not connected if a_b[i] not in m_b_a_value_count: ans *= pow(2, a_b_value_count[a_b[i]], MODULO) ans %= MODULO used[a_b_value_rep[a_b[i]]] = True # for j in a_b_value_indices[a_b[i]]: # used[j] = True else: s = a_b_value_count[a_b[i]] t = m_b_a_value_count[a_b[i]] ans *= ((1*pow(2, s, MODULO) + 1*pow(2, t, MODULO) -1) % MODULO) ans %= MODULO used[a_b_value_rep[a_b[i]]] = True used[m_b_a_value_rep[a_b[i]]] = True # for j in m_b_a_value_indices[a_b[i]]: # used[j] = True # used_a_b_keys.add(a_b_key) # used_a_b_keys.add(-1/a_b_key) # -1 is for empty print((ans-1+zeroes) % MODULO)
N = int(input()) E = [[] for _ in range(N+1)] for _ in range(N): tmp = list(map(int, input().split())) if tmp[1] == 0: continue E[tmp[0]] = tmp[2:] cnt = [0 for _ in range(N+1)] q = [1] while q: cp = q.pop(0) for np in E[cp]: if cnt[np] != 0: continue cnt[np] = cnt[cp] + 1 q.append(np) for ind, cost in enumerate(cnt): if ind == 0: continue if ind == 1: print(ind, 0) else: if cost == 0: print(ind, -1) else: print(ind, cost)
0
null
10,468,108,537,208
146
9
X, N = map(int, input().split()) P = list(map(int, input().split())) st = set(P) for i in range(111): if not X - i in st: print(X - i) exit(0) if not X + i in st: print(X + i) exit(0)
def RSQ_add(i, x, y): while True: if i > n: break BIT[y][i] += x i += i & -i def RSQ_getsum(i, x): s = 0 while True: if i <= 0: break s += BIT[x][i] i -= i & -i return s n = int(input()) s = list(input()) q = int(input()) BIT = [[0] * (n + 1) for _ in range(26)] for i in range(n): y = ord(s[i]) - 97 RSQ_add(i + 1, 1, y) for _ in range(q): com, a, b = map(str, input().split()) com, a = int(com), int(a) if com == 1: RSQ_add(a, -1, ord(s[a - 1]) - 97) RSQ_add(a, 1, ord(b) - 97) s[a - 1] = b else: b = int(b) ans = 0 for i in range(26): if RSQ_getsum(b, i) - RSQ_getsum(a - 1, i) >= 1: ans += 1 print(ans)
0
null
38,196,936,907,752
128
210
n=int(input()) X=input() x=X.count('1') x_plus=int(X,2)%(x+1) keta=n-1 if x==1: for i in range(n): tmp=0 ans=1 if X[i]=='0': tmp+=x_plus tmp+=pow(2,keta,(x+1)) tmp%=(x+1) while tmp: tmp=tmp%(bin(tmp).count('1')) ans+=1 print(ans) else: print(0) keta-=1 else: x_minus=int(X,2)%(x-1) for i in range(n): tmp=0 ans=1 if X[i]=='0': tmp+=x_plus tmp+=pow(2,keta,(x+1)) tmp%=(x+1) while tmp: tmp=tmp%(bin(tmp).count('1')) ans+=1 print(ans) else: tmp+=x_minus tmp-=pow(2,keta,(x-1)) tmp%=(x-1) while tmp: tmp=tmp%(bin(tmp).count('1')) ans+=1 print(ans) keta-=1
import sys read = sys.stdin.buffer.read input = sys.stdin.buffer.readline inputs = sys.stdin.buffer.readlines import bisect n,m=map(int,input().split()) A=list(map(int,input().split())) A.sort() def hand(x): cnt=0 for i in range(n): p=x-A[i] cnt+=n-bisect.bisect_left(A,p) return cnt def main(): l=0 h=2*10**5+1 mid=(l+h)//2 while l+1<h: mid=(l+h)//2 if hand(mid)<m: h=mid else: l=mid B=A[::-1] for i in range(n-1): B[i+1]+=B[i] B=B[::-1] ans=0 cnt=0 for i in range(n): y=l-A[i]+1 index=bisect.bisect_left(A,y) if n==index: continue else: ans+=(n-index)*A[i]+B[index] cnt+=(n-index) ans+=(m-cnt)*l print(ans) if __name__ == "__main__": main()
0
null
58,411,685,073,518
107
252
N = int(input()) l = list(map(int, input().split())) counter = 1 ans = 0 for j in l: if j == counter: counter += 1 else: ans += 1 if counter==1: print(-1) else: print(ans)
def readinput(): n,k=map(int,input().split()) okashi=[] for _ in range(k): d=int(input()) l=list(map(int,input().split())) okashi.append(l) return n,k,okashi def main(n,k,okashi): sunuke=[0]*(n+1) for l in okashi: for su in l: sunuke[su]+=1 count=0 for i in range(n): if sunuke[i+1]==0: count+=1 return count if __name__=='__main__': n,k,okashi=readinput() ans=main(n,k,okashi) print(ans)
0
null
69,238,071,436,190
257
154
def main(): num = int(input()) con = 10**9 + 7 a, b, c = 1, 1, 2 for i in range(num): a *= 10 a %= con b *= 8 b %= con c *= 9 c %= con if(a+b-c < 0): print(a+b-c+con) return 0 print(a+b-c) if __name__ == '__main__': main()
n=int(input()) x=(pow(10,n)-2*pow(9,n)+pow(8,n))%1000000007 print(x)
1
3,192,972,359,500
null
78
78
s1,s2=map(str,input().split(' ')) print(s2+s1)
from collections import Counter,defaultdict,deque from heapq import heappop,heappush,heapify import sys,bisect,math,itertools,pprint,fractions sys.setrecursionlimit(10**8) mod = 10**9+7 INF = float('inf') def inp(): return int(sys.stdin.readline()) def inpl(): return list(map(int, sys.stdin.readline().split())) n,ta,ao = inpl() ta -= 1; ao -= 1 g = [[] for i in range(n)] for i in range(n-1): a,b = inpl() g[a-1].append(b-1) g[b-1].append(a-1) ta_dist = [None] * n ta_dist[ta] = 0 ao_dist = [None] * n ao_dist[ao] = 0 def ta_dfs(node): for v in g[node]: if ta_dist[v] != None: continue ta_dist[v] = ta_dist[node] + 1 ta_dfs(v) def ao_dfs(node): for v in g[node]: if ao_dist[v] != None: continue ao_dist[v] = ao_dist[node] + 1 ao_dfs(v) ao_dfs(ao) ta_dfs(ta) res = 0 for i in range(n): if ta_dist[i] > ao_dist[i]: continue res = max(res, ao_dist[i]) print(res-1)
0
null
110,413,195,677,460
248
259
n,m = map(int,input().split()) a = []; b= []; for i in range(n): a.append(list(map(int,input().split()))) for i in range(m): b.append(int(input())) for i in range(n): c = 0 for j in range(m): c += a[i][j]*b[j] print(c)
N = int(input()) arr = [int(n) for n in input().split()] swap_cnt = 0 for i in range(0, N): minj = i for j in range(i + 1, N): if arr[j] < arr[minj]: minj = j if (i != minj): arr[i], arr[minj] = arr[minj], arr[i] swap_cnt += 1 print(' '.join(map(str, arr))) print(swap_cnt)
0
null
601,301,292,230
56
15
while True: W = input().rstrip() if W == "-": break m = int(input().rstrip()) l = list(W) for i in range(m): h = int(input().rstrip()) w = l[:h] del l[:h] l += w ans = "".join([str(i) for i in l]) print(ans)
#9_2 def shuffle(letter,num): for i in range(num): memory='' h=int(input()) bellow=letter[0:h] above=letter[h:len(letter)] memory=above+bellow letter=memory return letter ##main## while(True): letter=input() if(letter in "-"): break else: num=int(input()) letter=shuffle(letter,num) print(letter)
1
1,926,527,299,368
null
66
66
import sys def main(): input = sys.stdin.buffer.readline n = int(input()) a = list(map(int, input().split())) ans = 0 for i in range(0, n, 2): ans += a[i] % 2 == 1 print(ans) if __name__ == "__main__": main()
while 1: H, W=map(int, raw_input().split()) if H==0 and W==0: break for i in range(H): L=list("#"*W) if i==0 or i==(H-1): s="".join(L) print s else: for j in range(1, W-1): L[j]="." s="".join(L) print s print ""
0
null
4,319,125,391,560
105
50
import sys import itertools read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines sys.setrecursionlimit(10 ** 7) H,W = map(int,readline().split()) grid = '' grid += '#'* (W+2) for _ in range(H): grid += '#' + readline().decode().rstrip() + '#' grid += '#'*(W+2) L = len(grid) INF = float('inf') def bfs(start): dist = [-INF] * L q = [start] dist[start] = 0 while q: qq = [] for x,dx in itertools.product(q,[1,-1,W+2,-W-2]): y = x+dx if dist[y] != -INF or grid[y] == '#': continue dist[y] = dist[x] + 1 qq.append(y) q = qq return max(dist) ans = 0 for i in range(L): if grid[i] == '.': start = i max_dist = bfs(start) ans = max(ans,max_dist) print(ans)
def pon(): a = int(input()) % 10 if a in [2, 4, 5, 7, 9]: print("hon") elif a in [0, 1, 6, 8]: print("pon") else: print("bon") pon()
0
null
57,145,994,653,230
241
142
import math import string import itertools import fractions import heapq import collections import re import array import bisect import sys import random import time inf = 10**9 class Coordinate: def __init__(self, x, y): self.x = x self.y = y def kock(n, p1, p2): if n == 0: return s = Coordinate((2 * p1.x + p2.x) / 3, (2 * p1.y + p2.y) / 3) t = Coordinate((p1.x + 2 * p2.x) / 3, (p1.y + 2 * p2.y) / 3) u = Coordinate( (t.x - s.x) * math.cos(math.pi / 3) - (t.y - s.y) * math.sin(math.pi / 3) + s.x, (t.x - s.x) * math.sin(math.pi / 3) + (t.y - s.y) * math.cos(math.pi / 3) + s.y ) kock(n - 1, p1, s) print(s.x, s.y) kock(n - 1, s, u) print(u.x, u.y) kock(n - 1, u, t) print(t.x, t.y) kock(n - 1, t, p2) def main(): n = int(input()) p1 = Coordinate(0, 0) p2 = Coordinate(100, 0) print(p1.x, p1.y) kock(n, p1, p2) print(p2.x, p2.y) if __name__ == '__main__': main()
s = input() p = input() s = s+s if s.find(p) < 0: print('No') else: print('Yes')
0
null
930,108,176,128
27
64
from collections import deque N = int(input()) edge = [[] for _ in range(N+1)] for i in range(N): A = list(map(int, input().split())) edge[A[0]] = A[2:] ans = [-1]*N ans[0] = 0 d = deque([[1,0]]) while len(d)>0: v,dist = d.popleft() for w in edge[v]: if ans[w-1]==-1: ans[w-1]=dist+1 d.append([w,dist+1]) for i in range(N): print(i+1,ans[i])
from collections import deque n = int(input()) M = [[0]*n for _ in range(n)] d = [-1]*n st = [0]*n for _ in range(n): i = [int(i) for i in input().split()] u = i[0] k = i[1] V = i[2:] for v in V: M[u-1][v-1] = 1 def bfs(s): Q = deque() Q.append(s) st[s] = 1 d[s] = 0 while Q: u = Q.popleft() for v in range(n): if M[u][v] and st[v] == 0: st[v] = 1 Q.append(v) d[v] = d[u]+1 st[u] = 2 return bfs(0) for i in range(n): print(i+1,d[i])
1
4,536,252,188
null
9
9
n, k = map(int, input().split()) a = list(map(int, input().split())) for i in range(n): a[i] = a[i] % k if k == 1: print(0) exit() ruiseki = [0] for i in range(n): ruiseki.append((a[i]+ruiseki[-1])%k) mini = 0 dic = {} ans = 0 for j in range(1, n+1): if j-k+1 > mini: dic[(ruiseki[mini]-mini)%k] -= 1 mini += 1 if (ruiseki[j-1]-(j-1))%k in dic: dic[(ruiseki[j-1]-(j-1))%k] += 1 else: dic[(ruiseki[j-1]-(j-1))%k] = 1 if (ruiseki[j]-j)%k in dic: ans += dic[(ruiseki[j]-j)%k] print(ans)
from collections import deque k = int(input()) x = 0 q = deque(list(range(1, 10))) for _ in range(k): x = q.popleft() d = x % 10 y = 10 * x + d if d > 0: q.append(y - 1) q.append(y) if d < 9: q.append(y + 1) print(x)
0
null
88,286,793,508,188
273
181
# coding: utf-8 import sys from collections import defaultdict sr = lambda: sys.stdin.readline().rstrip() ir = lambda: int(sr()) lr = lambda: list(map(int, sr().split())) # MODใฎ็ฎก็†ใ‚’ใ—ใฆๅณใ‹ใ‚‰้€ฒใ‚ใ‚‹ N, P = lr() S = sr() dic = defaultdict(int) dic[0] = 1 num = 0 power = 1 answer = 0 if P%2 == 0 or P%5 == 0: x = [i for i, y in enumerate(S, 1) if int(y) % P == 0] answer = sum(x) else: for s in S[::-1]: s = int(s) num += s * power num %= P answer += dic[num] dic[num] += 1 power *= 10; power %= P print(answer) # 14
def main(): n, mod = map(int, input().split()) s = input().rstrip() a = [int(c)%mod for c in s] ans = 0 if mod == 2 or mod == 5: for i in range(n): if a[i] == 0: ans += i + 1 else: mul = 10 % mod for i in reversed(range(n-1)): a[i] = (a[i]*mul + a[i+1]) % mod mul = mul*10 % mod b = [0] * mod b[0] = 1 for i in reversed(range(n)): ans += b[a[i]] b[a[i]] += 1 print(ans) if __name__ == "__main__": main()
1
58,257,297,587,090
null
205
205
import sys sys.setrecursionlimit(10 ** 5 + 10) def input(): return sys.stdin.readline().strip() def resolve(): H, W, K = map(int, input().split()) grid = [[_ for _ in input()] for _ in range(H)] ans = [[0] * W for _ in range(H)] cnt = 0 for h in range(H): for w in range(W): if grid[h][w] == '#': cnt += 1 ans[h][w] = cnt now = -1 for h in range(H): for w in range(W): if ans[h][w] != 0: now = ans[h][w] if ans[h][w] == 0: if now != -1: ans[h][w] = now now = -1 now = -1 for h in range(H): for w in reversed(range(W)): if ans[h][w] != 0: now = ans[h][w] if ans[h][w] == 0: if now != -1: ans[h][w] = now now = -1 for h in range(1,H): if ans[h][0] == 0: for w in range(W): ans[h][w] = ans[h - 1][w] for h in reversed(range(H-1)): if ans[h][0] == 0: for w in range(W): ans[h][w] = ans[h + 1][w] for a in ans: print(*a) resolve()
x = list(map(int, input().split())) x0 = [x[0], x[1]] y0 = [x[2], x[3]] results = [(x0[0]*y0[0]), (x0[1]*y0[0]), (x0[0]*y0[1]), (x0[1]*y0[1])] print(max(results))
0
null
73,173,376,092,682
277
77
while True: x = [int(z) for z in input().split(" ")] if x[0] == 0 and x[1] == 0: break for h in range(0,x[0]): dot = h % 2 for w in range(0,x[1]): if (dot == 0 and w % 2 == 0) or (dot == 1 and w % 2 == 1): print("#", end="") else: print(".", end="") print("\n", end="") print("")
while True: H,W=[int(i) for i in input().split(" ")] if H==W==0: break for h in range(H): s="#" if h%2==0 else "." for w in range(W-1): s+="#" if s[-1]=="." else "." print(s) print()
1
875,226,228,032
null
51
51
from sys import stdin def I(): return int(stdin.readline().rstrip()) def LI(): return list(map(int,stdin.readline().rstrip().split())) if __name__=='__main__': mod = 10**9 + 7 n = I() a = LI() ans = 0 for i in range(60): c1 = 0 for rep in a: if (rep>>i)&1: c1 += 1 ans += ((n-c1)*c1*(2**i)%mod)%mod print(ans%mod)
x = int(input()) cnt = 0 m_500 = x // 500 m_5 = (x - m_500*500) // 5 print(m_500*1000 + m_5*5)
0
null
83,036,689,375,192
263
185
def gcd(x,y): a=max(x,y) b=min(x,y) if a%b==0: return b else: return gcd(a%b,b) A,B=map(int,input().split()) print(gcd(A,B))
X=int(input()) for x in range(X,2*X): for i in range(2,int(x**.5)+1): if x%i==0: break else: print(x) break
0
null
52,928,084,745,580
11
250
X = int(input()) ans = X//500*1000 X = X % 500 ans += X//5*5 print(ans)
money = int(input()) count_div_500, left_after_500 = divmod(money, 500) count_div_5, left_after_5 = divmod(left_after_500, 5) print(count_div_500 * 1000 + count_div_5 * 5)
1
42,475,654,000,188
null
185
185
h,n=map(int,input().split()) a=[int(x) for x in input().split()] if sum(a) >= h: print("Yes") else: print("No")
h,n = map(int, input().split()) a = list(map(int, input().split())) print('Yes' if h-sum(a) <= 0 else 'No')
1
78,185,761,256,772
null
226
226
n, m = map(int, input().split()) a = input().split() a_sum = 0 for b in a: a_sum += int(b) if a_sum > n: print(-1) else: print(n - a_sum)
a,b=map(int,input().split()) List = list(map(int, input().split())) wa = 0 for i in range(b): wa += List[i] res = a - wa if res < 0: res = -1 print(res)
1
32,154,855,196,092
null
168
168
n, m = map(int, input().split()) if n%2: for i in range(1, m+1): print(i, n-i) else: for i in range(1, m+1): if 2*i >= n/2: print(i, n-i-1) else: print(i, n-i)
def solve(s, p=2019): s = s[::-1] memo = [0] * p memo[0] = 1 ans = 0 q = 0 r = 1 for c in s: q = (q + int(c)*r) % p ans += memo[q] r = 10*r % p memo[q] += 1 return ans s = input() print(solve(s))
0
null
29,776,410,356,264
162
166
N, A, B = map(int, input().split()) ans = 0 if (B-A)%2 == 0: print((B-A)//2) else: if A-1 < N-B: ans += A-1 else: ans += N-B ans += 1 ans += (B-A-1)//2 print(ans)
N, A, B = map(int, input().split()) d = A - B if A > B else B - A d2 = d // 2 d3 = d % 2 if d3 == 0: ans = d2 else: bigger = B smaller = A if A > B: bigger = A smaller = B shortest = smaller if N - bigger + 1 > smaller else N - bigger + 1 ans = shortest + d2 print(ans)
1
108,884,196,970,428
null
253
253
#!/usr/bin/env python3 import sys import collections as cl def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def main(): x, y, a, b, c = MI() p = LI() q = LI() r = LI() p.sort(reverse=True) q.sort(reverse=True) r.sort(reverse=True) p_selected = p[:x] q_selected = q[:y] ans = [*p_selected, *q_selected] ans.sort() i = 0 while i < x + y and i < c and ans[i] < r[i]: ans[i] = r[i] i += 1 print(sum(ans)) main()
x, y, a, b, c = map(int, input().split()) p_l = sorted(list(map(int, input().split())), reverse=True)[:x] q_l = sorted(list(map(int, input().split())), reverse=True)[:y] r_l = list(map(int, input().split())) p_l = sorted(p_l + q_l + r_l, reverse=True) print(sum(p_l[:x+y]))
1
45,014,192,604,352
null
188
188
N = int(input()) answer = 0 for i in range(1,10): for j in range(1,10): if N == i*j: answer = 1 break if answer == 1: print('Yes') else: print('No')
while 1: x,y = map( int , raw_input().split() ) if x == 0 and y == 0: break elif x <= y: print "%d %d" %(x,y) elif y < x : print "%d %d" %(y,x)
0
null
80,179,798,971,300
287
43
import math n = int(input()) x = list(map(int, input().split())) y = list(map(int, input().split())) xy = [] for i in range(n): xy.append(abs(x[i] - y[i])) D = [0, 0, 0, 0] D[0] = sum(xy) D2 = 0 for xyi in xy: D2 += xyi ** 2 D[1] = math.sqrt(D2) D3 = 0 for xyi in xy: D3 += xyi ** 3 D[2] = math.pow(D3, 1.0/3.0) D[3] = max(xy) for d in D: print(d)
n = int(input()) s = input() t="RGBRGB" c=0 cm=0 for i in range(3): for j in range(1,n-1): if s[j]==t[i] : low = s[:j].count(t[i+1]) high = s[j:].count(t[i+2]) c += low*high low = s[:j].count(t[i+2]) high = s[j:].count(t[i+1]) c += low*high for k in range(1,j+1): if j+k>n-1 : break if s[j-k]!=t[i] and s[j+k]!=t[i] and s[j-k]!=s[j+k]: cm+=1 print(c-cm)
0
null
18,196,421,989,020
32
175
# AOJ ITP1_9_B def main(): while True: string = input() if string == "-": break n = int(input()) # ใ‚ทใƒฃใƒƒใƒ•ใƒซๅ›žๆ•ฐ for i in range(n): h = int(input()) front = string[0 : h] # 0็•ชใ‹ใ‚‰h-1็•ชใพใง back = string[h : len(string)] # h็•ชใ‹ใ‚‰len-1็•ชใพใง string = back + front print(string) if __name__ == "__main__": main()
N,M = map(int, input().split()) a = 1 b = N for i in range(M, 0, -1): if i%2 == M%2: print(a, a+i) a += 1 else: print(b, b-i) b -= 1
0
null
15,385,157,809,080
66
162
W, H, x, y, r = [int(i) for i in input().rstrip().split(" ")] hidari, migi = x-r, x + r shita, ue = y-r, y+r if (0 <= hidari) and (migi <= W) and (0 <= shita) and (ue <= H): print("Yes") else: print("No")
import collections import sys S = input()[::-1] MOD=2019 X = [0] for i, s in enumerate(S): X.append((X[-1]+int(s)*pow(10,i,MOD))%MOD) c = collections.Counter(X) ans = 0 for v in c.values(): ans += v * (v - 1) // 2 print(ans)
0
null
15,709,056,402,528
41
166
N=int(input()) a,b,c,d=0,0,0,0 for i in range(N): A=input() if A=="AC": a+=1 elif A=="WA": b+=1 elif A=="TLE": c+=1 else: d+=1 print("AC x",a) print("WA x",b) print("TLE x",c) print("RE x",d)
num = int(input()) ac = 0 wa = 0 tle = 0 re = 0 for i in range(num): k = input() if k == "AC": ac += 1 elif k == "WA": wa += 1 elif k == "TLE": tle += 1 elif k == "RE": re += 1 print("AC x " + str(ac)) print("WA x " + str(wa)) print("TLE x " + str(tle)) print("RE x " + str(re))
1
8,695,943,736,480
null
109
109
N = int(input()) An = list(map(int, input().split())) money = 1000 stock = 0 for i in range(N-1): if An[i+1] > An[i]: stock = money // An[i] money += (An[i+1] - An[i]) * stock print(money)
n = int(input()) ls = list(map(int,input().split())) mon = 1000 beet = 0 p = [] m = [] if ls[1]-ls[0] > 0: beet += int(mon//ls[0]) mon -= ls[0]*int(mon//ls[0]) be = True else: be = False for i in range(n-1): if ls[i] < ls[i+1]: if be == False: beet += int(mon//ls[i]) mon -= ls[i]*int(mon//ls[i]) be = True if ls[i] > ls[i+1]: if be == True: mon += ls[i] * beet beet = 0 be = False if beet > 0: mon += beet*ls[-1] print(mon)
1
7,262,711,327,562
null
103
103
# encording: utf-8 _ = int(input()) ns = [int(e) for e in input().split()] _ = int(input()) qs = [int(e) for e in input().split()] def main(ns, qs): total = 0 for q in qs: if q in ns: total += 1 return total print(main(ns, qs))
n = int(input()) S = [int(i) for i in input().split()] q = int(input()) T = [int(i) for i in input().split()] cnt = 0 for T in T: if T in S: cnt += 1 print(cnt)
1
68,886,511,668
null
22
22
def isprime(x): if x == 2: return True elif x < 2 or x%2 == 0: return False i = 3 while i <= pow(x,1/2): if x%i == 0: return False i = i + 2 return True count = 0 for s in range(int(input())): if isprime(int(input())): count += 1 print(count)
def insertionSort(a): for i,v in enumerate(a): j=i-1 while j>=0 and a[j]>v: a[j+1]=a[j] j-=1 a[j+1]=v print(*a) n = int(input()) a = list(map(int,input().split())) insertionSort(a)
0
null
7,893,245,052
12
10
dataList = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51] input_k = input('') k_index = int(input_k) print(dataList[k_index - 1])
l = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51] k = int(input()) ans = l[k - 1] print(ans)
1
50,207,346,668,372
null
195
195
x,n=[int(x) for x in input().split()] if n!=0: p=[int(x) for x in input().split()] l=list(range(110)) for i in p: l.remove(i) L=[] for i in l: L.append(abs(i-x)) m=min(L) print(l[L.index(m)]) else: print(x)
X,N = map(int, input().split()) P = list(map(int, input().split())) if len(P) == 0: print(X) else: for i in range(0,10000): if P.count(X-i) == 0: print(X-i) break elif P.count(X+i) == 0: print(X+i) break else: continue
1
14,212,288,227,270
null
128
128