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
|
---|---|---|---|---|---|---|
H = int(input())
res, cnt = 0, 1
while H > 1:
H = H // 2
res += cnt
cnt *= 2
print(res + cnt)
| def attacks(HP, times=1):
if HP==0: return times-1
HP //= 2
times *= 2
return attacks(HP,times)
h=int(input())
print(attacks(h))
| 1 | 80,074,289,500,480 | null | 228 | 228 |
n,k=map(int,input().split())
import sys
sys.setrecursionlimit(2147483647)
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 = 2*n+1 # 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)
ans=1
if k>n:
ans=cmb(n+n-1,n,p)
else:
for i in range(1,k+1):
ans+=(cmb(n, i, p)*cmb(n-1,n-i-1,p))%p
#print(i,cmb(n, i, p),cmb(n-1,n-i-1,p))
print(ans%p)
| import copy
D = int( input() )
c = list( map( int, input().split() ) )
s = []
for _ in range( D ):
s_i = list( map( int, input().split() ) )
s.append( s_i )
t = []
for _ in range( D ):
t_i = int( input() )
t.append( t_i - 1 )
last = []
last_i = [ -1 for _ in range( 26 ) ]
for i in range( D ):
last_i[ t[ i ] ] = i
last.append( copy.deepcopy( last_i ) )
# def satisfy( D, c, s, t, last, d ):
satisfy = [ 0 for _ in range( D ) ]
for d in range( D ):
satisfy[ d ] += s[ d ][ t[ d ] ]
for i in range( 26 ):
satisfy[ d ] -= c[ i ] * ( d - last[ d ][ i ] )
v = 0
for v_i in satisfy:
v += v_i
print( v )
| 0 | null | 38,666,600,832,262 | 215 | 114 |
import math
K = int(input())
total = 0
for x in range(1, K+1):
for y in range(1, K+1):
for z in range(1, K+1):
total = total+math.gcd(x, math.gcd(y, z))
print(total)
| n=int(input())
s,t = map(str, input().split())
print(''.join([s[i] + t[i] for i in range(0,n)])) | 0 | null | 73,635,733,883,712 | 174 | 255 |
a=input().split()
print(str(int(a[0])*int(a[1]))+" "+str((int(a[0])+int(a[1]))*2)) | a, b = map(int, raw_input().split())
print ("%d %d" % (a * b, 2 * (a + b))) | 1 | 294,486,219,032 | null | 36 | 36 |
N = int(input())
S = str(input())
flag = False
half = (N+1) // 2
if S[:half] == S[half:N]:
flag = True
if flag:
print("Yes")
else:
print("No") | r = input()
r = int(r)
ans = r ** 2
print(ans)
| 0 | null | 145,909,837,050,428 | 279 | 278 |
n=list(input())
if "7" in n:
print("Yes")
else:
print("No")
| # coding: utf-8
# Your code here!
# coding: utf-8
# 自分の得意な言語で
# Let's チャレンジ!!
S=input()
if "7" in S:
print("Yes")
else:
print("No") | 1 | 34,131,896,751,540 | null | 172 | 172 |
import math
N = int(input())
M = int(math.sqrt(N))
ans = 0
check = [True for _ in range(M + 1)]
e = [0 for _ in range(M + 1)]
for p in range(2, M + 1):
if check[p] == True:
for j in range(2, M // p + 1):
check[p * j] = False
while N % p == 0:
N = N // p
e[p] += 1
ans += int((math.sqrt(1 + 8 * e[p]) - 1)/2)
if N > 1:
ans += 1
print(ans) | n = int(input())
def prime_factorize(n):
a = []
while n % 2 == 0:
a.append(2)
n //= 2
f = 3
while f * f <= n:
if n % f == 0:
a.append(f)
n //= f
else:
f += 2
if n != 1:
a.append(n)
return a
import collections
c = collections.Counter(prime_factorize(n))
ans = 0
for _,value in c.items():
i = 0
while value>i:
i += 1
value -= i
ans += i
print(ans) | 1 | 16,985,622,650,740 | null | 136 | 136 |
input()
while True:
try:
lst = list(map(int,input().split()))
lst.sort()
if lst[2] ** 2 == lst[1] ** 2 + lst[0] ** 2:
print("YES")
else:
print("NO")
except EOFError:
break
| n, r = map(int, input().split())
print(r if n >= 10 else r + 1000 - (100 * n))
| 0 | null | 31,894,331,100,932 | 4 | 211 |
# 入力
# 値は全てint
# Nは本数、Kは切る回数、AはN本の木のそれぞれの長さ(リスト)
N,K = map(int,input().split())
A = list(map(int,input().split()))
# 二分探索
# lが左端、rが右端
l,r = 0, 10**9
# 整数で返すので、差が1より大きい時はループする
while r - l > 1:
# 真ん中を設定
x = (r+l) // 2
# 切る回数をc
c = 0
for a in A:
# それぞれの丸太の(長さ-1)をxで割った値の合計が、切る回数
c += (a-1) // x
# 切る回数がKよりも小さい時はOKなので右端を寄せる
if c <= K:
r = x
else:
l = x
print(r)
| def main():
_, K = (int(i) for i in input().split())
A = [int(i) for i in input().split()]
if K == 0:
print(max(A))
return
def is_ok(x):
need = 0
for a in A:
if a % x == 0:
need += a//x - 1
else:
need += a//x
if need <= K:
return True
else:
return False
def binary_search():
ng = 0
ok = 10**9
while abs(ok - ng) > 1:
mid = ng + (ok - ng) // 2
if is_ok(mid):
ok = mid
else:
ng = mid
return ok
print(binary_search())
if __name__ == '__main__':
main()
| 1 | 6,494,514,466,992 | null | 99 | 99 |
#!/usr/bin/env python3
from pprint import pprint
from collections import deque, defaultdict
import itertools
import math
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.buffer.readline
INF = float('inf')
n_nodes = int(input())
graph = [[] for _ in range(n_nodes)]
for _ in range(n_nodes):
line = list(map(int, input().split()))
u, k = line[0], line[1]
if k > 0:
for v in line[2:]:
graph[u - 1].append(v - 1)
# pprint(graph)
def dfs(v):
global time
time += 1
for v_adj in graph[v]:
if d[v_adj] == -1:
d[v_adj] = time
dfs(v_adj)
f[v] = time
time += 1
d = [-1] * n_nodes
f = [-1] * n_nodes
time = 1
for v in range(n_nodes):
if d[v] == -1:
d[v] = time
dfs(v)
# pprint(d)
# pprint(f)
for v in range(n_nodes):
print(f"{v + 1} {d[v]} {f[v]}")
| #ALDS_11_B - 深さ優先探索
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)#再帰回数の上限
def DFS(u, cnt):
visited.add(u)
first_contact = cnt
cnt += 1
for nb in edge[u]:
if nb not in visited:
cnt = DFS(nb, cnt)
stamp[u] = first_contact, cnt
return cnt+1
N = int(input())
edge = [[] for i in range(N)]
for _ in range(N):
tmp = list(map(int,input().split()))
if tmp[1] != 0:
edge[tmp[0]-1] = list(map(lambda x: int(x-1),tmp[2:]))
stamp = [None]*N
visited = set()
c = 1
for i in range(N):
if i not in visited:
c = DFS(i, c)
for i,s in enumerate(stamp):
print(i+1,*s)
| 1 | 2,868,069,000 | null | 8 | 8 |
import copy
from sys import exit
n, k = map(int, input().split())
A = list(map(int, input().split()))
mod = 10**9 + 7
plus = []
minus = []
for a in A:
if a >= 0:
plus.append(a)
else:
minus.append(a)
plus.sort()
minus.sort(reverse=True)
def is_ans_plus():
if len(plus) > 0:
if n == k:
if len(minus) % 2 == 0:
return True
else:
return False
else:
return True
else:
if k % 2 == 0:
return True
else:
return False
ans = 1
if is_ans_plus():
if k % 2 == 1:
ans *= plus.pop()
k -= 1
for i in range(k // 2):
plus_pair, minus_pair = -1, -1
if len(plus) >= 2:
plus_pair = plus[-1] * plus[-2]
if len(minus) >= 2:
minus_pair = minus[-1] * minus[-2]
if plus_pair >= minus_pair:
ans *= plus_pair
plus.pop()
plus.pop()
else:
ans *= minus_pair
minus.pop()
minus.pop()
ans %= mod
else:
ans = -1
A = sorted([abs(a) for a in A])
for i in range(k):
ans *= A[i]
ans %= mod
print(ans)
| N,K=map(int, input().split())
A=list(map(int, input().split()))
D,E=[],[]
zcnt,scnt,fcnt=0,0,0
for i in A:
if i==0:
zcnt+=1
D.append(0)
elif i>0:
D.append(i)
scnt+=1
else:
E.append(i)
fcnt+=1
mod=10**9+7
ans=1
if K==N:
for i in A:
ans*=i
ans%=mod
print(ans)
exit()
#マイナスを奇数個絶対かける→確実に答えはマイナス
if K%2==1 and max(A)<0:
A=sorted(A)[::-1]
for i in range(K):
ans*=A[i]
ans%=mod
print(ans)
exit()
#絶対0をかけなきゃいけない
if K>scnt+fcnt:
print(0)
exit()
D,E=sorted(D)[::-1],sorted(E)
#print(D,E)
ans=1
cnt=0
a,b=0,0
while K-cnt>1:
if a+1<=len(D)-1 and b+1<=len(E)-1:
d,e=D[a]*D[a+1],E[b]*E[b+1]
if d>e:
ans*=D[a]
a+=1
cnt+=1
ans%=mod
else:
ans*=e
b+=2
ans%=mod
cnt+=2
elif a+1<=len(D)-1:
d=D[a]*D[a+1]
ans*=D[a]
a+=1
cnt+=1
ans%=mod
elif b+1<=len(E)-1:
e=E[b]*E[b+1]
ans*=e
b+=2
cnt+=2
ans%=mod
if K-cnt==1:
Z=[]
if a!=scnt:
Z.append(D[a])
if b!=fcnt:
Z.append(E[-1])
if 0 in A:
Z.append(0)
ans*=max(Z)
ans%=mod
print(ans)
| 1 | 9,333,345,000,550 | null | 112 | 112 |
while True:
m, t, f = map( int, raw_input().split())
if m == -1 and t == -1 and f == -1:
break
if m == -1 or t == -1:
r = "F"
elif (m + t) >= 80:
r = "A"
elif (m + t) >= 65:
r = "B"
elif (m + t) >= 50:
r = "C"
elif (m + t) >= 30:
if f >= 50:
r = "C"
else:
r = "D"
else:
r = "F"
print r | # coding:utf-8
while True:
m,f,r = map(int, raw_input().split())
if m == -1 and f == -1 and r == -1:
break
if m == -1 or f == -1:
print 'F'
elif m + f >= 80:
print 'A'
elif m + f >= 65:
print 'B'
elif m + f >= 50:
print 'C'
elif m + f >= 30:
if r >= 50:
print 'C'
else:
print 'D'
else:
print 'F'
| 1 | 1,250,934,358,592 | null | 57 | 57 |
n, k = map(int, input().split())
a = list(map(int, input().split()))
f = list(map(int, input().split()))
a.sort()
f.sort(reverse=True)
def solve(x):
lk = k
for i, j in zip(a, f):
if i*j<=x:
continue
lk -= (j*(i+1)-x-1)//j
if lk<0:
return False
return True
ok = 10**12
ng = -1
while abs(ok-ng)>1:
mid = (ok+ng)//2
if solve(mid):
ok = mid
else:
ng = mid
print(ok)
| import sys
input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]
IN=lambda : map(int, input().split())
mod=1000000007
#+++++
def main():
ll=[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]
a = int(input())
print(ll[a-1])
#+++++
isTest=False
def pa(v):
if isTest:
print(v)
def input_clipboard():
import clipboard
input_text=clipboard.get()
input_l=input_text.splitlines()
for l in input_l:
yield l
if __name__ == "__main__":
if sys.platform =='ios':
if input_method==input_methods[0]:
ic=input_clipboard()
input = lambda : ic.__next__()
elif input_method==input_methods[1]:
sys.stdin=open('inputFile.txt')
else:
pass
isTest=True
else:
pass
#input = sys.stdin.readline
ret = main()
if ret is not None:
print(ret) | 0 | null | 107,717,328,272,180 | 290 | 195 |
InputNum = input().split()
tate = int(InputNum[0])
yoko = int(InputNum[1])
square = tate * yoko
length = 2* (tate + yoko)
print(square,length) | K=int(input())
S=input()
if len(S)<=K:
print(S)
else:
for i in range(K):
print(S[i],end="")
print("...") | 0 | null | 10,059,595,902,048 | 36 | 143 |
import base64
exec(base64.b64decode(b'aW1wb3J0IHN1YnByb2Nlc3MKaW1wb3J0IHN5cwoKY29kZSA9IHIiIiIjcHJhZ21hIEdDQyBvcHRpbWl6ZSgidW5yb2xsLWxvb3BzIikKCiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDxiaXRzZXQ+CiNpbmNsdWRlIDxjYXNzZXJ0PgojaW5jbHVkZSA8Y2N0eXBlPgojaW5jbHVkZSA8Y2hyb25vPgojaW5jbHVkZSA8Y21hdGg+CiNpbmNsdWRlIDxjb21wbGV4PgojaW5jbHVkZSA8Y3N0cmluZz4KI2luY2x1ZGUgPGRlcXVlPgojaW5jbHVkZSA8aW9tYW5pcD4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWFwPgojaW5jbHVkZSA8bnVtZX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sKICAgICAgaWYgKG4gPj0gKGludCkxZTgpIHJldHVybiA5OwogICAgICBpZiAobiA+PSAoaW50KTFlNykgcmV0dXJuIDg7CiAgICAgIGlmIChuID49IChpbnQpMWU2KSByZXR1cm4gNzsKICAgICAgcmV0dXJuIDY7CiAgICB9IGVsc2UgewogICAgICBpZiAobiA+PSAoaW50KTFlNCkgcmV0dXJuIDU7CiAgICAgIGlmIChuID49IChpbnQpMWUzKSByZXR1cm4gNDsKICAgICAgaWYgKG4gPj0gKGludCkxZTIpIHJldHVybiAzOwogICAgICBpZiAobiA+PSAoaW50KTFlMSkgcmV0dXJuIDI7CiAgICAgIHJldHVybiAxOwogICAgfQogIH0KCiAgdGVtcGxhdGUgPGNsYXNzIFQsIHN0ZDo6ZW5hYmxlX2lmX3Q8c3RkOjppc19zYW1lPFQsIGludDY0Pjo6dmFsdWUsIGludD4gPSAwPgogIGlubGluZSBpbnQgZ2V0X2RpZ2l0KFQgbikgewogICAgaWYgKG4gPj0gKGludDY0KTFlMTApIHsKICAgICAgaWYgKG4gPj0gKGludDY0KTFlMTQpIHsKICAgICAgICBpZiAobiA+PSAoaW50NjQpMWUxOCkgcmV0dXJuIDE5OwogICAgICAgIGlmIChuID49IChpbnQ2NCkxZTE3KSByZXR1cm4gMTg7CiAgICAgICAgaWYgKG4gPj0gKGludDY0KTFlMTYpIHJldHVybiAxNzsKICAgICAgICBpZiAobiA+PSAoaW50NjQpMWUxNSkgcmV0dXJuIDE2OwogICAgICAgIHJldHVybiAxNTsKICAgICAgfSBlbHNlIHsKICAgICAgICBpZiAobiA+PSAoaW50NjQpMWUxNCkgcmV0dXJuIDE1OwogICAgICAgIGlmIChuID49IChpbnQ2NCkxZTEzKSByZXR1cm4gMTQ7CiAgICAgICAgaWYgKG4gPj0gKGludDY0KTFlMTIpIHJldHVybiAxMzsKICAgICAgICBpZiAobiA+PSAoaW50NjQpMWUxMSkgcmV0dXJuIDEyOwogICAgICAgIHJldHVybiAxMTsKICAgICAgfQogICAgfSBlbHNlIHsKICAgICAgaWYgKG4gPj0gKGludDY0KTFlNSkgewogICAgICAgIGlmIChuID49IChpbnQ2NCkxZTkpIHJldHVybiAxMDsKICAgICAgICBpZiAobiA+PSAoaW50NjQpMWU4KSByZXR1cm4gOTsKICAgICAgICBpZiAobiA+PSAoaW50NjQpMWU3KSByZXR1cm4gODsKICAgICAgICBpZiAobiA+PSAoaW50NjQpMWU2KSByZXR1cm4gNzsKICAgICAgICByZXR1cm4gNjsKICAgICAgfSBlbHNlIHsKICAgICAgICBpZiAobiA+PSAoaW50NjQpMWU0KSByZXR1cm4gNTsKICAgICAgICBpZiAobiA+PSAoaW50NjQpMWUzKSByZXR1cm4gNDsKICAgICAgICBpZiAobiA+PSAoaW50NjQpMWUyKSByZXR1cm4gMzsKICAgICAgICBpZiAobiA+PSAoaW50NjQpMWUxKSByZXR1cm4gMjsKICAgICAgICByZXR1cm4gMTsKICAgICAgfQogICAgfQogIH0KCiAgdGVtcGxhdGUgPGNsYXNzIFQsIHN0ZDo6ZW5hYmxlX2lmX3Q8c3RkOjppc19zYW1lPFQsIGludD46OnZhbHVlLCBpbnQ+ID0gMD4KICBpbmxpbmUgdm9pZCB3cml0ZShUIG51bSkgewogICAgaWYgKF9fYnVpbHRpbl9leHBlY3QoY3VyICsgaW50ZWdlcl9zaXplID49IGJ1ZiArIGJ1Zl9zaXplLCAwKSkgZmx1c2goKTsKICAgIGlmIChudW0gPT0gMCkgewogICAgICB3cml0ZSgnMCcpOwogICAgICByZXR1cm47CiAgICB9CiAgICBpZiAobnVtIDwgMCkgewogICAgICB3cml0ZSgnLScpOwogICAgICBudW0gPSAtbnVtOwogICAgfQogICAgaW50IGxlbiA9IGdldF9kaWdpdChudW0pOwogICAgaW50IGRpZ2l0cyA9IGxlbjsKICAgIHdoaWxlIChudW0gPj0gMTAwMDApIHsKICAgICAgbWVtY3B5KGN1ciArIGxlbiAtIDQsIHRhYmxlICsgKG51bSAlIDEwMDAwKSAqIDQsIDQpOwogIC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AgICAgIH0KICAgICAgfQogICAgfQogIH0KCiAgaW50NjQgYW5zID0gMDsKICBmb3IgKGludCBpID0gMDsgaSA8IDQ7ICsraSkgYW5zID0gbWF4KGFucywgZHBbciAtIDFdW2MgLSAxXVtpXSk7CiAgZm91dC5wcmludGxuKGFucyk7CgogIHJldHVybiAwOwp9CiIiIgoKd2l0aCBvcGVuKCdzb2wuY3BwJywgJ3cnKSBhcyBmOgogICAgZi53cml0ZShjb2RlKQoKc3VicHJvY2Vzcy5Qb3BlbihbJ2crKycsICctc3RkPWMrKzE3JywgJy1PMicsICdzb2wuY3BwJ10pLmNvbW11bmljYXRlKCkKc3VicHJvY2Vzcy5Qb3BlbihbJy4vYS5vdXQnXSwgc3RkaW49c3lzLnN0ZGluLCBzdGRvdXQ9c3lzLnN0ZG91dCkuY29tbXVuaWNhdGUoKQ==')) | import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product
from heapq import heappush, heappop
from functools import lru_cache
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 1000000007
sys.setrecursionlimit(1000000)
R, C, K = rl()
I = [[0]*(C+2) for _ in range(R+2)]
for i in range(K):
r, c, v = rl()
I[r][c] = v
dp = [[0]*4 for _ in range(C+2)]
dp2 = [[0]*4 for _ in range(C+2)]
for i in range(R+1):
for j in range(C+1):
for m in range(4):
ni, nj = i, j+1
dp[nj][m] = max(dp[nj][m], dp[j][m])
if m < 3:
dp[nj][m+1] = max(dp[nj][m+1], dp[j][m]+I[ni][nj])
ni, nj = i+1, j
dp2[nj][0] = max(dp2[nj][0], dp[j][m])
dp2[nj][1] = max(dp2[nj][1], dp[j][m]+I[ni][nj])
dp = dp2
dp2 = [[0]*4 for _ in range(C+2)]
ans = 0
for m in range(4):
ans = max(ans, dp[C][m])
print(ans)
| 1 | 5,533,479,007,098 | null | 94 | 94 |
import collections
n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = []
for i in range(q):
bci = list(map(int, input().split()))
bc.append(bci)
d = collections.Counter(a)
ans = sum(a)
for i in range(q):
b, c = bc[i]
num = (c-b)*d[b]
ans += num
print(ans)
d[c] += d[b]
d[b] = 0 | N = int(input())
A = list(map(int, input().split()))
Q = int(input())
sum_res = sum(A)
counter = [0 for _ in range(10 ** 5 + 1)]
for a in A:
counter[a] += 1
for _ in range(Q):
B, C = map(int, input().split())
sum_res += (C - B) * counter[B]
counter[C] += counter[B]
counter[B] = 0
print(sum_res)
| 1 | 12,176,066,305,662 | null | 122 | 122 |
tmp = input().split(" ")
a = int(tmp[0])
b = int(tmp[1])
c = int(tmp[2])
if a + b + c >= 22:
print("bust")
else:
print("win") | import sys
from collections import deque
N, M = map(int, input().split())
G = [[] for _ in range(N)]
for _ in range(M):
a, b = map(int, sys.stdin.readline().split())
G[a-1].append(b-1)
G[b-1].append(a-1)
ars = [0] * N
todo = deque([0])
done = {0}
while todo:
p = todo.popleft()
for np in G[p]:
if np in done:
continue
todo.append(np)
ars[np] = p + 1
done.add(np)
if len(done) == N:
print("Yes")
for i in range(1, N):
print(ars[i])
else:
print("No") | 0 | null | 69,458,830,541,280 | 260 | 145 |
x = int(input())
c_500 = x // 500
r_500 = x % 500
c_5 = r_500 // 5
print(c_500 * 1000 + c_5 * 5)
| x = int(input())
num = (x//500)*1000
num += ((x%500)//5)*5
print(num) | 1 | 42,683,498,299,332 | null | 185 | 185 |
import bisect, copy, heapq, math, sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
return sys.stdin.readline()[:-1]
def ruiseki(lst):
return [0]+list(accumulate(lst))
def celi(a,b):
return -(-a//b)
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]
n=int(input())
x=list(map(int,input().split()))
ans=float('inf')
for i in range(100):
tmp=0
for j in range(n):
tmp+=(x[j]-i-1)**2
ans=min(ans,tmp)
print(ans) | n = input()
s = list(map(int,input().split()))
m = 10000000
for p in range(0,101):
t = 0
for x in s:
t+=(x-p)**2
m = min(t,m)
print(m) | 1 | 65,612,843,945,350 | null | 213 | 213 |
import math
import sys
import os
sys.setrecursionlimit(10**7)
def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))
if os.getenv("LOCAL"):
inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
sys.stdin = open(inputFile, "r")
INF = float("inf")
N,K = LI()
P = LI()
C = LI()
P = list(map(lambda x:x-1,P))
res = -INF
used = [False] * N
ss = []
for i in range(N):
# print('i',i)
if used[i]:
continue
cur = i
s = []
while not used[cur]:
used[cur] = True
s.append(C[cur])
cur = P[cur]
# print(cur)
ss.append(s)
# print(ss)
for vec in ss:
M = len(vec)
# 2周の累積和
sum = [0]*(2*M + 1)
for i in range(2*M):
sum[i+1] = sum[i]+vec[i%M]
# amari[r] := 連続するr個の最大値
amari = [-INF]*M
for i in range(M):
for j in range(M):
amari[j] = max(amari[j],sum[i+j] - sum[i])
for r in range(M):
if r>K:
continue
q = (K-r)//M
if r==0 and q==0:
continue
if sum[M]>0:
res = max(res, amari[r] + sum[M] * q)
elif r > 0:
res = max(res, amari[r])
print(res)
| N, K = map(int, input().split())
P = list(map(lambda x: int(x) - 1, input().split()))
C = list(map(int, input().split()))
maxscore = -(10 ** 18)
for st in range(N):
cumsum = [0]
now = st
while(P[now] != st):
now = P[now]
cumsum.append(cumsum[-1] + C[now])
cumsum.append(cumsum[-1] + C[st])
m = len(cumsum) - 1
loopsum = cumsum[-1]
if loopsum < 0:
maxscore = max(maxscore, max(cumsum[1:min(K + 1, m + 1)]))
continue
score = (K // m - 1) * loopsum
extended_cumsum = cumsum + [cumsum[i] + loopsum for i in range(1, K % m + 1)]
score += max(extended_cumsum)
maxscore = max(maxscore, score)
print(maxscore) | 1 | 5,383,454,530,312 | null | 93 | 93 |
def bingo():
# 初期処理
A = list()
b = list()
comb_list = list()
is_bingo = False
# 入力
for _ in range(3):
dummy = list(map(int, input().split()))
A.append(dummy)
N = int(input())
for _ in range(N):
dummy = int(input())
b.append(dummy)
# ビンゴになる組み合わせ
for i in range(3):
# 横
comb_list.append([A[i][0], A[i][1], A[i][2]])
# 縦
comb_list.append([A[0][i], A[1][i], A[2][i]])
# 斜め
comb_list.append([A[0][0], A[1][1], A[2][2]])
comb_list.append([A[0][2], A[1][1], A[2][0]])
# 集計(出現した数字をnullに置き換え)
for i in b:
for j in range(8):
for k in range(3):
if i == comb_list[j][k]:
comb_list[j][k] = 'null'
# すべてnullのリストの存否
for i in comb_list:
for j in i:
if 'null' != j:
is_bingo = False
break
else:
is_bingo = True
# 一組でもbingoがあれば即座に関数を抜ける
if is_bingo == True:
return 'Yes'
# すべてのリストを確認後
if is_bingo == False:
return 'No'
result = bingo()
print(result)
| import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
return sys.stdin.readline()[:-1]
def ruiseki(lst):
return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]
s=input()
lns=len(s)
lst=[0]*(lns+1)
start=[]
if s[0]=="<":
start.append(0)
for i in range(lns-1):
if s[i]==">" and s[i+1]=="<":
start.append(i+1)
if s[lns-1]==">":
start.append(lns)
for i in start:
d=deque([[i,0],[i,1]])
while d:
now,lr=d.popleft()
# print(now)
if now-1>=0 and lr==0 and s[now-1]==">":
lst[now-1]=max(lst[now-1],lst[now]+1)
d.append([now-1,0])
if now+1<=lns and lr==1 and s[now]=="<":
lst[now+1]=max(lst[now+1],lst[now]+1)
d.append([now+1,1])
# print(lst)
# print(start)
# print(lst)
print(sum(lst)) | 0 | null | 107,887,264,496,984 | 207 | 285 |
n=input()
z=ord(n)+1
print(chr(z)) | N,K,C=list(map(int,input().split()))
S=input()
R=[]
L=[]
i=0
while i<N:
if S[i]=="o":
R.append(i)
i+=C+1
else:
i+=1
i=N-1
while i>=0:
if S[i]=="o":
L.append(i)
i-=(C+1)
else:
i-=1
R=R[:K+1]
L=L[:K+1]
L.reverse()
for i in range(K):
if R[i]==L[i]:
print(str(R[i]+1),end=" ")
| 0 | null | 66,125,668,980,714 | 239 | 182 |
K=int(input())
if K==4 :
print(2)
elif K==6 :
print(2)
elif K==8 :
print(5)
elif K==9 :
print(2)
elif K==10 :
print(2)
elif K==12 :
print(5)
elif K==14 :
print(2)
elif K==16 :
print(14)
elif K==18 :
print(5)
elif K==20 :
print(5)
elif K==21 :
print(2)
elif K==22 :
print(2)
elif K==24 :
print(15)
elif K==25 :
print(2)
elif K==26 :
print(2)
elif K==27 :
print(5)
elif K==28 :
print(4)
elif K==30 :
print(4)
elif K==32 :
print(51)
else :
print(1) | data =[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]
line =int(input())
print(data[line -1]) | 1 | 49,783,945,254,368 | null | 195 | 195 |
def main():
from collections import deque
K = int(input())
if K <= 9:
print(K)
return
q = deque()
for i in range(1, 10):
q.append(i)
count = 9
while True:
get_num = q.popleft()
for i in range(-1, 2):
add_num = get_num % 10 + i
if 0 <= add_num and add_num <= 9:
q.append(get_num * 10 + add_num)
count += 1
if count == K:
print(q.pop())
return
if __name__ == '__main__':
main() | from collections import deque
K = int(input())
nums = deque([])
for i in range(1, 10):
nums.appendleft(i)
count = 1
while count < K:
num = nums.pop()
last = int(str(num)[-1])
for i in range(max(0, last-1), min(10, last+2)):
nums.appendleft(int(str(num)+str(i)))
count += 1
print(nums.pop())
| 1 | 39,926,539,555,770 | null | 181 | 181 |
alpha = input()
if(alpha.isupper()):
print("A")
else:
print("a") | N = int(input())
d_ls=[]
for vakawkawelkn in range(N):
D1,D2=map(int, input().split())
if D1==D2:
d_ls+=[1]
else:
d_ls+=[0]
flag=0
for i in range(N-2):
if d_ls[i]*d_ls[i+1]*d_ls[i+2]==1:
flag=1
if flag==0:
print("No")
else:
print("Yes")
# 2darray [[0] * 4 for i in range(3)]
# import itertools | 0 | null | 6,943,784,949,570 | 119 | 72 |
import sys;input=lambda:sys.stdin.readline().rstrip();aint=lambda:int(input());ints=lambda:list(map(int,input().split()))
import math;floor,ceil=math.floor,math.ceil
from collections import deque
Yes=lambda b:print('Yes')if b else print('No');YES=lambda b:print('YES')if b else print('NO')
is_even=lambda x:True if x%2==0 else False
def main():
n,r = ints()
if n>=10:
print(r)
else:
print(r+100*(10-n))
if __name__ == '__main__':
main() | n = int(input())
list_ = [0]*n
joshi_list = list(map(int, input().split()))[::-1]
for i,num in enumerate(joshi_list):
list_[num-1]+=1
for num in list_:
print(num) | 0 | null | 48,186,860,052,288 | 211 | 169 |
n = int(input())
Building = [[[0 for r in range(11)] for f in range(4)] for b in range(5)]
for i in range(n):
b, f, r, v = map(int, input().split())
Building[b][f][r] += v
for b in range(1, 5):
for f in range(1, 4):
for r in range(1, 11):
print(" " + str(Building[b][f][r]), end = "")
print()
if b != 4:
print("####################") | rooms = [0] * (4*3*10)
count = int(input())
for i in range(count):
b, f, r, v = [int(x) for x in input().split()]
rooms[30*(b-1) + 10*(f-1) + (r-1)] += v
for i, room in enumerate(rooms, start=1):
print('', room, end='')
if i%10 == 0:
print()
if i%30 == 0 and i%120 != 0:
print('#'*20) | 1 | 1,106,331,648,464 | null | 55 | 55 |
n = int(input())
s = []
t = []
for _ in range(n):
name,time = map(str,input().split())
s.append(name)
t.append(int(time))
target = input()
start = s.index(target)
ans = 0
for i in range(start+1,n):
ans += t[i]
print(ans) | n=int(input())
l=[input().split() for i in range(n)]
x=input()
time=0
for i in range(n):
time += int(l[i][1])
if l[i][0]==x:
time=0
print(time) | 1 | 96,910,983,561,850 | null | 243 | 243 |
n = int(input())
c=0
for i in range(1,(n//2)+1):
if((n-i) != i):
c+=1
print(c) | while True:
cards = raw_input()
if cards == '-':
break
n = input()
for _ in xrange(n):
h = input()
cards = cards[h:] + cards[:h]
print cards | 0 | null | 77,251,677,004,082 | 283 | 66 |
N = input()
a=0
for i in range(len(N)):
a+=int(N[i])%9
if a%9==0:
print('Yes')
if a%9!=0:
print('No') | n = list(input())
n1 = [int(i) for i in n]
sum = 0
for i in n1:
sum += i
if sum % 9 == 0:
print('Yes')
else:
print('No') | 1 | 4,380,361,254,610 | null | 87 | 87 |
from sys import stdin, setrecursionlimit
def main():
input = stdin.buffer.readline
n = int(input())
d = list(map(int, input().split()))
ans = 0
for i in range(n - 1):
for j in range(i + 1, n):
ans += d[i] * d[j]
print(ans)
if __name__ == "__main__":
setrecursionlimit(10000)
main()
| N=int(input())
d=list(map(int, input().split()))
ans=0
for i in range(N):
for j in range(N):
if i == j:
continue
elif i < j:
ans += d[i]*d[j]
print(ans) | 1 | 167,698,767,001,370 | null | 292 | 292 |
N = int(input())
slist = []
for i in range(N):
slist.append(input())
print (len(set(slist))) | n=int(input())
s=[str(input()) for _ in range(n)]
print(len(set(s))) | 1 | 30,367,825,574,450 | null | 165 | 165 |
h,n= map(int, input().split())
a = list(map(int,input().split()))
for i in range(n):
h -= a[i]
print("Yes" if h <=0 else "No") | datalist = []
Flag = True
for i in range(3000):
if Flag:
x, y = map(int, input().split())
if x == 0 and y == 0:
Flag = False
else:
datalist.append((x, y))
#print(datalist)
for (i,j) in datalist:
if i < j :
print("{} {}".format(i, j))
else:
print("{} {}".format(j, i))
| 0 | null | 39,323,181,569,684 | 226 | 43 |
from collections import deque
import math
import copy
#https://qiita.com/ophhdn/items/fb10c932d44b18d12656
def max_dist(input_maze,startx,starty,mazex,mazey):
input_maze[starty][startx]=0
que=deque([[starty,startx]])
# print(que)
while que:
# print(que,input_maze)
y,x=que.popleft()
for i,j in [(1,0),(0,1),(-1,0),(0,-1)]:
nexty,nextx=y+i,x+j
if (nexty>=0) & (nextx>=0) & (nextx<mazex) & (nexty<mazey):
dist=input_maze[nexty][nextx]
if dist!=-1:
if (dist>input_maze[y][x]+1) & (dist > 0):
input_maze[nexty][nextx]=input_maze[y][x]+1
que.append([nexty,nextx])
# print(que)
# print(max(list(map(lambda x: max(x),copy_maze))))
return max(list(map(lambda x: max(x),copy_maze)))
h,w =list(map(int,input().split()))
maze=[list(input()) for l in range(h)]
# Maze preprocess
for y in range(h):
for x in range(w):
if maze[y][x]=='.':
maze[y][x]=math.inf
elif maze[y][x] == '#':
maze[y][x]=int(-1)
results = []
for y in range(h):
for x in range(w):
if maze[y][x]==math.inf:
copy_maze = copy.deepcopy(maze)
max_dist_result = max_dist(copy_maze,x,y,w,h)
results.append(int(max_dist_result))
#print(max_dist_result)
print(max(results))
| N = int(input())
def solve(x1, y1, x2, y2, n):
if n == 0:
return print(x1, y1)
else:
solve(x1, y1, (2 * x1 + x2) / 3, (2 * y1 + y2) / 3, n - 1)
solve((2 * x1 + x2) / 3, (2 * y1 + y2) / 3, (x1 + x2) / 2 + (y1 - y2) * (3 ** (1 / 2)) / 6, (y1 + y2) / 2 + (x2 - x1) * (3 ** (1 / 2)) / 6, n - 1)
solve((x1 + x2) / 2 + (y1 - y2) * (3 ** (1 / 2)) / 6, (y1 + y2) / 2 + (x2 - x1) * (3 ** (1 / 2)) / 6, (x1 + 2 * x2) / 3, (y1 + 2 * y2) / 3, n - 1)
solve((x1 + 2 * x2) / 3, (y1 + 2 * y2) / 3, x2, y2, n - 1)
solve(0, 0, 100, 0, N)
print(100, 0)
| 0 | null | 47,219,818,492,868 | 241 | 27 |
import math
import numpy
a,b,c = map(int, input().split())
d = c - a - b
if d > 0 and d*d > 4 * a * b:
print("Yes")
else:
print("No")
| n = int(input())
A = list(map(int, input().split()))
l = []
for a in A:
if a % 2 == 0:
l.append(a)
if all([i % 3 ==0 or i % 5 ==0 for i in l]):
print('APPROVED')
else:
print('DENIED') | 0 | null | 59,958,952,572,960 | 197 | 217 |
def coin(larger=False, single=False):
"""
ナップサックに入れる重さが W 丁度になる時の価値の最小値
:param larger: False = (重さ = W)の時の最小価値
True = (重さ =< W)の時の最小価値
:param single: False = 重複あり
dp[weight <= W+1] = 重さを固定した時の最小価値
dp[W+1] = 重さがWより大きい時の最小価値
"""
W2 = W + 1 # dp[W+1] に W より大きい時の全ての場合の情報を持たせる
dp_max = float("inf") # 総和価値の最大値
dp = [dp_max] * (W2 + 1)
dp[0] = 0 # 重さ 0 の時は価値 0
for item in range(N):
if single:
S = range(W2, weight_list[item] - 1, -1)
else:
S = range(W2)
for weight in S:
dp[min2(weight + weight_list[item], W2)] = min2(dp[min2(weight + weight_list[item], W2)], dp[weight] + cost_list[item])
if larger:
return min(dp[w] for w in range(W, W2+1))
else:
return dp[W]
#######################################################################################################
import sys
input = sys.stdin.readline
def max2(x, y):
return x if x > y else y
def min2(x, y):
return x if x < y else y
W, N = map(int, input().split()) # N: 品物の種類 W: 重量制限
cost_list = []
weight_list = []
for _ in range(N):
""" cost と weight が逆転して入力されている場合有り """
weight, cost = map(int, input().split())
cost_list.append(cost)
weight_list.append(weight)
print(coin(larger=True, single=False)) | INF = 1 << 60
h, n = map(int, input().split())
a = [0 for i in range(n)]
b = [0 for i in range(n)]
for i in range(n):
a[i], b[i] = map(int, input().split())
MAX = h - 1 + max(a)
dp = [[INF for j in range(MAX + 1)] for i in range(n)]
dp[0][0] = 0
if 0 <= a[0] < MAX + 1:
dp[0][a[0]] = b[0]
for j in range(MAX + 1):
if 0 <= j - a[0] < MAX + 1:
dp[0][j] = min(dp[0][j], dp[0][j - a[0]] + b[0])
for i in range(1, n):
for j in range(MAX + 1):
dp[i][j] = min(dp[i][j], dp[i - 1][j])
if 0 <= j - a[i] < MAX + 1:
dp[i][j] = min(dp[i][j], dp[i][j - a[i]] + b[i])
ans = INF
for j in range(h, MAX + 1):
ans = min(ans, dp[-1][j])
print(ans) | 1 | 81,022,270,132,748 | null | 229 | 229 |
import sys
def input():
return sys.stdin.readline()
N,K = map(int,input().split())
A=list(map(int,input().split()))
P=[True]*N
P[0]=False
i,a=1,0
while 1:
a+=1
if P[A[i-1]-1]:
P[A[i-1]-1]=False
i=A[i-1]
else:
i=A[i-1]
break
if a>=K:
i=1
for _ in range(K):
i=A[i-1]
print(i)
else:
K-=a
a,j=0,i
while 1:
a+=1
if A[j-1]==i:
break
j=A[j-1]
K%=a
for _ in range(K):
i=A[i-1]
print(i) | # -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**6)
N,K=map(int, sys.stdin.readline().split())
A=[None]+map(int, sys.stdin.readline().split())
visit=[ 0 for _ in range(N+1) ]
L=[] #閉路になった町番号を順番に記録する
def func(fro):
global K
to=A[fro]
visit[fro]+=1
if visit[fro]==2:
L.append(fro)
if visit[to]<=1: #閉路ループは2回まで許可する
K-=1 #操作回数を減らす
if K==0: #Kが途中でゼロになれば、その時に移動しようとしていたtoが答え
print to
quit()
func(to)
func(1)
K-=1
l=len(L)
print L[K%l]
| 1 | 22,774,088,044,580 | null | 150 | 150 |
ret = []
while True:
n, x = map(int, raw_input().split())
num_arr = [i for i in range(1, n+1)]
if (n, x) == (0, 0):
break
cnt = 0
for i in range(len(num_arr)):
for j in range(i + 1, len(num_arr)):
k_flg = False
for k in range(j + 1, len(num_arr)):
work = num_arr[i] + num_arr[j] + num_arr[k]
if x == work:
cnt += 1
k_flg = True
break
ret += [cnt]
for i in ret:
print i | from sys import stdin
def main():
for l in stdin:
n, x = map(int, l.split(' '))
if 0 == n == x:
break
print(len(num_sets(n, x)))
def num_sets(n, x):
num_sets = []
for i in range(1, n-1):
for j in [m for m in range(i+1, n)]:
k = x - (i+j)
if n < k:
continue
if k <= j:
break
num_sets.append((i, j, k))
return num_sets
if __name__ == '__main__': main() | 1 | 1,300,554,258,454 | null | 58 | 58 |
s = input()
k = len(s)
a = [0]*(k+1)
for i in range(k):
if s[i] == '<':
a[i+1] = a[i]+1
for j in range(k-1,-1,-1):
if s[j] == '>':
a[j] = max(a[j],a[j+1]+1)
print(sum(a))
| H,W = list(map(int,input().split()))
N=H*W
#壁のノード
wall=[]
for h in range(H):
for w_idx,w in enumerate(list(input())):
if w == '#':
wall.append(W*h+w_idx+1)
#道のノード
path=[_ for _ in range(1,N+1) if _ not in wall]
#隣接リスト
ad = {}
for n in range(N):
ad[n+1]=[]
for n in range(N):
n=n+1
if n not in wall:
up = n-W
if up > 0 and up not in wall:
ad[n].append(up)
down = n+W
if down <= N and down not in wall:
ad[n].append(down)
left = n-1
if n % W != 1 and left not in wall and left > 0:
ad[n].append(left)
right = n+1
if n % W != 0 and right not in wall:
ad[n].append(right)
from collections import deque
def BFS(start):
que = deque([start])
visit = deque([])
color = {}
for n in range(N):
color[n+1] = -1
color[start] = 0
depth = {}
for n in range(N):
depth[n+1] = -1
depth[start] = 0
while len(que) > 0:
start = que[0]
for v in ad[start]:
if color[v] == -1:
que.append(v)
color[v] = 0
depth[v] = depth[start]+1
color[start] = 1
visit.append(que.popleft())
return depth[start]
ans=-1
for start in path:
ans_=BFS(start)
if ans < BFS(start):
ans = ans_
# print(start,ans_)
print(ans) | 0 | null | 125,664,019,971,450 | 285 | 241 |
import heapq
def main():
_ = int(input())
A = sorted(list(map(int, input().split())), reverse=True)
q = []
heapq.heapify(q)
ans = A[0]
tmp_score = min(A[0], A[1])
heapq.heappush(q, (-tmp_score, A[0], A[1]))
heapq.heappush(q, (-tmp_score, A[1], A[0]))
for a in A[2:]:
g = heapq.heappop(q)
ans += -g[0]
tmp_score1 = min(a, g[1])
tmp_push1 = (-tmp_score1, a, g[1])
tmp_score2 = min(a, g[2])
tmp_push2 = (-tmp_score2, a, g[2])
heapq.heappush(q, tmp_push1)
heapq.heappush(q, tmp_push2)
print(ans)
if __name__ == '__main__':
main() | #!/usr/bin/env python3
import sys
from itertools import chain
def solve(N: int, K: int, A: "List[int]"):
M = [-1 for _ in range(N)]
M[0] = 0
current = 1
n = 1
while n <= K:
current = A[current - 1]
if M[current - 1] == -1: # 一度も通ったことがない
M[current - 1] = n # n ステップ目に通った事を記録
else:
loop_len = n - M[current - 1] # ループの長さ
rest = K - n # 残り長さ
rest = rest % loop_len # 残り長さをループの余剰にする
K = n + rest
n += 1
return current
def main():
tokens = chain(*(line.split() for line in sys.stdin))
# N, K, A = map(int, line.split())
N = int(next(tokens)) # type: int
K = int(next(tokens)) # type: int
A = [int(next(tokens)) for _ in range(N)] # type: "List[int]"
answer = solve(N, K, A)
print(answer)
if __name__ == "__main__":
main()
| 0 | null | 16,063,815,619,400 | 111 | 150 |
n,m=map(int,input().split());print('YNeos'[n!=m::2]) | L = int(input(""))
a = L / 3.0
print(pow(a,3)) | 0 | null | 64,750,507,296,348 | 231 | 191 |
import sys
#from collections import deque
#from functools import *
#from fractions import Fraction as f
from copy import *
from bisect import *
#from heapq import *
from math import gcd,ceil,sqrt
from itertools import permutations as prm,product
def eprint(*args):
print(*args, file=sys.stderr)
zz=1
#sys.setrecursionlimit(10**6)
if zz:
input=sys.stdin.readline
else:
sys.stdin=open('input.txt', 'r')
sys.stdout=open('all.txt','w')
di=[[-1,0],[1,0],[0,1],[0,-1]]
def inc(d,c,x=1):
d[c]=d[c]+x if c in d else x
def bo(i):
return ord(i)-ord('A')
def li():
return [int(xx) for xx in input().split()]
def fli():
return [float(x) for x in input().split()]
def comp(a,b):
if(a>b):
return 2
return 2 if a==b else 0
def gi():
return [xx for xx in input().split()]
def fi():
return int(input())
def pro(a):
return reduce(lambda a,b:a*b,a)
def swap(a,i,j):
a[i],a[j]=a[j],a[i]
def si():
return list(input().rstrip())
def mi():
return map(int,input().split())
def gh():
sys.stdout.flush()
def isvalid(i,j):
return 0<=i<n and 0<=j<m and a[i][j]!="."
def bo(i):
return ord(i)-ord('a')
def graph(n,m):
for i in range(m):
x,y=mi()
a[x].append(y)
a[y].append(x)
t=1
mod=998244353
while t>0:
t-=1
n,k,m=mi()
vis={}
cycle=[]
i=ans=0
while k not in vis:
cycle.append(k)
vis[k]=i
i+=1
ans+=k
if i==n:
print(ans)
exit(0)
k=(k**2)%m
if k in vis:
cycle=cycle[vis[k]:]
n-=i
s=sum(cycle)
p=n%len(cycle)
z=len(cycle)
print(ans+(n//z)*s+sum(cycle[:p]))
| import math
y=int(input())
cnt=0
for i in range (1,y+1):
for k in range(1,y+1):
for j in range(1,y+1):
a = [i,j,k]#リスト
ans = a[0]
for x in range(len(a)):
ans = math.gcd(ans, a[x])
cnt+=ans
print(cnt) | 0 | null | 19,124,621,326,116 | 75 | 174 |
N = int(input())
A = list(map(int, input().split()))
if 0 in A:
print(0)
else:
ans = 1
for a in A:
ans *= a
if ans > 10**18:
print(-1)
exit(0)
print(ans)
| def main():
N, A, B = map(int, input().split())
if A % 2 == B % 2:
return abs(A - B)//2
else:
x = A + (B - A - 1)//2
y = (N - B) + (- A + B + 1)//2
return min(x, y)
print(main())
| 0 | null | 63,020,884,850,208 | 134 | 253 |
t = input()
n = len(t)
t += "a"
ans = ""
for i in range(n):
if t[i] == "?":
ans += "D"
else:
ans += t[i]
print(ans) | import sys
d_num = int(sys.stdin.readline().strip())
graph_list = []
for _ in range(0, d_num):
array = sys.stdin.readline().strip().split(" ")
if len(array) <= 2:
graph_list.append([])
else:
graph_list.append([int(array[i]) - 1 for i in range(2, len(array))])
t = 1
result = [[] for _ in range(0, d_num)]
searched_list = []
stack = []
while True:
if len(stack) == 0:
ll = filter(lambda x: x not in searched_list, range(0, d_num))
if len(ll) == 0:
break
stack.append(ll[0])
result[ll[0]].append(t)
searched_list.append(ll[0])
else:
node = stack[-1]
children = filter(lambda x: x not in searched_list, graph_list[node])
if len(children) != 0:
next_node = children[0]
stack.append(next_node)
result[next_node].append(t)
searched_list.append(next_node)
else:
stack.pop()
result[node].append(t)
t += 1
s = ""
for i in range(0, len(result)):
print str(i+1) + " " + str(result[i][0]) + " " + str(result[i][1]) | 0 | null | 9,316,881,802,692 | 140 | 8 |
N = int(input())
arr = [0]*(N+1)
ans =0
for i in range(1,N+1):
for j in range(i,N+1,i):
arr[j] += 1
ans += i*arr[i]
print(ans) | n = int(input())
i = 1
ans = 0
while (i <= n):
total = sum(j for j in range(i, n+1, i))
ans += total
i += 1
print(ans) | 1 | 11,020,298,849,100 | null | 118 | 118 |
import math
A=[]
while True:
n=int(input())
if n==0 :
break
else :
b=list((map(int,input().split())))
ave=sum(b)/n
for i in range(n):
A.append((ave-b[i])**2)
p=math.sqrt(sum(A)/n)
print("{:.8f}".format(p))
A.clear()
| n, m = map(int, input().split())
print(int((n * (n - 1)) / 2) + int((m * (m - 1)) / 2))
| 0 | null | 22,741,665,937,178 | 31 | 189 |
import statistics
while True:
n = input()
if n == '0': break
print(statistics.pstdev(map(int, input().split()))) | while True:
a, b = map(int, input().split())
if a == b == 0:
break
for i in range(a):
if i == 0 or i == a - 1:
print('#' * b)
continue
print('#', '.' * (b - 2), '#', sep = '')
print()
| 0 | null | 494,946,619,008 | 31 | 50 |
import sys
i,j = input().split()
matrixa = []
for _ in range(int(i)):
matrixa += [[int(n) for n in sys.stdin.readline().split()]]
matrixb =[ int(num) for num in sys.stdin.readlines() ]
matrixc = [ 0 for _ in range(int(i))]
for n in range(int(i)):
for a,b in zip(matrixa[n],matrixb):
matrixc[n] += a*b
for c in matrixc:
print(c)
| 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) | 1 | 1,162,486,389,118 | null | 56 | 56 |
a,b = map(int,input().split())
lis = list(map(int,input().split()))
c = sum(lis)
print("No" if a > c else "Yes") | # ABC153
# B Common Raccoon VS Monster
h, n = map(int, input().split())
a = list(map(int, input().split()))
s = sum(a)
if h <= s:
print("Yes")
else:
print("No")
| 1 | 77,871,697,071,190 | null | 226 | 226 |
def fibonacci(n):
if n == 0 or n == 1:
F[n] = 1
return F[n]
if F[n] is not None:
return F[n]
F[n] = fibonacci(n - 1) + fibonacci(n - 2)
return F[n]
n = int(input())
F = [None]*(n + 1)
print(fibonacci(n)) | def resolve():
a, b = input().split()
print(b+a)
resolve() | 0 | null | 51,633,456,387,360 | 7 | 248 |
N, K, C = map(int, input().split())
S = input()
right, left = [0 for _ in range(N)], [0 for _ in range(N)]
cnt = 0
work = 0
while True:
if work == K:
break
if S[cnt] != "x":
right[cnt] += 1
work += 1
cnt += C+1
else:
cnt += 1
S = list(reversed(S))
cnt = 0
work = 0
while True:
if work == K:
break
if S[cnt] != "x":
left[cnt] += 1
work += 1
cnt += C+1
else:
cnt += 1
left.reverse()
# print(right)
# print(left)
cnt1 = 0
cnt2 = 0
for i in range(N):
if right[i] == 1:
cnt1 += 1
if left[i] == 1:
cnt2 += 1
if right[i] == left[i] == 1 and cnt1 == cnt2:
print(i+1) | def e_yutori():
# 参考: https://drken1215.hatenablog.com/entry/2020/04/05/163400
N, K, C = [int(i) for i in input().split()]
S = input()
def sub_solver(string):
"""string が表す労働日時に対して、労働数が最大になるように働いたとき、
i 日目には何回働いているかのリストを返す"""
n = len(string)
current, last = 0, -C - 1 # 最初に 'o' があっても動作するように設定
ret = [0] * (n + 1)
for i in range(n):
if i - last > C and string[i] == 'o':
current += 1
last = i
ret[i + 1] = current
return ret
left = sub_solver(S)
right = sub_solver(S[::-1])
ans = []
for i in range(N):
if S[i] == 'x':
continue
if left[i] + right[N - i - 1] < K:
ans.append(i + 1)
return '\n'.join(map(str, ans))
print(e_yutori()) | 1 | 40,726,832,916,990 | null | 182 | 182 |
import numpy as np
n, k = map(int, input().split())
aa = list(map(np.int64, input().split()))
aa = np.array(aa)
def mul(dd):
mod = 10**9+7
ret = 1
for d in dd:
ret = (ret*d)%mod
return ret
def sol(aa, n, k):
mod = 10**9+7
aap = aa[aa>0]
aam = aa[aa<0]
if n == k:
return mul(aa)
if len(aap) + 2*(len(aam)//2) < k:
return 0
# マイナスのみ
if len(aam) == n:
aam.sort()
if k%2==1:
return mul(aam[-k:])
else:
return mul(aam[:k])
aap = aa[aa>=0]
aap.sort()
aap = aap[::-1]
aam.sort()
ret=1
if k%2 >0:
k = k-1
ret *= aap[0]
aap = aap[1:]
aap2 = [aap[2*i]*aap[2*i+1] for i in range(len(aap)//2)]
aam2 = [aam[2*i]*aam[2*i+1] for i in range(len(aam)//2)]
aap2.extend(aam2)
aap2.sort(reverse=True)
aap2 = [i%mod for i in aap2]
return (ret*mul(aap2[:k//2]))%mod
print(sol(aa, n, k))
| N, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10**9+7
def solve():
A.sort(key=lambda x: abs(x), reverse=True)
ans = 1
nneg = 0
a, b, c, d = -1, -1, -1, -1
for k in range(K):
ans = (ans * A[k])%MOD
if A[k] < 0:
nneg += 1
b = k
else:
a = k
if K == N or nneg%2 == 0:
return ans
for k in range(N-1, K-1, -1):
if A[k] < 0:
d = k
else:
c = k
# b must be >= 0
if a == -1 and c == -1: # all minus
ans = 1
for k in range(K):
ans = (ans * A[-1-k])%MOD
return ans
if a == -1 or d == -1:
outn = A[b]
inn = A[c]
elif c == -1:
outn = A[a]
inn = A[d]
else:
if A[a]*A[c] > A[b]*A[d]:
outn = A[b]
inn = A[c]
else:
outn = A[a]
inn = A[d]
ans = (ans * pow(outn, MOD-2, MOD))%MOD
ans = (ans * inn)%MOD
return ans
if __name__ == "__main__":
print(solve())
| 1 | 9,372,924,436,922 | null | 112 | 112 |
x = input()
y = x.split(" ")
y = [int(z) for z in y]
w = y[0]
h = y[1]
x = y[2]
r = y[4]
y = y[3]
if (x <= w-r and x >= r) and (y <= h-r and y >= r):
print("Yes")
else:
print("No") | s = input()
n = len(s)
c = 0
for i in range(int((n - 1)/2)):
if (s[i] != s[n - 1 - i]):
c = 1
x = int((n - 1)/2)
for i in range(x):
if (s[i] != s[x - 1 - i]):
c = 1
if (c == 1):
print("No")
else:
print("Yes")
| 0 | null | 23,234,838,774,620 | 41 | 190 |
a,b=map(int,raw_input().split())
print a*b,a+a+b+b | s=input()
a,b=s.split()
a=int(a)
b=int(b)
print("%d %d"%(a*b,2*a+2*b)) | 1 | 303,282,887,310 | null | 36 | 36 |
x = int(input())
if x >= 2000:
print(1)
else:
d = 0
for i in range(1,20):
if 100*i <= x <= 105*i:
print(1)
d += 1
break
if d == 0:
print(0) | x = int(input())
dp = [0] * 100200
dp[100] = 1
dp[101] = 1
dp[102] = 1
dp[103] = 1
dp[104] = 1
dp[105] = 1
for i in range(100, x+1):
for p in [100, 101, 102, 103, 104, 105]:
dp[i+p] = max(dp[i], dp[i+p])
print(dp[x])
| 1 | 127,400,963,510,250 | null | 266 | 266 |
data = [int(i) for i in input().split()]
out = ' '.join([str(i) for i in sorted(data)])
print(out)
| a = input().split()
print(' '.join(sorted(a))) | 1 | 416,412,010,080 | null | 40 | 40 |
N = int(input())
s = input()
num_R = s.count('R')
print(s[:num_R].count('W')) | from collections import Counter
N = int(input())
c = input()
cntr = Counter(c)
ans = 0
for c, r in zip(c, "R" * cntr["R"]):
if c != r:
ans += 1
print(ans)
| 1 | 6,323,341,283,428 | null | 98 | 98 |
H,W=list(map(int,input().split()))
l=[list(input()) for i in range(H)]
inf=10**9
DP=[[inf]*W for i in range(H)]
DP[0][0]=0 if l[0][0]=="." else 1
for i in range(H):
for j in range(W):
if i>0:
DP[i][j]=min(DP[i][j],DP[i-1][j]+1) if l[i-1][j] == "." and l[i][j]=="#" else min(DP[i][j],DP[i-1][j])
if j>0:
DP[i][j]=min(DP[i][j],DP[i][j-1]+1) if l[i][j-1] == "." and l[i][j]=="#" else min(DP[i][j],DP[i][j-1])
print(DP[H-1][W-1]) | A,V = map(int,input().split())
B,W = map(int,input().split())
T = int(input())
D1 = abs(A-B)
D2 = (V-W)*T
if (D1 - D2) <=0:
print("YES")
else:
print("NO")
| 0 | null | 32,223,629,910,900 | 194 | 131 |
import fractions
lst = []
for i in range(200):
try:
lst.append(input())
except EOFError:
break
nums = [list(map(int, elem.split(' '))) for elem in lst]
# gcd
res_gcd = [fractions.gcd(num[0], num[1]) for num in nums]
# lcm
res_lcm = [nums[i][0] * nums[i][1] // res_gcd[i] for i in range(len(nums))]
for (a, b) in zip(res_gcd, res_lcm):
print('{} {}'.format(a, b)) | import math
a, b, C=map(int,input().split())
S=a*b*math.sin(math.pi*C/180)/2
c=math.sqrt(a**2+b**2-2*a*b*math.cos(math.pi*C/180))
L=a+b+c
h=2*S/a
print('{:.4f}'.format(S))
print('{:.4f}'.format(L))
print('{:.4f}'.format(h))
| 0 | null | 92,088,751,130 | 5 | 30 |
#!/usr/bin/env python3
n=int(input())
print((n-n//2)/n)
| def resolve():
N = int(input())
print((N//2+1)/N if N%2==1 else 0.5)
if '__main__' == __name__:
resolve() | 1 | 177,308,201,934,368 | null | 297 | 297 |
a,b=map(int,input().split())
if a<2*b:
print(0)
else:
print(int(a-2*b)) | a, b = list(map(int, input().split()))
print(a - 2 * b) if a > 2 * b else print(0)
| 1 | 166,014,859,928,192 | null | 291 | 291 |
D = int(input())
c = [int(i) for i in input().split()]
s = [[int(i) for i in input().split()] for _ in range(D)]
t = [int(input())-1 for _ in range(D)]
ans = 0
llst = [0]*26
def last(d,i): return llst[i]
for day,i in enumerate(range(D),1):
llst[t[i]] = day
ans += s[i][t[i]]
for j in range(26): ans -= c[j]*(day-last(day,j))
print(ans) | N = list(input())
if '7' in N:
print("Yes")
else:
print("No") | 0 | null | 22,250,000,832,060 | 114 | 172 |
a = list(input())
b = list(input())
flag = True
for i,j in enumerate(a):
if j !=b[i]:
print('No')
flag = False
break
if flag == True:
print('Yes') | n,m=map(int,input().split())
b=[list(map(int,input().split())) for i in range(n)]
c =[int(input()) for j in range(m)]
[print(sum([d*e for d,e in zip(b[i],c)])) for i in range(n)] | 0 | null | 11,184,780,512,740 | 147 | 56 |
n,a,b = map(int,input().split())
Mod = 10**9+7
def Mod_pow(x, n):
x = x%Mod
if n==0:
return 1
elif n%2==1:
return (x*Mod_pow(x,n-1)%Mod)
else:
return (Mod_pow(x*x,n/2)%Mod)
def comb(n,r):
x,y = 1,1
for i in range(n-r+1,n+1):
x = x*i%Mod
for i in range(1,r+1):
y = y*i%Mod
#この問題の特徴,フェルマーの小定理よりyで除算することはy^(Mod-2)をかけることと同値
y = Mod_pow(y,Mod-2)
return x*y%Mod
ans = (Mod_pow(2,n)-1-comb(n,a)-comb(n,b))
while ans<0:
ans += Mod
print(ans) | n,k=map(int,input().split())
mod=10**9+7
ans=0
for x in range(k,n+2):
min_a=(0+x-1)*x//2
max_a=(n-x+1+n)*x//2
aa=max_a-min_a+1
ans+=aa
print(ans%mod) | 0 | null | 49,391,097,460,000 | 214 | 170 |
h,n = map(int, input().split())
l = list(map(int, input().split()))
print("Yes" if h <= sum(l) else "No") | import sys
#入力する名前S
S = input()
#新しいID T
T = input()
#print(S)
#print(type(S))
#print(T)
#print(type(T))
if S == T[:-1]:
print("Yes")
sys.exit()
else:
print("No") | 0 | null | 49,492,906,630,752 | 226 | 147 |
import sys
input = sys.stdin.readline
N = int(input())
ans = 0
for i in range(1,N+1):
if i%3 != 0 and i%5 !=0:
ans += i
print(ans)
| def to_fizzbuzz(number):
if number % 15 == 0:
return 'FizzBuzz'
if number % 3 == 0:
return 'Fizz'
if number % 5 == 0:
return 'Buzz'
else:
return str(number)
# return i
def main():
N = int(input())
# this list concludes "FizzBuzz", "Fizz" or "Buzz"
fblist = []
for number in range(1, 10**6):
result = to_fizzbuzz(number)
fblist.append(result)
# the list up to N
n_list = fblist[0:N]
# this list contains only numbers and up to N
n_numlist = []
for s in n_list:
if s.isdigit() == True:
n_numlist.append(int(s))
print(sum(n_numlist))
main() | 1 | 35,143,923,773,280 | null | 173 | 173 |
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)
| H ,W = map(int,input().split())
from collections import deque
S = [input() for i in range(H)]
directions = [[0,1],[1,0],[-1,0],[0,-1]]
counter = 0
#インデックス番号 xが行番号 yが列番号
for x in range(H):
for y in range(W):
if S[x][y]=="#":
continue
que = deque([[x,y]])
memory = [[-1]*W for _ in range(H)]
memory[x][y]=0
while True:
if len(que)==0:
break
h,w = que.popleft()
for i,k in directions:
x_new,y_new = h+i,w+k
if not(0<=x_new<=H-1) or not(0<=y_new<=W-1) :
continue
elif not memory[x_new][y_new]==-1 or S[x_new][y_new]=="#":
continue
memory[x_new][y_new] = memory[h][w]+1
que.append([x_new,y_new])
counter = max(counter,max(max(i) for i in memory))
print(counter)
| 1 | 94,197,377,217,700 | null | 241 | 241 |
N=int(input())
XY=[[] for _ in range(N)]
for i in range(N):
A=int(input())
for _ in range(A):
x,y = map(int, input().split())
x-=1
XY[i].append([x,y])
ans=0
for bit_state in range(2**N):
flag=True
for i in range(N):
if (bit_state>>i)&1:
for x, y in XY[i]:
if (bit_state >>x)&1 != y:
flag=False
if flag:
ans=max(ans, bin(bit_state).count("1"))
print(ans) | suit = ['S','H','C','D']
num = range(0, 13)
deck = {'S':[0]*13, 'H':[0]*13, 'C':[0]*13, 'D':[0]*13}
n = input()
for i in range(0, n):
a, b = raw_input().split(" ")
deck[a][int(b)-1] = 1
for i in suit:
for j in num:
if (deck[i][j] == 0):
print("%s %d" % (i, j+1)) | 0 | null | 61,003,155,014,736 | 262 | 54 |
def A():
n, x, t = map(int, input().split())
print(t * ((n+x-1)//x))
def B():
n = list(input())
s = 0
for e in n:
s += int(e)
print("Yes" if s % 9 == 0 else "No")
def C():
int(input())
a = list(map(int, input().split()))
l = 0
ans = 0
for e in a:
if e < l: ans += l - e
l = max(l, e)
print(ans)
A()
| from collections import Counter
n=int(input())
s=input()
c=Counter(s)
ans=c['R']*c['G']*c['B']
lim=n//2
for i in range(1,n//2+1):
for j in range(i,n-i):
l=s[j-i]
m=s[j]
r=s[j+i]
if l!=m and m!=r and r!=l:
ans-=1
print(ans) | 0 | null | 20,318,204,107,488 | 86 | 175 |
import sys
X = int(input())
a = 100
d = 0
while(a<X):
a = a * 101 // 100
d += 1
print(d)
| string = input()
a,b = map(int,string.split(' '))
print(a*b, 2*(a+b))
| 0 | null | 13,774,326,615,904 | 159 | 36 |
import itertools
n = int(input())
L = list(map(int, input().split()))
comb = itertools.combinations(L, 3)
ans = 0
for i in comb:
i = list(i)
i.sort()
if i[0] + i[1] > i[2] and (i[0] != i[1] and i[1] != i[2] and i[2] != i[0]):
ans += 1
print(ans) | #keyence c subarry sum
n,k,s=map(int,input().split())
x=10**9
ans=[print(s) for _ in range(k)]
for i in range(n-k):
if s==x:
print(1)
else:
print(s+1) | 0 | null | 47,942,469,056,900 | 91 | 238 |
def main():
a, b, k = map(int, input().split())
if a >= k:
remain_a = a - k
remain_b = b
else:
remain_a = 0
if k - a >= b:
remain_b = 0
else:
remain_b = b - (k - a)
print(remain_a, remain_b)
if __name__ == "__main__":
main()
| a,b,k=map(int,input().split())
if a<k:
b-=k-a
a=0
if b<0:
b=0
else:
if a-k>0:
a-=k
else:
a=0
print(a,b) | 1 | 103,932,215,940,210 | null | 249 | 249 |
# 5
import sys
def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()
INF = float('inf')
n = I()
s = S()
ciphers = [f'{i:03}' for i in range(1000)]
ans = 0
for v in ciphers:
j = 0
for i in range(n):
if s[i] == v[j]:
j += 1
if j == 3:
ans += 1
break
print(ans)
| N = int(input())
S = list(input())
S = list(map(lambda x: int(x), S))
ans = 0
for i in range(10):
for j in range(10):
for k in range(10):
if i in S:
a = S.index(i)
if j in S[a+1:]:
b = S[a+1:].index(j) + a + 1
if k in S[b+1:]:
ans += 1
else:
continue
else:
continue
else:
continue
print(ans)
| 1 | 128,856,389,362,890 | null | 267 | 267 |
import numpy as np
t1,t2 = map(int, input().split())
a1,a2 = map(int, input().split())
b1,b2 = map(int, input().split())
# まず大きい方にswap
if a1 <= b1:
a1,b1 = b1,a1
a2,b2 = b2,a2
# t1での距離
x1 = a1*t1 - b1*t1
# t2での距離
x2 = x1 + a2*t2 - b2*t2
if x2 == 0:
print("infinity")
elif x2 > 0:
print(0)
else:
ans = int(np.ceil(x1/abs(x2))*2)
if x1 % abs(x2) != 0:
ans -= 1
print(ans) | t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())
first = a1 * t1 - b1 * t1
second = (a2 * t2 - b2 * t2)
last = second + first
if first * last > 0:
ans = 0
elif (abs(last) == 0):
ans = 'infinity'
else:
s = abs(first) // abs(last)
t = abs(first) % abs(last)
ans = 2 * s
if t != 0:
ans += 1
print(ans) | 1 | 132,094,197,500,352 | null | 269 | 269 |
def build_combinations_counter(N=10**5, p=10**9+7):
fact = [1, 1] # fact[n] = (n! mod p)
factinv = [1, 1] # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1] # mod p におけるnの逆元 n^(-1)
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)
def cmb(n, r, p, fact, factinv):
if (r < 0) or (n < r):
return 0
r = min(r, n - r)
return fact[n] * factinv[r] * factinv[n-r] % p
import functools
return functools.partial(cmb, p=p, fact=fact, factinv=factinv)
def resolve():
n, k = list(map(int, input().split()))
MOD = 10**9+7
counter = build_combinations_counter(2*10**5, 10**9+7)
ans = 0
for i in range(min(n, k+1)):
ans += counter(n, i) * counter(n-1, n-i-1)
ans %= MOD
print(ans)
if __name__ == "__main__":
resolve()
| def nCr_frL(n,r,mod):
ret=[1,n%mod]
for i in range(2,r+1):
inv=pow(i,mod-2,mod)
ret.append((ret[-1]*(n-i+1)*inv)%mod)
return ret
N,K=map(int,input().split())
MOD=10**9+7
com1=nCr_frL(N,N,MOD) # nCi
com2=nCr_frL(N-1,N-1,MOD) # n-1Ci
if K>N-1:
K=N-1
ans=0
for m in range(K+1):
ans+=(com1[m]*com2[N-m-1])%MOD
print(ans%MOD)
| 1 | 67,140,619,924,740 | null | 215 | 215 |
S=int(input())
A=list(input().split())
T=int(input())
B=list(input().split())
n=0
for i in B:
if i in A:
n=n+1
print(n)
| def linearSearch(key, list, N):
l = list + [key]
i = 0
while l[i] != key:
i += 1
return int(i != N)
N1 = int(input())
arr1 = [int(n) for n in input().split()]
N2 = int(input())
arr2 = [int(n) for n in input().split()]
print(sum([linearSearch(key, arr1, N1) for key in arr2]))
| 1 | 68,119,214,880 | null | 22 | 22 |
## necessary imports
import sys
input = sys.stdin.readline
# from math import ceil, floor, factorial;
def ceil(x):
if x != int(x):
x = int(x) + 1;
return x;
# swap_array function
def swaparr(arr, a,b):
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp
## gcd function
def gcd(a,b):
if b == 0:
return a
return gcd(b, a % b);
## nCr function efficient using Binomial Cofficient
def nCr(n, k):
if(k > n - k):
k = n - k
res = 1
for i in range(k):
res = res * (n - i)
res = res / (i + 1)
return int(res)
## upper bound function code -- such that e in a[:i] e < x;
def upper_bound(a, x, lo=0, hi = None):
if hi == None:
hi = len(a);
while lo < hi:
mid = (lo+hi)//2
if a[mid] < x:
lo = mid+1
else:
hi = mid
return lo
## prime factorization
def primefs(n):
## if n == 1 ## calculating primes
primes = {}
while(n%2 == 0 and n > 0):
primes[2] = primes.get(2, 0) + 1
n = n//2
for i in range(3, int(n**0.5)+2, 2):
while(n%i == 0 and n > 0):
primes[i] = primes.get(i, 0) + 1
n = n//i
if n > 2:
primes[n] = primes.get(n, 0) + 1
## prime factoriazation of n is stored in dictionary
## primes and can be accesed. O(sqrt n)
return primes
## MODULAR EXPONENTIATION FUNCTION
def power(x, y, p):
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
## DISJOINT SET UNINON FUNCTIONS
def swap(a,b):
temp = a
a = b
b = temp
return a,b
# find function with path compression included (recursive)
# def find(x, link):
# if link[x] == x:
# return x
# link[x] = find(link[x], link);
# return link[x];
# find function with path compression (ITERATIVE)
def find(x, link):
p = x;
while( p != link[p]):
p = link[p];
while( x != p):
nex = link[x];
link[x] = p;
x = nex;
return p;
# the union function which makes union(x,y)
# of two nodes x and y
def union(x, y, link, size):
x = find(x, link)
y = find(y, link)
if size[x] < size[y]:
x,y = swap(x,y)
if x != y:
size[x] += size[y]
link[y] = x
## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES
def sieve(n):
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
#### PRIME FACTORIZATION IN O(log n) using Sieve ####
MAXN = int(1e5 + 5)
def spf_sieve():
spf[1] = 1;
for i in range(2, MAXN):
spf[i] = i;
for i in range(4, MAXN, 2):
spf[i] = 2;
for i in range(3, ceil(MAXN ** 0.5), 2):
if spf[i] == i:
for j in range(i*i, MAXN, i):
if spf[j] == j:
spf[j] = i;
## function for storing smallest prime factors (spf) in the array
################## un-comment below 2 lines when using factorization #################
# spf = [0 for i in range(MAXN)]
# spf_sieve();
def factoriazation(x):
ret = {};
while x != 1:
ret[spf[x]] = ret.get(spf[x], 0) + 1;
x = x//spf[x]
return ret
## this function is useful for multiple queries only, o/w use
## primefs function above. complexity O(log n)
## taking integer array input
def int_array():
return list(map(int, input().strip().split()))
## taking string array input
def str_array():
return input().strip().split();
#defining a couple constants
MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');
################### ---------------- TEMPLATE ENDS HERE ---------------- ###################
n, k = int_array(); a = int_array();
pos = []; neg = []; ans = 1;
for i in a:
if i < 0:
neg.append(i);
else:
pos.append(i);
if n == k:
for i in a:
ans = (ans * i) % MOD;
elif not pos:
if k & 1:
neg.sort(reverse = 1);
else:
neg.sort();
for i in range(k):
ans = (ans * neg[i]) % MOD;
else:
pos.sort(); neg.sort(reverse = 1);
if k & 1:
ans = pos.pop();
for _ in range(k // 2):
if len(pos) > 1 and len(neg) > 1:
if pos[-1] * pos[-2] >= neg[-1] * neg[-2]:
ans = (ans * pos.pop()) % MOD;
ans = (ans * pos.pop()) % MOD;
else:
ans = (ans * neg.pop()) % MOD;
ans = (ans * neg.pop()) % MOD;
elif len(pos) > 1:
ans = (ans * pos.pop()) % MOD;
ans = (ans * pos.pop()) % MOD;
else:
ans = (ans * neg.pop()) % MOD;
ans = (ans * neg.pop()) % MOD;
print(ans); | n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
mia, pla = [], []
for ai in a:
if ai < 0:
mia.append(ai)
elif ai >= 0:
pla.append(ai)
mia.sort(reverse=True)
pla.sort()
cnt = 1
if len(pla) == 0 and k % 2 == 1:
for i in mia[:k]:
cnt = cnt * i % mod
else:
while k > 0:
if k == 1 or len(mia) <= 1:
if len(pla) == 0:
cnt *= mia.pop()
elif len(pla) > 0:
cnt *= pla.pop()
k -= 1
elif len(pla) <= 1:
cnt *= mia.pop() * mia.pop()
k -= 2
elif len(pla) >= 2 and len(mia) >= 2:
if pla[-1] * pla[-2] > mia[-1] * mia[-2]:
cnt *= pla.pop()
k -= 1
elif pla[-1] * pla[-2] <= mia[-1] * mia[-2]:
cnt *= mia.pop() * mia.pop()
k -= 2
cnt %= mod
print(cnt)
| 1 | 9,403,076,997,890 | null | 112 | 112 |
N=int(input())
c=list(input())
minimum=len(c)
W=0
R=c.count('R')
for i in range(0,len(c)):
if c[i]=='W':
W += 1
else :
R -= 1
min_i=max(W,R)
if min_i < minimum:
minimum=min_i
if c.count('R')==0:
minimum=0
print(minimum) | n=int(input())
c=input()
d=c.count("R")
e=c[:d].count("R")
print(d-e) | 1 | 6,271,752,304,482 | null | 98 | 98 |
ord_s = ord(input())
if ord_s in range(65, 91):
print('A')
elif ord_s in range(97, 123):
print('a') | α = input()
if α.isupper() == True:
print('A')
elif α.islower() == True:
print('a') | 1 | 11,384,384,745,590 | null | 119 | 119 |
n,a,b=input().split()
n=int(n)
a=int(a)
b=int(b)
c=a+b
if n%c>a:
print((n//c)*a+a)
else:
print((n//c)*a+n%c) | a,b,m=map(int,input().split())
ali= list(map(int,input().split()))
bli= list(map(int,input().split()))
min=min(ali)+min(bli)
for i in range(0,m):
x,y,c=map(int,input().split())
mon=ali[x-1]+bli[y-1]-c
if mon<min:
min=mon
print(min) | 0 | null | 54,873,475,516,038 | 202 | 200 |
list =[]
i=0
index=1
while(index != 0):
index=int(input())
list.append(index)
i +=1
for i in range(0,i):
if list[i] == 0:
break
print("Case",i+1,end='')
print(":",list[i])
| # -*- coding: utf-8 -*-
# スペース区切りの整数の入力
S, T = input().split()
U = T+S
print(U)
| 0 | null | 51,785,612,079,718 | 42 | 248 |
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
return sys.stdin.readline()[:-1]
mod=10**9+7
n,d,a=map(int,input().split())
xh=[list(map(int,input().split())) for i in range(n)]
xh.sort()
r,tama=[float('inf')]*n,[0]*(n+1)
for i in range(n):
x,h=xh[i]
tmpr=bisect.bisect_left(r,x)
h-=(tama[i]-tama[tmpr])*a
tmp=0
if h>0:
tmp=math.ceil(h/a)
tama[i+1]=tmp
tama[i+1]+=tama[i]
r[i]=x+2*d
print(tama[-1])
# print(r) | 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))
| 0 | null | 69,711,239,633,254 | 230 | 204 |
from math import ceil
t1,t2 = map(int,input().split())
a1,a2 = map(int,input().split())
b1,b2 = map(int,input().split())
a1 -= b1
a2 -= b2
a1 *= t1
a2 *= t2
if a1 + a2 == 0:
print("infinity")
exit()
if a1*(a1+a2) > 0:
print(0)
exit()
x = -a1/(a1+a2)
n = ceil(x)
if n == x:
print(2*n)
else:
print(2*n-1) |
def main():
H,W,K = list(map(int, input().split()))
S = ['' for i in range(H)]
for i in range(H):
S[i] = input()
ans = H*W
for i in range(0,1<<(H-1)):
g = 0
id = [0 for j in range(H)]
for j in range(0,H):
id[j] = g
if i>>j&1 == 1:
g += 1
g_cnt = [0 for j in range(g+1)]
c_ans = g
j = 0
div_w = 0
while j < W:
for k in range(0, H):
if S[k][j] == '1':
g_cnt[id[k]] += 1
for k in range(0,g+1):
if g_cnt[k] > K:
c_ans += 1
g_cnt = [0 for j in range(g+1)]
if div_w == j:
c_ans += H*W
break
j -= 1
div_w = j
break
j += 1
if c_ans > ans:
break
ans = min(ans, c_ans)
print(ans)
main() | 0 | null | 89,909,125,884,918 | 269 | 193 |
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
H, W, K = map(int, input().split())
S = [tuple(map(int, list(input()))) for _ in range(H)]
ans = 10 ** 9
for bit in range(1 << (H-1)):
canSolve = True
ord = [0] * (H + 1)
N = 0
for i in range(H):
if bit & 1 << i:
ord[i+1] = ord[i] + 1
N += 1
else:
ord[i+1] = ord[i]
nums = [0] * (H + 1)
for w in range(W):
one = [0] * (H + 1)
overK = False
for h in range(H):
one[ord[h]] += S[h][w]
nums[ord[h]] += S[h][w]
if one[ord[h]] > K:
canSolve = False
if nums[ord[h]] > K:
overK = True
if not canSolve:
break
if overK:
N += 1
nums = one
if canSolve and ans > N:
ans = N
print(ans)
if __name__ == '__main__':
main()
| import statistics as st
while(1):
n = int(input())
if n == 0:
break
s = list(map(int, input().split()))
print(st.pstdev(s)) | 0 | null | 24,234,059,316,600 | 193 | 31 |
import sys
n = int(input()) # ?????£?????????????????????????????°
pic_list = ['S', 'H', 'C', 'D']
card_dict = {'S':[], 'H':[], 'C':[], 'D':[]} # ?????£?????????????????\?????????????????°
lost_card_dict = {'S':[], 'H':[], 'C':[], 'D':[]} # ?¶??????????????????\?????????????????°
# ?????????????¨??????\???????????????
for i in range(n):
pic, num = input().split()
card_dict[pic].append(int(num))
# ?¶???????????????????????¨??????\????¨????
for pic in card_dict.keys():
for j in range(1, 14):
if not j in card_dict[pic]:
lost_card_dict[pic].append(j)
# ?????????????????????
for pic in card_dict.keys():
lost_card_dict[pic] = sorted(lost_card_dict[pic])
# ?¶????????????????????????????
for pic in pic_list:
for k in range(len(lost_card_dict[pic])):
print(pic, lost_card_dict[pic][k]) | import sys
N = int(sys.stdin.readline().strip())
S = []
for _ in range(N):
s_i = sys.stdin.readline().strip()
S.append(s_i)
# left bracket - right bracketを高さとした、最小の高さと最終的な高さの座標列
# ex)
# ")": (-1, -1)
# "(": (1, 1)
# "()": (1, 0)
# ")()(": (-1, 0)
# "))))(((((: (-4, 1)
plus_seqs = []
minus_seqs = []
for s_i in S:
h = 0
min_h = float("inf")
for bracket in s_i:
if bracket == "(":
h += 1
else:
h -= 1
min_h = min(min_h, h)
if h >= 0:
plus_seqs.append((min_h, h))
else:
# minus_seqs.append((-1 * min_h, -1 * h))
minus_seqs.append((min_h - h, -1 * h))
# print(plus_seqs)
# print(minus_seqs)
hight = 0
for (min_h, h) in sorted(plus_seqs, reverse=True):
if hight + min_h < 0:
print("No")
sys.exit()
hight += h
hight2 = 0
for (min_h, h) in sorted(minus_seqs, reverse=True):
if hight2 + min_h < 0:
print("No")
sys.exit()
hight2 += h
# print(hight, hight2)
if hight == hight2:
print("Yes")
else:
print("No") | 0 | null | 12,388,852,099,946 | 54 | 152 |
def draw(h,w):
t = "#." * (w/2)
if w%2 == 0:
s = t
k = t.strip("#") + "#"
else:
s = t + "#"
k = "." + t
for i in range(0,h/2):
print s
print k
if h%2==1:
print s
print
if __name__ == "__main__":
while True:
h,w = map(int, raw_input().split())
if h==0 and w==0:
break
else:
draw(h,w) | # C問題
# 難しく考えず、入力された配列の先頭の要素から順に見に行く
# cur_maxという、①最大身長格納変数を用意、②①を更新して行く この2ステップの考え方が大事
N = int(input())
cur_max = 0
ans = 0
# 配列を入力
A = list(map(int, input().split()))
for i in A:
if cur_max > i:
ans += cur_max - i
# どんどん高いものに更新→max関数で処理
cur_max = max(i, cur_max)
print(ans) | 0 | null | 2,747,495,241,320 | 51 | 88 |
N = input()
Sum = 0
for n in list(N):
Sum = Sum + int(n)
ANS = 'Yes' if Sum % 9 == 0 else 'No'
print(ANS) | N=list(map(int,input().split()))
temp = sum(N)
if temp%9 == 0:print("Yes")
else:print("No") | 1 | 4,424,109,186,728 | null | 87 | 87 |
a, b, m = map(int, input().split())
li_a = list(map(int, input().split()))
li_b = list(map(int, input().split()))
li_c = []
for _ in range(m):
x, y, c = map(int, input().split())
x, y = x-1, y-1
li_c.append(li_a[x]+li_b[y]-c)
li_a.sort()
li_b.sort()
li_c.sort()
ans = min(li_a[0]+li_b[0], li_c[0])
print(ans)
| n=int(input())
a = list(map(int, input().split()))
ans = 0
temp = a[0]
for i in a[1:]:
if temp > i:
ans += (temp - i)
temp = max(temp, i)
print(ans) | 0 | null | 29,289,624,867,552 | 200 | 88 |
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
A=[0 for i in range(n)]
B=[0 for i in range(m)]
A[0]=a[0]
B[0]=b[0]
for i in range(n-1):
A[i+1]=a[i+1]+A[i]
for i in range(m-1):
B[i+1]=b[i+1]+B[i]
ans=0
A.insert(0,0)
B.insert(0,0)
for i in range(n+1):
x=0
y=m
j=(x+y)//2
if A[i]+B[m]<=k:
ans=max(ans,i+m)
elif not A[i]+B[0]>k:
while y-x>1:
if A[i]+B[j]<=k:
x=j
else:
y=j
j=(x+y)//2
ans=max(ans,i+x)
print(ans)
| import sys
def lcm(a, b):
a, b = max(a, b), min(a, b)
c = a * b
while a % b > 0:
a, b = b, a % b
return c // b
def solve():
input = sys.stdin.readline
X = int(input())
L = lcm(360, X)
print(L // X)
return 0
if __name__ == "__main__":
solve() | 0 | null | 11,857,971,015,830 | 117 | 125 |
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]
n, m = getList()
if n == m:
print("Yes")
else:
print("No") | n = input()
(p,q) = n.split()
a='Yes'
b='No'
if p == q:
print(a)
else:
print(b)
| 1 | 83,315,316,451,460 | null | 231 | 231 |
S = input()
T = input()
if len(S)+1 != len(T):
print("No")
elif T[0:len(S)] == S:
print("Yes")
else:
print("No") | n,k,c=map(int,input().split())
s=input()
l=[0]*n
pre=n+c+1
cnt=k
for i in range(n-1,-1,-1):
if s[i]=="o" and -(i)+pre>c and cnt>0:
pre=i
l[i]=cnt
cnt-=1
pre=-c-5
cnt=0
for i in range(n):
if s[i]=="o" and i-pre>c and cnt<k:
pre=i;cnt+=1
if l[i]==cnt:print(i+1)
| 0 | null | 31,020,540,438,372 | 147 | 182 |
#DISCO presents ディスカバリーチャンネル コードコンテス a
def get_money(num):
if num==3:
return 100000
elif num==2:
return 200000
elif num==1:
return 300000
else:
return 0
x,y=map(int,input().split())
money=get_money(x)+get_money(y)
if x==y and x==1:
money+=400000
print(money) | A = input()
B = input()
answer = [str(i) for i in range(1, 4)]
answer.remove(A)
answer.remove(B)
print(''.join(answer))
| 0 | null | 126,034,036,743,940 | 275 | 254 |
import numpy as np
X,N = map(int,input().split())
if N ==0:
print(X)
else:
p = list(map(int,input().split()))
length =np.zeros(102)
length =list(length)
for i in range(len(length)):
length[i]= abs(X-i)
for i in range(N):
length[p[i]]=1000
ans = length.index(min(length))
print(ans)
| N = int(input())
s = [input() for i in range(N)]
S = [[0]*len(s[i]) for i in range(N)]
for i, x in enumerate(s):
res = []
q = 0
for j in range(len(x)):
if x[j] == ')':
S[i][j] = -1
else:
S[i][j] = 1
s = [0]*N
m = [len(S[i]) for i in range(N)]
for i, x in enumerate(S):
tmp = 0
for j in range(len(x)):
tmp += S[i][j]
m[i] = min(tmp, m[i])
s[i] = tmp
if sum(s) == 0:
sp = [(m[i], s[i]) for i in range(N) if s[i] > 0]
mp = [(-(s[i]-m[i]), -s[i]) for i in range(N) if s[i] <= 0]#右から見た場合
sp.sort(key=lambda x: x[0], reverse=True)
tmp = 0
for x, y in sp:
if tmp + x < 0:
print("No")
exit(0)
tmp += y
mp.sort(key=lambda x: x[0], reverse=True)
tmp2 = 0
for x, y in mp:
if tmp2 + x < 0:
print("No")
exit(0)
tmp2 += y
print("Yes")
else:
print("No")
| 0 | null | 18,857,505,059,378 | 128 | 152 |
def main():
m = list(input())
q = int(input())
A = []
for _ in range(q):
A.append(input().split())
for x in range(q):
if A[x][0] == "print":
A[x][1] = int(A[x][1])
A[x][2] = int(A[x][2])
for y in range(A[x][1], A[x][2] + 1):
if y != A[x][2]:
print(m[y], end = "")
else:
print(m[y])
elif A[x][0] == "reverse":
A[x][1] = int(A[x][1])
A[x][2] = int(A[x][2])
hoge = []
for y in range(A[x][1], A[x][2] + 1):
hoge.append(m[y])
hoge = hoge[::-1]
i = 0
for y in range(A[x][1], A[x][2] + 1):
m[y] = hoge[i]
i += 1
elif A[x][0] == "replace":
A[x][1] = int(A[x][1])
A[x][2] = int(A[x][2])
fuga = list(A[x][3])
j = 0
for y in range(A[x][1], A[x][2] + 1):
m[y] = fuga[j]
j += 1
if __name__ == "__main__":
main() | s = input()
n = int(input())
s_array = []
for i in range(len(s)):
s_array.append(s[i])
for i in range(n):
order = input().split()
if order[0] == "replace":
first = int(order[1])
last = int(order[2])
for j in range(last-first+1):
s_array[first+j] = order[3][j]
elif order[0] == "reverse":
a = int(order[1])
b = int(order[2])
buf = []
for j in range(b, a-1, -1):
buf.append(s_array[j])
for j in range(b+1-a):
s_array[a+j] = buf[j]
elif order[0] == "print":
a = int(order[1])
b = int(order[2])
for j in range(a, b+1):
print("%s" % s_array[j], end="")
print()
| 1 | 2,101,415,018,924 | null | 68 | 68 |
a,b,c,k=map(int,input().split())
if k < a:
print(k)
elif k <= a+b:
print(a)
else:
print(2*a+b-k) | A,B,C,K = map(int,input().split())
SCO = 0
if A>K:
SCO+=K
else:
SCO+=A
if B<=(K-A):
if C<=(K-A-B):
SCO-=C
else:
SCO-=K-A-B
print(SCO) | 1 | 21,827,724,151,090 | null | 148 | 148 |
def main():
alphabets = "abcdefghijklmnopqrstuvwxyz"
c = input()
print(alphabets[alphabets.index(c)+1])
main() | S = input()
print(chr(ord(S) + 1))
| 1 | 91,823,191,649,770 | null | 239 | 239 |
import math
n = input()
for i in range(n):
flag = 0
a = map(int, raw_input().split())
if(pow(a[0],2)+pow(a[1],2) == pow(a[2],2)):
flag = 1
if(pow(a[1],2)+pow(a[2],2) == pow(a[0],2)):
flag = 1
if(pow(a[2],2)+pow(a[0],2) == pow(a[1],2)):
flag = 1
if flag == 1:
print "YES"
else:
print "NO" | # -*-coding:utf-8
lineNum = int(input())
for i in range(lineNum):
line = input()
tokens = list(map(int, line.strip().split()))
tokens.sort()
a, b, c = tokens[0], tokens[1], tokens[2]
if pow(a,2)+pow(b,2) == pow(c,2):
print('YES')
else:
print('NO') | 1 | 296,505,640 | null | 4 | 4 |
n = int(input())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
ans, cnt = 1, [0, 0, 0]
for i in a:
ans = ans * cnt.count(i) % mod
for j in range(3):
if cnt[j] == i:
cnt[j] += 1
break
print(ans)
| import sys,queue,math,copy
input = sys.stdin.readline
MOD = 10**9 + 7
LI = lambda : [int(x) for x in input().split()]
N = int(input())
A = LI()
c =[0,0,0]
cnt = [0 for _ in range(N)]
for i in range(N):
cnt[i] = c.count(A[i])
if cnt[i] == 0: break
c[c.index(A[i])] += 1
ans = 1
for i in range(N):
ans = (ans * cnt[i]) % MOD
print (ans) | 1 | 130,147,925,391,210 | null | 268 | 268 |
n = int(input())
S = [int(s) for s in input().split()]
q = int(input())
T = [int(s) for s in input().split()]
print(sum([t in S for t in T])) | ns = int(input())
s = list(map(int, input().split()))
nt = int(input())
t = list(map(int, input().split()))
count = 0
for i in t:
# 配列に番兵を追加
s += [i]
j = 0
while s[j] != i:
j += 1
s.pop()
if j == ns:
continue
count += 1
print(count)
| 1 | 67,536,087,950 | null | 22 | 22 |
n = int(input())
i = 1
while i <= n:
x = i
if x % 3 == 0:
print(" {:d}".format(i),end="")
else:
while x:
if x % 10 == 3:
print(" {:d}".format(i),end="")
break
x = x // 10
i += 1
print("")
| a=[]
n=int(input())
i=1
c=0
while i <= n:
if c==0:
x=i
if x%3==0:
a.append(i)
i+=1
continue
c=0
if x%10==3:
a.append(i)
i+=1
continue
x//=10
if x == 0:
i+=1
continue
else:
c=1
print(' ', end='')
print(*a) | 1 | 924,117,853,978 | null | 52 | 52 |