code1
stringlengths 16
427k
| code2
stringlengths 16
427k
| similar
int64 0
1
| pair_id
int64 6.82M
181,637B
⌀ | question_pair_id
float64 101M
180,471B
⌀ | code1_group
int64 2
299
| code2_group
int64 2
299
|
---|---|---|---|---|---|---|
MOD = 10 ** 9 + 7
table_len = 10 ** 5 + 10
fac = [1, 1]
for i in range(2, table_len):
fac.append(fac[-1] * i % MOD)
finv = [0] * table_len
finv[-1] = pow(fac[-1], MOD - 2, MOD)
for i in range(table_len-1, 0, -1):
finv[i-1] = finv[i] * i % MOD
def sum_of_maxS(N, K, As):
ret = 0
for i in range(N - K + 1):
ret += ((As[i] * fac[N - i - 1]) % MOD) * \
((finv[K-1] * finv[N - K - i]) % MOD) % MOD
ret %= MOD
return ret
N, K = map(int, input().split())
As = sorted(map(int, input().split()), reverse=True)
minus_As = [-A for A in reversed(As)]
print((sum_of_maxS(N, K, As) + sum_of_maxS(N, K, minus_As)) % MOD)
|
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():
N, K, *A = map(int, read().split())
COM_MAX = N
fac, finv, inv = [0] * (COM_MAX + 1), [0] * (COM_MAX + 1), [0] * (COM_MAX + 1)
fac[0] = fac[1] = finv[0] = finv[1] = inv[1] = 1
for i in range(2, COM_MAX + 1):
fac[i] = fac[i - 1] * i % MOD
inv[i] = MOD - inv[MOD % i] * (MOD // i) % MOD
finv[i] = finv[i - 1] * inv[i] % MOD
def com(n, r):
if n < 0 or r < 0 or n < r:
return 0
return fac[n] * (finv[r] * finv[n - r] % MOD) % MOD
A.sort()
ans = 0
for i, a in enumerate(A):
ans = (ans + a * com(i, K - 1) % MOD) % MOD
ans = (ans - a * com(N - i - 1, K - 1) % MOD) % MOD
print(ans)
return
if __name__ == '__main__':
main()
| 1 | 96,175,175,597,082 | null | 242 | 242 |
import bisect
import copy
import heapq
import math
import sys
from collections import *
from itertools import accumulate, combinations, permutations, product
def input():
return sys.stdin.readline()[:-1]
def ruiseki(lst):
return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]
n,k=map(int,input().split())
a=list(map(int,input().split()))
# amari=[a[i]%k for i in range(n)]
rui=ruiseki(a)
# print(a)
# print(amari)
# print(rui)
dic=defaultdict(int)
ans=0
for i in range(n+1):
if i>=k:
dic[(rui[i-k]-(i-k))%k]-=1
ans+=dic[(rui[i]-i)%k]
dic[(rui[i]-i)%k]+=1
# print(dic)
print(ans)
|
# #
# author : samars_diary #
# 17-09-2020 │ 16:08:30 #
# #
import sys, os.path, math
#if(os.path.exists('input.txt')):
#sys.stdin = open('input.txt',"r")
#sys.stdout = open('output.txt',"w")
sys.setrecursionlimit(10 ** 5)
def mod(): return 10**9+7
def i(): return sys.stdin.readline().strip()
def ii(): return int(sys.stdin.readline())
def li(): return list(sys.stdin.readline().strip())
def mii(): return map(int, sys.stdin.readline().split())
def lii(): return list(map(int, sys.stdin.readline().strip().split()))
#print=sys.stdout.write
def solve():
n,d=mii();c=0
for _ in range(n):
a,b=mii()
if math.sqrt((a**2)+(b**2))<=d:
c+=1
print(c)
for _ in range(1):
solve()
| 0 | null | 71,681,491,347,028 | 273 | 96 |
'''
ITP-1_4-A
?????????
???????????´??° a ??¨ b ?????????????????§?????\?????????????¨??????????????????°?????????????????????????????????
a ?? b ??? d (??´??°)
a ?? b ????????? ??? r (??´??°)
a ?? b ??? f (???????°???°?????°)
???Input
1?????????????????´??° a, b ????????????????????????
???Output
d, r, f ????????????????????§????????£????????????????????????????????????f??????????????????0.00001??\????????????????????£????????????????????¨????????????
'''
# import
# ?????°??????????????????
inputData01, inputData02 = map(int,input().split())
# ??????????????´??°???????±???????
d = inputData01 // inputData02
# print( d )
# ??????????????????????±???????
r = inputData01 % inputData02
# print( r )
# ??????????????´??°???????±???????
f = inputData01 / inputData02
print( d, r, "{0:f}".format( f ))
|
import sys
n = int(sys.stdin.readline().rstrip("\n"))
if n % 2 == 0:
print(n // 2)
else:
print(n // 2 + 1)
| 0 | null | 29,891,143,446,532 | 45 | 206 |
import math
R=int(input())
cir=2*math.pi*R
print(cir)
|
import math
r=float(input())
print(2*r*math.pi)
| 1 | 31,267,026,951,982 | null | 167 | 167 |
n = int(input())
if n % 2 == 0:
print("{:.10f}".format((n/2)/n))
else:
print("{:.10f}".format((n//2+1)/n))
|
n = int(input())
cnt = 0
for i in range(n):
if (i + 1) % 2 == 1:
cnt += 1
print(cnt/n)
| 1 | 177,534,875,891,718 | null | 297 | 297 |
n = int(input())
s = input()
for i in range(1, n):
if s[0] == s[i] and s[:i] == s[i:]:
print("Yes")
break
else:
print("No")
|
X,Y,Z=map(int, input().split())
A=[Z,X,Y]
print(*A,end='')
| 0 | null | 92,200,243,266,248 | 279 | 178 |
#import time
count=0
def merge(A,left,mid,right):
global count
L=A[left:mid]+[2**30]
R=A[mid:right]+[2**30]
i=0
j=0
for k in range(left,right):
count+=1
if L[i]<=R[j]:
A[k]=L[i]
i+=1
else:
A[k]=R[j]
j+=1
def mergeSort(A,left,right):
if left+1 < right:
mid = int((left+right)/2)
mergeSort(A,left,mid)
mergeSort(A,mid,right)
merge(A,left,mid,right)
#start = time.time()
n=int(input())
s=list(map(int,input().split()))
count=0
mergeSort(s,0,n)
print(s[0],end='',sep='')
for i in range(1,n):
print(" ",s[i],end='',sep='')
print()
print(count)
#end=time.time()-start
#end*=1000
#print ("Time:{0}".format(end) + "[m_sec]")
|
n = int(input())
A = list(map(int, input().split()))
N = list(range(1, n + 1))
c = sorted(zip(A, N))
A, N = zip(*c)
print(*N)
| 0 | null | 90,036,686,094,810 | 26 | 299 |
n = int(raw_input())
points = [0, 0]
for i in xrange(n):
card_list = raw_input().split(" ")
if card_list[0] > card_list[1]:
points[0] += 3
elif card_list[0] < card_list[1]:
points[1] += 3
else:
points[0] += 1
points[1] += 1
print str(points[0]) + " " + str(points[1])
|
def solve():
N = int(input())
ans = 0
for i in range(1, N+1):
if i%3 != 0 and i%5 != 0:
ans += i
print(ans)
if __name__ == "__main__":
solve()
| 0 | null | 18,479,973,491,710 | 67 | 173 |
n,k = map(int, input().split())
alist=list(map(int, input().split()))
def is_ok(arg):
cnt=0
for i in alist:
cnt+=(i-1)//arg
return cnt<=k
def nibun(ng, ok):
while (abs(ok - ng) > 1):
mid = (ok + ng) // 2
if is_ok(mid):
ok = mid
else:
ng = mid
return ok
print(nibun(0 , 10**9 + 1))
|
N, K = map(int, input().split())
A = list(map(int, input().split()))
r = max(A)
l = 0
while l+1 < r:
mid = (l+r)//2
cnt = 0
for a in A:
if(a > mid):
if(a%mid == 0):
cnt += (a//mid)-1
else:
cnt += a//mid
if(cnt > K):
l = mid
else:
r = mid
print(r)
| 1 | 6,521,887,029,540 | null | 99 | 99 |
s = input()
if "B" in s:
print("ARC")
else:
print("ABC")
|
S = input()
ans="ABC"
if(S=="ABC"):
ans="ARC"
print(ans)
| 1 | 23,917,432,506,632 | null | 153 | 153 |
import sys
def m():
d={};input()
for e in sys.stdin:
if'f'==e[0]:print('yes'if e[5:]in d else'no')
else:d[e[7:]]=0
if'__main__'==__name__:m()
|
n = int(input())
d = {}
for i in range(n):
cmd, string = input().split()
if cmd == "insert":
d[string] = True
else:
try:
if d[string]:
print("yes")
except:
print("no")
| 1 | 75,157,943,322 | null | 23 | 23 |
class Stack():
def __init__(self):
self.stack = [0 for _ in range(1000)]
self.top = 0
def push(self, n):
self.top += 1
self.stack[self.top] = n
def pop(self):
a = self.stack[self.top]
del self.stack[self.top]
self.top -= 1
return a
def lol(self):
return self.stack[self.top]
def main():
input_data = raw_input()
data = input_data.strip().split(" ")
st = Stack()
for l in data:
if l == "+":
a = st.pop()
b = st.pop()
st.push(a + b)
elif l == "-":
a = st.pop()
b = st.pop()
st.push(b - a)
elif l == "*":
a = st.pop()
b = st.pop()
st.push(a * b)
else:
st.push(int(l))
print st.lol()
if __name__ == '__main__':
main()
|
operand = ["+", "-", "*"]
src = [x if x in operand else int(x) for x in input().split(" ")]
stack = []
for s in src:
if isinstance(s, int):
stack.append(s)
elif s == "+":
b, a = stack.pop(), stack.pop()
stack.append(a+b)
elif s == "-":
b, a = stack.pop(), stack.pop()
stack.append(a-b)
elif s == "*":
b, a = stack.pop(), stack.pop()
stack.append(a*b)
print(stack[0])
| 1 | 36,863,685,702 | null | 18 | 18 |
dct = {
0: (0, 0, 1),
5: (0, 0, -1),
2: (0, 1, 0),
3: (0, -1, 0),
1: (1, 0, 0),
4: (-1, 0, 0)
}
def calc(x, y):
res = (
x[1] * y[2] - x[2] * y[1],
x[2] * y[0] - x[0] * y[2],
x[0] * y[1] - x[1] * y[0]
)
return res
if __name__ == "__main__":
a = [int(x) for x in input().split()]
Q = int(input())
for _ in range(Q):
x, y = map(int, input().split())
for i, value in enumerate(a):
if x == value:
x_ind = i
elif y == value:
y_ind = i
s, t = dct[x_ind], dct[y_ind]
res = calc(s,t)
for key in dct:
if dct[key] == res:
print(a[key])
break
|
result = [(temp1 + str(temp2)) for temp1 in ('S ','H ','C ','D ') for temp2 in range(1,14)]
for check in range(int(input())) :
result.remove(input())
for temp in result :
print(temp)
| 0 | null | 664,790,991,062 | 34 | 54 |
import sys
def main():
input = sys.stdin.readline
n, k = map(int, input().split())
a = [int(x) for x in input().split()]
f = [int(x) for x in input().split()]
a.sort(reverse=True)
f.sort()
l, r = -1, pow(10, 12)+1
while r-l > 1:
judge = (r+l)//2
sub = 0
for i in range(n):
if a[i]*f[i] <= judge:
continue
sub += a[i]-judge//f[i]
if sub <= k:
r = judge
else:
l = judge
print(r)
if __name__ == "__main__":
main()
|
from itertools import combinations
def is_achievable(M, X, A):
result = [0] * M
for a_row in A:
for i, a in enumerate(a_row):
result[i] += a
for n in result:
if n < X:
return False
return True
def main():
N, M, X = map(int, input().split())
C = [0] * N
A = []
for i in range(N):
query = list(map(int, input().split()))
C[i] = query[0]
A.append(query[1:])
if not is_achievable(M, X, A):
print(-1)
return
index_C = [ i for i in range(N) ]
min_cost = 10 ** 10
for i in range(len(index_C)+1):
for comb in combinations(index_C, i):
cur_A = []
cur_cost = 0
for i in comb:
cur_A.append(A[i])
cur_cost += C[i]
if is_achievable(M, X, cur_A):
min_cost = min(min_cost, cur_cost)
print(min_cost)
main()
| 0 | null | 93,497,864,156,358 | 290 | 149 |
A,B,N = map(int,input().split())
if N >= B-1:
i = B-1
ans = A*i//B - A*(i//B)
else:
ans = A*N//B - A*(N//B)
print(ans)
|
n = int(input())
s = input()
ans = ''
for c in s:
ans += chr((ord(c) - ord("A") + n) % 26 + ord("A"))
print(ans)
| 0 | null | 81,065,000,219,632 | 161 | 271 |
n, k = map(int, input().split())
a = list(map(int, input().split()))
s = [0] * (n+1)
for i in range(n):
s[i+1] = s[i] + a[i]
s[i] = (s[i] - i) % k
s[n] = (s[n] - n) % k
count = dict()
for i in range(min(k-1, n+1)):
if not s[i] in count:
count[s[i]] = 0
count[s[i]] += 1
ans = 0
for i in range(k-1, n+1):
if not s[i] in count:
count[s[i]] = 0
count[s[i]] += 1
count[s[i-k+1]] -= 1
ans += count[s[i-k+1]]
for i in count.values():
ans += i * (i - 1) // 2
print(ans)
|
n,k=map(int,input().split())
a=list(map(lambda x:(int(x)-1)%k,input().split()))
s=[0]
for i in a:
s.append((s[-1]+i)%k)
mp={}
ans=0
for i in range(len(s)):
if i-k>=0:
mp[s[i-k]]-=1
if s[i] in mp:
ans+=mp[s[i]]
mp[s[i]]+=1
else:
mp[s[i]]=1
print(ans)
| 1 | 137,403,838,003,640 | null | 273 | 273 |
import sys
input = sys.stdin.readline
n, (s, t) = int(input()), input()[:-1].split()
print(''.join(s[i] + t[i] for i in range(n)))
|
N = int(input())
S, T = input().split()
assert len(S) == N
assert len(T) == N
print(''.join([s + t for s, t in zip(S, T)]))
| 1 | 112,236,765,810,202 | null | 255 | 255 |
def make_text():
temp = []
while True:
row = input().split()
if row == ['END_OF_TEXT']:
break
temp.append(list(map(lambda x: x.lower(), row)))
return temp
def find_word(word,text):
num = 0
for row in text:
for t in row:
if t == word:
num += 1
return num
if __name__ == '__main__':
w = input()
t = make_text()
num = find_word(w,t)
print(num)
|
A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
xyc = [map(int, input().split()) for _ in range(M)]
x, y, c = [list(x) for x in zip(*xyc)]
m = min(a)+min(b)
for i in range(M):
tmp = a[x[i]-1]+b[y[i]-1]-c[i]
m = min(m, tmp)
print(m)
| 0 | null | 28,095,320,995,532 | 65 | 200 |
A,B,C = map(int, input().split())
K = int(input())
for i in range(K):
if B >= C or A >= C:
C *= 2
else:
if A >= B:
B *= 2
if C > B and B > A:
print("Yes")
else:
print("No")
|
a,b=map(int,input().split())
print('%d %d %.10f' % (a//b,a%b,a/b))
| 0 | null | 3,726,230,446,890 | 101 | 45 |
import sys
read = sys.stdin.read
readline = sys.stdin.buffer.readline
from itertools import combinations_with_replacement
INF = float('inf')
def main():
N, M, Q = map(int, readline().split())
abcd = []
for _ in range(Q):
a,b,c,d = map(int, readline().split())
abcd.append([a,b,c,d])
As = combinations_with_replacement(range(1,M+1),N)
ans = 0
for A in As:
score = 0
for a,b,c,d in abcd:
if A[b-1] - A[a-1] == c:
score += d
ans = max(ans, score)
print(ans)
if __name__ == '__main__':
main()
|
S = str(input())
s = list(map(str,S))
if s[2] == s[3] and s[4] == s[5]:
print('Yes')
else:
print('No')
| 0 | null | 34,627,317,503,520 | 160 | 184 |
x = input()
ans = 1 if x == "0" else 0
print(ans)
|
n = int(input())
print(0 if n == 1 else 1)
| 1 | 2,976,763,840,840 | null | 76 | 76 |
import sys
input = sys.stdin.readline
from bisect import *
H, W, K = map(int, input().split())
S = [input()[:-1] for _ in range(H)]
ans = [[-1]*W for _ in range(H)]
now = 1
l = []
for i in range(H):
cnt = 0
for j in range(W):
if S[i][j]=='#':
cnt += 1
if cnt==0:
continue
l.append(i)
c = 0
for j in range(W):
ans[i][j] = now
if S[i][j]=='#':
c += 1
if c<cnt:
now += 1
now += 1
for i in range(H):
if '#' not in S[i]:
j = bisect_left(l, i)
if j==len(l):
for k in range(W):
ans[i][k] = ans[l[-1]][k]
else:
for k in range(W):
ans[i][k] = ans[l[j]][k]
for ans_i in ans:
print(*ans_i)
|
import numpy as np
H,N= map(int, input().split())
m = np.array([list(map(int, input().split())) for _ in range(N)])
max_a = np.max(m[:,0])
dp = np.zeros(H+max_a+1, dtype='i8')
for i in range(max_a+1, H+max_a+1):
dp[i] = np.min(dp[i-m[:,0]] + m[:,1])
print(dp[H+max_a])
| 0 | null | 112,425,941,677,728 | 277 | 229 |
n,k,c = map(int,raw_input().split())
s = raw_input()
most = [0]*(n+1)
i = n
days = 0
while i > 0:
if s[i-1] == 'o':
days += 1
for j in xrange(min(max(1,c),i-1)):
#print days
#print i,j,1,days
most[i-j-1] = days
i -= (c+1)
else:
i -= 1
if i<n:
most[i] = most[i+1]
remain = k
i = 1
while i<=n:
if s[i-1] == 'o':
if most[i] < remain:
print i
remain -= 1
i += c+1
else:
i += 1
|
#!usr/bin/env python3
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def S():
res = list(sys.stdin.readline())
if res[-1] == "\n":
return res[:-1]
return res
def IR(n):
return [I() for i in range(n)]
def LIR(n):
return [LI() for i in range(n)]
def SR(n):
return [S() for i in range(n)]
def LSR(n):
return [LS() for i in range(n)]
sys.setrecursionlimit(1000000)
mod = 1000000007
def solve():
a, b, c = LI()
k = I()
for i in range(k):
if a >= b:
b *= 2
elif b >= c:
c *= 2
if a < b < c:
print('Yes')
else:
print('No')
return
# Solve
if __name__ == "__main__":
solve()
| 0 | null | 23,777,070,173,750 | 182 | 101 |
while 1:
x,y = map( int , raw_input().split() )
if x == 0 and y == 0:
break
elif x <= y:
print "%d %d" %(x,y)
elif y < x :
print "%d %d" %(y,x)
|
while 1:
a,b = sorted(map(int,raw_input().split()))
if a==b==0:
break
print ('{} {}'.format(a,b))
| 1 | 521,523,664,260 | null | 43 | 43 |
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
a,b,n = LI()
ans = 0
def f(a,b,x):
ans = (a*x)//b - a*(x//b)
return ans
if b-1<=n:
ans = f(a,b,b-1)
else:
ans = f(a,b,n)
print(ans)
main()
|
a,b,n = map(int,input().split())
x=min(n,b-1)
ans = (int(a*x/b) - a * int(x/b))
print(ans)
| 1 | 28,154,243,502,348 | null | 161 | 161 |
A1 = list(map(int, input().split()))
A2 = list(map(int, input().split()))
A3 = list(map(int, input().split()))
N = int(input())
B = [int(input()) for i in range(N)]
result = ''
for b in B:
if b in A1:
a = A1.index(b)
A1.remove(b)
A1.insert(a, 0)
elif b in A2:
a = A2.index(b)
A2.remove(b)
A2.insert(a, 0)
elif b in A3:
a = A3.index(b)
A3.remove(b)
A3.insert(a, 0)
else:
pass
if A1.count(0) == 3:
result ='y'
elif A2.count(0) == 3:
result ='y'
elif A3.count(0) == 3:
result ='y'
for m in range(3):
if A1[m] == 0 and A2[m] == 0 and A3[m] == 0:
result ='y'
else:
pass
if A1[0] == 0 and A2[1] == 0 and A3[2] == 0:
result = 'y'
elif A1[2] == 0 and A2[1] == 0 and A3[0] == 0:
result = 'y'
if result == 'y':
print('Yes')
else:
print('No')
|
# -*- 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
@mt
def slv(N, K, LR):
M = 998244353
memo = [0] *(N+2)
memo[1] = 1
memo[1+1] = -1
for i in range(1, N+1):
memo[i] += memo[i-1]
memo[i] %= M
for l, r in LR:
ll = min(N+1, i+l)
rr = min(N+1, i+r+1)
memo[ll] += memo[i]
memo[ll] %= M
memo[rr] -= memo[i]
memo[rr] %= M
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 | 31,488,821,709,440 | 207 | 74 |
from itertools import combinations_with_replacement
N, M, Q = map(int, input().split())
T = []
for q in range(Q):
T.append(list(map(int, input().split())))
A = list(combinations_with_replacement(list(range(1, M+1)), N))
Alist = [list(a) for a in A]
#print(Alist)
Max = 0
for a in Alist:
cost = 0
for t in T:
if a[t[1]-1] - a[t[0]-1] == t[2]:
cost += t[3]
if cost > Max:
Max = cost
print(Max)
|
from collections import defaultdict
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
pc_memo = defaultdict(int)
memo = defaultdict(int)
def to_next(x):
if x not in pc_memo.keys():
pc_memo[x] = x % "{:b}".format(x).count('1')
return pc_memo[x]
def f(x):
if x == 0:
return 0
if x not in memo.keys():
memo[x] = f(to_next(x))+1
return memo[x]
for i in range(2*10**5):
f(i)
n = int(input())
x = input().rstrip()
one_cnt = x.count('1')
mod_minus = 0
mod_plus = 0
for i, a in enumerate(reversed(x)):
if a == '1':
if 0 < one_cnt-1:
mod_minus += pow(2, i, one_cnt-1)
mod_minus %= one_cnt-1
mod_plus += pow(2, i, one_cnt+1)
mod_plus %= one_cnt+1
for i in range(n):
if x[i] == '0':
nx = mod_plus+pow(2, n-i-1, one_cnt+1)
nx %= one_cnt+1
print(f(nx)+1)
else:
if 0 < one_cnt-1:
nx = mod_minus-pow(2, n-i-1, one_cnt-1)
nx %= one_cnt-1
print(f(nx)+1)
else:
print(0)
| 0 | null | 17,748,518,156,018 | 160 | 107 |
N = int(input())
s = 1
t = 0
while N > 26**s + t:
t += 26**s
s += 1
N -= t
ans = []
for i in range(s):
c = 0
while N > 26**(s-i-1):
N -= 26**(s-i-1)
c += 1
ans.append(chr(97+c))
print("".join(ans))
|
ring = input() * 2
s = input()
if s in ring:
print('Yes')
else:
print('No')
| 0 | null | 6,876,558,461,600 | 121 | 64 |
N=int(input())
X=input()
popcount=X.count('1')
bi1=[]
bi0=[]
Xnum1 = 0
Xnum0 = 0
if popcount != 1:
for i in range(N):
if i==0: bi1.append(1)
else: bi1.append(bi1[i-1]*2%(popcount-1))
Xnum1 = (Xnum1 + int(X[N-i-1])*bi1[i])%(popcount-1)
for i in range(N):
if i==0: bi0.append(1)
else: bi0.append(bi0[i-1]*2%(popcount+1))
Xnum0 = (Xnum0 + int(X[N-i-1])*bi0[i])%(popcount+1)
for i in range(N):
if popcount==1 and X[i]=='1':
print(0)
continue
if X[i]=='1':
Xi = (Xnum1 - bi1[N-i-1])%(popcount-1)
else:
Xi = (Xnum0 + bi0[N-i-1])%(popcount+1)
ans = 1
while Xi!=0:
Xi = Xi % bin(Xi).count('1')
ans += 1
print(ans)
|
# coding: utf-8
# Your code here!
n=int(input())
x=input()
ones=x.count("1")
a=ones+1
b=ones-1
xda=0
xdb=0
for i in range(n):
if x[-1-i]=="1":
xda+=pow(2,i,a)
xda%=a
if b==0:
continue
xdb+=pow(2,i,b)
xdb%=b
xds=[-1]*n
for i in range(n):
if x[-1-i]=="0":
xds[-1-i]=(xda+pow(2,i,a))%a
else:
if b==0:
continue
xds[-1-i]=(xdb-pow(2,i,b))%b
def popcount(x):
return bin(x).count("1")
def solve(x):
if x<0:
return -1
if x==0:
return 0
cnt=0
while x>0:
cnt+=1
x=x%popcount(x)
return cnt
for xd in xds:
print(1+solve(xd))
| 1 | 8,161,330,017,440 | null | 107 | 107 |
N,K = map(int,input().split())
def solve(n,k):
n %= k
return min(n,abs(n-k))
print(solve(N,K))
|
# C Replacing Integer
N, K = map(int, input().split())
while N >= K:
N = N % K
frag = 0
while frag == 0:
cand = abs(N - K)
if cand < N:
N = cand
else:
frag = 1
print(N)
| 1 | 39,556,580,671,608 | null | 180 | 180 |
# 解説を参考に作成
def solve(N, X, Y):
ans = [0] * (N)
for i in range(1, N):
for j in range(i + 1, N + 1):
k = min(abs(j - i), abs(X - i) + 1 + abs(Y - j))
ans[k] += 1
for i in ans[1:]:
print(i)
if __name__ == '__main__':
N, X, Y = map(int, input().split())
solve(N, X, Y)
|
#6回目、2020-0612
#2重ループ +O(1)
#i,jがループの中か外で場合分け
#初期入力
N, x, y = map(int, input().split())
ans ={i:0 for i in range(1,N)}
for i in range(1,N):
for j in range(i+1,N+1):
#(i,jがループより共に左、ともに右)
if (j <=x or y <=i) :
ans[j-i] +=1
# (iがループより左 and jがループより右)
elif i <=x and y <=j:
ans[x-i +1 +j-y] +=1
# i,jが共にループの中
elif x <i and j <y:
dist =min(j-i,i-x + y-j +1) #時計回りと反時計回りの短いほう、+1はxとyを結んだ部分
ans[dist] +=1
# iがループより左、jがループの中
elif i <=x and j < y:
dist =x-i + min(j-x , y+1-j)
ans[dist] +=1
# iがループの中、jがループの右
elif x <i and y <=j:
dist =min(y-i , 1 +i-x) + j-y
ans[dist] +=1
#答え出力
for i in range(1,N):
print(ans[i])
| 1 | 44,168,018,589,744 | null | 187 | 187 |
a = int(input());
print(a+a**2+a**3);
|
s = int(input())
a = [-1]*(s+10)
mod = 10**9 + 7
a[0] = 1
a[1] = 0
a[2] = 0
a[3] = 1
if s > 3:
for i in range (4,s+1):
a[i] = (a[i-1]+a[i-3])%mod
print(a[s])
| 0 | null | 6,765,373,731,478 | 115 | 79 |
N,M = list(map(int,input().split()))
a = list(map(int,input().split()))
a.sort(reverse=True)
if a[M-1]<sum(a)/(4*M):
print("No")
else:
print("Yes")
|
import sys, math
from functools import lru_cache
sys.setrecursionlimit(10**9)
MOD = 10**9+7
def input():
return sys.stdin.readline()[:-1]
def mi():
return map(int, input().split())
def ii():
return int(input())
def i2(n):
tmp = [list(mi()) for i in range(n)]
return [list(i) for i in zip(*tmp)]
def main():
N = ii()
x, y = i2(N)
Mw = max(x[i]+y[i] for i in range(N))
mw = min(x[i]+y[i] for i in range(N))
Mz = max(x[i]-y[i] for i in range(N))
mz = min(x[i]-y[i] for i in range(N))
print(max(Mz-mz, Mw-mw))
if __name__ == '__main__':
main()
| 0 | null | 21,122,850,889,700 | 179 | 80 |
def gacha():
# 入力
N = int(input())
S = [input() for _ in range(N)]
# 重複排除
freebie_tuple = set(S)
# 景品の種類
return len(freebie_tuple)
result = gacha()
print(result)
|
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
BC = [list(map(int, input().split())) for _ in range(Q)]
counter = Counter(A)
counter_sum = sum(k*v for k, v in counter.items())
for b, c in BC:
counter[c] += counter[b]
counter_sum -= b * counter[b]
counter_sum += c * counter[b]
counter[b] = 0
print(counter_sum)
| 0 | null | 21,333,913,336,052 | 165 | 122 |
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")
|
n,m,q = map(int,input().split())
l = []
def dfs(a):
if len(a)==n: l.append(a)
else:
for i in range(a[-1],m+1):
dfs(a+[i])
for i in range(m):
dfs([i+1])
Query = [list(map(int,input().split())) for _ in range(q)]
ans = 0
for li in l:
tmp = 0
for a,b,c,d in Query:
if li[b-1]-li[a-1]==c:
tmp += d
ans = max(ans,tmp)
print(ans)
| 0 | null | 100,874,832,281,132 | 295 | 160 |
#!/usr/bin/env python3
from collections import Counter
def main():
S = input()
N = len(S)
T = [0] * (N+1)
for i in range(N-1,-1,-1):
T[i] = (T[i+1] + int(S[i]) * pow(10,N-i,2019)) % 2019
l = Counter(T)
ans = 0
for v in l.values():
ans += v * (v-1) // 2
print(ans)
if __name__ == "__main__":
main()
|
def resolve():
N = int(input())
S = str(input())
print(S.count('ABC'))
return
resolve()
| 0 | null | 65,124,182,955,952 | 166 | 245 |
N = int(input())
S = input()
A = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
ans = ''
for s in S:
ans += A[(A.index(s)+N)%26]
print(ans)
|
import sys
for i in sys.stdin.readlines():
nums = list(map(int,i.split()))
h = nums[0]
w = nums[1]
if h == 0 and w == 0:
break
for j in range(1,h+1):
if h == 1 and w == 1:
print("#")
elif j % 2 == 1:
if w % 2 == 1:
x = int((w-1)/2)
print("#."*x +"#")
else:
x = int(w/2)
print("#."*x)
elif j % 2 == 0:
if w % 2 == 1:
x = int((w-1)/2)
print(".#"*x +".")
else:
x = int(w/2)
print(".#"*x)
print("")
| 0 | null | 68,067,370,142,240 | 271 | 51 |
S, T = [input() for _ in range(2)]
count = 0
for i, j in zip(S, T):
count += i != j
print(count)
|
N=list(input())
M=list(input())
ans=0
number=len(N)
for i in range(number):
if N[i]==M[i]:
ans+=1
print(number-ans)
| 1 | 10,496,221,912,448 | null | 116 | 116 |
# import itertools
# import math
# import sys
# sys.setrecursionlimit(500*500)
import numpy as np
# import heapq
# from collections import deque
# N = int(input())
# S = input()
# n, *a = map(int, open(0))
N, K = map(int, input().split())
# A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()
# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)
# def dfs(tree, s):
# for l in tree[s]:
# if depth[l[0]] == -1:
# depth[l[0]] = depth[s] + l[1]
# dfs(tree, l[0])
# dfs(tree, 1)
# def factorization(n):
# arr = []
# temp = n
# for i in range(2, int(-(-n**0.5//1))+1):
# if temp%i==0:
# cnt=0
# while temp%i==0:
# cnt+=1
# temp //= i
# arr.append([i, cnt])
# if temp!=1:
# arr.append([temp, 1])
# if arr==[]:
# arr.append([n, 1])
# return arr
A = [i for i in range(N + 1)]
A = np.array(A)
cum_A = np.cumsum(A)
cnt = 0
for i in range(K, N + 1):
cnt += (cum_A[N] - cum_A[N - i]) - cum_A[i - 1] + 1
print((cnt + 1) % (10**9 + 7))
|
po=pow(10,9)+7
n,k=map(int,input().split())
cc=0
for i in range(k,n+2):
cc+=(n*i-i*(i-1)+1)%po
print(cc%po)
| 1 | 33,225,333,254,488 | null | 170 | 170 |
import sys
for x in sys.stdin:
a, op, b = x.split()
a = int(a)
b = int(b)
if op == '?':
break
elif op == '+':
print(a + b)
elif op == '-':
print(a - b)
elif op == '*':
print(a * b)
elif op == '/':
print(a // b)
|
import numpy as np
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
from numba import njit
def getInputs():
D = int(readline())
CS = np.array(read().split(), np.int32)
C = CS[:26]
S = CS[26:].reshape((-1, 26))
return D, C, S
def _compute_score(output, i, d, last):
mask = np.ones((26, ))
mask[i] = 0
score = S[d][i] - np.sum(C * (d + 1 - last) * mask)
return score
def solve():
output = []
last = np.zeros((26, ))
#SCORE = 0
for d in range(D):
max_score = float('-inf')
best_i = 0
for i in range(26):
output.append(i)
score = _compute_score(output, i, d, last)
if max_score < score:
max_score = score
best_i = i + 1
output.pop()
output.append(best_i)
last[best_i - 1] = d + 1
#SCORE += max_score
#print(SCORE)
return output
if __name__ == "__main__":
D, C, S = getInputs()
ans = solve()
print('\n'.join(map(str, ans)))
| 0 | null | 5,141,785,398,930 | 47 | 113 |
import sys
n, m = map(int, input().split())
s = input()
p = n
hist = []
while p>0:
for i in range(max(0,p-m), p):
if s[i]=='0':
hist.append(p-i)
p = i
break
else:
print(-1)
sys.exit()
print(*hist[::-1])
|
n = int(input())
a = []
for i in range(n):
temp = []
temp_temp = []
temp.append(int(input()))
for j in range(temp[0]):
temp_temp.append(list(map(int,input().split())))
temp.append(temp_temp)
a.append(temp)
ans = 0
for i in range(2**n):
ans_t = 0
id_list =list(bin(i))[2:]
id_list = [int(x) for x in id_list]
while len(id_list) != n:
id_list.insert(0,0)
for j in range(n):
flag = 0
if id_list[j] == 0:
continue
else:
for k in range(a[j][0]):
if id_list[a[j][1][k][0]-1] != a[j][1][k][1]:
flag = 1
break
if flag == 1:
ans_t = 0
break
else:
ans_t += 1
if ans_t > ans:
ans = ans_t
print(ans)
| 0 | null | 130,897,864,763,840 | 274 | 262 |
import sys,collections
input = sys.stdin.readline
N=input().rstrip()
A=list(map(int,input().split()))
Ac = collections.Counter(A)
Q = int(input())
for i in range(Q):
B,C = list(map(int,input().split()))
nb = Ac[B]
Ac[C] += Ac[B]
Ac[B] = 0
if i==0:
ans = 0
for key,val in Ac.items():
ans += key*val
else:
ans +=nb*C - nb*B
print(ans)
|
N,M = map(int,input().split())
A_ls = list(map(int,input().split()))
cnt = 0
vote = sum(A_ls) / (4 * M)
for i in range(N):
if A_ls[i] >= vote:
cnt += 1
print("Yes" if cnt >= M else "No")
| 0 | null | 25,557,719,977,472 | 122 | 179 |
S=int(input())
ans=[0]*(S+1)
MOD=10**9+7
if S<=2:
print(0)
exit(0)
elif S==3:
print(1)
exit(0)
ans[3]=1
for i in range(4,S+1):
ans[i]=ans[i-1]+ans[i-3]
print(ans[i]%MOD)
|
def main():
k = int(input())
seven = 7
already = set()
cnt = 0
if k%2==0:
print(-1)
return
while seven != 0:
if seven in already:
print(-1)
return
seven %= k
already.add(seven)
cnt += 1
if seven != 0:
seven = seven * 10 + 7
print(cnt)
return
if __name__ == '__main__':
main()
| 0 | null | 4,757,277,153,060 | 79 | 97 |
def resolve():
H, A = list(map(int, input().split()))
import math
print(math.ceil(H/A))
if '__main__' == __name__:
resolve()
|
H, A = [int(s) for s in input().split()]
print(H // A + (H % A != 0))
| 1 | 77,143,481,336,100 | null | 225 | 225 |
print(f'{"Yes" if int(input()) >= 30 else "No"}')
|
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = 0
for i in (a):
if i >= k:
b += 1
else:
b += 0
print(b)
| 0 | null | 92,770,913,864,900 | 95 | 298 |
import math
r = int(input())
print(int(r * r))
|
from collections import defaultdict as dd
N, K = map(int, input().split())
A = list(map(int, input().split()))
S = [0]*(N+1)
# 累積和
for i in range(1, N + 1):
S[i] = S[i - 1] + A[i - 1] - 1
S[i] %= K
B = dd(int) # ここで範囲内のS[i]-iの個数を数えていく。
cnt = 0
for j in range(1, N + 1):
B[S[j - 1]] += 1
if j - K >= 0:
B[S[j - K]] -= 1
cnt += B[S[j]]
print(cnt)
| 0 | null | 141,151,020,631,280 | 278 | 273 |
n = int(input())
s = str(input())
w = s[0]
num = 0
for i in range(1,n):
if w[0] == s[i]:
w += s[i]
else:
num += 1
w = s[i]
num += 1
print(num)
|
n,m=map(int,input().split())
data=[0]*n
count,wa=0,0
for i in range(m):
p,a=input().split()
p=int(p)-1
if type(data[p]) is int:
if a=='WA':
data[p]+=1
else:
wa+=data[p]
data[p]=''
count+=1
print(count,wa)
| 0 | null | 131,070,072,718,152 | 293 | 240 |
n=int(input())
xl=[list(map(int, input().split())) for _ in range(n)]
rl=[]
for x,l in xl:
right=x-l
left=x+l
rl.append([right,left])
rl.sort(key=lambda x: x[1])
# print(rl)
cnt=0
pos=-float('inf')
for i in range(n):
if rl[i][0]>=pos:
pos=rl[i][1]
cnt+=1
print(cnt)
|
from collections import Counter
N = int(input())
X = list(map(int, input().split()))
ctr = Counter(X)
ans = 0
for v in ctr.values():
ans += v * (v - 1) // 2
for i in range(N):
val = ctr[X[i]]
print(ans - val * (val - 1) // 2 + (val - 1) * (val - 2) // 2)
| 0 | null | 68,899,873,095,840 | 237 | 192 |
import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))
n=int(input())
a=lmp()
ans=0
for i in range(n-1):
for j in range(i+1,n):
ans+=a[i]*a[j]
print(ans)
|
n = int(input())
oi = list(map(int,input().split()))
total = 0
for i in range(n-1):
for j in range(i+1,n):
total += oi[i]*oi[j]
print(total)
| 1 | 168,591,828,205,302 | null | 292 | 292 |
import bisect
n = int(input())
l = list(map(int, input().split()))
l.sort()
cnt = 0
for i in range(n):
for j in range(i+1, n):
if j == n-1:
continue
x = l[i] + l[j]
cnt += bisect.bisect_left(l[j+1:], x)
print(cnt)
|
ii = lambda:int(input())
mi = lambda:list(map(int,input().split()))
ix = lambda x:list(input() for _ in range(x))
mix = lambda x:list(mi() for _ in range(x))
iix = lambda x:list(int(input()) for _ in range(x))
##########
def resolve():
h1,m1,h2,m2,k = mi()
res = convert(h2,m2) - convert(h1,m1) - k
print(res)
def convert(h,m):
return h*60 + m
if __name__ == "__main__":
resolve()
| 0 | null | 95,146,192,332,448 | 294 | 139 |
a,b = map(int,input().split())
print(a//b,a%b,"{:.5f}".format(a/b))
|
from sys import stdin,stdout
def INPUT():return list(int(i) for i in stdin.readline().split())
def inp():return stdin.readline()
def out(x):return stdout.write(x)
import math
import random
J=998244353
########################################################
n,k=INPUT()
A=[]
for i in range(k):
x,y=INPUT()
A.append([x,y])
A.sort()
s,e=-1,-1
S=[]
for i in range(len(A)):
if i==0:
S.append([A[i][0],A[i][1]])
else:
if A[i][0]>S[-1][1]:
S.append([A[i][0],A[i][1]])
else:
S[-1][1]=A[i][1]
cum=[0]*(n+1)
dp=[0]*(n+1)
dp[1]=1
cum[1]=1
for i in range(2,n+1):
for ele in S:
x=ele[0]
y=ele[1]
dp[i]=(dp[i]+cum[max(i-x,0)]-cum[max(0,i-y-1)])%J
cum[i]=(cum[i-1]+dp[i])%J
print(dp[n])
| 0 | null | 1,644,654,359,700 | 45 | 74 |
import sys;print"\n".join("%d %d"%(lambda x:(x[2],x[0]/x[2]*x[1]))((lambda f,x:(x[0],x[1],f(f,x[0],x[1])))(lambda f,a,b:a%b==0 and b or f(f,b,a%b),map(int,s.split(' '))))for s in sys.stdin)
|
import sys
def gcd(x, y):
if y == 0:
return x
else:
return gcd(y, x%y)
def lcm(x, y):
return x * y //gcd(x,y)
for line in sys.stdin:
x, y = map(int, line.split())
print(gcd(x, y), lcm(x,y))
| 1 | 755,884,932 | null | 5 | 5 |
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
n = map(int, raw_input().split())
a = n[0]
b = n[1]
c = n[2]
if(a < b and b < c):
print "Yes"
else:
print "No"
|
import sys
for line in sys.stdin:
nums = list(map(int, line.split()))
res = "Yes" if nums[0] < nums[1] < nums[2] else "No"
print(res)
| 1 | 398,662,420,436 | null | 39 | 39 |
n = int(input())
l = {}
for _ in range(n):
ss = input().split()
order, string = ss[0], ss[1]
if order == "insert":
l[string] = 1
else:
print('yes' if string in l else 'no')
|
i = raw_input().strip().split()
a = int(i[0])
b = int(i[1])
c = int(i[2])
if a < b and b < c:
print "Yes"
else:
print "No"
| 0 | null | 231,733,058,270 | 23 | 39 |
n,m,l =map(int,input().split())
a =[]
b=[]
for i in range(n):
a.append(list(map(int,input().split())))
for j in range(m):
b.append(list(map(int,input().split())))
for i in range(n):
li=[]
for j in range(l):
su =0
for k in range(m):
su += a[i][k] * b[k][j]
li.append(su)
print(" ".join(list(map(str,li))))
|
def readinput():
n,k=map(int,input().split())
lr=[]
for _ in range(k):
l,r=map(int,input().split())
lr.append((l,r))
return n,k,lr
def main(n,k,lr):
lrs=sorted(lr,key=lambda x:x[0])
MOD=998244353
dp=[0]*(n+1)
dp[1]=1
for i in range(1,n):
#print(i)
if dp[i]==0:
continue
skip=False
for l,r in lrs:
for j in range(l,r+1):
#print('i+j: {}'.format(i+j))
if i+j>n:
skip=True
break
dp[i+j]=(dp[i+j]+dp[i])%MOD
#print(dp)
if skip:
break
return dp[n]
def main2(n,k,lr):
lrs=sorted(lr,key=lambda x:x[0])
MOD=998244353
dp=[0]*(n+1)
ruiseki=[0]*(n+1)
dp[1]=1
ruiseki[1]=1
for i in range(2,n+1):
for l,r in lrs:
if i-l<1:
break
dp[i]+=ruiseki[i-l]-ruiseki[max(1,i-r)-1]
dp[i]=dp[i]%MOD
ruiseki[i]=(ruiseki[i-1]+dp[i])%MOD
return dp[n]
if __name__=='__main__':
n,k,lr=readinput()
ans=main2(n,k,lr)
print(ans)
| 0 | null | 2,077,041,175,420 | 60 | 74 |
class BIT:
def __init__(self, n):
self.n = n
self.bit = [0]*(self.n+1) # 1-indexed
def init(self, init_val):
for i, v in enumerate(init_val):
self.add(i, v)
def add(self, i, x):
# i: 0-indexed
i += 1 # to 1-indexed
while i <= self.n:
self.bit[i] += x
i += (i & -i)
def sum(self, i, j):
# return sum of [i, j)
# i, j: 0-indexed
return self._sum(j) - self._sum(i)
def _sum(self, i):
# return sum of [0, i)
# i: 0-indexed
res = 0
while i > 0:
res += self.bit[i]
i -= i & (-i)
return res
class RangeAddBIT:
def __init__(self, n):
self.n = n
self.bit1 = BIT(n)
self.bit2 = BIT(n)
def init(self, init_val):
self.bit2.init(init_val)
def add(self, l, r, x):
# add x to [l, r)
# l, r: 0-indexed
self.bit1.add(l, x)
self.bit1.add(r, -x)
self.bit2.add(l, -x*l)
self.bit2.add(r, x*r)
def sum(self, l, r):
# return sum of [l, r)
# l, r: 0-indexed
return self._sum(r) - self._sum(l)
def _sum(self, i):
# return sum of [0, i)
# i: 0-indexed
return self.bit1._sum(i)*i + self.bit2._sum(i)
import sys
import io, os
#input = sys.stdin.buffer.readline
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
def main():
n, d, a = map(int, input().split())
XH = []
for i in range(n):
x, h = map(int, input().split())
XH.append((x-d, h))
XH.sort()
X = []
H = []
for x, h in XH:
X.append(x)
H.append(h)
bit = RangeAddBIT(n+1)
bit.init(H)
import bisect
ans = 0
for i in range(n):
h = bit.sum(i, i+1)
if h > 0:
q = (h+a-1)//a
ans += q
j = bisect.bisect_right(X, X[i]+2*d)
bit.add(i, j, -q*a)
print(ans)
if __name__ == '__main__':
main()
|
#C - Replacing Intege
N,K = map(int, input().split())
for i in range(N):
if N % K == 0:
N = 0
break
N = N % K
if abs(N-K) < N:
N = abs(N-K)
else :
break
print(N)
| 0 | null | 61,065,165,979,980 | 230 | 180 |
def solve():
dp = [0] * N
for i in range(N):
dp[A[i]-1] = i+1
for i in range(N):
print(dp[i], end=" ")
if __name__ == "__main__":
N = int(input())
A = list(map(int, input().split()))
solve()
|
N = int(input())
A = list(map(int,input().split()))
B = [0]*N
for n in range(N):
B[A[n]-1] = n+1
print(*B)
| 1 | 180,971,280,335,608 | null | 299 | 299 |
if __name__ == "__main__":
n = int(input())
taro, hanako = 0, 0
for _ in range(n):
tw, hw = input().split()
if tw > hw:
taro += 3
elif tw < hw:
hanako += 3
else:
taro += 1
hanako += 1
print(taro, hanako)
|
n, m = map(int,input().split())
a = list(map(int,input().split()))
b = 0
for i in a:
b = b + i
if n >= b:
print(n - b)
if n < b:
print( -1 )
| 0 | null | 17,002,370,165,992 | 67 | 168 |
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("")
|
H, W, K = map(int, input().split())
s = ['' for i in range(H)]
for i in range(H):
s[i] = input()
now = 1
ans = [[] for i in range(H)]
count = 0
flag = False
for i in range(H):
gokei = s[i].count('#')
shap = 0
if gokei >= 1:
flag = True
for j in range(W):
ans[i].append(now)
if j == W-1:
now += 1
elif s[i][j] == '#' and shap < gokei-1:
now += 1
shap += 1
elif s[i][j] == '#' and shap == gokei-1:
shap += 1
elif gokei == 0 and flag:
ans[i] = ans[i-1]
elif gokei == 0 and flag == False:
count += 1
for i in range(count):
ans[i] = ans[count]
for i in range(H):
print(*ans[i])
| 0 | null | 72,126,246,782,770 | 52 | 277 |
import math
def main():
A, B = map(int,input().split())
print(int(A*B/math.gcd(A,B)))
if __name__ =="__main__":
main()
|
import math
A,B=map(int,input().split())
print(A*B//math.gcd(A,B))
| 1 | 113,534,297,915,710 | null | 256 | 256 |
import math
from math import gcd
INF = float("inf")
import sys
input=sys.stdin.readline
import itertools
from collections import Counter
def main():
x = int(input())
while True:
if all( x%i != 0 for i in range(2,x)):
print(x)
exit()
x += 1
if __name__=="__main__":
main()
|
import sys
def main():
_ = int(input())
imps = sys.stdin.readlines()
db = {}
for imp in imps:
c, k = imp.split(' ')
if c == 'insert':
db[k] = 0
elif k in db:
print('yes')
else:
print('no')
main()
| 0 | null | 53,002,904,660,440 | 250 | 23 |
import sys
sys.setrecursionlimit(10 ** 5 * 2)
N, K = map(int, input().split())
A = [0] + list(map(int, input().split()))
top = -1
hist = []
finished = [True] + [False for _ in range(N)]
seen = [True] + [False for _ in range(N)]
def dfs(cur):
global hist, finished, seen, top
hist.append(cur)
seen[cur] = True
nex = A[cur]
if seen[nex] and not finished[nex]:
top = nex
return
dfs(nex)
if not top == -1:
return
hist.pop()
finished[cur] = True
dfs(1)
cycle = []
while not hist == []:
i = hist.pop()
cycle.append(i)
if i == top:
break
cycle.reverse()
cur = 1
while not cur == top and not K == 0:
cur = A[cur]
K -= 1
if cur == top:
cur = cycle[K % len(cycle)]
print(cur)
|
n, k = map(int, input().split())
a = list(map(int, input().split()))
d = [[-1] * n for _ in range(70)]
for i in range(n):
d[0][i] = a[i] - 1
for i in range(1, 70):
for j in range(n):
d[i][j] = d[i - 1][d[i - 1][j]]
dst = 0
while k:
i = 70
while pow(2, i) & k <= 0:
i -= 1
dst = d[i][dst]
k -= pow(2, i)
print(dst + 1)
| 1 | 22,805,559,226,232 | null | 150 | 150 |
A,B =map(int,input().split())
if A-B == 0:
print("a == b")
elif A >= B:
print("a > b")
else:
print("a < b")
|
a,b=list(map(int,input().split()))
if a==b:
print("a == b")
elif a<b:
print("a < b")
else:
print("a > b")
| 1 | 358,675,698,150 | null | 38 | 38 |
M1, D1 = map(int, input().split())
M2, D2 = map(int, input().split())
if M1 in [1, 3, 5, 7, 8, 10, 12]:
if D1 == 31:
print(1)
else:
print(0)
elif M1 in [4, 6, 9, 11]:
if D1 == 30:
print(1)
else:
print(0)
elif M1 == 2:
if D1 == 28:
print(1)
else:
print(0)
|
import sys
input = sys.stdin.readline
M1, D1 = map(int, input().split())
M2, D2 = map(int, input().split())
if D2 == 1:
print(1)
else:
print(0)
| 1 | 124,721,501,423,682 | null | 264 | 264 |
N = int(input())
A = list(map(int, input().split()))
XOR = 0
for i in range (0, N):
XOR= XOR^A[i]
B = []
for i in range (0, N):
B.append(XOR^A[i])
print(*B)
|
def e_red_scarf():
from functools import reduce
N = int(input())
A = [int(i) for i in input().split()]
total = reduce(lambda x, y: x ^ y, A)
ans = [a ^ total for a in A]
return ' '.join(map(str, ans))
print(e_red_scarf())
| 1 | 12,360,749,867,732 | null | 123 | 123 |
def insertionSort(n, A, g):
global cnt
for i in range(g, n):
temp = A[i]
j = i - g
while j >= 0 and A[j] > temp:
A[j + g] = A[j]
j -= g
cnt += 1
A[j + g] = temp
return A
def shellSort(A, n):
global m
global G
h = 1
while True:
if h > n:
break
G.append(h)
h = 3 * h + 1
G.reverse()
m =len(G)
for i in range(m):
insertionSort(n, A, G[i])
if __name__ == "__main__":
n = int(input())
A = [int(input()) for i in range(n)]
G = []
m = 0
cnt = 0
shellSort(A, n)
print(m)
print(*G)
print(cnt)
for i in range(n):
print(A[i])
|
#! /usr/bin/env python
# -*- coding: utf-8 -*-
''' ?????¢??¢?´¢ '''
# n??????????´??????????????????????q????????¢?´¢????????£?????´???
# ?¨??????????O(qn)
def linear_search(S, T):
count = 0
for t in T:
if t in S:
count += 1
return count
if __name__ == '__main__':
NS = int(input())
S = list(map(int, input().split()))
NT = int(input())
T = list(map(int, input().split()))
print(linear_search(S, T))
| 0 | null | 49,439,221,472 | 17 | 22 |
x=int(input())
next=[]
now=[1,2,3,4,5,6,7,8,9]
for _ in range(11):
if x<=len(now):
print(now[x-1])
exit()
else:
x-=len(now)
next=now
now=[]
for i in next:
i=str(i)
if i[-1]=='0':
j=i+'0'
k=i+'1'
now+=[j]
now+=[k]
elif i[-1]=='9':
j=i+'8'
k=i+'9'
now+=[j]
now+=[k]
else:
j=i+str(int(i[-1])-1)
k=i+str(int(i[-1]))
l=i+str(int(i[-1])+1)
now+=[j]
now+=[k]
now+=[l]
|
N = int(input())
A = list(map(int, input().split()))
y = sorted([(i+1, A[i]) for i in range(N)], key=lambda x: x[1])
print(" ".join(map(str, [z[0] for z in y])))
| 0 | null | 110,416,024,819,140 | 181 | 299 |
X = int(input())
lsx = [1]*(2*10**5)
for i in range(2,2*10**5):
if lsx[i] == 1:
for j in range(i,2*10**5//i):
lsx[j*i] = 0
for i in range(X,2*10**5):
if lsx[i] == 1:
print(i)
break
|
#!/usr/bin/env python3
import math
x = int(input())
while True:
key=1
for k in range(2, int(math.sqrt(x)) + 1):
if x % k == 0:
key=0
if key==1:
print(x)
exit()
else:
x += 1
print(x)
| 1 | 105,515,165,888,248 | null | 250 | 250 |
def check(a,b,c):
if a == b:
return False
if b == c:
return False
if c == a:
return False
m = max(a,b,c)
if m >= (a+b+c)-m:
return False
return True
N = int(input())
L = list(map(int,input().split()))
ans = 0
if N >= 3:
for i in range(0,N-2):
for j in range(i+1,N-1):
for k in range(j+1,N):
if check(L[i],L[j],L[k]):
ans += 1
print(ans)
|
print(sum([i//500*1000+i%500//5*5 for i in [int(input())]]))
| 0 | null | 23,874,220,671,232 | 91 | 185 |
# 10-Structured_Program_I-Print_a_Frame.py
# ?????¬???????????????
# ??\????????????????????????H cm ?????? W cm ???????????????????????°?????????????????????????????????
# ##########
# #........#
# #........#
# #........#
# #........#
# ##########
# ?????????????????? 6 cm ?????? 10 cm ???????????¨??????????????????
# Input
# ??\???????????°????????????????????????????§???????????????????????????????????????????????????¢????????\????????¨????????§??????
# H W
# H, W ?????¨?????? 0 ?????¨????????\?????????????????¨????????????
# ????????????????????¨??????????????\???????????¶??????3 ??? H, W ??? 100 ??§??????
# Output
# ?????????????????????????????????????????? H cm ?????? W cm ?????????????????????????????????
# ?????????????????????????????????????????????????????\??????????????????
# Constraints
# H, W ??? 300
# Sample Input
# 3 4
# 5 6
# 3 3
# 0 0
# Sample Output
# ####
# #..#
# ####
# ######
# #....#
# #....#
# #....#
# ######
# ###
# #.#
# ###
# a = []
# a = [ *map(int,input().split() ) ]
H=[]
W=[]
while 1:
temp = input().split()
H.append( int(temp[0]) )
W.append( int(temp[1]) )
if H[-1]==0 and W[-1]==0:
break;
for i in range( len(H) ):
for j in range( H[i] ):
#???????¨???°????§?
if j==0 or j==H[i]-1: #?????????????????????????????????
for k in range( W[i] ):
print("#",end="")
else:
print("#",end="")
for k in range(W[i]-2):
print(".",end="")
print("#",end="")
print("")
#???????¨???°?????????
if i !=len(H)-1:
print("")
|
while True:
H, W = map(int, input().split())
if H == 0:
break
print("#"*W)
for i in range(H-2):
print("#" + "."*(W - 2) + "#")
print("#"*W)
print()
| 1 | 811,272,190,680 | null | 50 | 50 |
import math
n = int(input())
a = list(map(int, input().split()))
nmax = 10**6
check = [0]*(nmax + 5)
for i in a:
check[i] += 1
pairwise = True
for i in range(2, nmax + 5):
cnt = 0
for j in range(i, nmax + 5, i):
cnt += check[j]
if cnt > 1:
pairwise = False
if pairwise == True:
print('pairwise coprime')
exit()
x = 0
for i in a:
x = math.gcd(x,i)
if x == 1:
print('setwise coprime')
else:
print('not coprime')
|
from functools import reduce
from math import gcd
N=int(input())
A=list(map(int, input().split()))
c=max(A)+1
D=[0]*c
div=[0]*c
P=[]
for i in range(2,c):
if D[i]==0:
P.append(i)
D[i]=i
for j in P:
if i*j>=c or j>D[i] :
break
D[i*j]=j
f=0
for i in A:
if i==1:
continue
temp=i
while temp!=1:
if div[D[temp]]==1:
f=1
break
div[D[temp]]+=1
temp2=D[temp]
while temp%temp2==0 and temp>=temp2:
temp=temp//temp2
if f==1:
break
if max(div)<=1 and f==0:
print('pairwise coprime')
elif reduce(gcd, A) == 1:
print('setwise coprime')
else:
print('not coprime')
| 1 | 4,091,663,769,102 | null | 85 | 85 |
n,m,k=map(int, input().split( ))
mod = 998244353
ans=0
if m==1:##コーナー
print(1 if k==n-1 else 0)
exit()
tmp=m*pow(m-1,n-1,mod)
m_inv=pow(m-1,mod-2,mod)
invs=[0]+[pow(i+1,mod-2,mod) for i in range(n)]
for i in range(1,k+2):
ans+=tmp
#print(ans)
tmp*=m_inv
if i<n:##?
tmp*=n-i
tmp*=invs[i]
tmp%=mod
print(ans%mod)
|
# coding: utf-8
cnt = int()
m = int()
g = []
def insersion_sort(a, n, g):
global cnt
for i in xrange(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 list(a)
def shell_sort(a, n):
global cnt
global m
global g
cnt = 0
m = 0
g = []
nn = n
while True:
nn /= 2
if nn <= 0:
break
g.append(nn)
m += 1
if n == 1:
m = 1
g = [1]
for i in g:
a = insersion_sort(a, n, i)
return a
def main():
global cnt
global m
global g
n = input()
a = []
for i in xrange(n):
a.append(input())
a = shell_sort(a, n)
print m
print " ".join(map(str, g))
print cnt
for i in xrange(n):
print a[i]
main()
| 0 | null | 11,687,582,654,018 | 151 | 17 |
l = [[[0 for r in range(10)] for f in range(3)] for b in range(4)]
n = int(input())
for num in range(n):
b,f,r,v = [int(x) for x in input().split()]
l[b-1][f-1][r-1] += v
for b in range(4):
for f in range(3):
for r in range(10):
print('', l[b][f][r], end='')
print("")
if b != 3:
print("#" * 20)
|
data = [None] * 4
for i in xrange(4):
data[i] = [None] * 3
for j in xrange(3):
data[i][j] = [0] * 10
n = input()
for i in xrange(n):
line = map(int, raw_input().split())
data[line[0] - 1][line[1] - 1][line[2] - 1] += line[3]
for i in xrange(4):
for j in xrange(3):
s = ""
for k in xrange(10):
s += ' '
s += str(data[i][j][k])
print s
if i < 3:
print '#' * 20
| 1 | 1,116,131,640,388 | null | 55 | 55 |
#coding: UTF-8
N = int(input())
A = input()
#A = list(map(int, input().split()))
IN1 = [None] * N
IN2 = [None] * N
i=0
for a in A.split():
tmp = int(a.lstrip("SHCD"))
IN1[i] = [tmp, i, a]
IN2[i] = [tmp, i, a]
i+=1
def BubbleSort(A,N):
for i in range(N - 1):
for j in range(N - 1, i,-1):
if A[j][0] < A[j - 1][0]:
tmp = A[j]
A[j] = A[j - 1]
A[j - 1] = tmp
def InsertionSort(A,N):
for i in range(0,N):
minj = i
for j in range(i, N):
if A[j][0] < A[minj][0]:
minj = j
if i != minj:
tmp = A[i]
A[i] = A[minj]
A[minj] = tmp
def StableCheck(A):
tmp = [0,0]
for t in A:
if(tmp[1] > t[1]):
if(tmp[0] == t[0]):
return False
tmp = t
return True
BubbleSort(IN1,N)
InsertionSort(IN2,N)
for i in range(N - 1):
print(IN1[i][2],end=' ')
print(IN1[N - 1][2])
if(StableCheck(IN1)):print("Stable")
else:print("Not stable")
for i in range(N - 1):
print(IN2[i][2],end=' ')
print(IN2[N - 1][2])
if(StableCheck(IN2)):print("Stable")
else:print("Not stable")
|
from copy import copy
n = int(input())
cards = [(s[0],int(s[1])) for s in input().split()]
cards_1 = copy(cards)
cards_2 = copy(cards)
need_to_sort = True
while need_to_sort:
need_to_sort = False
for index in range(len(cards_1) - 1, 0, -1):
if cards_1[index][1] < cards_1[index-1][1]:
cards_1[index], cards_1[index-1] = cards_1[index-1], cards_1[index]
need_to_sort = True
print(" ".join([s[0]+ str(s[1]) for s in cards_1]))
print("Stable")
for idx1 in range(len(cards_2)):
mins = idx1
for idx2 in range(idx1, len(cards_2)):
if cards_2[idx2][1] < cards_2[mins][1]:
mins = idx2
if mins != idx1:
cards_2[idx1], cards_2[mins] = cards_2[mins], cards_2[idx1]
print(" ".join([s[0]+ str(s[1]) for s in cards_2]))
is_stable = True
for index in range(n):
if cards_1[index] != cards_2[index]:
is_stable = False
break
print("Stable" if is_stable else "Not stable")
| 1 | 25,169,111,020 | null | 16 | 16 |
class FastModComb:
def __init__(self, n, mod=10 ** 9 + 7):
f, self.mod = [1], mod
for i in range(1, n + 1):
f.append(f[-1] * i % mod)
rf = [self.modpow(f[-1], mod - 2)]
for i in range(1, n + 1):
rf.append(rf[-1] * (n - i + 1) % mod)
self.f, self.rf = f, list(reversed(rf))
def modpow(self, a, b):
ret = 1
while b > 0:
if b % 2 == 1:
ret = ret * a % self.mod
a = a * a % self.mod
b //= 2
return ret
def modcomb(self, n, c):
return self.f[n] * self.rf[c] * self.rf[n - c] % self.mod
n, k = map(int, input().split())
mod, mc = 10 ** 9 + 7, FastModComb(n)
print(sum([mc.modcomb(n, m) * mc.modcomb(n - 1, n - m - 1) % mod
for m in range(min(n - 1, k) + 1)]) % mod)
|
import sys, bisect, math, itertools, heapq, collections
from operator import itemgetter
# a.sort(key=itemgetter(i)) # i番目要素でsort
from functools import lru_cache
# @lru_cache(maxsize=None)
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9 + 7
eps = 10**-7
def inp():
'''
一つの整数
'''
return int(input())
def inpl():
'''
一行に複数の整数
'''
return list(map(int, input().split()))
class combination():
def __init__(self, mod):
'''
modを指定して初期化
'''
self.mod = mod
self.fac = [1, 1] # 階乗テーブル
self.ifac = [1, 1] # 階乗の逆元テーブル
self.inv = [0, 1] # 逆元計算用
def calc(self, n, k):
'''
nCk%modを計算する
'''
if k < 0 or n < k:
return 0
self.make_tables(n) # テーブル作成
k = min(k, n - k)
return self.fac[n] * (self.ifac[k] * self.ifac[n - k] %
self.mod) % self.mod
def make_tables(self, n):
'''
階乗テーブル・階乗の逆元テーブルを作成
'''
for i in range(len(self.fac), n + 1):
self.fac.append((self.fac[-1] * i) % self.mod)
self.inv.append(
(-self.inv[self.mod % i] * (self.mod // i)) % self.mod)
self.ifac.append((self.ifac[-1] * self.inv[-1]) % self.mod)
comb = combination(mod)
n, k = inpl()
ans = 0
# m個の0人の部屋を作る
for m in range(min(n, k + 1)):
# n個の部屋からm個の0人の部屋を選ぶ
a = comb.calc(n, m) % mod
# n個の部屋にm人を自由に割り当てる
b = comb.calc(n - 1, n - m - 1) % mod
ans += a * b % mod
print(ans % mod)
| 1 | 67,174,557,489,898 | null | 215 | 215 |
import collections
n = int(input())
L = list(map(int,input().split()))
ans = 0
for i in range(n):
for j in range(i,n):
for k in range(j,n):
if L[i]+L[j] > L[k] and L[k]+L[j] > L[i] and L[i]+L[k] > L[j]:
if L[i] != L[j] and L[j] != L[k] and L[k] != L[i]:
ans += 1
print(ans)
|
N = int(input())
L = list(map(int, input().split()))
if N <= 2:
print(0)
exit()
count = 0
for i in range(N-2):
for j in range(i+1, N-1):
for k in range(j+1, N):
rod = [L[i], L[j], L[k]]
condition = set(rod)
rod = sorted(rod)
if rod[0] + rod[1] > rod[2] and len(condition) == 3:
count += 1
print(count)
| 1 | 5,070,508,811,072 | null | 91 | 91 |
import math
a,b,deg = map(float,input().split(" "))
sindeg = math.sin(math.radians(deg))
S = (a*b*sindeg)/2
cosdeg = math.cos(math.radians(deg))
z = a*a + b*b - 2*a*b*cosdeg
L = a + b + math.sqrt(z)
h = b*sindeg
print("{:.5f}".format(S))
print("{:.5f}".format(L))
print("{:.5f}".format(h))
|
import math
import re
import copy
t = input()
ans = t.replace("?","D")
print(ans)
| 0 | null | 9,374,279,359,020 | 30 | 140 |
# coding: utf-8
# Your code here!
import math
while True:
n = int(input())
if n == 0:
break
s = list(map(int,input().split()))
sum = 0
for i in range(len(s)):
sum += s[i]
m = sum/len(s)
a = 0
for i in range(len(s)):
a += s[i]*s[i]
a = a/len(s)
print("{0:.8f}".format(math.sqrt(a-m*m)))
|
import math
while True:
n = int(input())
if n == 0:
break
s = [int(s) for s in input().split()]
ave = sum(s) / n
a = 0
for N in s:
a += (N - ave)**2
a = math.sqrt((a / n))
print(a)
| 1 | 187,897,017,664 | null | 31 | 31 |
N,K = map(int,input().split())
A = sorted(list(map(int,input().split())))
mod = 10**9+7
ans = 1
mi = []
pl = []
for a in A:
if a < 0:
mi.append(a)
else:
pl.append(a)
if N == K:
for a in A:
ans *= a
ans %= mod
print(ans)
exit()
if pl == []:
if K%2:
for k in range(K):
ans *= mi[-1-k]
ans %= mod
else:
for k in range(K):
ans *= mi[k]
ans %= mod
print(ans)
exit()
pl = pl[::-1]
ip = 0
im = 0
if K % 2:
ans = pl[0]
ip += 1
K -= 1
while K > 0:
check = True
if ip + 2 <= len(pl):
x = pl[ip]*pl[ip+1]
else:
x = 1
if im + 2 <= len(mi):
y = mi[im]*mi[im+1]
else:
check = False
y = 1
if x >= y or check == False:
ans *= x
ans %= mod
ip += 2
else:
ans *= y
ans %= mod
im += 2
K -= 2
print(ans)
|
import math
X = int(input())
X -= 1
while True:
flg = 0
X += 1
lim = int(math.sqrt(X))
for i in range(2, lim+1):
if X % i == 0:
flg = 1
break
if flg == 1:
continue
break
print(X)
| 0 | null | 57,798,992,065,308 | 112 | 250 |
def calculus(a):
b = 1 + a
c = b*a + 1
return a*c
a = int(input())
print(calculus(a))
|
n=int(input())
print(n*(1+n*(1+n)))
| 1 | 10,222,580,393,218 | null | 115 | 115 |
N = int(input())
S = input()
if N<4:
print(0)
else:
a = [0] * N
R, G, B = 0, 0, 0
for i in range(N):
if S[i] == "R":
a[i] = 1
R += 1
if S[i] == "G":
a[i] = 2
G += 1
if S[i] == "B":
a[i] = 4
B += 1
rgb = R * G * B
cnt = 0
for i in range(N-2):
for d in range(1, (N-i-1) // 2+1):
if a[i] + a[i+d] + a[i+2*d] == 7:
cnt += 1
print(rgb -cnt)
|
s1 = input()
s2 = input()
while len(s1) <= 100:
s1 = s1 + s1
if s2 in s1:
print("Yes")
else:
print("No")
| 0 | null | 18,850,137,077,410 | 175 | 64 |
x=int(input())
X=x
k=1
while X%360!=0:
k+=1
X+=x
print(k)
|
a = int(input())
b = a % 2
if b == 0:
my_result = 0.5
else:
my_result = (a + 1) / (2 * a)
print(my_result)
| 0 | null | 95,381,527,123,728 | 125 | 297 |
n = int(input())
d = {}
for i in range(n):
cmd, string = input().split()
if cmd == "insert":
d[string] = True
else:
try:
if d[string]:
print("yes")
except:
print("no")
|
def m(L,R):
global c
T=[];i=j=0
for _ in L[:-1]+R[:-1]:
if L[i]<R[j]:T+=[L[i]];i+=1
else:T+=[R[j]];j+=1
c+=1
return T
def d(A):s=len(A)//2;return m(d(A[:s])+[1e9],d(A[s:])+[1e9]) if len(A)>1 else A
c=0
input()
print(*d(list(map(int,input().split()))))
print(c)
| 0 | null | 94,775,485,412 | 23 | 26 |
twoNumbers=list(map(int,input().split()))
x=twoNumbers[0]
y=twoNumbers[1]
devidedBy=[]
syou=[]
if x>=y:
for number in range(1, y+1):
if y%number==0:
if number>y//number:
break
else:
if not number in devidedBy:
devidedBy.append(number)
if not y//number in syou:
syou.append(y//number)
else:
for number in range(1, x+1):
if x%number==0:
if number>x//number:
break
else:
if not number in devidedBy:
devidedBy.append(number)
if not x//number in syou:
syou.append(x//number)
if devidedBy[len(devidedBy)-1]==syou[len(syou)-1]:
del devidedBy[len(devidedBy)-1]
devidedBy.extend(syou)
devidedBy.sort()
index=len(devidedBy)-1
while index>=0:
if x>=y:
if x%devidedBy[index]==0:
print(devidedBy[index])
break
else:
index-=1
else:
if y%devidedBy[index]==0:
print(devidedBy[index])
break
else:
index-=1
|
def gcd(x, y):
if x < y:
tmp = x
x = y
y = tmp
r = x % y
while r != 0:
x = y
y = r
r = x % y
return y
x, y = map(int, input().split())
print(gcd(x, y))
| 1 | 8,100,274,432 | null | 11 | 11 |
x = int(input())
ans = x // 500 * 1000
x = x % 500
ans += x // 5 * 5
print(ans)
|
a=int(input())
print(a//500*1000+a%500//5*5)
| 1 | 42,563,158,814,848 | null | 185 | 185 |
INF = 10**9
H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]
# (i, j)に進むときの色の反転回数の最小値
dp = [[INF for j in range(W)] for i in range(H)]
if s[0][0] == '#':
dp[0][0] = 1
else:
dp[0][0] = 0
for i in range(H):
for j in range(W):
if i == H - 1 and j == W - 1:
break
if i != H - 1:
if s[i][j] == '.' and s[i + 1][j] == '#':
dp[i + 1][j] = min(dp[i][j] + 1, dp[i + 1][j])
else:
dp[i + 1][j] = min(dp[i][j], dp[i + 1][j])
if j != W - 1:
if s[i][j] == '.' and s[i][j + 1] == '#':
dp[i][j + 1] = min(dp[i][j] + 1, dp[i][j + 1])
else:
dp[i][j + 1] = min(dp[i][j], dp[i][j + 1])
print(dp[H-1][W-1])
|
# A - Range Flip Find Route
H,W = map(int,input().split())
INF = 1000000
s = [input() for _ in range(H)]
a = [[INF]*(W+1) for _ in range(H+1)]
for i in range(1,H+1):
for j in range(1,W+1):
if (i,j)==(1,1):
a[i][j] = int(s[0][0]=='#')
elif i==1:
a[i][j] = a[i][j-1]+int(s[i-1][j-2]=='.' and s[i-1][j-1]=='#')
elif j==1:
a[i][j] = a[i-1][j]+int(s[i-2][j-1]=='.' and s[i-1][j-1]=='#')
else:
a[i][j] = min(
a[i][j-1]+int(s[i-1][j-2]=='.' and s[i-1][j-1]=='#'),
a[i-1][j]+int(s[i-2][j-1]=='.' and s[i-1][j-1]=='#')
)
print(a[H][W])
| 1 | 49,526,086,901,170 | null | 194 | 194 |
tmp = int(input())
if tmp>=30:
print("Yes")
else:
print("No")
|
has_seven = "7" in input()
if has_seven:
print("Yes")
else:
print("No")
| 0 | null | 20,112,101,910,430 | 95 | 172 |
A,B = list(map(int,input().split()))
N={1:300000,2:200000,3:100000}
if A ==1 and B==1:
print(1000000)
else:
try:
A=N[A]
except KeyError:
A=0
try:
B=N[B]
except KeyError:
B=0
print(A+B)
|
def main():
x, y = input().split()
answer = 0
prize = {"1": 300000, "2": 200000, "3": 100000}
if x in prize:
answer += prize[x]
if y in prize:
answer += prize[y]
if x == y == "1":
answer += 400000
print(answer)
if __name__ == '__main__':
main()
| 1 | 140,369,243,919,242 | null | 275 | 275 |
n, k = (int(i) for i in input().split())
m = n % k
ans = min(m, k - m)
print(ans)
|
#-------------------------------------------------------------------------------
#関数定義
def f(a,d,T):
try:
return T[d+1:].index(a)+(d+1)
except ValueError:
return len(T)
def g(a,d,T):
try:
return (d-1)-T[d-1::-1].index(a)
except ValueError:
return 0
#-------------------------------------------------------------------------------
#インポート
from math import inf,exp
from random import random,randint
import time
from copy import copy
#-------------------------------------------------------------------------------
#定数
TIME_LIMIT=1.864 #ループの時間制限
A=26 #アルファベットの数
START=time.time()
ALPHA=15
ep=10**(-6) #アニーリングの微小温度
#-------------------------------------------------------------------------------
#読み込み部
D=int(input())
C=[True]+list(map(int,input().split())) #減る満足度のベース
S=[True]*(D+1) #S[d][i] :(d+1)日目にコンテストiを開催した時の満足度
for i in range(1,D+1):
S[i]=[0]+list(map(int,input().split()))
#-------------------------------------------------------------------------------
#貪欲部
L=[0]*(A+1)
T=[0]
for d in range(1,D+1):
Y=-inf
E=0
for a in range(1,A+1):
X=S[d][a]
for s in range(1,A+1):
if a!=s:
X-=C[s]*(d-L[s])
if X>Y:
Y=X
E=a
L[E]=d
T.append(E)
#-------------------------------------------------------------------------------
#調節部
t=time.time()-START
while t<TIME_LIMIT:
d=randint(1,D)
p=T[d]
q=randint(1,A)
Delta=(S[d][q]-S[d][p])+(d-g(q,d,T))*(f(q,d,T)-d)*C[q]-(d-g(p,d,T))*(f(p,d,T)-d)*C[p]
if (Delta>=0) or (Delta<0 and random()<exp(Delta/(ALPHA*(TIME_LIMIT-t)+ep))):
T[d]=q
t=time.time()-START
for i in range(1,D+1):
print(T[i])
| 0 | null | 24,578,902,764,480 | 180 | 113 |
# coding: UTF-8
line = str(raw_input()*2)
word = str(raw_input())
if line.count(word):
print "Yes"
else: print "No"
|
s = input()
p = input()
s *= 2
if p in s:
print('Yes')
else:
print('No')
| 1 | 1,746,747,244,970 | null | 64 | 64 |
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 = I()
s = 'abcdefghijklmnopqrstuvwxyz'
assert len(s) == 26
cur = []
while N > 0:
amari = N % 26
if amari == 0:
amari += 26
cur.append(s[amari-1])
N -= amari
N //= 26
print(''.join(cur[::-1]))
main()
|
# Problem E - Dividing Chocolate
# input
H, W, K = map(int, input().split())
board = [['']*W for i in range(H)]
for i in range(H):
s = list(input())
for j in range(W):
board[i][j] = s[j]
# initialization
min_divide = 10**10
s = [0]*(H-1) # 横線の入り方 0:無 1:有
is_ok = True
# search
for i in range(2**(H-1)): # bit全探索
# sの初期化
h_count = 0
w_count = 0
for j in range(H-1):
if ((i>>j)&1):
s[j] = 1
h_count += 1
else:
s[j] = 0
# 列の全探索
s_score = [0]*H # 各区域のスコア
s_cur = [0]*H
for j in range(W):
c = 0
s_cur = [0]*H
for k in range(H):
if board[k][j]=='1':
s_cur[c] += 1
# Kを超えていないかのチェック
if s_cur[c]>K:
is_ok = False
break
# 次の遷移先(横線の有無でグループが別れる)
if k+1<H:
if s[k]==1:
c += 1
# Kを超えていたらループ中止
if not is_ok:
break
# 前の列のグループと足してみてKを超えていないかチェック
# 超えていれば縦線分割を施す
group_smaller = True
if j>0:
for c_num in range(c+1):
if s_score[c_num]+s_cur[c_num]>K:
group_smaller = False
else:
group_smaller = True
if group_smaller:
for c_num in range(c+1):
s_score[c_num] += s_cur[c_num]
else:
w_count += 1
for c_num in range(c+1):
s_score[c_num] = s_cur[c_num]
if not is_ok:
is_ok = True
continue
# for c_num in range(h_count+1):
# if s_score[c_num]>K:
# h_count = 10 ** 6
# 縦線と横線の合計でmin_divideを更新
min_divide = min(min_divide, w_count + h_count)
# output
print(min_divide)
| 0 | null | 30,298,125,554,118 | 121 | 193 |
import itertools
def solve():
N = int(input())
S = input()
l = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
p = itertools.product(l,repeat=3)
count = 0
for v in p:
S_temp = S
dig_0_index = S_temp.find(v[0])
if dig_0_index != -1:
S_temp = S_temp[dig_0_index+1::]
dig_1_index = S_temp.find(v[1])
if dig_1_index != -1:
S_temp = S_temp[dig_1_index+1::]
dig_2_index = S_temp.find(v[2])
if dig_2_index != -1:
count += 1
print(count)
if __name__ == "__main__":
solve()
|
data = input()
x = []
x = data.split()
a = int(x[0])
b = int(x[1])
if(a > b):
print("a > b")
elif(a < b):
print("a < b")
else:
print('a == b')
| 0 | null | 64,587,803,218,430 | 267 | 38 |
while(True):
i=0
a=list(map(int,input().split()))
if(a[0]==0 and a[1]==0):
break
for x in range(2,a[0]):
sum=a[1]-x
for y in range(1,x):
if (sum-y<a[0]+1 and sum-y>x):
i+=1
print(i)
|
while True:
n, x = map(int, input().split())
if (n == x == 0):
break
count = 0
for a in range(1, x // 3):
for b in range(a + 1, x // 2):
for c in range(b + 1, n + 1):
Sum = a + b + c
if (Sum == x):
count += 1
print(count)
| 1 | 1,317,920,823,872 | null | 58 | 58 |
s = input()
k = int(input())
if len(s) == 1:
print(k//2)
exit()
rep = [1]
prev = s[0]
for i in range(1, len(s)):
if s[i] == prev:
rep[-1] += 1
else:
rep.append(1)
prev = s[i]
if len(rep) == 1:
print(rep[0]*k//2)
exit()
cnt = 0
for r in rep:
cnt += r//2
if s[0] == s[-1]:
ans = cnt*k + ((rep[0]+rep[-1])//2 - rep[0]//2 - rep[-1]//2)*(k-1)
print(ans)
else:
print(cnt*k)
|
def main():
s = input()
p = input()
if (s + s).__contains__(p):
print('Yes')
else:
print('No')
if __name__ == '__main__':
main()
| 0 | null | 88,457,127,604,780 | 296 | 64 |
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 9 01:55:56 2020
@author: liang
"""
import math
N = int(input())
key = int(math.sqrt(N))
ans = 10**12
for i in reversed(range(1,key+1)):
if N%i == 0:
ans = i-1 + N//i -1
break
print(ans)
|
import sys
input = sys.stdin.buffer.readline
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
n, k = MAP()
h = LIST()
h.sort(reverse=True)
if k >= n:
print(0)
else:
print(sum(h[k:]))
| 0 | null | 120,734,713,503,632 | 288 | 227 |
string = list(input())
times = int(input())
for i in range(times):
order = input().split()
if order[0] == "print":
print("".join(string[int(order[1]):int(order[2]) + 1]))
elif order[0] == "replace":
string[int(order[1]):int(order[2]) + 1] = order[3]
else:
string[int(order[1]):int(order[2]) + 1] = list(reversed(string[int(order[1]):int(order[2]) + 1]))
|
from random import randint
import sys
input = sys.stdin.readline
INF = 9223372036854775808
def calc_score(D, C, S, T):
"""
開催日程Tを受け取ってそこまでのスコアを返す
コンテストi 0-indexed
d 0-indexed
"""
score = 0
last = [0]*26 # コンテストiを前回開催した日
for d, t in enumerate(T):
last[t] = d + 1
for i in range(26):
score -= (d + 1 - last[i]) * C[i]
score += S[d][t]
return score
def update_score(D, C, S, T, score, ct, ci):
"""
ct日目のコンテストをコンテストciに変更する
スコアを差分更新する
ct: change t 変更日 0-indexed
ci: change i 変更コンテスト 0-indexed
"""
new_score = score
last = [0]*26 # コンテストiを前回開催した日
prei = T[ct] # 変更前に開催する予定だったコンテストi
for d, t in enumerate(T, start=1):
last[t] = d
new_score += (d - last[prei])*C[prei]
new_score += (d - last[ci])*C[ci]
last = [0]*26
for d, t in enumerate(T, start=1):
if d-1 == ct:
last[ci] = d
else:
last[t] = d
new_score -= (d - last[prei])*C[prei]
new_score -= (d - last[ci])*C[ci]
new_score -= S[ct][prei]
new_score += S[ct][ci]
return new_score
def evaluate(D, C, S, T, k):
"""
d日目終了時点での満足度を計算し,
d + k日目終了時点での満足度の減少も考慮する
"""
score = 0
last = [0]*26
for d, t in enumerate(T):
last[t] = d + 1
for i in range(26):
score -= (d + 1 - last[i]) * C[i]
score += S[d][t]
for d in range(len(T), min(len(T) + k, D)):
for i in range(26):
score -= (d + 1 - last[i]) * C[i]
return score
def greedy(D, C, S):
Ts = []
for k in range(5, 13):
T = [] # 0-indexed
max_score = -INF
for d in range(D):
# d+k日目終了時点で満足度が一番高くなるようなコンテストiを開催する
max_score = -INF
best_i = 0
for i in range(26):
T.append(i)
score = evaluate(D, C, S, T, k)
if max_score < score:
max_score = score
best_i = i
T.pop()
T.append(best_i)
Ts.append((max_score, T))
return max(Ts, key=lambda pair: pair[0])
def local_search(D, C, S, score, T):
# ct 日目のコンテストをciに変更
for k in range(50000):
ct = randint(0, D-1)
ci = randint(0, 25)
ori = T[ct]
new_score = update_score(D, C, S, T, score, ct, ci)
T[ct] = ci
if score < new_score:
score = new_score
else:
T[ct] = ori
return T
if __name__ == '__main__':
D = int(input())
C = [int(i) for i in input().split()]
S = [[int(i) for i in input().split()] for j in range(D)]
init_score, T = greedy(D, C, S)
T = local_search(D, C, S, init_score, T)
for t in T:
print(t+1)
| 0 | null | 5,840,558,809,262 | 68 | 113 |
import math
n = int(input())
towns = [list(map(int, input().split())) for _ in range(n)]
ans = 0
for i in range(n-1):
x1, y1 = towns[i]
for j in range(i+1, n):
x2, y2 = towns[j]
ans += math.sqrt((x1-x2)**2 + (y1-y2)**2)*(n-1)*2
print(ans/((n-1)*n))
|
#!/usr/bin python3
# -*- coding: utf-8 -*-
import sys
input = sys.stdin.readline
def main():
N, A, B = map(int, input().split())
if abs(A-B)%2==0:
ret = abs(A-B)//2
else:
A, B = min(A, B), max(A, B)
ret = min(B-(B-A+1)//2, N-(B-A-1)//2-A)
print(ret)
if __name__ == '__main__':
main()
| 0 | null | 128,457,464,826,570 | 280 | 253 |
import heapq
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
L = []
ans = 0
heapq.heappush(L, -A[0])
for i in range(1, N):
max = heapq.heappop(L)
max *= -1
ans += max
heapq.heappush(L, -A[i])
heapq.heappush(L, -A[i])
print(ans)
|
def Range():
a,b,c = list(map(int,input().split()))
if a<b<c:
print("Yes")
else:
print("No")
Range()
| 0 | null | 4,747,484,717,470 | 111 | 39 |
MOD = 10 ** 9 + 7
n, k = map(int, input().split())
ans = 0
for x in range(k, n + 2):
p = x * (x - 1) // 2
q = x * (x - 1) // 2 + (n - x + 1) * x
ans += q - p + 1
print(ans % MOD)
|
D = int(input())
C = [int(T) for T in input().split()]
S = [[] for TD in range(0,D)]
for TD in range(0,D):
S[TD] = [int(T) for T in input().split()]
Type = 26
Last = [0]*Type
SatD = [0]*D
SatA = [0]*(D+1)
for TD in range(0,D):
Test = int(input())-1
Last[Test] = TD+1
SatD[TD] = S[TD][Test]
for TC in range(0,Type):
SatD[TD] -= C[TC]*(TD+1-Last[TC])
SatA[TD+1] = SatA[TD]+SatD[TD]
print('\n'.join(str(T) for T in SatA[1:]))
| 0 | null | 21,394,787,834,904 | 170 | 114 |
#http://judge.u-aizu.ac.jp/onlinejudge/commentary.jsp?id=ALDS1_4_A
#?????¢??¢?´¢
#???????????§?¨????????????????????????¨?????§??????????????\????????????????????????????????????
def linear_search(list_1, list_2):
count = 0
for a in list_1:
i = 0
list_2.append(a)
while not a == list_2[i]:
i += 1
if not i == len(list_2) - 1:
count += 1
list_2 = list_2[:-1]
return count
def main():
n_listA = int(input())
a = list(set([n for n in input().split()]))
n_listB = int(input())
b = list(set([n for n in input().split()]))
print(linear_search(a,b))
if __name__ == "__main__":
main()
|
def main():
n = int(input())
d = set([])
for i in range(n):
command, string = input().split()
if (command == 'insert'):
d.add(string)
elif (command == 'find'):
if string in d:
print('yes')
else:
print('no')
if __name__ == "__main__":
main()
| 0 | null | 72,203,498,400 | 22 | 23 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.