code1
stringlengths
16
427k
code2
stringlengths
16
427k
similar
int64
0
1
pair_id
int64
6.82M
181,637B
question_pair_id
float64
101M
180,471B
code1_group
int64
2
299
code2_group
int64
2
299
N = int(input()) S = input() ans = ['' for _ in range(len(S))] a = ord('A') for i, c in enumerate(S): ans[i] = chr((ord(c) - a + N) % 26 + a) print(''.join(ans))
def main(): n = int(input()) s = input() for i in s: mod = (ord(i) - ord('A') + n) % 26 ans = chr(ord('A') + mod) print(ans, end='') if __name__ == '__main__': main()
1
134,511,061,027,310
null
271
271
h, w, k = map(int, input().split(' ')) c = [input() for x in range(h)] cnt = 0 for maskR in range(2**h): for maskC in range(2**w): black = 0 for i in range(h): for j in range(w): if (((maskR >> i) &1) == 0 and ((maskC >> j) &1) == 0 and c[i][j] == '#'): black += 1 if black == k: cnt += 1 print(cnt)
def main(): H, W, K = map(int, input().split()) A = [list(input()) for _ in range(H)] combs = [] total_blacks = 0 ans = 0 for a in A: for a_ in a: if a_ == "#": total_blacks += 1 for i in range(2**(H+W)): tmp_combs = [] for j in range(H+W): if (i >> j) & 1: tmp_combs.append(1) else: tmp_combs.append(0) combs.append(tmp_combs) for c in combs: num_black = total_blacks for i in range(H): for j in range(W): h, w = c[i], c[H+j] if (h == 1 or w == 1) and A[i][j] == "#": num_black -= 1 if num_black == K: ans += 1 print(ans) if __name__ == '__main__': main()
1
8,957,607,306,120
null
110
110
A, B =map(float,input().split()) A = int(A) B = int(B*100+0.5) ans = A*B//100 print(ans)
from decimal import Decimal a, b = map(Decimal,input().split()) ans = int(a * b) print(ans)
1
16,513,308,467,870
null
135
135
from collections import deque N = int(input()) Q = deque([1, 2, 3, 4, 5, 6, 7, 8, 9]) for i in range(N - 1): x = Q.popleft() if x % 10 != 0: y = 10 * x + x % 10 - 1 Q.append(y) y = 10 * x + x % 10 Q.append(y) if x % 10 != 9: y = 10 * x + x % 10 + 1 Q.append(y) ans = Q.popleft() print(ans)
from collections import deque k = int(input()) a = deque([1, 2, 3, 4, 5, 6, 7, 8, 9]) for i in range(k-1): b = a.popleft() if b%10 != 0: a.append(b*10+b%10-1) a.append(b*10+b%10) if b%10 != 9: a.append(b*10+b%10+1) print(a[0])
1
40,025,754,410,700
null
181
181
import sys, bisect, math, itertools, string, queue, copy import numpy as np import scipy from collections import Counter,defaultdict,deque from itertools import permutations, combinations from heapq import heappop, heappush from fractions import gcd input = sys.stdin.readline sys.setrecursionlimit(10**8) mod = 10**9+7 def inp(): return int(input()) def inpm(): return map(int,input().split()) def inpl(): return list(map(int, input().split())) def inpls(): return list(input().split()) def inplm(n): return list(int(input()) for _ in range(n)) def inplL(n): return [list(input()) for _ in range(n)] def inplT(n): return [tuple(input()) for _ in range(n)] def inpll(n): return [list(map(int, input().split())) for _ in range(n)] def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) n = inp() ans = 0 n_ketasuu = len(str(n)) sta_max = int(str(n)[0]) end_max = int(str(n)[n_ketasuu-1]) for i in range(1,n+1): sta = int(str(i)[len(str(i))-1]) end = int(str(i)[0]) if sta == 0: continue #1桁 if sta == end: ans +=1 #2桁 if n_ketasuu >= 2 and sta*10 + end <= n: ans += 1 #3桁 if n_ketasuu > 3 or (n_ketasuu == 3 and sta < sta_max): ans += 10 elif n_ketasuu == 3 and sta == sta_max: ans += int(str(n)[1:n_ketasuu-1]) if end <= end_max: ans += 1 #4桁 if n_ketasuu > 4 or (n_ketasuu == 4 and sta < sta_max): ans += 100 elif n_ketasuu == 4 and sta == sta_max: ans += int(str(n)[1:n_ketasuu-1]) if end <= end_max: ans += 1 #5桁 if n_ketasuu > 5 or (n_ketasuu == 5 and sta < sta_max): ans += 1000 elif n_ketasuu == 5 and sta == sta_max: ans += int(str(n)[1:n_ketasuu-1]) if end <= end_max: ans += 1 #6桁 if n_ketasuu > 6 or (n_ketasuu == 6 and sta < sta_max): ans += 10000 elif n_ketasuu == 6 and sta == sta_max: ans += int(str(n)[1:n_ketasuu-1]) if end <= end_max: ans += 1 print(ans)
n = int(input()) digit = 1 while n > 26 ** digit: n -= 26 ** digit digit += 1 ans = [] n -= 1 char = 'abcdefghijklmnopqrstuvwxyz' for i in list(range(digit)): ans.append(char[n % 26]) n -= n%26 n = int(n/26) print(''.join(reversed(ans)))
0
null
49,351,702,464,868
234
121
def main(): ary = [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()) print(ary[K - 1]) main()
n=int(input()) ev=[] for i in range(n): a=int(input()) ev.append([list(map(int,input().split())) for _ in range(a)]) ans=0 for i in range(2**n): bit=(bin(i)[2:].zfill(n)) flag=1 for j in range(n): if bit[j]=="0": continue for e in ev[j]: if int(bit[e[0]-1])!=e[1]: flag=0 if flag: ans=max(ans,bit.count("1")) print(ans)
0
null
86,152,710,040,700
195
262
def solve(): A,B,N = map(int, input().split()) if N < B-1: print((A*N)//B - A * (N//B)) else: print(((A*(B-1))//B) - A* ((B-1)//B)) if __name__ == "__main__": solve()
import sys import math def IL(): return map(int,sys.stdin.readline().rstrip().split()) def Main(): a,b,n = IL() x = min(b-1,n) print(math.floor(a*x/b)-a*math.floor(x/b)) return if __name__=='__main__': Main()
1
28,072,155,423,872
null
161
161
rStr = input() N = int(rStr.split(' ')[0]) M = int(rStr.split(' ')[1]) if N == M : print("Yes") else : print("No")
INPUT = list(input().split()) a = int(INPUT[0]) b = int(INPUT[1]) if a == b: print("Yes") else: print("No")
1
83,391,139,588,328
null
231
231
import sys def solve(): input = sys.stdin.readline R, C, K = map(int, input().split()) item = [[0 for _ in range(C)] for r in range(R)] for _ in range(K): r, c, k = map(int, input().split()) item[r-1][c-1] = k DP = [[[-1 for _ in range(C)] for r in range(R)] for i in range(4)] DP[0][0][0] = 0 for r in range(R): for c in range(C): for i in range(4): if DP[i][r][c] > -1: if r + 1 < R: DP[0][r+1][c] = max(DP[0][r+1][c], DP[i][r][c]) if c + 1 < C: DP[i][r][c+1] = max(DP[i][r][c+1], DP[i][r][c]) if i < 3 and item[r][c] > 0: if r + 1 < R: DP[0][r+1][c] = max(DP[0][r+1][c], DP[i][r][c] + item[r][c]) if c + 1 < C: DP[i+1][r][c+1] = max(DP[i+1][r][c+1], DP[i][r][c] + item[r][c]) ans = DP[3][R-1][C-1] for i in range(3): ans = max(ans, DP[i][R-1][C-1] + item[R-1][C-1]) print(ans) return 0 if __name__ == "__main__": solve()
R, C, K = map(int, input().split()) items = [[0]*C for _ in range(R)] for i in range(K): r, c, v = map(int, input().split()) items[r-1][c-1] = v dp = [[0]*4 for _ in range(C)] for x in range(1, 4): dp[0][x] = items[0][0] for j in range(1, C): dp[j][x] = max(dp[j-1][x-1] + items[0][j], dp[j-1][x]) #print(items) #print(dp) for i in range(1, R): for j in range(C): for x in range(4): dp[j][x] = dp[j][3] for x in range(1, 4): dp[0][x] += items[i][0] for j in range(1, C): dp[j][x] = max(dp[j-1][x-1] + items[i][j], dp[j-1][x], dp[j][3] + items[i][j]) #print(dp) print(dp[-1][-1])
1
5,548,203,208,004
null
94
94
import math import sys def insertionSort(data, N, g, cnt): for i in range(g, N): v = data[i] j = i - g while j >= 0 and data[j] > v: data[j+g] = data[j] j = j - g cnt += 1 data[j+g] = v return data, cnt def shellSort(data, N, G, cnt): h = 1 while (True): if h > N: break G.append(h) h = 3 * h + 1 m = len(G) G.reverse() for i in range(m): data, cnt = insertionSort(data, N, G[i], cnt) return m, G, cnt def main(): N = int(input()) data = [int(input()) for _ in range(N)] cnt = 0 G = [] m = 0 m, G, cnt = shellSort(data, N, G, cnt) print(m) print(' '.join(map(str, G))) print(cnt) for i in range(N): print(data[i]) if __name__ == '__main__': main()
n=int(input()) s=input() ans=0 for i in range(1000): i=str(i).zfill(3) a=0 for c in s: if c==i[a]: a+=1 if a==3: ans+=1 break print(ans)
0
null
64,064,283,252,848
17
267
Spades = 0 Hearts = 1 Clovers = 2 Diamonds = 3 Cards = [[Spades, Hearts, Clovers, Diamonds] for j in range(13)] for i in range(4): for j in range(13): Cards[j][i] = True n = input() for i in range(n): Input = raw_input().split() Numb = int(Input[1]) if Input[0] == 'S': Mark = Spades if Input[0] == 'H': Mark = Hearts if Input[0] == 'C': Mark = Clovers if Input[0] == 'D': Mark = Diamonds Cards[Numb - 1][Mark] = False for i in range(4): for j in range(13): if Cards[j][i]: if i == Spades: print 'S', j + 1 if i == Hearts: print 'H', j + 1 if i == Clovers: print 'C', j + 1 if i == Diamonds: print 'D', j + 1
# coding: utf-8 ''' S 1 H 3 H 7 C 12 D 8 ''' cards = ['S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9', 'S10', 'S11', 'S12', 'S13', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H8', 'H9', 'H10', 'H11', 'H12', 'H13', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'C10', 'C11', 'C12', 'C13', 'D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9', 'D10', 'D11', 'D12', 'D13'] for _ in range(int(input())): # suit, number = input().split() suit, number = map(str, input().split()) cards.remove(suit + number) for trump in cards: print(trump[0], trump[1:])
1
1,019,028,391,612
null
54
54
N, M = [int(v) for v in input().rstrip().split()] r = 'Yes' if N == M else 'No' print(r)
n, d = map(int, input().split(" ")) p = [] ans = 0 for i in range(n): p.append(list(map(int, input().split(" ")))) for i, [x, y] in enumerate(p): if(x > d or y > d): continue if(x**2 + y**2 <= d**2): ans += 1 print(ans)
0
null
44,743,994,956,300
231
96
V = input() s = 0 for c in V: n = int(c) s = s + n if s%9 == 0: print("Yes") else: print("No")
N, M = map(int, input().split()) A = list(map(int, input().split())) for i in A: N -= i if N >= 0: print(N) else: print(-1)
0
null
18,139,448,653,380
87
168
n = int(input()) xy = [map(int, input().split()) for _ in range(n)] x, y = [list(i) for i in zip(*xy)] count = 0 buf = 0 for xi, yi in zip(x, y): if xi == yi: buf += 1 else: if buf > count: count = buf buf = 0 if buf > count: count = buf if count >= 3: print('Yes') else: print('No')
n=int(input()) cnt=0 ans=0 for i in range(n): d1,d2=map(int,input().split()) if d1==d2: cnt=cnt+1 ans=max(ans,cnt) else: cnt=0 if ans>=3: print("Yes") else: print("No")
1
2,487,760,579,402
null
72
72
while True: m,f,r = [int(x) for x in input().split()] if (m,f,r)==(-1,-1,-1): break s_mf = m + f if m < 0 or f < 0: print('F') elif s_mf < 30: print('F') elif s_mf >= 80: print('A') elif s_mf >= 65: print('B') elif s_mf >= 50: print('C') elif r >= 50: print('C') else: print('D')
# -*- coding: utf-8 -*- import sys import os def input_to_list(): return list(map(int, input().split())) H, W = input_to_list() M = [] for i in range(H): M.append(input_to_list()) v = [] for i in range(W): v.append(int(input())) # M x v for i in range(H): all = 0 for j in range(W): all += M[i][j] * v[j] print(all)
0
null
1,212,624,618,668
57
56
build = [[[0]*10,[0]*10,[0]*10],[[0]*10,[0]*10,[0]*10],[[0]*10,[0]*10,[0]*10],[[0]*10,[0]*10,[0]*10]] n = int(input()) for i in range(n): b, f, r, v = [int(x) for x in input().split()] build[b-1][f-1][r-1] += v count = 0 for i1 in build: for i2 in i1: line = "" for i3 in i2: line += " " + str(i3) print(line) if count < 3: print("#"*20) count += 1
rows, cols = [int(x) for x in input().split()] matrix = [] vector = [] for i in range(rows): matrix.append([int(x) for x in input().split()]) for i in range(cols): vector.append(int(input())) for row in matrix: result = 0 for x, y in zip(row, vector): result += x*y print(result)
0
null
1,121,954,470,700
55
56
# -*- coding: utf-8 -*- sen = '' while True: try: buf = raw_input() except: break sen += buf.lower() for i in range(ord('a'), ord('z')+1): print "%s : %d" %(chr(i), sen.count(chr(i)))
import sys N = int(input()) + 1 ans = N // 2 print(ans)
0
null
30,518,818,169,570
63
206
''' Created on 2020/08/23 @author: harurun ''' def f(n): arr=[] temp=n for c in range(2,int(-(-n**0.5//1))+1): if temp%c==0: cnt=0 while temp%c==0: cnt+=1 temp//=c arr.append([c,cnt]) if temp!=1: arr.append([temp,1]) return arr def main(): import math import sys pin=sys.stdin.readline pout=sys.stdout.write perr=sys.stderr.write N=int(pin()) r=f(N) ans=0 for i in r: ans+=int((math.sqrt(1+8*i[1])-1)/2) print(ans) return main()
import sys S = input() if not ( S == 'ABC' or S == 'ARC' ): sys.exit() print('ABC') if S == 'ARC' else print('ARC')
0
null
20,344,215,246,082
136
153
n = int(input()) s = list(input()) s = [ord(i)-97 for i in s] dic = {} for i in range(26): dic[i] = [] for i in range(n): dic[s[i]].append(i) for i in range(26): dic[i].append(float('inf')) from bisect import bisect_left q = int(input()) for i in range(q): x, y, z = input().split() if x == '1': y, z = int(y) - 1, ord(z) - 97 p = bisect_left(dic[s[y]], y) dic[s[y]].pop(p) dic[z].insert(bisect_left(dic[z], y), y) s[y] = z else: res = 0 y, z = int(y) - 1, int(z) - 1 for i in range(26): p = dic[i][bisect_left(dic[i], y)] if p <= z: res += 1 print(res)
s= input(); q = int(input()); flag = 0; fornt = ""; back = ""; while(q>0): q-=1; ls = list(map(str,input().split(" "))); if(ls[0]=='1'): flag +=1; else: if(flag%2 == 1): if(ls[1]=='2'): fornt=ls[2]+fornt; else: back=back+ls[2]; else: if(ls[1]=='1'): fornt=ls[2]+fornt; else: back=back+ls[2]; s = fornt+s+back if(flag%2==0): print(s); else: print(s[::-1])
0
null
59,710,709,478,522
210
204
n = input() cnt = 0 for i in n: cnt += int(i) if cnt % 9 == 0: print('Yes') else: print('No')
count = input() #print(len(count)) num = 0 for i in range(len(count)): num += int(count[i]) if num % 9 == 0: print("Yes") else: print("No")
1
4,425,720,458,588
null
87
87
n, k = map(int, input().split()) W = [int(input()) for _ in range(n)] def can_load(W, k, q, n): i = 0 for _ in range(k): s = 0 while i < n and s + W[i] <= q: s += W[i] i += 1 if i == n: return True else: return False # 積載量をmidと仮定して二分探索 hi = 10 ** 9 # OK lo = 0 # NG while hi - lo > 1: mid = (hi + lo) // 2 if can_load(W, k, mid, n): hi = mid else: lo = mid print(hi)
N, K = map(int, input().split()) w = [int(input()) for _ in range(N)] def f(P): tmp_capacity = P num_used_truck = 1 i = 0 while i <= N-1: if w[i] > P: return i if w[i] > tmp_capacity: tmp_capacity = P num_used_truck += 1 if num_used_truck >= K+1: return i tmp_capacity -= w[i] i += 1 return i left = 0 right = 1e9 mid = right flag = 0 while right - left > 1: mid = (left + right)//2 tmp = f(mid) if tmp >= N: right = mid elif tmp < N: left = mid print(int(right))
1
92,693,463,760
null
24
24
N,K=map(int,input().split()) A=list(map(int,input().split())) imos=[0 for i in range(N+1)] for t in range(K): for i in range(N): l=max(0,i-A[i]) r=min(N,i+A[i]+1) imos[l]+=1 imos[r]-=1 for i in range(0,N): imos[i+1]+=imos[i] A[i]=imos[i] imos=[0 for i in range(N+1)] if len(set(A))==1 and (N in set(A)): break for i in range(N): A[i]=str(A[i]) ans=" ".join(A) print(ans)
n,k,*l=map(int,open(0).read().split()) for _ in range(min(k,41)): s=[0]*-~n for i in range(n): s[max(i-l[i],0)]+=1 s[min(i+l[i]+1,n)]-=1 for i in range(n): s[i+1]+=s[i] l=s print(*l[:n])
1
15,476,915,684,048
null
132
132
#!/usr/bin/env python3 import math n, m = map(int, input().split()) nans = (n * (n - 1)) // 2 mans = (m * (m - 1)) // 2 print(nans + mans)
# coding: utf-8 def gcd(a, b): if a < b: a, b = b, a while b: a, b = b, a % b return a def main(): a, b = map(int, raw_input().split()) print gcd(a, b) if __name__ == '__main__': main()
0
null
22,917,075,644,750
189
11
X,Y=map(int, input().split()) v = [300000, 200000, 100000] x = 0 y = 0 if X <= 3: x = int(v[X-1]) if Y <= 3: y = int(v[Y-1]) print(int(x + y + ((X==1) & (Y==1)) * 400000))
N, M, L = map(int, input().split()) D = [[1000000000000]*N for i in range(N)] for i in range(N): D[i][i] = 0 for i in range(M): A, B, C = map(int, input().split()) A, B = A-1, B-1 D[A][B] = C D[B][A] = C for k in range(N): for i in range(N): for j in range(N): D[i][j] = min(D[i][j], D[i][k]+D[k][j]) E = [[1000000000000]*N for i in range(N)] for i in range(N): E[i][i] = 0 for i in range(N): for j in range(N): if D[i][j] <= L: E[i][j] = 1 for k in range(N): for i in range(N): for j in range(N): E[i][j] = min(E[i][j], E[i][k]+E[k][j]) Q = int(input()) for i in range(Q): s, t = map(int, input().split()) s, t = s-1, t-1 if E[s][t] == 1000000000000: r = -1 else: r = E[s][t]-1 print(r)
0
null
156,744,656,094,962
275
295
i=0 j = int(input()) numlist = list(int(i) for i in input().split()) while i < j-1: print(numlist[j-i-1],end='') print(' ',end='') i += 1 print(numlist[0])
input() a=list(map(int, input().split())) a.reverse() print(*a)
1
985,363,831,232
null
53
53
K = int(input()) ans = -1 tmp = 0 for i in range(1, K + 1): tmp = (tmp*10%K + 7) % K if tmp == 0: ans = i break print(ans)
import sys X = int(input()) for a in range(-200,200): for b in range(-200,200): if a ** 5 - b ** 5 == X: print(a,b) sys.exit()
0
null
15,949,326,321,922
97
156
n_s = list(input().rstrip()) n_t = list(input().rstrip()) cnt = 0 for i in range(len(n_s)): if n_s[i] != n_t[i]: cnt += 1 print(cnt)
str1 = input() str2 = input() count = 0 for i in range(len(str1)): if str1[i] != str2[i]: count += 1 print(count)
1
10,562,467,813,972
null
116
116
k = int(input()) now = 7 cnt = 1 used = [False] * k ten = 10 while not used[now % k]: if now % k == 0: print(cnt) exit() used[now % k] = True now = (now + ten * 7) % k ten = ten * 10 % k cnt += 1 print(-1)
def dfs(v, cnt): D[v] = cnt cnt += 1 for c in edge[v]: if D[c] == -1: cnt = dfs(c, cnt) F[v] = cnt cnt += 1 return cnt V = int(input()) edge = [[] for _ in range(V)] for _ in range(V): u, _, *v = map(lambda x: int(x)-1, input().split()) edge[u] = sorted(v) D, F = [-1] * V, [-1] * V c = 1 for i in range(V): if D[i] == -1: c = dfs(i, c) for i, (d, f) in enumerate(zip(D, F)): print(i+1, d, f)
0
null
3,027,958,582,240
97
8
moji = input() if moji.isupper() == True: print("A") else: print("a")
a=input() b=a.capitalize() if a==b: print("A") else: print("a")
1
11,292,817,511,078
null
119
119
W = input() count = 0 #T = input().lower() #print(T) #while T != 'END_OF_TEXT': while True: T = input() if T == 'END_OF_TEXT': break for i in T.lower().split(): if W == i: count += 1 #T = input().lower() #print(i) print(count)
import math print(2**(int(math.log2(int(input())))+1)-1)
0
null
40,882,224,993,230
65
228
N = int(input()) rob = [] for i in range(N): X, L = map(int, input().split()) rob.append([X-L, X+L]) rob.sort(key=lambda x: x[1]) ans = N for i in range(N-1): if rob[i+1][0] < rob[i][1]: rob[i+1][1] = rob[i][1] ans -= 1 print(ans)
M,a=-float("inf"),0 for X,L in sorted(zip(*[iter(map(int,open(0).read().split()[1:]))]*2),key=sum): l,r=X-L,X+L if M<=l:a+=1;M=r print(a)
1
90,308,342,201,428
null
237
237
class Dice: def __init__(self, men): self.men = men def throw(self, direction): if direction == "E": pmen = men[:] men[0] = pmen[3] men[1] = pmen[1] men[2] = pmen[0] men[3] = pmen[5] men[4] = pmen[4] men[5] = pmen[2] elif direction == "W": pmen = men[:] men[0] = pmen[2] men[1] = pmen[1] men[2] = pmen[5] men[3] = pmen[0] men[4] = pmen[4] men[5] = pmen[3] elif direction == "S": pmen = men[:] men[0] = pmen[4] men[1] = pmen[0] men[2] = pmen[2] men[3] = pmen[3] men[4] = pmen[5] men[5] = pmen[1] elif direction == "N": pmen = men[:] men[0] = pmen[1] men[1] = pmen[5] men[2] = pmen[2] men[3] = pmen[3] men[4] = pmen[0] men[5] = pmen[4] def printUe(self): print (self.men)[0] def printMen(self): print self.men men = map(int, (raw_input()).split(" ")) d = Dice(men) S = raw_input() for s in S: d.throw(s) d.printUe()
class Dice: def __init__(self, hoge): self.num = hoge self.face = {'U': 0, 'W': 3, 'E': 2, 'N': 4, 'S': 1, 'B': 5} def move(self, direct): previouse = {k: v for k, v in self.face.items()} if direct == 'E': self.face['U'] = previouse['W'] self.face['W'] = previouse['B'] self.face['E'] = previouse['U'] self.face['B'] = previouse['E'] elif direct == 'W': self.face['U'] = previouse['E'] self.face['W'] = previouse['U'] self.face['E'] = previouse['B'] self.face['B'] = previouse['W'] elif direct == 'N': self.face['U'] = previouse['S'] self.face['N'] = previouse['U'] self.face['S'] = previouse['B'] self.face['B'] = previouse['N'] elif direct == 'S': self.face['U'] = previouse['N'] self.face['N'] = previouse['B'] self.face['S'] = previouse['U'] self.face['B'] = previouse['S'] def get_upper(self): return self.num[self.face['U']] if __name__ == '__main__': dice = Dice([int(x) for x in input().split()]) for d in input(): dice.move(d) print (dice.get_upper())
1
232,763,683,552
null
33
33
from math import gcd from collections import defaultdict n=int(input()) abd=defaultdict(int) mod=10**9+7 wzero=0 azero=0 bzero=0 for i in range(n): a,b=map(int,input().split()) if a==0 and b==0: wzero+=1 elif a==0 and b!=0: azero+=1 elif a!=0 and b==0: bzero+=1 else: g=gcd(a,b) a=a//g b=b//g abd[(a,b)]+=1 liskey=list(abd.keys()) ans=1 for k in liskey: if abd[k] != 0: a,b=k x=abd[(a,b)]+abd[(-a,-b)] y=abd[(-b,a)]+abd[(b,-a)] abd[(a,b)]=0 abd[(-a,-b)]=0 abd[(-b,a)]=0 abd[(b,-a)]=0 ans*=(2**x+2**y-1+mod)%mod ans%=mod ans*=(2**azero+2**bzero-1) ans%=mod ans=(ans+wzero)%mod ans=(ans+mod-1)%mod print(ans)
import sys sys.setrecursionlimit(300000) from fractions import gcd from collections import defaultdict def I(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LMI(): return list(map(int, sys.stdin.readline().split())) MOD = 10 ** 9 + 7 INF = float('inf') N = I() d = defaultdict(int) zero = 0 for i in range(N): a, b = MI() if a == 0 and b == 0: zero += 1 continue g = gcd(a, b) a, b = a // g, b // g minus = (-1) ** ((a < 0) + (b < 0)) d[(minus * abs(a), abs(b))] += 1 ans = 1 seen = set() for a, b in d: if (a, b) not in seen: if (a, b) in [(1, 0), (0, 1)]: ainv, binv = b, a else: ainv, binv = b * (1 if a < 0 else -1), abs(a) lans = (2 ** d[(a, b)] - 1) lans += (2 ** d[(ainv, binv)] - 1) if d.get((ainv, binv)) is not None else 0 lans += 1 ans = ans * lans % MOD seen.add((a, b)) seen.add((ainv, binv)) ans = (ans + zero) % MOD print((ans + MOD - 1) % MOD)
1
20,826,264,409,980
null
146
146
n,x,m=map(int,input().split()) def f(ai,m): return (ai*ai)%m if x==0: print(0) elif x==1: print(n) elif m==1: print(0) elif n<=m*2: asum=x ai=x for i in range(1,n): ai=f(ai,m) asum+=ai print(asum) else: chk=[-1]*m asum00=[0]*m ai,asum=x,0 for i in range(m): if chk[ai]!=-1: icnt0=chk[ai] break else: chk[ai]=i asum00[i]=asum asum+=ai ai=f(ai,m) icnt=i asum0=asum00[icnt0] icntk=icnt-icnt0 n0=n-1-icnt0 nk=n0//icntk nr=n0%icntk asumk=asum-asum0 air,asumr=ai,0 for i in range(nr): asumr+=air air=f(air,m) asumr+=air ans=asum0+asumk*nk+asumr print(ans)
N,X,M = map(int, input().split()) A = [X] S = set(A) cnt = 0 i = 0 for i in range(1, N): A.append(A[i-1] ** 2 % M) if A[i] in S: break else: S.add(A[i]) roup_cycle = i - A.index(A[i]) before_roup_sums = sum(A[:A.index(A[i])]) roup_sums = sum(A[A.index(A[i]):i]) if roup_cycle != 0: roup_amount = (N - A.index(A[i])) // roup_cycle roup_mod = (N - A.index(A[i])) % roup_cycle print(before_roup_sums + roup_sums * roup_amount + sum(A[A.index(A[i]):(A.index(A[i]) + roup_mod)])) else: print(sum(A))
1
2,824,743,612,550
null
75
75
while True: line = input() if line == '0': break n = 0 for c in line: n += int(c) print(n)
N = int(input()) if N % 2 == 0: print("0.5") else: a = N // 2 + 1 print(a/N)
0
null
89,231,412,145,938
62
297
n,k = map(int,input().split()) print((n%k) if (n%k) < (k-(n%k)) else (k-(n%k)))
n,k = [int(x) for x in input().split()] a = n // k n -= a * k print(min(n,abs(n-k)))
1
39,165,209,971,520
null
180
180
mod = 10**9+7 N, K = map(int,input().split()) A = sorted(list(map(int,input().split()))) pi = 1 ni = 0 ans = 1 i = 0 while i < K-1: if A[ni]*A[ni+1] > A[-pi]*A[-pi-1]: ans = ans*A[ni]*A[ni+1]%mod ni += 2 i += 2 else: ans = ans*A[-pi]%mod pi += 1 i += 1 if i == K-1: ans = ans*A[-pi]%mod if A[-1]<0 and K%2==1: ans = 1 for i in A[N-K:]: ans = ans*i%mod print(ans)
n,k=map(int,input().split()) a=sorted(list(map(int,input().split()))) mod=10**9+7 fac=[1]*n finv=[1]*n inv=[0]*n inv[1]=1 for i in range(2,n): fac[i]=fac[i-1]*i%mod inv[i]=mod-inv[mod%i]*(mod//i)%mod finv[i]=finv[i-1]*inv[i]%mod def comb(n,k): if n<k: return 0 if n<0 or k<0: return 0 return fac[n]*(finv[k]*finv[n-k]%mod)%mod ans=0 for i in range(n-k+1): ans-=a[i]*comb(n-i-1,k-1)%mod ans%=mod a=a[::-1] for i in range(n-k+1): ans+=a[i]*comb(n-i-1,k-1)%mod ans%=mod print(ans%mod)
0
null
52,616,219,839,400
112
242
# A - Duplex Printing from math import ceil print(ceil(int(input()) / 2))
# 2:01 X = int(input()) ans = 0 ans += X // 500 * 1000 X %= 500 ans += X // 5 * 5 print(ans)
0
null
50,652,565,228,762
206
185
s=input() q=int(input()) mae="" usr="" flg=True for i in range(q): a=list(map(str,input().split())) if a[0]=="1": if flg: flg=False elif flg==False: flg=True if a[0]=="2": if a[1]=="1": if flg: mae=a[2]+mae else: usr=usr+a[2] if a[1]=="2": if flg: usr=usr+a[2] else: mae=a[2]+mae if flg: ans=mae+s+usr else: ans=usr[::-1]+s[::-1]+mae[::-1] print(ans)
s = input() q = int(input()) f1 = 1 f2 = 2 st = "" se = "" for _ in range(q): q = input().split() if int(q[0]) == 1: f1,f2 = f2,f1 elif int(q[0]) == 2: if int(q[1]) == f1: st = q[2] + st elif int(q[1]) == f2: se += q[2] ans = st + s + se if f1 == 1: print(ans) else: ans = list(ans) ans.reverse() ans = "".join(ans) print(ans)
1
57,218,719,926,532
null
204
204
# -*- coding: utf-8 -*- import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10 ** 9) INF = 10 ** 18 MOD = 10 ** 9 + 7 class SegTree: """ セグメント木 1.update: i番目の値をxに更新する 2.query: 区間[l, r)の値を得る """ def __init__(self, n, func, intv, A=[]): """ :param n: 要素数(0-indexed) :param func: 値の操作に使う関数(min, max, add, gcdなど) :param intv: 要素の初期値(単位元) :param A: 初期化に使うリスト(オプション) """ self.n = n self.func = func self.intv = intv # nより大きい2の冪数 n2 = 1 while n2 < n: n2 <<= 1 self.n2 = n2 self.tree = [self.intv] * (n2 << 1) # 初期化の値が決まっている場合 if A: # 1段目(最下段)の初期化 for i in range(n): self.tree[n2+i] = A[i] # 2段目以降の初期化 for i in range(n2-1, -1, -1): self.tree[i] = self.func(self.tree[i*2], self.tree[i*2+1]) def update(self, i, x): """ i番目の値をxに更新 :param i: index(0-indexed) :param x: update value """ i += self.n2 self.tree[i] = x while i > 0: i >>= 1 self.tree[i] = self.func(self.tree[i*2], self.tree[i*2+1]) def query(self, a, b): """ [a, b)の値を得る :param a: index(0-indexed) :param b: index(0-indexed) """ l = a + self.n2 r = b + self.n2 s = self.intv while l < r: if r & 1: r -= 1 s = self.func(s, self.tree[r]) if l & 1: s = self.func(s, self.tree[l]) l += 1 l >>= 1 r >>= 1 return s def get(self, i): """ 一点取得 """ return self.tree[i+self.n2] def all(self): """ 全区間[0, n)の取得 """ return self.tree[1] N, M = MAP() S = input() dp = SegTree(N+1, min, INF) dp.update(N, 0) for i in range(N-1, -1, -1): # ゲームオーバーマスには遷移できない if S[i] == '1': continue mn = dp.query(i+1, min(N+1, i+M+1)) if mn != INF: dp.update(i, mn+1) # 辿り着けない if dp.get(0) == INF: print(-1) exit() cnt = dp.get(0) cur = 0 prev = -1 ans = [] for i in range(1, N+1): if dp.get(i) == INF: continue # 手数が変わる場所でなるべく手前に飛ぶ if dp.get(i) != cnt: prev = cur cur = i ans.append(cur-prev) cnt = dp.get(i) print(*ans)
import heapq def main(): N, M = list(map(int, input().split(' '))) S = input() # 最短手数のdpテーブルを作る T = S[::-1] # ゴールから逆順にたどる(最後に逆にする) dp = [-1] * (N + 1) que = [0] * M for i, t in enumerate(T): if i == 0: dp[0] = 0 continue if len(que) == 0: print(-1) return index = heapq.heappop(que) if t == '1': continue dp[i] = 1 + dp[index] while len(que) < M: heapq.heappush(que, i) dp.reverse() # 細切れに進んでいく path = list() target = dp[0] - 1 cnt = 0 for i in range(N + 1): if dp[i] != target: cnt += 1 else: path.append(cnt) cnt = 1 target -= 1 print(' '.join(map(str, path))) if __name__ == '__main__': main()
1
139,183,084,384,060
null
274
274
import math n=int(input()) a=list(map(int,input().split())) soin = [0]*(10**6+1) if a.count(1)==n: print("pairwise coprime") exit() def factorization(n): arr = [] temp = n for i in range(2, int(-(-n ** 0.5 // 1)) + 1): if temp % i == 0: while temp % i == 0: temp //= i arr.append(i) if temp != 1: arr.append(temp) if arr == []: arr.append(n) return arr if len(a)>80000: ans=a[0] for i in a: ans=math.gcd(ans,i) if ans==1: print("setwise coprime") else: print("not coprime") else: for i in a: if i ==1:continue for num in factorization(i): soin[num]+=1 if max(soin)==1: print("pairwise coprime") elif max(soin)==n and 1 not in a: print("not coprime") else:print("setwise coprime")
from math import gcd n = int(input()) a = list(map(int, input().split())) max_a = max(a) cnt_a = [0] * (max_a + 1) for ai in a: cnt_a[ai] += 1 ans = 'pairwise' for i in range(2, max_a + 1): cnt = 0 for j in range(i, max_a + 1, i): cnt += cnt_a[j] if cnt > 1: ans = 'setwise' g = 0 for ai in a: g = gcd(g, ai) if ans == 'pairwise': print(ans, 'coprime') elif ans == 'setwise' and g == 1: print(ans, 'coprime') else: print('not coprime')
1
4,104,094,714,720
null
85
85
n = int(input()) ans = 0 for a in range(1, n+1): cnt = (n-1)//a ans += cnt print(ans)
import math n = int(input()) address = list(map(int, input().split())) min_hp = math.inf for i in range(1, 101): hp = sum(map(lambda x: (x - i) ** 2, address)) if hp < min_hp: min_hp = hp print(min_hp)
0
null
33,873,678,031,388
73
213
n = int(input()) li = ["#"*20 if i%4==0 else "0"*10 for i in range(1, 16)] for _ in range(n): b, f, r, v = map(int, input().split()) h = 4*b-(4-f)-1 li[h] = li[h].replace(li[h], ''.join([str(int(list(li[h])[r-1])+v) if i == r-1 else list(li[h])[i] for i in range(10)])) li = [' '+' '.join(li[i]) if (i+1)%4!=0 else li[i] for i in range(len(li))] print('\n'.join(li))
ryou = [0]*4 for i in range(4): ryou[i] = [0]*3 for i in range(4): for j in range(3): ryou[i][j] = [0]*10 sep = '#' * 20 n = int(input()) for i in range(n): b, f, r, v = [int(x) for x in input().split()] ryou[b-1][f-1][r-1] += v for i in range(4): for j in range(3): s = ' ' s += ' '.join(map(str,ryou[i][j])) print(s) if not i == 3: print(sep)
1
1,095,429,707,390
null
55
55
import sys input = sys.stdin.readline H,N = map(int,input().split()) spells = [list(map(int,input().split())) for i in range(N)] INF = 10**10 dp = [INF]*(H+1) dp[0] = 0 for use in spells: damage = use[0] mp = use[1] for i in range(1,H+1): dp[i] = min(dp[max(0,i-damage)] + mp, dp[i]) print(dp[-1])
h,n = map(int,input().split()) M = [] max_val = 0 for _ in range(n): a,b = map(int,input().split()) max_val = max(max_val,a) M.append((a,b)) dp = [float('inf')]*(h+1+max_val) dp[0] = 0 for i in range(1,h+1+max_val): for j in range(n): dp[i] = min(dp[i-M[j][0]] + M[j][1],dp[i]) print(min(dp[h:-1]))
1
81,379,746,928,110
null
229
229
if __name__ == '__main__': p_taro, p_hanako = 0, 0 n = int(input()) for i in range(n): animal_taro, animal_hanako = input().split() if animal_taro > animal_hanako: p_taro += 3 elif animal_taro < animal_hanako: p_hanako += 3 else: p_taro, p_hanako = p_taro+1, p_hanako+1 print(p_taro, p_hanako)
n = int(input()) x = 0 y = 0 for _ in range(n): a,b=input().split() if a < b: y+=3 elif a ==b: x+=1 y+=1 else: x+=3 print (x,y)
1
2,004,516,020,320
null
67
67
ABC = list(map(int,input().split())) red = ABC[0] green = ABC[1] blue = ABC[2] K = int(input()) for i in range(K): if green <= red: green *= 2 continue elif blue <= green: blue *= 2 if green > red and blue > green: print('Yes') else: print('No')
W, H, x, y, r = map(int, input().split()) if r <= x <= W-r and r <= y <= H-r : print('Yes') else : print('No')
0
null
3,645,776,700,632
101
41
import math def koch(d, p1, p2): if d == 0: return sx = (2*p1[0]+1*p2[0])/3 sy = (2*p1[1]+1*p2[1])/3 s = (sx, sy) tx = (1*p1[0]+2*p2[0])/3 ty = (1*p1[1]+2*p2[1])/3 t = (tx, ty) rad = math.radians(60) ux = (t[0]-s[0])*math.cos(rad)-(t[1]-s[1])*math.sin(rad)+s[0] uy = (t[0]-s[0])*math.sin(rad)+(t[1]-s[1])*math.cos(rad)+s[1] u = (ux, uy) koch(d-1, p1, s) print(s[0], s[1]) koch(d-1, s, u) print(u[0], u[1]) koch(d-1, u, t) print(t[0], t[1]) koch(d-1, t, p2) d = int(input()) p1 = (0, 0) p2 = (100, 0) print(p1[0], p1[1]) koch(d, p1, p2) print(p2[0], p2[1])
N = int(input()) def kccv(a, b, c, d): return [a*2/3 + c/3, b*2/3 + d/3, a/2 + b*(3**0.5)/6 + c/2 - d*(3**0.5)/6, -a*(3**0.5)/6 + b/2 + c*(3**0.5)/6 + d/2, a/3 + c*2/3, b/3 + d*2/3] ans = [[[0,0],[100,0]]] for i in range(1,N+1): m = len(ans[i-1]) newset = [] for j in range(m-1): newset.append(ans[i-1][j]) newdot = kccv(ans[i-1][j][0], ans[i-1][j][1], ans[i-1][j+1][0], ans[i-1][j+1][1]) x1 = newdot[0] y1 = newdot[1] x2 = newdot[2] y2 = newdot[3] x3 = newdot[4] y3 = newdot[5] newset.append([x1,y1]) newset.append([x2,y2]) newset.append([x3,y3]) newset.append(ans[i-1][m-1]) ans.append(newset) for i in range(len(ans[N])): print(ans[N][i][0],ans[N][i][1])
1
129,575,147,648
null
27
27
a1 = [[0 for i in range(10)] for j in range(3)] a2 = [[0 for i in range(10)] for j in range(3)] a3 = [[0 for i in range(10)] for j in range(3)] a4 = [[0 for i in range(10)] for j in range(3)] n = int(input()) for i in range(0,n): x = [int (z) for z in input().split(' ')] if x[0] == 1: a1[x[1]-1][x[2]-1] += x[3] elif x[0] == 2: a2[x[1]-1][x[2]-1] += x[3] elif x[0] == 3: a3[x[1]-1][x[2]-1] += x[3] elif x[0] == 4: a4[x[1]-1][x[2]-1] += x[3] for t in range(0,3): for i in a1[t]: print(" %d" % i, end='') print('') print("####################") for t in range(0,3): for i in a2[t]: print(" %d" % i, end='') print('') print("####################") for t in range(0,3): for i in a3[t]: print(" %d" % i, end='') print('') print("####################") for t in range(0,3): for i in a4[t]: print(" %d" % i, end='') print('')
n, p = map(int, input().split()) s = input() ruisekiwa = [0 for _ in range(0, n + 1)] if 10 % p == 0: ans = 0 for i in range(0, n): if (ord(s[i]) - ord('0')) % p == 0: ans += i + 1 print(ans) exit() ten = 1 for _i in range(0, n): i = n - _i - 1 ruisekiwa[n - i] = ((ord(s[i]) - ord('0')) * ten + ruisekiwa[n - i - 1]) % p ten *= 10 ten %= p ans = 0 cnt = [0 for _ in range(0, p)] for i in range(0, n+1): ans += cnt[ruisekiwa[i]] cnt[ruisekiwa[i]] += 1 print(ans)
0
null
29,659,486,815,708
55
205
from collections import deque S=deque(input()) Q=int(input()) ans=0 for i in range(Q): q=input() if q[0]=='2': if ans%2==0: if q[2]=='1': S.appendleft(q[4]) else: S.append(q[4]) else: if q[2]=='1': S.append(q[4]) else: S.appendleft(q[4]) else: ans+=1 S="".join(S) print(S if ans%2==0 else S[::-1])
from collections import deque S = deque(input()) Q = int(input()) reverse_count = 0 for _ in range(Q): Query = input().split() if Query[0] == "1": reverse_count += 1 else: F, C = Query[1], Query[2] if F == "1": if reverse_count % 2 == 0: S.appendleft(C) else: S.append(C) else: if reverse_count % 2 == 0: S.append(C) else: S.appendleft(C) if reverse_count % 2 == 1: S.reverse() print("".join(S))
1
57,218,376,768,442
null
204
204
terms = '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'.split(', ') print(terms[int(input()) - 1])
array_str = '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' array = array_str.split(', ') k = int(input()) print(array[k - 1])
1
49,799,348,840,348
null
195
195
a, b, c = map(int, input().split()) if a*a + b*b + c*c -2*(a*b + b*c + c*a) > 0 and c-a-b > 0: print("Yes") else: print("No")
a, b, c = map(int, input().split()) d = c - a - b if d > 0 and d ** 2 > 4 * a * b: print('Yes') else: print('No')
1
51,587,933,836,426
null
197
197
m,d=map(int,input().split()) if m in [1,3,5,7,8,10,12] and d==31: print(1) elif m ==2 and d==28: print(1) elif m in [4,6,9,11] and d==30: print(1) else: print(0)
import math R = int(input()) round_R = 2 * R * math.pi print(round(round_R, 10))
0
null
78,248,088,097,620
264
167
import sys sys.setrecursionlimit(300000) def I(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LMI(): return list(map(int, sys.stdin.readline().split())) MOD = 998244353 INF = float('inf') N, A = MI() a = [0, *list(map(int, input().split()))] # dp[i][j] := i番目までの数字を使って総和をjとする場合の数 # dp[i][j] = dp[i - 1][j - a[i]] + dp[i - 1][j] dp = [[0] * (A + 1) for _ in range(N + 1)] dp[0][0] = 1 for i in range(1, N + 1): for j in range(A + 1): if j - a[i] < 0: # j - a[i] が負の数になってしまう時はa[i]を使用することはできない dp[i][j] = 2 * dp[i - 1][j] % MOD continue dp[i][j] = (dp[i - 1][j - a[i]] + 2 * dp[i - 1][j]) % MOD print(dp[N][A])
in_string = input() in_arr = in_string.split() N = int(in_arr[0]) A = int(in_arr[1]) B = int(in_arr[2]) if ((B-A) % 2) == 0: print(int((B-A)//2)) else: if (N-B) > A-1: print(int(A+(B-A-1)//2)) else: print(int(N-B+1+((N-(A+N-B+1))//2)))
0
null
63,609,777,123,680
138
253
from fractions import gcd from collections import Counter, deque, defaultdict from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort from itertools import product, combinations,permutations from copy import deepcopy import sys import numpy as np sys.setrecursionlimit(4100000) if __name__ == '__main__': N = int(input()) S = input() Z = ord('Z') ans = '' for s in S: if ord(s)+N>ord('Z'): ans += chr(ord('A')+ord(s)+N-ord('Z')-1) else: ans += chr(ord(s)+N) print(ans)
a,b,c,k=[int(i) for i in input().split()] if a>=k: print(k) elif a+b>=k: print(a) else: print(a-(k-a-b))
0
null
78,184,930,689,088
271
148
N = int(input()) a = list(map(int,input().split())) i=0 for x,y in enumerate (a): b=x+1 if b%2!=0 and y%2!=0: i+=1 print(i)
n = int(input()) c = list(input()) rcnt = 0 allrcnt = 0 for i in range(len(c)): if c[i]=='R': allrcnt += 1 for i in range(len(c)): if i+1<=allrcnt and c[i]=='R': rcnt += 1 ans = allrcnt-rcnt print(ans)
0
null
7,083,226,485,788
105
98
def MI(): return map(int, input().split()) N,X,T=MI() if N%X!=0: ans=(N//X+1)*T else: ans=N//X*T print(ans)
import math N, X, T = map(int, input().split()) t = int(math.ceil(N/X)) print('{}'.format(t*T))
1
4,299,374,360,340
null
86
86
m={"E":[3,1,0,5,4,2], "N":[1,5,2,3,0,4], "W":[2,1,5,0,4,3], "S":[4,0,2,3,5,1]} d=map(int, raw_input().split()) nd=[0]*6 for x in raw_input(): for j in range(6): nd[j]=d[m[x][j]] d=list(nd) print d[0]
n = input() ans = 0 for i,j in enumerate(map(int,input().split())): ans += (~i&1 and j&1) print(ans)
0
null
3,958,668,547,988
33
105
# https://atcoder.jp/contests/sumitrust2019/tasks/sumitb2019_e import sys input=sys.stdin.readline MOD=10**9+7 def main(): N=int(input()) A=list(map(int,input().split())) cnt=[0]*N ans=1 for i in range(N): if A[i]==0: ans*=3-cnt[0] ans%=MOD cnt[A[i]]+=1 else: ans*=cnt[A[i]-1]-cnt[A[i]] ans%=MOD cnt[A[i]]+=1 print(ans) if __name__=="__main__": main()
print(len(set(open(0).read().split())) - 1)
0
null
80,013,356,354,668
268
165
import sys from collections import deque import bisect import copy import heapq import itertools import math input = sys.stdin.readline sys.setrecursionlimit(1000000) mod = 10 ** 9 + 7 def read_values(): return map(int, input().split()) def read_index(): return map(lambda x: int(x) - 1, input().split()) def read_list(): return list(read_values()) def read_lists(N): return [read_list() for n in range(N)] def main(): R, C, K = read_values() V = [0] * (R * C) for _ in range(K): r, c, v = read_values() r -= 1 c -= 1 V[r * C + c] = v dp = [0 for _ in range(R * C * 4)] for i in range(R * C): r = i // C c = i % C if c + 1 < C: # not take for k in range(4): dp[4 * (i + 1) + k] = max(dp[4 * (i + 1) + k], dp[4 * i + k]) # take for k in range(3): dp[4 * (i + 1) + k + 1] = max(dp[4 * (i + 1) + k + 1], dp[4 * i + k] + V[i]) # next r if r + 1 < R: for k in range(4): dp[4 * (i + C)] = max(dp[4 * (i + C)], dp[4 * i + k] + (V[i] if k < 3 else 0)) res = 0 for k in range(4): res = max(res, dp[4 * (R * C - 1) + k] + (V[-1] if k < 3 else 0)) print(res) if __name__ == "__main__": main()
H,A = (int(x) for x in input().split()) print(H//A+(H%A>0))
0
null
41,128,992,338,092
94
225
n=raw_input() k=n.split(" ") a=int(k[0]) b=int(k[1]) if a<b: print "a < b" if a==b: print "a == b" if a>b: print "a > b"
a,b=[int(x) for x in input().split()] op = '>' if a>b else '<' if a<b else '==' print('a',op,'b')
1
346,926,130,122
null
38
38
import sys chash = {} for i in range( ord( 'a' ), ord( 'z' )+1 ): chash[ chr( i ) ] = 0 lines = sys.stdin.readlines() for i in range( len( lines ) ): for j in range( len( lines[i] ) ): if lines[i][j].isalpha(): chash[ lines[i][j].lower() ] += 1 for i in range( ord( 'a' ), ord( 'z' )+1 ): print( "{:s} : {:d}".format( chr( i ), chash[ chr( i ) ] ) )
import sys MOD = 10**9+7 def main(): input = sys.stdin.readline N,K=map(int, input().split()) ans=Mint() cnt=[Mint() for _ in range(K+1)] for g in range(K,0,-1): cnt[g]+=pow(K//g,N,MOD) for h in range(g+g,K+1,g): cnt[g]-=cnt[h] ans+=g*cnt[g] print(ans) class Mint: def __init__(self, value=0): self.value = value % MOD if self.value < 0: self.value += MOD @staticmethod def get_value(x): return x.value if isinstance(x, Mint) else x def inverse(self): a, b = self.value, MOD u, v = 1, 0 while b: t = a // b b, a = a - t * b, b v, u = u - t * v, v if u < 0: u += MOD return u def __repr__(self): return str(self.value) def __eq__(self, other): return self.value == other.value def __neg__(self): return Mint(-self.value) def __hash__(self): return hash(self.value) def __bool__(self): return self.value != 0 def __iadd__(self, other): self.value = (self.value + Mint.get_value(other)) % MOD return self def __add__(self, other): new_obj = Mint(self.value) new_obj += other return new_obj __radd__ = __add__ def __isub__(self, other): self.value = (self.value - Mint.get_value(other)) % MOD if self.value < 0: self.value += MOD return self def __sub__(self, other): new_obj = Mint(self.value) new_obj -= other return new_obj def __rsub__(self, other): new_obj = Mint(Mint.get_value(other)) new_obj -= self return new_obj def __imul__(self, other): self.value = self.value * Mint.get_value(other) % MOD return self def __mul__(self, other): new_obj = Mint(self.value) new_obj *= other return new_obj __rmul__ = __mul__ def __ifloordiv__(self, other): other = other if isinstance(other, Mint) else Mint(other) self *= other.inverse() return self def __floordiv__(self, other): new_obj = Mint(self.value) new_obj //= other return new_obj def __rfloordiv__(self, other): new_obj = Mint(Mint.get_value(other)) new_obj //= self return new_obj if __name__ == '__main__': main()
0
null
19,175,867,975,742
63
176
# -*- coding: utf-8 -*- 'import sys' input() a=[int(i) for i in input().split()] a.reverse() f=0 for i in range(len(a)): if f: print(" ",end="") print("{}".format(a[i]),end="") f=1 print("")
import heapq (N,K) = map(int,input().split()) h = [int(x)*-1 for x in input().split()] ans = 0 heapq.heapify(h) if K <= N: for i in range(K): heapq.heappop(h) while h != []: ans -= heapq.heappop(h) print(ans)
0
null
40,039,653,406,190
53
227
n = list(raw_input()) for i in xrange(len(n)): if n[i].isupper(): n[i]=n[i].lower() elif n[i].islower(): n[i]=n[i].upper() print ''.join(n)
# -*- coding: utf-8 -*- """ B - TAKOYAKI FESTIVAL 2019 https://atcoder.jp/contests/abc143/tasks/abc143_b """ import sys from itertools import combinations def solve(takoyaki): return sum(x * y for x, y in combinations(takoyaki, 2)) def main(args): _ = input() takoyaki = map(int, input().split()) ans = solve(takoyaki) print(ans) if __name__ == '__main__': main(sys.argv[1:])
0
null
84,922,929,806,706
61
292
N=int(input()) syogen=[] for i in range(N): a=int(input()) temp=[list(map(int, input().split())) for _ in range(a)] syogen.append([a,temp]) ans=0 for h in range(2**N): flag=0 cnt=0 l=[0]*N for g in range(N): if h>>g &1: l[g]=1 #print(l) for gg in range(N): if l[gg]==1: temp_=syogen[gg] #print(temp_) for n in range(temp_[0]): if l[temp_[1][n][0]-1]==temp_[1][n][1]: continue else: flag=1 break if flag==1: break elif flag==0 and gg==N-1: #print(bin(h)) ans=max(ans,sum(l)) print(ans)
def sep(): return map(int,input().strip().split(" ")) def lis(): return list(sep()) n,s=sep() ar=lis() ar.insert(0,0) dp=[[0]*(s+2) for _ in range(n+2)] dp[0][0]=1 N=998244353 for i in range(1,n+1): for j in range(0,s+1): dp[i][j]=(2*dp[i-1][j])%N if j-ar[i]>=0: dp[i][j]=(dp[i][j]+dp[i-1][j-ar[i]])%N #print(dp) print(dp[n][s])
0
null
69,856,339,279,890
262
138
# coding: utf-8 import sys from collections import deque n, q = map(int, input().split()) total_time = 0 tasks = deque(map(lambda x: x.split(), sys.stdin.readlines())) for task in tasks: task[1] = int(task[1]) try: while True: t = tasks.popleft() if t[1] - q <= 0: total_time += t[1] print(t[0], total_time) else: t[1] = t[1] - q total_time += q tasks.append(t) except Exception: pass
#16D8101014F 久留米 竜之介 Kurume Ryunosuke Python All = 0 q = [] Queue = [] tmp,time = map(int,input().split()) for i in range(tmp): p,zi =input().split() q.append([p,int(zi)]) while len(q) > 0: if q[0][1]<= time: All+=q[0][1] v=q.pop(0) Queue.append([v[0],All]) else: All+=time q[0][1]-=time last=q.pop(0) q.append(last) for i in range(len(Queue)): print("{0} {1}".format(Queue[i][0],Queue[i][1]))
1
44,706,045,382
null
19
19
from random import randint import sys input = sys.stdin.readline INF = 9223372036854775808 def calc_score(D, C, S, T): """ 開催日程Tを受け取ってそこまでのスコアを返す コンテストi 0-indexed d 0-indexed """ score = 0 last = [0]*26 # コンテストiを前回開催した日 for d, t in enumerate(T): last[t] = d + 1 for i in range(26): score -= (d + 1 - last[i]) * C[i] score += S[d][t] return score def update_score(D, C, S, T, score, ct, ci): """ ct日目のコンテストをコンテストciに変更する スコアを差分更新する ct: change t 変更日 0-indexed ci: change i 変更コンテスト 0-indexed """ last = [0]*26 # コンテストiを前回開催した日 for d in range(ct): last[T[d]] = d + 1 prei = T[ct] # 変更前に開催する予定だったコンテストi score -= S[ct][prei] score += S[ct][ci] for d in range(ct, D): if d != ct and T[d] == prei: break score += C[prei]*(d + 1 - ct - 1) score -= C[prei]*(d + 1 - last[prei]) for d in range(ct, D): if d != ct and T[d] == ci: break score += C[ci]*(d + 1 - last[ci]) score -= C[ci]*(d + 1 - ct - 1) T[ct] = ci return score, T def evaluate(D, C, S, T, k): """ d日目終了時点での満足度を計算し, d + k日目終了時点での満足度の減少も考慮する """ score = 0 last = [0]*26 for d, t in enumerate(T): last[t] = d + 1 for i in range(26): score -= (d + 1 - last[i]) * C[i] score += S[d][t] for d in range(len(T), min(len(T) + k, D)): for i in range(26): score -= (d + 1 - last[i]) * C[i] return score def greedy(D, C, S): Ts = [] for k in range(5, 10): T = [] # 0-indexed max_score = -INF for d in range(D): # d+k日目終了時点で満足度が一番高くなるようなコンテストiを開催する max_score = -INF best_i = 0 for i in range(26): T.append(i) score = evaluate(D, C, S, T, k) if max_score < score: max_score = score best_i = i T.pop() T.append(best_i) Ts.append((max_score, T)) return max(Ts, key=lambda pair: pair[0]) def local_search(D, C, S, score, T): ct = randint(0, D-1) ci = randint(0, 25) for _ in range(400000): new_score, new_T = update_score(D, C, S, T, score, ct, ci) if score < new_score: score = new_score T = new_T return T if __name__ == '__main__': D = int(input()) C = [int(i) for i in input().split()] S = [[int(i) for i in input().split()] for j in range(D)] init_score, T = greedy(D, C, S) T = local_search(D, C, S, init_score, T) for t in T: print(t+1)
import copy import random import time def down(d, last, c): result = 0 for i in range(26): result += c[i] * (d - last[i]) return result def calc_score_of_the_day(S, d, t, last, C): return S[d][t] - down(d+1, last, C) + C[t] * (d+1 - last[t]) def main(): start = time.time() D = int(input()) C = [int(c) for c in input().split()] S = [0] * D for d in range(D): S[d] = [int(s) for s in input().split()] last = [0] * 26 scores = [float('-inf')] * D result = 0 T = [0] * D for d in range(D): next_t = 0 score = float('-inf') for t in range(26): s = calc_score_of_the_day(S, d, t, last, C) if s > score: score = s next_t = t last[next_t] = d + 1 result += score scores[d] = result T[d] = next_t + 1 while time.time() - start < 1.9: result = scores[-1] change_date = 4 #random.randint(0, D-1) change_from = T[change_date] change_to = 13 # random.randint(1, 26) if change_from == change_to: continue copied_T = copy.copy(T) copied_T[change_date] = change_to last_of_the_day = [t if t < change_date else 0 for t in last] copied_scores = copy.copy(scores) for d in range(change_date, D): t = copied_T[d] - 1 score = calc_score_of_the_day(S, d, t, last_of_the_day, C) last_of_the_day[t] = d + 1 before = copied_scores[d-1] if d > 0 else 0 copied_scores[d] = before + score copied_T[d] = t + 1 if copied_scores[-1] > result: scores = copied_scores T = copied_T for t in T: print(t) if __name__ == "__main__": main()
1
9,726,035,168,512
null
113
113
N, K = map(int, input().split()) def f(x): return( N-x*K ) def test(x): return( f(x) >= 0 ) left = - 1 # return = right = (取り得る値の最小値) の可能性を排除しないために、-1 が必要 right = 10**18 while right - left > 1: # 最終的に (right, left, mid) = (最小値, 最小値 - 1, 最小値 - 1) に収束するため、差が 1 になったときに終了すればよい mid = (left+right)//2 if test(mid): left = mid else: right = mid print(min(abs(N-left*K), abs(N-right*K)))
n,k=map(int,input().split()) if n>=k: n%=k ans=min(n,k-n) print(ans)
1
39,547,855,731,250
null
180
180
c = int(input()) n = 0 for i in range(1,10): if c /i < 10 and c %i ==0: n += 1 break ans = 'Yes' if n >= 1 else 'No' print(ans)
from collections import Counter S=input() K=int(input()) S_double = S*2 n = 0 if len(set(list(S)))==1: print(len(S)*K//2) exit() while n< len(S_double)-1: if S_double[n] ==S_double[n+1]: S_double = S_double[:n+1]+' '+S_double[n+2:] n += 1 ans = Counter(S_double[:len(S)])[' '] + Counter(S_double[len(S):])[' ']*(K-1) print(ans)
0
null
167,405,930,477,372
287
296
N = int(input()) S = input() ans = 1 for i in range(N-1): if(S[i] != S[i+1]): ans += 1 print(ans)
N=int(input()) a=int(N/100) b=int((N-100*a)/10) c=N-100*a-10*b if a==7 or b==7 or c==7: print("Yes") else: print("No")
0
null
102,613,665,298,552
293
172
n, x, t = map(int, input().split()) time = 0 cnt = 0 while cnt < n: time += t cnt += x print(time)
from fractions import gcd def lcm(*args): ans = 1 for x in args: ans = ans * x // gcd(ans, x) return ans n, m = map(int, input().split()) a = [int(x) for x in input().split()] def log(x): ans = 0 while x % 2 == 0: ans += 1 x //= 2 return ans def solve(a): tmp = None for ta in a: if tmp is None: tmp = log(ta) else: if tmp == log(ta): continue else: print(0) return l = lcm(*a) print(m // (l//2) - m // l) solve(a)
0
null
53,064,249,319,868
86
247
## coding: UTF-8 #import random import math N = int(input()) A = list(map(int,input().split())) #N = 9 #A = [1, 2, 3, 40, 50, 60, 100, 200, 300] A = sorted(A, reverse = True) #print(N, A) score = 0 for i in range(1, N): index = math.floor(i/2) score += A[index] #print(i, index, A[index], score) print(score)
a, b, k = map(int,input().split()) if a <= k: k -= a a = 0 if b <= k: b =0 else: b -= k else: a -= k print(a, b)
0
null
56,777,857,511,648
111
249
N = str(input()) lenN = len(N) fN =N[0:(lenN - 1)//2] lN =N[(lenN + 3)//2-1:] print(("No","Yes")[(0,1)[fN == fN[-1::-1]]*(0,1)[lN == lN[-1::-1]]*(0,1)[lN == fN]])
import sys s=list(input()) r=list(reversed(s)) n=len(s) for i in range(n): if s[i]!=r[i]: print('No') sys.exit() s1=s[:] del s1[(n-1)//2:] r1=list(reversed(s1)) for i in range(len(s1)): if s1[i]!=r1[i]: print('No') sys.exit() s2=s[:] del s2[:(n+3)//2-1] r2=list(reversed(s2)) for i in range(len(s2)): if s2[i]!=r2[i]: print('No') sys.exit() print('Yes')
1
46,418,007,161,852
null
190
190
while True: s = input() if s == '-': break l = sum([int(input()) for _ in range(int(input()))]) % len(s) print(s[l:]+s[:l])
n, m = map(int, input().split()) ans = ((n*(n-1))+(m*(m-1)))//2 print(ans)
0
null
23,804,714,773,422
66
189
from collections import defaultdict MOD = 2019 def main(): S = input() dp = [0] * (len(S)+1) ex = 1 for i in range(len(S)): dp[i+1] = (int(S[-1-i])*ex + dp[i]) % MOD ex *= 10 ex %= MOD d = defaultdict(int) for i in range(len(S)+1): d[dp[i]] += 1 output = 0 for i in d.keys(): output += (d[i] * (d[i]-1) // 2) print(output) return if __name__ == '__main__': main()
import math n, d = map(int, input().split()) p = 0 for i in range(n): a, b = map(int, input().split()) a = a ** 2 b = b ** 2 c = math.sqrt(a + b) if c <= d: p = p + 1 print(p)
0
null
18,316,521,033,216
166
96
N,K=list(map(int,input().split())) A=sorted(list(map(int,input().split()))) mod=10**9+7 def modinv(x): return pow(x,mod-2,mod) comb=1 ans=0 for i in range(N-K+1): ans+=comb*A[K-1+i] ans-=comb*A[N-K-i] ans%=mod comb*=(K+i)*modinv(i+1) comb%=mod print(ans)
#!/usr/bin/env python # -*- coding: utf-8 -*- # # FileName: D_fix_3 # CreatedDate: 2020-08-30 13:22:07 +0900 # LastModified: 2020-08-30 13:41:03 +0900 # import os import sys # import numpy as np # import pandas as pd def main(): S = input() Q = int(input()) appendix_1 = '' appendix_2 = '' reverse = 0 for _ in range(Q): q = list(input().split()) if q[0] == '1': reverse += 1 else: if q[1] == '1' and reverse % 2 == 0: appendix_1 += q[2] elif q[1] == '1' and reverse % 2 == 1: appendix_2 += q[2] if q[1] == '2' and reverse % 2 == 0: appendix_2 += q[2] elif q[1] == '2' and reverse % 2 == 1: appendix_1 += q[2] if reverse % 2 == 0: print(appendix_1[::-1] + S + appendix_2) else: print(appendix_2[::-1] + S[::-1] + appendix_1) if __name__ == "__main__": main()
0
null
76,484,404,046,792
242
204
N, K = map(int, input().split()) ans = 0 mod = 10 ** 9 + 7 count = [0] * (K+10) for i in range(K,0,-1): t = pow(K//i, N, mod) j = 2 while j * i <= K: t -= count[j*i] j += 1 count[i] = t # tがiの倍数からなる数列の個数であることが確定しているはず ans += t * i ans %= mod print(ans)
N, K = map(int, input().split()) MOD = 10 ** 9 + 7 c = [0] * (K + 1) for i in range(K, 0, -1): t = pow(K // i, N, MOD) for j in range(2, K // i + 1): t -= c[i * j] t %= MOD c[i] = t result = 0 for i in range(1, K + 1): result += c[i] * i result %= MOD print(result)
1
36,789,540,247,280
null
176
176
num = int(input()) s = list(map(int, input().split())) cnt = 0 def merge(A, left, mid, right): global cnt L = A[left:mid] + [10**9 + 1] R = A[mid:right] + [10**9 + 1] i = 0 j = 0 for k in range(left, right): if L[i] <= R[j]: A[k] = L[i] i += 1 else: A[k] = R[j] j += 1 cnt += right - left def merge_sort(A, left, right): if (left + 1) < right: mid = (left + right) // 2 merge_sort(A, left, mid) merge_sort(A, mid, right) merge(A, left, mid, right) merge_sort(s, 0, num) print(*s) print(cnt)
infinity=2**35 def merge(A,left,mid,right): global cnt L=A[left:mid] R=A[mid:right] L.append(infinity) R.append(infinity) i=0 j=0 for k in range(left,right): cnt+=1 if L[i]<=R[j]: A[k]=L[i] i+=1 else: A[k]=R[j] j+=1 def mergeSort(A,left,right): if (left+1)<right: mid=(left+right)//2 mergeSort(A,left,mid) mergeSort(A,mid,right) merge(A,left,mid,right) n=int(input()) A=list(map(int,input().split())) cnt=0 mergeSort(A,0,n) print(*A) print(cnt)
1
113,518,507,662
null
26
26
from collections import deque k = int(input()) q = deque([1, 2, 3, 4, 5, 6, 7, 8, 9]) cnt = 0 while q: curr = q.popleft() cnt += 1 if cnt == k: break if curr % 10 == 9: q.append(curr * 10 + curr % 10 - 1) q.append(curr * 10 + curr % 10) elif curr % 10 == 0: q.append(curr * 10 + curr % 10) q.append(curr * 10 + curr % 10 + 1) else: q.append(curr * 10 + curr % 10 - 1) q.append(curr * 10 + curr % 10) q.append(curr * 10 + curr % 10 + 1) print(curr)
import sys, re from collections import deque, defaultdict, Counter from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby from operator import itemgetter, mul from copy import deepcopy from string import ascii_lowercase, ascii_uppercase, digits from bisect import bisect, bisect_left, insort, insort_left from heapq import heappush, heappop from functools import reduce def input(): return sys.stdin.readline().strip() def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(): return list(map(str, input().split())) def ZIP(n): return zip(*(MAP() for _ in range(n))) sys.setrecursionlimit(10 ** 9) INF = float('inf') mod = 10 ** 9 + 7 #import numpy as np from decimal import * K = INT() n = [-1]*11 cnt = 0 def DFS(n): global cnt for i in range(1, 11): if n[-i] == 9: continue elif n[-i-1] != -1 and n[-i] == n[-i-1]+1: continue else: if n[-i] == -1: n[-i] = 1 else: n[-i] += 1 for j in range(i-1, 0, -1): n[-j] = max(0, n[-j-1] - 1) break cnt += 1 if cnt == K: print(*n[bisect(n, -1):], sep = "") exit() DFS(n) DFS(n)
1
40,085,811,163,398
null
181
181
import sys readline = sys.stdin.buffer.readline a,b,c,k =list(map(int,readline().rstrip().split())) if k <= a: print(k) elif k > a and k <= a + b: print(a) else: print(a -(k-(a+b)))
def main(): a,b,c,k = list(map(int,input().split())) if k<=a: print(k) elif k<=a+b: print(a) elif k < a+b+c: print(a-(k-(a+b))) else: print(a-c) main()
1
21,724,280,963,870
null
148
148
from collections import deque while True: s = input() if s == '-': break else: ds = deque(s) ds.rotate(sum([int(input()) for _ in range(int(input()))])*-1) print(''.join(ds))
N = int(input()) x = 0 for i in range(N): a, b = map(int, input().split()) if a == b: x += 1 else: x = 0 if x >= 3: print("Yes") break else: print("No")
0
null
2,210,332,070,180
66
72
s=input() n=len(s) p=n//2 t=1 for i in range(p): if s[i]==s[p-1-i]: t=1 else: t=0 break; x=1 for i in range(p): if s[p+1+i]==s[n-1-i]: x=1 else: x=0 break y=1 for i in range(n): if s[i]==s[n-1-i]: y=1 else: y=0 break if (t==1 and x==1 and y==1): print('Yes') else: print('No')
A = int(input()) B = int(input()) if A == 1: if B == 2: print(3) else: print(2) elif A == 2: if B == 1: print(3) else: print(1) else: if B == 1: print(2) else: print(1)
0
null
78,749,369,611,228
190
254
W,H,x,y,r = map(int, raw_input().split()) if (r <= x <= (W - r)) and (r <= y <= (H - r)): print "Yes" else: print "No"
n,m=map(int,input().split()) li=[] if n==1: score="0" else: score="1"+"0"*(n-1) for i in range(m): s,c=map(int,input().split()) if n!=1 and (s,c)==(1,0): print("-1") quit() li.append([s,c]) for j in range(len(li)): if li[j][0]==s and li[j][1]!=c: print("-1") quit() score=score[:s-1]+str(c)+score[s:] print(int(score))
0
null
30,461,503,684,470
41
208
import sys sys.setrecursionlimit(10**6) n, u, v = map(int, input().split()) u -= 1 v -= 1 graph = [[] for _ in range(n)] for _ in range(n-1): a, b = map(int, input().split()) graph[a-1].append(b-1) graph[b-1].append(a-1) pre = [None]*n def dfs(node, p_node=-1): for c_node in graph[node]: if c_node==p_node: continue pre[c_node] = node dfs(c_node, node) dfs(u) path = [v] now = v while now!=u: now = pre[now] path.append(now) s1 = path[len(path)//2] p1 = path[len(path)//2-1] def dfs2(node, depth, p_node): mx = depth for c_node in graph[node]: if c_node==p_node: continue mx = max(mx, dfs2(c_node, depth+1, node)) return mx print(dfs2(s1, 0, p1)+len(path)//2-1)
n,u,v = map(int,input().split()) import queue dist_t = [0] * n dist_a = [0] * n elb_t = [0] * n elb_a = [0] * n ab = [[] for j in range(n)] for i in range(n-1): a,b = map(int,input().split()) ab[a-1].append(b-1) ab[b-1].append(a-1) u -= 1 v -= 1 q = queue.Queue() q.put(u) elb_t[u] = 1 dist_t[u] = 0 while not q.empty(): tmp = q.get() for i in range(len(ab[tmp])): idx = ab[tmp][i] if elb_t[idx] != 1: if dist_t[idx] != 0: dist_t[idx] = min(dist_t[idx], dist_t[tmp] + 1) else: dist_t[idx] = dist_t[tmp] + 1 elb_t[idx] = 1 q.put(idx) q = queue.Queue() q.put(v) elb_a[v] = 1 dist_a[v] = 0 while not q.empty(): tmp = q.get() for i in range(len(ab[tmp])): idx = ab[tmp][i] if elb_a[idx] != 1: dist_a[idx] = dist_a[tmp] + 1 elb_a[idx] = 1 q.put(idx) ans = 0 for i in range(n): if dist_a[i] > dist_t[i]: ans = max(ans,dist_a[i]-1) print(ans)
1
116,853,761,693,340
null
259
259
while True: num = map(int, raw_input().split()) num.sort() if num[0] == 0 and num[1] == 0: break print "%d %d" % (num[0], num[1])
N = int(input()) dp = [True] * 10**6 count = 0 if N % 2 == 0: times = 0 while(N % 2 == 0): times += 1 N //= 2 i = 1 while(times-i >= 0): times -= i i += 1 count += 1 f = 3 while f**2 < N: if N % f == 0: times = 0 while(N % f == 0): times += 1 N //= f i = 1 while(times-i >= 0): times -= i i += 1 count += 1 else: f += 2 if N != 1: count += 1 print(count)
0
null
8,687,175,310,142
43
136
# -*- coding: utf-8 -*- import math def main(): N = int(input()) ans = math.ceil(N / 2) print(ans) if __name__ == "__main__": main()
from itertools import combinations n = int(input()) d = list(map(int, input().split())) ans = 0 d = combinations(d, 2) for i, j in d: ans += i*j print(ans)
0
null
113,536,768,703,372
206
292
def linearSearch(A, key): i = 0 A.append(key) while A[i] != key: i += 1 if i == len(A)-1: A.pop() return 'Not_found' A.pop() return i if __name__ == '__main__': c = 0 n = int(input()) hoge1 = [int(x) for x in input().split()] q = int(input()) for key in (int(x) for x in input().split()): if linearSearch(hoge1, key) == 'Not_found': continue else: c += 1 print (c)
num = map(int, raw_input().split()) if num[0] > num[1]: x = num[0] y = num[1] else: y = num[0] x = num[1] while y > 0: z = x % y x = y y = z print x
0
null
39,843,319,780
22
11
def robin(process, q): if int(process[0][1]) - q > 0: process[0][1] = int(process[0][1]) - q process.append([process[0][0],process[0][1]]) process.pop(0) return process, q else: q = int(process[0][1]) process.pop(0) return process, q n, q = input().split() n, q = int(n), int(q) process = [] for i in range(n): process.append([i for i in input().split()]) time = 0 while len(process) != 0: length = len(process) name = process[0][0] process, process_time = robin(process, q) time += process_time if length > len(process): print(name,time)
a,b = input().split() A = (a*int(b)) B = (b*int(a)) if A < B: print(A) else: print(B)
0
null
42,023,297,556,992
19
232
import sys import itertools sys.setrecursionlimit(10000000) from heapq import heapify,heappop,heappush,heappushpop import math import collections import copy if __name__ == "__main__": a,b,c,d = map(int,input().split()) e = a*c f = a*d g = b*c h = b*d ans = max(e,f) ans = max(ans,g) ans = max(ans,h) print(ans)
a,b,c,d = map(int,input().split()) e = a*c f = a*d g = b*c h = b*d xy =[e,f,g,h] print(int(max(xy)))
1
3,033,841,844,640
null
77
77
H, W = map(int, input().split()) S = [0]*(H+2) S[0] = ''.join(['#']*(W+2)) S[-1] = ''.join(['#']*(W+2)) for i in range(1,H+1): S[i] = '#'+input()+'#' maxd = 0 import queue dy = [1,0,-1,0] dx = [0,1,0,-1] for h in range(1,H+1): for w in range(1,W+1): if S[h][w]=='.': visited = [[False]*(W+2) for _ in range(H+2)] q = queue.Queue() visited[h][w] = True q.put([h,w,0]) while not q.empty(): a,b,c = q.get() #print(a,b,c) for i in range(4): y,x = a+dy[i], b+dx[i] if S[y][x]=='.' and visited[y][x]==False: q.put([y,x,c+1]) visited[y][x]=True maxd = max(maxd,c) print(maxd)
#import sys #import numpy as np import math #from fractions import Fraction import itertools from collections import deque from collections import Counter import heapq from fractions import gcd #input=sys.stdin.readline #import bisect h,w,k=map(int,input().split()) a=1 res=[] ans_r=[0]*h x=0 for i in range(h): s=list(input()) ans=[0]*w cnt=0 if a==k+1: ans_r[i]=x-1 continue for j in range(w): if s[j]=="#": if cnt==0: cnt+=1 else: a+=1 ans[j]=a else: ans[j]=a ans_r[i]=x if cnt!=0: res.append(ans) x+=1 a+=1 for i in range(h): j=ans_r[i] if j==-1: print(*res[-1]) else: print(*res[j])
0
null
118,885,538,366,560
241
277
n = int(input()) for i in range(9,0,-1): if n % i == 0: work = n / i if work < 10: print("Yes") break else: print("No") break
#!/usr/bin/env python3 import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines def main(): As = map(int, readline().split()) if sum(As) >= 22: print('bust') else: print('win') if __name__ == '__main__': main()
0
null
139,935,704,557,972
287
260
a, b=map(int,input().split()) if a<b: print("a < b") elif a>b: print("a > b") elif a==b: print("a == b") else: pass
n = input().split() a = int(n[0]) b = int(n[1]) if a < b: print("a < b") elif a > b: print("a > b") elif a == b: print("a == b")
1
364,346,624,058
null
38
38
a =sorted( [int(input()) for i in range(2)]) print("1" if a == [2,3] else "2" if a == [1,3] else "3")
a = int(input()) b = int(input()) ans = list(filter(lambda x: x != a and x != b, [1, 2, 3])) print(ans[0])
1
111,066,285,449,152
null
254
254
n = int(input()) a = [input() for i in range(n)] c0, c1, c2, c3 = 0, 0, 0, 0 for output in a: if output == 'AC': c0 += 1 elif output == 'WA': c1 += 1 elif output == 'TLE': c2 += 1 elif output == 'RE': c3 += 1 print('AC x {}'.format(c0)) print('WA x {}'.format(c1)) print('TLE x {}'.format(c2)) print('RE x {}'.format(c3))
x=int(input()) import math def is_prime(n): if n == 1: return False for k in range(2, int(math.sqrt(n)) + 1): if n % k == 0: return False return True while True: ans=is_prime(x) if ans==True: print(x) break x=x+1
0
null
57,144,062,106,952
109
250
import sys read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines from collections import deque def main(): n, *a = map(int, read().split()) a.sort(reverse=True) a = deque(a) circle_joined = deque() next_score = a.popleft() r = 0 flag = False while a: circle_joined.append(a.popleft()) if flag: next_score = circle_joined.popleft() r += next_score flag = False else: r += next_score flag = True print(r) if __name__ == '__main__': main()
x = input() if x == "RRR": print(3) elif x == "RRS": print(2) elif x == "SRR": print(2) elif x == "SSS": print(0) else: print(1)
0
null
7,070,039,417,088
111
90
X = int(input()) prime_under_X = [] for i in range(2, int(X ** 0.5) + 1): flg = True for j in range(len(prime_under_X)): if i % prime_under_X[j] == 0: flg = False break if flg: prime_under_X.append(i) while True: flg = True for j in range(len(prime_under_X)): if X % prime_under_X[j] == 0: flg = False break if flg: print(X) break X = X + 1
s = list(input()) k = int(input()) n = len(s) if s == [s[0]]*n: print(n*k//2) else: ss = s*2 ans1 = 0 for i in range(1, n): if s[i] == s[i -1]: s[i] = "" ans1 += 1 ans2 = 0 for i in range(1, 2*n): if ss[i] == ss[i - 1]: ss[i] = "" ans2 += 1 j = ans2 - ans1 print(ans1 + j*(k - 1))
0
null
140,190,117,412,992
250
296
def solve(): n=int(input()) a=list(map(int,input().split())) cnt=[0]*(int(1e5)+9) s=0 for i in range(0,n) : s+=a[i] cnt[a[i]]+=1 q=int(input()) for q in range(0,q) : b,c=map(int,input().split()) ct=cnt[b] s+=ct*(c-b) cnt[b]=0 cnt[c]+=ct print(s) solve()
N = int(input()) A = list(map(int, input().split())) Q = int(input()) cnt = [0]*(10**5+1) s = sum(A) for i in A: cnt[i] += 1 for _ in range(Q): B, C = map(int, input().split()) s += (C-B)*cnt[B] print(s) cnt[C] += cnt[B] cnt[B] = 0
1
12,192,796,404,448
null
122
122
# D - Sum of Divisors N = int(input()) ans = (1 + N) * N for i in range(2, N//2 + 1): j = 2 while True: if i * j <= N: ans += i * j j += 1 else: break print(ans - 1)
n, m, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) a_rui = [0] b_rui = [0] counta = 0 countb = 0 for i in range(n): a_rui.append(a_rui[i]+a[i]) if a_rui[i+1] <= k: counta = i+1 for i in range(m): b_rui.append(b_rui[i]+b[i]) if b_rui[i] <= k: countb = i+1 # print(counta, countb) # print(a_rui, b_rui) ans = 0 for i in range(counta+1): # print("DFGHj") # print(i, countb) while a_rui[i]+b_rui[countb] > k: # print(a_rui[i]+b_rui[countb], i, countb) countb -= 1 # print(i, countb) ans = max(ans, i+countb) print(ans)
0
null
10,884,875,387,432
118
117
S = [] A = raw_input().split() for i in range(len(A)): if A[i] == '+': S.append(int(S.pop()) + int(S.pop())) elif A[i] == '-': S.append(-int(S.pop()) + int(S.pop())) elif A[i] == '*': S.append(int(S.pop()) * int(S.pop())) else: S.append(A[i]) print S[0]
import re def bubble_sort(C, N): for i in range(N): for j in reversed(range(1, N)): if C[j][1] < C[j-1][1]: # 1???????????????????°????????????°?????? C[j], C[j-1] = C[j-1], C[j] return C def selection_sort(C, N): for i in range(N): # i ?????????????????¨???????????? minj = i for j in range(i, N): # ?????????????????¨??????????°??????????????????? if C[j][1] < C[minj][1]: minj = j # ??????????????¨??????????°????????????????????????\???????????? C[i], C[minj] = C[minj], C[i] return C def is_stable(C, sorted): # ????????°???????????????????¨???????????????????????´??????????????????? c_container = [] s_container = [] for i in range(len(C)): # C ??§ i ????????\?????????????????§????????°????????????????????¢??? for j in range(len(C)): # ????????°???????????????????????£?????´??? if C[i][1] == C[j][1]: # C ????????????????¨????????????? c_container.append(C[j][0]) # ????????°???????????????????????¨????????´??? if len(c_container) >= 2: # C ??§ i ???????????????????????°?????¨????????°????????????????????¢??? for k in range(len(sorted)): # ????±??????????????????°?????¨????????°???????¨?????????????????????????????????????????????????????´? if sorted[k][1] == C[i][1]: s_container.append(sorted[k][0]) # ??????????????´?????????????????£?????´??? if c_container != s_container: # Not Stable return False # ????????????????????? c_container = [] s_container = [] return True if __name__ == '__main__': N = int(input()) # ??????????????????????????????????????¨?????¨??°?????¨?????????????????????????´? cards = input().split() C = [[] for i in range(N)] for i in range(N): # ??????????¨??????¨??°???????????? symbol_and_num = re.findall(r'(\d+|\D+)', cards[i]) C[i] = [symbol_and_num[0], int(symbol_and_num[1])] # ?????????????????????????????? bubble_sorted = bubble_sort(C.copy(), N) # ?¨??????¨??????????????? bubble_sorted_cards = [val[0] + str(val[1]) for val in bubble_sorted] # ????????????????????? print(' '.join(bubble_sorted_cards)) # stable or not stable print('Stable' if is_stable(C, bubble_sorted) else 'Not stable') # ????????????????????? selection_sorted = selection_sort(C.copy(), N) # ?¨??????¨??????????????? selection_sorted_cards = [val[0] + str(val[1]) for val in selection_sorted] # ????????????????????? print(' '.join(selection_sorted_cards)) # stable or not stable print('Stable' if is_stable(C, selection_sorted) else 'Not stable')
0
null
30,348,960,898
18
16
input_line = input() # W,H,x,y,r input_data = input_line.strip().split(' ') W, H, x, y, r = [int(i) for i in input_data] if x < 0 or y < 0: print("No") elif W >= (x+r) and H >= (y+r): print("Yes") else: print("No")
W,H,x,y,r=map(int,raw_input().split()) print'Yes' if((0+r)<=x and (W-r)>=x) and ((0+r)<=y and (H-r)>=y) else'No'
1
451,307,482,432
null
41
41
i=0 while True: i+=1 a=input() if a==0: break else: print("Case "+str(i)+": "+str(a))
a5=[] while True: x=int(input()) if x==0: break a5.append(x) j = 0 for i in a5: print("Case " + str(j+1)+": "+ str(i)) j=j+1
1
476,791,887,058
null
42
42