code1
stringlengths 17
427k
| code2
stringlengths 17
427k
| similar
int64 0
1
| pair_id
int64 2
181,637B
⌀ | question_pair_id
float64 3.7M
180,677B
⌀ | code1_group
int64 1
299
| code2_group
int64 1
299
|
---|---|---|---|---|---|---|
import collections
N = int(input())
S = [input() for _ in range(N)]
c = collections.Counter(S)
print(len(c))
|
h,n = map(int,input().split())
Mag = []
for i in range(n):
AB = list(map(int,input().split()))
Mag.append(AB)
dp = [[float("inf")]*(h+1) for i in range(n+1)]
dp[0][0] = 0
for i in range(n):
for j in range(h+1):
dp[i+1][j] = min(dp[i+1][j],dp[i][j])
dp[i+1][min(j+Mag[i][0],h)] = min(dp[i+1][min(j+Mag[i][0],h)],dp[i+1][j]+Mag[i][1])
print(dp[n][h])
| 0 | null | 55,402,740,938,354 | 165 | 229 |
from itertools import permutations
if __name__ == "__main__":
N = int(input())
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
arr = [i for i in range(1,N+1)]
pattern_arr = list(permutations(arr))
a,b = -1,-1
for i,pattern in enumerate(pattern_arr):
if pattern == P:
a = i
if pattern == Q:
b = i
print(abs(a-b))
|
import itertools
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
# Nが10程度で小さい
# 階乗の全探索をしても間に合う
# Pythonならitertoolsが使える
# permutations(関数)にlistを渡すと、順列を生成!
l = [i for i in range(1, N+1)]
permutations_l = list(itertools.permutations(l))
a, b = 0, 0
for i, R in enumerate(permutations_l):
if R == P: a = i
for i, R in enumerate(permutations_l):
if R == Q: b = i
print(abs(a-b))
| 1 | 100,740,561,801,694 | null | 246 | 246 |
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)
|
n=int(input())
a=list(map(int,input().split()))
x=0
for i in range(n):
x^=a[i]
for j in range(n):
print(x^a[j],end=" ")
| 1 | 12,379,977,583,834 | null | 123 | 123 |
def solve():
a, b, k = map(int, input().split())
if a > k:
print(a-k, b)
elif a <= k < a+b:
print(0, b-k+a)
else:
print(0, 0)
if __name__ == '__main__':
solve()
|
A, B, K = map(int, input().split())
A_after = 0
B_after = 0
if A - K >= 0:
A_after = A - K
B_after = B
elif A + B - K >= 0:
B_after = B - (K - A)
print(A_after, B_after)
| 1 | 104,459,662,454,280 | null | 249 | 249 |
import sys
N=int(sys.stdin.readline())
# ans=sum((N//x)*(N//x+1)*x//2 for x in range(1,N+1))
ans=0
for x in range(1,N+1):
y=N//x
ans+=y*(y+1)*x//2
print(ans)
|
n = int(input())
def cal(i):
s = (i + i*(n//i))*(n//i)*0.5
return s
ans = 0
for i in range(1, n+1):
ans += cal(i)
print(int(ans))
| 1 | 11,009,147,332,650 | null | 118 | 118 |
import bisect
N=int(input())
S=list(str(input()))
def ci(x):
return "abcdefghijklmnopqrstuvwxyz".find(x)
d=[[] for _ in range(26)]
for i,s in enumerate(S):
d[ci(s)].append(i)
for i in range(int(input())):
t,x,y=input().split()
if t=="1":
x=int(x)-1
if S[x]!=y:
l=bisect.bisect_left(d[ci(S[x])],x)
d[ci(S[x])].pop(l)
bisect.insort(d[ci(y)],x)
S[x]=y
else:
x,y=int(x)-1,int(y)-1
c=0
for j in range(26):
l=bisect.bisect_left(d[j],x)
if l<len(d[j]) and d[j][l] <= y:
c += 1
print(c)
|
class BinaryIndexedTree():
def __init__(self, max_n):
self.size = max_n + 1
self.tree = [0] * self.size
self.depth = self.size.bit_length()
def initialize(self, seq):
for i, x in enumerate(seq[1:], 1):
self.tree[i] += x
j = i + (i & (-i))
if j < self.size:
self.tree[j] += self.tree[i]
def __repr__(self):
return self.tree.__repr__()
def get_sum(self, i):
s = 0
while i:
s += self.tree[i]
i -= i & -i
return s
def add(self, i, x):
while i < self.size:
self.tree[i] += x
i += i & -i
def find_kth_element(self, k):
x, sx = 0, 0
dx = (1 << self.depth)
for i in range(self.depth - 1, -1, -1):
dx = (1 << i)
if x + dx >= self.size:
continue
y = x + dx
sy = sx + self.tree[y]
if sy < k:
x, sx = y, sy
return x + 1
n = int(input())
s = ["!"]+list(input())
q = int(input())
bit = [BinaryIndexedTree(n+1) for i in range(26)]
for i in range(n):
ind = ord(s[i+1])-97
bit[ind].add(i+1,1)
for i in range(q):
a,b,c = input().split()
b = int(b)
if a == "1":
ind = ord(s[b])-97
bit[ind].add(b,-1)
ind = ord(c)-97
bit[ind].add(b,1)
s[b] = c
else:
ans = 0
c = int(c)
for i in range(26):
if bit[i].get_sum(c)-bit[i].get_sum(b-1):
ans += 1
print(ans)
| 1 | 62,627,973,112,200 | null | 210 | 210 |
MOD = 998244353
class mint:
def __init__(self, x):
if isinstance(x, int):
self.x = x % MOD
elif isinstance(x, mint):
self.x = x.x
else:
self.x = int(x) % MOD
def __str__(self):
return str(self.x)
__repr__ = __str__
def __iadd__(self, other):
self.x += other.x if isinstance(other, mint) else other
self.x -= MOD if self.x >= MOD else 0
return self
def __isub__(self, other):
self.x += MOD-other.x if isinstance(other, mint) else MOD-other
self.x -= MOD if self.x >= MOD else 0
return self
def __imul__(self, other):
self.x *= other.x if isinstance(other, mint) else other
self.x %= MOD
return self
def __add__(self, other):
return (
mint(self.x + other.x) if isinstance(other, mint) else
mint(self.x + other)
)
def __sub__(self, other):
return (
mint(self.x - other.x) if isinstance(other, mint) else
mint(self.x - other)
)
def __mul__(self, other):
return (
mint(self.x * other.x) if isinstance(other, mint) else
mint(self.x * other)
)
def __floordiv__(self, other):
return (
mint(
self.x * pow(other.x, MOD - 2, MOD)
) if isinstance(other, mint) else
mint(self.x * pow(other, MOD - 2, MOD))
)
def __pow__(self, other):
return (
mint(pow(self.x, other.x, MOD)) if isinstance(other, mint) else
mint(pow(self.x, other, MOD))
)
__radd__ = __add__
def __rsub__(self, other):
return (
mint(other.x - self.x) if isinstance(other, mint) else
mint(other - self.x)
)
__rmul__ = __mul__
def __rfloordiv__(self, other):
return (
mint(
other.x * pow(self.x, MOD - 2, MOD)
) if isinstance(other, mint) else
mint(other * pow(self.x, MOD - 2, MOD))
)
def __rpow__(self, other):
return (
mint(pow(other.x, self.x, MOD)) if isinstance(other, mint) else
mint(pow(other, self.x, MOD))
)
n,s = map(int, input().split())
a = list(map(int, input().split()))
import copy
dp = [[0 for _ in range(s+1)] for _ in range(n+1)]
dp[0][0]=pow(2,n,MOD)
div=pow(2,MOD-2,MOD)
for i in range(n):
for j in range(s+1):
dp[i+1][j]=dp[i][j]
if j-a[i]>=0:
dp[i+1][j]+=dp[i][j-a[i]]*div
dp[i+1][j]%=MOD
print(dp[n][s])
|
N, S = map(int, input().split())
A = list(map(int, input().split()))
# dp[n][i][s] := i 番目まで見て n 個使って合計が s になる場合の数 これは無理
# dp[i][s] := i 番目まで見て合計 s
dp = [[0]*(S+1) for _ in range(N+1)]
dp[0][0] = 1
mod = 998244353
for i, a in enumerate(A, 1):
for s in range(S+1):
if s-a >= 0:
dp[i][s] = (dp[i-1][s] * 2 + dp[i-1][s-a]) % mod
else:
dp[i][s] = dp[i-1][s] * 2 % mod
print(dp[N][S])
| 1 | 17,787,094,711,958 | null | 138 | 138 |
r,c=map(int,input().split())
mat = [ list(map(int,input().split())) for _ in range(0,r) ]
for xs in mat: xs.append(sum(xs))
ll = [ sum([ xs[i] for xs in mat ]) for i in range (0,c+1) ]
for xs in mat: print(' '.join(map(str,xs)))
print(' '.join(map(str,ll)))
|
import sys
from collections import deque
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007
def main():
N, p, q, *AB = map(int, read().split())
p -= 1
q -= 1
G = [[] for _ in range(N)]
for a, b in zip(*[iter(AB)] * 2):
G[a - 1].append(b - 1)
G[b - 1].append(a - 1)
if len(G[p]) == 1 and G[p][0] == q:
print(0)
return
dist1 = [-1] * N
dist1[p] = 0
queue = deque([p])
while queue:
v = queue.popleft()
for nv in G[v]:
if dist1[nv] == -1:
dist1[nv] = dist1[v] + 1
queue.append(nv)
dist2 = [-1] * N
dist2[q] = 0
queue = deque([q])
while queue:
v = queue.popleft()
for nv in G[v]:
if dist2[nv] == -1:
dist2[nv] = dist2[v] + 1
queue.append(nv)
max_d = 0
for d1, d2 in zip(dist1, dist2):
if d1 < d2 and max_d < d2:
max_d = d2
print(max_d - 1)
return
if __name__ == '__main__':
main()
| 0 | null | 59,325,279,954,270 | 59 | 259 |
a, b = map(int, input().split())
print("a", a < b and "<" or a > b and ">" or "==", "b")
|
import itertools
N = int(input())
d = list(map(int, input().split()))
lst = list(itertools.combinations(d, 2))
total = 0
for n in lst:
c = n[0] * n[1]
total += c
print(total)
| 0 | null | 84,664,273,032,650 | 38 | 292 |
n = int(input())
a = [0,0,0]
mod = 1000000007
su=0
for i in range(3,n+1):
su=(su+a[i-3])%mod
a.append((su+1)%mod)
print(a[n])
|
s=[input() for _ in range(int(input()))]
def c(b):return str(s.count(b))
x=' x '
n='\n'
a='AC'
w='WA'
t='TLE'
r='RE'
print(a+x+c(a)+n+w+x+c(w)+n+t+x+c(t)+n+r+x+c(r))
| 0 | null | 5,974,045,784,818 | 79 | 109 |
n = int(input())
s = input()
ans = 0
for i in range(n - 2):
a = s[i:i + 3]
if a == "ABC":
ans += 1
print(ans)
|
A,B,C,D=map(int,input().split())
ans=0
while 1:
C-=B
if C<=0:
ans=1
break
A-=D
if A<=0:
break
if ans==1:
print("Yes")
else:
print("No")
| 0 | null | 64,266,157,576,000 | 245 | 164 |
import math
H = int(input())
W = int(input())
N = int(input())
ans = max([H,W])
fa = N/ans
print(math.ceil(fa))
|
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce
rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rf = lambda: map(float, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7
a = max(ri(), ri())
print(math.ceil(ri()/a))
| 1 | 88,490,849,672,906 | null | 236 | 236 |
import math
def solve():
N = int(input())
A = list(map(int, input().split()))
if(N == 0 and A[0] == 1):
print(1)
return
leaf_list = [0] * (N+1)
for d in range(N, -1 , -1):
if(d == N):
leaf_list[d] = (A[d],A[d])
else:
Min = math.ceil(leaf_list[d+1][0]/2) + A[d]
Max = leaf_list[d+1][1] + A[d]
leaf_list[d] = (Min,Max)
if(not(leaf_list[0][0] <= 1 and leaf_list[0][1] >= 1)):
print(-1)
return
node_list = [0] * (N+1)
node_list[0] = 1
for d in range(1, N+1):
node_list[d] = min((node_list[d-1] - A[d-1]) * 2, leaf_list[d][1])
print(sum(node_list))
solve()
|
n = int(input())
home = [[0 for i in range(20)] for j in range(15)] #横最大20→間の#,縦最大3階*4棟+3行の空行
for i in range(n) :
b, f, r, v = map(int, input().split())
h = (b - 1) * 4 + f - 1
w = r * 2 - 1
s = v
home[h][w] += s
for y in range(15) :
for x in range(20) :
if(y % 4 == 3) :
home[y][x] = "#"
elif(x % 2 == 0) :
home[y][x] = " "
print(home[y][x], end = "")
print()
| 0 | null | 10,054,641,931,320 | 141 | 55 |
def main():
S, T = input().split()
print(T + S)
main()
|
S, T = input().split(' ')
print(T + S)
| 1 | 103,365,153,585,052 | null | 248 | 248 |
try:
s = ''
while True:
s = s + input().lower()
except EOFError:
[print(chr(o)+' : '+str(s.count(chr(o)))) for o in range(ord('a'),ord('z')+1)]
|
# -*- coding: utf-8 -*-
w = raw_input().upper()
c = 0
while 1:
t = raw_input()
if t=="END_OF_TEXT": break
c += t.upper().split().count(w)
print c
| 0 | null | 1,760,081,322,428 | 63 | 65 |
a, b, c = map(int,raw_input().split())
count = 0
for x in xrange(a,b+1):
if c%x == 0:
count+=1
print count
|
inp = input()
a = inp.split()
a[0] = int(a[0])
a[1] = int(a[1])
a[2] = int(a[2])
c = 0
for i in range(a[0],a[1]+1):
if a[2] % i == 0:
c += 1
print(c)
| 1 | 560,939,505,568 | null | 44 | 44 |
input_list = list(map(str, input().split()))
stack_list = []
for i in input_list:
if i not in ['+', '-', '*']:
stack_list.append(i)
else:
a = stack_list.pop()
b = stack_list.pop()
stack_list.append(str(eval(b + i + a)))
print(stack_list[0])
|
stak = []
for i in input().split():
if i in "+-*":
a = stak.pop()
b = stak.pop()
stak.append(str(eval(b + i + a)))
else:
stak.append(i)
print(stak.pop())
| 1 | 36,510,324,928 | null | 18 | 18 |
n = int(input())
s = input()
ans = 0
#print(s.count('ABC'))
for ni in range(n):
if s[ni:ni+3] == 'ABC':
ans += 1
print(ans)
|
n = int(input())
X = input()
a = 0
for i in range(n-2):
if X[i] + X[i+1] + X[i+2] == 'ABC':
a +=1
else:
a +=0
print(a)
| 1 | 99,351,477,890,532 | null | 245 | 245 |
import sys
MOD = 998244353
def main():
input = sys.stdin.buffer.readline
n, s = map(int, input().split())
a = list(map(int, input().split()))
dp = [[None] * (s + 1) for _ in range(n + 1)]
# dp[i][j]:=集合{1..i}の空でない部分集合T全てについて,和がjとなる部分集合の個数の和
for i in range(n + 1):
for j in range(s + 1):
if i == 0 or j == 0:
dp[i][j] = 0
continue
if j > a[i - 1]:
dp[i][j] = dp[i - 1][j] * 2 + dp[i - 1][j - a[i - 1]]
elif j == a[i - 1]:
dp[i][j] = dp[i - 1][j] * 2 + pow(2, i - 1, MOD)
else:
dp[i][j] = dp[i - 1][j] * 2
dp[i][j] %= MOD
print(dp[n][s])
if __name__ == '__main__':
main()
|
class Node(object):
def __init__(self, num, prv = None, nxt = None):
self.num = num
self.prv = prv
self.nxt = nxt
class DoublyLinkedList(object):
def __init__(self):
self.start = self.last = None
def insert(self, num):
new_elem = Node(num)
if self.start is None:
self.start = self.last = new_elem
else:
new_elem.nxt = self.start
self.start.prv = new_elem
self.start = new_elem
def delete_num(self, target):
it = self.start
while it is not None:
if it.num == target:
if it.prv is None and it.nxt is None:
self.start = self.last = None
else:
if it.prv is not None:
it.prv.nxt = it.nxt
else:
self.start = self.start.nxt
if it.nxt is not None:
it.nxt.prv = it.prv
else:
self.last = self.last.prv
break
it = it.nxt
def delete_start(self):
if self.start is self.last:
self.start = self.last = None
else:
self.start.nxt.prv = None
self.start = self.start.nxt
def delete_last(self):
if self.start is self.last:
self.start = self.last = None
else:
self.last.prv.nxt = None
self.last = self.last.prv
def get_content(self):
ret = []
it = self.start
while it is not None:
ret.append(it.num)
it = it.nxt
return ' '.join(ret)
def _main():
from sys import stdin
n = int(input())
lst = DoublyLinkedList()
for _ in range(n):
cmd = stdin.readline().strip().split()
if cmd[0] == 'insert':
lst.insert(cmd[1])
elif cmd[0] == 'delete':
lst.delete_num(cmd[1])
elif cmd[0] == 'deleteFirst':
lst.delete_start()
elif cmd[0] == 'deleteLast':
lst.delete_last()
print(lst.get_content())
if __name__ == '__main__':
_main()
| 0 | null | 8,790,163,676,590 | 138 | 20 |
N, K, C = list(map(int, input().split()))
S = input()
l = [-1] * N
r = [-1] * N
idx = 1
i = 0
C += 1
while i < N:
if S[i] == 'o':
l[i] = idx
idx += 1
i += C
else:
i += 1
idx = 1
i = N-1
while 0 <= i:
if S[i] == 'o':
r[i] = idx
idx += 1
i -= C
else:
i -= 1
if idx-1 <= K:
for i in range(N):
if l[i] != -1 and r[i] != -1: print(i+1)
|
import copy
N = int(input())
A = input().split()
B = copy.copy(A)
boo = 1
while boo:
boo = 0
for i in range(N-1):
if A[i][1] > A[i+1][1]:
A[i], A[i+1] = A[i+1], A[i]
boo = 1
print(*A)
print("Stable")
for i in range(N):
mi = i
for j in range(i,N):
if B[mi][1] > B[j][1]:
mi = j
B[i], B[mi] = B[mi], B[i]
if A==B:
print(*B)
print("Stable")
else:
print(*B)
print("Not stable")
| 0 | null | 20,506,038,576,532 | 182 | 16 |
print('Yes' if '7' in input() else 'No')
|
out_grade = ""
while True:
m, f, r = map(int, input().split(" "))
if m==f==r==-1:
#EOF
break
if m==-1 or f==-1:
out_grade = "F"
elif m + f >= 80:
out_grade = "A"
elif m + f >= 65:
out_grade = "B"
elif m + f >= 50:
out_grade = "C"
elif m + f >= 30:
if r >= 50:
out_grade = "C"
else:
out_grade = "D"
else:
out_grade = "F"
print(out_grade)
| 0 | null | 17,766,024,069,652 | 172 | 57 |
res = {1:300000, 2:200000, 3:100000}
a, b = map(int, input().split())
if a == b == 1:
c = 400000
else:
c = 0
try:
a = res[a]
except:
a = 0
try:
b = res[b]
except:
b = 0
print(c+a+b)
|
#!/usr/bin/env python3
import sys
MOD = 1000000007 # type: int
class Factorial:
def __init__(self,MOD):
self.MOD = MOD
self.factorials = [1,1] # 階乗を求めるためのキャッシュ
self.invModulos = [0,1] # n^-1のキャッシュ
self.invFactorial_ = [1,1] # (n^-1)!のキャッシュ
def calc(self,n):
if n <= -1:
print("Invalid argument to calculate n!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.factorials):
return self.factorials[n]
nextArr = [0]*(n+1-len(self.factorials))
initialI = len(self.factorials)
prev = self.factorials[-1]
m = self.MOD
for i in range(initialI,n+1):
prev = nextArr[i-initialI] = prev * i%m
self.factorials += nextArr
return self.factorials[n]
def inv(self,n):
if n <= -1:
print("Invalid argument to calculate n^(-1)")
print("n must be non-negative value. But the argument was " + str(n))
exit()
p = self.MOD
pi = n%p
if pi < len(self.invModulos):
return self.invModulos[pi]
nextArr = [0]*(n+1-len(self.invModulos))
initialI = len(self.invModulos)
for i in range(initialI,min(p,n+1)):
next = -self.invModulos[p%i]*(p//i)%p
self.invModulos.append(next)
return self.invModulos[pi]
def invFactorial(self,n):
if n <= -1:
print("Invalid argument to calculate (n^(-1))!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.invFactorial_):
return self.invFactorial_[n]
self.inv(n) # To make sure already calculated n^-1
nextArr = [0]*(n+1-len(self.invFactorial_))
initialI = len(self.invFactorial_)
prev = self.invFactorial_[-1]
p = self.MOD
for i in range(initialI,n+1):
prev = nextArr[i-initialI] = (prev * self.invModulos[i%p])%p
self.invFactorial_ += nextArr
return self.invFactorial_[n]
class Combination:
def __init__(self,MOD):
self.MOD = MOD
self.factorial = Factorial(MOD)
def choose_k_from_n(self,n,k):
if k < 0 or n < k:
return 0
k = min(k,n-k)
f = self.factorial
return f.calc(n)*f.invFactorial(max(n-k,k))*f.invFactorial(min(k,n-k))%self.MOD
def solve(n: int, k: int):
c = Combination(MOD)
sum = 0
for i in range(min(n,k+1)-1,-1,-1):
c1 = c.choose_k_from_n(n,i)
c2 = c.choose_k_from_n(n-1,i)
sum = (sum + c1*c2)%MOD
print(sum)
return
# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template)
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
n = int(next(tokens)) # type: int
k = int(next(tokens)) # type: int
solve(n, k)
if __name__ == '__main__':
main()
| 0 | null | 104,147,596,970,912 | 275 | 215 |
N = int(input())
A = []
for _ in range(N):
a = int(input())
b = []
for _ in range(a):
b.append(list(map(int, input().split())))
A.append(b)
# 証言リスト A[i人目][j個目の証言] -> [誰が, bit(1は正、0は誤)]
# bitが1であれば正しい証言、0であれば間違った証言とする
# 正しい証言だけ確認して、[i, 1]と証言した i も1かどうか、[j,0]と証言したjが0かどうか
def F(i):
cnt = 0
B = [-1]*N #B = [-1,-1,-1]
r = 0
for j in range(N): # i=1, j=0,1,2
if (i>>j)&1: # 001 -> 1,0,0 j=0
r += 1 # r = 1
if B[j] == 0: #B[0] == -1
return 0
B[j]=1 # B = [1,-1,-1]
for p,q in A[j]: # A[0] = [[2, 1], [3, 0]]
if q == 0:
if B[p-1]==1: # B[2] == -1
return 0
B[p-1] = 0 # B = [1,1,0]
else:
if B[p-1]==0: # B[1] == -1
return 0
B[p-1] = 1 # B = [1,1,-1]
else:
cnt += 1 # cnt = 1
else: # j=1
if B[j]==1: #B[1] ==1
return 0
B[j]=0
if cnt == r:
return cnt
ans = 0
for i in range(1<<N):
ans = max(ans,F(i))
print(ans)
|
import sys
from io import StringIO
import unittest
import itertools
def yn(b):
print("Yes" if b==1 else "No")
return
def resolve():
readline=sys.stdin.readline
n=int(readline())
dat = [[-1 for i in range(n)] for j in range(n)]
for i in range(n):
m=int(readline())
for j in range(m):
x,y=map(int, readline().rstrip().split())
x-=1
dat[i][x] = y
ans=0
for ptn in itertools.product([False,True], repeat=n):
ok=True
for i in range(n):
if ptn[i]==False:
continue
for j in range(n):
if dat[i][j] == -1:
continue
if dat[i][j] == 1 and ptn[j]==False:
ok=False
break
if dat[i][j] == 0 and ptn[j]==True:
ok=False
break
if ok==False:
break
if ok==True:
ans=max(ans,ptn.count(True))
print(ans)
return
if 'doTest' not in globals():
resolve()
sys.exit()
| 1 | 121,554,705,161,828 | null | 262 | 262 |
s=input()
if len(s)%2==0 and s == 'hi'*(len(s)//2):
print('Yes')
else:
print('No')
|
dice = list(map(int, input().split()))
directions = input()
for direction in directions:
if direction == 'N':
dice = [dice[1], dice[5], dice[2], dice[3], dice[0], dice[4]]
elif direction == 'S':
dice = [dice[4], dice[0], dice[2], dice[3], dice[5], dice[1]]
elif direction == 'W':
dice = [dice[2], dice[1], dice[5], dice[0], dice[4], dice[3]]
else:
dice = [dice[3], dice[1], dice[0], dice[5], dice[4], dice[2]]
print(dice[0])
| 0 | null | 26,907,668,653,492 | 199 | 33 |
H, N = map(int, input().split())
List = [list(map(int, input().split())) for _ in range(N)]
inf = 10 ** 9
dp = [0] + [inf] * H
for h in range(1, H + 1):
for n in range(1, N + 1):
a, b = List[n - 1]
ref = h - a
if ref < 0: ref = 0
dp[h] = min(dp[h], dp[ref] + b)
print(dp[H])
|
import sys
input = sys.stdin.readline
from collections import *
S = input()[:-1]
K = int(input())
if S==S[0]*len(S):
print(len(S)*K//2)
exit()
l = []
cnt = 1
for i in range(len(S)-1):
if S[i]!=S[i+1]:
l.append((S[i], cnt))
cnt = 1
else:
cnt += 1
l.append((S[-1], cnt))
if K==1 or l[0][0]!=l[-1][0]:
ans = 0
for _, c in l:
ans += c//2*K
else:
ans = (l[0][1]+l[-1][1])//2*(K-1)
ans += l[0][1]//2
ans += l[-1][1]//2
for _, c in l[1:-1]:
ans += c//2*K
print(ans)
| 0 | null | 127,908,557,670,642 | 229 | 296 |
import copy
from collections import deque
n, m, q = list(map(int, input().split()))
abcd = []
for _ in range(q):
abcd.append(list(map(int, input().split())))
ans = 0
def dfs(A, low, high, n):
global ans
if len(A) >= n:
result = 0
for a,b,c,d in abcd:
if A[b-1]-A[a-1] == c:
result += d
ans = max(ans, result)
return
for i in range(low, high+1):
A.append(i)
dfs(A, i, high, n)
A.pop()
dfs([], 1, m, n)
print(ans)
|
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)
| 1 | 27,379,307,768,868 | null | 160 | 160 |
N= int(input())
n = N % 10
if n == 2 or n==4 or n ==5 or n == 7 or n == 9:
print('hon')
elif n == 0 or n ==1 or n == 6 or n == 8:
print('pon')
elif n == 3:
print('bon')
|
def main_1_11_A():
values = list(map(int, input().split()))
query = input()
dice = Dice(values)
for d in query:
dice.roll(d)
def main_1_11_B():
values = list(map(int, input().split()))
n = int(input())
for _ in range(n):
t_value, s_value = map(int, input().split())
print(_B(values, t_value, s_value))
def _B(values, t_value, s_value):
"""
????????¢?????????????????¨??????
??´??¢???????????¢?????????????????§???????????????????????¨?????????
??????????????????????????¢???????¢??????????
"""
def rot_n(dice, n):
for _ in range(n):
dice.roll("N").roll("E").roll("S")
return dice
def make_dices():
# ?????¢???TOP?????????
return [
Dice(values),
Dice(values).roll("N"),
Dice(values).roll("N").roll("N"),
Dice(values).roll("N").roll("N").roll("N"),
Dice(values).roll("E"),
Dice(values).roll("W"),
]
dices = (
make_dices() +
list(map(lambda dice: rot_n(dice, 1), make_dices())) +
list(map(lambda dice: rot_n(dice, 2), make_dices())) +
list(map(lambda dice: rot_n(dice, 3), make_dices()))
)
for dice in dices:
# print(dice)
for d in ("E" * 4 + "W" * 4 + "S" * 4 + "N" * 4):
dice.roll(d)
#print(d, (dice.top_value, dice.south_value), (t_value, s_value))
if (dice.top_value, dice.south_value) == (t_value, s_value):
return dice.east_value
class Dice:
def __init__(self, values):
self.values = values
# top, bottom, direction 4
self.t = 1
self.b = 6
self.w = 4
self.e = 3
self.n = 5
self.s = 2
def __repr__(self):
labels = {
"t": self.t,
"b": self.b,
"w": self.w,
"e": self.e,
"n": self.n,
"s": self.s,
}
return "<%s (%s, %s)>" % (self.__class__, self.values, labels)
def roll(self, direction):
if direction == "E":
after_lables = self.t, self.b, self.e, self.w
self.e, self.w, self.b, self.t = after_lables
elif direction == "W":
after_lables = self.t, self.b, self.e, self.w
self.w, self.e, self.t, self.b = after_lables
elif direction == "S":
after_lables = self.t, self.b, self.n, self.s
self.s, self.n, self.t, self.b = after_lables
elif direction == "N":
after_lables = self.t, self.b, self.n, self.s
self.n, self.s, self.b, self.t = after_lables
return self
@property
def top_value(self):
return self.values[self.t - 1]
@property
def south_value(self):
return self.values[self.s - 1]
@property
def east_value(self):
return self.values[self.e - 1]
if __name__ == "__main__":
# main_1_11_A()
main_1_11_B()
| 0 | null | 9,804,177,490,062 | 142 | 34 |
# ABC149
# B Greesy Takahashi
# takはA枚、aokiはB枚、TAKはK回
a, b, k = map(int, input().split())
if k > a:
if k - a > b:
print(0,0)
else:
print(0,b - (k - a))
else:
print(a-k,b)
|
a=input()
b=a.split()
c=list()
for d in b:
c.append(int(d))
d=sorted(c)
print(d[0],d[1],d[2])
| 0 | null | 52,613,391,615,708 | 249 | 40 |
import numpy as np
last=[0]*26
D=int(input())
c=list(map(int, (input().split(' '))))
s=[]
a=[0]*D
sat=0
for i in range(D):
s.append(list(map(int, (input().split(' ')))))
for i in range(D):
a[i]=int(input())
last[a[i]-1] = i+1
sat += s[i][a[i]-1] - np.dot(c, list(map(lambda x: i+1-x, last)))
print(sat)
|
D = int(input())
C = list(map(int,input().split())) #C[i]AiCを実施しないと下がる満足度
S = [] #S[i][j] i日目にAjcというコンテストを実施した場合に上がる満足度
for i in range(D): #コンテストは0index
temp = list(map(int,input().split()))
S.append(temp)
T = []
for i in range(D):
temp = int(input())
T.append(temp)
since = [0 for _ in range(26)] #最後に開催されてから何日経ったか。
manzoku = 0
for i in range(26):
since[i] += C[i]
for i in range(D):
contest = T[i]-1 #0index
manzoku += S[i][contest]
for j in range(26):
if j != contest:
manzoku -= since[j]
print(manzoku)
for j in range(26):
if j != contest:
since[j] += C[j]
else:
since[j] = C[j]
| 1 | 9,940,033,862,778 | null | 114 | 114 |
from collections import deque
n = int(input())
A = [0]*(n+1)
D = [-1]*(n+1)
DP = [0]*(n+1)
d = deque()
for i in range(n):
A[i+1] = [int(i) for i in input().split()]
d.append(1)
D[1] = 0
DP[1] = 1
while(len(d)>0):
tmp = d.pop()
for i in A[tmp][2:]:
if not DP[i]:
D[i] = D[tmp]+1
DP[i] = 1
d.appendleft(i)
for i in range(1,n+1):
tmp = "{} {}".format(i,D[i])
print(tmp)
|
import sys
sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))
ss, st = ins().split()
a, b = inm()
su = ins()
def solve():
if su == ss:
return (a - 1), b
else:
return a, (b - 1)
print(*solve())
| 0 | null | 36,201,101,144,130 | 9 | 220 |
n = int(input())
Tp = 0
Hp = 0
tmp = "abcdefghijklmnopqrstuvwxyz"
alph = list(tmp)
for i in range(n):
Ta,Ha = input().split()
lTa = list(Ta)
lHa = list(Ha)
num = 0
if (len(Ta) <= len(Ha)): #definition of num
num = len(Ta)
else:
num = len(Ha)
if (Ta in Ha and lTa[0] == lHa[0]): #drow a
if (Ta == Ha):
Tp += 1
Hp += 1
else:
Hp += 3
elif (Ha in Ta and Ha != Ta and lTa[0] == lHa[0]):
Tp += 3
else:
for i in range(len(lTa)): # convert alphabet to number
for j in range(26):
if (lTa[i] == alph[j]):
lTa[i] = int(j)
else :
continue
for i in range(len(lHa)): # convert alphabet to number
for j in range(26):
if (lHa[i] == alph[j]):
lHa[i] = int(j)
else :
continue
for i in range(num):
if (lTa[i] < lHa[i]):
Hp +=3
break
elif (lHa[i] < lTa[i]):
Tp +=3
break
elif (lHa[i] == lTa[i]):
continue
print(Tp,Hp)
|
def main(listseq):
x = y = 0
for strings in listseq:
a, b = strings.split(" ")
if a < b: y += 3
elif a > b: x += 3
elif a == b: x += 1; y += 1
return x, y
times = int(input())
words = [input() for _ in range(times)]
a, b = main(words)
print(f"{a} {b}")
| 1 | 2,019,357,832,800 | null | 67 | 67 |
N = int(input())
A = list(map(int, input().split()))
mod = 10**9+7
G = [0]*3
ans = 1
for i in range(N):
x = 0
cnt = 0
for j, g in enumerate(G):
if g == A[i]:
x = j if cnt == 0 else x
cnt += 1
G[x] += 1
ans *= cnt
ans = ans % mod
print(ans)
|
import sys
input = sys.stdin.readline
n,m = map(int,input().split())
a = [int(i) for i in input().split()]
a = sorted(list(set(a)))
from fractions import gcd
lc = a[0]
if n != 1:
for num,i in enumerate(a[1:]):
if lc == 0:
break
lc = lc * i // gcd(lc, i)
if gcd(lc, i) == min(i,lc):
for j in a[:num+1]:
if (i/j)%2 == 0:
lc = 0
break
if lc == 0:
print(0)
else:
print((m+lc//2)//lc)
| 0 | null | 115,733,935,234,970 | 268 | 247 |
M=10**9+7;n,k=map(int,input().split());a=c=1
for i in range(1,min(n,k+1)):m=n-i;c=c*-~m*m*pow(i,M-2,M)**2%M;a+=c
print(a%M)
|
D = int(input())
c = list(map(int, input().split()))
S = [list(map(int, input().split())) for i in range(D)]
T = [int(input()) for i in range(D)]
SUM = 0
last = [0] * 28
for d in range(1, D + 1):
i = T[d - 1]
SUM += S[d - 1][i - 1]
SU = 0
last[i] = d
for j in range(1, 27):
SU += (c[j - 1] * (d - last[j]))
SUM -= SU
print(SUM)
| 0 | null | 38,759,386,177,600 | 215 | 114 |
N = int(input())
edges = [[] for _ in range(N)]
for i in range(N - 1):
fr, to = map(lambda a: int(a) - 1, input().split())
edges[fr].append((i, to))
edges[to].append((i, fr))
ans = [-1] * (N - 1)
A = [set() for _ in range(N)]
st = [0]
while st:
now = st.pop()
s = 1
for i, to in edges[now]:
if ans[i] != -1:
continue
while s in A[now]:
s += 1
ans[i] = s
A[to].add(s)
A[now].add(s)
st.append(to)
print(max(ans))
print(*ans, sep="\n")
|
a=[0]*45
def Fib(n):
if n == 0:
a[0]=1
return 1
elif n == 1:
a[1]=1
return 1
else:
if a[n]==0:
a[n]=Fib(n-1)+Fib(n-2)
return a[n]
else:
return a[n]
n=int(input())
print(Fib(n))
| 0 | null | 68,238,582,159,458 | 272 | 7 |
n = int(input())
s = [input() for i in range(n)]
ac = s.count('AC')
wa = s.count('WA')
tle = s.count('TLE')
re = s.count('RE')
print('AC x ', ac)
print('WA x ', wa)
print('TLE x ', tle)
print('RE x ', re)
|
num=list(map(int,input().split()))
q=int(input())
def E(ls):
ls=[ls[3],ls[1],ls[0],ls[5],ls[4],ls[2]]
return ls
def N(ls):
ls=[ls[1],ls[5],ls[2],ls[3],ls[0],ls[4]]
return ls
def R(ls):
ls=[ls[0],ls[2],ls[4],ls[1],ls[3],ls[5]]
return ls
for i in range(q):
num0,num1=list(map(int,input().split()))
if num.index(num0) in [0,2,3,5]:
while num[0]!=num0:
num=E(num)
while num[1]!=num1:
num=R(num)
print(num[2])
else:
while num[0]!=num0:
num=N(num)
while num[1]!=num1:
num=R(num)
print(num[2])
| 0 | null | 4,494,580,354,430 | 109 | 34 |
from queue import Queue
def isSafe(row, col):
return row >= 0 and col >= 0 and row<h and col<w and mat[row][col] != '#'
def bfs(row, col):
visited = [[False]*w for _ in range(h)]
que = Queue()
dst = 0
que.put([row, col, 0])
visited[row][col] = True
moves = [[-1, 0],[1, 0], [0, -1], [0, 1]]
while not que.empty():
root = que.get()
row, col, dst = root
for nrow, ncol in moves:
row2 = row + nrow
col2 = col + ncol
if isSafe(row2, col2) is True and visited[row2][col2] is False:
visited[row2][col2] = True
que.put([row2, col2, dst+1])
return dst
h, w = map(int, input().split())
mat = [input() for _ in range(h)]
ans = 0
for row in range(h):
for col in range(w):
if mat[row][col] == '.':
ans = max(ans, bfs(row, col))
print(ans)
|
n = int(input())
A = list(map(int, input().split()))
total = 0
min_ = A[0]
for i in range(n):
if A[i] > min_:
min_ = A[i]
else:
total += (min_ - A[i])
print(total)
| 0 | null | 49,489,085,869,060 | 241 | 88 |
x=int(input())
def 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
max_lower_div=max(divisors(x))
print((max_lower_div-1)+(x//max_lower_div-1))
|
#!/usr/bin/env python3
import decimal
#import
#import math
#import numpy as np
#= int(input())
#= input()
A, B = map(decimal.Decimal, input().split())
print(int(A * B))
| 0 | null | 89,017,684,483,920 | 288 | 135 |
import numpy as np
N = int(input())
A = []
B = []
for i in range(N):
a,b=map(int, input().split())
A.append(a)
B.append(b)
Aarray = np.array(A)
Barray = np.array(B)
medA = np.median(Aarray)
medB = np.median(Barray)
if N%2 == 1:
ans = medB-medA+1
else:
ans = 2*(medB-medA)+1
print(str(int(ans)))
|
n , k , s = map(int,input().split())
l = []
for i in range(k):
l.append(s)
if s == 1:
t = 3
elif s == 10**9:
t = 1
else:
t = s + 1
for i in range(n-k):
l.append(t)
print(' '.join(map(str, l)))
| 0 | null | 54,232,592,558,820 | 137 | 238 |
X = int(input())
ans = 8 - (X - 400)//200
print(ans)
|
# coding=utf-8
from collections import deque
n, q = map(int, input().split())
queue = deque()
total_time = 0
for i in range(n):
name, time = input().split()
queue.append([name, int(time)])
while queue:
poped = queue.popleft()
if poped[1] > q:
queue.append([poped[0], poped[1] - q])
total_time += q
else:
total_time += poped[1]
print(poped[0], total_time)
| 0 | null | 3,332,915,150,630 | 100 | 19 |
def main():
n = int(input())
d = [int(x) for x in input().split()]
ans = 0
for i in range(n):
for j in range(n):
if i < j:
ans += d[i]*d[j]
print(ans)
if __name__ == '__main__':
main()
|
# ABC143
# B Tkoyaki Festival 2019
n = int(input())
D = list(map(int, input().split()))
ct = 0
for i in range(n):
for j in range(n):
if i != j:
if i < j:
ct += D[i] * D[j]
print(ct)
| 1 | 168,452,987,674,932 | null | 292 | 292 |
N = int(input())
s, t = map(str, input().split())
ans = []
for i in range(N):
a = s[i] + t[i]
ans.append(a)
print(''.join(ans))
|
n = int(input())
s, t = input().split()
a = ''
for i, j in zip(s, t):
a = a + i + j
print(a)
| 1 | 112,368,432,700,468 | null | 255 | 255 |
import math
def LI():
return list(map(int, input().split()))
L, R, d = LI()
Ld = (L-1)//d
Rd = R//d
ans = Rd-Ld
print(ans)
|
main=list(map(int,input().split()));count=0
for i in range(main[0],main[1]+1):
if(i%main[2]==0): count=count+1
print(count)
| 1 | 7,575,391,768,032 | null | 104 | 104 |
S = input().strip()
print(S[:3])
|
def main():
A, B, C, K = map(int, input().split())
if K <= A + B:
print(min([A, K]))
else:
print(A - (K - A - B))
if __name__ == '__main__':
main()
| 0 | null | 18,205,945,489,600 | 130 | 148 |
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
# from collections import deque, defaultdict
# from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np # cumsum
# from bisect import bisect_left, bisect_right
def solve():
N, K = MI()
S = []
mod = 998244353
for i in range(K):
l, r = MI()
S.append((l, r))
dp = [0] * (N+1)
dp[1] = 1
C = [0] * (N+1)
C[1] = 1
for i in range(2, N+1):
for s in S:
l = max(1, i - s[1])
r = i - s[0]
if r < 1:
continue
dp[i] += (C[r] - C[l-1]) % mod
C[i] += (dp[i] + C[i-1]) % mod
print(dp[N] % mod)
if __name__ == '__main__':
solve()
|
mod = 998244353
# 貰うDP
def main(N, S):
dp = [0 if n != 0 else 1 for n in range(N)] # dp[i]はマスiに行く通り数. (答えはdp[-1]), dp[0] = 1 (最初にいる場所だから1通り)
A = [0 if n != 0 else 1 for n in range(N)] # dp[i] = A[i] - A[i-1] (i >= 1), A[0] = dp[0] = 1 (i = 0) が成り立つような配列を考える.
for i in range(1, N): # 今いる点 (注目点)
for l, r in S: # 選択行動範囲 (l: 始点, r: 終点)
if i - l < 0: # 注目点が選択行動範囲の始点より手前の場合 → 注目点に来るために使用することはできない.
break
else: # 注目点に来るために使用することができる場合
dp[i] += A[i-l] - A[max(i-r, 0)-1] # lからrの間で,注目点に行くために使用できる点を逆算. そこに行くことができる = 選択行動範囲の値を選択することで注目点に達することができる通り数.
dp[i] %= mod
A[i] = (A[i-1] + dp[i]) % mod
print(dp[-1])
if __name__ == '__main__':
N, K = list(map(int, input().split()))
S = {tuple(map(int, input().split())) for k in range(K)}
S = sorted(list(S), key = lambda x:x[0]) # 始点でsort (範囲の重複がないため)
main(N, S)
| 1 | 2,721,628,855,606 | null | 74 | 74 |
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')
#solve
def solve():
t1, t2 = LI()
a1, a2 = LI()
b1, b2 = LI()
if t1 * a1 + t2 * a2 == t1 * b1 + t2 * b2:
print("infinity")
return
if t1 * a1 > t1 * b1:
if t1 * a1 + t2 * a2 > t1 * b1 + t2 * b2:
print(0)
return
else:
x = t1 * (a1 - b1) / (-t1 * (a1 - b1) + t2 * (b2 - a2))
if x.is_integer():
print(2 * (t1 * (a1 - b1) // (-t1 * (a1 - b1) + t2 * (b2 - a2))))
else:
print(2 * (t1 * (a1 - b1) // (-t1 * (a1 - b1) + t2 * (b2 - a2))) + 1)
return
elif t1 * a1 < t1 * b1:
if t1 * a1 + t2 * a2 < t1 * b1 + t2 * b2:
print(0)
return
else:
x = t1 * (b1 - a1) / (-t1 * (b1 - a1) + t2 * (a2 - b2))
if x.is_integer():
print(2 * (t1 * (b1 - a1) // (-t1 * (b1 - a1) + t2 * (a2 - b2))))
else:
print(2 * (t1 * (b1 - a1) // (-t1 * (b1 - a1) + t2 * (a2 - b2))) + 1)
return
return
#main
if __name__ == '__main__':
solve()
|
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
P = (A1 - B1) * T1
Q = (A2 - B2) * T2
if P > 0:
P *= -1
Q *= -1
if P + Q < 0:
print(0)
elif P + Q == 0:
print("infinity")
else:
S = (-P) // (P + Q)
T = (-P) % (P + Q)
if T != 0:
print(S * 2 + 1)
else:
print(S * 2)
| 1 | 131,595,067,657,758 | null | 269 | 269 |
x,y=map(int,input().split())
X=max(x,y)
Y=min(x,y)
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
mod = 10**9+7 #出力の制限
N = 10**6+1
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 )
if (X+Y)%3!=0:
print(0)
else:
if X>(Y*2):
print(0)
else:
N=(X+Y)//3
H=(N-(X-Y))//2
print(cmb(N,H,10**9+7))
#print(N,H)
|
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, log
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *
#階乗#
lim = 10**6 #必要そうな階乗の限界を入力
fact = [1] * (lim+1)
for n in range(1, lim+1):
fact[n] = n * fact[n-1] % mod
#階乗の逆元#
fact_inv = [1]*(lim+1)
fact_inv[lim] = pow(fact[lim], mod-2, mod)
for n in range(lim, 0, -1):
fact_inv[n-1] = n*fact_inv[n]%mod
def C(n, r):
return (fact[n]*fact_inv[r]%mod)*fact_inv[n-r]%mod
X, Y = MAP()
a = (2*X - Y)/3
b = (2*Y - X)/3
if a%1 == b%1 == 0 and 0 <= a and 0 <= b:
print(C(int(a+b), int(a)))
else:
print(0)
| 1 | 149,934,624,275,072 | null | 281 | 281 |
import sys, itertools
print(len(set(itertools.islice(sys.stdin.buffer, 1, None))))
|
n = int(input())
num = [input() for _ in range(n)]
print(len(set(num)))
| 1 | 30,487,574,654,460 | null | 165 | 165 |
N = int(input())
ans = ''
while(N > 0):
N -= 1
_i = N % 26
ans = chr(ord('a') + _i) + ans
N //= 26
print(ans)
|
def main():
n = int(input())
A = sorted(map(int, input().split()), reverse=True)
ans = 0
mod = 10 ** 9 + 7
m=A[0]
for i in range(61):
cnt0 = cnt1 = 0
if m < 2 ** i:
break
for j, a in enumerate(A):
if a < 2 ** i:
cnt0 += n - j
break
if (a >> i) & 1:
cnt1 += 1
else:
cnt0 += 1
ans += (((2 ** i) % mod) * ((cnt1 * cnt0) % mod)) % mod
ans %= mod
print(ans)
if __name__ == "__main__":
main()
| 0 | null | 67,493,474,149,740 | 121 | 263 |
import math
x = int(input())
now = 100
ans = 0
while(now < x):
ans += 1
now += now//100
print(ans)
|
def main():
# 100*1.01^4000 = 1.9297237e+19
X = int(input())
val = 100
for i in range(4000):
val = (val*101)//100
if val >= X:
print(i+1)
return
main()
| 1 | 27,155,332,924,040 | null | 159 | 159 |
num = int(input())
hh = num // 3600
h2 = num % 3600
mm = h2 // 60
ss = h2 % 60
print(str(hh)+":"+str(mm)+":"+str(ss))
|
import math
from functools import reduce
from copy import deepcopy
n,m = map(int,input().split())
A = list(map(int,input().split()))
def lcm_base(x, y):
return (x * y) // math.gcd(x, y)
def lcm_list(numbers):
return reduce(lcm_base, numbers, 1)
Acopy = deepcopy(A)
flag = -1
for i in range(n):
cnt = 0
while True:
if Acopy[i] % 2 == 1:
break
else:
Acopy[i] //= 2
cnt += 1
if i == 0:
flag = cnt
else:
if flag != cnt:
print(0)
break
else:
A = [a//2 for a in A]
lcm = lcm_list(A)
print((lcm+m)//2//lcm)
| 0 | null | 51,153,829,492,612 | 37 | 247 |
HP, n = map(int, input().split())
AB = [list(map(int,input().split())) for _ in range(n)]
# DP
## DP[i]は、モンスターにダメージ量iを与えるのに必要な魔力の最小コスト
## DPの初期化
DP = [float('inf')] * (HP+1); DP[0] = 0
## HP分だけDPを回す.
for now_hp in range(HP):
## 全ての魔法について以下を試す.
for damage, cost in AB:
### 与えるダメージは、現在のダメージ量+魔法のダメージか体力の小さい方
next_hp = min(now_hp+damage, HP)
### 今の魔法で与えるダメージ量に必要な最小コストは、->
####->現在わかっている値か今のHPまでの最小コスト+今の魔法コストの小さい方
DP[next_hp] = min(DP[next_hp], DP[now_hp]+cost)
print(DP[-1])
|
n, m = map(int, input().split())
c = list(map(int, input().split()))
dp = [float("inf")] * (n+1)
dp[0] = 0
for i in range(1, n + 1):
for j in range(m):
if i >= c[j]:
dp[i] = min(dp[i], dp[i-c[j]]+1)
print(dp[n])
| 0 | null | 40,799,742,992,552 | 229 | 28 |
def solve():
string = list(input())
ans = 0
if string[-1]=="s":
ans = "".join(string)+"es"
print(ans)
return
else:
print("".join(string)+"s")
return
solve()
|
a=input()
e=a[len(a)-1:len(a)]
if e == "s":
print(a+"es")
else:
print(a+"s")
| 1 | 2,415,900,868,800 | null | 71 | 71 |
n = int(input())
ab = [list(map(int, input().split())) for _ in range(n)]
a = [_ab[0] for _ab in ab]
b = [_ab[1] for _ab in ab]
a.sort()
b.sort()
if n % 2 == 1:
m = a[n // 2]
M = b[n // 2]
print(M - m + 1)
else:
m = (a[n // 2 - 1] + a[n // 2]) / 2
M = (b[n // 2 - 1] + b[n // 2]) / 2
print(int((M - m) * 2 + 1))
|
N = int(input())
A = [0]*N
B = [0]*N
for i in range(N):
A[i],B[i] = map(int, input().split())
A.sort()
B.sort()
if len(A)%2 != 0:
ans = B[N//2]-A[N//2]+1
else:
ans = (B[N//2]+B[N//2-1]) - (A[N//2]+A[N//2-1]) + 1
print(ans)
| 1 | 17,399,743,783,268 | null | 137 | 137 |
import sys, math
from functools import lru_cache
from collections import deque
sys.setrecursionlimit(500000)
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():
S = input()
for i in range(1, 6):
if S == 'hi'*i:
print('Yes')
return
print('No')
if __name__ == '__main__':
main()
|
S = input()
a = len(S)
if a%2==1:
print("No")
exit()
for i in range(0,a,2):
if S[i:i+2]!="hi":
print("No")
break
else:
print("Yes")
break
| 1 | 53,068,894,973,270 | null | 199 | 199 |
a,b= input().split()
print(str(min(int(a),int(b)))*int(str(max(int(a),int(b)))))
|
A,B,C = map(int,input().split())
L = [i for i in range(A,B + 1)]
_ = 0
for l in L:
if l % C == 0:
_ += 1
print(_)
| 0 | null | 45,710,556,945,650 | 232 | 104 |
def execute(X, K, D):
for i in range(K):
if X > 0:
X -= D
else:
X += D
return abs(X)
def execute2(X, K, D):
T = int(abs(X) / D)
if X > 0:
if K < T:
return X - K * D
else:
Y = X - T * D
if (K - T) % 2 == 0:
return Y
else:
return D - Y
else:
if K < T:
return (X + K * D) * -1
else:
Y = X + T * D
if (K - T) % 2 == 0:
return -1 * Y
else:
return D + Y
if __name__ == '__main__':
X, K, D = map(int, input().split())
print(execute2(X, K, D))
|
N = int(input())
cnt = 0
for a in range(1,N):
for b in range(1,N):
if a * b >= N:
break
else:
cnt +=1
print(cnt)
| 0 | null | 3,922,927,017,968 | 92 | 73 |
# coding: utf-8
import sys
from fractions import gcd
import numpy as np
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
MOD = 10 ** 9 + 7
N = ir()
A = lr()
lcm = 1
for a in A:
lcm *= a // gcd(lcm, a)
A = np.array(A)
answer = (lcm // A).sum() % MOD
print(answer % MOD)
# np.int64とint型の違いに注意
|
n=int(input())
a=list(map(int,input().split()))
mod=10**9+7
def eucrid(a,b):
a,b=max(a,b),min(a,b)
while True:
if a%b==0:
return b
else:
a,b=b,a%b
m=a[0]
for i in range(n):
m=m//eucrid(m,a[i])*a[i]
b=0
m=m%mod
for i in a:
b+=m*pow(i,mod-2,mod)%mod
print(b%mod)
| 1 | 87,206,136,369,340 | null | 235 | 235 |
N = int(input())
S =[str(input()) for i in range(N)]
print(len(set(S)))
|
n = int(input())
s = list(input() for _ in range(n))
keihin = set()
for i in s:
keihin.add(i)
print(len(keihin))
| 1 | 30,351,294,332,828 | null | 165 | 165 |
a = int(input())
b = int(input())
if (a == 1 and b == 2) or (a == 2 and b == 1):
print(3)
elif (a == 1 and b == 3) or (a == 3 and b == 1):
print(2)
else:
print(1)
|
A = int(input())
B = int(input())
ans = 6-A-B
print(ans)
| 1 | 110,721,876,736,550 | null | 254 | 254 |
# -*- coding: utf-8 -*-
import sys
from bisect import bisect_left, bisect_right
N,D,A=map(int, sys.stdin.readline().split())
XH=[ map(int, sys.stdin.readline().split()) for _ in range(N) ]
XH.sort()
bit=[ 0 for _ in range(N+1) ]
#BIT 関数は全て1-indexed
def add(a,w):
while a<=N:
bit[a]+=w
a+=a&-a
def sum(a):
ret=0
while 0<a:
ret+=bit[a]
a-=a&-a
return ret
#区間[l,r]に一律wを加える
def range_add(l,r,w):
add(l,w)
add(r+1,w*-1)
#a番目のモンスターの体力を取得
def get_value(a):
return sum(a)
X=[float("-inf")]
H=[float("-inf")]
for x,h in XH:
X.append(x)
H.append(h)
for i in range(1,N+1):
if i==1:
add(i,H[i])
else:
add(i,H[i]-H[i-1]) #区間に対する更新を行うため、値の差分をBITに持つ
ans=0
for i in range(1,N+1):
h=get_value(i)
if h<=0: #モンスターの体力がゼロ以下だったら何もしない
pass
else:
x=X[i]
pos=bisect_right(X,x+2*D)-1 #攻撃範囲の一番右側を二分探索で決定
if h%A==0: #hがAで割り切れる場合
cnt=h/A
range_add(i,pos,A*cnt*-1)
else:
cnt=h/A+1 #hをAで割って余りが出る場合は攻撃回数を+1する
range_add(i,pos,A*cnt*-1)
ans+=cnt
print ans
|
from collections import deque
n, d, a = map(int, input().split())
mons = []
for i in range(n):
x, h = map(int, input().split())
mons.append((x, h))
mons = sorted(mons)
q = deque()
dm_sum = 0
ans = 0
for i in range(n):
while dm_sum > 0:
if q[0][0] < mons[i][0]:
cur = q.popleft()
dm_sum -= cur[1]
else:
break
if mons[i][1] <= dm_sum:
continue
rem = mons[i][1] - dm_sum
at_num = rem // a
if rem % a != 0:
at_num += 1
ans += at_num
q.append((mons[i][0] + 2 * d, at_num*a))
dm_sum += at_num*a
print(ans)
| 1 | 81,973,818,431,260 | null | 230 | 230 |
n = int(input())
L = list(map(int, input().split()))
ans = 0
for i in range(n):
for j in range(i+1, n):
for k in range(j+1, n):
if L[i] == L[j] or L[j] == L[k] or L[k] == L[i]:
pass
elif L[i] + L[j] > L[k] and L[j] + L[k] > L[i] and L[k] + L[i] > L[j]:
ans += 1
print(ans)
|
import sys
input = sys.stdin.readline
P = 2019
def main():
S = input().rstrip()
N = len(S)
count = [0] * P
count[0] += 1
T = 0
for i in range(N):
T = (T + int(S[(N - 1) - i]) * pow(10, i, mod=P)) % P
count[T] += 1
ans = 0
for k in count:
ans = (ans + k * (k - 1) // 2)
print(ans)
if __name__ == "__main__":
main()
| 0 | null | 18,018,700,521,648 | 91 | 166 |
S = input()
T = input()
lens = len(S)
lent = len(T)
ans = []
for i in range(lens - lent + 1):
ss = S[i:i+lent]
count = 0
for j in range(len(ss)):
if(ss[j] != T[j]):
count += 1
ans.append(count)
print(min(ans))
|
#nord_aで使っている色の+1
#初期入力
import sys
input = sys.stdin.readline
#from collections import deque
import heapq
N = int(input())
connection ={i:set() for i in range(1,N+1)}
A =[[i] for i in range(N-1)]
A_sort =sorted(A)
for i in range(N-1):
a,b = (int(x) for x in input().split())
connection[a].add(b) # つながりを逆向きも含めて入力
connection[b].add(a) #
A_sort[i].append((a,b)) #色を出力するため
A_sort.sort(key=lambda x:x[1])
#色の数(各頂点の最大値)
color_max =0
for i in connection.values():
color_max =max(color_max, len(i) )
#各頂点に色を準備⇒辺の色決定
aa=[]
hq_none =heapq.heapify(aa)
color_node ={i:[] for i in range(1,N+1)} #頂点に使った色の管理,dequeで高速?
for i in range(1,N+1):
heapq.heapify(color_node[i])
color_edge =[0]*(N-1)
for i in range(N-1):
n_a,n_b = A_sort[i][1] #i番目の辺とつながっている頂点
if color_node[n_a] ==[]:
color =0
else:
color =(color_node[n_a][0]) -1
while True:
#color +=1
q,color =divmod(color,(-1)*color_max)
if color not in color_node[n_a] and color not in color_node[n_b]:
#color_edge[i] =color+1 #辺の色を出力用に決める。1~color_max
A_sort[i].append((-1)*color+1) #辺の色を出力用に決める。1~color_max,A_sortに入れて、再度ソート
heapq.heappush(color_node[n_a],color) #前の頂点に使った色を追加、
heapq.heappush(color_node[n_b],color) #heapqの最大値出したいので*(-1)
break
else:
color -=1
#出力
print(color_max)
A_sort.sort()
for i in A_sort:
print(i[2])
| 0 | null | 69,540,587,093,692 | 82 | 272 |
S = input()
K = int(input())
N = len(S)
#全て同じ文字の時
if len(set(S)) == 1:
print(N*K//2)
exit()
#隣合う文字列が何個等しいか
cycle = 0
tmp = S[0]
cnt = 1
for i in range(1, N):
if tmp != S[i]:
cycle += cnt//2
tmp = S[i]
cnt = 1
else:
cnt += 1
cycle += cnt//2
if S[0] == S[-1]:
i = 0
a = 0
tmp = S[0]
while S[i] == tmp:
i += 1
a += 1
j = N-1
b = 0
tmp = S[-1]
while S[j] == tmp:
j -= 1
b += 1
print((cycle*K) - (a//2 + b//2 - (a+b)//2)*(K-1))
else:
print(cycle*K)
|
S = list(input())
K = int(input())
ans = 0
#2つ並べて一般性を確かめる
S2 = S*2
for i in range(1,len(S2)):
if S2[i-1]== S2[i]:
S2[i] = '@'
#2回目以降はk-1回任意の@へ直す
if i>=len(S2)//2:
ans+= K-1
else:
ans+=1
if len(set(S)) ==1 and len(S)%2 !=0:
ans = len(S)*(K//2)+(K%2==1)*(len(S)//2)
print(ans)
| 1 | 175,578,444,109,112 | null | 296 | 296 |
n, k, c = map(int, input().split())
s = input()
l = [0] * k
r = [0] * k
p = 0
# for i in range(n):
i = 0
while i < n:
if s[i] == "o":
l[p] = i
p += 1
if (p >= k):
break
i += c
i += 1
p = k-1
# for i in range(n - 1, -1, -1):
i = n - 1
while i >= 0:
if s[i] == "o":
r[p] = i
p -= 1
if (p < 0):
break
i -= c
i -= 1
#print(l, r)
for i in range(k):
if l[i] == r[i]:
print(l[i]+1)
|
N, K, C = map(int, input().split())
S = list(input())
# 左詰めの働くリスト
l_list = [-1 for i in range(N)]
i = 0
cnt = 0
while(i < N and cnt < K):
if(S[i] == "o"):
l_list[i] = cnt
cnt += 1
i += C
i += 1
# 右詰めの働くリスト
r_list = [-1 for i in range(N)]
i = N - 1
cnt = 0
while(i >= 0 and cnt < K):
if(S[i] == "o"):
r_list[i] = K-1-cnt
cnt += 1
i -= C
i -= 1
# 両方のリストで一致するマス(日)を出力
for i in range(N):
if(l_list[i] == -1 or r_list[i] == -1):
continue
if(l_list[i] == r_list[i]):
print(i+1)
| 1 | 40,937,039,967,560 | null | 182 | 182 |
N = int(input())
X = list(map(int, input().split()))
ans = [0] * (N + 1)
for v in X:
ans[v] += 1
print(*ans[1:], sep="\n")
|
import itertools
n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
r = list(itertools.permutations([i for i in range(1,n+1)]))
P , Q = (r.index(p)), (r.index(q))
print(abs(P-Q))
| 0 | null | 66,688,544,549,308 | 169 | 246 |
def main():
n = int(input())
_list = list(range(1,n+1))
for x in _list:
if x % 3 == 0 or any(list(map(lambda x: x == 3, list(map(int,list(str(x))))))):
print(' %d' % x, end = '')
print()
main()
|
n = int(input())
print(" " + " ".join([str(i) for i in range(1, n+1) if i % 3 == 0 or '3' in str(i)]))
| 1 | 926,503,941,650 | null | 52 | 52 |
n = int(input())
min_value = 10**10
result = -(10**10)
for _ in [0]*n:
current = int(input())
result = max(result, current-min_value)
min_value = min(min_value, current)
print(result)
|
n = int(input())
maxv = -20000000000
minv = int(input())
for i in range(n-1):
R = int(input())
maxv = max(maxv, R - minv)
minv = min(minv, R)
print(maxv)
| 1 | 13,392,945,512 | null | 13 | 13 |
S = input()
N = len(S)
ans = ["x" for i in range(N)]
print(*ans, sep="")
|
S=input()
ch=""
for i in range(len(S)):
ch+="x"
print(ch)
| 1 | 72,987,396,530,958 | null | 221 | 221 |
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)
N, K, C = mapint()
S = list(input())
r_S = S[::-1]
pos = [0]*N
neg = [0]*N
ok = 0
cnt = 0
for i in range(N):
if S[i]=='o':
if ok<=i:
pos[i] = cnt + 1
cnt += 1
ok = i + C + 1
if cnt==K:
break
else:
pass
ok = 0
cnt = 0
for i in range(N):
if r_S[i]=='o':
if ok<=i:
neg[i] = cnt + 1
cnt += 1
ok = i + C + 1
if cnt==K:
break
else:
pass
ans = []
neg = neg[::-1]
for i in range(N):
if pos[i]+neg[i]-1==K:
ans.append(i)
for a in ans:
print(a+1)
|
#!/usr/bin/env python3
# Generated by https://github.com/kyuridenamida/atcoder-tools
from typing import *
import collections
import functools
import itertools
import math
import sys
INF = float('inf')
def solve(S: str):
return sum(S[i] != S[-(1 + i)] for i in range(len(S)//2))
def main():
sys.setrecursionlimit(10 ** 6)
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
S = next(tokens) # type: str
print(f'{solve(S)}')
if __name__ == '__main__':
main()
| 0 | null | 79,903,273,744,882 | 182 | 261 |
'''
Created on 2020/09/03
@author: harurun
'''
from dataclasses import *
@dataclass
class UnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
return -self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
def members(self, x):
root = self.find(x)
return [i for i in range(self.n) if self.find(i) == root]
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
def group_count(self):
return len(self.roots())
def all_group_members(self):
return {r: self.members(r) for r in self.roots()}
def __str__(self):
return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
def main():
import sys
pin=sys.stdin.readline
N,M=map(int,pin().split())
uf=UnionFind(N)
for i in range(M):
A,B=map(int,pin().split())
A-=1
B-=1
uf.union(A, B)
ans=0
for j in range(N):
ans=max(ans,uf.size(j))
print(ans)
return
main()
#解説AC
|
class UnionFind:
def __init__(self, n):
self.par = [i for i in range(n+1)]
self.rank = [0] * (n+1)
self.size = [1] * (n+1)
def find(self, x):
if self.par[x] == x:
return x
else:
self.par[x] = self.find(self.par[x])
return self.par[x]
def same_check(self, x, y):
return self.find(x) == self.find(y)
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.rank[x] < self.rank[y]:
self.par[x] = y
self.size[y] += self.size[x]
else:
self.par[y] = x
self.size[x] += self.size[y]
if self.rank[x] == self.rank[y]:
self.rank[x] += 1
n, m = map(int, input().split())
uf = UnionFind(n)
for i in range(m):
a, b = map(int, input().split())
uf.union(a, b)
max = 1
for size in uf.size:
if size > max:
max = size
print(max)
| 1 | 3,985,059,605,214 | null | 84 | 84 |
A=map(int,raw_input().split(" "))
vec=A[0]
col=A[1]
def zero(n):
zerolist=list()
for i in range(n):
zerolist+=[0]
return(zerolist)
A=list()
for i in range(vec):
A+=[zero(col)]
for i in range(vec):
k=map(int,raw_input().split(" "))
for j in range(col):
A[i][j]=k[j]
#ok
v=zero(col)
for i in range(col):
v[i]=int(raw_input())
k=0
for i in range(vec):
for j in range(col):
k+=A[i][j]*v[j]
print k
k=0
|
n, m = map(int, raw_input().split())
a, b, result = list(), list(), list()
for _ in range(n):
a.append(map(int, raw_input().split()))
for _ in range(m):
b.append(int(raw_input()))
for i in range(n):
temp = 0
for j in range(m):
temp += a[i][j] * b[j]
result.append(temp)
for i in result:
print(i)
| 1 | 1,147,712,847,360 | null | 56 | 56 |
N=int(input())
A=list(map(int,input().split()))
ans=0
tree=1
node=sum(A)
flag=0
for i in A:
ans+=i
node-=i
if i>tree:
ans=-1
break
tree-=i
if tree<node:
ans+=tree
else:
ans+=node
tree=2*tree
print(ans)
|
n=int(input())
s=100000
for i in range(n):
s=int(s*1.05)
if s % 1000>0:
s=s//1000*1000+1000
print(s)
| 0 | null | 9,446,773,165,536 | 141 | 6 |
import sys
# input = sys.stdin.readline
def main():
S, T =input().split()
print(T,S,sep="")
if __name__ == "__main__":
main()
|
print("".join(reversed(input().split())))
| 1 | 102,605,226,619,870 | null | 248 | 248 |
n, k = map(int,input().split())
H = sorted(list(map(int, input().split())), reverse=True)
cnt = 0
for i in range(n):
if H[i] >= k:
cnt += 1
elif H[i] < k:
break
print(cnt)
|
a, b, c = map(int, input().split())
sahen = 4*a*b
uhen = (c-a-b)**2
if c-a-b < 0:
ans = "No"
elif sahen < uhen:
ans = "Yes"
else:
ans = "No"
print(ans)
| 0 | null | 115,186,830,302,012 | 298 | 197 |
N, S = int(input()), input()
print("Yes" if N%2 == 0 and S[:(N//2)] == S[(N//2):] else "No")
|
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
C1 = A1 - B1
C2 = A2 - B2
if C1 > 0:
C1, C2 = -C1, -C2
x1 = C1 * T1 + C2 * T2
x2 = x1 + C1 * T1
if x1 == 0:
print('infinity')
elif x1 < 0:
print(0)
else:
if 0 < x2:
count = 1
elif 0 == x2:
count = 2
else:
n = (-x2 + x1 - 1) // x1
count = 1 + 2 * n
if x2 % x1 == 0:
count += 1
print(count)
| 0 | null | 139,346,517,282,390 | 279 | 269 |
a = 1000 - int(input()) % 1000
print(a if a != 1000 else 0)
|
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)
A, B = mapint()
if 1<=A<=9 and 1<=B<=9:
print(A*B)
else:
print(-1)
| 0 | null | 83,647,682,511,470 | 108 | 286 |
import math
def main():
a,b,c = map(int,input().split())
left = 4*a*b
right = (c-a-b)**2
if (c-a-b)<0:
return 'No'
if left < right:
return 'Yes'
else:
return 'No'
print(main())
|
n,k = list(map(int,input().split()))
point = list(map(int,input().split()))
rsp = ['r', 's', 'p']
t = input()
m = [rsp[rsp.index(i)-1] for i in t]
ans = 0
for i in range(n):
if i<k :
ans += point[rsp.index(m[i])]
else:
if m[i]!=m[i-k]:
ans += point[rsp.index(m[i])]
else:
try:
m[i] = rsp[rsp.index(m[i+k])-1]
except:
pass
# print(ans)
print(ans)
| 0 | null | 79,545,172,442,720 | 197 | 251 |
n,k=map(int,input().split())
r,s,p = map(int,input().split())
T=list(input())
a=[[] for i in range(k)]
for i in range(n):
a[i%k].append(T[i])
score=0
for j in range(k):
b=a[j]
flg=0
for l in range(len(b)):
if l==0:
if b[l]=='r':
score+=p
elif b[l]=='s':
score+=r
elif b[l]=='p':
score+=s
else:
if b[l-1]==b[l] and flg==0:
flg=1
continue
elif b[l-1]==b[l] and flg==1:
if b[l]=='r':
score+=p
elif b[l]=='s':
score+=r
elif b[l]=='p':
score+=s
flg=0
elif b[l-1]!=b[l]:
if b[l]=='r':
score+=p
elif b[l]=='s':
score+=r
elif b[l]=='p':
score+=s
flg=0
print(score)
|
x = int(input())
print("Yes") if x >= 30 else print("No")
| 0 | null | 56,107,442,355,104 | 251 | 95 |
from operator import itemgetter
N, T = [int(i) for i in input().split()]
data = []
for i in range(N):
data.append([int(i) for i in input().split()])
data.sort(key=itemgetter(0))
dp = [[0]*(T) for i in range(N)]
ans = data[0][1]
for i in range(1, N):
for j in range(T):
if j >= data[i-1][0]:
value1 = dp[i-1][j]
value2 = dp[i-1][j-data[i-1][0]] + data[i-1][1]
dp[i][j] = max(value1, value2)
else:
dp[i][j] = dp[i-1][j]
value3 = dp[i][T-1] + data[i][1]
ans = max(value3, ans)
print(ans)
|
def knapsack_weight(single=True):
"""
重さが小さい時のナップサックDP
:param single: True = 重複なし
"""
""" dp[weight <= W] = 重さ上限を固定した時の最大価値 """
dp_min = 0 # 総和価値の最小値
dp = [dp_min] * (W + 1)
for item in range(N):
if single:
S = range(W, weight_list[item] - 1, -1)
else:
S = range(weight_list[item], W + 1)
for weight in S:
if weight - weight_list[item] < T:
dp[weight] = max2(dp[weight], dp[weight - weight_list[item]] + price_list[item])
return max(dp[T:])
import sys
input = sys.stdin.readline
def max2(x, y):
return x if x > y else y
def min2(x, y):
return x if x < y else y
N, T = map(int, input().split()) # N: 品物の種類 W: 重量制限
price_list = []
weight_list = []
data = []
for _ in range(N):
weight, price = map(int, input().split())
data.append((weight, price))
data.sort()
for weight, price in data:
price_list.append(price)
weight_list.append(weight)
max_weight = max(weight_list)
W = T + max_weight - 1
print(knapsack_weight(single=True))
| 1 | 151,121,784,393,062 | null | 282 | 282 |
K = int(input())
num = 0
for i in range(0,K+1):
num = (num*10+7)%K
if num==0:
print(i+1,'\n')
break
if num:
print("-1\n")
|
N = int(input())
b=[]
c=[]
for j in range(N):
a = input()
b.append(a)
c = set(b)
print(len(c))
| 0 | null | 18,216,111,214,340 | 97 | 165 |
import sys
sys.setrecursionlimit(10**7)
def input(): return sys.stdin.readline().rstrip()
def main():
x, k, d = map(int, input().split())
desired = abs(x) // d
if k <= desired:
if x < 0:
x += k * d
else:
x -= k * d
print(abs(x))
return
if x < 0:
x += desired * d
else:
x -= desired * d
if (k - desired) % 2 == 0:
print(abs(x))
return
if x == 0:
print(d)
elif x > 0:
x -= d
print(abs(x))
elif x < 0:
x += d
print(abs(x))
if __name__ == '__main__':
main()
|
from collections import deque
X, K, D = map(int, input().split())
# まずは何回目で符号が逆転するかを算出
num_reverse = abs(X) // D + 1
if num_reverse > K:
print(abs(X) - (K * D))
else:
K -= num_reverse
ans = abs(X) - (num_reverse * D)
if K % 2 == 0:
print(abs(ans))
else:
print(abs(abs(ans) - D))
| 1 | 5,189,183,036,674 | null | 92 | 92 |
n = int(input())
a = list(map(int, input().split()))
m = [0] * (n+1)
mx = 1
for i in range(n+1):
mx -= a[i]
m[i] = mx
if (mx <= 0 and i != n) or (mx < 0 and i == n):
print(-1)
exit()
mx *= 2
a.reverse()
m.reverse()
m[0] = a[0]
for i in range(1, n+1):
m[i] = min(m[i], m[i-1]) + a[i]
print(sum(m))
|
import sys
import math
import heapq
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 sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def FLIST(n):
res = [1]
for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
return res
N=II()
AS=LI()
SM=sum(AS)
B=1
res=0
M = 1
m = 1
for a in AS:
B = M-a
if B < 0:
print(-1)
exit(0)
res += a
res += B
SM -= a
M = min(B*2, SM)
m = B
print(res)
| 1 | 18,865,608,655,242 | null | 141 | 141 |
print "\n".join(['%dx%d=%d'%(x,y,x*y) for x in range(1,10) for y in range(1,10)])
|
x=int(input())
print("%d:%d:%d"%(x/3600,(x%3600)/60,x%60))
| 0 | null | 168,288,074,082 | 1 | 37 |
from bisect import bisect_left
n = int(input())
l = list(map(int, input().split()))
l.sort()
ans = 0
for i in range(n):
for j in range(i + 1, n):
k = l[i] + l[j]
pos = bisect_left(l, k)
ans += max(0, pos - j - 1)
print(ans)
|
import bisect
def main():
n = int(input())
l = sorted(list(int(i) for i in input().split()))
cnt = 0
for i in range(n - 2):
a = l[i]
for j in range(i + 1, n-1):
b = l[j]
cnt += bisect.bisect_left(l, a+b)-(j+1)
print(cnt)
if __name__ == "__main__":
main()
| 1 | 172,332,809,227,108 | null | 294 | 294 |
n=int(input())
m=1000000007
print(((pow(10,n,m)-2*pow(9,n,m)+pow(8,n,m))+m)%m)
|
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)
| 1 | 3,186,556,807,658 | null | 78 | 78 |
n,k =map(int,input().split())
l = list(map(int,input().split()))
l = sorted(l)
print(sum(l[0:k]))
|
import sys
sys.setrecursionlimit(10**7)
def dfs(v,p,d):
dist[v]=d
for nv in G[v]:
if nv==p:
continue
dfs(nv,v,d+1)
def getSP(v):
p=v
if dist[v]==lim:
return p
for nv in G[v]:
if dist[nv]<dist[v]:
p=getSP(nv)
return p
def depth(v,p,d):
sdist[v]=d
for nv in G[v]:
if dist[nv]<dist[v]:
continue
depth(nv,v,d+1)
N,u,v=map(int,input().split())
u,v=u-1,v-1
G=[[] for i in range(N)]
for i in range(N-1):
a,b=map(lambda x:int(x)-1,input().split())
G[a].append(b)
G[b].append(a)
dist=[-1]*N
dfs(v,-1,0)
lim=dist[u]-(dist[u]-1)//2
SP=getSP(u)
sdist=[-1]*N
depth(SP,-1,0)
D,tmp=-1,-1
for i in range(N):
if sdist[i]>tmp:
D=i
tmp=sdist[i]
print(dist[D]-1)
| 0 | null | 64,357,633,387,378 | 120 | 259 |
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_1_D
#?????§??????
#??????????????????????????°???????????????????????¢?????´?????????????????????
def get_maximum_profit(value_list, n_list):
minv = pow(10,10) + 1
profit = -minv
for v in value_list:
profit = max(profit, v - minv)
minv = min(minv, v)
return profit
def main():
n_list = int(input())
target_list = [int(input()) for i in range(n_list)]
print(get_maximum_profit(target_list, n_list))
if __name__ == "__main__":
main()
|
N = input()
R = [int(raw_input()) for _ in xrange(N)]
S = [0 for i in xrange(N)]
S[N-1] = R[N-1]
for i in xrange(N-2, -1, -1):
S[i] = max(R[i], S[i+1])
ans = float('-inf')
for i in xrange(N-1):
ans = max(ans, S[i+1]-R[i])
print ans
| 1 | 13,599,221,148 | null | 13 | 13 |
import sys
from collections import defaultdict
from queue import Queue
def main():
input = sys.stdin.buffer.readline
n = int(input())
g = defaultdict(list)
for i in range(n):
line = list(map(int, input().split()))
if line[1] > 0:
g[line[0]] = line[2:]
q = Queue()
q.put(1)
dist = [-1] * (n + 1)
dist[1] = 0
while not q.empty():
p = q.get()
for nxt in g[p]:
if dist[nxt] == -1:
dist[nxt] = dist[p] + 1
q.put(nxt)
for i in range(1, n + 1):
print(i, dist[i])
if __name__ == "__main__":
main()
|
ans = input()
if int(ans) >= 30:
print("Yes")
else:
print("No")
| 0 | null | 2,912,689,244,732 | 9 | 95 |
x=int(input())
print(x*x)
|
from collections import deque
import math
n,d,a = map(int,input().split())
F = []
for _ in range(n):
x,h = map(int,input().split())
F.append((x,h))
F.sort()
total = 0
d *=2
ans = 0
q = deque([])
for x,h in F:
while q and q[0][0]<x:#累積和
total -= q.popleft()[1]
h-=total
if h >0:
tmp = math.ceil(h/a)
ans += tmp
damage = tmp*a
total += damage
q.append((x+d,damage))
print(ans)
| 0 | null | 113,533,320,555,970 | 278 | 230 |
integers=[]
i=0
j=0
while True:
num=int(input())
integers.append(num)
if integers[i]==0:
del integers[i]
break
i+=1
while j<i:
print('Case {}: {}'.format(j+1,integers[j]))
j+=1
|
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
for p in range(1,4):
print("{0:.6f}".format(pow(sum([pow(abs(x[i] - y[i]), p) for i in range(n)]) ,1/p)))
print("{0:.6f}".format(pow(pow(max([abs(x[i] - y[i]) for i in range(n)]),n),1/n)))
| 0 | null | 350,054,836,092 | 42 | 32 |
n = int(input())
minv = int(input())
maxv = -1000000000
for i in range(1,n):
r = int(input())
m = r-minv
if maxv < m < 0:
maxv = m
minv = r
elif maxv < m:
maxv = m
elif m < 0:
minv = r
print(maxv)
|
# -*-coding:utf-8
class Dice:
def __init__(self, diceList):
self.d1, self.d2, self.d3, self.d4, self.d5, self.d6 = diceList
def diceRoll(self, r):
if(r == 'N'):
self.d1, self.d2, self.d5, self.d6 = self.d2, self.d6, self.d1, self.d5
elif(r == 'E'):
self.d1, self.d3, self.d4, self.d6 = self.d4, self.d1, self.d6, self.d3
elif(r == 'W'):
self.d1, self.d3, self.d4, self.d6 = self.d3, self.d6, self.d1, self.d4
elif(r == 'S'):
self.d1, self.d2, self.d5, self.d6 = self.d5, self.d1, self.d6, self.d2
def main():
inputDiceList = list(map(int, input().split()))
rollList = list(input())
d = Dice(inputDiceList)
for i in rollList:
d.diceRoll(i)
print(d.d1)
if __name__ == '__main__':
main()
| 0 | null | 125,690,418,072 | 13 | 33 |
N = int(input())
if N == 1 or N == 2:
print(0)
else:
if N % 2 == 0:
print(N//2-1)
else:
print(N//2)
|
import copy
n,m = map(int,input().split())
a = set(list(map(int,input().split())))
a = list(a)
a_div2 = [0]*len(a)
a2 = copy.deepcopy(a)
# for i in range(len(a)):
# if a[i] % 2 == 1:
# print(0)
# exit()
for i in range(len(a)):
while a2[i] % 2 == 0:
a2[i] //= 2
a_div2[i] += 1
if len(set(a_div2)) != 1:
print(0)
exit()
def gcd(a,b):
while b != 0:
a, b = b, a % b
return a
def lcm(a,b):
return a // gcd(a,b) * b
if len(a) == 1:
if m < a[0]//2:
print(0)
else:
print((m-a[0]//2)//a[0] + 1)
else:
cnt = 1
for i in range(len(a)):
a[i] //= 2
cnt = lcm(cnt,a[i])
if m < cnt:
print(0)
else:
print((m-cnt)//(cnt*2)+1)
| 0 | null | 127,725,980,032,050 | 283 | 247 |
#K = input()
NR = list(map(int, input().split()))
#s = input().split()
#N = int(input())
if (NR[0] >= 10):
print(NR[1])
else:
print(NR[1] + 100 * (10 - NR[0]))
|
N,R=map(int,input().split())
if N<10:
print(int(R+(100*(10-N))))
else:
print(R)
| 1 | 63,502,280,082,782 | null | 211 | 211 |
a,b = input().split()
if len(a) + len(b) == 2:
print(int(a) * int(b))
else:
print(-1)
|
def kuku(a, b):
return a*b if max(a, b) < 10 else -1
def main():
a, b = map(int, input().split())
print(kuku(a, b))
if __name__ == '__main__':
main()
| 1 | 158,814,398,113,600 | null | 286 | 286 |
N, K = [int(n) for n in input().split()]
NUM = 1000000007
def modpow(a, b): # (a ** b) % NUM
ans = 1
while b != 0:
if b % 2 == 1:
ans *= a
ans %= NUM
a = a * a
a %= NUM
b //= 2
return ans
C = [0 for _ in range(K+1)]
for d in range(K):
k = K - d # K ... 1
L = K // k
C[k] = modpow(L, N) # A = 1*k ... L*k
for l in range(2, L+1):
C[k] -= C[l*k]
if C[k] < 0:
C[k] += NUM
C[k] %= NUM
ans = 0
for k in range(1, K+1):
ans += k * C[k]
ans %= NUM
print(ans)
|
import sys
import math
a = []
for line in sys.stdin:
a.append(line)
for n in a:
inl=n.split()
num1=int(inl[0])
check=1
list=[]
while check<=math.sqrt(num1):
if num1%check==0:
list.append(check)
list.append(num1/check)
check+=1
list.sort()
list.reverse()
num2=int(inl[1])
for i in list:
if num2%i==0:
gud=i
break
lcm=num1*num2/gud
print gud,lcm
| 0 | null | 18,481,744,572,028 | 176 | 5 |
import math
n,x,t = map(int,input().strip().split(" "))
ans = math.ceil(n/x)
ans = ans*t
print(ans)
|
def main():
N, X, T = (int(i) for i in input().split())
print((0--N//X)*T)
if __name__ == '__main__':
main()
| 1 | 4,259,631,252,548 | null | 86 | 86 |
def insertion_sort(A, n, g, cnt):
for i in range(g, n):
v = A[i]
j = i - g
while j >= 0 and A[j] > v:
A[j+g] = A[j]
cnt += 1
j -= g
A[j+g] = v
return A, cnt
def shell_sort(A, n):
cnt = 0
g = 1
G = []
while g <= n:
G.append(g)
g = 3 * g + 1
G.reverse()
m = len(G)
for g in G:
A, cnt = insertion_sort(A, n, g, cnt)
return m, G, cnt, A
n = int(input().rstrip())
A = []
for _ in range(n):
A.append(int(input().rstrip()))
m, G, cnt, A = shell_sort(A, n)
print(m)
print(' '.join(list(map(str, G))))
print(cnt)
print('\n'.join(list(map(str, A))))
|
def insertion_sort(A, n, g, cnt):
for i in range(g, n):
v = A[i]
j = i - g
while j >= 0 and v < A[j]:
A[j+g] = A[j]
j -= g
cnt += 1
A[j+g] = v
return cnt
def shell_sort(A, n):
cnt = 0
h = 1
g = []
while h <= n:
g.append(h)
h = 3*h + 1
g.reverse()
m = len(g)
for i in range(m):
cnt = insertion_sort(A, n, g[i], cnt)
show(n, m, g, A, cnt)
def show(n, m, G, A, cnt):
print(m)
for i in range(m):
if i != m-1:
print(G[i], end = " ")
else:
print(G[i])
print(cnt)
for i in range(n):
print(A[i])
if "__main__" == __name__:
n = int(input())
data = []
for i in range(n):
data.append(int(input()))
shell_sort(data, n)
| 1 | 33,221,346,480 | null | 17 | 17 |
for n in range(1,10):
for m in range(1,10):
print(n,"x",m,"=",n*m,sep="")
|
a, b, k = map(int, input().split())
total = a+b
if total <= k:
ans = [0, 0]
else:
if a <= k:
k -= a
a = 0
b -= k
ans = [a, b]
else:
a -= k
ans = [a, b]
print(*ans)
| 0 | null | 52,120,835,651,888 | 1 | 249 |
i = int(input())
print((i-1)//2)
|
n = int(input())
print((n - (n+1)%2)//2)
| 1 | 153,417,923,511,662 | null | 283 | 283 |
#144_C
import math
n = int(input())
m = math.floor(math.sqrt(n))
div = 1
for i in range(1,m+1):
if n % i == 0:
div = i
print(int(div + n/div -2))
|
if __name__ == "__main__":
A,B,C = map(int,input().split())
K = int(input())
count = 0
while A>=B:
B *= 2
count += 1
while B>=C:
C *= 2
count += 1
print("Yes" if count <= K else "No")
| 0 | null | 84,542,846,019,332 | 288 | 101 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.