code1
stringlengths 16
427k
| code2
stringlengths 16
427k
| similar
int64 0
1
| pair_id
int64 6.82M
181,637B
⌀ | question_pair_id
float64 101M
180,471B
⌀ | code1_group
int64 2
299
| code2_group
int64 2
299
|
---|---|---|---|---|---|---|
a, b, c, k = map(int, input().split())
print(max(min(k, a) ,0) - max(min(k - (a + b), c), 0)) | letter = input()
if letter == letter.upper():
print("A")
else:
print("a")
| 0 | null | 16,566,912,340,772 | 148 | 119 |
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) | n = int(input())
A = [input() for _ in range(n)]
print("AC x " + str(A.count("AC")))
print("WA x " + str(A.count("WA")))
print("TLE x " + str(A.count("TLE")))
print("RE x " + str(A.count("RE")))
| 0 | null | 32,004,297,086,498 | 202 | 109 |
cards = []
suits = ['S', 'H', 'C', 'D']
for i in range(len(suits)):
for j in range(1, 14):
cards.append(suits[i] + ' ' + str(j))
n = int(input())
for i in range(n):
cards.remove(input())
for i in range(len(cards)):
print(cards[i]) | n = int(input())
d = []
z = 0
for i in range(n):
d1, d2 = map(int, input().split())
z = z+1 if d1==d2 else 0
d.append(z)
ans = 'Yes' if max(d)>=3 else 'No'
print(ans)
| 0 | null | 1,779,709,148,770 | 54 | 72 |
t1,t2=map(int,input().split())
a1,a2=map(int,input().split())
b1,b2=map(int,input().split())
d1=t1*a1+t2*a2
d2=t1*b1+t2*b2
import sys
if d1>d2:
dif=d1-d2
gap=(b1-a1)*t1
elif d1<d2:
dif=d2-d1
gap=(a1-b1)*t1
else:
print('infinity')
sys.exit()
if (b1-a1)*(b2-a2)>0 or (b1-a1)*(d2-d1)>0:
print(0)
sys.exit()
if gap%dif!=0:
print(gap//dif*2+1)
else:
print(gap//dif*2) | N, M = map(int, input().split())
Ev = M * (M - 1) // 2
Od = N * (N - 1) // 2
print(Ev + Od) | 0 | null | 88,535,777,674,840 | 269 | 189 |
[print(i) for i in (sorted([int(input()) for _ in range(10)])[:6:-1])] | n = int(input())
card = ['' for i in range(n)]
score1 = 0
score2 = 0
def win(score):
score += 3
return score
def draw(score):
score += 1
return score
for i in range(n):
card[i] = [j for j in input().split()]
for i in range(n):
if card[i][0] == card[i][1]:
score1 = draw(score1)
score2 = draw(score2)
else:
if card[i][0] > card[i][1]:
score1 = win(score1)
else:
score2 = win(score2)
print(score1,score2) | 0 | null | 989,558,905,412 | 2 | 67 |
[w,h,x,y,r] = map(int, input().split())
print("Yes" if r <= x and x <= w - r and r <= y and y <= h - r else "No") | whxyr = input().split()
[w, h, x, y, r] = [int(x) for x in whxyr]
if 0 <= x - r and x + r <= w and 0 <= y - r and y + r <= h:
print("Yes")
else:
print("No") | 1 | 450,114,780,660 | null | 41 | 41 |
s = raw_input().rstrip().split(" ")
W = int(s[0])
H = int(s[1])
x = int(s[2])
y = int(s[3])
r = int(s[4])
if (x-r)>=0 and (x+r)<=W and (y-r)>=0 and (y+r)<=H:print "Yes"
else:print "No" | n = int(input())
a = list(map(int, input().split()))
b = a[0]
for v in a[1:]:
b ^= v
print(*map(lambda x: x ^ b, a))
| 0 | null | 6,437,453,858,370 | 41 | 123 |
N,K = map(int,input().split())
A = list(map(int,input().split()))
already_town_check =[False]*(N+1)
already_town_check[1] = True
already_town = [1]
now_town=1
check=False
my_append = already_town.append
for i in range(K):
check_town=A[now_town-1]
if(already_town_check[check_town]):
cicle_start_value=check_town
check=True
break
already_town_check[check_town]=True
my_append(check_town)
now_town = check_town
if(check==False):
print(now_town)
else:
cicle_start=already_town.index(cicle_start_value)
roop_town=already_town[cicle_start:]
print(roop_town[(K-cicle_start)%((i+1)-cicle_start)])
| while True:
h,w = map(int,input().split())
if h == 0 and w == 0:
break
for x in range(h):
if x == range(h)[0] or x == range(h)[-1]:
print(w*"#",end="")
else:
for y in range(w):
if y == range(w)[0] or y == range(w)[-1]:
print("#",end="")
else:
print(".",end="")
print()
print()
| 0 | null | 11,872,220,341,430 | 150 | 50 |
h, n = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]
INF = 1 << 60
dp = [INF] * (h + 1)
dp[0] = 0
for i in range(0, h + 1):
# hを超えるために必要な最小コストは?
for a, b in ab:
if i + a <= h:
dp[i + a] = min(dp[i] + b, dp[i + a])
else:
dp[h] = min(dp[i] + b, dp[h])
print(dp[h]) | X = int(input())
K = 1
while K > 0:
if X * K % 360 == 0:
print(K)
break
else:
K += 1
| 0 | null | 47,077,823,995,220 | 229 | 125 |
N, K = map(int,input().split())
P = [int(x)-1 for x in input().split()]
C = [int(x) for x in input().split()]
def loop(s):
value = 0
done = set()
while s not in done:
done.add(s)
s = P[s]
value += C[s]
return len(done), value
def f(s,K):
ans = -10**18
total = 0
done = set()
while s not in done:
done.add(s)
s = P[s]
total += C[s]
ans = max(ans,total)
K -= 1
if K==0:
return ans
lamb, value = loop(s)
if K > 2*lamb:
if value > 0:
K -= lamb
total += (K//lamb)*value
ans = max(ans,total)
K -= (K//lamb)*lamb
K += lamb
if value <= 0:
K = min(K,lamb+5)
while K > 0:
s = P[s]
total += C[s]
ans = max(ans,total)
K -= 1
return ans
print(max([f(s,K) for s in range(N)])) | n,k=map(int,input().split())
P=list(map(int,input().split()))
for i in range(n):
P[i]-=1
C=list(map(int,input().split()))
PP=[0]*n
A=[0]*n
V=[0]*n
for i in range(n):
if V[i]==1:
continue
B=[i]
c=1
j=i
p=C[i]
while P[j]!=i:
j=P[j]
B.append(j)
c=c+1
p=p+C[j]
for j in B:
A[j]=c
PP[j]=p
ans=-10**10
for i in range(n):
if PP[i]<0:
aans=0
f=0
else:
if k//A[i]-1>0:
aans=(k//A[i]-1)*PP[i]
f=1
else:
aans=0
f=2
l=i
aaans=-10**10
if f==0:
for j in range(min(k,A[i])):
aans=aans+C[P[l]]
l=P[l]
if aans>aaans:
aaans=aans
elif f==1:
for j in range(k%A[i]+A[i]):
aans=aans+C[P[l]]
l=P[l]
if aans>aaans:
aaans=aans
else:
for j in range(min(k,2*A[i])):
aans=aans+C[P[l]]
l=P[l]
if aans>aaans:
aaans=aans
if aaans>ans:
ans=aaans
print(ans) | 1 | 5,378,957,157,990 | null | 93 | 93 |
# A Curtain
a, b = map(int, input().split())
x = a-(b * 2)
if x > 0:
print(x)
else:
print(0)
| a,b=(map(int,input().split()))
c=0
if a<=b:
print(0)
elif a>b:
c=a-b*2
if c>0:
print(c)
elif c<=0:
print(0) | 1 | 166,460,588,207,300 | null | 291 | 291 |
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def main():
N = INT()
A = LI()
RGB = [0,0,0]
answer = 1
MOD = 10**9+7
for i in range(N):
if RGB.count(A[i]) == 0:
print(0)
return
answer *= RGB.count(A[i])
answer %= MOD
for j in range(3):
if RGB[j] == A[i]:
RGB[j] += 1
break
print(answer)
return
if __name__ == '__main__':
main()
|
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def main():
mod=10**9+7
K=I()
S=input()
N=len(S)
def cmb(n, r, mod):
if (r < 0) or (n < r):
return 0
r = min(r, n - r)
return (fact[n] * factinv[r] * factinv[n-r])%mod
fact=[1,1]
factinv=[1,1]
inv=[0,1]
for i in range(2, N+K+2 + 1):
fact.append((fact[-1] * i) % mod)
inv.append((-inv[mod % i] * (mod // i)) % mod)
factinv.append((factinv[-1] * inv[-1]) % mod)
#Sの条件を初めて満たした時の末端いちで場合分け
#i文字目が末端で,自由枠がj個(iが決まればjも決まるけど,まとめやすいので)
def calc(i,j):
temp=pow(26,K-j,mod)#残りの完全自由枠
temp=(temp*pow(25,j,mod)*cmb(i-1,j,mod))%mod
return temp
ans=0
for j in range(K+1):
i=j+N
ans=(ans+calc(i,j))%mod
print(ans)
main()
| 0 | null | 71,288,321,410,712 | 268 | 124 |
def have_ABC_string(n, str):
count = 0
for i in range(n-2):
if str[i] == 'A' and str[i+1] == 'B' and str[i+2] == 'C':
count += 1
return count
N = int(input())
S = input()
result = have_ABC_string(N, S)
print(result) | n=input()
s1= input()
count=0
for i in range(len(s1)-2):
if(s1[i]=='A' and s1[i+1]=='B' and s1[i+2]=='C'):
count=count+1
print(count) | 1 | 99,497,571,163,650 | null | 245 | 245 |
n, x, m = map(int, input().split())
ans = 0
if x == 0:
print(0)
exit()
elif x == 1:
print(n)
exit()
flag = [False]*m
a = []
while not flag[x]:
flag[x] = True
a.append(x)
x = pow(x, 2, m)
loop_start_idx = a.index(x)
loop_len = len(a) - loop_start_idx
loop_count = (n - loop_start_idx)//loop_len
loop_amari = (n-loop_start_idx)%loop_len
ans = sum(a[:loop_start_idx])
ans += sum(a[loop_start_idx:])*loop_count
ans += sum(a[loop_start_idx: loop_start_idx + loop_amari])
print(ans)
| n, k = map(int, input().split())
A = list(map(int, input().split()))
l = 1
r = 10**9+1
while l < r:
mid = (l+r)//2
count = 0
for i in range(n):
if A[i] > mid:
count += A[i]//mid
if count <= k:
r = mid
else:
l = mid+1
print(l) | 0 | null | 4,696,040,062,230 | 75 | 99 |
n,k = map(int,input().split())
mod = 10**9 +7
sum1 = 0
for i in range(k,n+2):
sum1 +=(-i*(i-1)//2 + i*(2*n-i+1)//2+1)%mod
print(sum1%mod) | N, K = map(int, input().split())
mod = 10 ** 9 + 7
ans = 0
for i in range(K, N+2):
ans += ((N + 1) * i - i ** 2 + 1) % mod
print(ans % mod) | 1 | 32,890,399,557,740 | null | 170 | 170 |
a=map(int,raw_input().split())
print(str(a[0]*a[1])+" "+str(2*(a[0]+a[1]))) | #文字列をリストに変換
list_coffee = list(input())
#判別
if list_coffee[2] == list_coffee[3] and list_coffee[4] == list_coffee[5]:
text = "Yes"
else:
text = "No"
#結果の表示
print(text) | 0 | null | 21,185,991,781,808 | 36 | 184 |
import collections
N = int (input())
nums = list(map(int,input().split(" ")))
MOD = 998244353
di = collections.defaultdict(lambda :0)
for num in nums:
di[num] += 1
ans = 1 if nums[0] == 0 else 1
ans = 0 if 0 in nums[1:] else 1
bottom = min(nums)
top= max(nums)
for d in range(1,top + 1):
ans *= pow(di[d - 1] ,di[d ],MOD)
print(ans % MOD) | def my_pow(base, n, mod):
if n == 0:
return 1
x = base
y = 1
while n > 1:
if n % 2 == 0:
x *= x
n //= 2
else:
y *= x
n -= 1
x %= mod
y %= mod
return x * y % mod
N = int(input())
D = list(map(int, input().split()))
dmax = max(D)
MOD = 998244353
cnt = [0] * (10 ** 5 + 1)
for d in D:
cnt[d] += 1
if D[0] or cnt[0] != 1:
print(0)
exit()
ans = cnt[0]
for i in range(1, dmax + 1):
now = my_pow(cnt[i - 1], cnt[i], MOD)
ans *= now
ans %= MOD
print(ans) | 1 | 155,075,711,782,752 | null | 284 | 284 |
N = int(input())
S, T = input().split()
i = ''
for j in zip(S, T):
for k in j:
i += k
print(i) | c = input()
print(chr((ord(c)+1)))
| 0 | null | 102,191,505,729,510 | 255 | 239 |
import sys
from math import gcd
input = lambda: sys.stdin.readline().rstrip()
n = int(input())
A = list(map(int, input().split()))
nowgcd = A[0]
# 全体のGCDを取る
for i in A:
nowgcd = gcd(nowgcd, i)
if nowgcd != 1:
print('not coprime')
exit()
# osa_k法で前処理
MAXN = 10**6 + 5
sieve = [i for i in range(MAXN + 1)]
p = 2
while p * p <= MAXN:
# まだチェックされていないなら
if sieve[p] == p:
# 次のqの倍数からp刻みでチェック入れていく
for q in range(2 * p, MAXN + 1, p):
if sieve[q] == q:
sieve[q] = p
p += 1
check = set()
for a in A:
tmp = set()
while (a > 1):
tmp.add(sieve[a])
a //= sieve[a]
for p in tmp:
if p in check:
print('setwise coprime')
exit()
check.add(p)
print('pairwise coprime')
| import sys, math
from functools import lru_cache
import numpy as np
import heapq
from collections import defaultdict
sys.setrecursionlimit(10**9)
MOD = 10**9+7
def input():
return sys.stdin.readline()[:-1]
def mi():
return map(int, input().split())
def ii():
return int(input())
def i2(n):
tmp = [list(mi()) for i in range(n)]
return [list(i) for i in zip(*tmp)]
def sieve(n):
res = [i for i in range(n)]
i = 2
while i*i < n:
if res[i] < i:
i += 1
continue
j = i*i
while j < n:
if res[j] == j:
res[j] = i
j += i
i += 1
return res
def factor(n, min_factor):
res = set()
while n > 1:
res.add(min_factor[n])
n //= min_factor[n]
return res
def main():
N = ii()
A = np.array(list(mi()))
m = max(A)
s = sieve(m+1)
d = defaultdict(bool)
g = np.gcd.reduce(A)
if g > 1:
print('not coprime')
return
for a in A:
f = factor(a, s)
for v in f:
if d[v]:
print('setwise coprime')
return
d[v] = True
print('pairwise coprime')
if __name__ == '__main__':
main()
| 1 | 4,122,514,336,562 | null | 85 | 85 |
sample_string = input()
test_string = input()
if test_string in sample_string * 2:
print("Yes")
else:
print("No") | import sys
s = list(input())
p = list(input())
for i in range(len(s)):
counter = 0
for j in range(len(p)):
m = (i+j)%len(s)
if s[m] == p[j]:
counter += 1
if counter == len(p):
print('Yes')
sys.exit()
print('No')
| 1 | 1,755,689,732,900 | null | 64 | 64 |
n,k = map(int,input().split())
A = list(map(int,input().split()))
c = 0
from collections import Counter
d = Counter()
d[0] = 1
ans = 0
r = [0]*(n+1)
for i,x in enumerate(A):
if i>=k-1:
d[r[i-(k-1)]]-=1#ここで範囲kからはみ出たものの数を減らす
c = (c+x-1)%k
ans += d[c]
d[c] += 1
r[i+1] = c
print(ans) | a, b, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
# cheapest price without using discount ticket
min_price = min(a) + min(b)
for i in range(m):
arr = list(map(int, input().split()))
discounted_price = a[arr[0] - 1] + b[arr[1] - 1] - arr[2]
min_price = min(min_price, discounted_price)
print(min_price) | 0 | null | 95,493,990,839,478 | 273 | 200 |
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)
bingo = [list(mapint()) for _ in range(3)]
N = int(input())
card = [[0]*3 for _ in range(3)]
for _ in range(N):
n = int(input())
for i in range(3):
for j in range(3):
if n==bingo[i][j]:
card[i][j] = 1
for i in range(3):
if sum(card[i])==3:
print('Yes')
exit()
for i in range(3):
if card[0][i]+card[1][i]+card[2][i]==3:
print('Yes')
exit()
if card[0][0]+card[1][1]+card[2][2]==3 or card[2][0]+card[1][1]+card[0][2]==3:
print('Yes')
exit()
print('No') | a1 = list(map(int,input().split()))
a2 = list(map(int,input().split()))
a3 = list(map(int,input().split()))
ans = "No"
n = int(input())
for i in range(n):
p = int(input())
if a1.count(p)>0:
a1[a1.index(p)]=-1
if a2.count(p)>0:
a2[a2.index(p)]=-1
if a3.count(p)>0:
a3[a3.index(p)]=-1
if max(a1)==-1 or max(a2)==-1 or max(a3)==-1:
ans="Yes"
if (a1[0]==-1 and a2[0]==-1 and a3[0]==-1) or (a1[1]==-1 and a2[1]==-1 and a3[1]==-1) or ((a1[2]==-1 and a2[2]==-1 and a3[2]==-1)):
ans="Yes"
if (a1[0]==-1 and a2[1]==-1 and a3[2]==-1) or (a1[2]==-1 and a2[1]==-1 and a3[0]==-1) :
ans="Yes"
print(ans)
| 1 | 60,288,227,105,652 | null | 207 | 207 |
def main():
N, M = list(map(lambda n: int(n), input().split(" ")))
if M == 0:
print("0 0")
return 0
AC = [0] * N
WA = [0] * N
for i in range(M):
tmp = input().split(" ")
No = int(tmp[0]) - 1
if tmp[1] == "AC":
AC[No - 1] = 1
if tmp[1] == "WA" and AC[No - 1] == 0:
WA[No - 1] += 1
totAC = sum(AC)
totWA = sum([AC[i] * WA[i] for i in range(len(AC))])
print(totAC, end=" ")
print(totWA)
main()
| print(['bust', 'win'][sum(map(int, input().split()))<=21]) | 0 | null | 106,032,290,473,538 | 240 | 260 |
import math
A,B,N = map(int,input().split())
if N >= B:
N = B - 1
print(math.floor(A*N/B) - A * math.floor(N/B)) | #from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
# d = m - k[i] - k[j]
# if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも!!
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
return list(map(int,input().split()))
def I():
return int(input())
n = input()
if n[-1] in ['2','4','5','7','9']:
print('hon')
elif n[-1] in ['0','1','6','8']:
print('pon')
else:
print('bon')
| 0 | null | 23,643,464,410,240 | 161 | 142 |
N = int(input())
BOX = {}
for i in range(N):
S = input()
if S in BOX:
BOX[S] = (BOX[S] + 1)
else:
BOX[S] = 0
MAX = max(BOX.values())
keys = [k for k, v in BOX.items() if v == MAX]
keys = sorted(keys)
for i in range(len(keys)):
print(keys[i])
| N, K = map(int, input().split())
ans = N
a = ans // K
b = ans % K
if a > 0:
ans = N - K * a
a = ans // K
b = ans % K
if a == 0:
if ans > abs(ans - K):
print(abs(ans - K))
else:
print(ans) | 0 | null | 54,529,297,195,450 | 218 | 180 |
#!/usr/bin/env python3
import sys
from itertools import chain
# form bisect import bisect_left, bisect_right, insort_left, insort_right
# from collections import Counter
# import numpy as np
def solve(N: int, K: int, p: "List[int]"):
p = sorted(p)
return sum(p[:K])
def main():
tokens = chain(*(line.split() for line in sys.stdin))
# N, K, p = map(int, line.split())
N = int(next(tokens)) # type: int
K = int(next(tokens)) # type: int
p = [int(next(tokens)) for _ in range(N)] # type: "List[int]"
answer = solve(N, K, p)
print(answer)
if __name__ == "__main__":
main()
| N, K = map(int,input().split())
P = list(map(int,input().split()))
P.sort()
price = sum(P[:K])
print(price) | 1 | 11,715,595,188,082 | null | 120 | 120 |
def main():
a, b, c = map(int, input().split())
ans = 0
for i in range(a, b + 1):
d = c % i
if d == 0:
ans += 1
print(ans)
if __name__ == "__main__":
main() | from collections import *
import copy
N,v,u=map(int,input().split())
AB=[list(map(int,input().split())) for i in range(N-1)]
data=[[] for i in range(N+1)]
for A,B in AB:
data[A].append(B)
data[B].append(A)
stack=deque([[v,0]])
visited=set([v])
while stack:
a,m=stack.popleft()
for p in data[a]:
if p==u:
K=m+1
if not p in visited:
visited.add(p)
stack.append([p,m+1])
for i in range(len(data)):
if len(data[i])>1:
visited=set([i])
stack=deque([i])
End=set()
break
if N==2:
print(0)
exit()
while stack:
a=stack.popleft()
flag=0
for p in data[a]:
if not p in visited:
visited.add(p)
stack.append(p)
flag=1
if flag==0:
End.add(a)
Uvisited=set([u])
Vvisited=set([])
if not v in End:
Vvisited.add(v)
Ustack=deque([[u,0]])
Vstack=deque([[v,0]])
ans=[]
M=1
while Ustack:
while Vstack:
a,m=Vstack.popleft()
if m==M:
Vstack.append([a,m])
break
for p in data[a]:
if not p in End:
if p in Uvisited:
pass
#ans.append(m)
elif not p in Vvisited:
Vvisited.add(p)
Vstack.append([p,m+1])
while Ustack:
a,m=Ustack.popleft()
if m==M:
Ustack.append([a,m])
break
for p in data[a]:
if not p in Uvisited:
if p in Vvisited:
ans.append(m+1)
Uvisited.add(p)
Ustack.append([p,m+1])
M+=1
print(max(ans))
| 0 | null | 58,712,799,155,922 | 44 | 259 |
from collections import deque
N, D, A = map(int, input().split())
monsters = []
for _ in range(N):
monsters.append(list(map(int, input().split())))
monsters.sort()
d = deque()
ans = 0
damage = 0
for x, h in monsters:
while d:
if d[0][0] >= x - D:
break
_, power = d.popleft()
damage -= power
h -= damage
if h <= 0:
continue
attack = (h - 1) // A + 1
ans += attack
d.append((x + D, attack * A))
damage += attack * A
print(ans)
| def solve(xy):
ans = 0
pos = [xy[0] for _ in range(4)]
for p in xy:
if pos[0][0] + pos[0][1] < p[0] + p[1]:
pos[0] = p
if pos[1][0] + pos[1][1] > p[0] + p[1]:
pos[1] = p
if pos[2][0] - pos[2][1] < p[0] - p[1]:
pos[2] = p
if pos[3][0] - pos[3][1] > p[0] - p[1]:
pos[3] = p
return max(pos[0][0]+pos[0][1]-pos[1][0]-pos[1][1], pos[2][0]-pos[2][1]-pos[3][0]+pos[3][1])
if __name__ == "__main__":
N = int(input())
xy = [list(map(int, input().split())) for _ in range(N)]
print(solve(xy))
| 0 | null | 43,006,301,966,628 | 230 | 80 |
import sys
from itertools import product
input = lambda:sys.stdin.buffer.readline().rstrip()
m, n, k = map(int, input().split())
grid = [[0] * n for _ in range(m)]
for _ in range(k):
i, j, v = map(int, input().split())
i -= 1; j -= 1
grid[i][j] = v
dp = [[0] * 4 for _ in range(n)]
for i in range(m):
ndp = [[0] * 4 for _ in range(n)]
for j, k in product(range(n), range(4)):
# from top
ndp[j][0] = max(ndp[j][0], dp[j][k])
ndp[j][1] = max(ndp[j][1], dp[j][k] + grid[i][j])
# from left
if j > 0:
ndp[j][k] = max(ndp[j][k], ndp[j - 1][k])
if k > 0:
ndp[j][k] = max(ndp[j][k], ndp[j - 1][k - 1] + grid[i][j])
dp = ndp
print(max(dp[-1])) | a=map(int,raw_input().split())
if 0<=a[2]-a[4] and a[2]+a[4]<=a[0] and 0<=a[3]-a[4] and a[3]+a[4]<=a[1]:
print "Yes"
else:
print "No" | 0 | null | 2,961,808,867,476 | 94 | 41 |
N, M, Q = map(int, input().split())
A, B, C, D = [],[],[],[]
for i in range(Q):
a, b, c, d = map(int, input().split())
A.append(a)
B.append(b)
C.append(c)
D.append(d)
L = []
def dfs(S):
if len(S) == N:
L.append(S)
elif S == "":
for i in range(M):
S = str(i)
dfs(S)
else:
for i in range(int(S[-1]), M):
dfs(S+str(i))
dfs("")
Ans = 0
for l in L:
ans = 0
for a, b, c, d in zip(A,B,C,D):
if int(l[b-1]) - int(l[a-1]) == c:
ans += d
Ans = max(ans, Ans)
print(Ans)
| import statistics
while True:
n = int(input())
if n == 0:
break
str = input().split()
list = []
for i in range(n):
list.append(int(str[i]))
print(statistics.pstdev(list)) | 0 | null | 13,890,891,531,760 | 160 | 31 |
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
n, x, y = LI()
ans = [0 for _ in range(n)]
m = y-x-1
for i in range(1,n):
for j in range(i+1,n+1):
direct = j-i
short = abs(i-x) + 1 + abs(y-j)
cnt = min(direct,short)
ans[cnt] += 1
print(*ans[1:],sep="\n")
main()
| import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])
import networkx as nx
n,x,y = inpm()
x -= 1
y -= 1
if x > y:
x,y = y,x
d = np.zeros((n,n),int)
ans = np.zeros(n,int)
for i in range(n):
for j in range(i+1,n):
a = j - i
b = abs(x - i) + abs(j - y) + 1
ans[min(a,b)] += 1
for i in range(1,n):
print(ans[i]) | 1 | 43,974,614,671,780 | null | 187 | 187 |
import collections
n,x,m=map(int,input().split())
if n==1 or x==0:
print(x)
exit()
start,end,loopcnt=0,0,0
a={x:0}
wk=x
for i in range(1,m):
wk=(wk*wk)%m
if not wk in a:
a[wk]=i
else:
start=a[wk]
end=i
break
a=sorted(a.items(),key=lambda x:x[1])
koteiindex=min(n,start)
koteiwa=0
for i in range(koteiindex):
koteiwa+=a[i][0]
loopcnt=(n-koteiindex)//(end-start)
loopindex=start-1+(n-koteiindex)%(end-start)
loopwa=0
amariwa=0
for i in range(start,end):
if i<=loopindex:
amariwa+=a[i][0]
loopwa+=a[i][0]
ans=koteiwa+loopwa*loopcnt+amariwa
print(ans)
| import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007
def main():
N, X, M = map(int, readline().split())
P = N.bit_length()
pos = [[0] * M for _ in range(P)]
value = [[0] * M for _ in range(P)]
for r in range(M):
pos[0][r] = r * r % M
value[0][r] = r
for p in range(P - 1):
for r in range(M):
pos[p + 1][r] = pos[p][pos[p][r]]
value[p + 1][r] = value[p][r] + value[p][pos[p][r]]
ans = 0
cur = X
for p in range(P):
if N & (1 << p):
ans += value[p][cur]
cur = pos[p][cur]
print(ans)
return
if __name__ == '__main__':
main()
| 1 | 2,835,973,368,040 | null | 75 | 75 |
n = int(input())
s = sorted([int(i) for i in input().split()])
for i, v in enumerate(s[0:-1]):
if v == s[i+1]:
print('NO')
exit()
print('YES') | N = int(input())
A = list(map(int,input().split()))
Q = int(input())
S = [list(map(int, input().split())) for l in range(Q)]
l = [0] * 10**6
tot = 0
for i in range(N) :
l[A[i]]+=1
tot += A[i]
for i in range(Q):
s0 = S[i][0]
s1 = S[i][1]
tot += s1 * (l[s0] + l[s1]) - (s0 * l[s0] + s1*l[s1])
l[s1] += l[s0]
l[s0] = 0
print(tot) | 0 | null | 42,947,655,412,542 | 222 | 122 |
s = input()
t = input()
ans = "Yes"
for i in range(len(s)):
if s[i] == t[i]:
continue
else:
ans = "No"
if ans == "Yes" and (len(s)+1) == len(t):
ans = "Yes"
else:
ans = "No"
print(ans) | S = input()
T = input()
if S+T[-1]==T: print('Yes')
else: print('No') | 1 | 21,539,426,248,814 | null | 147 | 147 |
def a():
x = int(input())
k = 1
while (k * x) % 360 != 0:
k += 1
print(k)
a()
| n,k=map(int,input().split())
h=list(map(int,input().split()))
h.append(k)
print(len(h[sorted(h).index(k)+1:])) | 0 | null | 96,072,255,504,820 | 125 | 298 |
from math import ceil
def is_ok(arg):
# 条件を満たすかどうか?問題ごとに定義
ret = 0
for i in range(n):
ret += ceil(A[i]/arg)-1
return bool(ret<=k)
def meguru_bisect(ng, ok):
'''
初期値のng,okを受け取り,is_okを満たす最小(最大)のokを返す
まずis_okを定義すべし
ng ok は とり得る最小の値-1 とり得る最大の値+1
最大最小が逆の場合はよしなにひっくり返す
'''
while (abs(ok - ng) > 1):
mid = (ok + ng) // 2
if is_ok(mid):
ok = mid
else:
ng = mid
return ok
n,k = map(int,input().split())
A = list(map(int,input().split()))
print(meguru_bisect(0,max(A)+1)) | N = int(input())
a = []
for i in range(N):
a.append(input())
a = sorted(a)
count = 0
count_list=[]
b = []
for i,k in enumerate(a):
if i == 0:
continue
if k == a[i-1]:
count+=1
else:
count_list.append(count)
b.append(a[i-1])
count = 0
if i==N-1:
count_list.append(count)
b.append(a[i])
maxi = 0
maxi_list =[]
for i,k in enumerate(count_list):
if maxi <k:
maxi_list.clear()
maxi_list.append(b[i])
maxi = k
elif maxi == k:
maxi_list.append(b[i])
else:
continue
for i in maxi_list:
print(i)
| 0 | null | 38,424,614,949,600 | 99 | 218 |
print(int(input()) ^ 0x01) | print(1 if input()=="0" else 0)
| 1 | 2,921,580,647,942 | null | 76 | 76 |
s = int(input())
n = (s/3)
print(n*n*n) | L = int(input())
A = L/3
B = (L-A)/2
C = L - A - B
print(A * B * C) | 1 | 47,089,788,839,632 | null | 191 | 191 |
a,b,k=map(int,input().split())
taka=a-k
if taka < 0:
aoki = b-abs(taka)
taka = 0
if aoki < 0:
aoki = 0
else:
aoki=b
print(taka,aoki) | A,B,K = map(int,input().split())
if K>A+B:
A,B = 0,0
elif K>A:
A,B = 0,B-K+A
else:
A -= K
print(f'{A} {B}') | 1 | 104,292,941,102,560 | null | 249 | 249 |
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)
| from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
class Combination:
"""
comb = Combination(1000000)
print(comb(5, 3)) # 10
"""
def __init__(self, n_max, mod=10**9+7):
self.mod = mod
self.modinv = self.make_modinv_list(n_max)
self.fac, self.facinv = self.make_factorial_list(n_max)
def __call__(self, n, r):
return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod
def make_factorial_list(self, n):
# 階乗のリストと階乗のmod逆元のリストを返す O(n)
# self.make_modinv_list()が先に実行されている必要がある
fac = [1]
facinv = [1]
for i in range(1, n+1):
fac.append(fac[i-1] * i % self.mod)
facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
return fac, facinv
def make_modinv_list(self, n):
# 0からnまでのmod逆元のリストを返す O(n)
modinv = [0] * (n+1)
modinv[1] = 1
for i in range(2, n+1):
modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
return modinv
co = Combination(200006)
n,m = inpl()
if m >= n:
m = n-1
res = 1
if m == 1:
print(co(n-1,n-2) * n)
quit()
else:
for i in range(m):
tmp = i+1
res += co(n-1,n-tmp-1) * co(n,tmp)
res %= mod
# print(res)
print(res) | 1 | 66,903,255,796,258 | null | 215 | 215 |
n = int(input())
s = str(input())
r = 0
g = 0
b = 0
for i in range(n):
if s[i] == "R":
r += 1
elif s[i] == "G":
g += 1
else:
b += 1
p = r*g*b
for i in range(n-2):
for j in range(int(n/2)+1):
if i + 2*j >n-1:
break
if s[i] != s[i+j] and s[i] != s[i+2*j] and s[i+2*j] != s[i+j]:
p -= 1
print(p) | from collections import Counter
n = int(input())
A = list(map(int, input().split()))
C = lambda x: x*(x-1)//2
s = 0
D = Counter(A)
for i,j in D.items():
s += C(j)
for i in A:
print(s-D[i]+1) | 0 | null | 41,871,070,113,440 | 175 | 192 |
import bisect
def main():
N,D,A=map(int,input().split())
XH=[list(map(int,input().split())) for _ in range(N)]
XH.sort()
for i in range(N):
XH[i][1]=-(-XH[i][1]//A)
X=[x for x,a in XH]
far=[0]*N
for i in range(N):
far[i]=bisect.bisect_right(X,X[i]+2*D)-1
ans=0
carry=0
outrange=[0]*(N+1)
for i in range(N):
x=XH[i][0]
hp=XH[i][1]
carry-=outrange[i]
if carry>=hp:
continue
outrange[bisect.bisect_right(X,X[i]+2*D)]+=hp-carry
ans+=hp-carry
carry+=hp-carry
print(ans)
if __name__ == '__main__':
main() | """
Given N, D, A, N means no. of monsters, D means distances of bombs, A attack of the bombs
Find the minimum of bombs required to win.
Each monster out of N pairs - pair[0] = position, pair[1] = health
"""
from collections import deque
mod = 1e9+7
def add(a, b):
c = a + b
if c >= mod:
c -= mod
return c
def main():
N, D, A = [int(x) for x in raw_input().split()]
D *= 2
monsters = []
for _ in range(N):
pos, health = [int(x) for x in raw_input().split()]
monsters.append([pos, health])
monsters.sort(key = lambda x : x[0])
remain_attacks_queue = deque([])
remain_attack = 0
ans = 0
for monster in monsters:
# monster[0] - pos, monster[1] - health
# queue[0] - position, queue[1] - attack points
while len(remain_attacks_queue) and monster[0] - D > remain_attacks_queue[0][0]:
remain_attack -= remain_attacks_queue.popleft()[1]
if remain_attack < monster[1]:
remained_health = monster[1] - remain_attack
times = remained_health / A if remained_health % A == 0 else remained_health // A + 1
#print(times)
attack = times * A
remain_attacks_queue.append([monster[0], attack])
ans += times
remain_attack += attack
print(ans)
main() | 1 | 81,817,057,106,460 | null | 230 | 230 |
while True:
n,x = map(int,input().split())
if n == x == 0:
break
d = 0
for i in range(1,n+1):
for j in range(i+1,n+1):
k = x-(i+j)
if k >= 1 and k<= n and k > j:
d += 1
print(d)
| NX = input().split()
n = int(NX[0])
x = int(NX[1])
while n!=0 or x!=0:
out = 0
for ii in range(n-2):
i = ii+1 #i:1~n-2
for jj in range(n-1-i):
j = jj+1+i #j:i+1~i+1+n-2-i=i+1~n-1
for kk in range(n-j):
k = kk+1+j #k:j+1~n-j+j+1 =
if k !=i and k!=j and i+j+k == x:
out = out+1
print(out)
NX = input().split()
n = int(NX[0])
x = int(NX[1]) | 1 | 1,296,450,850,510 | null | 58 | 58 |
n = int(input())
t_count = 0
h_count = 0
for i in range(n):
t_animal,h_animal = map(str,input().split())
if t_animal < h_animal:
h_count+=3
elif t_animal > h_animal:
t_count+=3
else:
t_count+=1
h_count+=1
print("{} {}".format(t_count,h_count))
| N = int(input())
L = []
for _ in range(N):
L.append(list(map(int,input().split())))
ans = 0
for i in range(N-1):
for j in range(i + 1, N):
ans += ((L[i][0] - L[j][0]) ** 2 + (L[i][1] - L[j][1]) ** 2) ** 0.5
ans *= 2 / N
print(ans) | 0 | null | 75,077,416,555,990 | 67 | 280 |
import sys
sys.setrecursionlimit(10**9)
def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))
INF=10**20
def main():
X=ii()
i = 1
yen = 100
while yen < X:
r = yen // 100
yen += r
i += 1
print(i-1)
if __name__ == "__main__":
main() | x = int(input())
m = 100
for i in range(1, 10**18):
m = (m*101)//100
if m >= x:
re = i
break
print(re) | 1 | 26,940,464,951,990 | null | 159 | 159 |
# coding: utf-8
input_nums = raw_input().rstrip().split()
a, b = [int(x) for x in input_nums]
menseki = a * b
syutyo = 2 * (a + b)
print menseki, syutyo | x= input()
a, b = x.split()
a=int(a)
b=int(b)
print(a*b, a+a+b+b) | 1 | 313,706,948,612 | null | 36 | 36 |
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
sum_res = sum(A)
counter = [0 for _ in range(10 ** 5 + 1)]
for a in A:
counter[a] += 1
for _ in range(Q):
B, C = map(int, input().split())
sum_res = sum_res - (counter[B] * B)
sum_res = sum_res + (counter[B] * C)
counter[C] += counter[B]
counter[B] = 0
print(sum_res)
| import sys
input = sys.stdin.readline
import collections
def main():
N = int(input())
A = list(map(int, input().split()))
v_freq = collections.Counter(A)
v_sum = 0
for v, freq in v_freq.items():
v_sum += v * freq
Q = int(input())
for _ in range(Q):
B, C = map(int, input().split())
if B in v_freq:
v_sum -= (B - C) * v_freq[B]
if C in v_freq:
v_freq[C] += v_freq[B]
else:
v_freq[C] = v_freq[B]
del v_freq[B]
print(v_sum)
else:
print(v_sum)
if __name__ == '__main__':
main()
| 1 | 12,153,825,604,318 | null | 122 | 122 |
import math
a = float(input())
print(2*a*math.pi) | n,m,l=map(int,raw_input().split())
A=[]
B=[]
for i in range(n):
A.append(map(int,raw_input().split()))
for i in range(m):
B.append(map(int,raw_input().split()))
for i in range(n):
print(' '.join(map(str,[sum([A[i][j]*B[j][k] for j in range(m)]) for k in range(l)]))) | 0 | null | 16,295,629,113,600 | 167 | 60 |
N = int(input())
L = []
for i in range(N):
L.append(list(input().split()))
S = input()
ans = 0
f = 0
for i in range(N):
if f == 1:
ans += int(L[i][1])
if L[i][0] == S:
f = 1
print(ans) | N = int(input())
clips = [input().split() for _ in range(N)]
X = input()
ans = 0
for i,title in enumerate(clips):
if X in title:
for j in range(i+1, N):
ans += int(clips[j][1])
break
print(ans) | 1 | 96,684,093,828,688 | null | 243 | 243 |
def solve(n,x):
count = 0
for i in range(1,n+1):
for j in range(i+1,n+1):
k = x - i - j
if j < k and k <= n:
count += 1
return count
while True:
n,x = map(int,input().split())
if n == x == 0:
break;
print(solve(n,x)) | N, K = map(int, input().split())
Max = [0]
Min = [0]
for i in range(1, N+2):
Max.append(N-i+1+Max[i-1])
Min.append(i-1+Min[i-1])
A = [mx-mn+1 for mx,mn in zip(Max,Min)]
B =[0]
for i in range(N+1):
B.append(B[i]+A[i+1])
print((B[N+1]-B[K-1])%(10**9+7)) | 0 | null | 17,080,262,039,190 | 58 | 170 |
n = int(input())
print('{:.10f}'.format(1-(n//2 / n)))
| #!/usr/bin/env python3
#!/usr/bin/env python3
N = int(input())
print(0.5 if N % 2 == 0 else ((N//2)+1) / N)
| 1 | 177,290,237,816,408 | null | 297 | 297 |
N = int(input())
ans = [0] * N
for i in range(1, 101):
for j in range(1, 101):
for k in range(1, 101):
tmp = i**2+j**2+k**2+i*j+j*k+k*i
if tmp <= N:
ans[tmp-1] += 1
print(*ans, sep='\n') | data = input().split()
n = data[0]
m = data[1]
if n == m:
print("Yes")
else:
print("No") | 0 | null | 45,737,558,791,244 | 106 | 231 |
S=input()
N=len(S)+1
LEFT=[0]
RIGHT=[0]
left=0
right=0
for i in range(N-1):
if S[i]==">":
left=0
else:
left+=1
LEFT+=[left]
for i in range(N-2,-1,-1):
if S[i]=="<":
right=0
else:
right+=1
RIGHT+=[right]
ans=0
for i in range(N):
ans+=max(LEFT[i],RIGHT[N-1-i])
print(ans) | S = input()
lst = []
cnt = 0
if S[0] == '<':
flg = 0
else:
flg = 1
for i in range(len(S)):
if flg == 0:
if S[i] == '<':
cnt += 1
elif S[i] == '>':
lst.append(['<',cnt])
cnt = 1
flg = 1
elif flg == 1:
if S[i] == '<':
lst.append(['>', cnt])
cnt = 1
flg = 0
elif S[i] == '>':
cnt += 1
if flg == 0:
lst.append(['<',cnt])
else:
lst.append(['>', cnt])
#print(lst)
ans = 0
for i in range(1,len(lst)):
if lst[i-1][0] == '<' and lst[i][0] == '>':
n = max(lst[i-1][1],lst[i][1])
n2 = min(lst[i-1][1],lst[i][1])
ans += n * (n+1) /2
ans += (n2-1)*n2 / 2
if lst[0][0] == '>':
n3 = lst[0][1]
ans += n3 * (n3 + 1) / 2
if lst[-1][0] == '<':
n4 = lst[-1][1]
ans += n4 * (n4+1) /2
print(int(ans)) | 1 | 156,679,989,901,204 | null | 285 | 285 |
import math
import itertools
from fractions import gcd
N = int(input())
A = list(map(int, input().split()))
mod = 10**9+7
color = [0]*3
ans = 1
for i in range(N):
if A[i] not in color:
ans = 0
break
ind = color.index(A[i])
ans *= len([c for c in color if c == A[i]])
ans %= mod
color[ind] += 1
#ans = rec(color,0)
print(ans) | a, b, k = list(map(int, input().split(' ')))
print(max(0, a-k), max(0, b-max(0, k-a))) | 0 | null | 117,537,051,547,820 | 268 | 249 |
N = int(input())
cc = map(int,input().split())
min_num = N
count = 0
for i in cc:
if i <= min_num:
min_num = i
count += 1
print(count) | import math
r = float(input())
s = r * r * math.pi
l = r * 2 * math.pi
print('{:f} {:f}'.format(s, l)) | 0 | null | 42,946,646,044,108 | 233 | 46 |
turn = int(input())
tp, hp = 0, 0
for i in range(turn):
t, h = input().split()
if t == h:
tp += 1
hp += 1
elif t > h:
tp += 3
elif t < h:
hp += 3
print(tp, hp)
| # -*- coding: utf-8 -*-
n = int(input())
t_point, h_point = 0, 0
for i in range(n):
t_card, h_card = map(str, input().split())
if t_card == h_card:
t_point += 1
h_point += 1
elif t_card > h_card:
t_point += 3
elif t_card < h_card:
h_point += 3
print('{0} {1}'.format(t_point, h_point))
| 1 | 1,997,485,966,642 | null | 67 | 67 |
#!python3
import sys
from collections import defaultdict
from math import gcd
iim = lambda: map(int, input().rstrip().split())
def resolve():
N = input()
it = map(int, sys.stdin.read().split())
mod = 10**9 + 7
d1 = [defaultdict(int), defaultdict(int)]
z0 = za = zb = 0
for ai, bi in zip(it, it):
t = ai * bi
if t == 0:
if ai != 0:
za += 1
elif bi != 0:
zb += 1
else:
z0 += 1
continue
ci = gcd(ai, bi)
ai, bi = ai // ci, bi // ci
t = t < 0
if ai < 0:
ai, bi = -ai ,-bi
d1[t][(ai, bi)] += 1
a1, a2 = d1
ans = 1
z1 = 0
for (ai, bi), v in a1.items():
k2 = (bi, -ai)
if k2 in a2:
v2 = a2[k2]
ans *= pow(2, v, mod) + pow(2, v2, mod) - 1
ans %= mod
else:
z1 += v
for (ai, bi), v in a2.items():
k2 = (-bi, ai)
if k2 in a1: continue
z1 += v
ans *= pow(2, za, mod) + pow(2, zb, mod) - 1
ans %= mod
ans *= pow(2, z1, mod)
print((ans-1+z0) % mod)
if __name__ == "__main__":
resolve()
| import math
MOD = 10**9 + 7
n = int(input())
mp = {}
ans1 = 0
def process(lst):
a = lst[0]
b = lst[1]
gcd = math.gcd(a, b)
a //= gcd
b //= gcd
if b < 0:
a *= -1
b *= -1
a = abs(a) if b == 0 else a
b = abs(b) if a == 0 else b
lst[0] = a
lst[1] = b
return lst
for _ in range(n):
a, b = map(int, input().split())
if a == 0 and b == 0:
ans1 += 1
continue
a, b = process([a, b])
mp.setdefault((a, b), 0)
mp[(a, b)] += 1
a, b = process([-b, a])
y = mp.setdefault( (a, b), 0)
ans = 1
taken = {}
for a, b in mp:
if taken.get((a, b), 0):
continue
taken.setdefault((a, b), 0)
taken[(a, b)] = 1
x = mp.get( (a, b), 0)
# print(a, b, x, end=" + ")
a, b = process([-b, a])
y = mp.get( (a, b), 0)
taken.setdefault((a, b), 0)
taken[(a, b)] = 1
# print(a, b, y)
ans = (ans * ( pow(2, x, MOD) + pow(2, y, MOD) - 1 + MOD) % MOD ) % MOD
# print(ans)
ans = ( ans - 1 + ans1 + MOD ) % MOD
print(ans)
| 1 | 20,887,721,609,510 | null | 146 | 146 |
import itertools
h, w, k = map(int, input().split())
c = []
ans = 0
for i in range(h):
c.append(list(input()))
for i in range(2 ** h - 1):
c2 = []
x = str(bin(i))[2:].zfill(h)
for a in range(h):
if x[a] == '0':
c2.append(c[a])
elif x[a] == '1':
c2.append(['*'] * w)
black = list(itertools.chain.from_iterable(c2)).count('#')
for j in range(2 ** w - 1):
black2 = black
y = str(bin(j))[2:].zfill(w)
for b in range(w):
if y[b] == '1':
for a in range(h):
if c2[a][b] == '#':
black2 -= 1
if black2 == k:
ans += 1
print(ans) | def main():
s = input()
s1 = s[:int((len(s)-1)/2)]
s2 = s[int((len(s)+3)/2-1):]
rs = s[::-1]
rs1 = s1[::-1]
rs2 = s2[::-1]
if s1 == rs1 and s2 == rs2 and s == rs:
print("Yes")
else:
print("No")
return
main()
| 0 | null | 27,660,082,360,100 | 110 | 190 |
import bisect
n = int(input())
A = tuple(map(int,input().split()))
q = int(input())
Q = tuple(map(int, input().split()))
dp = [0]*(2**n)
for i in range(n):
for j in range(1<<i):
dp[j+(1<<i)] = dp[j] + A[i]
dp.sort()
for n in Q:
i = bisect.bisect(dp, n)
print("yes" if i>0 and n==dp[i-1] else "no") | mod_val = 10**9+7
n, k = map(int, input().split())
factorials = [1]*(n+1) # values 0 to n
for i in range(2, n+1):
factorials[i] = (factorials[i-1]*i)%mod_val
def mod_binomial(a, b):
numerator = factorials[a]
denominator = (factorials[b]*factorials[a-b])%mod_val
invert = pow(denominator, mod_val-2, mod_val)
return (numerator*invert)%mod_val
partial = 0
# m is number of rooms with no people
for m in range(min(k+1, n)):
# m places within n to place the 'no people' rooms
# put n-(n-m) people in n-m rooms (n-m) must be placed to be non-empty
partial = (partial + (mod_binomial(n, m) * mod_binomial(n-1, m))%mod_val)%mod_val
print(partial) | 0 | null | 33,508,041,159,390 | 25 | 215 |
N, X, Y = map(int, input().split())
ans = [0]*(N-1)
def cal_dis(i, j):
return min(j-i, abs(i-X)+abs(j-Y)+1)
for i in range(1, N+1):
for j in range(i+1, N+1):
k = cal_dis(i, j)
ans[k-1] += 1
print('\n'.join(map(str, ans)))
| X,K,D = map(int, input().split())
X = abs(X)
if X//D > K:
print(X-(D*K))
exit()
if (K-X//D)%2:
print(D-X%D)
else:
print(X%D) | 0 | null | 24,776,875,110,440 | 187 | 92 |
N = int(input())
S = list(map(int,input().split()))
B= list()
X = list()
for i in S:
if i % 2== 0:
B.append(i)
for n in B:
if n % 3 == 0 or n % 5 == 0:
X.append(n)
if len(B) == len(X):
print('APPROVED')
else:
print('DENIED')
| n=int(input())
a=list(map(int,input().split()))
l=[]
flag=0
for aa in a:
if aa%2==0:
flag=1
l.append(aa)
if flag==1:
for ll in l:
if ll % 3!=0 and ll % 5!=0:
flag=2
if flag==0:
print('APPROVED')
elif flag==1:
print('APPROVED')
elif flag==2:
print('DENIED')
| 1 | 69,132,746,477,350 | null | 217 | 217 |
import collections
N = int(input())
ls = list(map(int,input().split()))
count = collections.Counter(ls)
for i in range(1,N+1):
if i in count.keys():
print(count[i])
else:
print(0) | n=int(input());print('NYOE S'[len(set(map(int,input().split())))==n::2]) | 0 | null | 53,426,861,260,580 | 169 | 222 |
dp = [0 for i in range(1000000)]
N = int(input())
for i in range(2,1000):
if i**2>N:
break
if dp[i]!=0:
continue
for j in range(i,1000000,i):
dp[j] = 1
while dp[N]!=0:
N += 1
print(N) | n = int(input())
a = []
for i in range(n):
s = str(input())
a.append(s)
b = set(a)
print(len(b)) | 0 | null | 67,638,444,127,942 | 250 | 165 |
x,y=map(int,input().split())
c=0
d=0
if x==1:
c=300000
elif x==2:
c=200000
elif x==3:
c=100000
if y==1:
d=300000
elif y==2:
d=200000
elif y==3:
d=100000
print(c+d if c+d!=600000 else 1000000) | N,M=[int(i) for i in input().split()]
ans=0
for i in [N,M]:
if i==1:
ans += 3
elif i==2:
ans += 2
elif i==3:
ans+=1
if N==1 and M==1:
ans+=4
print(ans*100000) | 1 | 140,394,451,197,132 | null | 275 | 275 |
import numpy as np
N = int(input())
N_List = list(map(int,input().split()))
ans = (100**2)*100
for i in range(1,101):
ca = sum(map(lambda x:(x-i)**2,N_List))
if ca < ans:
ans = ca
print(ans)
| # coding: utf-8
# Your code here!
n=int(input())
x=list(map(int,input().split()))
cost_min=1000000000
for i in range(1,101):
cost=0
for j in x:
cost+=(j-i)**2
if cost>=cost_min:
continue
cost_min=cost
print(cost_min) | 1 | 65,291,332,584,880 | null | 213 | 213 |
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall
def solve():
N, M, L = map(int, input().split())
dist = [[0 for _ in range(N)] for _ in range(N)]
for i in range(M):
a, b, c = map(int, input().split())
dist[a-1][b-1] = c
dist[b-1][a-1] = c
fw = floyd_warshall(csr_matrix(dist))
ok = [[0 for _ in range(N)] for _ in range(N)]
for i in range(N):
for j in range(N):
if fw[i][j] <= L:
ok[i][j] = 1
ans = floyd_warshall(csr_matrix(ok))
Q = int(input())
for i in range(Q):
s, t = map(lambda x: int(x)-1, input().split())
if ans[s][t] < N:
print(int(ans[s][t])-1)
else:
print(-1)
if __name__ == '__main__':
solve() | #input
N, M, L = map(int, input().split())
A = [0] * M
B = [0] * M
C = [0] * M
for i in range(M):
A[i], B[i], C[i] = map(int, input().split())
Q = int(input())
s = [0] * Q
t = [0] * Q
for i in range(Q):
s[i], t[i] = map(int, input().split())
#output
from scipy.sparse.csgraph import floyd_warshall, shortest_path, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix
import numpy as np
#便宜上A, Bを-1する。
for i in range(M):
A[i] -= 1
B[i] -= 1
#FW法で各経路の最短距離を計算する。
graph = csr_matrix((C, (A, B)), shape = (N, N))
dist_matrix = floyd_warshall(csgraph = graph, directed = False, return_predecessors = False)
graph2 = np.full((N, N), np.inf)
graph2[dist_matrix <= L] = 1
graph2 = csr_matrix(graph2)
dist_matrix2 = floyd_warshall(csgraph = graph2, directed = False, return_predecessors = False)
for q in range(Q):
if dist_matrix2[s[q]-1][t[q]-1] < 10000000:
print(int(dist_matrix2[s[q]-1][t[q]-1]-1))
else:
print(-1) | 1 | 173,394,333,099,102 | null | 295 | 295 |
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007
def main():
N, *A = map(int, read().split())
M = 61
C = [0] * M
for a in A:
a = str(format(a, 'b'))
for i, d in enumerate(reversed(a)):
if d == '1':
C[i] += 1
ans = 0
p = 1
for c in C:
ans = (ans + c * (N - c) * p) % MOD
p = p * 2 % MOD
print(ans)
return
if __name__ == '__main__':
main()
| n=int(input())
a=list(map(int,input().split()))
mod=10**9+7
ans=0
for i in range(60):
on=0
off=0
for j in a:
if (j>>i)&1:
on+=1
else:
off+=1
ans+=(on*off)*(2**i)
ans%=mod
print(ans) | 1 | 122,881,650,496,790 | null | 263 | 263 |
n=input()
c=[]
A=[]
for i in ['S ','H ','C ','D ']:
for j in map(str,range(1,14)):
A.append(i+j)
for i in range(n):
A.remove(raw_input())
for i in range(len(A)):
print A[i] | import sys
input()
card = sys.stdin.readlines()
bool_ = [[False for y in range(13)] for x in range(4)]
for i in card:
m, n = i.split()
m = m.replace('S', '0').replace('H', '1').replace('C', '2').replace('D', '3')
m, n = int(m), int(n)
bool_[m][n - 1] = True
for i in range(4):
for j in range(13):
if not bool_[i][j]:
if i == 0:
print('S', j + 1)
elif i == 1:
print('H', j + 1)
elif i == 2:
print('C', j + 1)
else:
print('D', j + 1) | 1 | 1,039,338,552,658 | null | 54 | 54 |
n = int(input())
a = sorted(list(map(int, input().split())))
ans = a.pop()
if n % 2:
for i in range((n - 2) // 2):
ans += a.pop() * 2
ans += a.pop()
print(ans)
else:
for i in range((n - 2) // 2):
ans += a.pop() * 2
print(ans) | n=int(input())
a=list(map(int,input().split()))
b=a+a
b.sort(reverse=True)
ans=0
for i in range(1,n):
ans+=b[i]
print(ans) | 1 | 9,120,672,884,540 | null | 111 | 111 |
import sys
input = sys.stdin.readline
N = int(input())
S = input().rstrip()
Q = int(input())
qs = [input().split() for i in range(Q)]
class BinaryIndexedTree:
def __init__(self,size):
self.N = size
self.bit = [0]*(size+1)
def add(self,x,w): # 0-indexed
x += 1
while x <= self.N:
self.bit[x] += w
x += (x & -x)
def _sum(self,x): # 1-indexed
ret = 0
while x > 0:
ret += self.bit[x]
x -= (x & -x)
return ret
def sum(self,l,r): # [l,r)
return self._sum(r) - self._sum(l)
def __str__(self): # for debug
arr = [self.sum(i,i+1) for i in range(self.N)]
return str(arr)
bits = [BinaryIndexedTree(N+1) for i in range(26)]
for i,c in enumerate(S):
ci = ord(c) - ord('a')
bits[ci].add(i,1)
now = list(S)
for a,b,c in qs:
if a=='1':
b = int(b)
pi = ord(now[b-1]) - ord('a')
bits[pi].add(b-1, -1)
ci = ord(c) - ord('a')
bits[ci].add(b-1, 1)
now[b-1] = c
else:
b,c = int(b),int(c)
cnt = 0
for i in range(26):
cnt += int(bits[i].sum(b-1,c) > 0)
print(cnt) | from collections import deque
class SegmentTree():
def __init__(self,n,ide_ele,merge_func,init_val):
self.n=n
self.ide_ele=ide_ele
self.merge_func=merge_func
self.val=[0 for i in range(1<<n)]
self.merge=[0 for i in range(1<<n)]
self.parent=[-1 for i in range(1<<n)]
deq=deque([1<<(n-1)])
res=[]
while deq:
v=deq.popleft()
res.append(v)
if not v&1:
gap=(v&-v)//2
self.parent[v-gap]=v
deq.append(v-gap)
self.parent[v+gap]=v
deq.append(v+gap)
for v in res[::-1]:
if v-1<len(init_val):
self.val[v-1]=init_val[v-1]
self.merge[v-1]=self.val[v-1]
if not v&1:
gap=(v&-v)//2
self.merge[v-1]=self.merge_func(self.merge[v-1],self.merge[v-gap-1],self.merge[v+gap-1])
def update(self,id,x):
self.val[id]=x
pos=id+1
while pos!=-1:
if pos&1:
self.merge[pos-1]=self.val[pos-1]
else:
gap=(pos&-pos)//2
self.merge[pos-1]=self.merge_func(self.val[pos-1],self.merge[pos+gap-1],self.merge[pos-gap-1])
pos=self.parent[pos]
def cnt(self,k):
lsb=(k)&(-k)
return (lsb<<1)-1
def lower_kth_merge(self,nd,k):
res=self.ide_ele
id=nd
if k==-1:
return res
while True:
if not id%2:
gap=((id)&(-id))//2
l=id-gap
r=id+gap
cnt=self.cnt(l)
if cnt<k:
k-=cnt+1
res=self.merge_func(res,self.val[id-1],self.merge[l-1])
id=r
elif cnt==k:
res=self.merge_func(res,self.val[id-1],self.merge[l-1])
return res
else:
id=l
else:
res=self.merge_func(res,self.val[id-1])
return res
def upper_kth_merge(self,nd,k):
res=self.ide_ele
id=nd
if k==-1:
return res
while True:
if not id%2:
gap=((id)&(-id))//2
l=id-gap
r=id+gap
cnt=self.cnt(r)
if cnt<k:
k-=cnt+1
res=self.merge_func(res,self.val[id-1],self.merge[r-1])
id=l
elif cnt==k:
res=self.merge_func(res,self.val[id-1],self.merge[r-1])
return res
else:
id=r
else:
res=self.merge_func(res,self.val[id-1])
return res
def query(self,l,r):
id=1<<(self.n-1)
while True:
if id-1<l:
id+=((id)&(-id))//2
elif id-1>r:
id-=((id)&(-id))//2
else:
res=self.val[id-1]
if id%2:
return res
gap=((id)&(-id))//2
L,R=id-gap,id+gap
#print(l,r,id,L,R)
left=self.upper_kth_merge(L,id-1-l-1)
right=self.lower_kth_merge(R,r-id)
return self.merge_func(res,left,right)
ide_ele=0
def seg_func(*args):
res=ide_ele
for val in args:
res|=val
return res
def popcount(x):
x = x - ((x >> 1) & 0x55555555)
x = (x & 0x33333333) + ((x >> 2) & 0x33333333)
x = (x + (x >> 4)) & 0x0f0f0f0f
x = x + (x >> 8)
x = x + (x >> 16)
return x & 0x0000007f
import sys
input=sys.stdin.readline
N=int(input())
S=input().rstrip()
init_val=[1<<(ord(S[i])-97) for i in range(N)]
test=SegmentTree(19,ide_ele,seg_func,init_val)
for _ in range(int(input())):
t,l,r=input().split()
t,l=int(t),int(l)
if t==1:
val=ord(r)-97
test.update(l-1,1<<val)
else:
r=int(r)
res=test.query(l-1,r-1)
print(popcount(res)) | 1 | 62,374,995,064,420 | null | 210 | 210 |
from collections import deque
n,m = map(int, input().split())
graph = [[] for _ in range(n+1)]
dist = [0] * (n+1)
visited = [0] * (n+1)
for _ in range(m):
a,b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
root = 1
visited[root] = 1
d = deque([root])
while d:
v = d.popleft()
for i in graph[v]:
if visited[i]:
continue
visited[i] = 1
dist[i] = v
d.append(i)
print('Yes')
ans = dist[2:]
print(*ans, sep="\n") | def main():
N = int(input())
S = input()
dict = {'R':0, 'B':0, 'G':0}
for i in range(N):
dict[S[i]] += 1
ans = dict['R']*dict['B']*dict['G']
for i in range(N-2):
if (N-i)%2 == 0:
tmp = int((N-i)/2)-1
else:
tmp = (N-i)//2
for j in range(1,tmp+1):
if S[i]!=S[i+j] and S[i]!=S[i+2*j] and S[i+j]!=S[i+2*j]:
ans = ans - 1
return ans
print(main())
| 0 | null | 28,517,087,326,140 | 145 | 175 |
MOD = 10**9 + 7
P = 7 ** 21 + 52
def F(a, b):
return (a * pow(b, P-2, P)) % P
def main():
from collections import Counter
import sys
input = sys.stdin.buffer.readline
N = int(input())
T = [[int(i) for i in input().split()] for j in range(N)]
cnt_zero = [0, 0] # 片方が0のとき
p = 0 # 両方0
cA = Counter()
B = []
used = set()
for a, b in T:
if a == 0 and b == 0:
p += 1
elif a == 0:
cnt_zero[0] += 1
elif b == 0:
cnt_zero[1] += 1
else:
x = F(a, b)
y = -F(b, a) % P
cA[x] += 1
if x not in used and y not in used:
B.append((x, y))
used.add(x)
used.add(y)
ans = 1
ans *= pow(2, cnt_zero[0], MOD) - 1 + pow(2, cnt_zero[1], MOD) - 1 + 1
for a, b in B:
ans *= pow(2, cA[a], MOD) - 1 + pow(2, cA[b], MOD) - 1 + 1
ans %= MOD
print((ans + p - 1) % MOD)
if __name__ == '__main__':
main()
| N = int(input())
c = list(str(input()))
r = c.count('R')
w = 0
for i in range(r):
if c[i] == 'W':
w += 1
print(w) | 0 | null | 13,546,836,269,120 | 146 | 98 |
input_nums = map(int,raw_input().split())
men = input_nums[0] * input_nums[1]
syu = (input_nums[0] * 2) + (input_nums[1] * 2)
print "{0} {1}".format(men,syu) | #coding:utf-8
#1_1_D
n = int(input())
prices = [int(input()) for x in range(n)]
maxv = -2 * 10 ** 9
minv = prices[0]
for i in range(1, n):
maxv = max(maxv, prices[i] - minv)
minv = min(minv, prices[i])
print(maxv) | 0 | null | 154,945,416,252 | 36 | 13 |
import math
n,x,t = map(int,input().strip().split(" "))
ans = math.ceil(n/x)
ans = ans*t
print(ans) | n, k = map(int, input().split())
l = list(int(input()) for i in range(n))
left = max(l)-1; right = sum(l)
while left+1 < right: #最大値と合計値の間のどこかに考えるべき値が存在する
mid = (left + right) // 2
cnt = 1; cur = 0 #初期化
for a in l:
if mid < cur + a:
cur = a
cnt += 1
else:
cur += a
if cnt <= k:
right = mid
else:
left = mid
print(right)
| 0 | null | 2,156,329,389,210 | 86 | 24 |
A,B,C,K=map(int,input().split())
if K<=A:
print(K)
elif K<=A+B:
print(A)
else:
m=K-(A+B)
print(A-m) | A, B, C, K = map(int, input().split())
num = 0
if K - A <= 0:
num = K
else:
K -= A
num = A
if K - B > 0:
K -= B
num += (-1)*K
print(num) | 1 | 21,756,676,940,470 | null | 148 | 148 |
def main():
a, b = input().split()
if a < b:
ans = a * int(b)
else:
ans = b * int(a)
print(ans)
if __name__ == '__main__':
main()
| def gcd(s,t):
if t==0:
return s
else:
return gcd(t, s%t)
s, t = [int(x) for x in input().split()]
GCD = gcd(s,t)
LCM = (s*t)//GCD
print(LCM) | 0 | null | 98,614,692,063,718 | 232 | 256 |
a = int(input())
ans = 0
if 1 <= a <= 10:
ans = a + a*a + a**3
print(int(ans)) | def main(a):
return ((a + 1)*a + 1)*a
if __name__ == '__main__':
a = int(input())
ans = main(a)
print(ans)
| 1 | 10,248,993,895,342 | null | 115 | 115 |
a = list(map(int, '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'.split(',')))
k = int(input())
print(a[k-1]) | def main():
ary = '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'
ans = list(map(int, ary.split(', ')))
a = int(input()) - 1
print(ans[a])
if __name__ == '__main__':
main()
| 1 | 49,922,206,840,100 | null | 195 | 195 |
k, n = map(int,input().split())
a = list(map(int,input().split()))
da =[-1] * n
da[n-1] = k - a[n-1] + a[0]
for i in range(n-1):
da[i] = a[i+1] - a[i]
print(k-max(da)) | def resolve():
k,n = map(int,input().split())
a = list(map(int,input().split()))
l = a[0]+k-a[-1]
for i in range(1,n):
l = max(l,a[i]-a[i-1])
print(k-l)
resolve() | 1 | 43,458,715,577,180 | null | 186 | 186 |
a, b, c = list(map(int, input().split()))
k = int(input())
for i in range(k):
if a >= b:
b *= 2
else:
c *= 2
if a < b and b < c:
print('Yes')
else:
print('No') | def resolve():
x = int(input())
dp = [0] * (x+1+105)
dp[0] = 1
for i in range(x):
if dp[i] == 1:
for j in range(100, 106):
dp[i+j] = 1
print(dp[x])
resolve() | 0 | null | 67,370,288,657,270 | 101 | 266 |
import sys
K = int(sys.stdin.readline())
M = len(sys.stdin.readline())-1
N = K+M
mod = 10**9+7
ans = pow(26, N, mod)
class Data():
def __init__(self):
self.power = 1
self.rev = 1
class Combi():
def __init__(self, N, mod):
self.lists = [Data() for _ in range(N+1)]
self.mod = mod
for i in range(2, N+1):
self.lists[i].power = ((self.lists[i-1].power)*i) % self.mod
self.lists[N].rev = pow(self.lists[N].power, self.mod-2, self.mod)
for j in range(N, 0, -1):
self.lists[j-1].rev = ((self.lists[j].rev)*j) % self.mod
def combi(self, K, R):
if K < R:
return 0
else:
return ((self.lists[K].power)*(self.lists[K-R].rev)*(self.lists[R].rev)) % self.mod
X = Combi(N, mod)
for i in range(M):
ans -= X.combi(N, i)*pow(25, N-i, mod)
ans %= mod
print(ans) | n = int(input())
if n & 1:
print((n - 1) // 2)
else:
print(n // 2 - 1) | 0 | null | 82,996,011,180,642 | 124 | 283 |
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)
class Combination:
def __init__(self, n_max, mod=10**9+7):
# O(n_max + log(mod))
self.mod = mod
f = 1
self.fac = fac = [f]
for i in range(1, n_max+1):
f = f * i % mod
fac.append(f)
f = pow(f, mod-2, mod)
self.facinv = facinv = [f]
for i in range(n_max, 0, -1):
f = f * i % mod
facinv.append(f)
facinv.reverse()
# "n 要素" は区別できる n 要素
# "k グループ" はちょうど k グループ
def __call__(self, n, r): # self.C と同じ
return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod
def C(self, n, r):
if not 0 <= r <= n:
return 0
return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod
def P(self, n, r):
if not 0 <= r <= n:
return 0
return self.fac[n] * self.facinv[n-r] % self.mod
def H(self, n, r):
if (n == 0 and r > 0) or r < 0:
return 0
return self.fac[n+r-1] * self.facinv[r] % self.mod * self.facinv[n-1] % self.mod
def rising_factorial(self, n, r): # 上昇階乗冪 n * (n+1) * ... * (n+r-1)
return self.fac[n+r-1] * self.facinv[n-1] % self.mod
def stirling_first(self, n, k): # 第 1 種スターリング数 lru_cache を使うと O(nk) # n 要素を k 個の巡回列に分割する場合の数
if n == k:
return 1
if k == 0:
return 0
return (self.stirling_first(n-1, k-1) + (n-1)*self.stirling_first(n-1, k)) % self.mod
def stirling_second(self, n, k): # 第 2 種スターリング数 O(k + log(n)) # n 要素を区別のない k グループに分割する場合の数
if n == k:
return 1 # n==k==0 のときのため
return self.facinv[k] * sum((-1)**(k-m) * self.C(k, m) * pow(m, n, self.mod) for m in range(1, k+1)) % self.mod
def balls_and_boxes_3(self, n, k): # n 要素を区別のある k グループに分割する場合の数 O(k + log(n))
return sum((-1)**(k-m) * self.C(k, m) * pow(m, n, self.mod) for m in range(1, k+1)) % self.mod
def bernoulli(self, n): # ベルヌーイ数 lru_cache を使うと O(n**2 * log(mod))
if n == 0:
return 1
if n % 2 and n >= 3:
return 0 # 高速化
return (- pow(n+1, self.mod-2, self.mod) * sum(self.C(n+1, k) * self.bernoulli(k) % self.mod for k in range(n))) % self.mod
def faulhaber(self, k, n): # べき乗和 0^k + 1^k + ... + (n-1)^k
# bernoulli に lru_cache を使うと O(k**2 * log(mod)) bernoulli が計算済みなら O(k * log(mod))
return pow(k+1, self.mod-2, self.mod) * sum(self.C(k+1, j) * self.bernoulli(j) % self.mod * pow(n, k-j+1, self.mod) % self.mod for j in range(k+1)) % self.mod
def lah(self, n, k): # n 要素を k 個の空でない順序付き集合に分割する場合の数 O(1)
return self.C(n-1, k-1) * self.fac[n] % self.mod * self.facinv[k] % self.mod
def bell(self, n, k): # n 要素を k グループ以下に分割する場合の数 O(k**2 + k*log(mod))
return sum(self.stirling_second(n, j) for j in range(1, k+1)) % self.mod
n, m, k = map(int, read().split())
mod = 998244353
comb = Combination(n, mod)
ans = 0
n -= 1
for i in range(k + 1):
ans += comb.C(n, n - i) * m * pow(m - 1, n - i, mod) % mod
ans %= mod
print(ans)
| def combs_mod(n,k,mod):
#nC0からnCkまで
inv = [1]*(k+1)
for i in range(1,k+1):
inv[i] = pow(i,mod-2,mod)
ans = [1]*(k+1)
for i in range(1,k+1):
ans[i] = ans[i-1]*(n+1-i)*inv[i]%mod
return ans
def solve():
ans = 0
mod = 998244353
N, M, K = map(int, input().split())
top = min(K,N-1)
combs = combs_mod(N-1,top,mod)
for k in range(top+1):
ans += M*combs[k]*pow(M-1,N-1-k,mod)
ans %= mod
return ans
print(solve()) | 1 | 23,199,064,290,012 | null | 151 | 151 |
from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations # (string,3) 3回
from collections import deque
from collections import defaultdict
import bisect
#
# d = m - k[i] - k[j]
# if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも!!
#
#
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
def readInts():
return list(map(int,input().split()))
def main():
a,b = readInts()
print(a - b*2 if a - b*2 >= 0 else 0)
if __name__ == '__main__':
main()
|
fibo_list=[0 for i in range(48)]
def fibo(n):
f=fibo_list[n]
if f:
return f
else:
if n==0 or n==1:
return 1
else:
f=fibo(n-1)+(fibo(n-2))
fibo_list[n]=f
return f
n=input()
print(fibo(n)) | 0 | null | 82,951,259,303,610 | 291 | 7 |
import sys
def main():
N = int(input())
P = list(map(int, input().split()))
min_so_far = sys.maxsize
ans = 0
for p in P:
if p <= min_so_far:
ans += 1
min_so_far = p
print(ans)
if __name__ == '__main__':
main()
| n = int(input())
p = list(map(int, input().split()))
count = 0
for i in range(n) :
if i == 0 :
mn = p[i]
count += 1
else :
if mn > p[i] :
mn = p[i]
count += 1
print(count)
| 1 | 85,819,888,188,718 | null | 233 | 233 |
# -*- coding: utf-8 -*-
import sys
from sys import stdin
import math
n, k = map(int, stdin.readline().split())
w = list([int(stdin.readline().rstrip()) for i in range(n)])
def main2():
def is_ok():
cnt_track = w_tmp = 0
for w_i in w:
w_tmp += w_i
if w_tmp > m:
w_tmp = w_i
cnt_track += 1
if cnt_track >= k:
return 0
return 1
r = sum(w)
l = max(math.ceil(r/k),max(w))
while l < r:
m = (l + r) // 2
# print("%d %d %d " % (l, r, m))
if is_ok():
r = m
else:
l = m + 1
print(r)
if __name__ == '__main__':
main2()
| import math
while True:
n=int(input())
if n==0:
break
s=list(map(int,input().split()))
m=sum(s)/n
aa=0
for i in s:
aa+= (i-m)**2
a=math.sqrt((aa)/n)
print(f'{a:.08f}')
| 0 | null | 136,314,167,990 | 24 | 31 |
N = int(input())
C = input()
answer = 0
for i in range(N):
if C[i] == 'W':
red_start = i
break
if i == N-1:
red_start = N-1
for i in range(1, N+1):
if C[-i] == 'R':
white_start = i
break
if i == N:
white_start = N
red_pos = red_start
white_pos = white_start
while red_pos + white_pos < N:
answer += 1
for i in range(red_pos+1, N):
if C[i] == 'W':
red_pos = i
break
if i == N-1:
red_pos = N-1
for i in range(white_pos+1, N+1):
if C[-i] == 'R':
white_pos = i
break
if i == N:
white_pos = N
print(answer) | N,K = map(int,input().split())
A = list(map(int,input().split()))
G = [1]
for n in range(N-1):
G.append(A[G[n]-1])
if len(G)<K:
a = G.index(G[-1])
b = len(G)-a-1
K = (K-a)%b+a
print(G[K]) | 0 | null | 14,427,750,811,500 | 98 | 150 |
max_row, max_col = map(int, input().split(" "))
rows = [[0 for i in range(max_col)] for j in range(max_row)]
row = ""
for i in range(max_row):
row = input().split(" ")
total_row = 0
for j in range(max_col):
rows[i][j] = int(row[j])
total_row = total_row + int(row[j])
rows[i].append(total_row)
#get summery of each columns
total = [0 for i in range(max_col+1)]
for m in range(max_col+1):
for n in range(max_row):
total[m] = total[m] + rows[n][m]
rows.append(total)
for i in range(max_row+1):
out_str = ""
for j in range(max_col+1):
out_str = out_str + " " + str(rows[i][j])
print(out_str.strip()) | l = input().split()
r = int(l[0])
c = int(l[1])
a = []
for i in range(r):
a.append(list(map(int, input().split())))
for i in range(len(a)):
a[i].insert(c, sum(a[i]))
b = []
a.append(b)
for i in range(c): #4
s = 0
for j in range(r): #5
s += a[j][i]
a[-1].append(s)
a[-1].append(sum(a[-1]))
for i in range(len(a)):
print(*a[i]) | 1 | 1,375,801,644,030 | null | 59 | 59 |
import bisect
N = int(input())
A = [int(x) for x in input().split()]
A=sorted(A)
index = 0
for i in range(1, N+1):
now = bisect.bisect_right(A, i)
print(now - index)
index = now
| import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]
#本当に貪欲法か? DP法では??
#本当に貪欲法か? DP法では??
#本当に貪欲法か? DP法では??
mod=998244353
n=ii()
arr=limii()
if arr[0]!=0:
print(0)
exit()
if n==1 and arr[0]==0:
print(1)
exit()
chk=True
p=Counter(arr)
if p[0]!=1:
print(0)
exit()
tmp=0
for i in range(1,max(list(p))+1):
if p[i]>0:
pass
else:
print(0)
exit()
ans=1
#掛け算の場合
# (a*b)%mod と同等
def mul(a, b):
return ((a % mod) * (b % mod)) % mod
#割り算の場合
# (a//b)%mod と同等
def div(a,b):
return a*pow(b, mod-2, mod)
#累乗の場合
#a**n %p と同等
def p_f(a,n,pp):
bi=str(format(n,"b"))#2進表現に
res=1
for i in range(len(bi)):
res=(res*res) %pp
if bi[i]=="1":
res=(res*a) %pp
return res
for i in range(max(p.keys())):
ans=(ans*pow(p[i], p[i+1], mod)) %mod
print(ans%mod) | 0 | null | 93,252,290,598,092 | 169 | 284 |
a, b = map(int, input().split())
d = a // b
r = a % b
f = float(a / b)
print('{0} {1} {2:0.5f}'.format(d, r, f)) | n = int(raw_input())
F = [0] * (n+1)
def fibonacci(n):
global F
if n == 0 or n == 1:
F[n] = 1
return F[n]
if F[n] != 0:
return F[n]
F[n] = fibonacci(n-2) + fibonacci(n-1)
return F[n]
def makeFibonacci(n):
global F
F[0] = 1
F[1] = 1
for i in xrange(2,n+1):
F[i] = makeFibonacci(i-2) + makeFibonacci(i-1)
return F[n]
def main():
ans = fibonacci(n)
print ans
return 0
main() | 0 | null | 301,917,328,398 | 45 | 7 |
rooms = [[[0 for k in range(10)] for j in range(3)] for i in range(4)]
n = int(input())
for _ in range(n):
b, f, r, v = map(int, input().split())
rooms[b - 1][f - 1][r - 1] += v
for b in range(4):
for f in range(3):
print(' ' + ' '.join(map(str, rooms[b][f])))
if b is not 3:
print('####################') | def main():
INF = 10 ** 10
H, N = list(map(int, input().split(' ')))
magic = [list(map(int, input().split(' '))) for _ in range(N)]
MAX_A = max(list(map(lambda x: x[0], magic)))
dp = [INF for _ in range(MAX_A + H + 1)]
dp[0] = 0
for h in range(1, MAX_A + H + 1):
for i in range(N):
a, b = magic[i]
if h - a < 0:
continue
dp[h] = min(dp[h], b + dp[h - a])
print(min(dp[H:]))
if __name__ == '__main__':
main() | 0 | null | 41,089,104,921,018 | 55 | 229 |
a,b,c,d = map(int,input().split())
while True:
c = c - b
if c <= 0:
print("Yes")
break
a = a - d
if a <= 0:
print("No")
break
| A,B,C,D=map(int,input().split())
x=(A-1)//D+1
y=(C-1)//B+1
if x<y:
print("No")
else:
print("Yes")
| 1 | 29,599,963,530,192 | null | 164 | 164 |
import re
if re.match('[A-Z]', input()):
print('A')
else:
print('a') | import re
i = str(input())
j = i.islower()
if j == 1:
o = 'a'
else:
o = 'A'
print(o) | 1 | 11,259,591,418,702 | null | 119 | 119 |
h, a = map(int, input().split())
print(int((a + h - 1) / a)) | score = list(map(int,input().split()))
kaisu = 1
hp = score[0]
while hp > score[1]:
kaisu += 1
hp -= score[1]
print(kaisu) | 1 | 76,576,182,336,232 | null | 225 | 225 |
A,B=map(int,input().split())
print(0 if A-(B*2)<1 else A-(B*2)) | a = list(map(int,input().split()))
print(0 if a[0]-2*a[1]<=0 else a[0]-2*a[1]) | 1 | 166,964,622,910,660 | null | 291 | 291 |
import math
A,B,H,M = map(int,input().split())
arg_H = (360*H)/12+(30*M)/60
if M ==0:
arg_M = 360
else:
arg_M = (360*M)/60
HM = abs(arg_H-arg_M)
arg_HM = min(HM, 360-HM)
CM2 = A**2+B**2-2*A*B*math.cos(math.radians(arg_HM))
CM = math.sqrt(CM2)
print(CM) | import sys
for index, line in enumerate(sys.stdin):
value = int(line.strip())
if value == 0:
break
print("Case {0}: {1}".format(index+1, value)) | 0 | null | 10,235,680,406,148 | 144 | 42 |
import collections
N = int(input())
S = input()
S_list = [S[i] for i in range(N)]
S_c = collections.Counter(S_list)
list12 = []
list1 = []
ans = 0
for i in range(N-2):
s1 = S_list[i]
S_c[s1] = S_c[s1]-1
if s1 in list1:
continue
S_c23 = S_c.copy()
list1.append(s1)
for j in range(i+1,N-1):
s2 = S_list[j]
S_c23[s2] = S_c23[s2] - 1
if (s1+s2) in list12:
continue
else:
list12.append(s1+s2)
for key in S_c23.keys():
if S_c23[key] != 0:
ans+=1
print(ans) | import numpy as np
n, m, k = map(int, input().split())
li_a = np.array(list(map(int, input().split())))
li_b = np.array(list(map(int, input().split())))
a_i, b_i = np.hstack([np.array([0]),li_a.cumsum()]), np.hstack([np.array([0]),li_b.cumsum()])
j = m
ans = 0
for i in range(n+1):
if a_i[i]>k:
break
while(b_i[j] > k - a_i[i]):
j -= 1
ans = max(ans, i+j)
print(ans) | 0 | null | 69,926,532,067,178 | 267 | 117 |
a, b, m = map(int, input().split())
a_p = list(map(int, input().split()))
b_p = list(map(int, input().split()))
min_price = min(a_p) + min(b_p)
for i in range(m):
x, y, c = map(int, input().split())
min_price = min(min_price, a_p[x-1] + b_p[y-1] - c)
print(min_price) | a, b, m = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
AB = []
for _ in range(m):
x, y, c = map(int, input().split())
x -= 1
y -= 1
AB.append(A[x]+B[y]-c)
AB.sort()
A.sort()
B.sort()
print(min(AB[0], A[0]+B[0]))
| 1 | 54,105,903,512,388 | null | 200 | 200 |
#coding: utf-8
while True:
m, f, r = (int(i) for i in input().split())
if m == f == r == -1:
break
if m == -1 or f == -1:
print("F")
elif m + f >= 80:
print("A")
elif m + f >= 65 and m + f < 80:
print("B")
elif (m + f >= 50 and m + f < 65) or r >= 50:
print("C")
elif m + f >= 30 and m + f < 50:
print("D")
elif m + f < 30:
print("F")
| while True:
m,f,r = [int(x) for x in input().split()]
if (m,f,r)==(-1,-1,-1): break
s_mf = m + f
if m < 0 or f < 0: mark = 'F'
elif s_mf < 30: mark = 'F'
elif s_mf >= 80: mark = 'A'
elif s_mf >= 65: mark = 'B'
elif s_mf >= 50: mark = 'C'
elif r >= 50: mark = 'C'
else: mark = 'D'
print(mark) | 1 | 1,255,103,948,146 | null | 57 | 57 |
#!usr/bin/env python3
import sys
def string_to_list_spliter():
n = sys.stdin.readline()
lst = [int(i) for i in sys.stdin.readline().split()]
return lst
def main():
lst = string_to_list_spliter()
print(min(lst), max(lst), sum(lst))
if __name__ == '__main__':
main() | from itertools import product
n = int(input())
l1 = []
for _ in range(n):
l2 = []
for _ in range(int(input())):
l2.append(list(map(int, input().split())))
l1.append(l2)
ans = 0
for k in product([1, 0], repeat=n):
check = True
for i, j in enumerate(l1):
if k[i] == 0:
continue
for x in j:
if k[x[0] - 1] != x[1]:
check = False
if check:
ans = max(ans, sum(k))
print(ans) | 0 | null | 61,095,815,166,148 | 48 | 262 |
import numpy as np
class Combination :
def __init__ (self, N=10**6, mod=10**9+7) :
self.mod = mod
self.fact = [1, 1]
self.factinv = [1, 1]
self.inv = [0, 1]
for i in range(2, N+1) :
self.fact.append( (self.fact[-1] * i) % mod )
self.inv .append( (-self.inv[mod%i] * (mod//i) ) % mod )
self.factinv.append( (self.factinv[-1] * self.inv[-1]) % mod )
def nCr(self, n, r) :
if ( r < 0 ) or ( n < r ) : return 0
r = min(r, n-r)
return self.fact[n]*self.factinv[r]*self.factinv[n-r] % self.mod
def nPr(self, n, r) :
if ( r < 0 ) or ( n < r ) : return 0
return self.fact[n]*self.factinv[n-r] % self.mod
n, k = map(int, input().split())
mod = 10**9+7
comb = Combination(2*10**5+100)
ans = 0
for m in range(min(n+1,k+1)) :
ans = ans%mod + (comb.nCr(n,m) * comb.nCr(n-1, n-m-1))%mod
print(ans%mod)
| N ,K = map(int, input().split())
MOD = 10**9 + 7
ans = 0
S = [0]*(2*10**5+1)
S[1] = 1
for i in range(2, 2*10**5+1):
S[i] = S[MOD%i]*(MOD-int(MOD/i))%MOD
if N-1 <= K:
ans = 1
num = 2*N - 1
for i in range(N-1):
ans *= (num-i)*S[i+1]
ans %= MOD
print(ans%MOD)
else:
S1 = [1]*N
S2 = [1]*N
for i in range(1, N):
S1[i] = (S1[i-1]*(N+1-i)*S[i])%MOD
S2[i] = (S2[i-1]*(N-i)*S[i])%MOD
ans = 0
for i in range(K+1):
ans += S1[i]*S2[N-1-i]
ans %= MOD
print(ans)
# nCm * n-mHm
# nCm * n-1Cn-m-1 | 1 | 67,330,880,903,612 | null | 215 | 215 |