code1
stringlengths 16
427k
| code2
stringlengths 16
427k
| similar
int64 0
1
| pair_id
int64 2
178,025B
⌀ | question_pair_id
float64 27.1M
177,113B
⌀ | code1_group
int64 1
297
| code2_group
int64 1
297
|
---|---|---|---|---|---|---|
S = input().rstrip()
print('x' * len(S))
|
print(len(str(input())) * 'x')
| 1 | 72,552,467,834,778 | null | 221 | 221 |
import sys
import bisect
N = int(input())
a = list(map(int,input().split()))
numdic = {}
for i, num in enumerate(a):
if num not in numdic:
numdic[num] = [i+1]
else:
numdic[num].append(i+1)
if 1 not in numdic:
print(-1)
sys.exit()
nowplace = 0
count = 0
for i in range(1,N+1):
if i not in numdic:
break
else:
place = bisect.bisect_right(numdic[i], nowplace)
if place >= len(numdic[i]):
break
else:
nowplace = numdic[i][place]
count += 1
print(N-count)
|
count=int(raw_input())
l=set()
for i in xrange(count):
a,b=raw_input().split()
if a=='insert':
l.add(b)
elif b in l:
print 'yes'
else:
print 'no'
| 0 | null | 57,637,252,071,068 | 257 | 23 |
n = int(input())
s = list(map(int, input().split()))
s.sort()
dp = [0] * ((10 ** 6) + 100)
for ss in s:
dp[ss] += 1
A = max(s)
pairwise = True
setwise = True
for i in range(2, A + 1):
cnt = 0
for j in range(i, A + 1, i):
cnt += dp[j]
if cnt > 1:
pairwise = False
if cnt >= n:
setwise = False
break
if pairwise:
print("pairwise coprime")
elif setwise:
print("setwise coprime")
else:
print("not coprime")
|
from math import gcd
def main():
N = int(input())
A = list([int(x) for x in input().split()])
max_a = max(A)
before = 0
result = [0 for _ in range(max_a + 1)]
for i in A:
before = gcd(before, i)
result[i] += 1
is_pairwise = True
for i in range(2, max_a + 1):
cnt = 0
for j in range(i, max_a + 1, i):
cnt += result[j]
if cnt > 1:
is_pairwise = False
break
if is_pairwise:
print('pairwise coprime')
exit()
if before == 1:
print('setwise coprime')
else:
print('not coprime')
if __name__ == '__main__':
main()
| 1 | 4,060,138,598,828 | null | 85 | 85 |
x=int(input(''))
print(x*x*x)
|
import math
a,b,C=map(int,input().split())
radC=math.radians(C)
S=a*b*math.sin(radC)*(1/2)
c=a**2+b**2-2*a*b*math.cos(radC)
L=a+b+math.sqrt(c)
h=2*S/a
list=[S,L,h]
for i in list:
print('{:.08f}'.format(i))
| 0 | null | 221,663,122,768 | 35 | 30 |
ab = set([input() for _ in range(2)])
s = {'1', '2', '3'}
print(''.join(s-ab))
|
def main():
s = input()
s_ = set(s)
if len(s_) >= 2:
print('Yes')
else:
print('No')
if __name__ == "__main__":
main()
| 0 | null | 83,046,604,012,670 | 254 | 201 |
num_list = []
while True:
values = [int(x) for x in input().split()]
if 0 == values[0] and 0 == values[1]:
break
num_list.append(values)
for n, t in num_list:
ret = ' '.join(str(x + y + z) for x in range(1, n + 1) for y in range(x + 1, n + 1) for z in range(y + 1, n + 1))
cnt = 0
for x in ret.split():
if str(t) == x:
cnt += 1
print(cnt)
|
while True:
n,x = map(int,input().split())
if n==0 and x==0:
break
count=0
for a in range(1,n-1):
for b in range(a+1,n):
for c in range(b+1,n+1):
if a+b+c == x:
count += 1
print(count)
| 1 | 1,274,778,294,140 | null | 58 | 58 |
import sys
read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7
def main():
A, B, K = map(int, readline().split())
if A>=K:
A -= K
else:
B = max(B-(K-A),0)
A = 0
print(A, B)
if __name__ == '__main__':
main()
|
a, b, h, m = map(int, input().split())
from math import cos,sin, sqrt, radians
x_a = - a * cos(radians(360/12.0*h+360/12/60*m))
y_a = a * sin(radians(360/12.0*h+360/12/60*m))
x_b = - b * cos(radians(360/60.0*m))
y_b = b * sin(radians(360/60.0*m))
print(sqrt((x_a-x_b)**2+(y_a-y_b)**2))
| 0 | null | 61,890,533,854,462 | 249 | 144 |
S = input()
T = input()
flag = True
for i in range(len(S)):
if(S[i] != T[i]):
flag = False
if(flag):
print("Yes")
else:
print("No")
|
word1 = input()
word2 = input()
if word1 == word2[:-1]:
print('Yes')
else:
print('No')
| 1 | 21,401,239,974,688 | null | 147 | 147 |
N, X, M = map(int, input().split())
ls = [False]*M
ls_mod = []
x = X
for m in range(M+1):
if ls[x] == False:
ls_mod.append(x)
ls[x] = m
x = (x**2)%M
else:
last = m
fast = ls[x]
diff = last - fast
break
if M == 1:
print(0)
else:
if last > N:
print(sum(ls_mod[:N]))
else:
shou = (N-fast) // diff
amari = (N-fast) % diff
print(sum(ls_mod[:fast])+sum(ls_mod[fast:])*shou+sum(ls_mod[fast:fast+amari]))
|
n, x, m = map(int, input().split())
def solve(n, x, m):
if n == 1:
return x
arr = [x]
for i in range(1, n):
x = x*x % m
if x in arr:
rem = n-i
break
else:
arr.append(x)
else:
rem = 0
sa = sum(arr)
argi = arr.index(x)
roop = arr[argi:]
nn, r = divmod(rem, len(roop))
return sa + nn*sum(roop) + sum(roop[:r])
print(solve(n, x, m))
| 1 | 2,783,641,141,820 | null | 75 | 75 |
N,T=map(int,input().split())
G=[list(map(int,input().split())) for _ in range(N)]
G.sort()
dp=[0]*(60000)
for g in G:
for i in range(T-1,-1,-1):
dp[i+g[0]]=max(dp[i+g[0]],dp[i]+g[1])
print(max(dp))
|
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
#mod = 998244353
INF = 10**18
eps = 10**-7
# 01ナップサック問題
N,W = map(int,readline().split())
AB = [tuple(map(int,readline().split())) for i in range(N)]
AB.sort(key=lambda x: x[0])
dp = [0]*(W+1)
for (w,v) in AB:
dpnew = dp[::]
for j in range(W):
dpnew[min(j+w,W)] = max(dp[min(j+w,W)],dp[j] + v)
dp = dpnew
print(dp[W])
| 1 | 152,051,507,300,718 | null | 282 | 282 |
_ = input()
text = input()
print(text.count("ABC"))
|
n = int(input())
p = list(map(int,input().split()))
m = n
q = 2 * (10 ** 5)
for i in range(n):
q = min(p[i],q)
if not p[i] <= q:
m -= 1
print(m)
| 0 | null | 92,487,328,191,490 | 245 | 233 |
# coding: utf-8
# Your code here!
n=int(input())
sum=0
min=1000001
max=-1000001
table=list(map(int,input().split(" ")))
for i in table:
sum+=i
if max<i:
max=i
if min>i:
min=i
print(str(min)+" "+str(max)+" "+str(sum))
|
class MinMaxAndSum:
def output(self, a):
print "%d %d %d" % (min(a), max(a), sum(a))
if __name__ == "__main__":
mms = MinMaxAndSum()
raw_input()
a = map(int, raw_input().split())
mms.output(a)
| 1 | 712,509,676,266 | null | 48 | 48 |
from math import *
def gcd(x, y):
while y != 0:
(x, y) = (y, x % y)
return x
def lcm(x,y):
return x*y//gcd(x,y)
n = int(input())
a = [int(x) for x in input().split()]
mod = 1000000000+7
k = 1
for x in a:
k = lcm(k,x)
ans = 0
for x in a:
ans += k//x
print(ans%mod)
|
N, K = map(int, input().split())
a = [0]*N
for i in range(K):
d = int(input())
A = list(map(int, input().split()))
for l in A:
a[l-1] = 1
print(a.count(0))
| 0 | null | 56,253,074,882,222 | 235 | 154 |
s = input()
s_l = [0] * (len(s) + 1)
s_r = [0] * (len(s) + 1)
for i in range(len(s)):
if s[i] == '<':
s_l[i + 1] = s_l[i] + 1
for i in range(len(s)):
if s[- i - 1] == '>':
s_r[- i - 2] = s_r[- i - 1] + 1
ans = 0
for i, j in zip(s_l, s_r):
ans += max(i, j)
print(ans)
|
if __name__ == "__main__":
S = input()
count_right = 0
count_left = 0
prev = ""
a = []
current = 0
for i, s in enumerate(S):
if prev == ">" and s == "<":
a.append((count_left, count_right))
count_left = 0
count_right = 0
if s == ">":
count_right += 1
prev = ">"
# a.append((count_left)
else:
count_left += 1
prev = "<"
a.append((count_left, count_right))
s = 0
for x in a:
large = max(x[0], x[1])
small = min(x[0], x[1])
s += sum(range(large + 1)) + sum(range(small))
print(s)
| 1 | 155,851,246,766,138 | null | 285 | 285 |
import sys
def main():
n,m = map(int, input().split())
pn_list = [0]*n
a = [0]*m
b = [0]*m
ans_ac = 0
ans_pn = 0
pn_list[0]== -1
for i in range (m):
a[i],b[i]=input().split()
a[i] = (int(a[i]))
# print(a[i])
if pn_list[a[i]-1] != -1:
if b[i]== "AC":
ans_pn += pn_list[a[i]-1]
ans_ac +=1
pn_list[a[i]-1] = -1 #ペナルティリスト初期化
else:
pn_list[a[i]-1] += 1
print(ans_ac,ans_pn)
if __name__ == "__main__":
main()
|
n, m = map(int, input().split())
ans = (n*(n-1))*0.5 + (m*(m-1))*0.5
print(int(ans))
| 0 | null | 69,526,708,367,196 | 240 | 189 |
a,b = input().split(' ')
if int(a) > int(b):
print('a > b')
elif int(a) < int(b):
print('a < b')
else:
print('a == b')
|
n = int(input())
import math
l=0
for i in range(1,n+1):
for j in range(1,n+1):
p=math.gcd(i,j)
for k in range(1,n+1):
l+=math.gcd(p,k)
print(l)
| 0 | null | 17,953,430,172,552 | 38 | 174 |
from heapq import heappop, heappush
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
def dijkstra(graph, n, s):
d = [float("inf")] * n
d[s] = 0
q = []
heappush(q, (0, s))
while q:
dist, v = heappop(q)
if d[v] < dist: continue
for nv, cost in graph[v]:
if d[nv] > d[v] + cost:
d[nv] = d[v] + cost
heappush(q, (d[nv], nv))
return d
n = h*w+1
g = [list() for _ in range(n)]
for i in range(h):
for j in range(w-1):
cid = i*w + j
g[cid].append((cid+1, int(s[i][j] == "." and s[i][j+1] == "#")))
for i in range(h-1):
for j in range(w):
cid = i*w + j
g[cid].append((cid+w, int(s[i][j] == "." and s[i+1][j] == "#")))
g[n-1].append((0, int(s[0][0] == "#")))
print(dijkstra(g, n, n-1)[n-2])
|
#A
H,W = map(int,input().split())
G = [list(str(input())) for _ in range(H)]
inf = float("inf")
dist = [[inf for w in range(W)] for h in range(H)]
if G[0][0] == "#":
dist[0][0] = 1
else:
dist[0][0] = 0
for i in range(H):
for j in range(W):
if i == 0 and j == 0:
pass
elif j == 0:
dist[i][j] = dist[i-1][j]
if G[i][j] == "#" and G[i-1][j] != "#":
dist[i][j]+=1
elif i == 0:
dist[i][j] = dist[i][j-1]
if G[i][j] == "#" and G[i][j-1] != "#":
dist[i][j]+=1
else:
d1 = dist[i-1][j]
d2 = dist[i][j-1]
if G[i][j] == "#" and G[i-1][j] != "#":
d1+=1
if G[i][j] == "#" and G[i][j-1] != "#":
d2+=1
dist[i][j] = min(d1,d2)
print(dist[H-1][W-1])
| 1 | 49,264,148,677,568 | null | 194 | 194 |
from collections import deque
rand = False
verbose = False
if rand:
import random
N, K = 200000, 10000
A = random.choices(range(-10**9, 10**9), k=N)
else:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
MOD = 10**9 + 7
def mul(A):
ret = 1
for i in range(len(A)):
ret = (ret * A[i]) % MOD
# print (ret)
return ret
if K==N:
print(mul(A))
exit()
POS, NEG = list(), list()
for i in range(len(A)):
if A[i] < 0:
NEG.append(A[i])
else:
POS.append(A[i])
POS.sort(reverse=True)
NEG.sort()
if len(POS)==0 and K%2==1: # must go negative
print(mul(NEG[-K:]))
exit()
POS = deque(POS)
NEG = deque(NEG)
ans = list()
while len(ans)<K:
if len(NEG) <= 1:
if verbose: print('no more neg numbers')
ans.append(POS.popleft()) # no negative numbers to add
elif len(POS) <= 1:
if verbose: print('no more pos numbers')
if K-len(ans)>=2:
ans.append(NEG.popleft())
ans.append(NEG.popleft())
else:
ans.append(POS.popleft())
elif POS[0]*POS[1] > NEG[0]*NEG[1] or K-len(ans)==1:
if verbose: print('pos bigger')
ans.append(POS.popleft())
else:
if verbose: print('neg biger')
ans.append(NEG.popleft())
ans.append(NEG.popleft())
print(mul(ans))
|
data =input()
s1 =[]
ans =[]
memo=-1
for i in range(len(data)):
if data[i]=='\\':
s1.append(i)
if data[i]=='/':
if len(s1)==0:pass
# elif len(s1)==1:
# ans.append(i-s1.pop())
else:
a = i-s1[-1]
while len(ans)!=0:
if ans[-1][1]<=s1[-1]:break
a+=ans.pop()[0]
ans.append([a,s1.pop()])
# print(ans)
ans2=[int(ans[i][0]) for i in range(len(ans))]
print(sum(ans2))
print(len(ans),end='')
if len(ans):
print('',end=' ')
print(' '.join(list(map(str,ans2))))
| 0 | null | 4,792,874,947,808 | 112 | 21 |
y,x=[int(i) for i in input().split()]
s=[[int(0)for i in range(x+1)]for j in range(y+1)]
for i in range(y):
k=input().split()
for j in range(x):
s[i][j]=int(k[j])
for i in range(y):
for j in range(x):
s[i][x]=s[i][x]+s[i][j]
s[y][j]=s[y][j]+s[i][j]
for i in range(y):
s[y][x]=s[y][x]+s[i][x]
for i in range(y+1):
for j in range(x):
print(s[i][j],"",end="")
print(s[i][x])
|
r, c = map(int, input().split())
sumOfCols = [0] * c
for i in range(r):
cols = list(map(int, input().split()))
s = sum(cols)
sumOfCols = [p + q for p,q in zip(sumOfCols, cols)]
print(' '.join(map(str, cols)), end=' ')
print(s)
s = sum(sumOfCols)
print(' '.join(map(str, sumOfCols)), end=' ')
print(s)
| 1 | 1,360,311,937,780 | null | 59 | 59 |
def main():
n = int(input())
ans = 0
for i in range(1, n + 1):
if (i % 3 != 0) and (i % 5 != 0):
ans += i
print(ans)
main()
|
N=input()
ans=0
for i in range(1,int(N)+1):
if i%15==0:
pass
elif i%5==0:
pass
elif i%3==0:
pass
else:
ans=ans+i
print(ans)
| 1 | 35,083,161,877,158 | null | 173 | 173 |
import sys
def solve():
input = sys.stdin.readline
A, B = map(int, input().split())
if A < 10 and B < 10: print(A * B)
else: print(-1)
return 0
if __name__ == "__main__":
solve()
|
#!/usr/bin/env python3
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**9)
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))
#%%code
def resolve():
A,B=pin()
if A>9 or B>9:print(-1);return
print(A*B)
#%%submit!
resolve()
| 1 | 158,886,464,101,020 | null | 286 | 286 |
import sys
N = int(input()); A = list(map(int,input().split()))
if N == 2:
print(max(A))
sys.exit()
dp = [0,max(A[:3]),A[0]+A[2]]
tmp1 = dp[:]
tmp2 = [0,max(A[:2]),0]
flag1 = False
flag2 = True if A[0] < A[2] and A[1] < A[2] else False
flag3 = False
for i in range(3,N):
b = A[i]
if i%2 == 0:
dp[0] = max(tmp2[0],tmp1[0]+b)
flag1 = True if tmp2[0] < tmp1[0]+b else False
if flag3:
dp[1] = max(tmp2[1],tmp1[1]+b)
flag2 = True if tmp2[1] < tmp1[1]+b else False
else:
dp[1] = max(tmp2[1],tmp2[0]+b,tmp1[1]+b)
flag2 = True if tmp2[1] < tmp2[0]+b or tmp2[1] < tmp1[1]+b else False
dp[2] += A[i]
tmp1 = dp[:]
else:
if flag1:
dp[0] = max(tmp1[1],tmp2[0]+b)
flag3 = True if tmp1[1] < tmp2[0]+b else False
else:
dp[0] = max(tmp1[1],tmp1[0]+b,tmp2[0]+b)
flag3 = True if tmp1[1] < tmp1[0]+b or tmp1[1] < tmp2[0]+b else False
if flag2:
dp[1] = max(tmp1[2],tmp2[1]+b)
else:
dp[1] = max(tmp1[2],tmp1[1]+b,tmp2[1]+b)
tmp2 = dp[:]
print (dp[1])
|
N = int(input())
A = list(map(int, input().split()))
if N % 2 == 0:
M = N // 2
even_A = [A[i* 2] for i in range(M)]
odd_A = [A[i* 2 + 1] for i in range(M)]
even_sum = []
_sum = 0
for a in even_A:
_sum += a
even_sum.append(_sum)
odd_sum = []
_sum = 0
for a in odd_A:
_sum += a
odd_sum.append(_sum)
ans = max(odd_sum[-1], even_sum[-1])
for i in range(M):
x = even_sum[i] + odd_sum[-1] - odd_sum[i]
ans = max(ans, x)
print(ans)
exit()
M = N // 2
# evenが元一つ多い
even_A = [A[i* 2] for i in range(M+1)]
odd_A = [A[i* 2 + 1] for i in range(M)]
even_sum = []
_sum = 0
for a in even_A:
_sum += a
even_sum.append(_sum)
odd_sum = []
_sum = 0
for a in odd_A:
_sum += a
odd_sum.append(_sum)
# 全て奇数
all_even = even_sum[-1] - min(even_A)
diff_odd_even = [odd_sum[k] - even_sum[k+1] for k in range(M)]
diff_max_odd_even = []
diff_max = diff_odd_even[-1]
for k in range(M):
diff_max = max(diff_max, diff_odd_even[-k-1])
diff_max_odd_even.append(diff_max)
diff_max_odd_even.reverse()
ans = all_even
for i in range(M):
x = even_sum[i] + odd_sum[-1] - odd_sum[i]
y = odd_sum[i] + even_sum[-1] - even_sum[i+1]
z = even_sum[i] + - odd_sum[i] + even_sum[-1] + diff_max_odd_even[i]
ans = max(ans, x, y, z)
print(ans)
| 1 | 37,240,378,938,090 | null | 177 | 177 |
n,m=map(int,input().split())
*s,=map(int,input()[::-1])
i=0
a=[]
while i<n:
j=min(n,i+m)
while s[j]:j-=1
if j==i:
print(-1)
exit()
a+=j-i,
i=j
print(*a[::-1])
|
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
#N = int(input())
#A = list(map(int,input().split()))
#S = list(input())
#S.remove("\n")
#N,M = map(int,input().split())
#S,T = map(str,input().split())
#A = [int(input()) for _ in range(N)]
#S = [list(input())[:-1] for _ in range(N)]
#A = [list(map(int,input().split())) for _ in range(N)]
MOD = 998244353
N = int(input())
D = list(map(int,input().split()))
d = defaultdict(int)
for i in D:
d[i] += 1
ans = 1
if d[0] != 1 or D[0] != 0:
print(0)
else:
for i in range(1,N):
ans = ans*pow(d[i-1],d[i],MOD)
print(ans%MOD)
| 0 | null | 147,202,600,791,740 | 274 | 284 |
n, m, ll = map(int, input().split())
a = []
b = []
for i in range(n):
a.append(list(map(int, input().split())))
for i in range(m):
b.append(list(map(int, input().split())))
for i in range(n):
prt = []
for j in range(ll):
s = 0
for k in range(m):
s += a[i][k] * b[k][j]
prt.append(s)
print(*prt)
|
n,m,l=map(int,input().split())
b=[]
for i in range(n):
x= list(map(int, input().split()))
b.append(x)
y=[]
for i in range(m):
z= list(map(int, input().split()))
y.append(z)
c = [[0] * l for i in range(n)]
for i in range(n):
for j in range(l):
for k in range(m):
c[i][j] += b[i][k] * y[k][j]
print(*c[i]) #*をつけると[]が外れて表示される
| 1 | 1,450,987,022,312 | null | 60 | 60 |
import math
N= int(input())
ans = math.ceil(N/2)-1
print(ans)
|
n = int(input())
print(max(0,((n-1)//2)))
| 1 | 153,196,543,937,122 | null | 283 | 283 |
h,w,kk=list(map(int,input().split()))
s=[list(map(int,list(input()))) for i in range(h)]
l=[]
for i in range(2**(h-1)):
ll=[0]
for j in range(h-1):
if i & 2**j:
ll.append(ll[-1]+1)
else:
ll.append(ll[-1])
l.append(ll)
mc=w+h
for i in l:
c=[0 for i in range(h)]
cc=0
f=1
#print(i,0)
for j in range(w):
#print(c)
ff=0
for k in range(h):
c[i[k]]+=s[k][j]
for k in range(h):
if c[k]>kk:
ff=1
break
if ff:
cc+=1
c=[0 for i in range(h)]
for k in range(h):
c[i[k]]+=s[k][j]
for k in range(h):
if c[k]>kk:
f=1
if f:
cc=w+h
#print(c)
break
f=0
#print(cc)
if mc>cc+len(set(i))-1:
mc=cc+len(set(i))-1
print(mc)
|
h1,m1,h2,m2,k = list(map(int,input().split()))
h_m = (h2 - h1)*60
m = m2 - m1
tz = h_m + m
print(tz-k)
| 0 | null | 33,385,175,970,720 | 193 | 139 |
X = int(input())
N = X // 100
for n in range(N + 1):
M = X - n * 100
if M > n * 5:
continue
else:
print(1)
break
else:
print(0)
|
h, w, n = (int(input()) for i in range(3))
print(-(-n // max(h, w)))
| 0 | null | 108,082,675,587,728 | 266 | 236 |
n = int(input())
a = [int(i) for i in input().split()]
for i in range(len(a)):
v = a[i]
j = i - 1
while j >= 0 and a[j]>v:
a[j+1] = a[j]
j-=1
a[j+1]=v
print(" ".join([str(i) for i in a]))
|
N=int(input())
arr=list(map(int,input().split()))
print(' '.join(map(str,arr)))
for key in range(1,len(arr)):
temp=arr[key]
i=key-1
while i>=0 and arr[i]>temp:
arr[i+1]=arr[i]
i-=1
arr[i+1]=temp
print(' '.join(map(str,arr)))
| 1 | 5,774,547,710 | null | 10 | 10 |
n=int(input())
a=list(map(int,input().split()))
d={}
for i in range(n):
if a[i] in d:
d[a[i]]+=1
else:
d[a[i]]=1
ans=0
D=list(d.values())
for i in range(len(D)):
s=D[i]*(D[i]-1)//2
ans+=s
for i in range(n):
print(ans-d[a[i]]+1)
|
n,a,b = list(map(int, input().split()))
MOD = 10**9 + 7
from functools import lru_cache
MOD = 10**9+7
fact = [1]
fact_inv = [1]
fact_rev = [1]
for i in range(1, max(a,b)+1):
fact.append(fact[i-1] * i % MOD)
fact_inv.append(pow(fact[i], MOD-2, MOD))
fact_rev.append(fact_rev[i-1] * (n-i+1) % MOD)
@lru_cache(maxsize=None)
def combi(n, k):
if n < k:
return 0
if n < 0 or k < 0:
return 0
return fact_rev[n] * fact_inv[k] % MOD
pattern = pow(2, n, MOD) - 1
if a==b:
pattern -= (combi(a, a))
else:
pattern -= (combi(a, a) + combi(b, b))
print(pattern%MOD)
| 0 | null | 56,811,406,282,942 | 192 | 214 |
# encoding:utf-8
import copy
import random
import bisect #bisect_left これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000
d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))
S = str(input())
if S[2] == S[3] and S[4] == S[5]:
ans = "Yes"
else:
ans = "No"
print(ans)
|
s = input()
ans = 'Yes'
if s[2] != s[3]:
ans = 'No'
if s[4] != s[5]:
ans = 'No'
print(ans)
| 1 | 41,971,792,322,212 | null | 184 | 184 |
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque
sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7
DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def main():
N, T = LI()
AB = []
for i in range(N):
_a, _b = LI()
AB.append((_a, _b))
AB = sorted(AB, key=lambda x: (x[0], x[1]))
a = [elem[0] for elem in AB]
b = [elem[1] for elem in AB]
dp = [[0 for _ in range(T + 3001)] for _ in range(N+1)]
for i in range(N):
for t in range(T):
dp[i+1][t+a[i]] = max(dp[i+1][t+a[i]], dp[i][t] + b[i])
dp[i+1][t] = max(dp[i+1][t], dp[i][t])
ans = 0
for i in range(N+1):
for j in range(T + 3001):
ans = max(dp[i][j], ans)
print(ans)
main()
|
'''
Auther: ghoshashis545 Ashis Ghosh
College: jalpaiguri Govt Enggineering College
'''
from os import path
import sys
from heapq import heappush,heappop,heapify
from functools import cmp_to_key as ctk
from collections import deque,defaultdict as dd
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input().rstrip()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
mod=1000000007
# mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]
def b(i):
# ord(i) -> Ascii value of i
return ord(i)-ord('a')
file = 1
def solve():
x=[599,799,999,1199,1399,1599,1799,1999]
n=ii()
for i in range(8):
if n<=x[i]:
print(8-i)
break
if __name__ =="__main__":
if(file):
if path.exists('input.txt'):
sys.stdin=open('input.txt', 'r')
sys.stdout=open('output.txt','w')
else:
input=sys.stdin.readline
solve()
| 0 | null | 79,108,257,541,800 | 282 | 100 |
x, y = map(int, input().split())
flag = 0
for i in range(101):
for j in range(101):
if i+j == x:
if 2*i+4*j == y:
print("Yes")
flag += 1
break
else:
continue
break
if flag==0:
print("No")
|
X, K, D = map(int, input().split())
ans = abs(abs(X) - D*K)
a = abs(X)//D
b = abs(X)%D
if (K - a)%2 == 1 and a < K:
ans = D - b
elif (K - a)%2 == 0 and a < K:
ans = b
print(ans)
| 0 | null | 9,426,231,293,820 | 127 | 92 |
from collections import deque
n, m, k = map(int,input().split())
friend_sum = [[] for i in range(n)]
block = []
block_sum = [[] for i in range(n)]
for i in range(m):
a, b = map(int,input().split())
if a > b:
a, b = b, a
friend_sum[a-1].append(b-1)
friend_sum[b-1].append(a-1)
for i in range(k):
a, b = map(int,input().split())
block.append([a, b])
block_sum[a-1].append(b-1)
block_sum[b-1].append(a-1)
label = 0
g_label = [0 for i in range(n)]
g_list = [0 for i in range(n)]
for i in range(n):
if g_label[i] == 0:
label += 1
g_label[i] = label
g_list[label-1] += 1
q = deque([])
q.append(i)
while len(q) != 0:
num = q.popleft()
for j in range(len(friend_sum[num])):
if g_label[friend_sum[num][j]] == 0:
g_label[friend_sum[num][j]] = label
g_list[label-1] += 1
q.append(friend_sum[num][j])
ans = 0
for i in range(n):
ans = g_list[g_label[i]-1] - len(friend_sum[i]) - 1
for j in range(len(block_sum[i])):
if g_label[i] == g_label[block_sum[i][j]]:
ans -= 1
print(ans, end=' ')
print()
|
n,m,k = map(int, input().split())
g = [[] for i in range(n+1)]
f = [0]*(n+1)
b = []
for i in range(m):
u, v = map(int, input().split())
g[u].append(v)
g[v].append(u)
f[u] += 1
f[v] += 1
for i in range(k):
b.append(map(int, input().split()))
count = 0
renketu_list = [-1]*(n+1)
renketu_size = [0]
stack = []
for i in range(1, n+1):
if renketu_list[i] == -1:
count += 1
renketu_list[i] = count
renketu_size.append(1)
while len(g[i]) > 0:
stack.append(g[i].pop())
while len(stack) > 0:
v=stack.pop()
if renketu_list[v] == -1:
renketu_list[v] = count
renketu_size[count] += 1
while len(g[v])>0:
stack.append(g[v].pop())
s = [0] * (n+1)
for i in range(1, n+1):
s[i] += renketu_size[renketu_list[i]] - f[i] - 1
for i in range(k):
u, v = b[i]
if renketu_list[u] == renketu_list[v]:
s[u] -= 1
s[v] -= 1
print(" ".join(list(map(str, s[1:]))))
| 1 | 62,015,963,022,770 | null | 209 | 209 |
from collections import Counter
n = int(input())
a = [int(x) for x in input().split()]
arr1 = []
arr2 = []
for i in range(1, n + 1):
arr1.append(i - a[i - 1])
arr2.append(i + a[i - 1])
dis1 = Counter(arr1)
dis2 = Counter(arr2)
ans = 0
for i in dis1.keys():
ans += dis1[i] * dis2[i]
print(ans)
|
n = input()
A = [i for i in input().split()]
A.reverse()
print(' '.join(A))
| 0 | null | 13,506,395,320,560 | 157 | 53 |
import sys
input = lambda: sys.stdin.readline().rstrip()
def solve():
N = int(input())
A = list(map(int, input().split()))
if A[0] != 0:
if N == 0 and A[0] == 1:
print('1')
exit()
else:
print('-1')
exit()
if N == 0:
print('1')
exit()
v = [0 for _ in range(N + 1)]
v[N] = A[N]
for i in range(N - 1, -1, -1):
up = min(2 ** i, v[i + 1] + A[i])
v[i] = up
# print(v)
for i in range(N):
up = min(v[i + 1], (v[i] - A[i]) * 2)
v[i + 1] = up
# print(v)
ans = 0
for i in range(N + 1):
if v[i] < A[i]:
print('-1')
exit()
ans += v[i]
# print(v)
print(ans)
if __name__ == '__main__':
solve()
|
import math
class Circle:
def output(self, r):
print "%.6f %.6f" % (math.pi * r * r, 2.0 * math.pi * r)
if __name__ == "__main__":
cir = Circle()
r = float(raw_input())
cir.output(r)
| 0 | null | 9,747,230,596,560 | 141 | 46 |
from collections import deque
import sys
deq = deque()
q = int(input())
for _ in range(q):
s = input()
if s == 'deleteFirst':
deq.popleft()
elif s == 'deleteLast':
deq.pop()
else:
ss, num = s.split()
if ss == 'insert':
deq.appendleft(num)
else:
try:
deq.remove(num)
except:
pass
print(" ".join(deq))
|
from collections import deque
n=int(input())
commands=[input().split() for i in range(n)]
List=deque()
for list_command in commands:
if 'insert' in list_command:
List.appendleft(list_command[1])
elif 'delete' in list_command:
try:
List.remove(list_command[1])
except:
pass
elif 'deleteFirst' in list_command:
List.popleft()
elif 'deleteLast' in list_command:
List.pop()
print(*List)
| 1 | 51,060,444,942 | null | 20 | 20 |
x,y,a,b,c=map(int,input().split())
R=sorted(map(int,input().split()),reverse=True)
G=sorted(map(int,input().split()),reverse=True)
N=sorted(map(int,input().split()),reverse=True)
L=sorted(R[:x]+G[:y]+N[:(x+y)],reverse=True)
print(sum(L[:(x+y)]))
|
x, y = map(int,raw_input().split())
if(x>y):
print "a > b"
elif(x<y):
print "a < b"
else:
print "a == b"
| 0 | null | 22,676,951,696,642 | 188 | 38 |
s,t = [x for x in input().split()]
a,b = [int(x) for x in input().split()]
ss = input()
if ss == s:
print(a-1,b)
else:
print(a,b-1)
|
S, T = input().split()
A, B = map(int, input().split())
U = input()
if U == S:
A -= 1
else:
B -= 1
print(f'{A} {B}')
| 1 | 72,129,140,029,692 | null | 220 | 220 |
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)
class BinaryIndexedTree(object):
def __init__(self, n):
# index is 1-based so vals[0] never be used
self.n = n
self.vals = [0] * (n + 1)
def add(self, i, v):
while i <= self.n:
self.vals[i] += v
i += i & -i
def sum(self, i):
ret = 0
while i > 0:
ret += self.vals[i]
i -= i & -i
return ret
def sum_range(self, l, r):
# l and r are 1-based index and inclusive
return self.sum(r) - self.sum(l - 1)
def solve(N, S, Q, queries):
trees = []
for i in range(26):
trees.append(BinaryIndexedTree(N))
s = []
for i, c in enumerate(S):
c_idx = ord(c) - ord('a')
if 'a' <= c <= 'z':
trees[c_idx].add(i + 1, 1)
s.append(c)
for q in queries:
if q[0] == '1':
i, c = int(q[1]), q[2]
p = s[i - 1]
s[i - 1] = c
trees[ord(p) - ord('a')].add(i, -1)
trees[ord(c) - ord('a')].add(i, 1)
if q[0] == '2':
l, r = int(q[1]), int(q[2])
ret = 0
for tree in trees:
cnt = tree.sum_range(l, r)
if cnt:
ret += 1
print(ret)
#pprint(get_range_sum(1, 5, bit))
#print(s)
#print()
return
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
N = int(next(tokens))
S = str(next(tokens))
Q = int(next(tokens))
q = []
for i in range(Q):
q.append(list(map(str, input().split())))
solve(N, S, Q, q)
if __name__ == '__main__':
main()
|
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 ok(p):
result = 0
for i in range(n):
result += max(a[i]-p//f[i],0)
if result <= k:
return True
return False
L = 0
U = a[-1]*f[0]
while U-L >= 1:
M = (U+L)//2
if ok(M):
U = M
else:
L = M+1
print(U)
| 0 | null | 113,466,862,635,490 | 210 | 290 |
a, b = input().split()
a = int(a)
b = int(b)
if a > b:
print('a > b')
elif a < b:
print('a < b')
else:
print('a == b')
|
# -*- coding: utf-8 -*-
list = map(int, raw_input().split())
a = list[0]
b = list[1]
if a < b:
print "a < b"
elif a > b:
print "a > b"
else:
print "a == b"
| 1 | 363,801,621,440 | null | 38 | 38 |
import itertools
import math
K = int(input())
nums = range(1, K + 1)
comb = itertools.combinations_with_replacement(nums, 3)
ans = 0
for v in comb:
n1 = math.gcd(v[0], v[1])
n2 = math.gcd(n1, v[2])
l = len(list(set(v)))
if l == 3:
ans += 6 * n2
elif l == 2:
ans += 3 * n2
elif l == 1:
ans += n2
print(ans)
|
import numpy as np
N=int(input())
A=np.array(list(map(int,input().split())))
'''
以下、方針
・Aから、各要素の出現回数を取得する。
・出現回数から、組み合わせを計算する。
'''
uni,cou=np.unique(A,return_counts=True)
# print(uni)
# print(cou)
dic0={}
dic1={}
dic={} #回答用
com=[]
for i in range(len(cou)):
c=cou[i]*(cou[i]-1)//2
dic0[uni[i]]=cou[i]
dic1[uni[i]]=c
com.append(c)
dic.setdefault(uni[i])
# print(com)
totalcombination=sum(com)
# print(com)
# print(dic1)
'''
以下方針
・Aから数字を取得
・その数字に対応する組み合わせを減らす。
・その数字の出現回数を1減らした状態の組み合わせの数を足す。
'''
for j in range (N):
n=A[j] #nはAの数列の数字
if dic[n]==None:
e=(dic0[n]-1)*(dic0[n]-2)//2
t=totalcombination-dic1[n]+e
dic[n]=t
print(t)
else:
print(dic[n])
| 0 | null | 41,653,342,855,520 | 174 | 192 |
from typing import List
def DFS(G: List[int]):
global time
time = 0
n = len(G)
for u in range(n):
if color[u] == "WHITE":
DFS_Visit(u)
def DFS_Visit(u: int):
global time
color[u] = "GRAY"
time += 1
d[u] = time
for v in G[u]:
if color[v] == "WHITE":
pi[v] = u
DFS_Visit(v)
color[u] = "BLACK"
time += 1
f[u] = time
if __name__ == "__main__":
n = int(input())
G = []
color = ["WHITE" for _ in range(n)]
pi = [None for _ in range(n)]
d = [0 for _ in range(n)]
f = [0 for _ in range(n)]
for _ in range(n):
command = list(map(int, input().split()))
G.append([x - 1 for x in command[2:]])
DFS(G)
for i in range(n):
print(f"{i + 1} {d[i]} {f[i]}")
|
T = [0]
def curserch(N,k,T,P,n):
T[0] = T[0]+1
P[k]["d"] = T[0]
for i in range(1,n+1):
if N[k][i]==1 and P[i]["d"]==0:
curserch(N,i,T,P,n)
T[0] = T[0]+1
P[i]["f"]=T[0]
n = int(input())
A = [[0 for j in range(n+1)] for i in range(n+1)]
for i in range(n):
vec = input().split()
u = int(vec[0])
k = int(vec[1])
nodes = vec[2:]
for i in range(int(k)):
v = int(nodes[i])
A[u][v] = 1
P=[{"d":0} for i in range(n+1)]
for i in range(1,n+1):
if P[i]["d"]==0:
curserch(A,i,T,P,n)
T[0] = T[0]+1
P[i]["f"]=T[0]
for i in range(1,n+1):
print(i,P[i]["d"],P[i]["f"])
| 1 | 3,195,134,180 | null | 8 | 8 |
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, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = 10**6#float('inf')
#mod = 10 ** 9 + 7
mod = 998244353
#from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10
N, M = MAP()
a = LIST()
A = sorted([x//2 for x in a], reverse=True)
lcm = A[-1]
for x in A:
lcm = lcm*x//gcd(lcm, x)
r = lcm//A[0]
n = M//A[0]
for x in A:
if lcm//x%2 == 0:
print(0)
exit()
#2p+1が1<=2p+1<=n の範囲で rの倍数となるpの個数
if r%2 == 0:
print(0)
else:
print(n//r - n//(2*r))
|
def pascal(xdata):
for x in xrange(3):
for y in xrange(3):
for z in xrange(3):
if xdata[x]**2 + xdata[y]**2 == xdata[z]**2:
return True
return False
N = input()
data = [map(int, raw_input().split()) for x in range(N)]
for x in data:
if pascal(x):
print "YES"
else:
print "NO"
| 0 | null | 51,213,757,330,414 | 247 | 4 |
# ??¨????????°r??¨?????°c???r ?? c ???????´????????????¨?????????????????§???????????¨??????????¨?????????\????????°????????¨???????????????????????°??????
r, c = map(int, input().split())
spreadSheet = [[0 for i in range(c + 1)] for j in range(r + 1)]
# ??¨??\????????????
for i in range(0, r):
spreadSheet[i][:c] = map(int, input().split())
# print(spreadSheet)
# ?????????????¨??????????
for i in range(0, r):
for j in range(0, c):
spreadSheet[i][c] += spreadSheet[i][j]
# ?????????????¨??????????
for i in range(0, c + 1):
for j in range(0, r):
spreadSheet[r][i] += spreadSheet[j][i]
# print(spreadSheet)
for i in range(0, r + 1):
for j in range(0, c + 1):
print("{0}".format(spreadSheet[i][j]), end="")
if j != c:
print(" ",end="")
else:
print("")
|
n, p = [int(i) for i in input().split()]
a = [[] for _ in range(p+1)]
for _ in range(n):
l = [int(i) for i in input().split()]
l.append(sum(l))
[a[i].append(j) for i, j in enumerate(l)]
print(*l)
for h,i in enumerate(a):
if h == len(a) - 1:
print(sum(i))
else:
print(sum(i),end=' ')
| 1 | 1,339,615,681,158 | null | 59 | 59 |
N = int(input())
P = list(map(int,input().split()))
num = float("inf")
ans = 0
for i in range(N):
if num >= P[i]:
ans += 1
num = P[i]
print(ans)
|
import sys
N, M, K = [int(s) for s in sys.stdin.readline().split()]
n = 0
A = [0]
for s in sys.stdin.readline().split():
n = n + int(s)
A.append(n)
n = 0
B = [0]
for s in sys.stdin.readline().split():
n = n + int(s)
B.append(n)
ans = 0
for i in range(N + 1):
if A[i] > K:
break
for j in range(M, -1, -1):
if A[i] + B[j] <= K:
ans = max(ans, i + j)
M = j
break
print(ans)
| 0 | null | 47,934,789,069,090 | 233 | 117 |
n,m = map(int,input().split())
print("Yes" if n == m else "No")
|
import math
def LI():
return list(map(int, input().split()))
L, R, d = LI()
Ld = (L-1)//d
Rd = R//d
ans = Rd-Ld
print(ans)
| 0 | null | 45,157,435,140,704 | 231 | 104 |
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007
def main():
H, A = map(int, read().split())
print((H + A - 1) // A)
return
if __name__ == '__main__':
main()
|
H, A = map(int,input().split())
a = int(H/A)
b = H/A
if a==b:
print(a)
else:
c = a+1
print(c)
| 1 | 77,017,151,593,756 | null | 225 | 225 |
import sys
alpha = "abcdefghijklmnopqrstuvwxyz"
dic = {c:0 for c in alpha}
t = sys.stdin.read()
for s in t:
s = s.lower()
if s in alpha :
dic[s] += 1
for key in sorted(dic):
print("{} : {}".format(key,dic[key]))
|
import string
import sys
table = {c: 0 for c in string.ascii_lowercase}
for raw in sys.stdin.read():
c = raw.lower()
if c in string.ascii_lowercase:
table[c] += 1
for key in string.ascii_lowercase:
print('{:s} : {:d}'.format(key, table[key]))
| 1 | 1,668,077,767,652 | null | 63 | 63 |
n,m = map(int,input().split())
od_od = n*(n-1)/2
ev_ev = m*(m-1)/2
print(int(od_od+ev_ev))
|
import math
num_of_even, num_of_odd = map(int, input().split())
def combinations_count(n, r):
if n < 2:
return 0
else:
return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
if num_of_even == 1 and num_of_odd == 1:
print('0')
else:
print(combinations_count(num_of_odd, 2) + combinations_count(num_of_even, 2))
| 1 | 45,574,162,914,548 | null | 189 | 189 |
n = int(input())
a = list(map(int, input().split()))
a = a[::-1]
for cnt, item in enumerate(a, 1):
if cnt == n:
print(item)
else:
print(item,end=' ')
|
from sys import stdin
stdin.readline().rstrip()
a = [int(x) for x in stdin.readline().rstrip().split()]
a.reverse()
print(*a)
| 1 | 971,759,763,432 | null | 53 | 53 |
n = int(input())
X_MAX = 50001
for x in range(X_MAX):
if int(x * 1.08) == n:
print(x)
exit()
print(":(")
|
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)
| 0 | null | 74,018,717,429,184 | 265 | 150 |
from collections import Counter
n = list(input())
n.reverse()
x = [0]*(len(n))
a=0
ex=1
for i in range(len(n)):
a+=int(n[i])*ex
x[i]=a%2019
ex=ex*10%2019
c=Counter(x)
ans = c[0]
for i in c.keys():
b=c[i]
ans += b*(b-1)//2
print(ans)
|
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from fractions import Fraction
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations, accumulate
from operator import add, mul, sub, itemgetter, attrgetter
import sys
sys.setrecursionlimit(10**6)
# readline = sys.stdin.buffer.readline
readline = sys.stdin.readline
INF = 1 << 60
def read_int():
return int(readline())
def read_int_n():
return list(map(int, readline().split()))
def read_float():
return float(readline())
def read_float_n():
return list(map(float, readline().split()))
def read_str():
return readline().strip()
def read_str_n():
return readline().strip().split()
def ep(*args):
print(*args, file=sys.stderr)
def mt(f):
import time
def wrap(*args, **kwargs):
s = time.perf_counter()
ret = f(*args, **kwargs)
e = time.perf_counter()
ep(e - s, 'sec')
return ret
return wrap
MOD = 998244353
class ModInt:
def __init__(self, x=0):
self.x = x % MOD
def __str__(self):
return str(self.x)
__repr__ = __str__
def __add__(self, other):
return (
ModInt(self.x + other.x) if isinstance(other, ModInt) else
ModInt(self.x + other)
)
def __sub__(self, other):
return (
ModInt(self.x - other.x) if isinstance(other, ModInt) else
ModInt(self.x - other)
)
def __mul__(self, other):
return (
ModInt(self.x * other.x) if isinstance(other, ModInt) else
ModInt(self.x * other)
)
def __truediv__(self, other):
return (
ModInt(self.x * pow(other.x, MOD - 2)) if isinstance(other, ModInt) else
ModInt(self.x * pow(other, MOD - 2))
)
def __pow__(self, other):
return (
ModInt(pow(self.x, other.x)) if isinstance(other, ModInt) else
ModInt(pow(self.x, other))
)
__radd__ = __add__
def __rsub__(self, other):
return (
ModInt(other.x - self.x) if isinstance(other, ModInt) else
ModInt(other - self.x)
)
__rmul__ = __mul__
def __rtruediv__(self, other):
return (
ModInt(other.x * pow(self.x, MOD - 2)) if isinstance(other, ModInt) else
ModInt(other * pow(self.x, MOD - 2))
)
def __rpow__(self, other):
return (
ModInt(pow(other.x, self.x)) if isinstance(other, ModInt) else
ModInt(pow(other, self.x))
)
@mt
def slv(N, K, LR):
memo = [ModInt(0)] * (N+2)
memo[1] = 1
memo[1+1] = -1
for i in range(1, N+1):
memo[i] += memo[i-1]
for l, r in LR:
ll = min(N+1, i+l)
rr = min(N+1, i+r+1)
memo[ll] += memo[i]
memo[rr] -= memo[i]
return memo[N]
def main():
N, K = read_int_n()
LR = [read_int_n() for _ in range(K)]
print(slv(N, K, LR))
if __name__ == '__main__':
main()
| 0 | null | 16,718,267,960,288 | 166 | 74 |
def main():
S = input()
T = input()
l_s = len(S)
l_t = len(T)
cnt_list = []
for i in range(l_s - l_t + 1):
cnt = 0
S_ = S[i: i+l_t]
for j in range(l_t):
if S_[j] != T[j]:
cnt += 1
cnt_list.append(cnt)
ans = min(cnt_list)
print(ans)
return
if __name__ == '__main__':
main()
|
S = list(input())
T = list(input())
mindiff = len(S)
for i in range(len(S) - len(T) +1):
diff = 0
for j in range(len(T)):
if(S[i+j] != T[j]):
diff += 1
pass
if(diff < mindiff):
mindiff = diff
print(mindiff)
| 1 | 3,701,064,737,668 | null | 82 | 82 |
N = int(input())
A = list(map(int, input().split()))
M = 0
S = 0
for a in A:
if M < a:
M = a
if a < M:
S += M - a
print(S)
|
import sys
while True:
h, w = [ int( val ) for val in sys.stdin.readline().split( " " ) ]
if 0 ==h and 0 == w:
break
pa = [ "#", "." ]
ouput = []
for i in range( h ):
for j in range( w ):
ouput.append( pa[(i+j)%2] )
ouput.append( "\n" )
print( "".join( ouput ) )
| 0 | null | 2,761,629,050,112 | 88 | 51 |
def main() :
n = int(input())
nums = [int(i) for i in input().split()]
flag = True
count = 0
index = 0
while flag :
flag = False
for i in reversed(range(index+1, n)) :
if nums[i-1] > nums[i] :
nums[i-1], nums[i] = nums[i], nums[i-1]
count += 1
flag = True
index += 1
nums_str = [str(i) for i in nums]
print(" ".join(nums_str))
print(count)
if __name__ == '__main__' :
main()
|
n = int(input())
numbers = [int(i) for i in input().split(" ")]
flag = 1
cnt = 0
while flag:
flag = 0
for j in range(n - 1, 0, -1):
if numbers[j] < numbers[j - 1]:
numbers[j], numbers[j - 1] = numbers[j - 1], numbers[j]
flag = 1
cnt += 1
numbers = map(str, numbers)
print(" ".join(numbers))
print(cnt)
| 1 | 17,184,569,220 | null | 14 | 14 |
s = input().rstrip()
t = input().rstrip()
S = []
T = []
count = 0
for i in s:
S.append(i)
for i in t:
T.append(i)
for i in range(len(S)):
if S[i] != T[i]:
S[i] = T[i]
count += 1
print(count)
|
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def main():
x = int(readline())
if x:
print(0)
else:
print(1)
if __name__ == '__main__':
main()
| 0 | null | 6,733,119,256,958 | 116 | 76 |
from scipy.sparse.csgraph import floyd_warshall
N, M, L = map(int, input().split())
edges = [[0] * N for _ in range(N)]
for _ in range(M):
a, b, c = map(int, input().split())
edges[a - 1][b - 1] = c
edges[b - 1][a - 1] = c
Q = int(input())
st = []
for _ in range(Q):
st.append([int(j) - 1 for j in input().split()])
edges = floyd_warshall(edges)
for i in range(N):
for j in range(N):
if edges[i][j] <= L:
edges[i][j] = 1
else:
edges[i][j] = 0
edges = floyd_warshall(edges)
for i, j in st:
if edges[i][j] == float("inf"):
print(-1)
else:
print(int(edges[i][j]) - 1)
|
import sys
import numpy as np
from scipy.sparse.csgraph import floyd_warshall
input = sys.stdin.buffer.readline
INF = 10**12
N, M, L = map(int, input().split())
G = np.zeros((N, N), dtype=np.int64)
for _ in range(M):
a, b, c = map(int, input().split())
a -= 1
b -= 1
G[a][b] = c
G[b][a] = c
Q = int(input())
ST = [list(map(int, input().split())) for _ in range(Q)]
fw = floyd_warshall(G)
path = np.full((N, N), INF, dtype=np.int64)
path[fw <= L] = 1
path_fw = (floyd_warshall(path) + 0.5).astype(np.int64)
ans = []
for s, t in ST:
s -= 1
t -= 1
if path_fw[s][t] >= INF:
ans.append(-1)
continue
ans.append(path_fw[s][t]-1)
print(*ans, sep="\n")
| 1 | 173,641,460,704,560 | null | 295 | 295 |
import sys
def print_arr(arr):
ln = ''
for v in arr:
ln += str(v) + ' '
ln = ln.strip()
print(ln)
def insertion_sort(arr):
for i in range(1, len(arr)):
print_arr(arr)
v = arr[i]
j = i-1
while j >= 0 and arr[j] > v:
arr[j+1] = arr[j]
j -= 1
arr[j+1] = v
print_arr(arr)
input0 = sys.stdin.readline()
input_sample = sys.stdin.readline()
# input_sample = "5 2 4 6 1 3"
input = input_sample.split(" ")
input = [int(i) for i in input]
insertion_sort(input)
|
while True:
try:
r = sorted(list(map(int, input().split())))
def euclidfunc(a, b):
while b != 0:
a, b = b, a % b
return a
def lcmfunc(a, b):
return a * b // euclidfunc(a, b)
print("{a} {b}".format(a=euclidfunc(r[1], r[0]), b=lcmfunc(r[1], r[0])))
except EOFError:
break
| 0 | null | 3,529,307,008 | 10 | 5 |
x=input()
y=x.swapcase()
print(y)
|
s = input()
t = ""
for i in range(len(s)):
if s[i].islower():
t += s[i].upper()
elif s[i].isupper():
t += s[i].lower()
else:
t += s[i]
print(t)
| 1 | 1,519,847,608,258 | null | 61 | 61 |
from collections import deque
n,q = map(int,input().split())
Q = deque()
for i in range(n):
a = input().split()
Q.append([a[0],int(a[1])])
s = 0
while Q:
qt = Q.popleft()
if qt[1] > q:
Q.append([qt[0],qt[1]-q])
s += q
else:
s += qt[1]
print(qt[0],s)
|
def main():
T = input()
T = T.replace('?', 'D')
# cnt = 0
# cnt_pd = t.count('PD')
# cnt_d = t.count('D')
# cnt = cnt_d + cnt_pd
print(T)
main()
| 0 | null | 9,309,812,547,520 | 19 | 140 |
x = input().split()
for i in range(5):
if x[i] == '0':
print(i+1)
|
import sys
A, B = [int(x) for x in input().split()]
ans_A = 0
ans_B = 0
for i in range(1500):
ans_A = (i*8//100)
ans_B = (i//10)
if(ans_A==A)and(ans_B==B):
print(i)
sys.exit()
print(-1)
| 0 | null | 34,875,286,460,128 | 126 | 203 |
n = int(input())
a = [set() for i in range(n)]
e = []
for i in range(n-1):
b, c = map(int, input().split())
b -= 1
c -= 1
a[b].add(c)
a[c].add(b)
e.append(c)
col = [0 for i in range(n)]
visited = [False for i in range(n)]
visited[0] = True
v = [0]
while v:
d = v.pop(0)
k = 1
for i in a[d]:
if visited[i] == False:
if col[d] == k:
k += 1
col[i] = k
visited[i] = True
v.append(i)
k += 1
print (max(col))
for i in e:
print (col[i])
|
def getval():
n,m,k = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
a = [A[0]]
b = [B[0]]
for i in range(0,n-1):
a.append(a[i]+A[i+1])
for i in range(0,m-1):
b.append(b[i]+B[i+1])
return n,m,k,a,b
def main(n,m,k,a,b):
idxa = 0
ans = 0
t = 0
while a[idxa]<=k and idxa<n:
idxa += 1
if idxa==n:
break
ans = idxa
idxa -= 1
t = a[idxa]
for i in range(m):
t = a[idxa] + b[i]
if t<=k:
ans = max(ans,idxa+i+2)
else:
flag = False
while t>k:
idxa -= 1
if idxa<0:
a.append(0)
break
t = a[idxa]+b[i]
ans = max(ans,idxa+i+2)
print(ans)
if __name__=="__main__":
n,m,k,a,b = getval()
main(n,m,k,a,b)
| 0 | null | 73,007,731,891,954 | 272 | 117 |
n = int(input())
a = map(int, input().split())
re = 0
from collections import Counter
c = Counter(a)
for i, v in c.items():
if v >= 2:
re += 1
if re >= 1:
print("NO")
else:
print("YES")
|
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
a = Counter(a).most_common()
if len(a) == n:
print('YES')
else:
print('NO')
| 1 | 73,889,429,299,258 | null | 222 | 222 |
n,k = map(int, input().split())
a = [0]*n
for i in range(k):
c = int(input())
L = list(map(int, input().split()))
for l in L:
a[l-1] = 1
print(a.count(0))
|
n, k = map(int, input().split())
a = [0] * n
for i in range(k):
d = int(input())
for l in list(map(int, input().split())):
a[l-1] = 1
cnt = 0
for i in range(n):
if a[i] == 0:
cnt += 1
print(cnt)
| 1 | 24,582,878,393,612 | null | 154 | 154 |
import sys
readline = sys.stdin.buffer.readline
n,k = map(int,readline().split())
mod = 10**9+7
def pow(n,p,mod=10**9+7): #繰り返し二乗法(nのp乗)
res = 1
while p > 0:
if p % 2 == 0:
n = n ** 2 % mod
p //= 2
else:
res = res * n % mod
p -= 1
return res % mod
def factrial_memo(n=10**6+1,mod=10**9+7):
fact = [1, 1]
for i in range(2, n + 1):
fact.append((fact[-1] * i) % mod)
return fact
def fermat_cmb(n, r, mod=10**9+7): #needs pow,factrial_memo(only fact). return nCk
return fact[n] * pow(fact[r],mod-2) * pow(fact[n-r],mod-2) %mod
fact = factrial_memo()
if k > n-1: #全通りの再現が可能ならば
ans = fermat_cmb(2*n-1,n)
else: #移動回数の制限によって全通りの再現ができないならば
ans = 1
for i in range(1,k+1): #全ての動作を再現する(kはたかだか2*10**5である)
ans += fermat_cmb(n,i)*fermat_cmb(n-1,i)
#n人の中から動かせる人物をi人取り、n-1箇所(それぞれの人物に付き同じ場所には戻れない)
#にi人を入れる組み合わせ。それぞれの事象に付き、0人になる部屋と他の部屋の人数は
#必ず違う組み合わせになるので、これをk回だけ繰り返せば良い
ans %= mod
print(ans)
|
M=10**9+7;n,k=map(int,input().split());a=c=1
for i in range(1,min(n,k+1)):m=n-i;c=c*-~m*m*pow(i,M-2,M)**2%M;a+=c
print(a%M)
| 1 | 67,284,107,662,112 | null | 215 | 215 |
n,*A=map(int,open(0).read().split());m=1000
for x,y in zip(A,A[1:]):
if x<y:m=m//x*y+m%x
print(m)
|
def generate_G(n):
h = 1
G = []
while h <= n:
G.append(h)
h = 3 * h + 1
G.reverse()
return G
def insertion_sort(A, n, g):
global count
for i in range(g, n):
v = A[i]
j = i - g
while j >= 0 and A[j] > v:
A[j + g] = A[j]
j -= g
count += 1
A[j + g] = v
def shell_sort(A, n):
G = generate_G(n)
print(len(G))
print(' '.join(map(str, G)))
for g in G:
insertion_sort(A, n, g)
def main():
global count
# input
n = int(input())
A = [int(input()) for _ in range(n)]
# sort
shell_sort(A, n)
# output
print(count)
for a in A:
print(a)
if __name__ == "__main__":
count = 0
main()
| 0 | null | 3,707,710,836,310 | 103 | 17 |
N=int(input())
S=input()
ans=0
for i in range(N-2):
if S[i]=='A':
i+=1
if S[i]=='B':
i+=1
if S[i]=='C':
ans+=1
print(ans)
|
S=input()
Q=int(input())
rev = 0
front, back=[],[]
for i in range(Q):
q = input()
if len(q)==1:rev = 1- rev
else:
n,f,c=q.split()
if f=='1' and rev==0:
front.append(c)
elif f=='2' and rev==1:
front.append(c)
elif f=='1' and rev==1:
back.append(c)
elif f=='2' and rev == 0:
back.append(c)
if rev==0:
S = ''.join(front[::-1]) + S + ''.join(back)
else:
S = ''.join(back[::-1]) + S[::-1] + ''.join(front)
print(S)
| 0 | null | 78,303,915,133,080 | 245 | 204 |
from decimal import Decimal
a,b,c=map(Decimal,input().split())
print('Yes' if a.sqrt()+b.sqrt()<c.sqrt() else 'No')
|
while True:
try:
a,b = map(int,input().split(" "))
print(len(str(a+b)))
except: break
| 0 | null | 25,743,017,344,538 | 197 | 3 |
def solve():
c = input()
print(chr(ord(c)+1))
if __name__ == '__main__':
solve()
|
a=input()
num=[]
for x in range(int(a)+1):
if x!=0 and x%3==0 or x%10==3 or "3" in str(x):
num.append(x)
str_list=map(str,num)
print(" "+" ".join(str_list))
| 0 | null | 46,632,337,258,352 | 239 | 52 |
import heapq
import time
st = time.time()
n = int(input())
a = list(map(int,input().split()))
hq = []
for i, ai in enumerate(a):
heapq.heappush(hq,(-ai,i))
dp = [[0 for i in range(j+1)] for j in range(n+1)]
dp[0][0] = 0
ai,hi = heapq.heappop(hq)
dp[1][0] = -ai*(n-1-hi)
dp[1][1] = -ai*hi
for i in range(2,n+1):
ai,hi = heapq.heappop(hq)
dp[i][0] = dp[i-1][0] - ai*abs(n-i-hi)
dp[i][i] = dp[i-1][i-1] - ai*abs(hi-i+1)
for j in range(1,i):
dp[i][j] = max(dp[i-1][j-1]-ai*abs(hi-j+1),dp[i-1][j]-ai*abs(n-i+j-hi))
print(max(dp[n]))
|
# 素因数分解
def prime_factors(n):
i = 2
while i * i <= n:
if n % i:
i += 1
else:
n //= i
yield i
if n > 1:
yield n
from itertools import count
X = int(input())
for x in count(X):
if len(tuple(prime_factors(x))) == 1:
print(x)
break
| 0 | null | 69,681,113,995,962 | 171 | 250 |
N = input()
K = int(input())
l = len(N)
dp = [[[0]*(K+1) for _ in range(2)] for _ in range(l)]
dp[0][0][1] = 1
dp[0][1][1] = int(N[0]) - 1
dp[0][1][0] = 1
#print(dp[0])
for i in range(1,l):
for j in range(K+1):
if int(N[i]) == 0:
if j > 0:
dp[i][0][j] = dp[i-1][0][j]
dp[i][1][j] += 9*dp[i-1][1][j-1]
else:
if j > 0:
dp[i][0][j] = dp[i-1][0][j-1]
dp[i][1][j] += dp[i-1][0][j-1]*(int(N[i])-1) + 9*dp[i-1][1][j-1]
dp[i][1][j] += dp[i-1][0][j]
dp[i][1][j] += dp[i-1][1][j]
#print(dp[i])
print(dp[l-1][0][K]+dp[l-1][1][K])
|
X, N = map(int, input().split())
ps = list(map(int, input().split()))
bgr, smr = X, X
while bgr <= 100:
if bgr not in ps:
break
bgr += 1
while smr >= 1:
if smr not in ps:
break
smr -= 1
print(bgr) if bgr - X < X - smr else print(smr)
| 0 | null | 44,710,391,295,860 | 224 | 128 |
N=int(input())
A=list(map(int, input().split()))
S=M=sum(A)
cnt=0
for a in A:
cnt+=a
M=min(M, abs(S-2*cnt))
print(M)
|
import math
n = int(input())
ans = 0
if n== 0:
print(0)
exit()
if n%2 == 1:
print(0)
else:
for j in range(1,100):
if n//((5**j)*2) == 0:
break
ans += n//((5**j)*2)
print(ans)
| 0 | null | 129,361,672,542,048 | 276 | 258 |
from decimal import Decimal
A,B=input().split()
A=int(A)
B=Decimal(B)
Bint=int(B)
B1=int((B-Bint)*100)
result=A*Bint+A*B1//100
print(result)
|
a=[]
for i in range(3):
a.append(list(map(int, input().split())))
n=int(input())
b=[]
for i in range(n):
b.append(int(input()))
for bingo in b:
for gyou in range(3):
for retu in range(3):
if a[gyou][retu]==bingo:
a[gyou][retu]=True
if a[0][0]==True and a[0][1]==True and a[0][2]==True:
print("Yes")
exit()
elif a[1][0]==True and a[1][1]==True and a[1][2]==True:
print("Yes")
exit()
elif a[2][0]==True and a[2][1]==True and a[2][2]==True:
print("Yes")
exit()
elif a[0][0]==True and a[1][0]==True and a[2][0]==True:
print("Yes")
exit()
elif a[0][1]==True and a[1][1]==True and a[2][1]==True:
print("Yes")
exit()
elif a[0][2]==True and a[1][2]==True and a[2][2]==True:
print("Yes")
exit()
elif a[0][0]==True and a[1][1]==True and a[2][2]==True:
print("Yes")
exit()
elif a[0][2]==True and a[1][1]==True and a[2][0]==True:
print("Yes")
exit()
print("No")
| 0 | null | 38,062,185,306,222 | 135 | 207 |
n = int(input())
a = list(map(int,input().split()))
ans = [0 for i in range(n)]
for i in a:
ans[i-1] += 1
for i in ans:
print(i)
|
N = int(input())
matrix = [[0,0,0,0,0,0,0,0,0,0] for _ in range(10)]
for i in range(1,N+1):
first = int(str(i)[0])
last = int(str(i)[-1])
matrix[first][last] += 1
ans = 0
for i in range(10):
for l in range(10):
ans += matrix[i][l]*matrix[l][i]
print(ans)
| 0 | null | 59,624,414,134,400 | 169 | 234 |
a,b,c=map(int, input().split())
A=(b*c)
if A>a:
print('Yes')
elif A==a:
print('Yes')
else:
print('No')
|
def main2():
K = int(input())
rem = set()
n = ans = 0
while True:
n = n * 10 + 7
ans += 1
if n % K == 0:
print(ans)
break
else:
n = n % K
if n in rem:
print(-1)
break
else:
rem.add(n)
if __name__ == "__main__":
main2()
| 0 | null | 4,835,889,672,488 | 81 | 97 |
n = int(input())
A = list(map(int, input().split()))
dp = [0] * (n+1)
dp[2] = max(A[0], A[1])
s = A[0]
for i, a in enumerate(A, 1):
if i <= 2:
continue
if i%2: # 奇数
dp[i] = max(dp[i-1], a+dp[i-2])
s += a
else: # 偶数
dp[i] = max(a+dp[i-2], s)
print(dp[-1])
|
n=int(input())
s=100000
for i in range(n):
s=s*1.05
if s%1000==0:
s=s
else:
s=(s//1000)*1000+1000
print(int(s))
| 0 | null | 18,781,311,689,952 | 177 | 6 |
if __name__ == '__main__':
for i in range(1,10):
for j in range(1,10):
print(str(i)+"x"+str(j)+"="+str(i*j))
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
???????????????
"""
inputstr = input().strip()
cmdnum = int(input().strip())
for i in range(cmdnum) :
cmd = input().strip().split()
if cmd[0] == "print":
start = int(cmd[1])
end = int(cmd[2]) + 1
print(inputstr[start:end])
if cmd[0] == "reverse":
start = int(cmd[1])
end = int(cmd[2]) + 1
prev = inputstr[:start]
revr = inputstr[start:end]
next = inputstr[end:]
inputstr = prev + revr[::-1] + next
if cmd[0] == "replace":
start = int(cmd[1])
end = int(cmd[2]) + 1
inputstr = inputstr[:start] + cmd[3] + inputstr[end:]
| 0 | null | 1,030,913,944,972 | 1 | 68 |
n,k=map(int,input().split())
m=n%k
print(min(m,abs(m-k)))
|
N, K = map(int, input().split())
A = N % K
B = K - A
print(min(A, B))
| 1 | 39,482,229,216,652 | null | 180 | 180 |
#!/usr/bin/env python3
import sys
from collections import defaultdict
sys.setrecursionlimit(10**6)
INF = 10 ** 9 + 1 # sys.maxsize # float("inf")
MOD = 10 ** 9 + 7
def debug(*x):
print(*x, file=sys.stderr)
def solve(N, K, PS, CS):
PS = [x - 1 for x in PS]
CS = [CS[PS[i]] for i in range(N)]
visited = {}
loops = []
loopScore = []
for i in range(N):
loop = []
c = 0
while i not in visited:
visited[i] = True
c += CS[i]
i = PS[i]
loop.append(i)
if loop:
loops.append(loop)
loopScore.append(c)
pos = list(range(N))
ret = -INF
for i, loop in enumerate(loops):
if loopScore[i] > 0:
baseScore = loopScore[i] * (K // len(loop))
r = K % len(loop)
if r == 0:
r = len(loop)
baseScore -= loopScore[i]
maxscore = 0
scores = defaultdict(int)
for i in range(r):
for x in loop:
scores[x] += CS[pos[x]]
pos[x] = PS[pos[x]]
maxscore = max(maxscore, max(scores.values()))
ret = max(maxscore + baseScore, ret)
else:
r = len(loop)
maxscore = -INF
scores = defaultdict(int)
for i in range(r):
for x in loop:
scores[x] += CS[pos[x]]
pos[x] = PS[pos[x]]
maxscore = max(maxscore, max(scores.values()))
ret = max(maxscore, ret)
return ret
def main():
# parse input
N, K = map(int, input().split())
PS = list(map(int, input().split()))
CS = list(map(int, input().split()))
print(solve(N, K, PS, CS))
# tests
T1 = """
5 2
2 4 5 1 3
3 4 -10 -8 8
"""
TEST_T1 = """
>>> as_input(T1)
>>> main()
8
"""
T2 = """
2 3
2 1
10 -7
"""
TEST_T2 = """
>>> as_input(T2)
>>> main()
13
"""
T3 = """
3 3
3 1 2
-1000 -2000 -3000
"""
TEST_T3 = """
>>> as_input(T3)
>>> main()
-1000
"""
T4 = """
10 58
9 1 6 7 8 4 3 2 10 5
695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719
"""
TEST_T4 = """
>>> as_input(T4)
>>> main()
29507023469
"""
T5 = """
3 1000
2 3 1
1 0 2
"""
TEST_T5 = """
>>> as_input(T5)
>>> main()
1001
"""
T6 = """
3 1000
2 3 1
1 1 -3
"""
TEST_T6 = """
>>> as_input(T6)
>>> main()
2
"""
T7 = """
4 1000
2 1 4 3
1 1 -10000 10000
"""
TEST_T7 = """
>>> as_input(T7)
>>> main()
10000
"""
T8 = """
4 1000
2 1 4 3
1 1 -10000 10001
"""
TEST_T8 = """
>>> as_input(T8)
>>> main()
10500
"""
def _test():
import doctest
doctest.testmod()
g = globals()
for k in sorted(g):
if k.startswith("TEST_"):
doctest.run_docstring_examples(g[k], g, name=k)
def as_input(s):
"use in test, use given string as input file"
import io
f = io.StringIO(s.strip())
g = globals()
g["input"] = lambda: bytes(f.readline(), "ascii")
g["read"] = lambda: bytes(f.read(), "ascii")
input = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
if sys.argv[-1] == "-t":
print("testing")
_test()
sys.exit()
main()
|
import math
import collections
import copy
import sys
n = int(input())
a = list(map(int,input().split()))
limit = 10 ** 6
sCheck = math.gcd(a[0],a[1])
if n == 2:
if sCheck == 1:
print("pairwise coprime")
else:
print("not coprime")
sys.exit()
else:
for i in range(2,n):
sCheck = math.gcd(sCheck,a[i])
beforeQ = collections.deque([int(i) for i in range(3,(limit + 1))])
D = dict()
p = 2
D[2] = 2
D[1] = 1
while p < math.sqrt(limit):
afterQ = collections.deque()
while len(beforeQ) > 0:
k = beforeQ.popleft()
if k % p != 0:
afterQ.append(k)
else:
D[k] = p
beforeQ = copy.copy(afterQ)
p = beforeQ.popleft()
D[p] = p
while len(beforeQ) > 0:
k = beforeQ.popleft()
D[k] = k
ansSet = set()
count = 0
for i in a:
k = i
kSet = set()
while k != 1:
if D[k] in ansSet:
if not(D[k] in kSet):
count = 1
break
else:
ansSet.add(D[k])
kSet.add(D[k])
k = int(k // D[k])
if count == 1:
break
if count == 0:
print("pairwise coprime")
elif sCheck == 1:
print("setwise coprime")
else:
print("not coprime")
| 0 | null | 4,781,433,075,456 | 93 | 85 |
n=int(input())
if n%10==7:
print("Yes")
exit()
n//=10
if n%10==7:
print("Yes")
exit()
n//=10
if n%10==7:
print("Yes")
exit()
print("No")
|
#!/usr/bin/env python3
import math
x = int(input())
#aもbも0以上のとき
border = 1;
while border ** 5 - (border - 1) ** 5 < x:
border += 1
for i in range(border + 1):
aa = ((x + i ** 5) ** (1.0 / 5.0)) #仮
a = round(aa)
if a ** 5 - i ** 5 == x:
b = i
print(a, b)
break
else:
b = -1
while x + b ** 5 >= 0:
aa = ((x + b ** 5) ** (1.0 / 5.0)) # 仮
a = round(aa)
if a ** 5 - b ** 5 == x:
print(a, b)
break
b -= 1
| 0 | null | 29,991,636,707,622 | 172 | 156 |
#coding:utf-8
import sys,os
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
from math import gcd
def main(given=sys.stdin.readline):
input = lambda: given().rstrip()
LMIIS = lambda: list(map(int,input().split()))
II = lambda: int(input())
XLMIIS = lambda x: [LMIIS() for _ in range(x)]
YN = lambda c : print('Yes') if c else print('No')
MOD = 10**9+7
A,B = LMIIS()
print(A*B//gcd(A,B))
if __name__ == '__main__':
main()
|
import numpy as np
N=int(input())
A=input().split()
n=np.zeros(N)
for i in range(N-1):
n[int(A[i])-1]+=1
for l in n :
print(int(l))
| 0 | null | 72,937,781,557,268 | 256 | 169 |
n = int(input())
a = list(map(int, input().split()))
def bubbleSort(a, n):
flag = 1
i = 0
count = 0
while flag:
flag = 0
for j in range(n-1, i, -1):
if a[j] < a[j-1]:
a[j],a[j-1] = a[j-1],a[j]
flag = 1
count += 1
i += 1
print(*a)
print(count)
bubbleSort(a,n)
|
def bubble_sort(a, n):
sw_num = 0
is_swapped = True
i = 0
while is_swapped:
is_swapped = False
for j in range(n-1, i, -1):
if a[j] < a[j-1]:
tmp = a[j]
a[j] = a[j-1]
a[j-1] = tmp
is_swapped = True
sw_num += 1
i += 1
return sw_num
n = int(input())
a = [int(i) for i in input().split()]
sw_num = bubble_sort(a, n)
print(' '.join([str(i) for i in a]))
print(sw_num)
| 1 | 18,363,709,920 | null | 14 | 14 |
n = int(input())
m = list(map(int, input().split()))
count = 0
for i in range(n-1):
minj =i
for j in range(i+1, n):
if m[j] < m[minj]:
minj = j
if m[i] != m[minj]:
m[i], m[minj] = m[minj], m[i]
count += 1
print(" ".join(str(x) for x in m))
print(count)
|
def sel_sort(A, N):
''' 選択ソート '''
count = 0
for n in range(N):
minm = n
for m in range(n, N):
if A[m] < A[minm]:
minm = m
if minm != n:
A[n], A[minm] = A[minm], A[n]
count += 1
return (A, count)
if __name__ == '__main__':
N = int(input())
A = list(map(int, input().split(' ')))
ans = sel_sort(A, N)
print(' '.join([str(x) for x in ans[0]]))
print(ans[1])
| 1 | 19,847,971,910 | null | 15 | 15 |
mozi = input()
kazu = int(ord(mozi))
if 65 <= kazu < 91:
print('A')
else:
print('a')
|
N, S = map(int, input().split())
MOD = 998244353
A = list(map(int, input().split()))
dp = [0]*(S+1)
dp[0] = 1
for a in A:
for s in reversed(range(S+1)):
if s+a<=S: dp[s+a] += dp[s]
dp[s] *= 2
dp[s] %= MOD
print(dp[S]%MOD)
| 0 | null | 14,567,007,676,550 | 119 | 138 |
def main():
N = int(input())
A = list(map(int,input().split()))
count = 0
for i in range(N):
minj = i
for j in range(i,N):
if A[j] < A[minj]:
minj = j
count += (i != minj)
A[i], A[minj] = A[minj], A[i]
print(" ".join(map(str,A)))
print(count)
if __name__ == "__main__":
import os
import sys
if len(sys.argv) > 1:
if sys.argv[1] == "-d":
fd = os.open("input.txt", os.O_RDONLY)
os.dup2(fd, sys.stdin.fileno())
main()
else:
main()
|
def print_arr(arr):
arr_str = ''
for i in range(len(arr)):
arr_str += str(arr[i])
if i != len(arr) - 1:
arr_str += ' '
print(arr_str)
n = int(input())
arr = list(map(int, input().split()))
swap_count = 0
for i in range(n):
mini = i
for j in range(i, n):
if arr[j] < arr[mini]:
mini = j
if mini != i:
temp = arr[i]
arr[i] = arr[mini]
arr[mini] = temp
swap_count += 1
print_arr(arr)
print(swap_count)
| 1 | 19,863,937,660 | null | 15 | 15 |
def resolve():
print("Yes" if input() == input()[:-1] else "No")
if '__main__' == __name__:
resolve()
|
n,m,k=map(int,input().split())
par = [-1]*n
num = [0]*n
def find(x):
if par[x]<0:
return x
else:
par[x] = find(par[x])
return par[x]
def union(x, y):
p,q=find(x), find(y)
if p==q:
return
if p>q:
p,q = q,p
par[p] += par[q]
par[q] = p
def size(x):
return -par[find(x)]
def same(x,y):
return find(x)==find(y)
for i in range(m):
a,b=map(int,input().split())
a-=1
b-=1
union(a,b)
num[a]+=1
num[b]+=1
for i in range(k):
c,d=map(int,input().split())
c-=1
d-=1
if same(c,d):
num[c]+=1
num[d]+=1
for i in range(n):
print(size(i)-1-num[i], end=" ")
| 0 | null | 41,647,851,819,380 | 147 | 209 |
import collections
n,q = map(int, input().split())
queue = collections.deque(maxlen=100000)
for i in range(n):
p,t = input().split()
queue.append([p, int(t)])
time = 0
while queue:
p,t = queue.popleft()
if t<=q:
time += t
print(p,time)
else:
time += q
queue.append([p,t-q])
|
n, time=map(int,raw_input().split())
p = []
t = []
for _ in xrange(n):
a = raw_input().split()
p.append(a[0])
t.append(int(a[1]))
alltime = 0
while True:
if len(p)==0:
break
if t[0]<=time:
alltime+=t[0]
print p.pop(0),
t.pop(0)
print alltime
else:
t.append(t[0])
t.pop(0)
t[-1]-=time
alltime += time
p.append(p[0])
p.pop(0)
| 1 | 40,360,931,820 | null | 19 | 19 |
from collections import deque
n, m = [int(w) for w in input().split()]
links = [[] for i in range(n + 1)]
mark = [-1] * (n + 1)
for i in range(m):
a, b = [int(w) for w in input().split()]
links[a].append(b)
links[b].append(a)
q = deque([1])
while q:
now = q.popleft()
for l in links[now]:
if mark[l] == -1:
q.append(l)
mark[l] = now
if -1 in mark[2:]:
print("No")
else:
print("Yes")
print("\n".join([str(w) for w in mark[2:]]))
|
N,A,B=map(int,input().split())
if (B-A)%2==0:
print((B-A)//2)
else:
if A-1<=N-B:
#Aが1に到達するまで+AとBの距離が2の倍数になるまで
x=A-1+1
#Aは負け続け、Bは勝ち続ける
y=(B-A)//2
print(x+y)
else:
#BがNに到達するまで+AとBの距離が2の倍数になるまで
x=N-B+1
#Aは勝ち続け、Bは負け続ける
y=(B-A)//2
print(x+y)
| 0 | null | 64,811,940,533,640 | 145 | 253 |
if __name__ == "__main__":
s = input()
print(s[0:3])
|
def main():
N = int(input())
m = float("inf")
ans = 0
for i in [int(j) for j in input().split()]:
if i < m:
m = i
ans += 1
print(ans)
if __name__ == '__main__':
main()
| 0 | null | 50,072,483,478,358 | 130 | 233 |
A, B, C = map(int, input().split())
l = [A, B, C]
if len(set(l)) == 2:
print("Yes")
else:
print("No")
|
a = list(map(int, input().split()))
if a[0] == a[1] and a[0] != a[2]:
print("Yes")
elif a[0] != a[1] and a[1] == a[2]:
print("Yes")
elif a[0] == a[2] and a[0] != a[1]:
print("Yes")
else:
print("No")
| 1 | 68,147,354,861,850 | null | 216 | 216 |
x, k, d = map(int, input().split())
x = -x if x <= 0 else x
if x - d * k >= 0:
print(x - d * k)
else:
a = x // d
b = a + 1
rest_cnt = k - a
if rest_cnt % 2 == 0:
print(abs(x - d * a))
else:
print(abs(x - d * b))
|
X, K, D = map(int, input().split())
X = abs(X)
if K%2 == 1:
K -= 1
X -= D
K_ = K // 2
min_Y = X - K_ * (2 * D)
if min_Y > 0:
print(min_Y)
else:
min_Y_p = X % (2 * D)
min_Y_m = 2 * D - X % (2 * D)
print(min(min_Y_p, min_Y_m ))
| 1 | 5,165,831,089,890 | null | 92 | 92 |
import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline
#sys.setrecursionlimit(10**8)
def geta(fn=lambda s: s.decode()):
return map(fn, readline().split())
def gete(fn=lambda s: s.decode()):
return fn(readline().rstrip())
def main():
h = gete(int)
w = gete(int)
n = gete(int)
if h < w:
h, w = w, h
print((h + n - 1) // h)
if __name__ == "__main__":
main()
|
l=int(input())
temp1=l/3
temp2=(l-l/3)/2
print(temp1*temp2*(l-temp1-temp2))
| 0 | null | 67,981,460,201,212 | 236 | 191 |
s=input()
q=int(input())
cnt=0
front=[]
rear=[]
for qq in range(q):
l=list(input().split())
if l[0]=='1':
cnt+=1
else:
if (cnt+int(l[1]))%2==1:
front.append(l[2])
else:
rear.append(l[2])
front=''.join(front[::-1])
rear=''.join(rear)
s=front+s+rear
print(s if cnt%2==0 else s[::-1])
|
S = input()
if S == "hi" * (len(S)//2):
print("Yes")
else:
print("No")
| 0 | null | 55,158,113,581,692 | 204 | 199 |
Nin = int(input())
noguchi = (Nin // 1000)
if (Nin % 1000):
noguchi += 1
print(noguchi*1000 - Nin)
|
import sys
def input(): return sys.stdin.readline().rstrip()
N = int(input())
num = (N + 1000 - 1 )// 1000
print(num * 1000 - N)
| 1 | 8,395,689,311,702 | null | 108 | 108 |
n = int(input())
list = [i for i in input().split()]
list.reverse()
print(" ".join(list))
|
n = int(input())
s = input().split()
s.reverse()
print(' '.join(s))
| 1 | 982,258,213,420 | null | 53 | 53 |
from collections import deque
N=int(input())
d=deque()
d.append("a")
dall=deque()
dall.append(d)
i=0
while i<N-1:
length=len(dall)
j=0
while j<length:
l=dall.popleft()
llen=len(set(l))
for k in range(llen+1):
q = deque()
for m in range(len(l)):
c = l.popleft()
q.append(c)
l.append(c)
q.append(chr(ord("a")+k))
dall.append(q)
#print(dall)
#input()
j=j+1
i=i+1
#print(dall)
#input()
#print(len(dall))
ans=[]
for i in range(len(dall)):
ans.append("".join(dall[i]))
ans.sort()
for i in range(len(ans)):
print(ans[i])
|
import math
n = int(input())
x = ["a"]
alps = "abcdefghijk"
i = 1
while i < n:
y = []
for t in x:
for s in alps[:len(set(t)) + 1]:
y.append(t + s)
x = y
i += 1
print("\n".join(x))
| 1 | 52,717,004,973,248 | null | 198 | 198 |
import numpy as np
n=int(input())
a=np.array(list(map(int,input().split())))
mod=10**9+7
s=0
for i in range(60):
bit = np.count_nonzero(a & 1)
s += bit*(n-bit)*(2**i)
a >>= 1
print(s % mod)
|
# F - Strivore
K = int(input())
S = list(str(input()))
N = len(S)
MOD = 10**9+7
fac = [1, 1]
inv = [0, 1]
finv = [1, 1]
for i in range(2, N+K+1):
fac.append(fac[-1] * i % MOD)
inv.append(MOD - inv[MOD%i] * (MOD//i) % MOD)
finv.append(finv[-1] * inv[-1] % MOD)
def comb_mod(n, r, m):
if (n<0 or r<0 or n<r): return 0
r = min(r, n-r)
return fac[n] * finv[n-r] * finv[r] % m
ans = 0
for i in range(K+1):
tmp = pow(25, i, MOD)
tmp *= comb_mod(i+N-1, N-1, MOD)
tmp *= pow(26, K-i, MOD)
ans += tmp
ans %= MOD
print(ans)
| 0 | null | 68,098,516,335,782 | 263 | 124 |
from collections import defaultdict
G = defaultdict(set)
N, M = [int(_) for _ in input().split()]
for _ in range(M):
A, B = [int(_) for _ in input().split()]
G[A].add(B)
G[B].add(A)
f = [-1 for _ in range(N+1)]
f[1] = 0
pool = set([1])
np = set()
cnt = 0
while pool:
np = set()
for p in pool:
for v in G[p]:
if f[v] == -1:
f[v] = p
cnt += 1
np.add(v)
pool = np
print("Yes")
for i in range(2, N+1):
print(f[i])
|
n, m = map(int, input().split())
h = list(map(int, input().split()))
roads = []
for i in range(n): roads.append([])
is_good_peak_arr = [True] * n
for i in range(m):
a, b = map(int, input().split())
a, b = a-1, b-1
if h[a] >= h[b]: is_good_peak_arr[b] = False
if h[b] >= h[a]: is_good_peak_arr[a] = False
print(is_good_peak_arr.count(True))
| 0 | null | 22,872,524,933,500 | 145 | 155 |
N = input()
SUM = 0
for i in N:
SUM += int(i)
if SUM % 9 == 0:
print('Yes')
else :
print('No')
|
K = int(input())
S = input()
mod = 10**9 + 7
class Combination:
def __init__(self, n):
self.facts = [1 for i in range(n+1)]
self.invs = [1 for i in range(n+1)]
for i in range(1, n+1):
self.facts[i] = self.facts[i-1] * i % mod
self.invs[i] = pow(self.facts[i], mod-2, mod)
def ncr(self, n, r):
if n < r:
return 0
if n < 0 or r < 0:
return 0
else:
return self.facts[n] * self.invs[r] * self.invs[n-r] % mod
def nhr(self, n, r):
if n < r:
return 0
if n < 0 or r < 0:
return 0
else:
return self.ncr(n+r-1, n-1)
N = K+len(S)
comb = Combination(K+len(S))
ans = 0
for i in range(K+1):
ans = (ans + comb.ncr(N-i-1, len(S)-1) * pow(25, N-i-len(S), mod) * pow(26, i, mod)) % mod
print(ans)
| 0 | null | 8,674,700,894,468 | 87 | 124 |
a = input('')
S = []
num = 0
for i in a:
S.append(i)
for j in range(3):
if S[j] == 'R':
num += 1
if num == 2 and S[1] == 'S':
print(1)
else:
print(num)
|
s = input().split('S')
m = 0
for i in s:
if len(i) > m:
m = len(i)
print(m)
| 1 | 4,865,212,600,260 | null | 90 | 90 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.