code1
stringlengths 17
427k
| code2
stringlengths 17
427k
| similar
int64 0
1
| pair_id
int64 2
181,637B
⌀ | question_pair_id
float64 3.7M
180,677B
⌀ | code1_group
int64 1
299
| code2_group
int64 1
299
|
---|---|---|---|---|---|---|
import random
import numpy as np
D=int(input())
C=np.array(list(map(int,input().split())))
S=[]
for _ in range(D):
S.append(list(map(int,input().split())))
l=np.zeros(26,int)
def decrease(d):
return np.dot(C,(d-l))
def pick(i,l):
diff=[]
ll=l.copy()
for j in range(26):
ll[j]=i
diff.append(S[i][j]-decrease(i))
return np.argmax(diff)+1
for i in range(D):
p=pick(i,l)
print(p)
l[p-1]=i+1 | from copy import copy
import random
import math
import sys
input = sys.stdin.readline
D = int(input())
c = list(map(int,input().split()))
s = [list(map(int,input().split())) for _ in range(D)]
last = [0]*26
ans = [0]*D
score = 0
for i in range(D):
ps = [0]*26
for j in range(26):
pl = copy(last)
pl[j] = i+1
ps[j] += s[i][j]
for k in range(26):
ps[j] -= c[k]*(i+1-pl[k])
idx = ps.index(max(ps))
last[idx] = i+1
ans[i] = idx+1
score += max(ps)
for k in range(1,40001):
na = copy(ans)
x = random.randint(1,365)
y = random.randint(1,365)
na[x-1] = na[y-1]
last = [0]*26
ns = 0
for i in range(D):
last[na[i]-1] = i+1
ns += s[i][na[i]-1]
for j in range(26):
ns -= c[j]*(i+1-last[j])
if k%100 == 1:
T = 80-(79*k/40000)
p = pow(math.e,-abs(ns-score)/T)
if ns > score or random.random() < p:
ans = na
score = ns
for a in ans:
print(a) | 1 | 9,676,315,521,002 | null | 113 | 113 |
a=input()
l=a.split(" ")
n=int(l[0])
k=int(l[1])
f=input()
fruits=f.split(" ")
for d in range(0, len(fruits)):
fruits[d]=int(fruits[d])
ans=0
b=0
while(b<k):
g=min(fruits)
ans+=g
fruits.remove(g)
b+=1
print(ans) | N,K=input().split()
N=int(N)
K=int(K)
a=[]
a=[int(a) for a in input().split()]
a.sort()
print(sum(a[0:K])) | 1 | 11,713,683,421,392 | null | 120 | 120 |
def resolve():
H, W, M = map(int, input().split())
pos = []
for _ in range(M):
pos.append(list(map(int, input().split())))
h_totals = [0] * (H+1)
w_totals = [0] * (W+1)
overlap = [set() for _ in range(H+1)]
for ph, pw in pos:
h_totals[ph] += 1
w_totals[pw] += 1
overlap[ph].add(pw)
max_h = -1
max_w = -1
h_list = []
w_list = []
for i in range(1, H+1):
if h_totals[i] > max_h:
max_h = h_totals[i]
h_list = [i]
elif h_totals[i] == max_h:
h_list.append(i)
for j in range(1, W+1):
if w_totals[j] > max_w:
max_w = w_totals[j]
w_list = [j]
elif w_totals[j] == max_w:
w_list.append(j)
for i in h_list:
for j in w_list:
if j not in overlap[i]:
print(max_h + max_w)
return
print(max_h + max_w - 1)
if __name__ == "__main__":
resolve() | H, W, M = map(int, input().split())
X = [0 for i in range(H)]
Y = [0 for j in range(W)]
s = set([])
for i in range(M):
h, w = map(int, input().split())
X[h-1] += 1
Y[w-1] += 1
s.add((h-1, w-1)) #タプルを使う
mX = max(X)
mY = max(Y)
I = [i for i in range(H) if X[i] == mX]
J = [j for j in range(W) if Y[j] == mY]
flag = 0
for i in I:
for j in J:
if {(i, j)} <= s:
continue
flag = 1
print(mX+mY)
break
if flag == 1:
break
if flag == 0:
print(mX+mY-1) | 1 | 4,728,310,083,876 | null | 89 | 89 |
N = int(input())
def dfs(s, mx):
if (len(s) == N):
print(s)
return
for i in range(mx + 1):
dfs(s + chr(97 + i), mx + (i == mx))
dfs('', 0)
| nag = int(input())
def dfs(rootlist,deep,n):
global nag
if len(rootlist) != nag:
for i in range(n+1):
nextroot = rootlist[:]
nextroot.append(i)
if i < n:
dfs(nextroot,deep+1,n)
else:
dfs(nextroot,deep+1,n+1)
else:
answer = ''
wordlist = ['a','b','c','d','e','f','g','h','i','j']
for index in rootlist:
answer += wordlist[index]
print(answer)
dfs([0],0,1)
| 1 | 52,187,503,227,130 | null | 198 | 198 |
import itertools
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
l = []
for i in range(1, n+1):
l.append(i)
m = []
for j in itertools.permutations(l, n):
m.append(list(j))
for a in range(9*8*7*6*5*4*3*2):
if p == m[a]:
x = a
break
for b in range(9*8*7*6*5*4*3*2):
if q == m[b]:
y = b
break
print(abs(x-y)) | #!/usr/bin/env python3
import math
x = int(input())
ans = 360//math.gcd(360, x)
print(ans)
| 0 | null | 56,555,160,312,908 | 246 | 125 |
# get line input split by space
def getLnInput():
return input().split()
# ceil(a / b) for a > b
def ceilDivision(a, b):
return (a - 1) // b + 1
def main():
getLnInput()
nums = list(map(int, getLnInput()))
print(min(nums), max(nums), sum(nums))
return
main() | n = input()
a = list(map(int, raw_input().split(" ")))
print("%d %d %d" % (min(a), max(a), sum(a))) | 1 | 732,992,643,688 | null | 48 | 48 |
import itertools
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
w = 0
x = 0
S = []
T = []
for i in range(N):
S.append(i+1)
for j in itertools.permutations(S):
T.append(list(j))
a = T.index(P)
b = T.index(Q)
print(abs(a-b))
| n,m = map(int,input().split())
c = list(map(int,input().split()))
minimum = [50000] * (n+1)
minimum[0] = 0
for i in range(1,n+1):
for j in range(m):
if c[j]<=i and minimum[i-c[j]] + 1 < minimum[i]:
minimum[i] = minimum[i-c[j]]+1
print(minimum[n]) | 0 | null | 50,433,487,289,120 | 246 | 28 |
import sys
k1 = [[0 for i in xrange(10)] for j in xrange(3)]
k2 = [[0 for i in xrange(10)] for j in xrange(3)]
k3 = [[0 for i in xrange(10)] for j in xrange(3)]
k4 = [[0 for i in xrange(10)] for j in xrange(3)]
n = input()
for i in xrange(n):
m = map(int,raw_input().split())
if m[0] == 1:
k1[m[1]-1][m[2]-1] = k1[m[1]-1][m[2]-1] + m[3]
elif m[0] == 2:
k2[m[1]-1][m[2]-1] = k2[m[1]-1][m[2]-1] + m[3]
elif m[0] == 3:
k3[m[1]-1][m[2]-1] = k3[m[1]-1][m[2]-1] + m[3]
elif m[0] == 4:
k4[m[1]-1][m[2]-1] = k4[m[1]-1][m[2]-1] + m[3]
for i in xrange(3):
for j in xrange(10):
x = ' ' + str(k1[i][j])
sys.stdout.write(x)
if j == 9:
print ""
print"#"*20
for i in xrange(3):
for j in xrange(10):
y = ' ' + str(k2[i][j])
sys.stdout.write(y)
if j == 9:
print ""
print"#"*20
for i in xrange(3):
for j in xrange(10):
z = ' ' + str(k3[i][j])
sys.stdout.write(z)
if j == 9:
print ""
print"#"*20
for i in xrange(3):
for j in xrange(10):
zz = ' ' + str(k4[i][j])
sys.stdout.write(zz)
if j == 9:
print "" | # -*- coding: utf-8 -*-
n = int(input())
Univ = [[[0 for i in range(10)]for j in range(3)]for k in range(4)]
for i in range(n):
b,f,r,v =[int(i)for i in input().split(' ')]
Univ[b-1][f-1][r-1] += v
for b in range(4):
for f in range(3):
for r in range(10):
print('',Univ[b][f][r],end="")
print()
if b<3:
print("#"*20) | 1 | 1,107,998,714,108 | null | 55 | 55 |
n = input()
cube = n ** 3
print cube | from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
# INF = float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353
N = int(input())
A = list(map(int, input().split()))
A = sorted(A, reverse=True)
ans = A[0]
for i in range(N-2):
ans += A[i//2+1]
print(ans)
| 0 | null | 4,757,323,230,368 | 35 | 111 |
a=int(input())
b=100
i=0
while b<a:
b+= b // 100
i+=1
print(i) | s = input()
if(s[0] == 'R' and s[1] == 'R' and s[2] == 'R'):
print(3)
elif(s[0] == 'R' and s[1] == 'R' and s[2] == 'S'):
print(2)
elif(s[0] == 'S' and s[1] == 'R' and s[2] == 'R'):
print(2)
elif(s[0] == 'R' or s[1] == 'R' or s[2] == 'R'):
print(1)
else:
print(0)
| 0 | null | 15,935,127,355,302 | 159 | 90 |
A = input().split()
B = int(A[0]) - 2 * int(A[1])
print(0 if B < 0 else B) | a,b=map(int,input().split())
result=a-b*2
if result > 0:
print(result)
else:
print(0) | 1 | 167,336,996,165,600 | null | 291 | 291 |
s=list(input())
mod=2019
mdict={}
mdict[0]=1
ans=0
tmp=0
for i in range(len(s)):
n=s.pop()
key=(int(n)*pow(10,i,mod)+tmp)%mod
if key in mdict:
mdict[key]+=1
else:
mdict[key]=1
tmp=key
for var in mdict.values():
ans+=var*(var-1)//2
print(ans) | import sys
input()
for a,b,c in map(lambda x:sorted(map(int,x.split())),sys.stdin):
print(['NO','YES'][a*a+b*b==c*c])
| 0 | null | 15,336,373,378,180 | 166 | 4 |
n = int(input())
m = 0
for i in range(1, int(n**0.5)+1):
if n % i == 0:
m = i
x = m
y = n/m
print(int(x-1 + y-1)) | def solve():
a1, a2, a3 = map(int, input().split())
print('bwuisnt'[a1+a2+a3<=21::2])
if __name__ == '__main__':
solve()
| 0 | null | 139,535,763,322,510 | 288 | 260 |
def main():
from sys import setrecursionlimit, stdin, stderr
from os import environ
from collections import defaultdict, deque, Counter
from math import ceil, floor
from itertools import accumulate, combinations, combinations_with_replacement
setrecursionlimit(10**6)
dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
input = lambda: stdin.readline().rstrip()
LMIIS = lambda: list(map(int,input().split()))
II = lambda: int(input())
P = 10**9+7
INF = 10**18+10
N,K = LMIIS()
P = list(map(lambda x: int(x)-1,input().split()))
C = LMIIS()
score_sets = []
used = [False]*N
tmp = P[0]
for i in range(N):
if not used[i]:
tmp = i
score_set = []
while not used[tmp]:
used[tmp] = True
tmp = P[tmp]
score_set.append(C[tmp])
score_sets.append(score_set)
ans = -INF
for score_set in score_sets:
# 合計が最大になる区間を探す
len_set = len(score_set)
sum_score_set_real = sum(score_set)
sum_score_set = max(0,sum_score_set_real)
for i in range(len_set):
cum_sum_1 = 0
for j in range(i,min(len_set, i+K)):
cum_sum_1 += score_set[j]
num_move = j-i+1
tmp_max = cum_sum_1 + (K - num_move) // len_set * sum_score_set
ans = max(ans, tmp_max)
for i in range(1,len_set-1):
cum_sum_2 = sum_score_set_real
for j in range(i,min(len_set-1-(K-i),len_set-1)):
cum_sum_2 -= score_set[j]
for j in range(max(i,len_set-1-(K-i)),len_set-1):
cum_sum_2 -= score_set[j]
num_move = len_set - (j-i+1)
tmp_max = cum_sum_2 + (K - num_move) // len_set * sum_score_set
ans = max(ans,tmp_max)
print(ans)
main()
| # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
INF = 10**10
def main(N, K, P, C):
ans = max(C)
for i in range(N):
used = [False] * N
now = 0
mx = -INF
cnt = 0
while True:
if used[i] or cnt >= K:
break
used[i] = True
i = P[i] - 1
now += C[i]
mx = max(mx, now)
cnt += 1
tmp = max(mx, now)
if now > 0:
cycle, mod = divmod(K, cnt)
if cycle > 1:
tmp = max(tmp, now * (cycle - 1) + mx)
t = now * cycle
tmp = max(tmp, t)
for j in range(mod):
i = P[i] - 1
t += C[i]
tmp = max(tmp, t)
ans = max(ans, tmp)
print(ans)
if __name__ == '__main__':
input = sys.stdin.readline
N, K = map(int, input().split())
*P, = map(int, input().split())
*C, = map(int, input().split())
main(N, K, P, C)
| 1 | 5,355,614,231,440 | null | 93 | 93 |
import sys
def ISI(): return map(int, sys.stdin.readline().rstrip().split())
a, b=ISI()
if a>9 or b>9:print(-1)
else:print(a*b)
| import sys
for line in sys.stdin:
print(len(str(sum(list(map(int, line.split())))))) | 0 | null | 78,683,431,752,322 | 286 | 3 |
R, C, K = map(int, input().split())
score = [[0]*C for i in range(R)]
for i in range(K):
r, c, v = map(int, input().split())
score[r-1][c-1]+=v
ans0 = [[0]*C for i in range(R)]
ans1 = [[0]*C for i in range(R)]
ans2 = [[0]*C for i in range(R)]
ans3 = [[0]*C for i in range(R)]
ans1[0][0]=score[0][0]
ans2[0][0]=score[0][0]
ans3[0][0]=score[0][0]
for i in range(R):
for j in range(C):
if i>0:
ans0[i][j]=max(ans0[i][j],ans3[i-1][j])
ans1[i][j]=max(ans1[i][j],ans3[i-1][j]+score[i][j])
ans2[i][j]=max(ans2[i][j],ans3[i-1][j]+score[i][j])
ans3[i][j]=max(ans3[i][j],ans3[i-1][j]+score[i][j])
if j>0:
ans0[i][j]=max(ans0[i][j],ans0[i][j-1])
ans1[i][j]=max(ans1[i][j],ans0[i][j-1]+score[i][j],ans1[i][j-1])
ans2[i][j]=max(ans2[i][j],ans1[i][j-1]+score[i][j],ans2[i][j-1])
ans3[i][j]=max(ans3[i][j],ans2[i][j-1]+score[i][j],ans3[i][j-1])
print(ans3[R-1][C-1]) | import sys
import copy
R, C, K = map(int, input().split())
item = [[0] * (C + 1) for _ in range(R + 1)] # dp配列と合わせるために, 0行目, 0列目を追加している.
for s in sys.stdin.readlines():
r, c, v = map(int, s.split())
r -= 1
c -= 1
item[r][c] = v
dp0 = [[0] * (C+1) for r in range(R+1)]
dp1 = [[0] * (C+1) for r in range(R+1)]
dp2 = [[0] * (C+1) for r in range(R+1)]
dp3 = [[0] * (C+1) for r in range(R+1)]
for r in range(R):
for c in range(C):
dp3[r][c] = max(dp3[r][c], dp2[r][c] + item[r][c])
dp2[r][c] = max(dp2[r][c], dp1[r][c] + item[r][c])
dp1[r][c] = max(dp1[r][c], dp0[r][c] + item[r][c])
dp1[r][c+1] = max(dp1[r][c], dp1[r][c+1])
dp2[r][c+1] = max(dp2[r][c], dp2[r][c+1])
dp3[r][c+1] = max(dp3[r][c], dp3[r][c+1])
dp0[r+1][c] = max(dp0[r+1][c], dp1[r][c])
dp0[r+1][c] = max(dp0[r+1][c], dp2[r][c])
dp0[r+1][c] = max(dp0[r+1][c], dp3[r][c])
ans = max(dp0[R-1][C-1], dp1[R-1][C-1])
ans = max(ans, dp3[R-1][C-1])
ans = max(ans, dp2[R-1][C-1])
print(ans) | 1 | 5,573,808,724,510 | null | 94 | 94 |
n, m = map(int, input().split())
a = list(map(int, input().split()))
total = sum(a)
cnt = 0
for _a in a:
if _a * 4 * m >= total:
cnt += 1
if cnt >= m:
print("Yes")
else:
print("No")
| n, m = map(int, input().split())
a = list(map(int, input().split()))
s = sum(a)
a.sort()
a = a[::-1]
print('Yes' if s <= 4 * m * a[m - 1] else 'No')
| 1 | 38,570,987,104,860 | null | 179 | 179 |
k = int(input())
a,b = map(int, input().split())
if (int(b/k)*k >= a):
print("OK")
else:
print("NG") | k=int(input())
import sys
a,b=map(int,input().split())
for i in range(a,b+1):
if i%k==0:
print('OK')
sys.exit()
print('NG') | 1 | 26,390,616,032,638 | null | 158 | 158 |
n,k,c = list(map(int,input().split()));s=[True if i=="o" else False for i in input()]
front=[0]*n;back=[0]*n;count=0
day = 0
while day < n:
if s[day]:
count += 1
front[day]= 1
day += c+1
else:
day += 1
s = s[::-1]
day = 0
while day < n:
if s[day]:
back[day]= 1
day += c+1
else:
day += 1
back = back[::-1]
if count ==k:
for i in range(n):
if front[i] ==1 and back[i] == 1:
print(i+1) | a = int(input())
b = int(input())
ans_list = [1, 2, 3]
ans_list.remove(a)
ans_list.remove(b)
print(ans_list[-1]) | 0 | null | 75,716,022,316,896 | 182 | 254 |
import sys
import math
#r = map(int,input().split())
r = float(input())
print ('%.5f %.5f'% (r**2*math.pi, 2*r*math.pi)) | n, k = map(int, input().split())
a = set()
for i in range(k):
d = int(input())
a |= set(map(int, input().split()))
print(len(set(range(1, n + 1)) - a)) | 0 | null | 12,569,371,798,912 | 46 | 154 |
n = int(input())
s = input()
res = 'Yes' if s[0:n//2] == s[n//2:] else 'No'
print(res) | a,b,c = sorted(map(int,raw_input().split()))
print a,b,c | 0 | null | 73,328,423,217,670 | 279 | 40 |
m = input()
c = m[-1]
if c == 's':
print(m+'es')
else:
print(m+'s') | def g(A,l,m,r):
global c
L=A[l:m]+[1e10]
R=A[m:r]+[1e10]
i=j=0
for k in range(l,r):
if L[i]<R[j]:A[k]=L[i];i+=1
else:A[k]=R[j];j+=1
c+=1
def s(A,l,r):
if l+1<r:
m=(l+r)//2
s(A,l,m)
s(A,m,r)
g(A,l,m,r)
c=0
n=int(input())
A=list(map(int,input().split()))
s(A,0,n)
print(*A)
print(c)
| 0 | null | 1,234,383,865,988 | 71 | 26 |
S = list(input())
#print(set(S))
setS = set(S)
if len(setS) == 2:
print("Yes")
else:
print("No")
| # -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1
def main():
N=int(input())
A=list(map(int,input().split()))
B=[(A[i],i) for i in range(N)]
B.sort(key=lambda t: -t[0])
dp=[[0]*(N+1) for _ in range(N+1)]
ans=0
for i in range(N):
for x in range(i+1):
y=i-x
dp[x+1][y]=max(dp[x+1][y],dp[x][y]+B[i][0]*abs(B[i][1]-x))
dp[x][y+1]=max(dp[x][y+1],dp[x][y]+B[i][0]*abs(N-1-y-B[i][1]))
ans=max(ans,dp[x+1][y],dp[x][y+1])
print(ans)
if __name__ == '__main__':
main()
| 0 | null | 44,521,046,632,996 | 201 | 171 |
S, W = map(int, input().split(' '))
if S <= W:
print('unsafe')
else:
print('safe') | def main():
S, W = list(map(int, input().split()))
print("unsafe" if S <= W else "safe")
pass
if __name__ == '__main__':
main() | 1 | 29,220,000,840,940 | null | 163 | 163 |
a = input()
print(a.replace('P?', 'PD').replace('?D', 'PD').replace('??', 'PD').replace('?', 'D'))
| T = input()
count = 0
for c in T:
count += 1
if c == '?' and T[count-1] == 'D':
c = 'P'
print(c, end="")
elif c == '?':
c = 'D'
print(c, end="")
else:
print(c, end="") | 1 | 18,362,425,563,958 | null | 140 | 140 |
N = int(input())
get = set()
for _ in range(N):
s = str(input())
get.add(s)
print(len(get)) | n = int(input())
li = list(map(int, input().split()))
cnt = 0
flag = True
while flag:
flag = False
for i in range(n-1,0,-1):
if li[i] < li[i-1]:
li[i], li[i-1] = li[i-1], li[i]
flag = True
cnt +=1
print(*li)
print(cnt) | 0 | null | 15,170,011,974,340 | 165 | 14 |
def main():
H, W, M = (int(i) for i in input().split())
hc = [0]*(H+1)
wc = [0]*(W+1)
maps = set()
for _ in range(M):
h, w = (int(i) for i in input().split())
hc[h] += 1
wc[w] += 1
maps.add((h, w))
mah = max(hc)
maw = max(wc)
ans = mah+maw
hmaps = []
wmaps = []
for i, h in enumerate(hc):
if mah == h:
hmaps.append(i)
for i, w in enumerate(wc):
if maw == w:
wmaps.append(i)
if M < len(hmaps) * len(wmaps):
# 爆破対象の合計が最大になるマスがM個より多ければ,
# 必ず爆破対象がないマスに爆弾を置くことができる
# 逆にM個以下なら3*10^5のループにしかならないので
# このようにif文で分けなくても,必ずO(M)になるのでelse節のforを回してよい
print(ans)
else:
for h in hmaps:
for w in wmaps:
if (h, w) not in maps:
print(ans)
return
else:
print(ans-1)
if __name__ == '__main__':
main()
| from itertools import combinations
n, a, q, ms, cache = int(input()), list(map(int, input().split())), input(), map(int, input().split()), {}
l = set(sum(t) for i in range(n) for t in combinations(a, i + 1))
for m in ms:
print('yes' if m in l else 'no') | 0 | null | 2,413,108,434,112 | 89 | 25 |
t = input()
t = t.replace('?', 'D')
print(t) | n = int(input())
s = input()
count = 0
for i in range(n-2):
if s[i:i+3] == 'ABC':
count += 1
print(count)
| 0 | null | 58,726,316,754,532 | 140 | 245 |
n, p = map(int, input().split())
s = list(input())
ans = 0
if p == 2 or p == 5:
for i in range(n):
if int(s[i]) % p == 0:
ans += i + 1
else:
d = [0] * (n + 1)
ten = 1
for i in range(n - 1, -1, -1):
a = int(s[i]) * ten % p
ten *= 10
ten %= p
d[i - 1] = (d[i] + a) % p
cnt = [0] * p
for i in range(n, -1, -1):
cnt[d[i]] += 1
for i in cnt:
ans+=i*(i-1)/2
print(int(ans)) | n,p = map(int,input().split())
s = list(map(int, list(input()))) # 数値のリストにしておく
def solve():
if p in [2,5]: # 例外的な処理
ret = 0
for i in range(n): # 文字位置: i: 左から, n-1-i:右から (0 基準)
lsd = s[n-1-i] # 最下位桁
if lsd % p == 0: ret += n - i # LSD が割り切れたら LSD から右の桁数分加算
return ret
ten = 1 # S[l,r) のときの 10^r, init r=0 -> 10^0 = 1
cnt = [0]*p # 余りで仕分けして個数を集計
r = 0 # 左からの文字数 0 のときの余り
cnt[r] += 1 # 余りが同じものをカウント
for i in range(n): # 文字位置: i: 左から, n-1-i:右から (0 基準)
msd = s[n-1-i] # 最上位桁
r = (msd * ten + r) % p # r: 今回の余り
ten = ten * 10 % p # N≦2*10^5 桁 なので剰余計算忘れずに!
cnt[r] += 1 # 余りが同じものをカウント
ret = 0
for r in range(p): # 余り r の個数から組み合わせを計算
ret += cnt[r] * (cnt[r] - 1) // 2 # nCr(n=i,r=2)= n(n-1)/2
return ret
print(solve())
| 1 | 58,311,199,372,228 | null | 205 | 205 |
import itertools
def resolve():
n = int(input())
d = tuple(map(int,input().split()))
ans = 0
for a in itertools.combinations(d,2):
ans += a[0]*a[1]
print(ans)
resolve() | n=int(input())
A=input().split()
def findProductSum(A,n):
product = 0
for i in range (n):
for j in range ( i+1,n):
product = product + int(A[i])*int(A[j])
return product
print(findProductSum(A,n)) | 1 | 169,052,982,143,332 | null | 292 | 292 |
S = input()
K = int(input())
N = len(S)
#全て同じ文字の時
if len(set(S)) == 1:
print(N*K//2)
exit()
#隣合う文字列が何個等しいか
cycle = 0
tmp = S[0]
cnt = 1
for i in range(1, N):
if tmp != S[i]:
cycle += cnt//2
tmp = S[i]
cnt = 1
else:
cnt += 1
cycle += cnt//2
if S[0] == S[-1]:
i = 0
a = 0
tmp = S[0]
while S[i] == tmp:
i += 1
a += 1
j = N-1
b = 0
tmp = S[-1]
while S[j] == tmp:
j -= 1
b += 1
print((cycle*K) - (a//2 + b//2 - (a+b)//2)*(K-1))
else:
print(cycle*K) | from itertools import groupby
# RUN LENGTH ENCODING str -> tuple
def run_length_encode(S: str):
"""
'AAAABBBCCDAABBB' -> [('A', 4), ('B', 3), ('C', 2), ('D', 1), ('A', 2), ('B', 3)]
"""
grouped = groupby(S)
res = []
for k, v in grouped:
res.append((k, len(list(v))))
return res
S = input()
K = int(input())
if len(set(list(S))) == 1:
print((len(S) * K) // 2)
else:
rle = run_length_encode(S)
cnt = 0
for w, length in rle:
if length > 1:
cnt += length // 2
ans = cnt * K
if S[0] == S[-1]:
a = rle[0][1]
b = rle[-1][1]
ans -= (a // 2 + b // 2 - (a + b) // 2) * (K - 1)
print(ans)
| 1 | 175,542,810,822,170 | null | 296 | 296 |
print(input()**2) | n = int(input())
if len(set(input().split())) == n:
print('YES')
else:
print('NO') | 0 | null | 109,725,258,354,238 | 278 | 222 |
N = int(input())
S = list(input())
r = S.count('R')
g = S.count('G')
b = S.count('B')
cnt = 0
for i in range(N-3):
if S[i] == 'R':
r -= 1
cnt += g * b
l = 1
while (i + 2 * l < N):
if (S[i+l] == 'G' and S[i+2*l] == 'B') or (S[i+l] == 'B' and S[i+2*l] == 'G'):
cnt -= 1
l += 1
if S[i] == 'G':
g -= 1
cnt += r * b
l = 1
while (i + 2 * l < N):
if (S[i+l] == 'R' and S[i+2*l] == 'B') or (S[i+l] == 'B' and S[i+2*l] == 'R'):
cnt -= 1
l += 1
if S[i] == 'B':
b -= 1
cnt += g * r
l = 1
while (i + 2 * l < N):
if (S[i+l] == 'G' and S[i+2*l] == 'R') or (S[i+l] == 'R' and S[i+2*l] == 'G'):
cnt -= 1
l += 1
print(cnt)
| N = int(input())
A = list(map(int,input().split()))
xor = 0
for i in range(N):
xor ^= A[i]
anslis = []
for i in range(N):
answer = xor^A[i]
anslis.append(answer)
print(' '.join(map(str,anslis))) | 0 | null | 24,256,872,018,522 | 175 | 123 |
import array
K = int(input())
s = 0
cache = [1] * K
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
for i in range(1, K+1):
cache[i-1] = [1] * K
for j in range(i, K+1):
cache[i-1][j-1] = array.array('i', [1] * K)
for k in range(j, K+1):
cache[i-1][j-1][k-1] = gcd(k, gcd(j, i))
for i in range(1, K+1):
for j in range(1, K+1):
for k in range(1, K+1):
a, b, c = sorted([i, j, k])
s += cache[a-1][b-1][c-1]
print(s)
| import math
k=int(input())
ans=0
for i in range(k):
for j in range(k):
q=math.gcd(i+1,j+1)
for l in range(k):
ans+=math.gcd(q,l+1)
print(ans)
| 1 | 35,432,788,239,740 | null | 174 | 174 |
n = int(input())
deck = []
for i in range(n):
mark, num = input().split()
deck.append((mark, int(num)))
for mark, num in [(mark, num) for mark in 'SHCD' for num in range(1,14) if (mark, num) not in deck]:
print(mark, num) | n = int(input())
full_set=set([1,2,3,4,5,6,7,8,9,10,11,12,13])
S=[]
H=[]
C=[]
D=[]
for i in range(n):
suit,num = input().split()
if suit=="S": S.append(int(num))
elif suit=="H": H.append(int(num))
elif suit=="C": C.append(int(num))
elif suit=="D": D.append(int(num))
S_set=set(S)
H_set=set(H)
C_set=set(C)
D_set=set(D)
for (suit,suit_set) in zip(["S","H","C","D"],[S_set,H_set,C_set,D_set]):
for num in sorted(list(full_set - suit_set)): print(suit,num) | 1 | 1,052,914,606,212 | null | 54 | 54 |
from sys import stdin
rs = stdin.readline
ri = lambda : int(rs())
ril = lambda : list(map(int, rs().split()))
def main():
N, K = ril()
A = ril()
l = 1
r = 1000000000
while l < r:
m = (l + r) // 2
k = 0
for a in A:
k += (a - 1) // m
if k <= K:
r = m
else:
l = m + 1
print(r)
if __name__ == '__main__':
main()
| string = input()
a, b, c = map(int, (string.split(' ')))
if a < b & b < c :
print('Yes')
else:
print('No')
| 0 | null | 3,418,038,827,690 | 99 | 39 |
num = int(input())
hh = num // 3600
h2 = num % 3600
mm = h2 // 60
ss = h2 % 60
print(str(hh)+":"+str(mm)+":"+str(ss))
| S, W = map(int, input().split())
print("unsafe" if W >= S else "safe" )
| 0 | null | 14,792,137,136,612 | 37 | 163 |
D = int(input())
for i in range(D):
print(i%26+1) | n=int(input())
s,t=input().split()
ans=''
for i in range(2*n):
ans=ans+(s[i//2] if i%2==0 else t[(i-1)//2])
print(ans)
| 0 | null | 61,082,333,518,316 | 113 | 255 |
N = int(input())
z = []
w = []
for i in range(N):
x,y = map(int,input().split())
z.append(x+y)
w.append(x-y)
z_max = max(z)
z_min = min(z)
w_max = max(w)
w_min = min(w)
print(max(z_max-z_min,w_max-w_min)) | def get_ints():
return list(map(int,input().split()))
n = int(input())
a, b = [], []
for i in range(n):
x, y = get_ints()
a.append(x-y)
b.append(x+y)
a.sort()
b.sort()
ans = max(a[-1] - a[0], b[-1] - b[0])
print(ans) | 1 | 3,422,423,751,588 | null | 80 | 80 |
import sys
from collections import deque
read = sys.stdin.read
N, *ab = map(int, read().split())
graph = [[] for _ in range(N + 1)]
for a, b in zip(*[iter(ab)] * 2):
graph[a].append(b)
color = [0] * (N + 1)
queue = deque([1])
while queue:
V = queue.popleft()
number = 1
for v in graph[V]:
if number == color[V]:
number += 1
color[v] = number
queue.append(v)
number += 1
print(max(color))
for a, b in zip(*[iter(ab)] * 2):
print(color[b])
| import sys
while 1:
H, W = map(int, raw_input().split())
if H == 0 and W == 0:
break
else:
for h in range(0, H):
for w in range(0, W):
sys.stdout.write("#")
print ""
print "" | 0 | null | 68,110,617,532,308 | 272 | 49 |
S=input()
h=S/3600
r=S%3600/60
s=S-h*3600-r*60
print str(h)+':'+str(r)+':'+str(s) | import sys
prm = sys.stdin.readline()
S = int(prm)
h = S / (60 * 60);
S = S % (60 * 60);
m = S / 60;
s = S % 60;
print str(h)+':'+str(m)+':'+str(s) | 1 | 341,266,516,276 | null | 37 | 37 |
l={}
s=[]
h=[0]
n=d=m=0
for c in raw_input():
if c=="\\":
l[d]=n
d-=1
elif c=="/":
d+=1
if d in l:
while len(s) and s[-1][1]>l[d]: s.pop()
s.append([l[d],n])
n+=1
h.append(d)
a=[sum([h[i]-k for k in h[i:j+1]]) for i,j in s]
print sum(a)
print " ".join(map(str,[len(a)]+a)) | n=[int(i) for i in input().split()]
if sum(n)%9==0:
print("Yes")
else:
print("No")
| 0 | null | 2,218,112,622,000 | 21 | 87 |
x, y = map(int, input().split())
ans = 0
for i in [x, y]:
if i <= 3:
ans += 4 - i
if x == 1 and y == 1:
ans += 4
print(ans * 100000) | import math
a, b, x = map(int, input().split())
# 水が多いか少ないかで作図が変わってくる
# 半分以上水が入っている場合
if x >= a**2 * b / 2:
# 限界の時、a**2 * b - 0.5 * a**2 * tanθ * a = x
tan = 2 * (a**2 * b - x) / a**3
ans = math.degrees(math.atan(tan))
print(ans)
# 半分以下の場合
else:
# 限界の時、0.5 * b**2 * tan(90 - θ) * a = x
# tan(90 - θ) = 1/tanθ
tan = (a * b**2) / (2 * x)
ans = math.degrees(math.atan(tan))
print(ans)
| 0 | null | 152,196,269,915,150 | 275 | 289 |
X = int(input())
ans = 8 - (X - 400)//200
print(ans) | # -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1
X=int(input())
if X>=1800:
print(1)
elif X>=1600:
print(2)
elif X>=1400:
print(3)
elif X>=1200:
print(4)
elif X>=1000:
print(5)
elif X>=800:
print(6)
elif X>=600:
print(7)
elif X>=400:
print(8) | 1 | 6,695,043,876,892 | null | 100 | 100 |
N, M = map(int, input().split())
s = []
c = []
for _ in range(M):
si, ci = map(int, input().split())
s.append(si)
c.append(ci)
for i in range(0, 999):
i = str(i)
if len(i) == N:
tf = True
for j in range(M):
if i[s[j] - 1] != str(c[j]):
tf = False
break
if tf == True:
print(i)
exit()
print(-1)
| # C - Guess The Number
N, M = map(int, input().split())
SC = [list(map(int, input().split())) for _ in range(M)]
ans = [0,0,0]
# 条件だけで不適がわかるものは弾く
for i in range(M):
for j in range(i+1,M):
if (SC[i][0] == SC[j][0]) and (SC[i][1] != SC[j][1]):
print(-1)
quit()
# 各桁を指定をする
for k in range(M):
ans[SC[k][0]-(N-2)] = str(SC[k][1])
# 頭の桁が0で指定されると不適(str型かどうかで判定する)
if (ans[3-N] == "0") and (N == 1):
print(0)
quit()
elif ans[3-N] == "0":
print(-1)
quit()
# N桁の文字列にする
ans = [str(ans[i]) for i in range(3)]
res = "".join(ans)
res = res[(3-N):4]
# 頭の桁が指定無しの0だったら1に変える
if (res[0] == "0") and (N == 1):
res = "0"
elif res[0] == "0":
ref = list(res)
ref[0] = "1"
res = "".join(ref)
print(res)
| 1 | 60,757,899,528,970 | null | 208 | 208 |
_, D1 = map(int, input().split())
_, D2 = map(int, input().split())
print(0 if D1 < D2 else 1) | lit = input()
length = len(lit)
times = 0
if length != 1:
for i in range(int(length/2)):
if lit[i] != lit[length-1-i]:
times += 1
print(times)
else:
print(times) | 0 | null | 122,143,192,164,412 | 264 | 261 |
import math
R=int(input())
cir=2*math.pi*R
print(cir) | import sys
import math
def resolve(in_):
r = int(in_.read())
return 2 * r * math.pi
def main():
answer = resolve(sys.stdin.buffer)
print(answer)
if __name__ == '__main__':
main() | 1 | 31,340,477,002,240 | null | 167 | 167 |
from time import time
from random import randint
from numba import njit
from numpy import int64
@njit('i8(i8[:], i8[:])', cache=True)
def func(s, x):
last = [0] * 26
score = 0
for i, v in enumerate(x, 1):
last[v] = i
c = 0
for j in range(26):
c += s[j] * (i - last[j])
score += s[i * 26 + v] - c
return score
def main():
start = time()
d, *s = map(int, open(0).read().split())
s = int64(s)
x = int64(([*range(26)] * 15)[:d])
M = func(s, x)
while time() - start < 1.5:
y = x.copy()
if randint(0, 1):
y[randint(0, d - 1)] = randint(0, 25)
elif randint(0, 1):
i=randint(0, d - 16)
j=randint(i + 1, i + 15)
y[i], y[j] = y[j], y[i]
else:
i = randint(0, d - 15)
j = randint(i + 1, i + 7)
k = randint(j + 1, j + 7)
if randint(0, 1):
y[i], y[j], y[k] = y[j], y[k], y[i]
else:
y[i], y[j], y[k] = y[k], y[i], y[j]
t = func(s, y)
if t > M:
M = t
x = y
print(*x + 1, sep='\n')
main() | x, n = map(int, input().split())
p = set(map(int, input().split()))
ans = min(abs(x - i) for i in range(102) if i not in p)
print(x + ans if x - ans in p else x - ans)
| 0 | null | 11,827,706,730,852 | 113 | 128 |
N, K = [int(_) for _ in input().split()]
P = [int(_) - 1 for _ in input().split()]
C = [int(_) for _ in input().split()]
def f(v, K):
if K == 0:
return 0
if max(v) < 0:
return max(v)
n = len(v)
X = [0]
for i in range(n):
X.append(X[-1] + v[i])
ans = -(10 ** 10)
for i in range(n + 1):
for j in range(i):
if i - j > K:
continue
ans = max(ans, X[i] - X[j])
return(ans)
X = [False for _ in range(N)]
ans = -(10 ** 10)
for i in range(N):
if X[i]:
continue
t = i
v = []
while X[t] is False:
X[t] = True
v.append(C[t])
t = P[t]
n = len(v)
if K > n:
s = sum(v)
x = f(v * 2, n)
if s > 0:
a = s * (K // n - 1) + max(s + f(v * 2, K % n), x)
else:
a = x
else:
a = f(v * 2, K)
ans = max(a, ans)
print(ans)
| N, K = map(int, input().split())
P = [int(i)-1 for i in input().split()]
C = [int(i) for i in input().split()]
ans = -1e18
for si in range(N):
x = si
s = []
tot = 0
while True:
x = P[x]
s.append(C[x])
tot += C[x]
if x == si:
break
l = len(s)
t = 0
for i in range(l):
t += s[i]
if i+1 > K:
break
now = t
if tot > 0:
e = (K-i-1)//l
now += tot*e
ans = max(ans, now)
print(ans)
| 1 | 5,406,679,136,328 | null | 93 | 93 |
S = input()
a = [0] * (len(S) + 1)
for i in range(len(S)):
if S[i] == "<":
a[i + 1] = a[i] + 1
for i in list(range(len(S)))[::-1]:
if S[i] == ">" and a[i + 1] >= a[i]:
a[i] = a[i + 1] + 1
print(sum(a))
| s = input()
n = len(s) + 1
t = [0]*n
for i in range(n-1):
if s[i] == '<':
t[i+1] = t[i] + 1
for i in range(n-2,-1,-1):
if s[i] == '>':
t[i] = max(t[i],t[i+1]+1)
print(sum(t)) | 1 | 156,548,525,839,966 | null | 285 | 285 |
N, K = [int(x) for x in input().split()]
H = [int(x) for x in input().split()]
ans = 0
for h in H:
if h >= K:
ans += 1
print(ans)
| # -*- coding: utf-8 -*-
pi = 3.141592653589793
r = float( input() )
area = r * r * pi
circle = 2 * r * pi
print(format(area, '.10f'), format(circle, '.10f')) | 0 | null | 90,138,260,635,626 | 298 | 46 |
S = str(input())
print(S[0:3]) | S_list = [input() for i in range(2)]
N,K = map(int,S_list[0].split())
h_list = list(map(int,S_list[1].split()))
number = 0
for i in h_list:
if i>=K:
number += 1
print(number) | 0 | null | 97,201,206,700,940 | 130 | 298 |
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)
N = int(input())
XY = []
for _ in range(N):
A = int(input())
xy = [list(mapint()) for _ in range(A)]
XY.append(xy)
ans = 0
for i in range(1<<N):
honest = [-1]*N
cnt = 0
ok = True
for j in range(N):
if (i>>j)&1:
honest[j]=1
else:
honest[j]=0
for j in range(N):
if (i>>j)&1:
cnt += 1
for x, y in XY[j]:
if honest[x-1]!=y:
ok = False
break
if ok:
ans = max(ans, cnt)
print(ans)
| def main():
import itertools
n = int(input())
test = []
for i in range(n):
a = int(input())
t = []
for j in range(a):
x,y = map(int,input().split())
t.append([x,y])
test.append(t)
stats = ((0,1) for i in range(n))
ans = 0
for k in itertools.product(*stats):
tf = [-1 for i in range(n)]
flg = 0
for i in range(len(k)):
if k[i]==1:
for t in test[i]:
if tf[t[0]-1]==-1:
tf[t[0]-1] = t[1]
else:
if tf[t[0]-1] != t[1]:
flg = 1
break
for i in range(len(k)):
if tf[i] != -1:
if tf[i] != k[i]:
flg = 1
break
if flg == 0:
if ans < sum(k):
ans = sum(k)
print(ans)
if __name__ == "__main__":
main()
| 1 | 121,096,117,636,490 | null | 262 | 262 |
X, Y = list(map(int, input().split()))
pX = [0 for _ in range(206)]
pX[1] = 300000
pX[2] = 200000
pX[3] = 100000
point = pX[X] + pX[Y]
if X == 1 and Y == 1:
point += 400000
print(point) | def main():
s = input()
lst1 = []
lst2 = []
total_area = 0
for i in range(len(s)):
if s[i] == "\\":
lst1.append(i)
elif s[i] == "/" and len(lst1) > 0:
j = lst1.pop()
area = i - j
total_area += area
while len(lst2) > 0 and lst2[-1][0] > j:
area += lst2.pop()[1]
lst2.append([j, area])
print(total_area)
print(len(lst2), *[area for j, area in lst2])
if __name__ == "__main__":
main()
| 0 | null | 70,416,680,332,768 | 275 | 21 |
def main():
print(int(input())**2)
if __name__ == "__main__":
main() | import sys
readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)
def main():
from itertools import permutations
N = int(readline())
P = tuple(map(int, readline().split()))
Q = tuple(map(int, readline().split()))
d = {x: i for i, x in enumerate(permutations(range(1, N + 1)))}
print(abs(d[Q] - d[P]))
if __name__ == '__main__':
main()
| 0 | null | 123,086,355,406,820 | 278 | 246 |
n = input()
n = n.split()
d = int(n[1])
n = int(n[0])
c = 0
for a in range(n):
b = input()
b = b.split()
x = int(b[0])
y = int(b[1])
if x * x + y * y <= d * d:
c = c + 1
print(c) | def main():
N = int(input())
C = list(input())
C_ = sorted(C)
wr = rw = 0
for i in range(len(C_)):
if C[i] != C_[i]:
if C[i] == "R" and C_[i] == "W":
rw += 1
else:
wr += 1
print(max(wr, rw))
if __name__ == "__main__":
main() | 0 | null | 6,121,383,450,712 | 96 | 98 |
def main():
x, y, z = map(int, input().split(" "))
print(f"{z} {x} {y}")
if __name__ == "__main__":
main() | import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from collections import defaultdict
def resolve():
N = ir()
A = sorted([[i+1, d] for i, d in enumerate(lr())], key=lambda x: x[1])[::-1]
dp = defaultdict(lambda: -float('inf'))
dp[0, 0] = 0
for x in range(N):
for y in range(N-x):
i, d = A[x+y]
dp[x+1, y] = max(dp[x, y]+d*(i-x-1), dp[x+1, y])
dp[x, y+1] = max(dp[x, y]+d*(N-y-i), dp[x, y+1])
ans = 0
for i in range(N+1):
ans = max(ans, dp[i, N-i])
print(ans)
resolve() | 0 | null | 35,965,905,735,970 | 178 | 171 |
H = int(input())
cnt = 0
while True:
H //= 2
cnt += 1
if H == 0:
break
print(2**cnt-1)
| import math
h = int(input())
num = math.log(h,2)
num = int(num)
ans = 0
for i in range(num+1):
ans += 2**i
print(ans) | 1 | 80,038,569,729,980 | null | 228 | 228 |
from collections import deque
def bfs(G,dist,s):
q = deque([])
dist[s] = 0
q.append(s)
while len(q) > 0:
v = q.popleft()
for nv in G[v]:
if dist[nv] != -1:
continue
dist[nv] = dist[v] + 1
q.append(nv)
n,m = map(int,input().split())
G = [[] for _ in range(n)]
for i in range(m):
a,b = map(int,input().split())
G[a-1].append(b-1)
G[b-1].append(a-1)
dist = [-1]*n
cnt = 0
for v in range(n):
if dist[v] != -1:
continue
bfs(G,dist,v)
cnt += 1
print(cnt-1) | import sys
sys.setrecursionlimit(10 ** 9)
n,m = map(int, input().split())
ab = []
c = [set() for i in range(n)]
for i in range(m):
a = list(map(int, input().split()))
a.sort()
x,y = a[0]-1,a[1]-1
c[x].add(y)
c[y].add(x)
def dfs(s):
if vis[s] == False:
ary.append(s)
vis[s] = True
for j in c[s]:
dfs(j)
vis = [False for i in range(n)]
ans= []
for i in range(n):
ary = []
dfs(i)
if len(ary) == 0:
continue
ans.append(len(ary))
print (len(ans)-1) | 1 | 2,277,766,945,662 | null | 70 | 70 |
a = [int(v) for v in input().rstrip().split()]
r = 'bust' if sum(a) >= 22 else 'win'
print(r)
| def f_sugoroku(INF=float('inf')):
from collections import deque
N, M = [int(i) for i in input().split()]
S = input()
dp = [INF] * (N + 1) # [v]: 頂点 v からゴール到達までに最低何回かかるか
dp[N] = 0
q = deque([0])
for i in range(N - 1, -1, -1):
while True:
if len(q) == 0:
return '-1'
if q[0] != INF and len(q) <= M:
break
q.popleft()
if S[i] == '0':
dp[i] = q[0] + 1
q.append(dp[i])
ans = []
current = 0
rest = dp[0] # ゴールまであと何回ルーレットを回すか
while current < N:
# dp の値が rest から 1 減るまでインデックスを進める
i = 1
rest -= 1 # ルーレットを回した
while dp[current + i] != rest:
i += 1
# ルーレットの出目を答えに追加し、現在位置を進める
ans.append(i)
current += i
return ' '.join(map(str, ans))
print(f_sugoroku()) | 0 | null | 128,699,995,384,460 | 260 | 274 |
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
INF=10**9+7
l=[0]*k
ans=0
for i in range(k-1,-1,-1):
x=i+1
temp=pow(k//x,n,INF)
for j in range(2,k//x+1):
temp=(temp-l[j*x-1])%INF
l[i]=temp
ans=(ans+x*temp)%INF
print(ans)
| d, t, s = map(int, input().split())
dist = t * s - d
if dist >= 0:
print("Yes")
elif dist < 0:
print("No") | 0 | null | 20,014,732,668,370 | 176 | 81 |
import sys
input = sys.stdin.readline
n, x, res = int(input()), 1, 0
if n & 1: print(0)
else:
n //= 2
while x < n:
x *= 5
res += n // x
print(res) | class Info:
def __init__(self,arg_start,arg_end,arg_S):
self.start = arg_start
self.end = arg_end
self.S = arg_S
POOL = []
LOC = []
cnt = 0
sum_S = 0
line = input()
for c in line:
if c == "\\":
LOC.append(cnt)
elif c == "/":
if len(LOC) == 0:
continue
tmp_start = LOC.pop()
tmp_end = cnt
tmp_S = tmp_end - tmp_start
sum_S += tmp_S
while len(POOL) > 0:
if POOL[-1].start > tmp_start and POOL[-1].end < tmp_end:
tmp_S += POOL[-1].S
POOL.pop()
else:
break
POOL.append(Info(tmp_start,tmp_end,tmp_S))
else:
pass
cnt += 1
lst = [len(POOL)]
for i in range(len(POOL)):
lst.append(POOL[i].S)
print(sum_S)
print(*lst)
| 0 | null | 57,833,626,072,330 | 258 | 21 |
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
N = int(input())
AB = [tuple(map(int,input().split())) for i in range(N-1)]
es = [[] for _ in range(N)]
for i,(a,b) in enumerate(AB):
a,b = a-1,b-1
es[a].append((b,i))
es[b].append((a,i))
ans = [None] * (N-1)
def dfs(v,p=-1,c=-1):
nc = 1
for to,e in es[v]:
if to==p: continue
if nc==c: nc += 1
ans[e] = nc
dfs(to,v,nc)
nc += 1
dfs(0)
print(max(ans))
print(*ans, sep='\n') | # -*- config: utf-8 -*-
if __name__ == '__main__':
for i in range(int(raw_input())):
nums = map(lambda x:x**2,map(int,raw_input().split()))
flg = False
for i in range(3):
s = int(0)
for j in range(3):
if i == j :
continue
s += nums[j]
if s == nums[i] :
flg = True
if flg == True :
print "YES"
else :
print "NO" | 0 | null | 67,962,449,520,768 | 272 | 4 |
def solve():
N = int(input())
XLs = [list(map(int, input().split())) for _ in range(N)]
XLs.sort(key = lambda x:(x[0],-1*x[1]))
interval = (XLs[0][0]-XLs[0][1],XLs[0][0]+XLs[0][1])
ret = 0
for XL in XLs[1:]:
interval_ = (XL[0]-XL[1], XL[0]+XL[1])
if interval_[0] < interval[1]:
interval = (max(interval[0], interval_[0]),min(interval[1], interval_[1]))
ret += 1
else:
interval = interval_
print(N-ret)
solve() | N = input()
L = len(N)
# dp[i][j]: 紙幣の最小枚数
# i: 決定した桁数
# j: 1枚多めに渡すフラグ
dp = [[float("inf")] * 2 for _ in range(L + 1)]
# 初期条件
dp[0][0] = 0
dp[0][1] = 1
# 貰うDP
for i in range(L):
n = int(N[i])
dp[i + 1][0] = min(dp[i][0] + n, dp[i][1] + (10 - n))
dp[i + 1][1] = min(dp[i][0] + (n + 1), dp[i][1] + (10 - (n + 1)))
print(dp[L][0]) | 0 | null | 80,545,415,487,460 | 237 | 219 |
S=input()
Q=int(input())
p=0
u=0
l=""
r=""
for i in range(Q):
q=list(input().split())
if q[0]=="1":
u+=1
else:
if q[1]=="1" and u%2==0 or q[1]=="2" and u%2==1:
l=l+q[2]
else:
r=r+q[2]
if u%2==0:
l=l[::-1]
S=l+S+r
else:
S=S[::-1]
r=r[::-1]
S=r+S+l
print(S)
| s=input()
n = int(input())
s1=""
s2=""
t=0
for i in range(n):
m=input().split()
if len(m)==1:
t=1-t
else:
if t==0:
if int(m[1])==2:
s2+=m[2]
else:
s1+=m[2]
else:
if int(m[1])==2:
s1+=m[2]
else:
s2+=m[2]
#print(s1,s2)
if t==0:
s3=s1[::-1]
print(s3+s+s2)
else:
s=s[::-1]
s2=s2[::-1]
print(s2+s+s1)
| 1 | 57,403,607,247,610 | null | 204 | 204 |
apple = [1, 2, 3]
apple.remove(int(input()))
apple.remove(int(input()))
print(apple[0]) | import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
A,B = map(int, read().split())
temp = [A,B]
for i in range(1,4):
if i not in temp:
print(i) | 1 | 110,325,570,640,572 | null | 254 | 254 |
n = list(map(int, input().split()))
n.sort()
print('{} {} {}'.format(n[0], n[1], n[2])) | l = map(int, raw_input().split())
a, b, c = sorted(l)
print a, b, c | 1 | 409,430,638,860 | null | 40 | 40 |
import sys
from math import gcd
from functools import reduce
def enum_div(n):
ir=int(n**(0.5))+1
ret=[]
for i in range(1,ir):
if n%i == 0:
ret.append(i)
if (i!= 1) & (i*i != n):
ret.append(n//i)
return ret
n=int(input())
ap=list(map(int,input().split()))
amin=min(ap)
amax=max(ap)
if amax==1:
print("pairwise coprime")
sys.exit()
if reduce(gcd,ap)!=1:
print("not coprime")
sys.exit()
if n>=78500 :
print("setwise coprime")
sys.exit()
aa=[0]*(amax+1)
for ai in ap:
aa[ai]+=1
for pp in range(2,amax+1):
psum=sum(aa[pp: :pp])
# print("pp:",pp,psum)
if psum>=2:
print("setwise coprime")
sys.exit()
## max_13.txt ... max_16.txt : "setwise coprime"
print("pairwise coprime")
| from collections import defaultdict
from math import gcd
N = int(input())
A = list(map(int, input().split()))
A_set = set(A)
num = 10 ** 6
primes = [0] * (num + 1)
primes[0] = 1
primes[1] = 1
for i in range(2, num + 1):
if primes[i] != 0:
continue
cnt = 1
while i * cnt <= num:
primes[i * cnt] = i
cnt += 1
def func(n):
res_dic = defaultdict(int)
while n > 1:
x = primes[n]
res_dic[x] += 1
n //= x
return res_dic
pairwise = True
cnt_dic = defaultdict(int)
for a in A:
dic = func(a)
for k in dic.keys():
cnt_dic[k] += 1
if cnt_dic[k] > 1:
pairwise = False
break
g = A[0]
for a in A[1:]:
g = gcd(g, a)
if pairwise:
print('pairwise coprime')
elif g == 1:
print('setwise coprime')
else:
print('not coprime')
| 1 | 4,061,107,625,148 | null | 85 | 85 |
from math import gcd
from functools import reduce
from collections import Counter
class Osa_k:
def __init__(self, maximum):
self.table = [-1] * (maximum+1)
for i in range(2, maximum+1):
for j in range(2, int(i**0.5)+1):
if i%j == 0:
self.table[i] = j
break
def factorize(self, n):
res = Counter()
b = n
while 1:
if self.table[b] == -1:
break
res[self.table[b]] += 1
b //= self.table[b]
res[b] += 1
return res
N = int(input())
*A, = map(int, input().split())
def f():
cnt = Counter()
ins = Osa_k(10**6)
for i in A:
res = ins.factorize(i) if i!=1 else {}
for j in res.keys():
cnt[j] += 1
if cnt[j] >= 2:
return False
return True
cond1 = f()
cond2 = (reduce(gcd, A)==1)
if cond1:
print("pairwise coprime")
elif cond2:
print("setwise coprime")
else:
print("not coprime")
| n = int(input())
pl = list(map(int, input().split()))
min_num = 1001001001
ans = 0
for p in pl:
if p <= min_num:
ans += 1
min_num = p
print(ans) | 0 | null | 44,593,062,782,912 | 85 | 233 |
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
aa = [0]
bb = [0]
for i in range(n):
aa.append(a[i]+aa[-1])
for i in range(m):
bb.append(b[i]+bb[-1])
ans = 0
j = m
for i in range(n+1):
r = k - aa[i]
while bb[j] > r and j >= 0:
j -= 1
if j >= 0:
ans = max(ans, i+j)
print(ans) | r, c = map(int, input().split())
matrix = [[0 for i in range(c+1)] for j in range(r+1)]
for i in range(r):
a = list(map(int, input().split()))
for j in range(c):
matrix[i][j] = a[j]
matrix[i][c] = sum(a)
for l in range(c+1):
for m in range(r):
matrix[r][l] += matrix[m][l]
for cv in range(r+1):
for fv in range(c+1):
print(matrix[cv][fv], end='')
if fv != c:
print(" ", end='')
print()
| 0 | null | 6,128,835,292,672 | 117 | 59 |
n=input()
if n.count('7')>0:
print('Yes')
else:
print('No') | print('Yes' if '7' in input() else ('No')) | 1 | 34,397,071,950,500 | null | 172 | 172 |
#
import sys
n = int(input())
#素因数分解
def factorization(n):
arr = []
tmp = n
#2から√n でnを割る ・・・割れた回数を指数として追加
for i in range(2, int(n ** 0.5 // 1) + 1):
if tmp % i == 0:
cnt = 0
while tmp % i == 0:
cnt += 1
tmp //= i
arr.append([i, cnt])
if tmp != 1: #割り切れなかったとき
arr.append([tmp, 1])
if arr == []: #素数の場合(割れなかった場合)
arr.append([n, 1])
return arr
p = factorization(n)
count = 0
#すべての素因数で分解する
for x in p:
#割れなくなったら抜ける
if n == 1:
break
#x[i][0]の1からp[i][1]乗で割る
for j in range(1, x[1] + 1):
if n % (x[0]**j) == 0:
n /= x[0]**j
count += 1
else:#割れなくなったら抜ける
break
print(count) | import math
from sys import stdin
input = stdin.readline
class Eratos:
def __init__(self, n):
self.is_prime = [True]*(n+1)
self.is_prime[0] = False
self.is_prime[1] = False
self.primes = []
# for i in range(2, int(math.sqrt(n))+1):
for i in range(2, n):
if self.is_prime[i]:
self.primes.append(i)
j = i + i
while j <= n:
self.is_prime[j] = False
j += i
def all(self):
return self.primes
def main():
N = int(input())
MAX = 10**6
primes = Eratos(MAX).all()
cnt = 0
for p in primes:
if N < p: break
e = 1
while not(N % p**e):
N //= (p**e)
cnt += 1
e += 1
if N > MAX:
for p in primes:
while N % p == 0:
N //= p
if N > MAX:
print(cnt+1)
else:
print(cnt)
else:
print(cnt)
if(__name__ == '__main__'):
main()
| 1 | 16,916,980,998,500 | null | 136 | 136 |
import sys
def insertionSort(A, n, g):
for i in range(g, n):
global cnt
v = A[i]
j = i - g
while j >= 0 and A[j] > v:
A[j+g] = A[j]
j = j - g
cnt += 1
A[j+g] = v
def shellSort(A, n):
global m
global G
for i in range(0, m):
insertionSort(A, n, G[i])
n = int(input())
A = []
for i in range(n):
A.append(int(input()))
if n == 1:
print(1)
print(1)
print(0)
print(A[0])
sys.exit()
t = n - 1
G = []
G.append(t)
while t != 1:
t = t//2
G.append(t)
m = len(G)
cnt = 0
shellSort(A, n)
print(m)
print(' '.join(map(str, G)))
print(cnt)
for i in range(n):
print(A[i])
| def insertionSort(A, n, g, cnt):
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 = j - g
cnt += 1
A[j+g] = v
return [cnt, A]
def shellSort(A, n):
cnt = 0
a = 1
G = []
while a <= n:
G.append(a)
a = 3*a + 1
m = len(G)
G = G[::-1]
for i in range(0, m):
cnt, A = insertionSort(A, n, G[i], cnt)
return [m, G, cnt, A]
if __name__ == "__main__":
n = int(input())
A = [int(input()) for _ in range(n)]
m, G, cnt, A = shellSort(A, n)
print(m)
print(*G)
print(cnt)
for i in A:
print(i)
| 1 | 28,276,517,568 | null | 17 | 17 |
N,M=list(map(int,input().split()))
import math
if N==1 or M==1:
print(1)
else:
print(int(math.ceil(N*M/2))) | a, b = input().split(" ")
a = int(a)
b = int(b)
if a-2*b < 0:
print(0)
else:
print(a-2*b)
| 0 | null | 108,578,310,760,920 | 196 | 291 |
if __name__ == '__main__':
from statistics import pstdev
while True:
# ??????????????\???
data_count = int(input())
if data_count == 0:
break
scores = [int(x) for x in input().split(' ')]
# ?¨??????????????¨????
result = pstdev(scores)
# ???????????????
print('{0:.8f}'.format(result)) | #!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
?¨??????????
n ????????????????????????????????§????????°???????????°???????¨?????????£??????
????????????????????????s1, s2 ... sn??¨????????¨??????????¨??????????????±???????????????°????????????????????????
??????????????????????????¨????????°????????£?±2?????\???????????§???????????????
?±2 = (???ni=1(si - m)2)/n
?????£?????£????????????????¨???????????±??¨?????????
"""
import math
while True:
num = int(input().strip())
if num == 0:
break
s = list(map(float,input().strip().split()))
# ?????????
m = 0
for i in range(num):
m += s[i]
m = m / num
# ?¨??????????
a2 = 0
for i in range(num):
a2 += (s[i] - m) ** 2
a = math.sqrt(a2 / num)
print(a) | 1 | 193,852,957,730 | null | 31 | 31 |
n = int(input())
ac = 0
wa = 0
tle = 0
re = 0
for i in range(n):
status = input()
if status == 'AC':
ac += 1
elif status == 'WA':
wa += 1
elif status == 'TLE':
tle += 1
else:
re += 1
print('AC x', ac)
print('WA x', wa)
print('TLE x', tle)
print('RE x', re)
| S = list(input())
countA = 0
countB = 0
for i in range(0, len(S)):
if S[i] == "A":
countA += 1
elif S[i] == "B":
countB += 1
if countA == 3 or countB == 3:
print("No")
else:
print("Yes") | 0 | null | 31,763,158,306,700 | 109 | 201 |
apple = [1, 2, 3]
apple.remove(int(input()))
apple.remove(int(input()))
print(apple[0]) | import itertools
N, M, Q = list(map(int, input().split()))
a = []
b = []
c = []
d = []
for i in range(Q):
ai, bi, ci, di = list(map(int, input().split()))
a.append(ai)
b.append(bi)
c.append(ci)
d.append(di)
ans = 0
for A in list(itertools.combinations_with_replacement(range(1, M+1), N)):
# print(A)
score = 0
for i in range(Q):
if A[b[i]-1] - A[a[i]-1] == c[i]:
score += d[i]
if ans < score:
ans = score
# print(A, score)
print(ans)
| 0 | null | 68,767,437,018,112 | 254 | 160 |
k = int(input())
a, b = map(int,input().split())
flg = False
for i in range(a, b+1):
if i % k == 0:
flg = True
break
print('OK') if flg else print('NG') | def func(n):
i = 1
dst = ""
def include3(x, i):
local_dst = ""
if x % 10 == 3:
local_dst += " " + str(i)
else:
if x // 10:
local_dst += include3(x // 10, i)
return local_dst
while i <= n:
x = i
if x % 3 == 0:
dst += " " + str(i)
else:
dst += include3(x, i)
i += 1
return dst
print(func(int(input()))) | 0 | null | 13,867,885,627,378 | 158 | 52 |
def gcd(a,b):
if b == 1:
return 1
elif b == 0:
return a
else:
return gcd(b,a%b)
a,b = map(int, raw_input().split(' '))
if a > b:
print gcd(a,b)
else:
print gcd(b,a) | x, y = map(int,input().split())
while True:
if x >= y:
x %= y
else:
y %= x
if x == 0 or y == 0:
print(x+y)
break | 1 | 7,370,199,550 | null | 11 | 11 |
def maximumProfit(A, N):
minR = A[0]
minP = A[1] - A[0]
for i in range(1, N):
R = A[i] - minR
if R > minP:
minP = R
if A[i] < minR:
minR = A[i]
return minP
if __name__ == "__main__":
N = input()
A = []
for i in range(N):
A.append(input())
print maximumProfit(A, N) | for i in range(0, 10000):
print('Case {}: {}'.format(i + 1, input()))
| 0 | null | 243,204,273,970 | 13 | 42 |
flg = len(set(input())) == 2
print('Yes' if flg else 'No')
| import bisect
from math import ceil
class BinaryIndexedTree():
def __init__(self, max_n):
self.size = max_n + 1
self.tree = [0] * self.size
self.depth = self.size.bit_length()
def initialize(self, seq):
for i, x in enumerate(seq[1:], 1):
self.tree[i] += x
j = i + (i & (-i))
if j < self.size:
self.tree[j] += self.tree[i]
def __repr__(self):
return self.tree.__repr__()
def get_sum(self, i):
s = 0
while i:
s += self.tree[i]
i -= i & -i
return s
def add(self, i, x):
while i < self.size:
self.tree[i] += x
i += i & -i
def find_kth_element(self, k):
x, sx = 0, 0
dx = (1 << self.depth)
for i in range(self.depth - 1, -1, -1):
dx = (1 << i)
if x + dx >= self.size:
continue
y = x + dx
sy = sx + self.tree[y]
if sy < k:
x, sx = y, sy
return x + 1
n,d,a = map(int,input().split())
monster = sorted([list(map(int,input().split())) for _ in range(n)],key=lambda x:x[0])
x_list = [0]+[monster[i][0] for i in range(n)]+[float("inf")]
h_list = [0]+[monster[i][1] for i in range(n)]+[float("inf")]
bit = BinaryIndexedTree(n+2)
ans = 0
for i in range(1,n+1):
hp = h_list[i] - bit.get_sum(i)
if hp < 0:
continue
atk_time = ceil(hp/a)
ans += atk_time
top = bisect.bisect_right(x_list, x_list[i]+2*d)
bit.add(i, atk_time*a)
bit.add(top, -atk_time*a)
print(ans) | 0 | null | 68,512,420,969,110 | 201 | 230 |
l = [1,2,3]
l.remove(int(input()))
l.remove(int(input()))
print(l[0])
| def generator(n):
if n == 1:
yield [0]
else:
for A in generator(n - 1):
for i in range(max(A) + 2):
A.append(i)
yield A
A.pop()
n = int(input())
for A in generator(n):
print(''.join([chr(a + 97) for a in A])) | 0 | null | 81,572,177,783,320 | 254 | 198 |
def sep():
return map(int,input().strip().split(" "))
def lis():
return list(sep())
n=int(input())
ar=lis()
data=[]
for i in range(n):
data.append((ar[i],i))
data.sort(reverse=True)
dp = [[0] * (n+1) for _ in range(n+1)]
for i in range(n):
a, p = data[i]
for j in range(i+1):
dp[i+1][j+1] = max(dp[i+1][j+1],dp[i][j] + abs(n-1-j-p)*a)
dp[i+1][j] = max(dp[i+1][j], dp[i][j] + abs(i-j-p)*a)
#print(dp)
print(max(dp[-1]))
| # coding: utf-8
import sys
import math
import collections
import itertools
INF = 10 ** 13
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def lcm(x, y) : return (x * y) // math.gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def LS() : return input().split()
def RS(N) : return [input() for _ in range(N)]
def LRS(N) : return [input().split() for _ in range(N)]
def PL(L) : print(*L, sep="\n")
def YesNo(B) : print("Yes" if B else "No")
def YESNO(B) : print("YES" if B else "NO")
N = I()
A = LI()
A = sorted([[a, i] for i, a in enumerate(A)], reverse=True, key=lambda x : x[0])
dp = [[0] * (N+1) for _ in range(N+1)]
for i in range(N):
for j in range(N-i):
dp[i+1][j] = max(dp[i+1][j], dp[i][j] + A[i+j][0] * (A[i+j][1] - i))
dp[i][j+1] = max(dp[i][j+1], dp[i][j] + A[i+j][0] * ((N-j-1) - A[i+j][1]))
res = 0
for i in range(N):
res = max(res, dp[i][N-i])
print(res)
| 1 | 33,761,838,998,290 | null | 171 | 171 |
A, B, C = map(int, input().split())
K = int(input())
while A >= B and K > 0:
B *= 2
K -= 1
while B >= C and K > 0:
C *= 2
K -= 1
# print(A, B, C, K)
if A < B and B < C:
print("Yes")
else:
print("No")
| import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)
X = int(input())
from math import gcd
print(360*X//gcd(360, X)//X) | 0 | null | 10,069,037,994,250 | 101 | 125 |
import math
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
import itertools
t=[i for i in range(1,n+1)]
a = list(itertools.permutations(t))
def num(b,t,n):
c = 0
for i in range(len(t)):
if list(t[i]) != b:
c += 1
else:
break
return(c)
x = num(p,a,n)
y = num(q,a,n)
print(abs(x-y)) | import itertools
def main():
n = int(input())
p_list = list(itertools.permutations ([i+1 for i in range(n)]))
p = tuple(map(int, input().split(" ")))
q = tuple(map(int, input().split(" ")))
pn = 0
qn = 0
for i in range(len(p_list)):
if p == p_list[i]:
pn = i
if q == p_list[i]:
qn = i
print(abs(pn-qn))
if __name__ == "__main__":
main() | 1 | 100,294,982,567,450 | null | 246 | 246 |
N = int(input())
T = [[] for _ in range(N)]
for i in range(N):
A = int(input())
for j in range(A):
x, y = map(int, input().split())
x -= 1
T[i].append((x, y))
ans = 0
for i in range(1 << N):
isPossible = True
bit = [0] * N
for k in range(N):
if (i >> k) & 1:
bit[k] = 1
for idx, b in enumerate(bit):
if b == 1:
for x, y in T[idx]:
isPossible &= (bit[x] == y)
if isPossible:
ans = max(ans, bit.count(1))
print(ans)
| n = int(input())
a = []
for _ in range(n):
a.append([list(map(int,input().split())) for __ in range(int(input()))])
l = []
for i in range(2**n):
flag2 = 0
for j in range(n):
if i >> j & 1 == 1:
flag1 = 0
for k in a[j]:
if i >> (k[0]-1) & 1 != k[1]:
flag1 = 1
flag2 += 1
break
if flag1 : break
if flag2 == 0 :l.append(list(bin(i)).count('1'))
print(max(l)) | 1 | 121,861,228,588,700 | null | 262 | 262 |
N = int(input())
S = input()
count = 1
check = S[0]
for c in S:
if c != check:
count += 1
check = c
print(count) | class dictionary:
def __init__(self):
self._d = set()
def insert(self, s):
self._d.add(s)
def find(self, s):
if s in self._d:
print("yes")
else:
print("no")
if __name__ == '__main__':
dd = dictionary()
n = int(input())
for _ in range(n):
args = input().split()
if args[0] == "insert":
dd.insert(args[1])
elif args[0] == "find":
dd.find(args[1])
| 0 | null | 85,175,715,126,710 | 293 | 23 |
N, P = map(int, input().split())
S = [int(s) for s in input()]
if P == 2 or P == 5:
print(sum(i for i, s in enumerate(S, 1) if s % P == 0))
quit()
C = [0] * P
tens = 1
cur = 0
for s in reversed(S):
cur = (cur + s * tens) % P
C[cur] += 1
tens = (tens * 10) % P
print(C[0] + sum(c * (c - 1) // 2 for c in C)) | import math
L,R,d = map(int, input().strip().split())
x=math.ceil(L/d)
y=math.floor(R/d)
print(y-x+1)
| 0 | null | 32,880,227,067,270 | 205 | 104 |
"""
N = list(map(int,input().split()))
S = [str(input()) for _ in range(N)]
S = [list(map(int,list(input()))) for _ in range(h)]
print(*S,sep="")
"""
import sys
sys.setrecursionlimit(10**6)
input = lambda: sys.stdin.readline().rstrip()
inf = float("inf") # 無限
r = int(input())
print(r*r) | def main():
n = int(input())
print((n + 1) // 2)
main() | 0 | null | 102,239,389,342,218 | 278 | 206 |
import math as mt
import sys, string
from collections import Counter, defaultdict
input = sys.stdin.readline
MOD = 1000000007
# input functions
I = lambda : int(input())
M = lambda : map(int, input().split())
Ms = lambda : map(str, input().split())
ARR = lambda: list(map(int, input().split()))
def main():
a, b, c = M()
k = I()
ans = 0
while b <= a:
b *= 2
ans += 1
while c <= b:
c *= 2
ans += 1
if ans <= k:
print("Yes")
else:
print("No")
# testcases
tc = 1
for _ in range(tc):
main() | a, b, c = map(int, input().split())
k = int(input())
count = 0
while True:
if b > a:
break
b *= 2
count += 1
while True:
if c > b:
break
c *= 2
count += 1
if count <= k:
print('Yes')
else:
print('No')
| 1 | 6,835,425,454,308 | null | 101 | 101 |
from math import gcd, sqrt
from functools import reduce
n, *A = map(int, open(0).read().split())
def f(A):
sup = max(A)+1
table = [i for i in range(sup)]
for i in range(2, int(sqrt(sup))+1):
if table[i] == i:
for j in range(i**2, sup, i):
table[j] = i
D = set()
for a in A:
while a != 1:
if a not in D:
D.add(a)
a //= table[a]
else:
return False
return True
if reduce(gcd, A) == 1:
if f(A):
print('pairwise coprime')
else:
print('setwise coprime')
else:
print('not coprime') | import sys
input = sys.stdin.readline
def gcd(a,b):
while b:
a,b=b,a%b
return a
def prime_factor(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 set(a)
n=int(input())
L=list(map(int,input().split()))
val = L[0]
for i in range(1,n):
val = gcd(L[i],val)
if val!=1:
print('not coprime')
sys.exit()
d={}
for i in range(n):
factor=prime_factor(L[i])
for j in factor:
if j not in d:
d[j]=1
else:
print('setwise coprime')
sys.exit()
print('pairwise coprime')
| 1 | 4,137,203,274,500 | null | 85 | 85 |
x=input()
y=x.split(" ")
n=int(y[0])
g=input()
h=g.split(" ")
su=0
for b in h:
su+=int(b)
if(su>n):
print(-1)
else:
print(n-su)
| s = input()
moji = len(s)
if moji % 2 == 0:
moji = int(moji/2)
else:
moji = int((moji-1)/2)
answer = 0
for i in range(moji):
if s[i] != s[len(s)-1-i]:
answer += 1
print(answer) | 0 | null | 75,786,606,136,518 | 168 | 261 |
from collections import deque # dequeはappendleftができるので。
letter = ['z'] + [chr(i) for i in range(97, 123)] # [0, a, b, c, ... , z]というリスト
N = int(input())
i = 0
n_24 = deque()
while N > 0:
n_24.appendleft(N % 26)
if N%26== 0:
N -= 26
N = N // 26
ans = ''
for dig in (n_24):
ans += letter[dig]
print(ans) | n = int(input())
ans = ''
while n:
n -= 1
ans += chr(ord('a') + n % 26)
n //= 26
print(ans[::-1]) | 1 | 11,992,676,542,210 | null | 121 | 121 |
X,Y=map(int,input().split())
if X==1 :
x=300000
elif X==2 :
x=200000
elif X==3 :
x=100000
else :
x=0
if Y==1 :
y=300000
elif Y==2 :
y=200000
elif Y==3 :
y=100000
else :
y=0
if X==1 and Y==1 :
print(x+y+400000)
else :
print(x+y) | x = int(input())
for i in range(1,x+1):
y = i
if y % 3 == 0:
print(" %d" % i, end="")
continue
while True:
if int(y) % 10 == 3:
print(" %d" % i, end="")
break
y /= 10
if int(y) == 0: break
print("") | 0 | null | 70,538,741,425,660 | 275 | 52 |
(n,k),p,c,*a=[[*map(int,t.split())]for t in open(0)]
while n:
n-=1;s,x,*l=0,n;f=j=k
while f:x=p[x]-1;s+=c[x];l+=s,;f=x!=n
for t in l[:k]:j-=1;a+=[t+j//len(l)*s*(s>0)]
print(max(a)) |
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
# 順列を各サイクルに分解する
used = [0] * N
ss = []
for i in range(N):
if used[i]: continue
cur = i
s = []
while used[cur] == 0:
used[cur] = 1
s.append(C[cur])
cur = P[cur] - 1 # 0-index
ss.append(s)
# 各サイクルごとに考える
res = -float("inf")
for vec in ss:
M = len(vec)
# サイクルを2周したものの累積和
accum = [0] * (2*M+1)
for i in range(M*2):
accum[i+1] = accum[i] + vec[i%M]
# amari[r] := 連続するr個の総和の最大値
amari = [-float("inf")] * M
for i in range(M):
for j in range(M):
amari[j] = max(amari[j], accum[i+j]-accum[i])
# あまりの長さで場合分け
for r in range(M):
if r > K: continue
q = (K - r) // M # ループ回数
if r == 0 and q == 0: continue
if accum[M] > 0:
res = max(res, amari[r] + accum[M] * q)
elif r > 0:
res = max(res, amari[r])
print(res)
| 1 | 5,371,271,387,800 | null | 93 | 93 |
x = int(input())
count = 0
for i in range(0, x):
a = int(input())
for j in range ( 2, a ):
c = int(a)
if a % j == 0:
count += 1
break;
if j * j > c:
break;
print(x-count)
| import math
A = []
N = int(input())
count = 0
for n in range(N):
x = int(input())
if x == 2:
A.append(x)
count += 1
elif x % 2 == 0:
pass
elif x > 2:
for i in range(3, int(math.sqrt(x))+2, 2):
if x % i == 0:
break
else:
count += 1
A.append(x)
print(count) | 1 | 10,097,295,760 | null | 12 | 12 |
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))
a,v=nm()
b,w=nm()
t=ni()
d=abs(a-b)
d2=(v-w)*t
if(d<=d2):
print('YES')
else:
print('NO')
| import string
s = raw_input()
print string.swapcase(s) | 0 | null | 8,329,811,209,502 | 131 | 61 |
from heapq import heappush, heappop
import sys
input = sys.stdin.readline
N = int(input())
pos = []
zero = []
neg = []
for _ in range(N):
S = input().rstrip()
m = s = 0
for c in S:
if c == '(':
s += 1
else:
s -= 1
m = min(m, s)
if s > 0:
heappush(pos, (-m, s)) # take larger mins first
elif s == 0:
heappush(zero, (m, s))
else:
heappush(neg, (m - s, s)) # take smaller mins first
num = 0
while pos:
m, s = heappop(pos)
if num - m < 0:
print('No')
exit()
num += s
while zero:
m, s = heappop(zero)
if num + m < 0:
print('No')
exit()
while neg:
m, s = heappop(neg)
m += s
if num + m < 0:
print('No')
exit()
num += s
if num == 0:
print('Yes')
else:
print('No')
| import sys
N, R = map(int, sys.stdin.readline().split())
if N < 10:
print(R + (10-N)*100)
else:
print(R) | 0 | null | 43,411,866,025,098 | 152 | 211 |
n = int(input())
ans = 0
for i in range(1, n + 1):
j = i
while j <= n:
ans += j
j += i
print(ans) | def main():
A = int(input())
B = int(input())
ans = 6 - A - B
print(ans)
if __name__ == "__main__":
main()
| 0 | null | 61,213,932,119,490 | 118 | 254 |
import sys
from collections import deque
mapin = lambda: map(int, sys.stdin.readline().split())
listin = lambda: list(map(int, sys.stdin.readline().split()))
inp = lambda: sys.stdin.readline()
class UnionFind():
def __init__(self,n):
self.N = n
self.par = [-1] * n
def find(self,n):
if self.par[n] < 0:
return n
else:
self.par[n] = self.find(self.par[n])
return self.par[n]
def unite(self,x,y):
x = self.find(x)
y = self.find(y)
if x == y:
return False
else:
if -self.par[x] < -self.par[y]:
x,y = y,x
self.par[x] += self.par[y]
self.par[y] = x
def size(self,x):
return -self.par[self.find(x)]
def tree_num(self):
res = 0
for i in self.par:
res += i < 0
return res
def same(self,x,y):
return self.find(x) == self.find(y)
def __str__(self):
return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
N,M = mapin()
UF = UnionFind(N)
for i in range(M):
a,b = mapin()
a,b = a - 1,b - 1
UF.unite(a,b)
ans = 0
for i in UF.par:
if i < 0:
if ans < -i:
ans = -i
print(ans)
| n, m = map(int,input().split())
lst = [-1 for i in range(n + 1)]
lst[0] = 0
def find(x):
if(lst[x] < 0):
return(x)
else:
return(find(lst[x]))
def unit(x, y):
xr = find(x)
yr = find(y)
if (xr == yr):
pass
else:
if (xr > yr):
x, y = y, x
xr, yr = yr, xr
lst[xr] = lst[xr] + lst[yr]
lst[yr] = xr
for i in range(m):
a, b = map(int,input().split())
unit(a, b)
print(-(min(lst)))
| 1 | 3,974,645,457,610 | null | 84 | 84 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.