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
|
---|---|---|---|---|---|---|
import sys
input = sys.stdin.readline
def read():
K = int(input().strip())
S = input().strip()
return K, S
def binom_preprocess(n, MOD=10**9+7):
f = [0 for i in range(n+1)] # n!
invf = [0 for i in range(n+1)] # (n!)^-1
f[0] = 1
f[1] = 1
invf[0] = 1
invf[1] = 1
for i in range(2, n+1):
f[i] = f[i-1] * i % MOD
invf[n] = pow(f[n], MOD-2, MOD)
for i in range(n, 2, -1):
invf[i-1] = invf[i] * i % MOD
return f, invf
def binom(n, k, f, invf, MOD=10**9+7):
if n < k or n < 0 or k < 0:
return 0
else:
return (f[n] * invf[k] % MOD) * invf[n-k] % MOD
def solve(K, S, MOD=10**9+7):
T = len(S)
f, invf = binom_preprocess(K+T+1)
ans = 0
for i in range(K+1):
ans += pow(25, i, MOD) * binom(T-1+i, i, f, invf, MOD) * pow(26, K-i, MOD)
ans %= MOD
print(ans)
if __name__ == '__main__':
inputs = read()
outputs = solve(*inputs)
if outputs is not None:
print("%s" % str(outputs))
|
n = int(input())
S = input()
q = int(input())
class BIT:
def __init__(self, n):
"""
初期化
Input:
n: 配列の大きさ
"""
self.n = n
self.bit = [0] * (n+10)
def add(self, x, w):
"""
更新クエリ(add)
Input:
x: インデックス
w: 加える値
Note:
self.bitが更新される
"""
while x <= self.n:
self.bit[x] += w
x += x&-x
def sum(self, x):
"""
部分和算出クエリ
Input:
x: インデックス
Return:
[1,a]の部分和
"""
cnt = 0
while x > 0:
cnt += self.bit[x]
# 次の最下位桁はどこか
x -= x&-x
return cnt
def psum(self, a, b):
"""
区間[a,b)(0-indexed)における和
Input
a,b: 半開区間
Return
[a,b)の和
"""
return self.sum(b-1) - self.sum(a-1)
def str2num(s):
return ord(s)-ord("a")
bits = [BIT(n) for i in range(26)]
# 入力
ans = []
for i, s in enumerate(S):
ind = str2num(s)
bits[ind].add(i+1, 1)
S = list(S)
for _ in range(q):
x,a,b = input().split()
if x == "1":
i = int(a)
# 更新処理
ind_before = str2num(S[i-1])
ind = str2num(b)
bits[ind_before].add(i,-1)
bits[ind].add(i, 1)
S[i-1] = b
else:
l = int(a)
r = int(b)
cnt = 0
for bit in bits:
if bit.psum(l,r+1) > 0:
cnt += 1
ans.append(cnt)
print(*ans)
| 0 | null | 37,783,005,137,338 | 124 | 210 |
# 解説AC
mod = 10 ** 9 + 7
n, k = map(int, input().split())
dp = [-1] * (k + 1)
ans = 0
for i in range(k, 0, -1):
dp[i] = pow(k // i, n, mod)
t = 0
t += 2 * i
while t <= k:
dp[i] -= dp[t]
dp[i] %= mod
t += i
ans += i * dp[i]
ans %= mod
print(ans)
|
#!/usr/bin/env python
n, k = map(int, input().split())
mod = 10**9+7
d = [-1 for _ in range(k+1)]
d[k] = 1
for i in range(k-1, 0, -1):
d[i] = pow(k//i, n, mod)
j = 2*i
while j <= k:
d[i] -= d[j]
j += i
#print('d =', d)
ans = 0
for i in range(1, k+1):
ans += (i*d[i])%mod
print(ans%mod)
| 1 | 36,992,185,016,668 | null | 176 | 176 |
def f():return map(int,raw_input().split())
n,m,l=f()
A = [f() for _ in [0]*n]
B = [f() for _ in [0]*m]
C = [[0 for _ in [0]*l] for _ in [0]*n]
for i in range(n):
for j in range(l):
print sum([A[i][k]*B[k][j] for k in range(m)]),
print
|
# coding: utf-8
def main():
n, m, l = map(int, raw_input().split())
mat_A = [] # (n, m)
mat_B = [] # (m, l)
for i in range(n):
mat_A.append(map(int, raw_input().split()))
for j in range(m):
mat_B.append(map(int, raw_input().split()))
for i in range(n):
c = [0 for g in range(l)]
for j in range(l):
for k in range(m):
# print "n:{}, m:{}, l:{}".format(i,k,j)
try:
c[j] += mat_A[i][k] * mat_B[k][j]
except:
print "n: {}, m: {}, l: {}".format(i,k,j)
print " ".join(map(str, c))
if __name__ == "__main__":
main()
| 1 | 1,413,082,489,360 | null | 60 | 60 |
# encoding:utf-8
import copy
import random
import bisect #bisect_left これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000
d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))
N, K = LI()
# 逆に考える
"""
X = gcd(A_1, A_2,...A_K)
となるようなXを考えると溶ける
X = 3の時その個数は
[K/3]**N
"""
x_cnt = [0] * (K + 1)
for x in range(K, 0, -1):
# print(x)
tmp = pow(K // x, N, mod)
for j in range(x + x, K+1, x):
tmp -= x_cnt[j]
x_cnt[x] = tmp
ans = 0
for i in range(1,K+1):
ans += i * x_cnt[i]
ans %= mod
print(ans)
|
import sys
sys.setrecursionlimit(10 ** 8)
ni = lambda: int(sys.stdin.readline())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
ns = lambda: sys.stdin.readline().rstrip()
MOD = 10 ** 9 + 7
N, K = nm()
def solve():
ans = 0
tbl = [0] * (K + 1)
for i in range(K, 0, -1):
m = K // i
p = pow(m, N, MOD)
j = 2
while j * i <= K:
p += MOD - tbl[j * i] % MOD
p %= MOD
j += 1
tbl[i] = p
ans += i * p % MOD
ans %= MOD
return ans
print(solve())
| 1 | 36,678,629,289,028 | null | 176 | 176 |
def resolve():
INF = 10 ** 18
N, M, L = map(int, input().split())
G = [[INF] * N for _ in range(N)]
for _ in range(M):
a, b, c = map(int, input().split())
a -= 1
b -= 1
G[a][b] = G[b][a] = c
for k in range(N):
for i in range(N):
for j in range(N):
G[i][j] = min(G[i][j], G[i][k] + G[k][j])
Cost = [[INF] * N for _ in range(N)]
for i in range(N):
for j in range(N):
if G[i][j] <= L:
Cost[i][j] = 1
for k in range(N):
for i in range(N):
for j in range(N):
Cost[i][j] = min(Cost[i][j], Cost[i][k] + Cost[k][j])
Q = int(input())
for _ in range(Q):
a, b = map(lambda x: int(x) - 1, input().split())
if Cost[a][b] == INF:
print(-1)
else:
print(Cost[a][b] - 1)
if __name__ == "__main__":
resolve()
|
a = input()
a = int(a)
h = a//3600
m = (a-3600*h)//60
s = (a-3600*h-60*m)
print(str(h)+':'+str(m)+':'+str(s))
| 0 | null | 86,532,641,884,150 | 295 | 37 |
def isprime(n):
divider = 2
while divider ** 2 <= n:
if n % divider == 0:
return False
divider += 1
return True
result = 0
n = int(input())
for n in range(n):
result += isprime(int(input()))
print(result)
|
def isPrime(x):
if x==2:
return True
if x<2 or x%2==0:
return False
for n in range(3,int(x**0.5)+1,2):
if x%n==0:
return False
return True
n=int(input())
num=[int(input()) for i in range(n)]
i=0
for nm in num:
if isPrime(nm):
i+=1
print(i)
| 1 | 10,179,221,440 | null | 12 | 12 |
ele_and_tar = []
rs = []
flag1 = 1
flag2 = 0
while flag1:
data = [int(x) for x in input().split()]
if data == [0,0]:
flag1 = 0
else:
ele_and_tar.append(data)
for i in range(len(ele_and_tar)):
rs.append(0)
for math in ele_and_tar:
for i in range(1,math[0]+1):
for j in range(i+1,math[0]+1):
for k in range(j+1,math[0]+1):
if (i + j + k) == math[1]:
rs[flag2] = rs[flag2] + 1
flag2 = flag2 + 1
for out in rs:
print(out)
|
while True:
(n, x) = [int(i) for i in input().split()]
if n == x == 0:
break
dataset = []
for a in range(1, n + 1):
for b in range(a + 1, n + 1):
for c in range(b + 1, n + 1):
dataset.append([a,b,c])
count = 0
for data in dataset:
if sum(data) == x:
count += 1
print(count)
| 1 | 1,292,023,429,128 | null | 58 | 58 |
a, b = map(int, input().split())
if a >= 10 or b >= 10:
print(-1)
else:
print(a*b)
|
N = int(input())
print((N // 2) - 1 if N % 2 == 0 else N // 2)
| 0 | null | 155,535,914,510,588 | 286 | 283 |
n,a,b=map(int,input().split())
p=a+b
c=n%p
d=n//p
if c<a:
print(d*a+c)
else:
print((d+1)*a)
|
while True:
try:
a,b=(int(i) for i in input().split())
print(len(str(a+b)))
except EOFError:
break
| 0 | null | 27,705,382,512,668 | 202 | 3 |
s=input()
num=len(s)
print("x"*num)
|
S = input()
# S のすべての文字を x で置き換えて出力せよ。
answer = ''
for i in range(len(S)):
answer += 'x'
print(answer)
| 1 | 73,152,404,470,042 | null | 221 | 221 |
N = int(input())
cnt = 0
for _ in range(N):
a, b = map(int, input().split())
if a == b:
cnt += 1
else:
cnt = 0
if cnt == 3:
print('Yes')
exit()
print('No')
|
n=int(input())
a=0
for _ in range(n):
x,y=map(int,input().split())
if x==y:
a+=1
else:
a=0
if a==3:
print("Yes")
exit(0)
print("No")
| 1 | 2,500,074,772,640 | null | 72 | 72 |
s = input()
n = len(s)
dp = [0 for _ in range(n)]
dp[-1] = int(s[-1])
memo = 0
cnt_dic = {}
tmp = 1
ans = 0
cnt_dic[0] = 1
for i in range(n-1, -1, -1):
memo = (memo + int(s[i]) * pow(10, n-i-1, 2019)) % 2019
if not memo in cnt_dic:
cnt_dic[memo] = 0
ans += cnt_dic[memo]
cnt_dic[memo] += 1
print(ans)
|
from collections import deque
n = int(input())
l = deque()
for _ in range (n):
cmd = input().split(' ')
if cmd[0]=='insert':
l.appendleft(cmd[1])
if cmd[0]=='delete':
try:l.remove(cmd[1])
except :pass
if cmd[0]=='deleteFirst':
l.popleft()
if cmd[0]=='deleteLast':
l.pop()
print(*l)
| 0 | null | 15,379,362,525,868 | 166 | 20 |
l=list(map(int,input().split()))
l.sort()
if l[0]==l[1]==l[2] or l[0]!=l[1]!=l[2]:
print("No")
else:
print("Yes")
|
import sys
for line in sys.stdin:
a, b = map(int, line.split())
digitNumber = len(str(a + b))
print(digitNumber)
| 0 | null | 34,005,966,925,160 | 216 | 3 |
import sys
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
def lcm(a, b):
return a * b // gcd(a, b)
def main():
for i in sys.stdin.readlines():
a, b = [int(x) for x in i.split()]
print(gcd(a, b), lcm(a, b))
if __name__ == '__main__':
main()
|
h1,m1,h2,m2,k =(int(x) for x in input().split())
hrtom= (h2-h1)
if (m2-m1 < 0) and (h2-h1 >=0) :
min = (h2-h1)*60 + (m2-m1) - k
print(min)
elif (m2-m1 >= 0) and (h2-h1 >=0 ):
min = (h2-h1)*60 + (m2-m1) - k
print(min)
else:
print('0')
| 0 | null | 8,939,703,636,876 | 5 | 139 |
import bisect
n = int(input())
lines = list(int(i) for i in input().split())
lines.sort()
counter = 0
for i in range(n-2):
for j in range(i+1, n-1):
counter += bisect.bisect_left(lines, lines[i] + lines[j]) - (j + 1)
print(counter)
|
# coding: utf-8
# Your code here!
import numpy as np
import math
A,B,H,M=map(int,input().split())
time=(60*H+M)
a=360/720*time
b=360/60*M
c=math.radians(b-a)
print((A**2+B**2-2*A*B*np.cos(c))**0.5)
| 0 | null | 95,662,137,963,644 | 294 | 144 |
#!/usr/bin/env python3
H1, M1, H2, M2, K = map(int, input().split())
ans = 0
if M1 <= M2:
ans = (H2-H1) * 60 + M2 - M1 - K
else:
ans = (H2-H1-1) * 60 + (60-M1) + M2 - K
if ans < 0:
print(0)
else:
print(ans)
|
x,y=map(int,input().split())
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
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+2*y)%3!=0:
ans=0
else:
a=(-x+2*y)//3
b=(2*x-y)//3
ans=cmb(a+b,a,mod)
print(ans)
| 0 | null | 84,353,649,298,718 | 139 | 281 |
A1 = list(map(str,input().split()))
A2 = list(map(str,input().split()))
A3 = list(map(str,input().split()))
N = int(input())
for k in range(N):
i = str(input())
if i == A1[0]:
A1[0] = 'OK'
if i == A1[1]:
A1[1] = 'OK'
if i == A1[2]:
A1[2] = 'OK'
if i == A2[0]:
A2[0] = 'OK'
if i == A2[1]:
A2[1] = 'OK'
if i == A2[2]:
A2[2] = 'OK'
if i == A3[0]:
A3[0] = 'OK'
if i == A3[1]:
A3[1] = 'OK'
if i == A3[2]:
A3[2] = 'OK'
if A1 == ['OK','OK','OK'] or A2 == ['OK','OK','OK'] or A3 == ['OK','OK','OK']:
print('Yes')
exit()
for i in range(3):
if A1[i] == 'OK' and A2[i] == 'OK' and A3[i]== 'OK':
print('Yes')
exit()
if A1[0] == 'OK' and A2[1] == 'OK' and A3[2]== 'OK':
print('Yes')
exit()
if A1[2] == 'OK' and A2[1] == 'OK' and A3[0]== 'OK':
print('Yes')
exit()
print('No')
|
n = int(input())
x = list(map(int,input().split()))
for i in x:
if i%2==0:
if i%6!=0:
if i%10!=0:
print("DENIED")
exit()
print("APPROVED")
| 0 | null | 64,423,449,659,338 | 207 | 217 |
a,b,m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
x = []
for i in range(m):
x.append(list(map(int, input().split())))
min_a = min(a)
min_b = min(b)
money = min_a + min_b
for i in range(m):
kingaku = a[x[i][0]-1] + b[x[i][1]-1] - x[i][2]
if kingaku < money:
money = kingaku
print(money)
|
l = input()
s = l[-1]
c = ''
if s == 's':
c = l + 'es'
else:
c = l + 's'
print(c)
| 0 | null | 28,352,666,223,850 | 200 | 71 |
def main():
from functools import lru_cache
import sys
sys.setrecursionlimit(10 ** 7)
inf = 2 * 10 ** 14 + 1
N = int(input())
*a, = map(int, input().split())
@lru_cache(maxsize=None)
def recursion(cur, need):
"""
cur: pickableなindex
"""
if cur >= N:
if need == 0:
return 0
else:
return -inf
rest = N - cur
if (rest + 1) // 2 < need:
return -inf
return max(
a[cur] + recursion(cur + 2, need - 1),
recursion(cur + 1, need)
)
ans = recursion(0, N // 2)
print(ans)
if __name__ == '__main__':
main()
|
import sys
input=lambda: sys.stdin.readline().rstrip()
n=int(input())
A=[int(i) for i in input().split()]
inf=float("inf")
DP=[[-inf]*4 for _ in range(n+1)]
DP[0][2]=0
for i,a in enumerate(A):
if (i+1)%2==0:
DP[i+1][0]=DP[i][3]+a
if i>=1:
DP[i+1][0]=max(DP[i+1][0],DP[i-1][2]+a)
DP[i+1][2]=DP[i][0]
DP[i+1][3]=max(DP[i][1],DP[i][3])
if i>=1:
DP[i+1][3]=max(DP[i+1][3],DP[i-1][2])
else:
DP[i+1][0]=DP[i][2]+a
DP[i+1][1]=DP[i][3]+a
DP[i+1][3]=max(DP[i][0],DP[i][2])
if n%2==0:
print(max(DP[n][0],DP[n][2]))
else:
print(max(DP[n][1],DP[n][3]))
| 1 | 37,534,177,878,638 | null | 177 | 177 |
n = input()
l = map(int, raw_input().split())
k = 0
a = []
while n > 0:
a.append(l[n - 1])
n -= 1
print ' '.join(map(str, a))
|
input()
a = list(input().split())
a.reverse()
print(' '.join(a))
| 1 | 990,587,669,392 | null | 53 | 53 |
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy
sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7
n = I()
def make_divisors(n):
divisors = set()
for i in range(1, int(n**0.5) + 1):
if n % i == 0:
divisors.add(i)
divisors.add(n // i)
return divisors # sortしたリストで欲しい時はsorted(divisors)
ret = 0
a = make_divisors(n)
for k in a:
if k == 1:
continue
m = n
while m % k == 0:
m //= k
if m % k == 1:
ret += 1
s = make_divisors(n - 1)
s.discard(1)
print(ret + len(s))
|
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)
| 1 | 41,195,927,930,080 | null | 183 | 183 |
from __future__ import division, print_function
from sys import stdin
word = stdin.readline().rstrip().lower()
cnt = 0
for line in stdin:
if line.startswith('END_OF_TEXT'):
break
cnt += line.lower().split().count(word)
print(cnt)
|
from collections import deque
n=int(input())
arr=[[] for _ in range(n)]
for i in range(n-1):
a,b=map(int,input().split())
arr[a-1].append([b-1,i])
arr[b-1].append([a-1,i])
que=deque([0])
ans=[0]*(n-1)
par=[0]*n
par[0]=-1
while que:
x=que.popleft()
p=par[x]
color=1
for tup in arr[x]:
if p==color:
color+=1
if ans[tup[1]]==0:
ans[tup[1]]=color
par[tup[0]]=color
color+=1
que.append(tup[0])
print(max(ans))
print(*ans,sep='\n')
| 0 | null | 68,555,807,117,760 | 65 | 272 |
print(1-int(input()))
|
if(int(input())== 1):
print(0)
exit()
print(1)
| 1 | 2,903,078,900,330 | null | 76 | 76 |
n, m = map(int, input().split())
a = list(map(int, input().split()))
for time in a:
n -= time
if n<0:
n = -1
break
print(n)
|
def insert(cnt, data):
T[str(data)] = cnt
cnt = 0
T = {}
n = int(input())
for i in range(n):
Order, data_S = input().split()
if Order[0] =="i":
insert(cnt, data_S)
cnt +=1
else:
if str(data_S) in T:
print("yes")
else:
print("no")
| 0 | null | 16,163,578,710,620 | 168 | 23 |
N=int(input())
st=[]
for i in range(N):
s,t=input().split()
t=int(t)
st.append((s,t))
X=input()
#st = sorted(st,key=lambda x:x[1])
flag=0
ans=0
for i in range(N):
if flag: ans += st[i][1]
if st[i][0]==X:flag=1
print(ans)
|
n = int(input())
s = []
t = []
for i in range(n):
s_, t_ = map(str, input().split())
s.append(s_)
t.append(int(t_))
x = input()
for i in range(n):
if s[i] == x:
break
ans = 0
for j in range(n-1, i, -1):
ans += t[j]
print(ans)
| 1 | 97,158,643,336,208 | null | 243 | 243 |
X,Y = map(int,input().split())
M = max(X,Y)
m = min(X,Y)
mod = 10 ** 9 + 7
con = (X + Y) // 3
dif = M - m
n = (con - dif) // 2
if (X + Y) % 3 != 0 or n < 0:
print(0)
else:
def comb(n, r):
n += 1
over = 1
under = 1
for i in range(1,r + 1):
over = over * (n - i) % mod
under = under * i % mod
#powでunder ** (mod - 2) % modを実現、逆元を求めている
return over * pow(under,mod - 2,mod) % mod
ans = comb(con,n)
print(ans)
|
class UnionFind:
def __init__(self, num):
self.parent = [-1] * num
def find(self, node):
if self.parent[node] < 0:
return node
self.parent[node] = self.find(self.parent[node])
return self.parent[node]
def union(self, node1, node2):
node1 = self.find(node1)
node2 = self.find(node2)
if node1 == node2:
return
if self.parent[node1] > self.parent[node2]:
node1, node2 = node2, node1
self.parent[node1] += self.parent[node2]
self.parent[node2] = node1
return
def same(self, node1, node2):
return self.find(node1) == self.find(node2)
def size(self, x):
return -self.parent[self.find(x)]
def roots(self):
return [i for i, x in enumerate(self.parent) if x < 0]
def group_count(self):
return len(self.roots())
n, m = map(int, input().split())
uf = UnionFind(n)
for i in range(m):
a, b = map(int, input().split())
a -= 1
b -= 1
uf.union(a, b)
print(uf.group_count() - 1)
| 0 | null | 76,096,958,451,680 | 281 | 70 |
import sys
n = sys.stdin.readlines()
for i in n:
a = [int(x) for x in i.split()]
if a[0] == 0 and a[1] == 0:
break
print(*sorted(a))
|
while(True):
a, b = map(int, input().split())
if(a == b == 0):
break
print('{} {}'.format(min(a, b), max(a, b)))
| 1 | 518,054,110,618 | null | 43 | 43 |
n = int(input())
ans = float('inf')
for i in range(1, int(n**0.5)+1):
if n%i == 0:
j = n//i
ans = min(ans, i-1+j-1)
print(ans)
|
import sys
sys.setrecursionlimit(10**7)
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 LI2(): return list(map(int,sys.stdin.readline().rstrip())) #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split()) #空白あり
def LS2(): return list(sys.stdin.readline().rstrip()) #空白なし
N = I()
for i in range(int(N**.5),0,-1):
if N % i == 0:
print(i+(N//i)-2)
exit()
| 1 | 161,977,682,623,810 | null | 288 | 288 |
import sys
sys.setrecursionlimit(1000000)
input = sys.stdin.readline
from bisect import *
from collections import *
from heapq import *
INF = 500000
mod = 10**9+7
l = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
K = int(input())-1
print(l[K])
# a, b, c = input(), input(), input()
# la, lb, lc = len(a), len(b), len(c)
# for i in range(1, min(la, lb)):
# S = ''
# for j in range(i):
# if a[la-i+j] == b[j]:
# S += b[j]
# continue
# elif a[la-i+j] == '?':
# S += b[j]
# elif b[j] == '?':
# S += c[j]
|
list = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
num = int(input()) - 1
print(list[num])
| 1 | 50,012,004,691,110 | null | 195 | 195 |
from itertools import product
def solve():
N = int(input())
graph = {i: [] for i in range(1, N+1)}
for i in range(N):
A = int(input())
for _ in range(A):
x,y = map(int, input().split())
graph[i+1].append((x,y))
def dfs(nodes):
searched = [-1] * N
stack = [i+1 for i,node in enumerate(nodes) if node == 1]
ret_len = len(stack)
while stack:
node = stack.pop(0)
for x,y in graph[node]:
if nodes[x-1] != y: return -1
if searched[x-1] == -1:
searched[x-1] = y
if y == 1: stack.append(x)
# print(nodes, searched)
return ret_len
ret = 0
for nodes in product([0,1], repeat=N):
ret = max(ret, dfs(nodes))
print(ret)
solve()
|
N = int(input())
ls1 = []
for i in range(N):
A = int(input())
ls2 = []
for i in range(A):
x, y = map(int,input().split())
ls2.append([x,y])
ls1.append(ls2)
ans = 0
f = True
for i in range(2**N):
for j in range(N):
if (i>>j&1):
f = True
for k in ls1[j]:
if (i >>(k[0] - 1) &1) == (k[1]&1):
continue
else:
f = False
break
if f == False:
break
if f:
ans = max(ans,bin(i).count('1'))
print(ans)
| 1 | 121,471,778,131,040 | null | 262 | 262 |
a, b, c, d = map(int, input().split())
x = [a, b]
y = [c, d]
ans = -1e30
for i in x:
for j in y:
ans = max(ans, i*j)
print(ans)
|
import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []
def main():
n = i_input()
xs = i_list()
min_list = []
for i in range(1, 101):
tmp = 0
for x in xs:
tmp += (i - x)**2
min_list.append(tmp)
print(min(min_list))
if __name__ == '__main__':
main()
| 0 | null | 34,131,215,630,002 | 77 | 213 |
import math
num_of_even, num_of_odd = map(int, input().split())
def combinations_count(n, r):
if n < 2:
return 0
else:
return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
if num_of_even == 1 and num_of_odd == 1:
print('0')
else:
print(combinations_count(num_of_odd, 2) + combinations_count(num_of_even, 2))
|
x, y = map(int, input().split())
if y % 2 != 0:
print("No")
else:
if 2 * x <= y <= 4 * x:
print("Yes")
else:
print("No")
| 0 | null | 29,868,742,003,566 | 189 | 127 |
# -*- coding: utf-8 -*-
A = [0] * 3
for i in range(3):
A[i] = list(map(int, input().split()))
N = int(input())
b = []
for i in range(N):
b.append(int(input()))
for b_i in b:
for i in range(3):
for j in range(3):
if A[i][j] == b_i:
A[i][j] = 0
if A[0][0] == 0 and A[0][1] == 0 and A[0][2] == 0:
print('Yes')
elif A[1][0] == 0 and A[1][1] == 0 and A[1][2] == 0:
print('Yes')
elif A[2][0] == 0 and A[2][1] == 0 and A[2][2] == 0:
print('Yes')
elif A[0][0] == 0 and A[1][0] == 0 and A[2][0] == 0:
print('Yes')
elif A[0][1] == 0 and A[1][1] == 0 and A[2][1] == 0:
print('Yes')
elif A[0][2] == 0 and A[1][2] == 0 and A[2][2] == 0:
print('Yes')
elif A[0][0] == 0 and A[1][1] == 0 and A[2][2] == 0:
print('Yes')
elif A[0][2] == 0 and A[1][1] == 0 and A[2][0] == 0:
print('Yes')
else:
print('No')
|
def encode(s):
val = 0
min_val = 0
for c in s:
if c == '(':
val += 1
else:
val -= 1
min_val = min(min_val, val)
return [min_val, val]
def check(s):
h = 0
for p in s:
b = h + p[0]
if b < 0:
return False
h += p[1]
return True
n = int(input())
ls = []
rs = []
l_total = 0
r_total = 0
for _ in range(n):
si = encode(input())
if si[1] > 0:
ls.append(si)
l_total += si[1]
else:
si[0] -= si[1]
si[1] *= -1
rs.append(si)
r_total += si[1]
list.sort(ls, reverse=True)
list.sort(rs, reverse=True)
if check(ls) and check(rs) and l_total == r_total:
print("Yes")
else:
print("No")
| 0 | null | 41,901,833,470,982 | 207 | 152 |
a=list(map(int,input().split()))
if sum(a)>=22:
print("bust")
elif sum(a)<=21:
print("win")
|
def resolve():
A = sum(map(int, input().split()))
print("bust" if A >= 22 else "win")
if '__main__' == __name__:
resolve()
| 1 | 118,949,094,266,692 | null | 260 | 260 |
while True:
try:
a, b = list(map(int, input().split()))
print(len(str(a + b)))
except EOFError:
break
except ValueError:
break
|
A=list(map(int,input().split()))
print(A[0]*A[1])
| 0 | null | 7,906,814,421,770 | 3 | 133 |
n = int(input())
dic = set()
for i in range(n):
x = input()
if 'insert' in x:
dic.add(x.strip('insert '))
else :
if x.strip('find ') in dic:
print('yes')
else :
print('no')
|
a = input()
a = [int(n) for n in a.split()]
print(a[0] * a[1])
| 0 | null | 7,953,457,950,230 | 23 | 133 |
import math
a, b, c = map(int, input().split())
c = math.radians(c)
S = 1 / 2 * (a * b * math.sin(c))
c2 = math.sqrt(a*a + b*b - 2*a*b*math.cos(c))
H = a + b + c2
h = b * math.sin(c)
print(S,H,h)
|
import math
a,b,C = map(float,input().split())
S = 1/2*a*b*math.sin(C*math.pi/180)
c = math.sqrt(a**2+b**2-2*a*b*math.cos(C*math.pi/180))
L = a+b+c
h = 2*S/a
print(format(S,'.8f'))
print(format(L,'.8f'))
print(format(h,'.8f'))
| 1 | 167,773,273,190 | null | 30 | 30 |
N = int(input())
table = []
for i in range(N):
X,L = map(int,input().split())
table.append((X-L,X+L))
table = sorted(table, key=lambda x:x[1])
cur = 0
l_cur, r_cur = table[0]
ans = N
for i in range(1,N):
l,r = table[i]
if r_cur > l:
ans -= 1
else:
l_cur, r_cur = l,r
print(ans)
|
def solve():
N, M = map(int, input().split())
print(int(N*(N-1)/2 + M*(M-1)/2))
if __name__ == '__main__':
solve()
| 0 | null | 68,051,630,622,998 | 237 | 189 |
import itertools
n=int(input())
p=[int(i) for i in input().split()]
q=[int(i) for i in input().split()]
t=[int(i) for i in range(1,n+1)]
a=b=0
for i,j in enumerate(list(itertools.permutations(t,n))):
w=[1,1]
for x,y in enumerate(j):
if p[x]!=y: w[0]=0
if q[x]!=y: w[1]=0
if w[0]: a=i
if w[1]: b=i
print(abs(a-b))
|
# author: Taichicchi
# created: 12.09.2020 17:49:24
from itertools import permutations
import sys
N = int(input())
P = int("".join(input().split()))
Q = int("".join(input().split()))
perm = permutations([str(i) for i in range(1, N + 1)], N)
ls = []
for i in perm:
ls.append(int("".join(i)))
print(abs(ls.index(P) - ls.index(Q)))
| 1 | 100,450,152,118,962 | null | 246 | 246 |
import heapq
def main():
N, M = list(map(int, input().split(' ')))
S = input()
# 最短手数のdpテーブルを作る
T = S[::-1] # ゴールから逆順にたどる(最後に逆にする)
dp = [-1] * (N + 1)
que = [0] * M
for i, t in enumerate(T):
if i == 0:
dp[0] = 0
continue
if len(que) == 0:
print(-1)
return
index = heapq.heappop(que)
if t == '1':
continue
dp[i] = 1 + dp[index]
while len(que) < M:
heapq.heappush(que, i)
dp.reverse()
# 細切れに進んでいく
path = list()
target = dp[0] - 1
cnt = 0
for i in range(N + 1):
if dp[i] != target:
cnt += 1
else:
path.append(cnt)
cnt = 1
target -= 1
print(' '.join(map(str, path)))
if __name__ == '__main__':
main()
|
n=int(input())
a=list(map(int,input().split()))
d={}
for v in a:
if v not in d:
d[v]=0
d[v]+=1
for x in range(n):
if x+1 not in d:
print(0)
else:
print(d[x+1])
| 0 | null | 86,085,147,771,600 | 274 | 169 |
#B問題
#ABSの定石使うとなぜかRE
#今回はPython解説サイト参照
#①入力を文字列で受け取る→②一文字ずつ整数に変換して、forループで回しながら足し算する
N = input()
cur = 0
for i in N:
cur += int(i)
if cur % 9 == 0:
print("Yes")
else:
print("No")
|
n = input()
if (sum(map(int, n))) % 9 == 0:
print("Yes")
else:
print("No")
| 1 | 4,414,060,974,700 | null | 87 | 87 |
w = input()
if w[2] == w[3] and w[4] == w[5]:
print("Yes")
else: print("No")
|
word = input()
print('Yes' if word[2] == word[3] and word[4] == word[5] else 'No')
| 1 | 41,891,772,305,202 | null | 184 | 184 |
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")
n = int(input())
d = list(map(int, input().split()))
M = 998244353
from collections import Counter
c = Counter(d)
if d[0]!=0 or c[0]>=2:
ans = 0
else:
m = max(c.keys())
ans = 1
for i in range(1,m+1):
if i not in c:
ans = 0
break
ans *= pow(c[i-1], c[i], M)
ans %= M
print(ans%M)
|
# -*- coding: utf-8 -*-
import sys
# from collections import defaultdict, deque
# from math import sqrt, gcd, factorial, tan, pi, sin, cos
def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline()[:-1]
MOD = 998244353
def solve():
n = int(input())
d = [int(x) for x in input().split()]
f = [0] * n
for e in d:
f[e] += 1
ok = f[0] == 1 and d[0] == 0
z = 0
ans = 1
for i in range(1, n):
if (f[i] == 0): z = 1
if (z and f[i]): ok = 0
ans *= pow(f[i-1], f[i], MOD)
ans %= MOD
print(ans * ok)
t = 1
# t = int(input())
for case in range(1,t+1):
ans = solve()
"""
1 -> (1, 2) -> (2 ** 3, 3) -> (3, 1)
"""
| 1 | 154,786,539,660,302 | null | 284 | 284 |
A,B=map(int,input().split())
for x in range(1,1001):
if A==int(x*0.08) and B==int(x*0.1):
print(x)
break
elif x==1000:
print(-1)
|
a,b=map(int,input().split())
a_s=a/0.08
a_t=(a+1)/0.08
b_s=b/0.1
b_t=(b+1)/0.1
a0=int(a_s)
a1=int(a_t)
b0=int(b_s)
b1=int(b_t)
if (a_s-a0)>0:
a0+=1
if (a_t-a1)<=0:
a1-=1
if (b_s-b0)>0:
b0+=1
if (b_t-b1)<=0:
b1-=1
lista=[i for i in range(a0,a1+1)]
listb=[i for i in range(b0,b1+1)]
ansl=[]
for j in range(len(lista)):
if lista[j] in listb:
ansl.append(lista[j])
if not ansl==[]:
ans=ansl[0]
print(ans)
else:
print(-1)
| 1 | 56,611,487,943,608 | null | 203 | 203 |
H,W=map(int,input().split())
import sys
N=1
if W==1 or H==1:
print(1)
sys.exit()
if (W-1)%2==0:
N+=(W-1)//2
N+=(W-1)//2
elif (W-1)%2==1:
N+=(W-2)//2
N+=W//2
if H%2==0:
NN=N*(H//2)
elif H%2==1:
if (W-1)%2==0:
NN=N*((H-1)//2)+(W-1)//2+1
elif (W-1)%2==1:
NN=N*((H-1)//2)+(W-2)//2+1
print(NN)
|
import math
import sys
from collections import deque
import heapq
import copy
import itertools
from itertools import permutations
from itertools import combinations
import bisect
def mi() : return map(int,sys.stdin.readline().split())
def ii() : return int(sys.stdin.readline().rstrip())
def i() : return sys.stdin.readline().rstrip()
a=ii()
l=[]
ans=0
for i in range(a):
s=ii()
h=[list(mi()) for _ in range(s)]
l.append(h)
for k in range(2**a):
lst=[]
for p in range(a):
if k>>p & 1:
lst.append(p)
t=True
for aa in lst:
for m in l[aa]:
if m[1]==0:
if m[0]-1 in lst:
t=False
break
else:
if not m[0]-1 in lst:
t=False
break
if t:
ans=max(ans,len(lst))
print(ans)
| 0 | null | 86,457,830,080,750 | 196 | 262 |
h,w,K = map(int,input().split())
sij = [input() for j in range(h)]
num = 2**(h-1)
hon = 10**18
for i in range(num):
tmp = format(i,'0'+str(h)+'b')
#print(tmp,tmp,tmp,tmp)
yoko_hon = sum(list(map(int,tmp)))+1
blk = [0]*(yoko_hon)
#blk_num = 0
tmp_col = 0
tmp_hon = yoko_hon-1
flag = True
ok_flag = True
for j in range(w):
tmp_blk = [0]*(yoko_hon)
blk_num = 0
for k in range(h):
if tmp[k] == str(1):
blk_num += 1
#print(blk_num,tmp_blk)
tmp_blk[blk_num] += int(sij[k][j])
#print(blk[blk_num] , tmp_blk[blk_num],k)
if blk[blk_num] + tmp_blk[blk_num] > K:
flag = False
continue
#print(ok_flag,flag,tmp_blk,blk)
if flag == False:
for l in range(yoko_hon):
if tmp_blk[l] > K:
ok_flag = False
break
blk[l] = tmp_blk[l]
tmp_hon += 1
tmp_col = 0
flag = True
else:
for l in range(yoko_hon):
blk[l] += tmp_blk[l]
tmp_col += 1
if ok_flag == False:
break
#print(ok_flag)
if ok_flag == False:
ok_flag = True
continue
#exit()
hon = min(tmp_hon,hon)
print(hon)
|
def main():
n = input()
m = int(n[-1])
hon = [2, 4, 5, 7, 9]
pon = [0, 1, 6, 8]
if m in hon:
print('hon')
elif m in pon:
print('pon')
else:
print('bon')
if __name__ == '__main__':
main()
| 0 | null | 33,856,219,146,208 | 193 | 142 |
import sys
from collections import defaultdict
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, *A = map(int, read().split())
memo = defaultdict(int) # 0で初期化する
answer = 0
for idx, x in enumerate(A, 1): # indexを1から始める
# i<jを満たしながらansを計算
# idx = 1の時はansが増えることはない
# idx = 2の時は、idx = 1の時の結果がmemoに記録されている
# idx = 3の時は、idx=1,2の結果が記録されている
# 以下同様
answer += memo[idx - x]
memo[idx + x] += 1
print(answer)
|
c, flag = 0, 0
for _ in range(int(input())):
d1, d2 = map(int, input().split())
if d1 == d2:
c += 1
else:
c = 0
if c >= 3:
flag = 1
if flag:
print("Yes")
else:
print("No")
| 0 | null | 14,206,834,197,532 | 157 | 72 |
sum = 0
for i in map(int,input().split()):
sum += i
print(15-sum)
|
S = input()
T = input()
n = 0
for s, t in zip(S, T):
if s != t:
n += 1
print(n)
exit()
| 0 | null | 11,968,306,520,712 | 126 | 116 |
import math
def rot60(s,t):
v=t-s
a=1/2+complex(0,(math.sqrt(3)/2))
return v*a+s
def puts(p):
x=p.real
y=p.imag
print('{real} {imaginary}'.format(real=x,imaginary=y))
def koch(p1, p2, n):
if n==0:
return
s=(p2-p1)*(1/3)+p1
t=(p2-p1)*(2/3)+p1
u=rot60(s,t)
koch(p1,s,n-1)
puts(s)
koch(s,u,n-1)
puts(u)
koch(u,t,n-1)
puts(t)
koch(t,p2,n-1)
n=int(input())
s=complex(0,0)
t=complex(100,0)
puts(s)
koch(s,t,n)
puts(t)
|
from collections import defaultdict
N, K = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
S = [0] * (N+1)
T = [0] * (N+1)
for i in range(N):
S[i+1] = (S[i] + A[i])%K
T[i+1] = (S[i+1] - (i+1))%K
rest = defaultdict(int)
for i in range(N+1):
if i < K:
ans += rest[T[i]]
rest[T[i]] += 1
else:
rest[T[i-K]] -= 1
ans += rest[T[i]]
rest[T[i]] += 1
print(ans)
| 0 | null | 68,496,650,026,880 | 27 | 273 |
S = input() *2
N = input()
if N in S:
print("Yes")
else:
print("No")
|
N=input()
N=N+N
S=input()
if S in N:
print("Yes")
else:
print("No")
| 1 | 1,754,675,950,980 | null | 64 | 64 |
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)
|
L,R,d = list(map(int, input().split()))
count = 0
for i in range(L,R+1):
count += (i%d == 0)
print(count)
| 1 | 7,537,951,688,032 | null | 104 | 104 |
'''
Date : 2020-08-09 00:46:52
Author : ssyze
Description :
'''
n, k = map(int, input().split())
a = list(map(int, input().split()))
def check(m):
sum = 0
for i in range(len(a)):
sum += (a[i] - 1) // m
if sum > k:
return False
else:
return True
l = 1
r = 10**9
ans = 10**9
while l < r:
mid = (l + r) // 2
if check(mid) == 1:
r = mid
ans = mid
else:
l = mid + 1
print(ans)
|
def check(x, A, K):
import math
sumA = 0
for a in A:
if a > x:
sumA += math.ceil(a / x) - 1
if sumA <= K:
return True
else:
return False
def resolve():
_, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
ok = max(A) # maxVal when minimize
ng = -1 # maxVal when maximize
while abs(ok - ng) > 1:
mid = (ok + ng) // 2
if mid > 0 and check(mid, A, K):
ok = mid
else:
ng = mid
print(ok)
resolve()
| 1 | 6,560,777,807,080 | null | 99 | 99 |
A,B,N = map(int,input().split())
if B<=N:
print(int((A*(B-1))/B)-(A*int((B-1)/B)))
else:
print(int((A*N)/B)-(A*int(N/B)))
|
import sys
from collections import deque
import copy
import math
def main():
A, B, N = map(int, input().split())
if B - 1 <= N:
x = B - 1
else:
x = N
ans = math.floor(A * x / B) - A * math.floor(x / B)
print(ans)
if __name__ == '__main__':
main()
| 1 | 28,171,435,951,868 | null | 161 | 161 |
A,B = map(int,input().split())
print(A*B,2*(A+B))
|
X, Y, A ,B, C = list(map(int, input().split()))
P = sorted(list(map(int, input().split())), reverse = True)[:X]
Q = sorted(list(map(int, input().split())), reverse = True)[:Y]
R = sorted(list(map(int, input().split())), reverse = True)
ans = 0
for i in range(C):
if(len(P) == 0):
if(len(Q) == 0):
break
elif(Q[-1] < R[i]):
Q.pop()
ans += R[i]
else:
break
elif(len(Q) == 0):
if(P[-1] < R[i]):
P.pop()
ans += R[i]
else:
break
else:
if(P[-1] < Q[-1] and P[-1] < R[i]):
P.pop()
ans += R[i]
elif(Q[-1] <= P[-1] and Q[-1] < R[i]):
Q.pop()
ans += R[i]
else:
break
ans += sum(P) + sum(Q)
print(ans)
| 0 | null | 22,650,906,260,290 | 36 | 188 |
#def area###############################
def pr(s,a,b):
print s[int(a):int(b)+1]
def rv(s,a,b):
# a="abcdefghijklmn"len=14
# ra="nmlkjihgfedcba"
# edc=9,10,11
# ans="abedcfghijklmn"
# reverse 2 4
# 14-b-1,14-a-1
# "
tmp=s[::-1]
news=s[:int(a)]+tmp[len(tmp)-int(b)-1:len(tmp)-int(a)]+s[int(b)+1:]
return news
def rp(s,a,b,p):
news=s[:int(a)]+p+s[int(b)+1:]
return news
########################################
I=raw_input()
n=int(input())
for i in range(n):
k=raw_input().split(" ")
if k[0]=="replace":
I=rp(I,k[1],k[2],k[3])
elif k[0]=="reverse":
I=rv(I,k[1],k[2])
elif k[0]=="print":
pr(I,k[1],k[2])
|
import sys
input = sys.stdin.readline
def main():
text = input().rstrip()
n = int(input().rstrip())
for i in range(n):
s = input().split()
if s[0] == "print":
print(text[int(s[1]):int(s[2])+1])
elif s[0] == "replace":
new_text = ""
new_text += text[:int(s[1])]
new_text += s[3]
new_text += text[int(s[2])+1:]
text = new_text
elif s[0] == "reverse":
new_text = ""
new_text += text[:int(s[1])]
for i in range(int(s[2])-int(s[1]) + 1):
new_text += text[int(s[2]) - i]
new_text += text[int(s[2])+1:]
text = new_text
if __name__ == "__main__":
main()
| 1 | 2,096,056,681,960 | null | 68 | 68 |
inp=input().split()
n=int(inp[0])
m=int(inp[1])
arr=[]
pares=0
impares=0
count=1
nums=0
def binary(arr, menor, mayor, x):
if mayor >= menor:
med = (mayor + menor) // 2
if arr[med] == x:
return med
elif arr[med] > x:
return binary(arr, menor, med - 1, x)
else:
return binary(arr, med + 1, mayor, x)
else:
return -1
while len(arr)<(m+n):
if count%2==0 and pares<n:
arr.append(count)
pares+=1
elif count%2!=0 and impares<m:
arr.append(count)
impares+=1
count+=1
sums=[]
for x in arr:
for y in arr:
if(x+y)%2==0 and binary(sums, 0, len(sums)-1, [y,x])==-1 and x!=y:
sums.append([x,y])
nums+=1
print(nums)
|
a,b=map(int,input().split())
c = a * (a-1)/2
d = b*(b-1)/2
res = int(c+d)
print(res)
| 1 | 45,441,300,940,542 | null | 189 | 189 |
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))
|
n = int( raw_input( ) )
minv = int( raw_input( ) )
maxv = 0 - 1000000000
for j in range( n-1 ):
num = int( raw_input( ) )
diff = num - minv
if maxv < diff:
maxv = diff
if num < minv:
minv = num
print maxv
| 0 | null | 78,189,275,159,782 | 285 | 13 |
K = int(input())
# 数列を漸化式と見て、順に余りを計算する
# 余りは循環するので、高々K項まで計算すれば、少なくとも1つの循環を抽出できるので十分
# あとは抽出した余りの列において、0が含まれるか、含まれる場合は何番目かを求める
i, a = 0, 0
l = []
for i in range(K):
a = (10*a + 7) % K
l.append(a)
j, ans = 0, -1
for j in range(K):
if l[j] == 0:
ans = j + 1
break
print(ans)
|
a=1
while True:
i = input()
if i==0:
break
else :
print "Case %d: %d"%(a,i)
a+=1
| 0 | null | 3,322,256,685,988 | 97 | 42 |
from collections import defaultdict
N, K = [int(i) for i in input().split()]
mod = 10 ** 9 + 7
dd = defaultdict(int)
ans = 0
for i in range(K, 0, -1):
dd[i] = pow(K // i, N, mod)
for temp in range(i * 2, K + 1, i):
dd[i] -= dd[temp]
ans += dd[i] * i
ans %= mod
print(ans)
|
mod=10**9+7
n,k=map(int,input().split())
l=[0]*(k+1)
for i in range(k):
num=(k-i)
if k//num==1:
l[num]=1
else:
ret=pow(k//num,n,mod)
for j in range(2,k//num+1):
ret-=l[num*j]
ret%=mod
l[num]=(ret)%mod
ans=0
for i in range(k+1):
ans+=(l[i]*i)%mod
print(ans%mod)
| 1 | 36,908,436,513,728 | null | 176 | 176 |
def main():
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)
s = readline().rstrip().decode()
check = {'ABC': 'ARC', 'ARC': 'ABC'}
print(check[s])
if __name__ == '__main__':
main()
|
import sys
while True:
num = input()
if int(num) == 0:
break
sum = 0
for i in range(len(num)):
sum += int(num[i])
print(sum)
| 0 | null | 12,800,826,429,070 | 153 | 62 |
n=int(input())
x=list(map(int,input().split()))
ans=10**9
for i in range(1,101):
tmp=0
for j in range(n):
tmp+=(i-x[j])*(i-x[j])
ans=min(ans,tmp)
print(ans)
|
import math
a = int(input())
answer = 100000000
b = list(map(int,input().split()))
for i in range(1,101):
cost = 0
for j in b:
cost+=(j-i)**2
answer = min(answer,cost)
print(answer)
| 1 | 65,312,072,683,398 | null | 213 | 213 |
N, M = map(int, input().split())
parent = [-1] * N
def get_group_root(x):
if parent[x] < 0:
return x
return get_group_root(parent[x])
for i in range(0, M):
A, B = map(int, input().split())
A -= 1
B -= 1
groupA = get_group_root(A)
groupB = get_group_root(B)
if groupA == groupB:
continue
elif parent[groupA] < parent[groupB]:
parent[groupB] = A
parent[groupA] -= 1
else:
parent[groupA] = B
parent[groupB] -= 1
def check(x):
return x < 0
print(sum(list(map(check, parent))) - 1)
|
class AlgUnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * n #各要素の親要素の番号を格納するリスト 要素が根(ルート)の場合は-(そのグループの要素数)を格納する
def find(self, x): #要素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が属するグループと要素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): #要素xが属するグループのサイズ(要素数)を返す
return -self.parents[self.find(x)]
def same(self, x, y): #要素x, yが同じグループに属するかどうかを返す
return self.find(x) == self.find(y)
def members(self, x): #要素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): #print()での表示用 ルート要素: [そのグループに含まれる要素のリスト]を文字列で返す
return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
if __name__ == "__main__":
N, M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(M)]
for j in range(M):
A[j][0] -= 1
A[j][1] -= 1
uf = AlgUnionFind(N)
for i in range(M):
uf.union(A[i][0], A[i][1])
count = uf.group_count() - 1
print(count)
| 1 | 2,262,416,251,550 | null | 70 | 70 |
import collections
import sys
S1 = collections.deque()
S2 = collections.deque()
S3 = collections.deque()
for i, j in enumerate(sys.stdin.readline()):
if j == '\\':
S1.append(i)
elif j == '/':
if S1:
left_edge = S1.pop()
new_puddle = i - left_edge
while S2 and (S2[-1] > left_edge):
if S2[-1] > left_edge:
S2.pop()
new_puddle += S3.pop()
S2.append(left_edge)
S3.append(new_puddle)
else:
pass
print(sum(S3))
print(len(S3), *S3)
|
import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
class Edge:
def __init__(self, to, id):
self.to = to
self.id = id
N = int(input())
graph = {}
ans = [0] * (N-1)
def dfs(v, c=-1, p=-1):
global graph, ans
k = 1
for edge in graph[v]:
nv = edge.to
if nv == p:continue
if k == c:k += 1
ans[edge.id] = k
dfs(nv, k, v)
k += 1
def main():
global N, graph, ans
for i in range(N):
graph[i] = set()
for i in range(N-1):
a, b = map(int, input().split())
graph[a-1].add(Edge(b-1, i))
graph[b-1].add(Edge(a-1, i))
color_count = 0
for i in range(N):
color_count = max(color_count, len(graph[i]))
dfs(0, 0, -1)
print(color_count)
for x in ans:
print(x)
if __name__ == "__main__":
main()
| 0 | null | 67,850,234,821,302 | 21 | 272 |
S = input()
N = len(S)
S1 = S[:(N - 1) // 2]
S2 = S[(N + 3) // 2 - 1:]
def check(s):
#print(s)
i = 0
j = len(s) - 1
while j > i:
if s[j] != s[i]:
return False
j -= 1
i += 1
return True
if check(S) and check(S1) and check(S2):
print("Yes")
else:
print("No")
|
a, b, m = map(int, input().split())
pa = list(map(int, input().split()))
pb = list(map(int, input().split()))
ans = min(pa) + min(pb)
for _ in range(m):
x, y, c = map(int, input().split())
x -= 1
y -= 1
ans = min(ans, pa[x] + pb[y] - c)
print(ans)
| 0 | null | 50,072,201,281,062 | 190 | 200 |
r = int(input())
print(r * 6.28)
|
class UnionFind:
par = None
def __init__(self, n):
self.par = [0] * n
def root(self, x):
if(self.par[x] == 0):
return x
else:
self.par[x] = self.root(self.par[x])
return self.root(self.par[x])
def unite(self, x, y):
if(self.root(x) != self.root(y)):
self.par[self.root(x)] = self.root(y)
def same(self, x, y):
return self.root(x) == self.root(y)
N, M = list(map(int, input().split()))
UF = UnionFind(N)
for i in range(M):
A, B = list(map(int, input().split()))
UF.unite(A - 1, B - 1)
ans = 0
for i in range(N):
if(UF.par[i] == 0):
ans += 1
print(ans - 1)
| 0 | null | 16,943,735,031,538 | 167 | 70 |
# coding: utf-8
# 標準入力 <int>
K = int(input())
k = "1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51"
kl = k.split(", ")
print(kl[K-1])
|
import sys
sys.setrecursionlimit(1000000)
input = sys.stdin.readline
from bisect import *
from collections import *
from heapq import *
INF = 500000
mod = 10**9+7
l = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
K = int(input())-1
print(l[K])
# a, b, c = input(), input(), input()
# la, lb, lc = len(a), len(b), len(c)
# for i in range(1, min(la, lb)):
# S = ''
# for j in range(i):
# if a[la-i+j] == b[j]:
# S += b[j]
# continue
# elif a[la-i+j] == '?':
# S += b[j]
# elif b[j] == '?':
# S += c[j]
| 1 | 49,870,957,467,430 | null | 195 | 195 |
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from collections import deque
from functools import lru_cache
import bisect
import re
import queue
import copy
import decimal
class Scanner():
@staticmethod
def int():
return int(sys.stdin.readline().rstrip())
@staticmethod
def string():
return sys.stdin.readline().rstrip()
@staticmethod
def map_int():
return [int(x) for x in Scanner.string().split()]
@staticmethod
def string_list(n):
return [Scanner.string() for i in range(n)]
@staticmethod
def int_list_list(n):
return [Scanner.map_int() for i in range(n)]
@staticmethod
def int_cols_list(n):
return [Scanner.int() for i in range(n)]
def solve():
N, K = Scanner.map_int()
A = Scanner.map_int()
for i in range(K, N):
if A[i - K] < A[i]:
print('Yes')
else:
print('No')
def main():
# sys.setrecursionlimit(1000000)
# sys.stdin = open("sample.txt")
solve()
if __name__ == "__main__":
main()
|
def main():
N, K = map(int, input().split())
*A, = map(int, input().split())
ans = []
for i in range(K, N):
cond = A[i] > A[i - K]
ans.append('Yes' if cond else 'No')
print(*ans, sep='\n')
if __name__ == '__main__':
main()
| 1 | 7,172,333,913,206 | null | 102 | 102 |
n = int(input())
num_list = input().split()
def bubbleSort(num_list, n):
flag = 1
count = 0
while flag:
flag = 0
for j in range(n-1, 0, -1):
if int(num_list[j]) < int(num_list[j-1]):
a = num_list[j]
b = num_list[j-1]
num_list[j] = b
num_list[j-1] = a
flag = 1
count += 1
result = ' '.join(num_list)
print(result)
print(count)
bubbleSort(num_list, n)
|
import numpy as np
import scipy as sp
import math
a, b, c = map(int, input().split())
d = a + b + c
if(d<22):
print("win")
else:
print("bust")
| 0 | null | 59,683,954,737,340 | 14 | 260 |
class queue():
def __init__(self):
self.head = 0
self.tail = 0
self.MAX = 100000
self.Q = [[0] for i in range(self.MAX)]
def is_empty(self):
return self.head == self.tail
def is_full(self):
return self.head == (self.tail + 1) % self.MAX
def enqueue(self, x):
if self.is_full():
raise ValueError("エラー(オーバーフロー)")
self.Q[self.tail] = x
if self.tail + 1 == self.MAX:
self.tail = 0
else:
self.tail += 1
def dequeue(self):
if self.is_empty():
raise ValueError("エラー(アンダーフロー)")
x = self.Q[self.head]
if self.head + 1 == self.MAX:
self.head = 0
else:
self.head += 1
return x
n, q = input().split(" ")
n = int(n)
q = int(q)
q_name = queue()
q_val = queue()
for i in range(n):
name, t = input().split(" ")
t = int(t)
q_name.enqueue(name)
q_val.enqueue(t)
results = []
end_time = 0
while True:
process_val = q_val.dequeue()
process_name = q_name.dequeue()
if process_val <= q:
end_time += process_val
results.append([process_name, end_time])
else:
end_time += q
rest_val = process_val - q
q_name.enqueue(process_name)
q_val.enqueue(rest_val)
if q_val.head == q_val.tail:
break
for v in results:
print("{} {}".format(v[0],v[1]))
|
from collections import deque
def solve(n,t):
q=deque([])
for i in range(n):
l=list(input().split())
q.append(l)
time=0
while not len(q)==0:
x=q.popleft()
if int(x[1])-t>0:
x[1]=str(int(x[1])-t)
time+=t
q.append(x)
else:
print(x[0]+" "+str(time+int(x[1])))
time+=int(x[1])
n,t=map(int,input().split())
solve(n,t)
| 1 | 42,554,524,100 | null | 19 | 19 |
s = input()
st = []
leftn = 0
def pushv(s, x):
tmp = s.pop()
tmpsum = 0
while tmp[0] != 'left':
tmpsum += tmp[1]
tmp = s.pop()
else:
tmpsum += x - tmp[1]
s.append(('v', tmpsum))
for x in range(len(s)):
if s[x] == '\\':
st.append(('left', x))
leftn += 1
elif s[x] == '/':
if leftn != 0:
pushv(st, x)
leftn -= 1
st = [x[1] for x in st if x[0] == 'v']
if len(st) != 0:
print(sum(st))
print(len(st), ' '.join([str(x) for x in st]))
else:
print(0)
print(0)
|
t=input()
s1=[]
s2=[]
pond=[]
totalsum=0
for i in range(len(t)):
if t[i]=="\\" :
s1.append(i)
elif t[i]=="/" and len(s1)!=0:
p1=s1.pop()
sum=i-p1
totalsum+=sum
while len(s2)>0:
if s2[-1]>p1:
s2.pop()
sum+=pond.pop()
else:
break
pond.append(sum)
s2.append(i)
print(totalsum)
print(len(pond),*pond)
| 1 | 60,905,099,410 | null | 21 | 21 |
n=int(input())
gou=n//2
gou+=n%2
print(gou)
|
s=input()
cnt=s.count("hi")
if cnt*2==len(s):
print("Yes")
else:
print("No")
| 0 | null | 56,371,175,667,222 | 206 | 199 |
a,b,c = map(int, input().split())
m=0
while b >=a:
if c % b == 0 :
m = m + 1
else:
pass
b = b-1
print(m)
|
i = count = 0
a = b = c = ''
line = input()
while line[i] != ' ':
a = a + line[i]
i += 1
i += 1
while line[i] != ' ':
b = b + line[i]
i += 1
i += 1
while i < len(line):
c = c + line[i]
i += 1
a = int(a)
b = int(b)
c = int(c)
while a <= b:
if c%a == 0:
count += 1
a += 1
print(count)
| 1 | 576,169,737,738 | null | 44 | 44 |
res_list = [[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]]
n = int(input())
for i in range(n):
io_rec = list(map(int, input().split(" ")))
bld_num = io_rec[0] - 1
floor_num = io_rec[1] - 1
room_num = io_rec[2] - 1
io_headcnt = io_rec[3]
res_list[bld_num][floor_num][room_num] += io_headcnt
for i in range(4):
if i:
print("#" * 20)
bld = res_list[i]
for j in bld:
floor_str = map(str, j)
print(" " + " ".join(floor_str))
|
#ITP1_6_C
A=[[[0 for k in range(10)]for j in range(3)]for i in range(4)]
format(A)
n=int(input())
for m in range(n):
b,f,r,v=map(int,input().split())
S=A[b-1][f-1][r-1]+v
A[b-1][f-1][r-1]=S
for i in range(4):
for j in range(3):
for k in range(10):
print ("",A[i][j][k],end='')
print("")
if i<3:
print("####################")
| 1 | 1,113,468,292,100 | null | 55 | 55 |
import math
r = int(input())
h = 2 * r * math.pi
print(h)
|
r = int(input())
D = 2* r
print(3.1415926535897 * D)
| 1 | 31,291,269,553,278 | null | 167 | 167 |
n, k = map(int, input().split())
ans = 0
cnt = dict(zip(range(1,k+1), [0] * k))
for i in range(1, k+1)[::-1]:
cnt[i] = pow((k // i), n, 10**9+7)
for j in range(2*i, k+1, i):
cnt[i] -= cnt[j]
ans += i * cnt[i]
print(ans % (10 ** 9 + 7))
|
import bisect
import copy
import heapq
import math
import sys
from collections import *
from itertools import accumulate, combinations, permutations, product
def input():
return sys.stdin.readline()[:-1]
def ruiseki(lst):
return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]
n,k=map(int,input().split())
a=list(map(int,input().split()))
# amari=[a[i]%k for i in range(n)]
rui=ruiseki(a)
# print(a)
# print(amari)
# print(rui)
dic=defaultdict(int)
ans=0
for i in range(n+1):
if i>=k:
dic[(rui[i-k]-(i-k))%k]-=1
ans+=dic[(rui[i]-i)%k]
dic[(rui[i]-i)%k]+=1
# print(dic)
print(ans)
| 0 | null | 87,206,364,064,890 | 176 | 273 |
# -*- coding: utf-8 -*-
def main():
A, B = map(int, input().split())
ans = A - 2 * B
if ans < 0:
ans = 0
print(ans)
if __name__ == "__main__":
main()
|
s = input()
for i in [s, s[:len(s)//2], s[len(s)//2+1:]]:
if i != i[::-1]:
print("No")
break
else:
print("Yes")
| 0 | null | 106,374,251,688,640 | 291 | 190 |
N = int(input())
locs = []
S = 0
for _ in range(N):
locs.append(list(map(int,input().split())))
for i in range(N):
for j in range(i+1,N):
dis = ((locs[i][0]-locs[j][0])**2 + (locs[i][1]-locs[j][1])**2)**(1/2)
S += dis
print(2*S/N)
|
N,K=map(int,input().split())
ans=0
while N>=K**ans:
ans+=1
print(ans)
| 0 | null | 106,576,995,947,968 | 280 | 212 |
import sys
input = sys.stdin.readline
def print_ans(X):
"""Test Case
>>> print_ans(30)
Yes
>>> print_ans(25)
No
>>> print_ans(35)
Yes
>>> print_ans(-10)
No
"""
if X >= 30:
print('Yes')
else:
print('No')
if __name__ == '__main__':
X = int(input().rstrip())
print_ans(X)
|
def main():
from sys import stdin
readline = stdin.readline
X = int(readline().rstrip())
if X >= 30:
print('Yes')
else:
print('No')
main()
| 1 | 5,657,292,070,910 | null | 95 | 95 |
#!/usr/bin/env python3
def solve(S: str):
return sum([a != b for a, b in zip(S, reversed(S))]) // 2
def main():
S = input().strip()
answer = solve(S)
print(answer)
if __name__ == "__main__":
main()
|
def solve(x):
if len(x) <= 1:
return 0
else:
return (1 if x[0] != x[-1] else 0) + solve(x[1:-1])
s = input()
print(solve(s))
| 1 | 120,412,809,968,478 | null | 261 | 261 |
from sys import setrecursionlimit, exit
setrecursionlimit(1000000000)
def main():
n = int(input())
ans = 0
for i in range(1, n + 1):
s = str(i)
if s[-1] == '0':
continue
if s[0] == s[-1]:
ans += 1
if i < 10:
continue
if s[0] == s[-1]:
ans += 2
l = len(s)
for i in range(2, l):
ans += 10 ** (i - 2) * 2
if s[0] < s[-1]:
continue
if s[0] > s[-1]:
ans += 10 ** (l - 2) * 2
elif l > 2:
dp = int(s[1])
for i in range(2, l - 1):
dp *= 10
dp += int(s[i])
ans += dp * 2
print(ans)
main()
|
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
X1 = T1 * (A1 - B1)
X2 = T2 * (A2 - B2)
D = X1 + X2
if D == 0:
print("infinity")
else:
if X1 < 0:
X1, X2 = [-X1, -X2]
else:
D = -D
if D < 0:
print("0")
else:
if X1 % D == 0:
print((X1 // D) * 2)
else:
print((X1 // D) * 2 + 1)
| 0 | null | 109,306,768,658,830 | 234 | 269 |
h = int(input())
for i in range(100):
if 2**i > h:
print(2**i - 1)
exit()
|
s,w=map(int,input().split());print('unsafe' if s<=w else 'safe')
| 0 | null | 54,574,924,034,112 | 228 | 163 |
import collections
n = int(input())
d = list(map(int,input().split()))
mod = 998244353
ans = 0
c = collections.Counter(d)
if c[0] == 1 and d[0] == 0:
ans = 1
for i in range(1,max(d)+1):
if c[i] == 0:
ans = 0
break
ans *= pow(c[i-1],c[i],mod)
ans %= mod
print(ans%mod)
|
N=int(input())
D=list(map(int,input().split()))
mod=998244353
ans=1
count=[0 for i in range(0,N)]
for i in range(0,N):
count[D[i]]+=1
able=True
if D[0]!=0 or count[0]!=1:
able=False
for i in range(0,N-1):
if count[i]==0 and count[i+1]!=0:
able=False
if able:
for i in range(0,N-1):
ans=(ans*pow(count[i],count[i+1],mod))%mod
print(ans)
else:
print(0)
| 1 | 155,271,798,819,360 | null | 284 | 284 |
n = int(input())
ai = list(map(int, input().split()))
# n=3
# ai = np.array([1, 2, 3])
# ai = ai[np.newaxis, :]
#
# ai2 = ai.T.dot(ai)
# ai2u = np.triu(ai2, k=1)
#
# s = ai2u.sum()
l = len(ai)
integ = [ai[0]] * len(ai)
for i in range(1, len(ai)):
integ[i] = integ[i-1] + ai[i]
s = 0
for j in range(l):
this_s = integ[-1] - integ[j]
s += ai[j] * this_s
ans = s % (1000000000 + 7)
print(ans)
|
def main():
N = int(input())
I = tuple(int(input()) for _ in range(N))
ans = 0
for i in I:
if is_prime(i):
ans += 1
print(ans)
def is_prime(n):
if n < 2:
return False
else:
for i in range(2, n):
if i * i > n:
break
elif n % i == 0:
return False
return True
main()
| 0 | null | 1,906,178,891,760 | 83 | 12 |
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint
sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007
t1, t2 = LI()
a1, a2 = LI()
b1, b2 = LI()
p = (a1 - b1) * t1
q = (a2 - b2) * t2
if p > 0:
p = -p
q = -q
if p + q == 0:
print('infinity')
elif p + q < 0:
print(0)
else:
print((-p // (p + q)) * 2 + bool(-p % (p + q)))
|
n, m = map(int, input().split())
matrix = [list(map(int, input().split())) for i in range(n)]
b = [int(input()) for i in range(m)]
for obj in matrix:
print(sum(obj[i] * b[i] for i in range(m)))
| 0 | null | 66,093,169,518,710 | 269 | 56 |
n=int(input())
c=input()
r_cnt=c.count('R')
w_cnt=c.count('W')
last_c='R'*r_cnt+'W'*w_cnt
num=0
for i in range(n):
if c[i]!=last_c[i]:
num+=1
print((num+2-1)//2)
|
n = int(input())
wr = input()
R_cnt = wr.count('R')
ans = wr.count('W',0,R_cnt)
print(ans)
| 1 | 6,345,404,061,600 | null | 98 | 98 |
N,A,B = map(int,input().split())
x = N //(A+B)
y = min(N%(A+B),A)
print(x * A + y)
|
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
p1 = p2 = p3 = pi = 0
for i in range(n):
p1 += abs(a[i] - b[i])
p2 += (abs(a[i] - b[i]))**2
p3 += (abs(a[i] - b[i]))**3
if pi < abs(a[i] - b[i]):
pi = abs(a[i] - b[i])
print("%f" % (p1))
print("%f" % (p2**(1/2)))
print("%f" % (p3**(1/3)))
print("%f" % (pi))
| 0 | null | 27,884,005,309,160 | 202 | 32 |
from collections import deque
N = int(input())
Q = deque([1, 2, 3, 4, 5, 6, 7, 8, 9])
for i in range(N - 1):
x = Q.popleft()
if x % 10 != 0:
y = 10 * x + x % 10 - 1
Q.append(y)
y = 10 * x + x % 10
Q.append(y)
if x % 10 != 9:
y = 10 * x + x % 10 + 1
Q.append(y)
ans = Q.popleft()
print(ans)
|
# 解説を参考に作成
from collections import deque
def solve(K):
lunlun = deque([i for i in range(1, 10)])
ans = 0
for _ in range(K):
ans = lunlun.popleft()
x = ans * 10 + ans % 10
if x % 10 != 0:
lunlun.append(x - 1)
lunlun.append(x)
if x % 10 != 9:
lunlun.append(x + 1)
print(ans)
if __name__ == '__main__':
K = int(input())
solve(K)
| 1 | 40,143,220,401,232 | null | 181 | 181 |
print(int(input())*6.2832)
|
class SegmentTree():
"""一点更新、区間取得クエリをそれぞれO(logN)で答えるデータ構造を構築する。
built(array) := arrayを初期値とするセグメント木を構築する O(N)。
update(i, val) := i番目の要素をvalに変更する。
get_val(l, r) := 区間[l, r)に対する二項演算の畳み込みの結果を返す。
"""
def __init__(self, n, op, e):
"""要素数、二項演算、単位元を引数として渡す
例) 区間最小値 SegmentTree(n, min, 10 ** 18)
区間和 SegmentTree(n, lambda a, b : a + b, 0)
"""
self.n = n
self.op = op
self.e = e
self.size = 2 ** ((n - 1).bit_length())
self.node = [self.e] * (2 * self.size)
def built(self, array):
"""arrayを初期値とするセグメント木を構築する"""
for i in range(self.n):
self.node[self.size + i] = array[i]
for i in range(self.size - 1, 0, -1):
self.node[i] = self.op(self.node[i << 1], self.node[(i << 1) + 1])
def update(self, i, val):
"""i番目の要素をvalに変更する"""
i += self.size
self.node[i] = val
while i > 1:
i >>= 1
self.node[i] = self.op(self.node[i << 1], self.node[(i << 1) + 1])
def get_val(self, l, r):
"""[l, r)の畳み込みの結果を返す"""
l, r = l + self.size, r + self.size
res_l, res_r = self.e, self.e
while l < r:
if l & 1:
res_l = self.op(res_l, self.node[l])
l += 1
if r & 1:
r -= 1
res_r = self.op(self.node[r], res_r)
l, r = l >> 1, r >> 1
return self.op(res_l, res_r)
n, m = map(int, input().split())
s = input()
INF = 10 ** 18
st = SegmentTree(n + 1, min, INF)
st.update(n, 0)
for i in range(n)[::-1]:
if s[i] == "1":
continue
tmp = st.get_val(i + 1, min(i + 1 + m, n + 1)) + 1
st.update(i, tmp)
if st.get_val(0, 1) >= INF:
print(-1)
exit()
ans = []
ind = 0
for i in range(n + 1):
if st.get_val(ind, ind + 1) - 1 == st.get_val(i, i + 1):
ans.append(i - ind)
ind = i
print(*ans)
| 0 | null | 85,021,007,518,432 | 167 | 274 |
from bisect import bisect_left as bl
H,W,K=map(int,input().split())
s=[input() for _ in range(H)]
a=[[0 for _ in range(W)] for _ in range(H)]
OK=[]
NG=[]
n=0
for h in range(H):
if "#" in s[h]:
OK.append(h)
n+=1
f=s[h].index("#")
for w in range(W):
if w>f and s[h][w]=="#":
n+=1
a[h][w]=n
else:
NG.append(h)
if len(NG)>0:
for n in NG:
if n>max(OK):
ref=max(OK)
else:
ref=OK[bl(OK,n)]
for w in range(W):
a[n][w]=a[ref][w]
for A in a:
print(*A)
|
h, w, k = map(int, input().split())
s = [input() for _ in range(h)]
ans = [[0 for _ in range(w)] for _ in range(h)]
num = 0
f = False
for i, j in enumerate(s):
tmp = j.find("#")
if tmp == -1:
if f:
ans[i] = ans[i - 1]
continue
else:
num += 1
for k in range(tmp + 1):
ans[i][k] = num
for l in range(tmp + 1, w):
if j[l] == ".":
ans[i][l] = num
else:
num += 1
ans[i][l] = num
if f == False:
f = True
for x in range(i):
ans[x] = ans[i]
for i in ans:
print(*i)
| 1 | 143,630,374,024,060 | null | 277 | 277 |
name = input()
print(name[:3])
|
s = input()
t = s[:3]
print(t)
| 1 | 14,804,719,261,210 | null | 130 | 130 |
N, K = map(int, input().split())
ans = min(N - K*(N//K), K - (N - K*(N//K)))
print(ans)
|
n = int(input())
print((n - (n+1)%2)//2)
| 0 | null | 96,474,915,827,132 | 180 | 283 |
N, X, M = map(int,input().split())
ans = 0
ls = [X%M] + [-1]*(M+1)
ls_app = [0]*(M+1)
for i in range(N):
a = ls[i]
ls_app[a] = i
ls[i+1] = pow(a,2,M)
if i+1 < N and ls_app[ls[i+1]] != 0:
b = ls[i+1]
L = ls_app[b]
R = i
len_roop = R - L + 1
p = (N - L) // len_roop
q = (N - L) % len_roop
break
else:
for i in range(N):
ans += ls[i]
print(ans)
exit()
sum_roop = 0
for i in range(L, R+1):
sum_roop += ls[i]
sum_edge = 0
for i in range(L, L+q):
sum_edge += ls[i]
for i in range(L):
ans += ls[i]
else:
ans += sum_roop * p + sum_edge
print(ans)
|
n,x,m = map(int, input().split())
be = x % m
ans = be
memo = [[0,0] for i in range(m)]
am = [be]
memo[be-1] = [1,0] #len-1
for i in range(n-1):
be = be**2 % m
if be == 0:
break
elif memo[be-1][0] == 1:
kazu = memo[be-1][1]
l = len(am) - kazu
syou = (n-i-1) // l
amari = (n-i-1)%l
sum = 0
for k in range(kazu, len(am)):
sum += am[k]
ans = ans + syou*sum
if amari > 0:
for j in range(kazu,kazu + amari):
ans += am[j]
break
else:
ans += be
am.append(be)
memo[be-1][0] = 1
memo[be-1][1] = len(am) -1
print(ans)
| 1 | 2,796,386,687,872 | null | 75 | 75 |
MOD = 1000000007
n,k = map(int,input().split())
fac = [0]*(n+1)
finv = [0]*(n+1)
inv = [0]*(n+1)
answer = 0
# テーブルを作る前処理
def COMinit(n, MOD):
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
inv[1] = 1
for i in range(2, n+1):
fac[i] = fac[i - 1] * i % MOD
inv[i] = MOD - inv[MOD%i] * (MOD // i) % MOD
finv[i] = finv[i - 1] * inv[i] % MOD
# 二項係数計算
def COM(n, k):
if (n < k): return 0
if (n < 0 or k < 0): return 0
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD
COMinit(n, MOD)
fac = tuple(fac)
finv = tuple(finv)
inv = tuple(inv)
a = n if n < k else k + 1
for i in range(a):
answer += COM(n,i) * COM(n-1, n-i-1)
# print(answer)
print(answer%MOD)
|
n, k = map(int, input().split())
mod = 1000000007
def pow(x, n):
ret = 1
while n > 0:
if (n & 1) == 1:
ret = (ret * x) % mod
x = (x * x) % mod
n //= 2
return ret
fac = [1]
inv = [1]
for i in range(n * 2):
fac.append(fac[-1] * (i + 1) % mod)
inv.append(pow(fac[-1], mod - 2))
def cmb(n, k):
if k < 0 or k > n:
return 0
return (fac[n] * inv[k] * inv[n - k]) % mod
x = max(0, n - k - 1)
ret = cmb(n * 2 - 1, n - 1)
for i in range(x):
ret = (ret - cmb(n, i + 1) * cmb(n - 1, i)) % mod
print(ret)
| 1 | 66,968,757,388,750 | null | 215 | 215 |
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def main():
a, b, m =[int(i) for i in input().split(" ")]
res = 0
for i in range(a, b + 1):
if m % i == 0:
res += 1
print(res)
if __name__ == '__main__':
main()
|
three_num = input()
three_num = [int(i) for i in three_num.split(" ")]
a = three_num[0]
b = three_num[1]
c = three_num[2]
cnt = 0
for i in range(a, b + 1):
if c % i == 0:
cnt += 1
print(cnt)
| 1 | 556,378,672,128 | null | 44 | 44 |
import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq as hpq
import itertools
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
def main():
h,w,k = map(int,ipt().split())
s = [list(map(int,list(input()))) for _ in [0]*h]
'''
sms = [[0]*w for _ in [0]*h]
for i in range(w):
for j in range(h):
sms[j][i] = sms[j-1][i]+s[j][i]
print(i)
'''
cmi = 10**18
for i in range(2**(h-1)):
pos = [0]*h
for j in range(h-1):
if (i >> j) & 1:
pos[j+1] = pos[j]+1
else:
pos[j+1] = pos[j]
cmii = pos[-1]
si = [0]*h
for jj in range(h):
si[pos[jj]] += s[jj][0]
if max(si) > k:
continue
f = True
for j in range(1,w):
if f:
for ii in range(h):
if si[pos[ii]]+s[ii][j] > k:
cmii += 1
si = [0]*h
for jj in range(h):
si[pos[jj]] += s[jj][j]
if max(si) > k:
f = False
break
else:
si[pos[ii]]+=s[ii][j]
if f and cmii < cmi:
cmi = cmii
print(cmi)
return
if __name__ == '__main__':
main()
|
H, W, K = map(int, input().split())
S = [list(input()) for _ in range(H)]
res = float('inf')
for i in range(2**(H-1)):
c = bin(i).count('1')
cnt = c
sum_l = [0] * (c+1)
j = 0
flag = True
while j < W:
tmp = sum_l.copy()
pos = 0
for k in range(H):
if S[k][j] == '1':
tmp[pos] += 1
if (i >> k) & 1:
pos += 1
if max(tmp) <= K:
sum_l = tmp.copy()
j += 1
flag = False
else:
if flag:
cnt = float('inf')
break
cnt += 1
flag = True
sum_l = [0] * (c+1)
res = min(res, cnt)
print(res)
| 1 | 48,287,818,205,990 | null | 193 | 193 |
H,W=list(map(int,input().split()))
l=[list(input()) for i in range(H)]
inf=10**9
DP=[[inf]*W for i in range(H)]
DP[0][0]=0 if l[0][0]=="." else 1
for i in range(H):
for j in range(W):
if i>0:
DP[i][j]=min(DP[i][j],DP[i-1][j]+1) if l[i-1][j] == "." and l[i][j]=="#" else min(DP[i][j],DP[i-1][j])
if j>0:
DP[i][j]=min(DP[i][j],DP[i][j-1]+1) if l[i][j-1] == "." and l[i][j]=="#" else min(DP[i][j],DP[i][j-1])
print(DP[H-1][W-1])
|
S, W = [int(n) for n in input().split()]
print('unsafe' if S <= W else 'safe')
| 0 | null | 39,317,684,240,220 | 194 | 163 |
n = int(input())
left = []
mid = []
midminus = []
right = []
L = []
R = []
for i in range(n):
s = input()
l = 0
r = 0
for x in s:
if x == '(':
l += 1
else:
if l > 0:
l -= 1
else:
r += 1
if l > 0 and r == 0:
left.append((l, r))
elif l > 0 and r > 0:
if l > r:
mid.append((r, l)) # a,b-a
else:
midminus.append((r,l))
elif l == 0 and r > 0:
right.append((l, r))
L.append(l)
R.append(r)
if sum(L) != sum(R):
print('No')
exit()
A = 0
B = 0
for x in left:
A += x[0]
for x in right:
B += x[1]
mid = sorted(mid, key=lambda x: (x[0],-x[1]))
midminus = sorted(midminus,key= lambda x:x[0]-x[1])
mid += midminus
l = A
r = 0
for a, b in mid:
if l < a:
print('No')
exit()
l -= a
l += b
print('Yes')
|
import sys
input = sys.stdin.readline
N = int(input())
op = []
ed = []
mid = [0]
dg = 10**7
for _ in range(N):
s = list(input())
n = len(s)-1
a,b,st = 0,0,0
for e in s:
if e == '(':
st += 1
elif e == ')':
if st > 0:
st -= 1
else:
a += 1
st = 0
for i in range(n-1,-1,-1):
if s[i] == ')':
st += 1
else:
if st > 0:
st -= 1
else:
b += 1
if a > b:
ed.append(b*dg + a)
elif a == b:
mid.append(a)
else:
op.append(a*dg + b)
op.sort()
ed.sort()
p = 0
for e in op:
a,b = divmod(e,dg)
if p < a:
print('No')
exit()
p += b-a
q = 0
for e in ed:
b,a = divmod(e,dg)
if q < b:
print('No')
exit()
q += a-b
if p == q and p >= max(mid):
print('Yes')
else:
print('No')
| 1 | 23,477,755,990,820 | null | 152 | 152 |
w = int(input())
h = w // 3600
m = (w % 3600) // 60
s = w % 60
print(f"{h}:{m}:{s}")
|
X,Y,A,B,C=list(map(int,input().split()))
p=sorted(list(map(int,input().split())),reverse=True)
q=sorted(list(map(int,input().split())),reverse=True)
r=sorted(list(map(int,input().split())),reverse=True)
i=X-1
j=Y-1
k=0
ans=sum(p[:X])+sum(q[:Y])
while k<len(r):
if i>-1 and j>-1:
if p[i]<q[j]:
cmin=p[i]
i-=1
else:
cmin=q[j]
j-=1
if r[k]<=cmin:
break
ans+=r[k]-cmin
k+=1
elif i>-1:
cmin=p[i]
i-=1
if r[k]<=cmin:
break
ans+=r[k]-cmin
k+=1
elif j>-1:
cmin=q[j]
j-=1
if r[k]<=cmin:
break
ans+=r[k]-cmin
k+=1
else:
break
print(ans)
| 0 | null | 22,728,436,097,680 | 37 | 188 |
import math
PI = math.pi
r = input()
men = r*r * PI
sen = r*2 * PI
print('%.6f %.6f' % (men, sen))
|
import math
x = raw_input()
x = float(x)
S = x*x*math.pi
l = 2*x*math.pi
print("%.6f %.6f" %(S, l))
| 1 | 635,640,437,072 | null | 46 | 46 |
n,k = map(int,input().split())
L = list(map(int,input().split()))
ok = 0
ng = 10**9
while abs(ok-ng) > 1:
mid = (ok+ng)//2
cur = 0
for i in range(n):
cur += L[i]//mid
if cur > k:
ok = mid
elif cur <= k:
ng = mid
K = [mid-1, mid, mid+1]
P = []
for i in range(3):
res = 0
if K[i] > 0:
for j in range(n):
res += (L[j]-1)//K[i]
if res <= k:
P.append(K[i])
print(min(P))
|
def resolve():
a, b, m = map(int, input().split())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
ans = min(a_list) + min(b_list)
for _ in range(m):
x, y, c = map(int, input().split())
ans = min(ans, a_list[x-1]+b_list[y-1]-c)
print(ans)
resolve()
| 0 | null | 30,178,825,760,860 | 99 | 200 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.