code1
stringlengths 16
24.5k
| code2
stringlengths 16
24.5k
| similar
int64 0
1
| pair_id
int64 2
181,637B
⌀ | question_pair_id
float64 3.71M
180,628B
⌀ | code1_group
int64 1
299
| code2_group
int64 1
299
|
---|---|---|---|---|---|---|
from collections import deque
K=int(input())
q=deque([i for i in range(1,10)])
for _ in range(K-1):
x=deque.popleft(q)
a=x%10
if a!=0 and a!=9:
deque.append(q,x*10+a-1)
deque.append(q,x*10+a)
deque.append(q,x*10+a+1)
elif a==0:
deque.append(q,x*10)
deque.append(q,x*10+1)
else:
deque.append(q,x*10+8)
deque.append(q,x*10+9)
print(deque.popleft(q))
|
k=int(input())
q=["1","2","3","4","5","6","7","8","9"]
x=9
for i in q:
if x>100000:
break
x+=1
a=i[-1]
for j in ([-1,0,1]):
bb=int(a)+j
if bb<10 and bb>=0:
q.append(i+str(bb))
print(q[k-1])
| 1 | 39,761,038,728,180 | null | 181 | 181 |
N = int(input())
xy = [tuple(map(int, input().split())) for _ in range(N)]
z = []; w = []
for x, y in xy:
z.append(x + y)
w.append(x - y)
print(max(max(z)-min(z), max(w)-min(w)))
|
n = int(input())
xy = []
for _ in range(n):
xy.append(list(map(int, input().split())))
tmp1 = []
tmp2 = []
for i in range(n):
tmp1.append([xy[i][0]+xy[i][1], xy[i][0], xy[i][1]])
tmp2.append([xy[i][0]-xy[i][1], xy[i][0], xy[i][1]])
tmp1.sort()
tmp2.sort()
ans = 0
if (tmp1[-1][2]-tmp1[0][2])*(tmp1[-1][1]-tmp1[0][1]) >= 0:
tmp = tmp1[-1][0]-tmp1[0][0]
ans = max(tmp, ans)
if (tmp2[-1][2]-tmp2[0][2])*(tmp2[-1][1]-tmp2[0][1]) <= 0:
tmp = tmp2[-1][0]-tmp2[0][0]
ans = max(tmp, ans)
print(ans)
| 1 | 3,456,004,632,000 | null | 80 | 80 |
from collections import deque
def marge_ponds(lx, area_of_pond):
global ponds
if ponds and ponds[-1][0] > lx:
return marge_ponds(lx, area_of_pond + ponds.pop()[1])
return area_of_pond
terrains = input().strip()
x, last_x, ponds = 0, deque(), deque()
for terrain in terrains:
if terrain == '\\':
last_x.append(x)
elif terrain == '/':
if last_x:
lx = last_x.pop()
ponds.append((lx, marge_ponds(lx, x - lx)))
x += 1
print(sum(pond[1] for pond in ponds))
ponds.appendleft((0, len(ponds)))
print(' '.join(map(str, [pond[1] for pond in ponds])))
|
from sys import stdin
from collections import deque
sect = stdin.readline().lstrip("/_").rstrip("\n\\_")
stack = deque()
res = deque()
for i in range(len(sect)):
if sect[i] == "\\":
stack.append(i)
elif sect[i] == "/":
if len(stack) != 0:
b = stack.pop()
if len(res) == 0:
res.append( (b, i-b) )
elif b > res[-1][0]:
res.append( (b, i-b) )
else: # 水たまりを結合
sum_L = i-b
while len(res) != 0:
if res[-1][0] > b:
sum_L += res[-1][1]
res.pop()
else:
break
res.append((b, sum_L))
res = [x[1] for x in res]
print(sum(res))
print(len(res), *res)
| 1 | 62,333,623,452 | null | 21 | 21 |
mod = 10**9 + 7
from collections import deque
def main():
N = iip(False)
K = iip(False)
ret = solve(N, K)
print(ret)
def solve(N, K):
strn = str(N)
A = []
B = []
for i in range(len(strn), 0, -1):
if strn[len(strn)-i] != "0":
A.append(int(strn[len(strn)-i]))
B.append(i)
if K == 1:
return 9*(B[0]-1) + A[0]
if K == 2:
if len(strn) < 2:
return 0
ret = 0
ret += (B[0]-1) * (B[0]-2) // 2 * (9**2) #桁数がmaxじゃない場合
ret += (A[0]-1) * 9 * (B[0]-1) #桁数がmaxで先頭桁がmax以外の場合
if len(B) >= 2 and len(A) >= 2:
ret += (B[1]-1) * 9 + A[1] #先頭桁がmaxの場合
return ret
ret = 0
ret += (B[0] - 1) * (B[0] - 2) * (B[0] - 3) // 6 * 9**3 #桁数がmaxじゃない場合
ret += (A[0] - 1) * (B[0] - 1) * (B[0] - 2) // 2 * 9**2 #桁数がmaxで先頭桁がmaxじゃない場合
#以下、桁数はmaxで先頭桁はmaxとする
if len(strn) < 3:
return 0
if len(B) >= 2:
ret += (B[1]-1) * (B[1]-2) // 2 * 9**2 #有効2桁目の桁数がmaxじゃない場合
if len(B) >= 2 and len(A) >= 2:
ret += (A[1] - 1) * (B[1]-1) * 9 #有効2桁目の桁数がmaxで数字がmaxじゃない場合
if len(B) >= 3 and len(A) >= 3:
ret += (B[2] - 1) * 9 + A[2] #有効2桁目,3桁目がmaxの場合
return ret
#####################################################ライブラリ集ここから
def split_print_space(s):
print(" ".join([str(i) for i in s]))
def split_print_enter(s):
print("\n".join([str(i) for i in s]))
def searchsorted(sorted_list, n, side):
if side not in ["right", "left"]:
raise Exception("sideはrightかleftで指定してください")
l = 0
r = len(sorted_list)
if n > sorted_list[-1]:
#print(sorted_list)
return len(sorted_list)
if n < sorted_list[0]:
return 0
while r-l > 1:
x = (l+r)//2
if sorted_list[x] > n:
r = x
elif sorted_list[x] < n:
l = x
else:
if side == "left":
r = x
elif side == "right":
l = x
if side == "left":
if sorted_list[l] == n:
return r - 1
else:
return r
if side == "right":
if sorted_list[l] == n:
return l + 1
else:
return l
def iip(listed):
ret = [int(i) for i in input().split()]
if len(ret) == 1 and not listed:
return ret[0]
return ret
def soinsuu_bunkai(n):
ret = []
for i in range(2, int(n**0.5)+1):
while n % i == 0:
n //= i
ret.append(i)
if i > n:
break
if n != 1:
ret.append(n)
return ret
def conbination(n, r, mod, test=False):
if n <=0:
return 0
if r == 0:
return 1
if r < 0:
return 0
if r == 1:
return n
ret = 1
for i in range(n-r+1, n+1):
ret *= i
ret = ret % mod
bunbo = 1
for i in range(1, r+1):
bunbo *= i
bunbo = bunbo % mod
ret = (ret * inv(bunbo, mod)) % mod
if test:
#print(f"{n}C{r} = {ret}")
pass
return ret
def inv(n, mod):
return power(n, mod-2)
def power(n, p):
if p == 0:
return 1
if p % 2 == 0:
return (power(n, p//2) ** 2) % mod
if p % 2 == 1:
return (n * power(n, p-1)) % mod
if __name__ == "__main__":
main()
|
s = []
while True:
hoge = input()
if hoge == '-':
break
num = int(input())
for i in range(num):
h = int(input())
hoge = hoge.replace(hoge,hoge[h:] + hoge[:h])
s += [hoge]
for i in s: print(i)
| 0 | null | 38,740,846,907,500 | 224 | 66 |
N = int(input())
A = [input() for _ in range(N)]
count = [0] * 4
for a in A:
if a == 'AC':
count[0] += 1
elif a == 'WA':
count[1] += 1
elif a == 'TLE':
count[2] += 1
else:
count[3] += 1
print(f"AC x {count[0]}")
print(f"WA x {count[1]}")
print(f"TLE x {count[2]}")
print(f"RE x {count[3]}")
|
N = int(input())
ansAC = 0
ansWA = 0
ansTLE = 0
ansRE = 0
for i in range(N):
S = input()
if S == "AC":
ansAC += 1
elif S == "TLE":
ansTLE += 1
elif S == "WA":
ansWA += 1
elif S == "RE":
ansRE += 1
print("AC x " + str(ansAC))
print("WA x " + str(ansWA))
print("TLE x " + str(ansTLE))
print("RE x " + str(ansRE))
| 1 | 8,685,522,216,670 | null | 109 | 109 |
from sys import stdin,stdout
l,r,d=list(map(int,stdin.readline().strip().split()))
c=0
if l%d==0 and r%d==0:
stdout.write(str(int(r/d)-int(l/d)+1)+'\n')
else:
stdout.write(str(int(r/d)-int(l/d))+'\n')
|
def main():
L, R, d = (int(i) for i in input().split())
ans = 0
for i in range(L, R+1):
if i % d == 0:
ans += 1
print(ans)
if __name__ == '__main__':
main()
| 1 | 7,549,213,151,520 | null | 104 | 104 |
n = int(input())
x = int(n**(1/2))+1
for i in range(1, x+1):
if n%i == 0:
p = i
q = n//i
print(p+q-2)
|
n = int(input())
root = int(n**0.5)
a = 0
for i in range(root, 0, -1):
if n % i == 0:
a = i
break
b = n // a
print(a+b - 2)
| 1 | 161,714,322,082,272 | null | 288 | 288 |
import math
r = float(float(input())) #??´??°??§?????????????????\???????????¨??? int?????????????????????????????????
#menseki
a = r*r*math.pi
#ensyu
b = 2*r*math.pi
print("%.6f %.6f" % (a, b))
|
import sys
import time
import math
import itertools as it
def inpl():
return list(map(int, input().split()))
st = time.perf_counter()
# ------------------------------
N = int(input())
mn = 1001001001001
a = 1
while a*a <= N:
if N % a == 0:
mn = min(mn, a + N//a - 2)
a += 1
print(mn)
# ------------------------------
ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)
| 0 | null | 81,435,313,396,658 | 46 | 288 |
from functools import reduce
def cin(): return list(map(int,input().split()))
N, M, K = cin()
MOD = 998244353
fac = [1] * (N + 1)
inv = [1] * (N + 1)
for j in range(1, N + 1): fac[j] = fac[j - 1] * j % MOD
inv[N] = pow(fac[N], MOD - 2, MOD)
for j in range(N - 1, -1, -1): inv[j] = inv[j + 1] * (j + 1) % MOD
def cmb(n, r):
if r > n or n < 0 or r < 0: return 0
return fac[n] * inv[n - r] * inv[r] % MOD
ans = 0
for i in range(K + 1):
ans += M * cmb(N - 1, i) * pow(M - 1, N - 1 - i, MOD)
ans %= MOD
print(ans)
|
n,m=map(int,input().split())
h=list(map(int,input().split()))
ans=[1]*n
for i in range(m):
a,b=map(int,input().split())
a,b,=a-1,b-1
if h[a]==h[b]:
ans[a],ans[b]=0,0
elif h[a]>h[b]:
ans[b]=0
else:
ans[a]=0
print(ans.count(1))
| 0 | null | 24,192,906,243,004 | 151 | 155 |
h = int(input())
counter = 0
while h > 0 :
h //= 2
counter += 1
print(pow(2, counter) - 1)
|
while 1:
H,W=[int(i) for i in input().split()]
if H==W==0:
break
else:
if W<=2:
for i in range(H):
for s in range (W):
print("#",end="")
print('')
print('')
else:
for s in range(W):
print("#",end="")
print('')
for i in range(H-2):
print('#',end="")
for s in range (W-2):
print(".",end="")
print('#',end="")
print('')
for s in range(W):
print("#",end="")
print('')
print('')
| 0 | null | 40,260,898,582,970 | 228 | 50 |
#セグ木
from collections import deque
def f(L, R): return L|R # merge
def g(old, new): return old^new # update
zero = 0 #零元
class segtree:
def __init__(self, N, z):
self.M = 1
while self.M<N: self.M *= 2
self.dat = [z] * (self.M*2-1)
self.ZERO = z
def update(self, x, idx, l=0, r=-1):
if r==-1: r = self.M
idx += self.M-1
self.dat[idx] = g(self.dat[idx], x)
while idx > 0:
idx = (idx-1)//2
self.dat[idx] = f(self.dat[idx*2+1], self.dat[idx*2+2])
def query(self, a, b=-1, idx=0, l=0, r=-1):
if r==-1: r = self.M
if b==-1: b = self.M
q = deque([])
q.append([l, r, 0])
ret = self.ZERO
while len(q):
tmp = q.popleft()
L = tmp[0]
R = tmp[1]
if R<=a or b<=L: continue
elif a<=L and R<=b:
ret = f(ret, self.dat[tmp[2]])
else:
q.append([L, (L+R)//2, tmp[2]*2+1])
q.append([(L+R)//2, R, tmp[2]*2+2])
return ret
n = int(input())
s = list(input())
q = int(input())
seg = segtree(n+1, 0)
for i in range(n):
num = ord(s[i]) - ord("a")
seg.update((1<<num), i)
for _ in range(q):
a, b, c = input().split()
b = int(b) - 1
if a == "1":
pre = ord(s[b]) - ord("a")
now = ord(c) - ord("a")
seg.update((1<<pre), b)
seg.update((1<<now), b)
s[b] = c
else:
q = seg.query(b, int(c))
bin(q).count("1")
print(bin(q).count("1"))
|
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
| 0 | null | 31,622,767,044,788 | 210 | 55 |
import itertools as it
import math
def Dist (x1,y1,x2,y2):
dis = (x2 - x1)**2 + (y2 - y1)**2
return math.sqrt(dis)
N = int(input())
Pos = []
res = 0
for i in range(N):
pos = list(map(int,input().split()))
Pos.append(pos)
array = list(it.permutations(i for i in range(N)))
for i in range(math.factorial(N)):
for j in range(N-1):
res += Dist(Pos[array[i][j]][0],Pos[array[i][j]][1],Pos[array[i][j+1]][0],Pos[array[i][j+1]][1])
print(res / math.factorial(N))
|
N=int(input())
A=list(map(int,input().split(' ')))
acc = A[0]
S = sum(A)
ans= abs(S-2*acc)
for i in A[1:]:
acc += i
ans = min(ans,abs(S-2*acc))
print(ans)
| 0 | null | 145,542,344,111,680 | 280 | 276 |
N = int(input())
lis = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
def dfs(ans, keep, n):
if n == N + 1:
print(ans)
else:
for i in range(keep):
dfs(ans + lis[i], max(keep, i + 2), n + 1)
dfs('', 1, 1)
|
from copy import copy
n=int(input())
alpha="abcdefghij"
c=[0 for i in range(n)]
m=[0 for i in range(n)]
ans=[]
ans.append(copy(c))
while True:
c[-1]+=1
i=n-1
while i!=0 and c[i]-2==m[i-1]:
c[i]=0
i-=1
c[i]+=1
if i==0:
break
for i in range(1,n):
m[i]=max(m[i-1],c[i])
ans.append(copy(c))
for i in ans:
print("".join(map(lambda x:alpha[x],i)))
| 1 | 52,383,060,553,880 | null | 198 | 198 |
cnt = 0
zorome = 0
num = int(input())
while cnt < num:
mass = input().split()
if(mass[0] == mass[1]):
zorome += 1
if(zorome >= 3):
break
else:
zorome = 0
cnt += 1
print("Yes" if zorome >= 3 else "No")
|
n1 = int(input())
nums1 = list(map(int, input().split()))
n2 = int(input())
nums2 = list(map(int, input().split()))
def linear_search(nums1, n1, key):
nums1[n1] = key
index = 0
while nums1[index] != key:
index += 1
return index != n1
output = 0
nums1.append(None)
for key in nums2:
if linear_search(nums1, n1, key):
output += 1
print(output)
| 0 | null | 1,267,400,924,608 | 72 | 22 |
import numpy as np
def solve(n, x, y):
x = np.asarray(x)
y = np.asarray(y)
z = x + y
w = x - y
return max(np.max(z) - np.min(z), np.max(w) - np.min(w))
n = int(input())
x, y = zip(*[map(int, input().split()) for i in range(n)])
print(solve(n, x, y))
|
a,*b=[],
for z in[*open(0)][1:]:x,y=map(int,z.split());a+=x+y,;b+=x-y,
print(max(max(a)-min(a),max(b)-min(b)))
| 1 | 3,411,695,590,930 | null | 80 | 80 |
from collections import deque
n = int(input())
graph_W = []
for _ in range(n):
graph_W.append(list(map(int, input().split())))
num_root = [0]*n
work_queue = deque([])
visited = set()
work_queue.append(1)
visited.add(1)
cnt = 0
while work_queue:
here = work_queue.popleft() - 1
num_node = graph_W[here][1]
cnt += 1
if num_node == 0:
continue
for k in range(num_node):
num_graph = graph_W[here][k+2]
if num_graph not in visited:
work_queue.append(num_graph)
visited.add(num_graph)
num_root[num_graph-1] = num_root[here] + 1
print(1,0)
for i in range(1,n):
if num_root[i] != 0:
print(i+1,num_root[i])
else:
print(i+1,-1)
|
import sys
input = sys.stdin.readline
def main():
H, N = map(int, input().split())
A = []
B = []
for _ in range(N):
a, b = map(int, input().split())
A.append(a)
B.append(b)
A_max = max(A)
dp = [1e14] * (H+A_max + 1) # ダメージ量がiの最小コスト
dp[0] = 0
for i in range(H):
for j in range(N):
dp[i+A[j]] = min(dp[i+A[j]], dp[i]+B[j])
ans = 1e30
for i in range(H, H+A_max+1):
ans = min(ans, dp[i])
print(ans)
main()
| 0 | null | 40,532,239,925,508 | 9 | 229 |
a,b,c = sorted(map(int,raw_input().split()))
print a,b,c
|
import numpy as np
H,W = input().split()
H,W=int(H),int(W)
P=[0]*H
for i in range(H):
P[i] = input()
dp=np.zeros((H,W),dtype='u8')
if P[0][0]=='#':
dp[0][0]=1
else:
dp[0][0]=0
def DP(y,x):
global dp
if 0<=x<=W-1 and 0<=y<=H-1:
return dp[y][x]
else:
return 9999999
for l in range(1,H+W):
for i in range(l+1):
if i<H and l-i<W:
if P[i][l-i]=='#':
a=DP(i-1,l-i)
b=DP(i,l-i-1)
if a < 9999999:
if P[i-1][l-i]=='.':
a+=1
if b < 9999999:
if P[i][l-i-1]=='.':
b+=1
dp[i][l-i]=min(a,b)
else:
#print(i,l-i,DP(i-1,l-i),DP(i,l-i-1))
dp[i][l-i]=min(DP(i-1,l-i),DP(i,l-i-1))
print(dp[H-1][W-1])
| 0 | null | 24,664,739,312,328 | 40 | 194 |
N = int(input())
ans = set()
for i in range(N):
ans.add(input())
print(len(ans))
|
n = int(input())
s = [input() for _ in range(n)]
print(len(list(set(s))))
| 1 | 30,169,791,093,140 | null | 165 | 165 |
from itertools import product
n = int(input())
al = []
for _ in range(n):
curr_al = []
a = int(input())
for i in range(a):
x,y = map(int, input().split())
curr_al.append((x,y))
al.append(curr_al)
pattern = 2
ite = product(range(pattern),repeat=n)
ans = 0
for it in ite:
a_01_l = list(it)
curr_ans = a_01_l.count(1)
# print('-----')
# print(a_01_l)
for i, curr_it in enumerate(it):
if curr_it == 1:
check = True
for a in al[i]:
if a_01_l[a[0]-1] != a[1]:
check = False
if not check:
break
else:
# print('ok')
ans = max(curr_ans,ans)
print(ans)
|
s = input()
t = input()
change = len(t)
for x in range(len(s) - len(t) + 1):
temp_change = 0
for y in range(len(t)):
if s[x+y] != t[y]:
temp_change += 1
if change > temp_change:
change = temp_change
print(change)
| 0 | null | 62,773,483,857,800 | 262 | 82 |
S = input()
T = input()
if len(S) != len(T) - 1:
print('No')
exit(0)
for n in range(len(S)):
if S[n] != T[n]:
print('No')
exit(0)
print('Yes')
|
def main():
s = input()
t = input()
if s == t[0:len(t) - 1]:
print('Yes')
else:
print('No')
main()
| 1 | 21,556,984,359,032 | null | 147 | 147 |
x = int(input())
def hantei(x):
p = []
for i in range(2,int(x**(1/2))+1):
if (x % i) == 0:
return False
else:
return True
while 1:
if hantei(x):
print(x)
break
else:
x+=1
|
n,a,b = map(int,input().split())
ab = a + b
m = n // ab
n %= ab
print(m*a+min(n,a))
| 0 | null | 80,808,555,734,570 | 250 | 202 |
import sys
mapin = lambda: map(int, sys.stdin.readline().split())
listin = lambda: list(map(int, sys.stdin.readline().split()))
inp = lambda: sys.stdin.readline()
N = int(inp())
mp = {}
for i in range(N):
S = inp()
S = S[:-1]
if S in mp:
mp[S] += 1
else:
mp[S] = 1
ans = 0
vec = []
for i in mp.values():
if ans < i:
ans = i
for i in mp.keys():
if mp[i] == ans:
vec.append(i)
vec.sort()
for i in vec:
print(i)
|
N = int(input())
S = input()
abc = S.count('R')*S.count('G')*S.count('B')
L = (N-1)//2
for i in range(1,L+1):
for j in range(N-2*i):
if S[j]!=S[j+i] and S[j+i]!=S[j+2*i] and S[j]!=S[j+2*i]:
abc -= 1
print(abc)
| 0 | null | 52,898,338,367,766 | 218 | 175 |
N = int(input())
A = list(map(int,input().split())) + [0]
kabu = 0
money = 1000
for p in range(N):
#株がない時
if A[p] < A[p+1]:
kabu += money//A[p]
money -= (money//A[p])*A[p]
#株があるとき
if A[p] > A[p+1]:
money += kabu*A[p]
kabu = 0
#print(str(p+1) + "日目" + str(money) + " " +str(kabu))
print(money)
|
import math
N, K = map(int, input().split())
A = list(map(float, input().split()))
min_A = 0
max_A = 10**10
while( max_A - min_A > 1):
now = (min_A + max_A) // 2
temp = 0
for i in A:
if i > now:
temp += (i // now)
if temp > K:
min_A = now
else:
max_A = now
print(int(min_A) + 1)
| 0 | null | 6,960,837,495,170 | 103 | 99 |
str = [w for w in input()]
q = int(input())
for i in range(q):
command = [w for w in input().split()]
command[1] = int(command[1])
command[2] = int(command[2])
if command[0] == "print":
for i in range(command[1], command[2]+1):
print(str[i], end="")
print()
if command[0] == "replace":
j = command[1]
for i in range(len(command[3])):
str[j] = command[3][i]
j += 1
if command[0] == "reverse":
r = []
for i in range(command[2], command[1]-1, -1):
r.append(str[i])
i = command[1]
for w in r:
str[i] = w
i += 1
|
s = input()
q = int(input())
for _ in range(q):
line = input().split()
if line[0] == 'print':
print(s[int(line[1]):int(line[2]) + 1])
elif line[0] == 'reverse':
s1 = s[:int(line[1])]
s2 = s[int(line[1]):int(line[2]) + 1]
s2 = s2[::-1]
s3 = s[int(line[2]) + 1::]
s = s1 + s2 + s3
elif line[0] == 'replace':
s1 = s[:int(line[1])]
s2 = s[int(line[2]) + 1:]
s = s1 + line[3] + s2
| 1 | 2,109,957,211,360 | null | 68 | 68 |
n = int(input())
s = input()
x = n
for i in range(n-1):
if s[i] == s[i+1]:
x -= 1
print(x)
|
N = int(input())
S = input()
ans = 0
p = S[0]
for i in range(1, N):
if S[i] == p: continue
else:
ans += 1
p = S[i]
print(ans + 1)
| 1 | 170,681,837,527,358 | null | 293 | 293 |
a ,b = input().split(' ')
print(int(a) * int(b),(int(a) * 2) + (int(b) * 2))
|
ab=input()
a=int(ab.split()[0])
b=int(ab.split()[1])
s=a*b
l=(a+b)*2
print("{0} {1}".format(s,l))
| 1 | 299,977,218,618 | null | 36 | 36 |
x = int(input())
a = [1, 0]
print(a[x])
|
def solve():
x = int(input())
if x == 1:
print(0)
else:
print(1)
if __name__ == "__main__":
solve()
| 1 | 2,899,222,264,080 | null | 76 | 76 |
N = int(input())
li = []
for i in input().split():
li.append(int(i))
a = 1
count = 0
for i in li:
if a % 2 == 1 and i % 2 == 1:
count += 1
a += 1
print(count)
|
n = int(input())
a = False
for x in range(1,10):
for y in range(1,10):
if x*y == n:
print("Yes")
a = True
break
if a:
break
else:
print("No")
| 0 | null | 84,140,182,467,744 | 105 | 287 |
import sys
n = int(input())
x = sorted(list(map(int, input().split())))
if len(list(set(x))) == 1:
print(0)
sys.exit()
if n == 1:
print(0)
sys.exit()
x_min = x[0]
x_max = x[n-1]
min_sum = 10000000
for p in range(x_min, x_max):
sum = 0
for e in x:
len = (e - p)**2
sum += len
# print("p", p, "sum", sum, "min_sum", min_sum)
if sum <= min_sum:
min_sum = sum
print(min_sum)
|
def main():
n = int(input())
al = []
bl = []
for _ in range(n):
a, b = map(int, input().split())
al.append(a)
bl.append(b)
al.sort()
bl.sort()
if n%2:
print(bl[n//2]-al[n//2]+1)
else:
print(bl[n//2]+bl[n//2-1]-al[n//2]-al[n//2-1]+1)
if __name__ == "__main__":
main()
| 0 | null | 41,137,450,638,870 | 213 | 137 |
n=int(input())
H=[]
T=[]
for i in range (n):
a=list(map(str, input().split()))
if a[0]>a[1] :
H.append(3)
elif a[0]<a[1] :
T.append(3)
else :
H.append(1)
T.append(1)
print(sum(H), sum(T))
|
turn = int(input())
taro_score = 0
hanako_score = 0
for i in range(turn):
tcard,hcard = input().split()
tcard = str(tcard)
hcard = str(hcard)
taro_card = [0]*len(tcard)
hanako_card = [0]*len(hcard)
for j in range(len(tcard)):
taro_card[j] = ord(tcard[j])
for k in range(len(hcard)):
hanako_card[k] = ord(hcard[k])
if len(tcard) == len(hcard):
for l in range(len(tcard)):
if taro_card[l] > hanako_card[l]:
taro_score += 3
break
elif taro_card[l] < hanako_card[l]:
hanako_score += 3
break
else:
if l == len(tcard)-1:
taro_score += 1
hanako_score += 1
else:
continue
elif len(tcard) < len(hcard):
for a in range(len(tcard)):
if taro_card[a] > hanako_card[a]:
taro_score += 3
break
elif taro_card[a] < hanako_card[a]:
hanako_score += 3
break
else:
if a == len(tcard)-1:
#taro_score += 3
hanako_score += 3
else:
continue
else:
for b in range(len(hcard)):
if taro_card[b] > hanako_card[b]:
taro_score += 3
break
elif taro_card[b] < hanako_card[b]:
hanako_score += 3
break
else:
if b == len(hcard)-1:
#hanako_score += 3
taro_score += 3
else:
continue
print("%d %d"%(taro_score,hanako_score))
| 1 | 1,969,857,090,432 | null | 67 | 67 |
import sys
n = int(input())
ans = n*100/108
for i in [int(ans), int(ans)+1]:
if int(i*1.08) == n:
print(i)
sys.exit()
print(":(")
|
N=int(input())
P,Q=108,100
A=(N*Q+P-1)//P
B=((N+1)*Q+P-1)//P-1
if A>B:
print(":(")
else:
print(A)
| 1 | 126,075,449,144,834 | null | 265 | 265 |
from collections import deque
K=int(input())
q=deque([i for i in range(1,10)])
for _ in range(K-1):
x=deque.popleft(q)
a=x%10
if a!=0 and a!=9:
deque.append(q,x*10+a-1)
deque.append(q,x*10+a)
deque.append(q,x*10+a+1)
elif a==0:
deque.append(q,x*10)
deque.append(q,x*10+1)
else:
deque.append(q,x*10+8)
deque.append(q,x*10+9)
print(deque.popleft(q))
|
k=int(input())
que=list(range(1,9+1))
while True:
if len(que)>=k:
break
k-=len(que)
for q in que[:]:
que.remove(q)
lq=int(str(q)[-1])
for r in (lq-1,lq,lq+1):
if 0<=r<=9:
que.append(int(str(q)+str(r)))
if len(que)>=k:
break
que.sort()
print(que[k-1])
| 1 | 39,864,846,736,120 | null | 181 | 181 |
N, M, K = map(int, input().split())
mod = 998244353
facm = 200500
fac = [1] * facm
facinv = [1] * facm
for i in range(facm-1):
fac[i+1] = (fac[i] * (i + 1)) % mod
facinv[i+1] = (facinv[i] * pow(i+1, -1, mod)) % mod
def nCk(n, k):
return (fac[n] * facinv[k] * facinv[n-k]) % mod
ans = 0
for i in range(K+1):
ans = (ans + nCk(N-1, i) * pow(M-1, N-i-1, mod) * M) % mod
print(ans)
|
def is_ok(x):
trainings = 0
for i in range(N):
t = A[i] - x // F[i]
if t > 0:
trainings += t
return trainings <= K
N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))
A.sort()
F.sort(reverse=True)
ng = -1
ok = A[-1] * F[0]
while ok - ng > 1:
m = ng + (ok - ng) // 2
if is_ok(m):
ok = m
else:
ng = m
print(ok)
| 0 | null | 93,863,542,716,672 | 151 | 290 |
def if_3(t1,ans1,t2,ans2,ans3):print(ans1 if t1 else ans2 if t2 else ans3)
def if_2(t1,ans1,ans2):print(ans1 if t1 else ans2)
def YN(t):print("YES" if t else "NO")
def yn(t):print("yes" if t else "no")
def Yn(t):print("Yes" if t else "No")
def main():
import sys
pin=sys.stdin.readline
pout=sys.stdout.write
perr=sys.stderr.write
x=int(pin())
if_2(x==1,0,1)
return
main()
|
import bisect,collections,copy,itertools,math,string
def I(): return int(input())
def S(): return input()
def LI(): return list(map(int,input().split()))
def LS(): return list(input().split())
##################################################
H,W = LI()
S = [S() for _ in range(H)]
dp = [[-1]*W for _ in range(H)]
for i in range(H):
for j in range(W):
judge = 1 if S[i][j]=='#' else 0
if i==0: #0行目
if j==0:
dp[0][0] = judge
else:
dp[0][j] = dp[0][j-1]+judge*(0 if S[0][j-1]=='#' else 1)
else: #0行目以外
if j==0:
dp[i][0] = dp[i-1][0]+judge*(0 if S[i-1][j]=='#' else 1)
else:
temp1 = dp[i][j-1]+judge*(0 if S[i][j-1]=='#' else 1)
temp2 = dp[i-1][j]+judge*(0 if S[i-1][j]=='#' else 1)
dp[i][j] = min(temp1,temp2)
print(dp[-1][-1])
| 0 | null | 26,214,607,556,072 | 76 | 194 |
W = input()
count = 0
while True:
line = input()
if line == 'END_OF_TEXT':
break
for s in line.lower().split():
if s == W:
count += 1
print(count)
|
def make_divisors(n: int):
lower_divs = []
upper_divs = []
i = 1
while i**2 <= n:
if n % i == 0:
lower_divs.append(i)
if i != n // i:
upper_divs.append(n // i)
i += 1
return lower_divs + upper_divs[::-1]
n = int(input())
ans = len(make_divisors(n - 1)) - 1
for x in make_divisors(n)[1:]:
if x == 1:
continue
z = n
while z % x == 0:
z = z // x
z = z % x
if z == 1:
ans += 1
print(ans)
| 0 | null | 21,569,773,741,982 | 65 | 183 |
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)
|
N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
def condition(num):
count=0
s=N-1
t=0
while N-1>=t and s>=0:
if num>A[s]+A[t]:
t+=1
else:
count+=N-t
s-=1
return count>=M
subans=0
start=1
end=2*A[N-1]
while end-start>1:
test=(end+start)//2
if condition(test):
start=test
else:
end=test
if condition(end):
subans=end
else:
subans=start
data=[0]*N
count=0
s=N-1
t=0
while N-1>=t and s>=0:
if subans>A[s]+A[t]:
t+=1
else:
count+=N-t
data[s]=2*(N-t)
s-=1
ans=sum(data[i]*A[i] for i in range(0,N))
if count>M:
ans-=(count-M)*subans
print(ans)
| 0 | null | 110,593,014,281,776 | 255 | 252 |
from math import hypot
x1, y1, x2, y2 = map(float, input().strip().split())
print(hypot(x2 - x1, y2 - y1))
|
import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
# ----------------------------------------------
aa = str(input())
#print(aa)
tar = ord(aa)
if (tar <= 91):
print('A')
else:
print('a')
| 0 | null | 5,755,112,672,078 | 29 | 119 |
a,b = map(int,input().split())
for i in range(1,1300):
if int(i*0.08) == a and int(i*0.1) == b:
print(i)
exit()
print(-1)
|
S=input()
count = 0
for i in range(len(S)//2):
if S[i] != S[-1-i]:count+=1
print(count)
| 0 | null | 88,480,555,973,680 | 203 | 261 |
def divisors(n):
divisors = []
for i in range(1, int(n**0.5)+1):
if n%i == 0:
divisors.append(i)
if i != n//i:
divisors.append(n//i)
return sorted(divisors)
N = int(input())
ans = len(divisors(N-1)) -1
for k in divisors(N)[1:]:
N_ = N
while N_ % k == 0:
N_ = N_//k
if N_ % k == 1:
ans += 1
print(ans)
|
import math
def prime(x):
p = {}
last = math.floor(x ** 0.5)
if x % 2 == 0:
cnt = 1
x //= 2
while x & 1 == 0:
x //= 2
cnt += 1
p[2] = cnt
for i in range(3, last + 1, 2):
if x % i == 0:
x //= i
cnt = 1
while x % i == 0:
cnt += 1
x //= i
p[i] = cnt
if x != 1:
p[x] = 1
return p
N = int(input())
P = prime(N - 1)
if N == 2:
print(1)
exit()
ans = 1
for i in P.keys():
ans *= P[i] + 1
ans -= 1
P = prime(N)
D = [1]
for i in P.keys():
L = len(D)
n = i
for j in range(P[i]):
for k in range(L):
D.append(D[k] * n)
n *= i
for i in D:
if i == 1: continue
t = N
while (t / i == t // i):
t //= i
t -= 1
if t / i == t // i:
ans += 1
print(ans)
| 1 | 41,478,275,728,580 | null | 183 | 183 |
import sys
readline = sys.stdin.readline
def main():
A, B, M = map(int, readline().split())
a = list(map(int, readline().split()))
b = list(map(int, readline().split()))
ans = min(a) + min(b)
for _ in range(M):
x, y, c = map(int, readline().split())
ans = min(ans, a[x-1] + b[y-1] - c)
print(ans)
if __name__ == "__main__":
main()
|
a, b, m = map(int, input().split())
list_a = list(map(int, input().split()))
list_b = list(map(int, input().split()))
total = min(list_a) + min(list_b)
for i in range(m):
x, y, c = map(int, input().split())
price = list_a[x - 1] + list_b[y - 1] - c
total = min(price, total)
print(total)
| 1 | 53,881,533,397,952 | null | 200 | 200 |
m1, d1 = input().split()
m2, d2 = input().split()
if m1==m2:
print(0)
else:
print(1)
|
m1, d1 = map(int, input().split())
m2, d2 = map(int, input().split())
if m1 + 1 == m2 and d2 == 1:
print(1)
else:
print(0)
| 1 | 124,132,840,053,212 | null | 264 | 264 |
# -*- coding: utf-8 -*-
import sys
from math import ceil
for line in sys.stdin.readlines():
List = map(int, line.strip().split())
n = List[0]
yen = 100000
for i in xrange(n):
yen *= 1.05
yen = int(ceil(yen/1000)) * 1000
print yen
|
n = input()
s = 100000
for i in range(n):
s = s * 1.05
if s % 1000 != 0:
s = ((s // 1000) + 1) * 1000
print "%d" % s
| 1 | 1,281,883,648 | null | 6 | 6 |
A, B, C, K = map( int, input().split())
ret = min( A, K )
if A < K:
ret -= max( 0, K - A - B )
print( ret )
|
n = int(input())
s = input()
count = 1
for i in range(n-1):
if s[i+1] != s[i]:
count = count + 1
print(count)
| 0 | null | 96,010,040,134,074 | 148 | 293 |
from functools import reduce
n=int(input())
A=list(map(int, input().split()))
sumxor=reduce(lambda x,y:x^y,A)
for AA in A:
print(sumxor ^ AA)
|
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right
import sys,math,itertools,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
n = inp()
a = inpl()
now = a[0]
for i in range(1,n):
now ^= a[i]
for i in range(n):
print(now ^ a[i])
| 1 | 12,460,262,463,288 | null | 123 | 123 |
n, k = map(int,input().split())
w_in = list()
for i in range(n):
w_in.append(int(input()))
def alloc(p):
load = 0
track = 1
for w in w_in:
if w > p:
return False
if load + w <= p:
load += w
else:
load = w
track += 1
if track > k:
return False
return True
def binsearch():
upper = sum(w_in)
lower = max(w_in)
while(upper != lower):
mid = int((upper + lower) / 2)
if alloc(mid):
upper = mid
else:
lower = mid + 1
return upper
print(binsearch())
|
n = int(input())
a = [int(i) for i in input().split()]
color = [0, 0, 0]
ans = 1
mod = 10 ** 9 + 7
for i in a:
cnt = color.count(i)
ans *= cnt
ans = ans % mod
if ans > 0:
color[color.index(i)] += 1
else:
break
print(ans)
| 0 | null | 64,809,851,991,358 | 24 | 268 |
s=input()
t=s[::-1]
num=0
for i in range(len(s)):
if s[i]!=t[i]:
num+=1
print(num//2)
|
S = input()
S_rev = S[::-1]
cnt = 0
for i in range(len(S) // 2):
if S[i] != S_rev[i]:
cnt = cnt + 1
print(cnt)
| 1 | 120,251,890,434,320 | null | 261 | 261 |
print((int(input())+1)//2-1)
|
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT() : return int(input())
def MAP() : return map(int,input().split())
def LIST() : return list(MAP())
def dfs(s,d,i):
for j in range(i+1):
s[d] = chr(ord('a')+j)
if d < len(s) - 1:
dfs(s, d+1, max(j+1, i))
else:
a.add("".join(s))
n = INT()
s = ['a']*n
a = set()
dfs(s, 0, 0)
b = sorted(list(a))
for x in b:
print(x)
| 0 | null | 102,751,991,437,982 | 283 | 198 |
n = int(input())
ls = []
rs = []
for _ in range(n):
x, y = map(int, input().split())
ls.append(x)
rs.append(y)
ls = sorted(ls)
rs = sorted(rs)
if n % 2 == 1:
print(rs[len(rs)//2] - ls[len(ls)//2] + 1)
else:
a = (rs[len(rs)//2-1] * 10 + rs[len(rs)//2] * 10) // 2
b = (ls[len(ls)//2-1] * 10 + ls[len(ls)//2] * 10) // 2
print((a - b) // 5 + 1)
|
import sys
input = lambda: sys.stdin.readline().rstrip()
n = int(input())
a= [0]*n
b= [0]*n
for i in range(n):
a[i], b[i] = map(int, input().split())
if n%2 ==1:
a.sort()
b.sort()
xmin= a[int((n-1)/2)]
xmax= b[int((n - 1) / 2)]
print(int(xmax-xmin+1))
if n%2 ==0:
a.sort()
b.sort()
xmin = a[int(n/2-1)]+a[int(n/2)]
xmax = b[int(n / 2 - 1)] + b[int(n / 2)]
print(int((xmax-xmin)+1))
| 1 | 17,331,590,914,040 | null | 137 | 137 |
n=int(input())
if n%2==0:
print("0.5")
if n%2==1:
print((int(n/2)+1)/n)
|
# A Odds of Oddness
n = int(input())
c = []
for i in range(1,n+1):
if i % 2:
c.append(i)
x = len(c)/n
print(x)
| 1 | 177,099,107,242,552 | null | 297 | 297 |
N=int(input())
count=0
#N未満の値がaで割り切れさえすればいい
for a in range(1,N):
count = count + (N-1)//a
print(count)
|
A, B = [int(s) for s in input().split()]
ans = A * B
print(ans)
| 0 | null | 9,239,032,244,312 | 73 | 133 |
N=int(input())
S={}
for n in range(N):
s=input()
S[s]=S.get(s,0)+1
maxS=max(S.values())
S=[k for k,v in S.items() if v==maxS]
print('\n'.join(sorted(S)))
|
s=str("ACL")
n=int(input())
print(s*n)
| 0 | null | 35,875,343,578,240 | 218 | 69 |
N = int(input())
As = []
Bs = []
for _ in range(N):
c = 0
m = 0
for s in input():
if s == '(':
c += 1
elif s == ')':
c -= 1
m = min(m, c)
if c >= 0:
As.append((-m, c - m))
else:
Bs.append((-m, c - m))
As.sort(key=lambda x: x[0])
Bs.sort(key=lambda x: x[1], reverse=True)
f = True
c = 0
for (l, r) in As:
if c < l:
f = False
break
c += r - l
if f:
for (l, r) in Bs:
if c < l:
f = False
break
c += r - l
f = f and (c == 0)
if f:
print('Yes')
else:
print('No')
|
N = int(input())
s = []
for i in range(N):
s.append(input())
inc = []
dec = []
total = 0
for i in s:
cn = 0
mn = 0
for j in i:
if j=="(":
cn+=1
else:
cn-=1
mn = min(cn,mn)
total += cn
if cn>=0:
inc.append([mn,cn])
else:
dec.append([mn-cn,-cn])
inc.sort(reverse=True,key=lambda x:x[0])
dec.sort(reverse=True,key=lambda x:x[0])
def check(lis):
hi = 0
for i in lis:
if hi+i[0]<0:
return False
else:
hi+=i[1]
return True
if check(inc) and check(dec) and total==0:
print("Yes")
else:
print("No")
| 1 | 23,621,081,550,180 | null | 152 | 152 |
n=int(input())
l=list(map(int,input().split()))
dp=[[0 for j in range(n+1)] for i in range(n+1)]
e=[]
for i in range(n):
e.append([l[i],i])
e.sort(reverse=True)
for i in range(1,n+1):
p=e[i-1][0]
q=e[i-1][1]
for j in range(i+1):
if j<i:
a1=dp[i-1][j]+p*abs(n-(i-j)-q)
else:
a1=0
if j>0:
a2=dp[i-1][j-1]+p*abs(q-j+1)
else:
a2=0
dp[i][j]=max(a1,a2)
print(max(dp[-1]))
|
N = int(input())
A = list(map(int, input().split()))
A = sorted([(a, p) for p, a in enumerate(A)], reverse=True)
dp = [[0]*(i+1) for i in range(N+1)]
for z in range(N):
for y in range(z+1):
dp[z+1][y] = max(A[z][0]*(A[z][1]-(z-y))+dp[z][y],dp[z+1][y])
dp[z+1][y+1]= A[z][0]*((N-y-1)-A[z][1])+dp[z][y]
print(max(dp[-1]))
| 1 | 33,603,640,365,300 | null | 171 | 171 |
#import numpy as np
N = int(input())
a = list(map(int, input().split()))
all_xor = 0
for _a in a:
all_xor = all_xor ^ _a
for _a in a:
print(all_xor ^ _a)
|
N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
def condition(num):
count=0
s=N-1
t=0
while N-1>=t and s>=0:
if num>A[s]+A[t]:
t+=1
else:
count+=N-t
s-=1
return count>=M
subans=0
start=1
end=2*A[N-1]
while end-start>1:
test=(end+start)//2
if condition(test):
start=test
else:
end=test
if condition(end):
subans=end
else:
subans=start
data=[0]*N
count=0
s=N-1
t=0
while N-1>=t and s>=0:
if subans>A[s]+A[t]:
t+=1
else:
count+=N-t
data[s]=2*(N-t)
s-=1
ans=sum(data[i]*A[i] for i in range(0,N))
if count>M:
ans-=(count-M)*subans
print(ans)
| 0 | null | 60,571,892,303,168 | 123 | 252 |
import math
h1,m1,h2,m2,k=map(int,input().split())
start=60*h1+m1
end=60*h2+m2
print(end-start-k)
"""
a=[]
temp=input().split()
for i in range(n):
a.append(int(temp[i])-1)
routetemp=[]
routetemp.append(0)
while True:
if a[routetemp[-1]] in routetemp:
roop=len(routetemp)-routetemp.index(a[routetemp[-1]])
pre=routetemp.index(a[routetemp[-1]])
break
routetemp.append(a[routetemp[-1]])
#print(routetemp)
#print(a)
#print(roop)
#print(pre)
num=(k-pre)%roop
#print(num)
print(routetemp[pre+num]+1)
"""
|
s = list(map(int,input().split()))
h = 60 * (s[2]-s[0])
m = s[3] - s[1]
k = s[4]
print(h+m-k)
| 1 | 18,069,873,435,030 | null | 139 | 139 |
N = int(input())
S, T = input().split()
ans = ""
for p in range(N):
ans += S[p]
ans += T[p]
print(ans)
|
import math
def make_divisors(n):
lower_divisors , upper_divisors = [], []
i = 1
while i*i <= n:
if n % i == 0:
lower_divisors.append(i)
if i != n // i:
upper_divisors.append(n//i)
i += 1
return lower_divisors + upper_divisors[::-1]
n = int(input())
yaku = make_divisors(n)
Min = float('inf')
for i in range(math.ceil(len(yaku)/2)):
Min = min(Min, yaku[i]+yaku[-(i+1)])
print(Min-2)
| 0 | null | 136,874,820,849,762 | 255 | 288 |
n=int(input())
print((n//2+n%2)/n)
|
x = list(map(int, input().split()))
if 0 in x:
ans = x.index(0) + 1
print(ans)
| 0 | null | 95,772,642,476,462 | 297 | 126 |
n ,q = map(int, input().split())
ntlist = []
for i in range(n):
nt = list(map(str, input().split()))
ntlist.append(nt)
tp = 0
while (len(ntlist)):
nt = ntlist.pop(0)
if (int(nt[1])> q):
nt[1] = int(nt[1]) - q
tp += q
ntlist.append(nt)
else:
tp += int(nt[1])
nt[1] = 0
print(nt[0], tp)
|
class My_Queue:
def __init__(self, S):
self.S = S
self.q = [0 for i in range(S)]
self.head = 0
self.tail = 0
def enqueue(self, x):
if self.isFull():
print('overflow')
raise
else:
self.q[self.tail] = x
if self.tail + 1 == self.S:
self.tail = 0
else:
self.tail += 1
def dequeue(self):
if self.isEmpty():
print('underflow')
raise
else:
x = self.q[self.head]
self.q[self.head] = 0
if self.head + 1 == self.S:
self.head = 0
else:
self.head += 1
return x
def isEmpty(self):
return self.head == self.tail
def isFull(self):
return self.head == (self.tail + 1) % self.S
def main():
n, qms = map(int, input().split())
elapsed_time = 0
q = My_Queue(n + 1)
for i in range(n):
name, time = input().split()
time = int(time)
q.enqueue([name, time])
while(q.head != q.tail):
a = q.dequeue()
if a[1] <= qms:
elapsed_time += a[1]
print(a[0], elapsed_time)
else:
a[1] -= qms
elapsed_time += qms
q.enqueue(a)
if __name__ == "__main__":
main()
| 1 | 43,009,652,638 | null | 19 | 19 |
S=input()
if S[-1]=='s':
Ans=S+'es'
else:
Ans=S+'s'
print(Ans)
|
X=int(input())
a=int(X/500)
s=1000*a
X-=500*a
b=int(X/5)
s+=5*b
print(s)
| 0 | null | 22,667,801,139,408 | 71 | 185 |
N = int(input())
for i in range(11):
b = i * 1000
c = b - N
if c >= 0:
break
print(c)
|
n = int(input())
bez = 1000
while bez < n:
bez += 1000
print(bez-n)
| 1 | 8,479,483,155,780 | null | 108 | 108 |
# coding: utf-8
num = int(input())
number = input().split()
count, count2 = 0, 0
table = list(number)
for i in number:
if int(i) % 2 == 0:
count += 1
if int(i) % 3 == 0 or int(i) % 5 == 0:
count2 += 1
if count == count2:
print("APPROVED")
else:
print("DENIED")
|
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
# map(int, input().split())
N = int(input())
A = list(map(int, input().split()))
for a in A:
if a % 2 == 0:
if not (a % 3 == 0 or a % 5 == 0):
print("DENIED")
break
else:
print("APPROVED")
| 1 | 69,146,007,602,052 | null | 217 | 217 |
a,b,c = map(int,raw_input().split())
count = 0
if a == b :
if c % a == 0:
count += 1
else:
for i in range(a,b+1):
if c % i == 0:
count += 1
print count
|
import sys
def main():
for line in iter(sys.stdin.readline, ""):
#print (line)
a = line.rstrip("\n")
tmp = a.split(" ")
a = int(tmp[0])
b = int(tmp[1])
n = int(tmp[2])
cnt = 0
for i in range(1, n+1):
if (n % i == 0) and (a <= i and i <= b):
cnt = cnt + 1
#print (i)
print (cnt)
if __name__ == "__main__":
main()
| 1 | 554,221,495,412 | null | 44 | 44 |
s = input()
if s[-1] == 's':
s = s + 'e'
s = s + 's'
print(s)
|
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")
s = input()
if s[-1]=="s":
s += "es"
else:
s += "s"
print(s)
| 1 | 2,416,587,602,830 | null | 71 | 71 |
import math
n=int(input())
debt=100000
for i in range(n):
debt=math.ceil((debt*1.05)/1000)*1000
print(int(debt))
|
cnt = int(input())
chain = 0
first = 0
second = 0
for i in range(cnt):
nList = list(map(int, input().split()))
first = nList.pop()
second = nList.pop()
if first == second:
chain = chain + 1
if chain >= 3:
break
else:
chain = 0
if chain >= 3:
print("Yes")
else:
print("No")
| 0 | null | 1,260,573,964,002 | 6 | 72 |
def main():
n = int(input())
a = list(map(int,input().split()))
f,s = {},{}
for i in range(n):
if i+1-a[i] not in f.keys():
f[i+1-a[i]] = 1
else:
f[i+1-a[i]]+=1
if i+1+a[i] not in s.keys():
s[i+1+a[i]] = 1
else:
s[i+1+a[i]] += 1
ans = 0
for k in f.keys():
if k in s.keys():
ans += f[k] * s[k]
print(ans)
if __name__ == "__main__":
main()
|
from collections import defaultdict
N = int(input())
X = list(map(int, input().split()))
ctr = defaultdict(int)
ans = 0
for i in range(N):
ctr[i + X[i]] += 1
ans += ctr[i - X[i]]
print(ans)
| 1 | 26,160,065,791,360 | null | 157 | 157 |
a = input()
alp = [chr(i) for i in range(ord('a'),ord('z')+1)]
if a in alp:
print('a')
else:
print('A')
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
a = input()
if a.upper() == a:
print('A')
else:
print('a')
| 1 | 11,291,877,087,462 | null | 119 | 119 |
n = int(input())
a = [int(s) for s in input().split()]
ans = 0
for i in range(len(a)):
if i % 2 == 0 and a[i] % 2 == 1:
ans += 1
print(ans)
|
input()
li = list(map(int, input().split()))
result = 0
for i in range(len(li)):
if (i + 1) % 2 == 1:
if li[i] % 2 == 1:
result += 1
print(result)
| 1 | 7,801,329,733,136 | null | 105 | 105 |
# 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()
if (s * 2).find(p) >= 0:
print("Yes")
else:
print("No")
| 1 | 1,746,193,824,070 | null | 64 | 64 |
from math import ceil
n=int(input())
s=100000
for i in range(n):
s +=(s*0.05)
s=int(int(ceil(s/1000)*1000))
print(s)
|
# -*- coding: utf-8 -*-
import sys
from math import ceil
for line in sys.stdin.readlines():
List = map(int, line.strip().split())
n = List[0]
yen = 100000
for i in xrange(n):
yen *= 1.05
yen = int(ceil(yen/1000)) * 1000
print yen
| 1 | 1,135,568,320 | null | 6 | 6 |
n = int(input())
tarou = 0
hanako = 0
for i in range(n):
w1, w2 = input().split()
if w1 == w2:
tarou += 1
hanako += 1
elif w1 > w2:
tarou += 3
else:
hanako += 3
print(tarou, hanako)
|
N,M,L = (int(i) for i in input().split())
A = [[int(i) for i in input().split()] for i in range(N)]
B = [[int(i)for i in input().split()] for i in range(M)]
C = []
for i in range(N):
for i2 in range(L):
ans = 0
for i3 in range(M):
ans += A[i][i3]*B[i3][i2]
if i2 == L-1:
print(ans)
else:
print(ans,end=" ")
| 0 | null | 1,690,163,128,570 | 67 | 60 |
# -*- coding: utf-8 -*-
## Library
import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
import copy
## input
# N=int(input())
# A,B,C,D=map(int, input().split())
# S = input()
# yoko = list(map(int, input().split()))
# tate = [int(input()) for _ in range(N)]
# N, M = map(int,input().split())
# P = [list(map(int,input().split())) for i in range(M)]
# S = []
# for _ in range(N):
# S.append(list(input()))
N=int(input())
for i in range(1,N+1):
if floor(i*1.08)==N:
print(i)
sys.exit()
print(":(")
|
import math
n = int(input())
og = n
n /= 1.08
n = math.ceil(n)
if int(n * 1.08) == og:
print(n)
else:
print(":(")
| 1 | 125,349,745,547,648 | null | 265 | 265 |
from collections import deque
N = int(input())
A = deque(map(int, input().split()))
mod = 10**9 + 7
M = len(bin(max(A))) - 2
ans = 0
bitlist = [1]
anslist = [0 for _ in range(M)]
for k in range(M):
bitlist.append(bitlist[-1]*2%mod)
counter = [0 for _ in range(M)]
for k in range(N):
a = A.pop()
c = 0
while a:
b = a & 1
if b == 0:
anslist[c] += counter[-c-1]
else:
anslist[c] += k - counter[-c-1]
counter[-c-1] += 1
c += 1
a >>= 1
while c < M:
anslist[c] += counter[-c-1]
c += 1
for k in range(M):
ans += anslist[k]*bitlist[k]%mod
ans %= mod
print(ans)
|
n = int(input())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
MAX = 60
acc = [0] * MAX
ans = [0] * MAX
num = a[0]
for i in range(MAX):
if num & 1:
acc[i] = 1
num >>= 1
for i, e in enumerate(a[1:], 1):
for j in range(MAX):
if e & 1:
ans[j] += i - acc[j]
acc[j] += 1
else:
ans[j] += acc[j]
e >>= 1
ans_num = 0
for i, e in enumerate(ans):
ans_num += pow(2, i, mod) * e
ans_num %= mod
print(ans_num)
| 1 | 122,358,587,616,210 | null | 263 | 263 |
import math
input = int(input())
hour = math.floor(input/3600)
input -= hour*3600
min = math.floor(input/60)
input -= min*60
sec = math.floor(input)
print(str(hour)+":"+str(min)+":"+str(sec))
|
S = raw_input()
S = int(S)
h = S / 3600
m = (S - h * 3600) / 60
s = S - 3600 * h - 60 * m
print u"%d:%d:%d" % (h,m,s)
| 1 | 331,877,448,342 | null | 37 | 37 |
def LinearSearch1(S, n, t):
for i in range(n):
if S[i] == t:
return i
break
else:
return -1
"""
def LinearSearch2(S, n, t):
S.append(t)
i = 0
while S[i] != t:
i += 1
S.pop()
if i == n:
return -1
else:
return i
"""
n = int(input())
S = [int(s) for s in input().split()]
q = int(input())
T = {int(t) for t in input().split()}
ans = sum(LinearSearch1(S, n, t) >= 0 for t in T)
print(ans)
|
n = int(input())
S = set(input().split())
q = int(input())
T = set(input().split())
print(len(S & T))
| 1 | 64,588,525,782 | null | 22 | 22 |
from collections import Counter
n, k = map(int, input().split())
a = [x - 1 for x in map(int, input().split())]
sa = [0 for _ in range(n+1)]
for i in range(n):
sa[i+1] = (sa[i] + a[i]) % k
#print(sa)
if n < k:
cnt = Counter(sa)
ans = 0
for x in cnt.values():
ans += x * (x - 1) // 2
print(ans)
else:
cnt = Counter(sa[:k-1])
ans = 0
for i in range(n):
cnt[sa[i]] -= 1
if i + k - 1 <= n:
cnt[sa[i+k-1]] += 1
ans += cnt[sa[i]]
print(ans)
|
from itertools import combinations
def main():
while True:
n, x = map(int, input().split())
if (n, x) == (0, 0):
break
ans = 0
for nums in combinations(range(1, n + 1), 3):
if x == sum(nums):
ans += 1
print(ans)
if __name__ == "__main__":
main()
| 0 | null | 69,437,771,243,840 | 273 | 58 |
A,B=input().split()
A=int(A)
B=int(B[2:])+int(B[0])*100
print(A*B//100)
|
l=[int(input()) for _ in range(10)]
[print(x) for x in sorted(l, reverse=True)[:3]]
| 0 | null | 8,324,702,726,240 | 135 | 2 |
# import sys
# sys.setrecursionlimit(10 ** 6)
# import bisect
from collections import deque
# from decorator import stop_watch
#
#
# @stop_watch
def solve(H, W, Ss):
Ss = ['#' + S + '#' for S in Ss]
Ss = ['#' * (W + 2)] + Ss + ['#' * (W + 2)]
# for S in Ss:
# print(S)
ans = 0
for i in range(1, H + 1):
for j in range(1, W + 1):
if Ss[i][j] == '#':
continue
visited = [[-1] * (W + 2) for _ in range(H + 2)]
now = 0
visited[i][j] = now
q = deque([(i, j, now)])
while q:
x, y, n = q.popleft()
# for v in visited:
# print(v)
# input()
if Ss[x + 1][y] == '.' \
and visited[x + 1][y] < 0:
q.append((x + 1, y, n + 1))
visited[x + 1][y] = n + 1
if Ss[x - 1][y] == '.' \
and visited[x - 1][y] < 0:
q.append((x - 1, y, n + 1))
visited[x - 1][y] = n + 1
if Ss[x][y + 1] == '.' \
and visited[x][y + 1] < 0:
q.append((x, y + 1, n + 1))
visited[x][y + 1] = n + 1
if Ss[x][y - 1] == '.' \
and visited[x][y - 1] < 0:
q.append((x, y - 1, n + 1))
visited[x][y - 1] = n + 1
ans = max(ans, max([max(v) for v in visited]))
print(ans)
if __name__ == '__main__':
H, W = map(int, input().split())
Ss = [input() for _ in range(H)]
solve(H, W, Ss)
|
ab = list(map(int,input().split()))
ab.sort()
print(str(ab[0])*ab[1])
| 0 | null | 89,700,243,612,824 | 241 | 232 |
import math
a,b,c = map(float,input().split())
rad = math.radians(c)
x = (a**2+b**2-2*a*b*math.cos(rad))**(1/2)
L = a+b+x
S = a*b*math.sin(rad)/2
h = 2*S/a
print("{:.8f}\n{:.8f}\n{:.8f}".format(S,L,h))
|
import math
a,b,c=map(float,input().split())
c=math.radians(c)
h=b*math.sin(c)
s=a*h/2
d=math.sqrt(a**2+b**2-2*a*b*math.cos(c))
l=a+b+d
print(s,l,h)
| 1 | 179,054,074,250 | null | 30 | 30 |
n = int(input())
a = list(map(int, input().split()))
a.sort()
ls = [1] * (10 ** 6 + 1)
cnt = [0] * (10 ** 6 + 1)
for i in range(n):
cnt[a[i]] += 1
if cnt[a[i]] == 1:
if ls[a[i]] == 1:
for j in range(a[i] * 2, 10 ** 6 + 1, a[i]):
ls[j] = 0
ans = 0
for i in range(n):
if ls[a[i]] == 1 and cnt[a[i]] == 1:
ans += 1
print(ans)
|
#!/usr/bin/env python3
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
n=int(input())
A=list(map(int, input().split()))
counts=[0]*(10**6+5)
for AA in A:
counts[AA]+=1
ans=0
for i in range(1,10**6+5):
if counts[i]>0:
for j in range(i+i,10**6+5,i):
counts[j]=-1
if counts[i]==1:
ans+=1
print(ans)
if __name__ == '__main__':
main()
| 1 | 14,360,247,600,608 | null | 129 | 129 |
from collections import defaultdict
h, w, m = map(int, input().split())
targets = []
targets_count_w = defaultdict(int)
targets_count_h = defaultdict(int)
for _ in range(m):
y, x = map(int, input().split())
y -= 1
x -= 1
targets_count_w[x] += 1
targets_count_h[y] += 1
targets.append((y, x))
max_w = max(targets_count_w.values())
max_h = max(targets_count_h.values())
y_idx = defaultdict(bool)
x_idx = defaultdict(bool)
max_count_x = 0
max_count_y = 0
for i in range(w):
if targets_count_w[i] == max_w:
x_idx[i] = True
max_count_x += 1
for i in range(h):
if targets_count_h[i] == max_h:
y_idx[i] = True
max_count_y += 1
ans = max_w + max_h
kumi = max_count_x*max_count_y
for ty, tx in targets:
if y_idx[ty] and x_idx[tx]:
kumi -= 1
if kumi == 0:
break
if kumi == 0:
print(ans-1)
else:
print(ans)
|
s = input()
a = input()
print("Yes" if a[:-1]==s else "No")
| 0 | null | 13,091,584,685,440 | 89 | 147 |
a,b=map(int,input().split())
c=b*10
for i in range(c,c+10):
if int(i*0.08)==a:
print(i)
exit()
print(-1)
|
import math
A, B = map(int, input().split())
arange = set(range(math.ceil(A*100/8), math.ceil((A+1)*100/8)))
brange = set(range(math.ceil(B*100/10), math.ceil((B+1)*100/10)))
ans_range = arange & brange
if len(ans_range) == 0:
print(-1)
else:
print(min(ans_range))
| 1 | 56,361,102,336,380 | null | 203 | 203 |
import sys
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()
sys.setrecursionlimit(10**6)
n = ni()
a = na()
b = []
for i in range(n):
b.append((a[i],i))
b.sort(reverse=True)
dp = [[0]*(n+1) for i in range(n+1)]
for i in range(1,n+1):
x,y = b[i-1]
for j in range(i+1):
left = dp[i-1][j-1] + x*abs(y-j+1) if j > 0 else -1
right = dp[i-1][j] + x*abs(n-i+j-y) if j < i else -1
dp[i][j] = max(left,right)
print(max(dp[-1]))
|
x = int(input())
k = 1
while k*x%360:
k += 1
print(k)
| 0 | null | 23,401,285,453,030 | 171 | 125 |
import sys
import bisect
from math import ceil
from itertools import accumulate
n, d, a = [int(i) for i in sys.stdin.readline().split()]
monsters = []
max_x = 0
for i in range(n):
x, h = [int(i) for i in sys.stdin.readline().split()]
max_x = max(x, max_x)
monsters.append([x, h])
monsters.sort(key=lambda x:x[0])
x, h = zip(*monsters)
x_ls = []
for i, (_x, _h) in enumerate(zip(x, h)):
ind = bisect.bisect_right(x, _x + 2 * d)
x_ls.append([i, ind, _h])
h = list(h) + [0]
ls = [0 for i in range(n+1)]
cur = 0
res = 0
for i, ind, _h in x_ls:
if h[i] > 0:
cur -= ls[i]
h[i] -= cur
if h[i] > 0:
hoge = ceil(h[i] / a)
res += hoge
cur += hoge * a
ls[ind] += hoge * a
print(res)
|
n = int(input())
x = list(input())
count = x.count('1')
one_count = count - 1
zero_count = count + 1
one_mod = 0
zero_mod = 0
for b in x:
if one_count:
one_mod = (one_mod * 2 + int(b)) % one_count
zero_mod = (zero_mod * 2 + int(b)) % zero_count
f = [0] * 2000001
pop_count = [0] * 200001
for i in range(1, 200001):
pop_count[i] = pop_count[i//2] + i % 2
f[i] = f[i % pop_count[i]] + 1
for i in range(n):
if x[i] == '1':
if one_count:
nxt = one_mod
nxt -= pow(2, n-i-1, one_count)
nxt %= one_count
print(f[nxt] + 1)
else:
print(0)
if x[i] == '0':
nxt = zero_mod
nxt += pow(2, n-i-1, zero_count)
nxt %= zero_count
print(f[nxt] + 1)
| 0 | null | 45,244,921,782,250 | 230 | 107 |
import bisect, collections, copy, heapq, itertools, math, string
from functools import reduce
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
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())
N = I()
S = S()
R_num = [n for n, v in enumerate(S) if v == 'R']
G_num = [n for n, v in enumerate(S) if v == 'G']
B_num = [n for n, v in enumerate(S) if v == 'B']
cnt = 0
for r in R_num:
for g in G_num:
x = r + g
if x % 2 == 0:
if x // 2 in B_num:
cnt += 1
for g in G_num:
for b in B_num:
x = g + b
if x % 2 == 0:
if x // 2 in R_num:
cnt += 1
for r in R_num:
for b in B_num:
x = r + b
if x % 2 == 0:
if x // 2 in G_num:
cnt += 1
ans = len(R_num) * len(G_num) * len(B_num) - cnt
print(ans)
|
while(True):
h,w=map(int,input().split())
if h==0 and w==0:
break
rect='#'*w+'\n'
rect+=('#'+'.'*(w-2)+'#\n')*(h-2)
rect+='#'*w+'\n'
print(rect)
| 0 | null | 18,349,824,089,312 | 175 | 50 |
import sys
input = sys.stdin.readline
n, k = map(int, input().split())
*a, = map(int, input().split())
s = [0]*(n+1)
for i in range(len(a)):
s[i+1] = s[i] + a[i]
for i in range(len(s)):
s[i] = (s[i] - i)%k
from collections import defaultdict
cnt = defaultdict(int)
left = 0
right = k-1
if right > n:
right = n
for i in range(right+1):
cnt[s[i]] += 1
ans = 0
while left < right:
ans += cnt[s[left]]-1
if right == n:
cnt[s[left]] -= 1
left += 1
else:
cnt[s[left]] -= 1
left += 1
right += 1
cnt[s[right]] += 1
print(ans)
|
import sys
import math
import heapq
import bisect
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())
def FLIST(n):
res = [1]
for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
return res
def gcd(x, y):
if x < y: x, y = y, x
div = x % y
while div != 0:
x, y = y, div
div = x % y
return y
# j - i == S[j] - S[i] # MOD K
# S[i] - i == S[j] - j
N,K=LI()
As=LI()
Cum=[0]*(N+1)
for i in range(N):
Cum[i+1] = Cum[i] + As[i]
REM2IDX = {}
for i in range(0,N+1):
rem = (Cum[i] - i) % K
if not rem in REM2IDX: REM2IDX[rem] = []
REM2IDX[rem].append(i)
res = 0
for i in range(0,N+1):
rem = (Cum[i] - i) % K
cnt = len(REM2IDX[rem])
l = bisect.bisect_right(REM2IDX[rem], i)
r = bisect.bisect_right(REM2IDX[rem], i+K-1)
res += (r-l)
print(res)
| 1 | 137,842,301,432,000 | null | 273 | 273 |
n, m = map(int, input().split())
route = [[] for _ in range(n+1)]
ans = [0]*n
for _ in range(m):
a, b = map(int, input().split())
route[a].append(b)
route[b].append(a)
q = [1]
l = set()
while True:
if len(q) == 0:
break
p = q.pop(0)
for i in route[p]:
if i not in l:
l.add(i)
ans[i-1] = p
q.append(i)
if ans.count(0) > 1:
print("No")
else:
print("Yes")
for i in range(1, n):
print(ans[i])
|
while True:
card=input()
if card=='-': break
loop=int(input())
for i in range(loop):
ch=int(input())
card=card[ch:]+card[:ch]
print(card)
| 0 | null | 11,208,300,235,012 | 145 | 66 |
s = input()
a = s[:len(s)//2]
b = s[len(s)//2+1:]
print('Yes' if s == s[::-1] and a == a[::-1] and b == b[::-1] else 'No')
|
S = input()
N = len(S)
ans = [0] * (N + 1)
for i in range(N):
if S[i] == "<":
ans[i + 1] = ans[i] + 1
for i in range(N - 1, -1, -1):
if S[i] == ">":
if ans[i] <= ans[i + 1]:
ans[i] = ans[i + 1] + 1
print(sum(ans))
| 0 | null | 101,162,107,754,190 | 190 | 285 |
a = list(input())
if a[-1] == 's':
a.append('e')
a.append('s')
else:
a.append('s')
print(''.join(a))
|
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def main():
mod=10**9+7
S=input()
if S[-1]=="s":
S+="es"
else:
S+="s"
print(S)
main()
| 1 | 2,364,882,660,430 | null | 71 | 71 |
N = int(input())
ans = 1000*(1+(int)((N-1)/1000))-N
print(ans)
|
import sys
def input(): return sys.stdin.readline().rstrip()
N = int(input())
num = (N + 1000 - 1 )// 1000
print(num * 1000 - N)
| 1 | 8,402,701,464,080 | null | 108 | 108 |
n=int(input())
d=list(map(int,input().split()))
r=1 if d[0]==0 else 0
d=d[1:]
d.sort()
c=1
nc=0
j=1
mod=998244353
for i in d:
if i<j:
r=0
elif i==j:
nc+=1
r=(r*c)%mod
elif i==j+1:
j=i
c=nc
nc=1
r=(r*c)%mod
else:
r=0
print(r)
|
import sys
input = sys.stdin.readline
def main():
N = int(input())
A = list(map(int, input().split()))
node_count = [0] * (N+1)
for i, a in enumerate(A[::-1]):
if i == 0:
node_count[N] = a
continue
node_count[N-i] = node_count[N-i+1] + a
can_build = True
for i, a in enumerate(A):
if i == 0:
if N > 0 and a > 0:
can_build = False
break
if N == 0 and a > 1:
can_build = False
break
node_count[0] = min(node_count[0], 1)
continue
if (i < N and a >= node_count[i-1]*2) or (i == N and a > node_count[i-1]*2):
can_build = False
break
node_count[i] = min(node_count[i], node_count[i-1]*2-A[i-1]*2)
if can_build:
ans = sum(node_count)
else:
ans = -1
print(ans)
if __name__ == "__main__":
main()
| 0 | null | 87,223,203,540,960 | 284 | 141 |
#!/usr/bin/env python3
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)
N = int(input())
C = input()
left = 0
right = N-1
count = 0
while left < right:
if C[left] == 'W' and C[right] == 'R':
count += 1
left += 1
right -= 1
if C[left] == 'R':
left += 1
if C[right] == 'W':
right -= 1
print(count)
|
import sys
input = sys.stdin.readline
from bisect import bisect_left
from itertools import accumulate
def func(A, N, M, x):
count = 0
for Ai in A:
idx = bisect_left(A, x - Ai)
count += N - idx
if count >= M:
return True
else:
return False
def main():
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))
A_rev = list(reversed(A))
B = [0] + list(accumulate(A_rev))
min_ = 0
max_ = 2 * 10 ** 5 + 1
while max_ - min_ > 1:
mid = (min_ + max_) // 2
if func(A, N, M, mid):
min_ = mid
else:
max_ = mid
ans = 0
count = 0
for Ai in A_rev:
idx = bisect_left(A, min_-Ai)
ans += Ai * (N - idx) + B[N-idx]
count += N - idx
print(ans-(count-M)*min_)
if __name__ == '__main__':
main()
| 0 | null | 57,284,521,835,838 | 98 | 252 |
n, p = map(int, input().split())
s = [int(i) for i in input()]
p_cnt = 0
if p == 2 or p == 5:
for i in range(n):
if s[i] % p == 0:
p_cnt += i+1
else:
s = s[::-1]
div_dic = dict(zip(range(p), [0] * p))
tmp = 0
for i in range(n):
tmp += s[i] * pow(10, i, p)
tmp %= p
div_dic[tmp] += 1
for v in div_dic.values():
p_cnt += v * (v - 1)
p_cnt //= 2
p_cnt += div_dic.get(0)
print(p_cnt)
|
from collections import Counter
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
def main():
N, P = map(int, readline().split())
S = [int(i) for i in readline().strip()[::-1]]
ans = 0
if P == 2 or P == 5:
for i, s in enumerate(S):
if s % P == 0:
ans += N - i
print(ans)
exit()
a = [0]
s = 0
for i in range(N):
s += S[i] * pow(10, i, P)
a.append(s % P)
t = Counter(a)
ans = 0
for v in t.values():
ans += v * (v-1) // 2
print(ans)
if __name__ == "__main__":
main()
| 1 | 58,018,982,024,612 | null | 205 | 205 |
rst = []
N = int(input())
S = input()
tmp_val = ''
for i in S:
if tmp_val != i:
rst.append(i)
tmp_val = i
print(len(rst))
|
n = int(input())
s = list(input())
b = 0
d = s[0]
for i in range(n):
if d != s[i]:
d = s[i]
b += 1
print(b+1)
| 1 | 170,623,747,503,132 | null | 293 | 293 |
N = int(input())
count = [[0] * 10 for _ in range(10)]
for i in range(1, N + 1):
s = str(i)
count[int(s[0])][int(s[-1])] += 1
ans = sum(count[i][j] * count[j][i] for i in range(1, 10) for j in range(1, 10))
print(ans)
|
n = int(input())
mod = 10 ** 9 + 7
dp = [[[0 for _ in range(2)] for _ in range(2)] for _ in range(n)]
dp[0][0][0] = 8
dp[0][0][1] = 1
dp[0][1][0] = 1
dp[0][1][1] = 0
for i in range(n-1):
for j in range(2):
dp[i+1][0][0] = (dp[i][0][0] * 8) % mod
dp[i+1][0][1] = (dp[i][0][0] + dp[i][0][1] * 9) % mod
dp[i+1][1][0] = (dp[i][0][0] + dp[i][1][0] * 9) % mod
dp[i+1][1][1] = (dp[i][0][1] + dp[i][1][0] + dp[i][1][1] * 10) % mod
print((dp[-1][-1][-1]) % mod)
| 0 | null | 44,707,367,574,208 | 234 | 78 |
n, q = list(map(int, input().split()))
que = []
for i in range(n):
p, t = input().split()
que.append([p, int(t)])
total = 0
while True:
if q >= que[0][1]:
total += que[0][1]
print(que[0][0], total)
que.pop(0)
else:
total += q
que[0][1] -= q
que.append(que[0])
que.pop(0)
if len(que) == 0:
break
|
# coding=utf-8
import sys
n, q = map(int, sys.stdin.readline().split())
queue = []
total_time = 0
finished_loop = 0
for i in range(n):
name, time = input().split()
queue.append([name, int(time)])
while queue:
n -= finished_loop
finished_loop = 0
for i in range(n):
poped = queue.pop(0)
name = poped[0]
time = poped[1]
if time > q:
queue.append([name, time - q])
total_time += q
else:
total_time += time
print(name, total_time)
finished_loop += 1
| 1 | 43,018,315,650 | null | 19 | 19 |
N, K = map(int, input().split())
INF = 998244353
LR = []
for i in range(K):
LR.append(list(map(int, input().split())))
dp = [0] * N
dp[0] = 1
a_sum = [0] * N
a_sum[0] = 1
for i in range(1, N):
for lr in LR:
if i - lr[0] >= 0:
if i - lr[1]-1 >= 0:
# print(i, lr, dp[i], i-lr[0], i-lr[1]-1, a_sum[i-lr[0]], a_sum[i-lr[1]-1])
dp[i] = dp[i] + a_sum[i-lr[0]] - a_sum[i-lr[1]-1]
else:
dp[i] = dp[i] + a_sum[i-lr[0]]
dp[i] %= INF
a_sum[i] = a_sum[i-1] + dp[i]
a_sum[i] %= INF
# print(i, dp, a_sum)
print(dp[-1])
|
n = int(input())
a0 = 1
a1 = 1
#print(a0, a1, end='')
if n == 0 or n == 1:
a2 = 1
else:
for i in range(n-1):
a2 = a0 + a1
a0 = a1
a1 = a2
#print('',a2,end='')
print(a2)
| 0 | null | 1,324,977,719,482 | 74 | 7 |
n, m, k = map(int, input().split())
mod = 998244353
def powerDX(n, r, mod):
if r == 0: return 1
if r%2 == 0:
return powerDX(n*n % mod, r//2, mod) % mod
if r%2 == 1:
return n * powerDX(n, r-1, mod) % mod
def cmb(n, r, mod):
if ( r<0 or r>n ):
return 0
r = min(r, n-r)
return g1[n] * g2[r] * g2[n-r] % mod
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]
for i in range(2, n + 1 ):
g1.append( ( g1[-1] * i ) % mod )
inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
g2.append( (g2[-1] * inverse[-1]) % mod )
ans = 0
for i in range(0, k+1):
ans += m*cmb(n-1, i, mod)*pow(m-1, n-i-1, mod)
ans %= mod
print(ans)
|
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush
from decimal import *
##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)
##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除&その最小値を出力
# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと
##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順
# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順
##### 累乗 #####
# pow(x, y, z) -> x**y % z
##### 割り算の切り上げ #####
# tmp = -(-4 // 3)
##### dict の for文 #####
# for k, v in d.items():
# print(k, v)
##### 進数の変換 #####
# bin_str = bin(i)
# oct_str = oct(i)
# hex_str = hex(i)
def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]
inf = float('inf')
mod = 1000000007
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
def main():
N,M,K = inputMap()
cnv = Combination(N, 998244353)
ans = 0
for i in range(K+1):
blc = N-i
tmp = pow(M-1, blc-1, 998244353)
tmp = (M * tmp) % 998244353
prn = cnv(N-1, i)
tmp = (tmp * prn) % 998244353
ans = (ans + tmp) % 998244353
print(ans)
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# N 個のボールを K グループに分ける場合のパターン数
def sunuke(N, K, mod=10**9+7):
if N < K or K-1 < 0:
return 0
else:
return combination(N-1, K-1, mod)
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
r = min(r, n-r)
res = 1
for i in range(r):
res = res * (n - i) * modinv(i+1, mod) % mod
return res
# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
return pow(a, mod-2, mod)
def egcd(a, b):
if a == 0:
return b, 0, 1
else:
g, y, x = egcd(b % a, a)
return g, x - (b // a) * y, y
# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
# comb = Combination(n+r-1, mod)
# return comb(n+r-1, r)
return combination(n+r-1, r, mod)
class Combination:
"""
O(n)の前計算を1回行うことで,O(1)でnCr mod mを求められる
n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
使用例:
comb = Combination(1000000)
print(comb(5, 3)) # 10
"""
def __init__(self, n_max, mod=10**9+7):
self.mod = mod
self.modinv = self.make_modinv_list(n_max)
self.fac, self.facinv = self.make_factorial_list(n_max)
def __call__(self, n, r):
return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod
def make_factorial_list(self, n):
# 階乗のリストと階乗のmod逆元のリストを返す O(n)
# self.make_modinv_list()が先に実行されている必要がある
fac = [1]
facinv = [1]
for i in range(1, n+1):
fac.append(fac[i-1] * i % self.mod)
facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
return fac, facinv
def make_modinv_list(self, n):
# 0からnまでのmod逆元のリストを返す O(n)
modinv = [0] * (n+1)
modinv[1] = 1
for i in range(2, n+1):
modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
return modinv
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# dfs のサンプル
def dfs(graph,parent,counter,edge):
stk = []
stk.append(edge)
while len(stk) > 0:
p = stk.pop()
for e in graph[p]:
if parent[p] == e:
continue
else:
parent[e] = p
counter[e] += counter[p]
stk.append(e)
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
main()
| 1 | 23,015,942,259,770 | null | 151 | 151 |
#!/usr/bin/env python3
def main():
N, K = map(int, input().split())
mod = 7 + 10 ** 9
res = 0
for k in range(K, N + 2):
res += ((k * (2 * N - k + 1) / 2) - (k * (k - 1) / 2) + 1)
res %= mod
print(int(res))
if __name__ == '__main__':
main()
|
N,K = map(int,input().split())
counter = 0
for k in range(K,N+2):
counter += k*(N-k+1) + 1
print(counter%(pow(10,9)+7))
| 1 | 33,186,397,327,200 | null | 170 | 170 |
from itertools import accumulate
H, W, K = map(int, input().split())
s = [input() for _ in range(H)]
ans = []
acc = 0
l = 0
for i in s:
cnt = i.count("#")
if cnt == 0:
if not ans:
l += 1
else:
ans.append(ans[-1])
else:
k = [0] * W
for j in range(W):
if i[j] == "#":
k[j] = 1
k = list(accumulate(k))
for j in range(W):
if k[j] == 0:
k[j] = 1
k[j] += acc
ans.append(k)
if ans and l >= 1 :
for _ in range(l):
ans.append(ans[-1])
l = 0
acc += cnt
for i in ans:
print(" ".join(map(str, i)))
|
def main():
h,w,k=map(int,input().split())
grid=[input() for _ in [0]*h]
ans=[[0]*w for _ in [0]*h]
berry=0
for i in range(h):
if "#" in grid[i]:
cnt=0
berry+=1
for j in range(w):
if grid[i][j]=="#":
cnt+=1
if cnt>1:
berry+=1
ans[i][j]=berry
for i in range(1,h):
if ans[i][0]==0:
for j in range(w):
ans[i][j]=ans[i-1][j]
for i in range(h-2,-1,-1):
if ans[i][0]==0:
for j in range(w):
ans[i][j]=ans[i+1][j]
for i in ans:
print(*i)
main()
| 1 | 143,667,406,818,852 | null | 277 | 277 |
while 1:
a=input()
if a=='0':break
print(sum([int(s) for s in list(a)]))
|
while 1:
numbers = [i for i in raw_input()]
if numbers[0] == '0': break
print sum(map(int,numbers))
| 1 | 1,555,884,779,972 | null | 62 | 62 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.