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
|
---|---|---|---|---|---|---|
mozi = input()
kazu = int(ord(mozi))
if 65 <= kazu < 91:
print('A')
else:
print('a')
|
K, N= map(int, input().split())
A = list(map(int, input().split()))
max=K-(A[N-1]-A[0])
for i in range(N-1):
a=A[i+1]-A[i]
if max<a:
max=a
print(K-max)
| 0 | null | 27,263,931,333,198 | 119 | 186 |
#! /usr/lib/python3
import fractions
while True:
try:
a, b=map(int, input().split())
s=fractions.gcd(a,b)
print("{0} {1:.0f}".format(s,a*b/s))
except:
break
|
def gcm(a, b):
if a<b: a,b = b,a
if (a%b ==0):
return b
else:
return gcm(b, a%b)
def lcm(a, b):
return a/gcm(a,b)*b
while True:
try:
a, b = map(int, raw_input().split())
except:
break
print "%d %d" %(gcm(a,b), lcm(a,b))
| 1 | 515,596,402 | null | 5 | 5 |
# D - Prediction and Restriction
N,K = map(int,input().split())
RSP = tuple(map(int,input().split()))
T = input()
# じゃんけんの勝敗を返す関数
def judge(a,b):
if a == 'r' and b == 2:
return True
elif a == 's' and b == 0:
return True
elif a == 'p' and b == 1:
return True
return False
# dp[i][j]: i 回目のじゃんけんで j を出したときの最大値
# グー:0, チョキ:1, パー:2
dp = [[0]*3 for _ in range(N+1)]
# v 回目のじゃんけん
for v in range(1,N+1):
# v 回目に出した手
for w in range(3):
# v-K 回目に出した手
for k in range(3):
if w == k:
continue
# じゃんけんで勝ったとき
if judge(T[v-1],w):
dp[v][w] = max(dp[v][w],dp[v-K][k] + RSP[w])
# じゃんけんで負けか引き分けたとき
else:
dp[v][w] = max(dp[v][w],dp[v-K][k])
# K 個に分けた各グループの部分和の最大値の総和を求める
ans = 0
for a in range(1,K+1):
ans += max(dp[-a])
print(ans)
|
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
t2 = [[] for _ in range(k)]
score = [0]*n
for i,c in enumerate(t):
t2[i%k].append(c)
score = {'r':p,'s':r,'p':s}
total = 0
for t3 in t2:
if len(t3)==0: continue
if len(t3)==1:
total += score[t3[0]]
continue
for i in range(len(t3)-1):
if t3[i] == 'e': continue
total += score[t3[i]]
if t3[i] == t3[i+1]:
t3[i+1]='e'
if t3[-1] != 'e':
total += score[t3[-1]]
print(total)
| 1 | 106,671,620,968,660 | null | 251 | 251 |
from collections import Counter
n=int(input())
A=list(map(int,input().split()))
c=Counter(A)
s=0
for v in c.values():
s+=v*(v-1)//2
for i in range(n):
ans=s-(c[A[i]])+1
print(ans)
|
a,b,c = map(int,raw_input().split())
count = 0
if a == b :
if c % a == 0:
count += 1
else:
for i in range(a,b+1):
if c % i == 0:
count += 1
print count
| 0 | null | 24,264,790,232,164 | 192 | 44 |
import math
dig_six = math.radians(60)
def koch(d, p1, p2):
if d == 0:
return d
#calc s, u, t from p1, p2
s_x = (2*p1[0]+1 * p2[0]) / 3
s_y = (2*p1[1]+1 * p2[1]) / 3
s = (s_x, s_y)
t_x = (1*p1[0]+2 * p2[0]) / 3
t_y = (1*p1[1]+2 * p2[1]) / 3
t = (t_x, t_y)
u_x = (t_x - s_x)*math.cos(dig_six) - (t_y - s_y)*math.sin(dig_six) + s_x
u_y = (t_x - s_x)*math.sin(dig_six) + (t_y - s_y)*math.cos(dig_six) + s_y
u = (u_x, u_y)
koch(d-1, p1, s)
print (' '.join([str(x) for x in s]))
koch(d-1, s, u)
print (' '.join([str(x) for x in u]))
koch(d-1, u, t)
print (' '.join([str(x) for x in t]))
koch(d-1, t, p2)
if __name__ == '__main__':
iterate = int(input())
p1 = (0.0, 0.0)
p2 = (100.0, 0.0)
print (' '.join([str(x) for x in p1]))
koch(iterate, p1, p2)
print (' '.join([str(x) for x in p2]))
|
import cmath
cmp_s = cmath.rect(1/3, 0)
cmp_u = cmath.rect(3**(-1/2), cmath.pi / 6)
cmp_t = cmath.rect(2/3, 0)
def koch_curve(p1: complex, p2: complex, n):
if n != 0:
cmp = p2 - p1
s, u, t = [p1 + cmp * c for c in (cmp_s, cmp_u, cmp_t)]
koch_curve(p1, s, n-1)
print('{0:8f} {1:8f}'.format(s.real, s.imag))
koch_curve(s, u, n-1)
print('{0:8f} {1:8f}'.format(u.real, u.imag))
koch_curve(u, t, n-1)
print('{0:8f} {1:8f}'.format(t.real, t.imag))
koch_curve(t, p2, n-1)
def main():
n = int(input())
print('{0:8f} {1:8f}'.format(.0, .0))
koch_curve(complex(.0, .0), complex(100.0, .0), n)
print('{0:8f} {1:8f}'.format(100.0, .0))
if __name__ == '__main__':
main()
| 1 | 120,888,241,562 | null | 27 | 27 |
# -*- coding: utf-8 -*-
import sys
rectangle = []
h,w = 1,1
while not(h == 0 and w == 0):
h,w = map(int, input().split())
if not(h == 0 and w == 0):
rectangle.append([h,w])
for rec in rectangle:
row_count = 1
display = []
for row in range(rec[0]):
col_count = 0
if (row_count %2 == 1):
col_count = 1
else:
col_count = 2
for col in range(rec[1]):
if (col_count % 2 == 1):
display.append('#')
else:
display.append('.')
col_count += 1
row_count += 1
display.append('\n')
print(''.join(display))
|
while True:
h, w = map(int, input().split())
if h == w == 0: break
for c in ['\n' if y == w else '#' if (x+y) % 2 == 0 else '.' for x in range(0,h) for y in range(0, w + 1)]: print(c, end='')
print('')
| 1 | 867,705,254,560 | null | 51 | 51 |
import math
a, b, h, m = list(map(int, input().split()))
deg_m = (m / 60) * 360
deg_h = ((60 * h + m) / 720) * 360
deg = abs(deg_h - deg_m)
deg = math.radians(min(360 - deg, deg))
x2 = b ** 2 + a ** 2 - 2 * b * a * math.cos(deg)
print(x2**0.5)
|
import math
A, B, H, M = map(int,input().split())
h = 30*H + (0.5)*M
m = 6*M
C = abs(h-m)
X = math.sqrt(A**2 + B**2 -(2*A*B*(math.cos(math.radians(C)))))
print(X)
| 1 | 20,087,565,923,470 | null | 144 | 144 |
n = int(input())
a = list(map(int, input().split()))
count = 0
flag = True
while flag:
flag = False
for j in range(n - 1, 0, -1):
if a[j] < a[j - 1]:
a[j], a[j - 1] = a[j - 1], a[j]
count += 1
flag = True
print(' '.join(list(map(str, a))))
print(count)
|
# -*- coding: utf-8 -*-
import sys
import os
import math
N = int(input())
A = list(map(int, input().split()))
def bubble_sort(A):
swap_num = 0
while True:
swapped = False
for i in range(N - 1):
if A[i] > A[i + 1]:
A[i], A[i + 1] = A[i + 1], A[i]
swap_num += 1
swapped = True
if not swapped:
return A, swap_num
A, num = bubble_sort(A)
print(*A)
print(num)
| 1 | 15,755,916,872 | null | 14 | 14 |
from sys import stdin, setrecursionlimit
def main():
mod = 10 ** 9 + 7
n, k = map(int, stdin.readline().split())
a = list(map(int, stdin.readline().split()))
a.sort()
ans = 0
m = n + 1
fac = [0] * m
finv = [0] * m
inv = [0] * m
def initialize_cmb(m):
fac[0] = 1
finv[0] = 1
if m > 1:
fac[1] = 1
finv[1] = 1
inv[1] = 1
for i in range(2, m):
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 cmb(n, k, mod=10 ** 9 + 7):
if n < k:
return 0
if n < 0 or k < 0:
return 0
return fac[n] * (finv[k] * finv[n - k] % mod) % mod
initialize_cmb(m)
for i in range(n - k + 1):
ans -= a[i] * cmb(n - 1 - i, k - 1)
ans %= mod
for i in range(k - 1, n):
ans += a[i] * cmb(i, k - 1)
ans %= mod
print(ans % mod)
if __name__ == "__main__":
setrecursionlimit(10000)
main()
|
MOD = 10 ** 9 + 7
table_len = 10 ** 5 + 10
fac = [1, 1]
for i in range(2, table_len):
fac.append(fac[-1] * i % MOD)
finv = [0] * table_len
finv[-1] = pow(fac[-1], MOD - 2, MOD)
for i in range(table_len-1, 0, -1):
finv[i-1] = finv[i] * i % MOD
def sum_of_maxS(N, K, As):
ret = 0
for i in range(N - K + 1):
ret += ((As[i] * fac[N - i - 1]) % MOD) * \
((finv[K-1] * finv[N - K - i]) % MOD) % MOD
ret %= MOD
return ret
N, K = map(int, input().split())
As = sorted(map(int, input().split()), reverse=True)
minus_As = [-A for A in reversed(As)]
print((sum_of_maxS(N, K, As) + sum_of_maxS(N, K, minus_As)) % MOD)
| 1 | 95,775,354,128,480 | null | 242 | 242 |
class UnionFind():
def __init__(self, n):
self.parents = list(range(n))
self.rank = [0] * n
def find(self, x):
if self.parents[x] == x:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.rank[x] < self.rank[y]:
self.parents[x] = y
else:
self.parents[y] = x
if self.rank[x] == self.rank[y]:
self.rank[x] += 1
n, m = map(int, input().split())
uf = UnionFind(n)
for _ in range(m):
x, y = map(int, input().split())
x, y = x-1, y-1
uf.union(x, y)
for i in range(n):
uf.find(i)
print(len(set(uf.parents))-1)
|
S = int(input())
h = str(S // 3600)
m = str((S % 3600) // 60)
s = str(((S % 3600) % 60))
print(h + ":" + m + ":" + s)
| 0 | null | 1,289,526,662,268 | 70 | 37 |
def main():
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
#from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil
#from operator import itemgetter
#inf = 10**17
#mod = 10**9 + 7
n = int(input())
a =[0]+list(map(int, input().split()))
b = list(accumulate(a))
c = sum(a)
res = 10000000000000000
for i in range(1, n):
s = b[i]
t = c-s
res = min(res, abs(s-t))
print(res)
if __name__ == '__main__':
main()
|
α = input()
if α.isupper() == True:
print('A')
elif α.islower() == True:
print('a')
| 0 | null | 76,507,217,528,092 | 276 | 119 |
x,k,d = map(int, input().split())
x = abs(x)
if x - d*k >= 0:
print(abs(x-d*k))
else:
a = x // d
if k % 2 == 0:
if a % 2 == 0:
print(abs(x - d*a))
else:
print(abs(x - d * (a+1)))
else:
if a % 2 == 0:
print(abs(x - d * (a + 1)))
else:
print(abs(x - d * a))
|
X, K, D = map(int, input().split())
X = abs(X)
r = X % D
t = X // D
if t >= K:
ans = abs(X - K * D)
elif (K - t) % 2 == 0:
ans = r
else:
ans = D - r
print(ans)
| 1 | 5,267,611,853,620 | null | 92 | 92 |
N=int(input())
S=input()
K=0
A="*"
for s in S:
if s!=A:
A=s
K+=1
print(K)
|
x = int(input())
#print(1 if x == 0 else 0)
print(x ^ 1)
| 0 | null | 86,369,215,426,080 | 293 | 76 |
md1=list(map(int,input().split()))
md2=list(map(int,input().split()))
if md1[0]==md2[0]:
print(0)
else:
print(1)
|
def main():
n = int(input())
A = list(map(int, input().split()))
for a in A:
if a % 2 == 0:
if not (a % 3 == 0 or a % 5 == 0):
print('DENIED')
return
print('APPROVED')
if __name__ == '__main__':
main()
| 0 | null | 96,935,215,621,298 | 264 | 217 |
N=int(input())
a=list(map(int,input().split()))
s=a[0]
for i in range(1,N):
s^=a[i]
for i in range(N):
print(s^a[i])
|
from enum import Enum
class Type(Enum):
win_Taro = 0
win_Hanako = 1
Even = 2
def comp_str(A, B):
if A > B:
return Type.win_Taro
elif A < B:
return Type.win_Hanako
else:
return Type.Even
N = int(input())
point_Taro = 0
point_Hanako = 0
for loop in range(N):
line = str(input()).split()
ret_type = comp_str(line[0], line[1])
if ret_type == Type.win_Taro:
point_Taro += 3
elif ret_type == Type.win_Hanako:
point_Hanako += 3
else:
point_Taro += 1
point_Hanako += 1
print("%d %d"%(point_Taro, point_Hanako))
| 0 | null | 7,262,367,483,778 | 123 | 67 |
x,y,a,b,c = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
p = sorted(p,reverse=True)
q = sorted(q,reverse=True)
r = sorted(r,reverse=True)
lis = p[:x]
lis.extend(q[:y])
lis = sorted(lis)
#print(lis)
ans = []
lr = len(r)
for i in range(len(lis)):
if i<lr:
ans.append(max(lis[i],r[i]))
else:
ans.append(lis[i])
print(sum(ans))
|
# -*- 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 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)]
class Math():
@staticmethod
def gcd(a, b):
if b == 0:
return a
return Math.gcd(b, a % b)
@staticmethod
def lcm(a, b):
return (a * b) // Math.gcd(a, b)
@staticmethod
def divisor(n):
res = []
i = 1
for i in range(1, int(n ** 0.5) + 1):
if n % i == 0:
res.append(i)
if i != n // i:
res.append(n // i)
return res
@staticmethod
def round_up(a, b):
return -(-a // b)
@staticmethod
def is_prime(n):
if n < 2:
return False
if n == 2:
return True
if n % 2 == 0:
return False
d = int(n ** 0.5) + 1
for i in range(3, d + 1, 2):
if n % i == 0:
return False
return True
@staticmethod
def fact(N):
res = {}
tmp = N
for i in range(2, int(N ** 0.5 + 1) + 1):
cnt = 0
while tmp % i == 0:
cnt += 1
tmp //= i
if cnt > 0:
res[i] = cnt
if tmp != 1:
res[tmp] = 1
if res == {}:
res[N] = 1
return res
def pop_count(x):
x = x - ((x >> 1) & 0x5555555555555555)
x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
x = x + (x >> 8)
x = x + (x >> 16)
x = x + (x >> 32)
return x & 0x0000007f
MOD = int(1e09) + 7
INF = int(1e15)
def solve():
X, Y, A, B, C = Scanner.map_int()
P = Scanner.map_int()
Q = Scanner.map_int()
R = Scanner.map_int()
P.sort(reverse=True)
P = P[:X]
Q.sort(reverse=True)
Q = Q[:Y]
R.sort(reverse=True)
R = R[:min(X + Y, C)]
ans = P + Q + R
ans.sort(reverse=True)
print(sum(ans[:X + Y]))
def main():
# sys.stdin = open("sample.txt")
# T = Scanner.int()
# for _ in range(T):
# solve()
# print('YNeos'[not solve()::2])
solve()
if __name__ == "__main__":
main()
| 1 | 44,985,297,696,352 | null | 188 | 188 |
def main():
MOD = 10 ** 9 + 7
N = int(input())
A = list(map(int, input().split()))
ans = 1
d = [0] * (max(A) + 1)
for a in A:
if a == 0:
ans *= 3 - d[a]
ans %= MOD
d[a] += 1
else:
ans *= d[a-1] - d[a]
d[a] += 1
ans %= MOD
print(ans)
if __name__ == "__main__":
main()
|
n = int(input())
a = list(map(int, input().split()))
from collections import defaultdict
d = defaultdict(int)
ans=1
for num in a:
if num==0:
last_cnt=3
tmp = last_cnt - d[num]
else:
last_cnt=d[num-1]
tmp = last_cnt - d[num]
if tmp == 0:
# tmp=1
ans=0
ans*=tmp
ans%=1000000007
d[num]+=1
print(ans)
| 1 | 129,837,523,627,252 | null | 268 | 268 |
import sys
sys.setrecursionlimit(4100000)
import math
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
def resolve():
# S = [x for x in sys.stdin.readline().split()][0] # 文字列 一つ
# N = [int(x) for x in sys.stdin.readline().split()][0] # int 一つ
H1, M1, H2, M2, K = [int(x) for x in sys.stdin.readline().split()] # 複数int
# h_list = [int(x) for x in sys.stdin.readline().split()] # 複数int
# grid = [list(sys.stdin.readline().split()[0]) for _ in range(N)] # 文字列grid
# v_list = [int(sys.stdin.readline().split()[0]) for _ in range(N)]
# grid = [[int(x) for x in sys.stdin.readline().split()]
# for _ in range(N)] # int grid
logger.debug('{}'.format([]))
print(H2 * 60 + M2 - K - H1 * 60 - M1)
if __name__ == "__main__":
resolve()
# AtCoder Unit Test で自動生成できる, 最後のunittest.main は消す
# python -m unittest template/template.py で実行できる
# pypy3 -m unittest template/template.py で実行できる
import sys
from io import StringIO
import unittest
class TestClass(unittest.TestCase):
def assertIO(self, input, output):
stdout, stdin = sys.stdout, sys.stdin
sys.stdout, sys.stdin = StringIO(), StringIO(input)
resolve()
sys.stdout.seek(0)
out = sys.stdout.read()[:-1]
sys.stdout, sys.stdin = stdout, stdin
self.assertEqual(out, output)
def test_入力例_1(self):
input = """10 0 15 0 30"""
output = """270"""
self.assertIO(input, output)
def test_入力例_2(self):
input = """10 0 12 0 120"""
output = """0"""
self.assertIO(input, output)
|
a = input().split()
print((int(a[2]) - int(a[0])) * 60 + (int(a[3]) - int(a[1])) - int(a[4]) )
| 1 | 18,156,667,158,980 | null | 139 | 139 |
a = [[[0 for r in range (10)] for f in range(3)] for b in range(4)]
ans = ""
n = input()
for i in range(0, n):
b, f, r, v = map(int, raw_input().split(" "))
a[b-1][f-1][r-1] += v
for b in range(4):
for f in range(3):
for r in range(10):
ans += " "+str(a[b][f][r])
if (b!=3 or f!=2 or r!=9):
ans += "\n"
if (b != 3):
ans += "#"*20+"\n"
print(ans)
|
house = [[[0 for col in range(10)]for row in range(3)]for layer in range(4)]
n = int(raw_input())
for i in range(n):
input_line = raw_input().split()
house[int(input_line[0])-1][int(input_line[1])-1][int(input_line[2])-1] += int(input_line[3])
for i in range(0,4):
if i != 0:
print "#"*20
for j in range(0,3):
buf = ""
for k in range(0,10):
buf += " " + str(house[i][j][k])
print buf
| 1 | 1,105,766,665,360 | null | 55 | 55 |
user_input = input()
ascci_value = ord(user_input)
if ascci_value in range(97, 123):
print('a')
else:
print('A')
|
str = input()
if str.islower():
print('a')
else:
print('A')
| 1 | 11,311,317,855,136 | null | 119 | 119 |
def resolve():
INF = 1<<60
H, N = map(int, input().split())
A, B = [], []
for i in range(N):
a, b = map(int, input().split())
A.append(a)
B.append(b)
dp = [INF]*(H+1)
dp[0] = 0
for h in range(H):
for i in range(N):
to = min(H, A[i]+h)
dp[to] = min(dp[to], dp[h]+B[i])
print(dp[H])
if __name__ == "__main__":
resolve()
|
import math
r = float(raw_input())
s = r * r * math.pi
l = 2 * r * math.pi
print '%f %f' %(s,l)
| 0 | null | 41,079,750,426,990 | 229 | 46 |
#cやり直し
import numpy
n = int(input())
a = list(map(int, input().split()))
ans = 0
mod = 10**9 +7
before = a[-1]
for i in range(n-1,0,-1):
ans += (a[i-1]*before)%mod
before += a[i-1]
print(ans%mod)
|
from collections import deque
n = int(input())
graph = [[] for _ in range(n + 1)]
for _ in range(n):
input_list = list(map(int, input().split()))
index = input_list[0]
num = input_list[1]
if num >= 1:
nodes = input_list[2:]
for node in nodes:
graph[index].append(node)
# graph[node].append(index)
visited = [-1] * (n + 1)
num = [-1] * (n + 1)
q = deque()
visited[1] = 1
num[1] = 0
q.append(1)
while q:
index = q.popleft()
for node in graph[index]:
if visited[node] == -1:
visited[node] = 1
num[node] = num[index] + 1
q.append(node)
for i, j in enumerate(num):
if i == 0:
continue
print(i, j)
| 0 | null | 1,929,733,092,660 | 83 | 9 |
N = int(input())
L = len(str(N))
if L == 1:
print(N)
quit()
pair = [[(10**(L-2)-1)//9]*9 for _ in range(9)]
for i in range(9):
pair[i][i] += 1
for i in range(10**(L-1), N+1):
pair[int(str(i)[0])-1][int(str(i)[L-1])-1] += 1 if i%10 else 0
ans = 0
for i in range(9):
for j in range(9):
ans += pair[i][j] * pair[j][i]
print(ans)
|
import sys
# import re
# import math
# import collections
# import decimal
# import bisect
# import itertools
# import fractions
# import functools
# import copy
# import heapq
# import decimal
# import statistics
# import queue
sys.setrecursionlimit(10000001)
INF = 10 ** 10
MOD = 10 ** 9 + 7
ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
# ===CODE===
def main():
n = ni()
mat = [[0 for _ in range(10)] for _ in range(10)]
for i in range(1, n + 1):
s = str(i)
ss = int(s[0])
st = int(s[-1])
mat[ss][st] += 1
ans=0
for i in range(1,10):
for j in range(1,10):
ans += mat[i][j]*mat[j][i]
print(ans)
if __name__ == '__main__':
main()
| 1 | 86,650,377,455,872 | null | 234 | 234 |
a,b,c = map(int, input().split())
print('Yes' if a/c <= b else 'No')
|
N=int(input())
x=(10**N-2*(9**N)+8**N)%(10**9+7)
print(x)
| 0 | null | 3,346,214,468,128 | 81 | 78 |
#!/usr/bin/env python3
def main():
import sys
from collections import deque
input = sys.stdin.readline
N, M = map(int, input().split())
G = [[] for _ in [0] * (N + 1)]
for _ in [0] * M:
a, b = map(int, input().split())
G[a].append(b)
G[b].append(a)
q = deque([1])
depth = [0 for _ in [0] * (N + 1)]
while q:
v = q.popleft()
for nv in G[v]:
if depth[nv] == 0:
# depth[nv] = depth[v] + 1
depth[nv] = v
q.append(nv)
if 0 in depth[2:]:
print('No')
return
else:
print('Yes')
for guide in depth[2:]:
print(guide)
if __name__ == '__main__':
main()
|
N,M=map(int,input().split())
V=[[]for i in range(N)]
for i in range(M):
a,b=map(int,input().split())
V[a-1].append(b-1)
V[b-1].append(a-1)
print('Yes')
p,q=0,[0]
seen=[0]*N
d=[0]*N
while len(q)!=p:
for i in V[q[p]]:
if not seen[i]:
q.append(i)
d[i]=q[p]+1
seen[i]+=1
p+=1
d.pop(0)
for i in d:print(i)
| 1 | 20,467,822,668,828 | null | 145 | 145 |
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')
import string
# string.ascii_lowercase
from bisect import bisect_left
def solve():
n = int(input())
l = [int(x) for x in input().split()]
l.sort()
ans = 0
for i in range(n):
for j in range(i + 1, n):
r = bisect_left(l, l[i] + l[j])
# if r > j:
ans += r - j - 1
print(ans)
t = 1
# t = int(input())
for case in range(1,t+1):
ans = solve()
"""
azyxwvutsrqponmlkjihgfedcb
"""
|
from collections import deque
import sys
import copy
N_MAX = 200000 + 5
INF = 10**9 + 7
sys.setrecursionlimit(N_MAX)
MOD = 10**9 + 7
### BFS ###
nextH = [0, 1, 0, -1]
nextW = [1, 0, -1, 0]
def bfs(u, S):
S[u[0]][u[1]] = 0 # state を変更
q = deque()
q.append(u)
m = 0
while q:
u = q.popleft()
for h, w in zip(nextH, nextW):
h += u[0]
w += u[1]
if not (0 <= h < len(S) and 0 <= w < len(S[0])):
continue
if S[h][w] == '.': # state を確認
S[h][w] = S[u[0]][u[1]] + 1 # state を変更
m = max(m, S[h][w])
q.append((h, w))
return m
def main():
H, W = map(int, sys.stdin.readline().rstrip().split())
S = []
for _ in range(H):
S.append([x for x in sys.stdin.readline().rstrip()])
# print(S)
m = 0
for h in range(H):
for w in range(W):
D = copy.deepcopy(S)
if S[h][w] == '.':
m = max(m, bfs((h, w), D))
print(m)
main()
| 0 | null | 133,671,145,497,096 | 294 | 241 |
import sys
input = sys.stdin.readline
def main():
ans = 'No'
N = int(input())
c = 0
for _ in range(N):
d1, d2 = map(int, input().split())
if d1 == d2:
c += 1
if c >= 3:
ans = 'Yes'
break
else:
c = 0
print(ans)
if __name__ == '__main__':
main()
|
n = int(input())
Ai = list(map(int, input().split()))
sum_ans = sum(Ai)
ans = 0
mod = 1000000007
for i in range(n-1):
sum_ans -= Ai[i]
ans += sum_ans * Ai[i]
ans %= mod
print(ans)
| 0 | null | 3,124,077,353,538 | 72 | 83 |
import sys
def input(): return sys.stdin.readline().rstrip()
# ライブラリ参照https://atcoder.jp/contests/practice2/submissions/16580070
class SegmentTree:
__slots__ = ["func", "e", "original_size", "n", "data"]
def __init__(self, length_or_list, func, e):
self.func = func
self.e = e
if isinstance(length_or_list, int):
self.original_size = length_or_list
self.n = 1 << ((length_or_list - 1).bit_length())
self.data = [self.e] * self.n
else:
self.original_size = len(length_or_list)
self.n = 1 << ((self.original_size - 1).bit_length())
self.data = [self.e] * self.n + length_or_list + \
[self.e] * (self.n - self.original_size)
for i in range(self.n-1, 0, -1):
self.data[i] = self.func(self.data[2*i], self.data[2*i+1])
def replace(self, index, value):
index += self.n
self.data[index] = value
index //= 2
while index > 0:
self.data[index] = self.func(
self.data[2*index], self.data[2*index+1])
index //= 2
def folded(self, l, r):
left_folded = self.e
right_folded = self.e
l += self.n
r += self.n
while l < r:
if l % 2:
left_folded = self.func(left_folded, self.data[l])
l += 1
if r % 2:
r -= 1
right_folded = self.func(self.data[r], right_folded)
l //= 2
r //= 2
return self.func(left_folded, right_folded)
def all_folded(self):
return self.data[1]
def __getitem__(self, index):
return self.data[self.n + index]
def max_right(self, l, f):
# assert f(self.e)
if l >= self.original_size:
return self.original_size
l += self.n
left_folded = self.e
while True:
# l //= l & -l
while l % 2 == 0:
l //= 2
if not f(self.func(left_folded, self.data[l])):
while l < self.n:
l *= 2
if f(self.func(left_folded, self.data[l])):
left_folded = self.func(left_folded, self.data[l])
l += 1
return l - self.n
left_folded = self.func(left_folded, self.data[l])
l += 1
if l == l & -l:
break
return self.original_size
# 未verify
def min_left(self, r, f):
# assert f(self.e)
if r == 0:
return 0
r += self.n
right_folded = self.e
while True:
r //= r & -r
if not f(self.func(self.data[r], right_folded)):
while r < self.n:
r = 2 * r + 1
if f(self.func(self.data[r], right_folded)):
right_folded = self.func(self.data[r], right_folded)
r -= 1
return r + 1 - self.n
if r == r & -r:
break
return 0
def orr(x, y):
return x | y
def main():
N = int(input())
S = input()
S = list(map(lambda c: 2**(ord(c) - ord('a')), list(S)))
Q = int(input())
seg = SegmentTree(S, orr, 0)
for _ in range(Q):
num, x, y = input().split()
if num == '1':
seg.replace(int(x)-1, 2**(ord(y) - ord('a')))
else:
bits = seg.folded(int(x)-1, int(y))
print(sum(map(int, list(bin(bits))[2:])))
if __name__ == '__main__':
main()
|
print raw_input()[:3]
| 0 | null | 38,462,220,513,138 | 210 | 130 |
nn = list(map(int, list(input())))
L = len(nn)
K = int(input())
dp = [[[0] * (4) for _ in range(2)] for __ in range(L + 1)]
dp[0][0][0] = 1
for i, n in enumerate(nn):
for x in range(0, 10):
if x == 0:
dp[i + 1][1][0] += dp[i][1][0]
dp[i + 1][1][1] += dp[i][1][1]
dp[i + 1][1][2] += dp[i][1][2]
dp[i + 1][1][3] += dp[i][1][3]
else:
dp[i + 1][1][1] += dp[i][1][0]
dp[i + 1][1][2] += dp[i][1][1]
dp[i + 1][1][3] += dp[i][1][2]
for x in range(0, n + 1):
if x == 0:
dp[i + 1][0 if x == n else 1][0] += dp[i][0][0]
dp[i + 1][0 if x == n else 1][1] += dp[i][0][1]
dp[i + 1][0 if x == n else 1][2] += dp[i][0][2]
dp[i + 1][0 if x == n else 1][3] += dp[i][0][3]
else:
dp[i + 1][0 if x == n else 1][1] += dp[i][0][0]
dp[i + 1][0 if x == n else 1][2] += dp[i][0][1]
dp[i + 1][0 if x == n else 1][3] += dp[i][0][2]
print(dp[-1][0][K] + dp[-1][1][K])
|
import math
import itertools
n = int(input())
k = int(input())
f = len(str(n))
if f < k:
print(0)
else:
#f-1桁以内に収まる数
en = 1
for i in range(k):
en *= f-1-i
de = math.factorial(k)
s = en // de * pow(9, k)
#f桁目によって絞る
kami = int(str(n)[0])
en = 1
for i in range(k-1):
en *= f-1-i
de = math.factorial(k-1)
s += (en // de * pow(9, k-1)) * (kami-1)
#以下上1桁は同じ数字
keta = list(range(f-1))
num = list(range(1,10))
b = kami * pow(10, f-1)
m = 0
if k == 1:
m = b
if m <= n:
s += 1
else:
for d in itertools.product(num, repeat=k-1):
for c in itertools.combinations(keta, k-1):
m = b
for i in range(k-1):
m += d[i] * pow(10, c[i])
if m <= n:
s += 1
print(s)
| 1 | 75,747,647,554,460 | null | 224 | 224 |
S = input()
ans = 0
cnt = 0
for w in S:
if w == 'S':
ans = max(ans,cnt)
cnt = 0
else:
cnt += 1
ans = max(cnt,ans)
print(ans)
|
import sys
def solve():
readline = sys.stdin.buffer.readline
mod = 10 ** 9 + 7
s = str(readline().rstrip().decode('utf-8'))
if s == "RRR":
print(3)
elif s == "RRS" or s == "SRR":
print(2)
elif s.count("R") == 0:
print(0)
else:
print(1)
if __name__ == '__main__':
solve()
| 1 | 4,920,843,663,000 | null | 90 | 90 |
N = int(input())
MX = 10**4
ans = {}
for x in range(1, 100):
for y in range(1, 100):
for z in range(1, 100):
tmp = x**2+y**2+z**2+x*y+x*z+y*z
if tmp > MX:
break
if not(tmp in ans):
ans[tmp] = 1
else:
ans[tmp] += 1
for i in range(1, N+1):
if i in ans:
print(ans[i])
else:
print(0)
|
if input() == "1":
print(0)
else:
print(1)
| 0 | null | 5,503,511,921,700 | 106 | 76 |
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
n = int(input())
a = list(map(int, input().split()))
s = sum(a)
half = s / 2
cum = 0
for v in a:
if abs(half - cum) < abs(half - (cum + v)):
break
cum += v
print(abs((s - cum) - cum))
|
h, w, n= [int(input()) for i in range(3)]
k = max(h, w)
ans = (n+k-1)//k
print(ans)
| 0 | null | 115,910,559,012,402 | 276 | 236 |
from bisect import bisect_left, bisect_right, insort_left
import sys
readlines = sys.stdin.readline
def main():
n = int(input())
s = list(input())
q = int(input())
d = {}
flag = {}
for i in list('abcdefghijklmnopqrstuvwxyz'):
d.setdefault(i, []).append(-1)
flag.setdefault(i, []).append(-1)
for i in range(n):
d.setdefault(s[i], []).append(i)
for i in range(q):
q1,q2,q3 = map(str,input().split())
if q1 == '1':
q2 = int(q2) - 1
if s[q2] != q3:
insort_left(flag[s[q2]],q2)
insort_left(d[q3],q2)
s[q2] = q3
else:
ans = 0
q2 = int(q2) - 1
q3 = int(q3) - 1
if q2 == q3:
print(1)
continue
for string,l in d.items():
res = 0
if d[string] != [-1]:
left = bisect_left(l,q2)
right = bisect_right(l,q3)
else:
left = 0
right = 0
if string in flag:
left2 = bisect_left(flag[string],q2)
right2 = bisect_right(flag[string],q3)
else:
left2 = 0
right2 = 0
if left != right:
if right - left > right2 - left2:
res = 1
ans += res
#print(string,l,res)
print(ans)
if __name__ == '__main__':
main()
|
# https://atcoder.jp/contests/abc166/tasks/abc166_e
"""
変数分離すれば互いに独立なので、
連想配列でO(N)になる。
"""
import sys
input = sys.stdin.readline
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
P = (j+1-A[j] for j in range(N))
M = (i+1+A[i] for i in range(N))
dic = Counter(P)
res = 0
for m in M:
res += dic.get(m,0)
print(res)
| 0 | null | 44,584,965,630,492 | 210 | 157 |
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7
def resolve():
def meguru_bisect(left, right):
"""
is_okを定義して下さい
:param left: 取りうる最小の値-1
:param right: 取りうる最大の値+1
:return: is_okを満たす最小(もしくは最大)の値
"""
while abs(left - right) > 1:
mid = (left + right) // 2
if is_ok(mid):
right = mid
else:
left = mid
return right
def is_ok(x):
total = 0
for i in range(n):
total += max(0, A[i] - x // F[i])
return total <= k
n, k = map(int, input().split())
A = sorted(list(map(int, input().split())))
F = sorted(list(map(int, input().split())), reverse=True)
left = -1
right = 10 ** 12 + 1
res = meguru_bisect(left, right)
print(res)
if __name__ == '__main__':
resolve()
|
H1, M1, H2, M2, K = map(int, input().split())
s = H1*60 + M1
e = H2*60 + M2
print(e - K - s)
| 0 | null | 91,389,372,891,672 | 290 | 139 |
x,y=map(int,input().split())
ans=0
x=max(4-x,0)
y=max(4-y,0)
ans+=x*100000+y*100000
if x==3 and y==3:
ans+=400000
print(ans)
|
# -*- coding:utf-8
def main():
N = int(input())
A = list(map(int, input().split()))
insertionSort(A, N)
print(' '.join(map(str, A)))
def insertionSort(A, N):
for i in range(1, N):
print(' '.join(map(str, A)))
v = A[i]
j = i-1
while(j>=0 and A[j]>v):
A[j+1] = A[j]
j -= 1
A[j+1] = v
if __name__ == '__main__':
main()
| 0 | null | 70,110,369,951,538 | 275 | 10 |
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())
|
n,k = map(int,input().split())
mod = 10**9+7
ans = 0
kosu = [0 for i in range(k+1)]
for i in range(k, 0, -1):
kin = 1 ## kosuに入れる数
nn = k//i ## iの倍数が何個あるかを計算する
ii = n
kini = nn
while ii > 0:
if ii%2 == 1:
kin = (kin*kini)%mod
kini = (kini**2)%mod
ii //= 2
for j in range(2,nn+1):
kin = (kin-kosu[j*i])%mod
kosu[i] = kin
for i in range(1,k+1):
ans = (ans+(kosu[i]*i)%mod)%mod
print(ans)
| 1 | 36,960,823,865,218 | null | 176 | 176 |
def main():
n = int(input())
if n%2:
print(n//2+1)
else:
print(n//2)
if __name__ == '__main__':
main()
|
def update(A,N):
B=[0 for i in range(N+1)]
for i in range(N):
l = max(0,i-A[i])
r = min(N,i+A[i]+1)
B[l]+=1
B[r]-=1
x = A[0] == B[0]
A[0]=B[0]
for i in range(1,N):
B[i]+=B[i-1]
x = x and A[i] == B[i]
A[i]=B[i]
return x
def resolve():
N,M = map(int,input().split())
A = list(map(int,input().split()))
for i in range(M):
x=update(A,N)
if x:
break
print(" ".join(map(str,A)))
resolve()
| 0 | null | 37,352,680,180,270 | 206 | 132 |
from itertools import combinations
import copy
H, W, K = map(int, input().split())
C = [list(input()) for _ in range(H)]
ans = 0
w = list(range(W))
h = list(range(H))
for i in range(W+1):
for comi in combinations(w, i):
for j in range(H+1):
for comj in combinations(h, j):
tmpC = copy.deepcopy(C)
for ci in comi:
for hj in range(H):
tmpC[hj][ci] = '.'
for cj in comj:
for wi in range(W):
tmpC[cj][wi] = '.'
tmp = 0
for tc in tmpC:
tmp += tc.count('#')
if tmp == K:
ans += 1
print(ans)
|
import sys
H,W,K = list(map(int, input().split()))
c = []
for i in range(H):
c.append(list(map(str, input().split()))[0])
o = 0
p = 0
for i in range(2**H):
for j in range(2**W):
o = 0
for b,n in enumerate(bin(i)[2:].zfill(H)):
for a,l in enumerate(bin(j)[2:].zfill(W)):
if n == '0' and l == '0' and c[b][a]=='#':
o += 1
if o == K:
p += 1
print(p)
| 1 | 8,921,576,272,070 | null | 110 | 110 |
import sys
def MI(): return map(int,sys.stdin.readline().rstrip().split())
N,u,v = MI()
Graph = [[] for _ in range(N+1)]
for i in range(N-1):
a,b = MI()
Graph[a].append(b)
Graph[b].append(a)
dist_T = [-1]*(N+1) # uからの最短距離
dist_A = [-1]*(N+1) # vからの最短距離
dist_T[u] = 0
dist_A[v] = 0
from collections import deque
q1 = deque([u])
q2 = deque([v])
while q1:
n = q1.pop()
for d in Graph[n]:
if dist_T[d] == -1:
dist_T[d] = dist_T[n] + 1
q1.appendleft(d)
while q2:
n = q2.pop()
for d in Graph[n]:
if dist_A[d] == -1:
dist_A[d] = dist_A[n] + 1
q2.appendleft(d)
ans = 0
for i in range(1,N+1):
if dist_T[i] < dist_A[i]:
ans = max(ans,dist_A[i]-1)
print(ans)
|
N, u, v = map(int, input().split())
edges = [[] for _ in range(N)]
for _ in range(N - 1):
fr, to = map(int, input().split())
fr -= 1
to -= 1
edges[fr].append(to)
edges[to].append(fr)
def calc(s):
minDist = [10**18] * N
st = [(s, 0)]
while st:
now, dist = st.pop()
if minDist[now] < dist:
continue
minDist[now] = dist
for to in edges[now]:
if minDist[to] > dist + 1:
st.append((to, dist + 1))
return minDist
distTaka = calc(u - 1)
distAoki = calc(v - 1)
ans = 0
for i in range(N):
if distTaka[i] < distAoki[i]:
ans = max(ans, distAoki[i] - 1)
print(ans)
| 1 | 117,052,628,333,158 | null | 259 | 259 |
import sys; input = sys.stdin.readline
n = int(input())
ac, wa, tle, re = 0, 0, 0, 0
for i in range(n):
a = input().strip()
if a == 'AC': ac+=1
elif a == 'WA': wa += 1
elif a == 'TLE': tle += 1
elif a == 'RE': re += 1
print(f"AC x {ac}")
print(f"WA x {wa}")
print(f"TLE x {tle}")
print(f"RE x {re}")
|
N = int(input())
data = [input() for _ in range(N)]
def f(s):
c = len([x for x in data if x in s])
print(f"{s} x {c}")
f("AC")
f("WA")
f("TLE")
f("RE")
| 1 | 8,679,852,522,890 | null | 109 | 109 |
from networkx import *
print(max(*map(len,connected_components(Graph([*map(str.split,open(0))][1:]))),1,0))
|
def main():
N = int(input())
A = list(map(int,input().split()))
buka_num = [0]*(N+1)
for i in range(N-1):
buka_num[A[i]] += 1
for i in range(1,N+1,1):
print(buka_num[i])
main()
| 0 | null | 18,143,592,036,640 | 84 | 169 |
A, B, C, K = map(int, input().split())
a = min(A, K)
K -= a
b = min(B, K)
K -= b
c = min(C, K)
print(a-c)
|
A,B,C,K = map(int,input().split())
count = 0
if K <= A:
print(K)
elif A < K <= A + B:
print(A)
elif A +B <= K <= A + B + C:
x = K -A -B
print(A -x)
| 1 | 21,719,909,407,610 | null | 148 | 148 |
def main():
n, k = map(int, input().split())
cnt = 0
i = 1
# nが大きい
while n >= i:
i *= k
cnt += 1
print(cnt)
if __name__ == '__main__':
main()
|
import sys
def main():
input = sys.stdin.buffer.readline
n, k = map(int, input().split())
i = 1
while k ** i <= n:
i += 1
print(i)
if __name__ == "__main__":
main()
| 1 | 64,123,963,701,180 | null | 212 | 212 |
#!/usr/bin/env python3
# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template)
def main():
N = int(input())
C = list(input())
Rc = len(list(filter(lambda x:x=="R",C)))
Wc = len(C) - Rc
c = 0
for i in range(Rc):
if C[i] == "W":
c+=1
# print(Rc)
# print(Wc)
print(c)
pass
if __name__ == '__main__':
main()
|
S = input()
if S == "AAA" or S == "BBB":
print("No")
else:
print("Yes")
| 0 | null | 30,576,935,161,542 | 98 | 201 |
h, a = map(int, input().split())
print(__import__('math').ceil(h / a))
|
def main():
n = int(input())
a = sorted(map(int,input().split()))
fld = [-1 for i in range(a[-1]+1)]
flg = []
for i in range(n):
if fld[a[i]]==-1:
fld[a[i]]=1
for j in range(2,a[-1]//a[i]+1):
fld[a[i]*j] = 0
elif fld[a[i]]==1:
flg.append(a[i])
for i in range(len(flg)):
fld[flg[i]] = 0
ans = 0
for i in range(len(fld)):
if fld[i] == 1:
ans += 1
print(ans)
if __name__ == "__main__":
main()
| 0 | null | 45,891,584,466,350 | 225 | 129 |
N, K = map(int, input().split())
A = tuple(map(int, input().split()))
MOD = 10 ** 9 + 7
if K == N:
ans = 1
for x in A:
ans = (ans * x) % MOD
print(ans)
exit()
plus, minus = [], []
for a in A:
if a >= 0:
plus.append(a)
else:
minus.append(a)
plus.sort(reverse=True)
minus.sort()
if not plus:
ans = 1
if K % 2:
# 答えは負値になるので絶対値小さいのを取る
for x in minus[-K:]:
ans = (ans * x) % MOD
else:
# 答えは非負値になるので絶対値大きいのを取る
for x in minus[:K]:
ans = (ans * x) % MOD
print(ans)
exit()
idx = 0
for i in range(2, N, 2):
if K - i < 0:
break
if not len(plus) >= K - i + 2:
idx += 2
continue
if len(minus) >= i:
if minus[i - 2] * minus[i - 1] < plus[K - i + 1] * plus[K - i]:
break
else:
idx += 2
ans = 1
for x in minus[:idx] + plus[:K - idx]:
ans = (ans * x) % MOD
print(ans)
|
#!/usr/bin/python3
import sys
from functools import reduce
from operator import mul
input = lambda: sys.stdin.readline().strip()
n, k = [int(x) for x in input().split()]
a = sorted((int(x) for x in input().split()), key=abs)
sgn = [0 if x == 0 else x // abs(x) for x in a]
M = 10**9 + 7
def modmul(x, y):
return x * y % M
if reduce(mul, sgn[-k:]) >= 0:
print(reduce(modmul, a[-k:], 1))
else:
largest_unselected_neg = next(i for i, x in enumerate(reversed(a[:-k])) if x < 0) if any(x < 0 for x in a[:-k]) else None
largest_unselected_pos = next(i for i, x in enumerate(reversed(a[:-k])) if x > 0) if any(x > 0 for x in a[:-k]) else None
smallest_selected_neg = next(i for i, x in enumerate(a[-k:]) if x < 0) if any(x < 0 for x in a[-k:]) else None
smallest_selected_pos = next(i for i, x in enumerate(a[-k:]) if x > 0) if any(x > 0 for x in a[-k:]) else None
can1 = largest_unselected_neg is not None and smallest_selected_pos is not None
can2 = largest_unselected_pos is not None and smallest_selected_neg is not None
def ans1():
return list(reversed(a[:-k]))[largest_unselected_neg] * reduce(modmul, (x for i, x in enumerate(a[-k:]) if i != smallest_selected_pos), 1) % M
def ans2():
return list(reversed(a[:-k]))[largest_unselected_pos] * reduce(modmul, (x for i, x in enumerate(a[-k:]) if i != smallest_selected_neg), 1) % M
if can1 and not can2:
print(ans1())
elif can2 and not can1:
print(ans2())
elif can1 and can2:
if list(reversed(a[:-k]))[largest_unselected_neg] * a[-k:][smallest_selected_neg] > list(reversed(a[:-k]))[largest_unselected_pos] * a[-k:][smallest_selected_pos]:
print(ans1())
else:
print(ans2())
else:
print(reduce(modmul, a[:k], 1))
| 1 | 9,388,776,185,152 | null | 112 | 112 |
#!/usr/bin/env python3
import sys
# https://en.wikipedia.org/wiki/Cycle_detection
def floyd_cycle_finding(f, x0):
'''
循環していない部分の長さをlam
循環部分の長さをmuとして
(lam, mu)
を返す
>>> floyd_cycle_finding(lambda x: x**2 % 3, 2)
(1, 1)
>>> floyd_cycle_finding(lambda x: x**2 % 1001, 2)
(2, 4)
>>> floyd_cycle_finding(lambda x: x**2 % 16, 2)
(2, 1)
>>> floyd_cycle_finding(lambda x: x*2 % 5, 3)
(0, 4)
>>> floyd_cycle_finding(lambda x: x*2 % 5, 0)
(0, 1)
'''
tortoise = f(x0)
hare = f(f(x0))
while tortoise != hare:
tortoise = f(tortoise)
hare = f(f(hare))
lam = 0
tortoise = x0
while tortoise != hare:
tortoise = f(tortoise)
hare = f(hare)
lam += 1
mu = 1
hare = f(tortoise)
while tortoise != hare:
hare = f(hare)
mu += 1
return lam, mu
def solve(N: int, X: int, M: int):
f = lambda x: x**2 % M
def g(x):
while True:
yield x
x = f(x)
lam, mu = floyd_cycle_finding(f, X)
ans = 0
gg = g(X)
k = max((N - lam) // mu, 0)
for i in range(min(N, lam)):
ans += next(gg)
for i in range(mu):
ans += k * next(gg)
for i in range(max((N-lam)%mu, 0)):
ans += next(gg)
return ans
# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
N = int(next(tokens)) # type: int
X = int(next(tokens)) # type: int
M = int(next(tokens)) # type: int
print(solve(N, X, M))
def test():
import doctest
doctest.testmod()
if __name__ == '__main__':
#test()
main()
|
# -*- coding: utf-8 -*-
N, X, M = map(int, input().split())
mod_check_list = [False for _ in range(M)]
mod_list = [(X ** 2) % M]
counter = 1
mod_sum = (X ** 2) % M
last_mod = 0
for i in range(M):
now_mod = (mod_list[-1] ** 2) % M
if mod_check_list[now_mod]:
last_mod = now_mod
break
mod_check_list[now_mod] = True
mod_list.append(now_mod)
counter += 1
mod_sum += now_mod
loop_start_idx = 0
for i in range(counter):
if last_mod == mod_list[i]:
loop_start_idx = i
break
loop_list = mod_list[loop_start_idx:]
loop_num = counter - loop_start_idx
ans = 0
if (N - 1) <= counter:
ans = X + sum(mod_list[:N - 1])
else:
ans += X + mod_sum
N -= (counter + 1)
ans += sum(loop_list) * (N // loop_num) + sum(loop_list[:N % loop_num])
print(ans)
| 1 | 2,808,355,311,212 | null | 75 | 75 |
a=map(int,raw_input().split())
print(str(a[0]*a[1])+" "+str(2*(a[0]+a[1])))
|
n=input().rstrip().split(" ")
w=int(n[0])
l=int(n[1])
print(w*l, 2*w+2*l)
| 1 | 307,560,424,090 | null | 36 | 36 |
n=int(input())
dic={}
for i in range(1,50001):
p=int(i*1.08)
dic[p]=i
if n in dic:
print(dic[n])
else:
print(":(")
|
N=int(input())
L= list(map(int,input().split()))
L_sorted=sorted(L,reverse=False)#昇順
count=0
import bisect
for i in range(N):
for j in range(i+1,N):
a=L_sorted[i]
b=L_sorted[j]
bisect.bisect_left(L_sorted,a+b)
count+=bisect.bisect_left(L_sorted,a+b)-j-1
print(count)
| 0 | null | 148,431,806,349,632 | 265 | 294 |
def solve():
h, a = map(int, input().split())
print(-(-h//a))
if __name__ == '__main__':
solve()
|
H, A = [int(s) for s in input().split()]
print(H // A + (H % A != 0))
| 1 | 77,157,157,377,092 | null | 225 | 225 |
n=int(input())
s='*'+input()
r=s.count('R')
g=s.count('G')
b=s.count('B')
cnt=0
for i in range(1,n+1):
for j in range(i,n+1):
k=2*j-i
if k>n:
continue
if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
cnt+=1
print(r*g*b-cnt)
|
n = int(input())
s = input()
r, g, b = s.count("R"), s.count("G"), s.count("B")
cnt = 0
for i in range(n):
for j in range(i + 1, n):
try:
k = j + (j - i)
if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
cnt += 1
except:
pass
print(r * g * b - cnt)
| 1 | 36,312,400,040,478 | null | 175 | 175 |
N = int(input())
T = [[] for _ in range(N)]
for i in range(N):
A = int(input())
for j in range(A):
x, y = map(int, input().split())
x -= 1
T[i].append((x, y))
ans = 0
for i in range(1 << N):
isPossible = True
bit = [0] * N
for k in range(N):
if (i >> k) & 1:
bit[k] = 1
for idx, b in enumerate(bit):
if b == 1:
for x, y in T[idx]:
isPossible &= (bit[x] == y)
if isPossible:
ans = max(ans, bit.count(1))
print(ans)
|
n, m = list(map(int, input().split()))
s = input()
if s.find('1' * m) != -1:
print('-1')
exit(0)
i = n
rans = []
flag = True
while flag:
for j in range(m, 1 - 1, -1):
if i - j == 0:
flag = False
if i - j >= 0 and s[i - j] == '0':
rans.append(j)
i = i - j
break
print(' '.join(map(str,reversed(rans))))
| 0 | null | 130,115,605,559,368 | 262 | 274 |
s = input()
print("Yes" if ("A" in s and "B" in s) else "No")
|
print('NYoe s'['AAA'<input()<'BBB'::2])
| 1 | 54,615,270,979,900 | null | 201 | 201 |
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
S = input()
n = 0
for s in S:
n += int(s)
if n%9 == 0:
print("Yes")
else:
print("No")
if __name__=='__main__':
main()
|
N = int(input())
def digitSum(n):
s = str(n)
array = list(map(int, s))
return sum(array)
if digitSum(N) % 9 == 0:
print("Yes")
else:
print("No")
| 1 | 4,404,824,419,558 | null | 87 | 87 |
n = int(input())
d = {}
t = [0]*n
for i in range(n):
S,T = input().split()
d[S] = i
t[i] = int(T)
x = input()
idx = d[x]
print(sum(t[idx+1:]))
|
n = int(input())
playList = []
sumTerm = 0
for i in range(n) :
song = input().split()
song[1] = int(song[1])
playList.append(song)
sumTerm += song[1]
x = input()
tmpSumTerm = 0
for i in range(n) :
tmpSumTerm += playList[i][1]
if playList[i][0] == x :
print(sumTerm - tmpSumTerm)
break
| 1 | 97,390,203,750,268 | null | 243 | 243 |
import math
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
left, right = 0, max(A)
while right - left > 1:
mid = (left + right) // 2
res = 0
for i in range(N):
res = res + math.ceil(A[i]/mid) - 1
if res <= K:
right = mid
else:
left = mid
print(right)
|
N, M, K = map(int, input().split())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
total = 0
Asum = sum(A)
nA = len(A)
Bsum = 0
ans = 0
for nB in range(len(B) + 1):
if nB != 0:
Bsum += B[nB-1]
if Bsum > K:
break
while Asum + Bsum > K:
nA -= 1
Asum -= A[nA]
ans = max(nA + nB, ans)
print(ans)
| 0 | null | 8,610,902,534,952 | 99 | 117 |
def prime_factorization(n):
"""do prime factorization using trial division
Returns:
[[prime1,numbers1],[prime2,numbers2],...]
"""
res = []
for i in range(2,int(n**0.5)+1):
if n%i==0:
cnt=0
while n%i==0:
cnt+=1
n//=i
res.append([i,cnt])
if n!=1:
res.append([n,1])
return res
def main():
import sys
input = sys.stdin.readline
N = int(input())
prime_factors = prime_factorization(N)
ans = 0
for p in prime_factors:
i = 1
while i <= p[1]:
ans += 1
N /= p[0]**i
p[1] -= i
i += 1
print(ans)
if __name__ == '__main__':
main()
|
def bubbleSort(A, N):
flag = 1
i = 0
swapnum = 0
while flag:
flag = 0
for j in range(N-1, i, -1):
if A[j] < A[j-1]:
temp = A[j-1]
A[j-1] = A[j]
A[j] = temp
swapnum += 1
flag = 1
i = i + 1
return swapnum
def showlist(A):
for i in range(len(A)):
if i==len(A)-1:
print(A[i])
else:
print(A[i], end=' ')
N = eval(input())
A = list(map(int, input().split()))
swapnum = bubbleSort(A, N)
showlist(A)
print(swapnum)
| 0 | null | 8,413,791,872,760 | 136 | 14 |
from collections import Counter
def solve(n, ddd):
if ddd[0] != 0:
return 0
cnt = Counter(ddd)
if cnt[0] != 1:
return 0
max_c = max(cnt)
for i in range(max_c + 1):
if i not in cnt:
return 0
MOD = 998244353
ans = 1
for i in range(max_c):
prev = cnt[i]
curr = cnt[i + 1]
ans = ans * pow(prev, curr, MOD) % MOD
return ans
n = int(input())
ddd = list(map(int, input().split()))
print(solve(n, ddd))
|
n = int(input())
d = list(map(int,input().split()))
m = max(d)
if(d[0] != 0 or m >= n):
print(0)
else:
s = [0] * (m+1)
for i in d:s[i] += 1
if(0 in s or s[0] != 1):
print(0)
else:
t = 1
for i in range(1,m+1):
t *= s[i-1]**s[i]
t %= 998244353
print(t)
| 1 | 154,770,606,834,272 | null | 284 | 284 |
N, K = map(int, input().split())
if N >= K:
print(min(N - (N // K) * K, ((N + K) // K) * K - N))
else:
print(min(N, K - N))
|
N,K=map(int,input().split())
x=N%K
y=abs(x-K)
print(min(x,y))
| 1 | 39,477,846,781,032 | null | 180 | 180 |
import os
import sys
import numpy as np
def solve(N, M, A):
# n 以上上がる方法だけを試した時に、M 回以上の握手を行うことができるか
ok = 0
ng = 202020
while ok + 1 < ng:
c = ok+ng >> 1
cnt = 0
idx_A = N-1
for a1 in A:
while idx_A >= 0 and A[idx_A]+a1 < c:
idx_A -= 1
cnt += idx_A + 1
if cnt >= M:
ok = c
else:
ng = c
idx_A = N-1
cum_A = np.zeros(len(A)+1, dtype=np.int64)
cum_A[1:] = np.cumsum(A)
ans = 0
cnt = 0
for a1 in A:
while idx_A >= 0 and A[idx_A]+a1 < ok:
idx_A -= 1
cnt += idx_A + 1
ans += cum_A[idx_A+1] + (idx_A+1)*a1
ans -= (cnt-M) * ok
return ans
# >>> numba compile >>>
numba_config = [
[solve, "i8(i8,i8,i8[:])"],
]
if sys.argv[-1] == "ONLINE_JUDGE":
from numba import njit
from numba.pycc import CC
cc = CC("my_module")
for func, signature in numba_config:
vars()[func.__name__] = njit(signature)(func)
cc.export(func.__name__, signature)(func)
cc.compile()
exit()
elif os.name == "posix":
exec(f"from my_module import {','.join(func.__name__ for func, _ in numba_config)}")
else:
from numba import njit
for func, signature in numba_config:
vars()[func.__name__] = njit(signature, cache=True)(func)
print("compiled!", file=sys.stderr)
# <<< numba compile <<<
def main():
N, M = map(int, input().split())
A = np.array(sorted(map(int, input().split()), reverse=True), dtype=np.int64)
ans = solve(N, M, A)
print(ans)
main()
|
S=input();N=len(S);print(sum(S[i]!=S[N-i-1] for i in range(N//2)))
| 0 | null | 114,402,070,108,742 | 252 | 261 |
N, M = map(int, input().split())
A = list(set(map(lambda x : int(x)//2, input().split())))
def _gcd(a, b):
return a if b == 0 else _gcd(b, a%b)
def _lcm(a,b):
return (a*b) // _gcd(a,b)
lcm = A[0]
for ai in A[1:]:
lcm = _lcm(lcm, ai)
ret = (M//lcm + 1)//2
for ai in A[1:]:
if (lcm // ai) % 2 == 0:
ret = 0
break
print(ret)
|
s=input()
n=len(s)
a=0
t=1
d=[1]+[0]*2018
for i in range(n):
a+=int(s[n-1-i])*t
a%=2019
t*=10
t%=2019
d[a]+=1
ans=0
for i in d:
ans+=i*(i-1)//2
print(ans)
| 0 | null | 66,557,143,590,102 | 247 | 166 |
N = int(input())
print(N+N*N+N*N*N)
|
s=list(input())
t=[]
for i in range(3):
t.append(s[i])
r=''.join(t)
print(r)
| 0 | null | 12,503,203,897,188 | 115 | 130 |
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)
n = int(readline())
inc = []
dec = []
for _ in range(n):
s = readline().rstrip().decode()
d, r = 0, 0
for m in s:
d += (1 if m == '(' else -1)
r = max(r, -d)
(dec if d < 0 else inc).append((d, r))
inc.sort(key=lambda x: x[1])
dec.sort(key=lambda x: x[0] + x[1])
p1 = 0
p2 = 0
flag = True
for i in inc:
flag &= i[1] <= p1
p1 += i[0]
for d in dec:
flag &= p2 >= d[0] + d[1]
p2 -= d[0]
flag &= p1 == p2
print('Yes' if flag else 'No')
|
import sys
input = sys.stdin.readline
N = int(input())
S = [input()[:-1] for _ in range(N)]
high = []
low = []
for i in range(N):
cnt = 0
m = 0
for j in range(len(S[i])):
if S[i][j] == "(":
cnt += 1
else:
cnt -= 1
m = min(m, cnt)
if cnt > 0:
high.append([m, cnt])
else:
low.append([m-cnt, -cnt])
high.sort(reverse=True)
low.sort(reverse=True)
h = 0
for m, up in high:
if h+m < 0:
print("No")
sys.exit()
h += up
p = 0
for m, down in low:
if p+m < 0:
print("No")
sys.exit()
p += down
if h == p:
print("Yes")
else:
print("No")
| 1 | 23,536,871,550,708 | null | 152 | 152 |
from collections import Counter
n=input()[::-1]
A=[0]
num,point=0,1
for i in n:
num +=int(i)*point
num %=2019
A.append(num)
point *=10
point %=2019
count=Counter(A)
ans=0
for i,j in count.items():
if j>=2:ans +=j*(j-1)//2
print(ans)
|
# D - Multiple of 2019
from collections import Counter
s = input()
r = [0]
for d in map(int, s):
r.append((r[-1] * 10 + d) % 2019)
e = 1
for i in range(len(r) - 1, -1, -1):
r[i] = r[i] * e % 2019
e = e * 10 % 2019
def choose2(x):
return x * (x - 1) // 2
counter = Counter(r)
print(sum(choose2(counter[i]) for i in counter))
| 1 | 30,943,734,948,768 | null | 166 | 166 |
s = input()
n = len(s)
if s != s[::-1]:
print('No')
else:
s_1 = s[0:(n-1)//2]
s_2 = s[(n+1)//2:n]
if s_1 == s_1[::-1] and s_2 == s_2[::-1]:
print('Yes')
else:
print('No')
|
S = input()
array = list(S)
N = len(array)
a = (N-2)//2
b = (N+2)//2
if array[0:a+1] == array[b:N]:
print('Yes')
else:
print('No')
| 1 | 46,300,816,170,770 | null | 190 | 190 |
n=int(input())
a=1
b=1
i=0
while i<n:
a,b=b,a+b
i+=1
print(a)
|
N=int(input())
if N%2==0:
print(N//2-1)
else:
print(N//2)
| 0 | null | 76,788,410,801,478 | 7 | 283 |
# coding: utf-8
# Your code here!
INF = 2000000000
def getTheNumberOfCoin():
for i in range(n + 1):
T.append(INF)
T[0] = 0
for i in range(m):
for j in range(C[i], n + 1):
T[j] = min(T[j], T[j-C[i]] + 1)
return T
T = []
C = []
nums=list(map(int,input().split()))
n = nums[0]
m = nums[1]
nums=list(map(int,input().split()))
for i in range(m):
C.append(nums[i])
getTheNumberOfCoin()
print(T[n])
|
import sys
inf_val = 10**10
m, n = map(int, sys.stdin.readline().split())
numbers = map(int, sys.stdin.readline().split())
dp = [inf_val]*(m+1)
dp[0] = 0
for x in numbers:
for y in xrange(x, m+1):
dp[y] = min(dp[y-x]+1, dp[y])
print dp[m]
| 1 | 141,162,133,620 | null | 28 | 28 |
a,b = map(int, input().split())
def gcd(x, y):
if y < x:
x,y = y,x
while x%y != 0:
x,y = y,x%y
return y
def lcm(x, y):
return (x*y)//gcd(x,y)
print(lcm(a,b))
|
import math
num1, num2 = map(int, input().split())
print(num1 * num2 // math.gcd(num1, num2))
| 1 | 113,583,109,569,020 | null | 256 | 256 |
from collections import Counter
N,*D = map(int, open(0).read().split())
MOD = 998244353
c = Counter(D)
cnt = 1
M = max(c.keys())
ans = 1
if D[0]!=0 or c[0]!=1:
print(0)
import sys
sys.exit()
while cnt<=M:
ans *= c[cnt-1]**c[cnt]
ans %= MOD
cnt += 1
print(ans)
|
import sys
n=int(input())
ans=[0]*(n+1)
MOD=998244353
a=list(map(int,input().split()))
if a[0]!=0:
print(0)
sys.exit()
for i in range(n):
ans[a[i]]+=1
if ans[0]!=1:
print(0)
sys.exit()
fin=1
for i in range(1,max(a)+1):
fin=(fin*pow(ans[i-1],ans[i],MOD))%MOD
print(fin)
| 1 | 154,884,959,195,822 | null | 284 | 284 |
# -*- coding:utf-8 -*-
x = int(input())
if x == False:
print("1")
else:
print("0")
|
x = int(input())
if x == 0:
print("1")
else:
print("0")
| 1 | 2,920,437,818,810 | null | 76 | 76 |
import math
def pow(x, n):
ans = 1
while n:
if n % 2:
ans *= x
x *= x
n >>= 1
return ans
H = int(input())
#print(int(math.log2(H)))
print(pow(2,int(math.log2(H))+1)-1)
|
import sys
sys.setrecursionlimit(10**6)
a,b = map(int, input().split())
if a < 10 and b < 10:
print(a*b)
else:
print(-1)
| 0 | null | 119,196,011,683,502 | 228 | 286 |
N, A, B = map(int, input().split())
d = A - B if A > B else B - A
d2 = d // 2
d3 = d % 2
if d3 == 0:
ans = d2
else:
bigger = B
smaller = A
if A > B:
bigger = A
smaller = B
shortest = smaller if N - bigger + 1 > smaller else N - bigger + 1
ans = shortest + d2
print(ans)
|
n=int(input())
vec=[0 for _ in range(10050)]
for x in range(1,105):
for y in range (1,105):
for z in range (1 , 105):
sum= x*x + y*y + z*z + x*y +y*z + z*x
if sum<10050:
vec[sum]+=1
for i in range(1,n+1):
print(vec[i])
| 0 | null | 58,414,421,235,350 | 253 | 106 |
m1, d1 = map(int, input().split())
m2, d2 = map(int, input().split())
print(1 if m2 == m1 % 12 + 1 and d2 == 1 else 0)
|
m,d=map(int,input().split())
n,e=map(int,input().split())
print(1 if m!=n else 0)
| 1 | 124,146,650,254,550 | null | 264 | 264 |
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
n, quantum = map(int, sys.stdin.readline().split())
proc_q = list()
for i in range(n):
p_name, length = sys.stdin.readline().split()
proc_q.append([p_name, int(length)])
elapse = 0
while proc_q:
process = proc_q.pop(0) # retrieve 1st element
p_time = min(quantum, process[1])
elapse += p_time
if p_time == process[1]:
print(process[0], elapse)
else:
proc_q.append([process[0], process[1]-quantum])
|
n,k=map(int,input().split())
h=list(map(int,input().split()))
res=0
for i in range(n):
res += (h[i] >= k)
print(res)
| 0 | null | 89,631,042,456,748 | 19 | 298 |
x = input().split()
a, b, c =x
d = int(a)+int(b)+int(c)
if d<22:
print("win")
else:
print("bust")
|
n = int(input())
cnt = 0
for a in range(1, n):
for b in range(1, ((n-1)//a)+1):
c = n - a*b
if c <= 0: break
if a*b + c == n: cnt += 1
print(cnt)
| 0 | null | 60,918,528,440,428 | 260 | 73 |
s,t = (i for i in input().split())
print(t+s)
|
print("".join(input().split()[::-1]))
| 1 | 103,269,595,748,400 | null | 248 | 248 |
import queue
N, M = map(int, input().split())
e = [[] for _ in range(N)]
for i in range(M):
A, B = map(int, input().split())
A -= 1
B -= 1
e[A].append(B)
e[B].append(A)
# print(e)
seen = [-1] * N
k = -1
for i in range(N):
if seen[i] == -1:
k += 1
seen[i] = k
que = queue.Queue()
que.put(i)
while not que.empty():
v = que.get()
for nv in e[v]:
if seen[nv] != -1: continue
seen[nv] = k
que.put(nv)
# print(seen)
print(k)
|
N = int(input())
A = list(map(int, input().split()))
S = [0]
for i in range(N):
S.append(A[i] + S[i])
ans = float('inf')
for i in range(1, N):
ans = min(ans, abs((S[i] - S[0]) - (S[N] - S[i])))
print(ans)
| 0 | null | 72,031,347,396,766 | 70 | 276 |
n = int(input())
#nを素因数分解したリストを返す
def prime_decomposition(n):
i = 2
table = []
while i * i <= n:
while n % i == 0:
n /= i
table.append(i)
i += 1
if n > 1:
table.append(n)
return table
a = prime_decomposition(n)
list_num=[]
for i,n in enumerate(a):
if i ==0:
list_num.append(n)
if n in list_num:
continue
elif n not in list_num:
list_num.append(n)
count = 0
for i in list_num:
num = a.count(i)
handan = True
j = 1
counter = 0
while handan == True:
num -=j
if num<0:
handan =False
elif num>=0:
counter+=1
j+=1
count +=counter
print(count)
|
N = int(input())
def factorization(num):
res = []
n = num
div_max = int(num ** 0.5)
for i in range(2, div_max+1):
if n % i == 0:
count = 0
while n % i == 0:
count += 1
n //= i
res.append([i, count])
if n != 1:
res.append([n, 1])
if len(res) == 0:
res.append([num, 1])
return res
res = factorization(N)
ans = 0
for i in range(len(res)):
p, n = res[i]
if p != 1:
j = 1
while n - j >= 0:
ans += 1
n -= j
j += 1
print(ans)
| 1 | 17,009,476,854,048 | null | 136 | 136 |
print(+(' 1\n'in[*open(0)][1]))
|
a = []
b = []
a = input().split()
b = input().split()
if a[0] == b[0]:
print("0")
else:
print("1")
| 1 | 124,518,566,560,612 | null | 264 | 264 |
#coding: UTF-8
l = raw_input().split()
W = int(l[0])
H = int(l[1])
x = int(l[2])
y = int(l[3])
r = int(l[4])
if x >=r:
if y >=r:
if x <=(H -r):
if y <=(H-r):
print "Yes"
else:
print "No"
else:
print"No"
else:
print "No"
else:
print "No"
|
(W, H, x, y, r) = [int(i) for i in input().rstrip().split()]
if x + r <= W and x - r >= 0 and y + r <= H and y - r >= 0:
print('Yes')
else:
print('No')
| 1 | 455,508,187,722 | null | 41 | 41 |
def main():
N = int(input())
def enum_divisors(n):
# 約数列挙
divs = []
for i in range(1, n+1):
if i*i > n:
break
if n % i == 0:
divs.append(i)
if n//i != i:
# i が平方数でない
divs.append(n//i)
return divs
ans = 0
for k in enum_divisors(N-1):
if 2 <= k:
ans += 1
for k in enum_divisors(N):
if 2 <= k:
n = N
while n > 1 and n % k == 0:
n //= k
if n % k == 1:
ans += 1
print(ans)
if __name__ == '__main__':
main()
|
import sys
# import re
import math
import collections
# import decimal
import bisect
import itertools
import fractions
# import functools
import copy
# import heapq
import decimal
# import statistics
import queue
# import numpy as np
sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353
ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
# ===CODE===
# 約数を列挙 n=10^12までいける
def make_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)
# divisors.sort()
divisors.remove(1)
return divisors
def main():
n = ni()
ans = set()
ans.add(n)
for i in range(2, int(n ** (0.5)) + 1):
tmp = n
while tmp % i == 0:
tmp //= i
if (tmp - 1) % i == 0:
ans.add(i)
yaku = set(make_divisors(n - 1))
ans = ans | yaku
print(len(ans))
if __name__ == '__main__':
main()
| 1 | 41,220,724,015,130 | null | 183 | 183 |
L, R, d = map(int, input().split())
ans = list(map((lambda x: x % d == 0) , [i for i in range(L, R+1)])).count(True)
print(ans)
|
li =list(map(int,input().split()))
n =li[0]
m =li[1]
k =li[2]
count =0
for i in range(n,m+1):
if i%k ==0:
count +=1
print(count)
| 1 | 7,571,726,725,430 | null | 104 | 104 |
n=int(input());s=['a']
while len(s)>0:
t=s.pop(-1)
if len(t)==n:print(t)
else:
a=max(ord(i)for i in t)
for a in range(a+1,96,-1):s.append(t+chr(a))
|
l = "abcdefghij"
def dfs(a, mx):
if len(a) == n:
print(a)
return
for i, s in enumerate(l):
if i == mx+1:
dfs(a+s, mx+1)
break
else:
dfs(a+s, mx)
n = int(input())
dfs("a", 0)
| 1 | 52,451,844,380,652 | null | 198 | 198 |
n,m,l=map(int,raw_input().split())
matA=[map(int,raw_input().split()) for i in range(n)]
matB=[map(int,raw_input().split()) for j in range(m)]
for i in range(n):
for j in range(l):
print sum([matA[i][k]*matB[k][j] for k in range(m)]),
print
|
n1,n2,n3 = map(int,input().split(" "))
list1 = [list(map(int,input().split(" "))) for _ in range(n1)]
list2 = [list(map(int,input().split(" "))) for _ in range(n2)]
mat = [[0 for _ in range(n3)] for _ in range(n1)]
for i in range(n1):
for j in range(n2):
for k in range(n3):
mat[i][k] += list1[i][j] * list2[j][k]
for m in mat:
print(*m)
| 1 | 1,434,075,827,200 | null | 60 | 60 |
#B
N, K=map(int,input().split())
d=[]
A=[]
for i in range(K):
di=int(input())
Ai=list(map(int,input().split()))
d.append(di)
A.append(Ai)
A_all=[]
for i in A:
for j in i:
A_all.append(j)
A_set=list(set(A_all))
print(N-len(A_set))
|
N,K = map(int,input().split())
list1 ={i for i in range(1,N+1)}
list2 = set()
for i in range(K):
NN = int(input())
MM = map(int,input().split())
for j in MM:
list2.add(j)
ans = list1 -list2
print(len(ans))
| 1 | 24,816,428,720,358 | null | 154 | 154 |
# -*- coding: utf-8 -*-
# B
import sys
from collections import defaultdict, deque
from heapq import heappush, heappop
import math
import bisect
input = sys.stdin.readline
# 再起回数上限変更
# sys.setrecursionlimit(1000000)
h,w = map(int, input().split())
if h == 1 or w == 1:
print(1)
elif (h%2==1) and (w%2==1):
print(int((h * w // 2)+1))
else:
print(int(h*w/2))
|
n=int(input())
a=list(map(int,input().split()))
suma=sum(a)
f=0
p=suma-2*f
for i in a:
f+=i
p=min(p,abs(suma-2*f))
print(p)
| 0 | null | 96,416,042,305,858 | 196 | 276 |
#!/usr/bin/env python3
import sys
import collections as cl
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def main():
N = input()
print("No" if N[0] == N[1] == N[2] else "Yes")
main()
|
n = int(input())
if n % 2 == 0:
print(0.5)
else:
print((n+1)//2/n)
| 0 | null | 115,346,869,291,628 | 201 | 297 |
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy
class UnionFind:
def __init__(self, n: int):
self._n = n
self._parents = [i for i in range(n)]
self._rank = [1 for _ in range(n)]
def unite(self, x: int, y: int) -> None:
px = self.find(x)
py = self.find(y)
# 一致していないときはリンクをつける
if px != py:
self._link(px, py)
def _link(self, x: int, y: int):
if self._rank[x] < self._rank[y]:
self._parents[x] = y
elif self._rank[x] > self._rank[y]:
self._parents[y] = x
else:
self._parents[x] = y
self._rank[y] += 1
def same(self, x: int, y: int) -> bool:
px = self.find(x)
py = self.find(y)
return px == py
def find(self, x: int) -> int:
if self._parents[x] == x:
return x
self._parents[x] = self.find(self._parents[x])
return self._parents[x]
N, M = map(int, input().split())
uf = UnionFind(N + 1)
for i in range(M):
A, B = map(int, input().split())
uf.unite(A, B)
s = set()
for i in range(1, N + 1):
s.add(uf.find(i))
print(len(s) - 1)
|
s = input()
a = int((len(s)-1)/2)
b = int((len(s)+3)/2)
r = ''.join(reversed(s[b-1:]))
if s[:a]== s[:a:-1] and s[b-1:]== r:
print('Yes')
else:
print('No')
| 0 | null | 24,328,742,088,070 | 70 | 190 |
H,W = [int(x) for x in input().split()]
s = ""
while not H == W == 0:
for i in range(H):
for j in range(W):
s+= "." if (i+j)%2 else "#"
print(s)
s = ""
print()
H,W = [int(x) for x in input().split()]
|
import sys
read = sys.stdin.read
def main():
n = int(input())
if n <= 9 or n % 2 == 1:
print(0)
sys.exit()
n5 = 5
r = 0
while n >= n5 * 2:
r += n // (n5 * 2)
n5 *= 5
print(r)
if __name__ == '__main__':
main()
| 0 | null | 58,392,905,087,712 | 51 | 258 |
class dice():
def __init__(self, num_list):
self.dice_index = 0
i = self.dice_index
self.NS = [num_list[i], num_list[i+1], num_list[i+5], num_list[i+4]]
self.WE = [num_list[i], num_list[i+2], num_list[i+5], num_list[i+3]]
def set_dict(self, direction):
i = self.dice_index
if direction == 'N' or direction == 'W':
m = 1
elif direction == 'S' or direction == 'E':
m = -1
if direction == 'N' or direction == 'S':
self.dice_index = self.NS[m]
self.NS = self.NS[m:]+self.NS[:m]
self.WE[0] = self.NS[0]
self.WE[2] = self.NS[2]
elif direction == 'W' or direction == 'E':
self.dice_index = self.WE[m]
self.WE = self.WE[m:]+self.WE[:m]
self.NS[0] = self.WE[0]
self.NS[2] = self.WE[2]
if __name__ == '__main__':
dice_number = input()
dice_direction = input()
dice_number = dice_number.split()
Dice = dice(dice_number)
for i in range(len(dice_direction)):
Dice.set_dict(dice_direction[i])
else:
print(Dice.dice_index)
|
class Dice:
def __init__(self,a,b,c,d,e,f):
self.face1 = a
self.face2 = b
self.face3 = c
self.face4 = d
self.face5 = e
self.face6 = f
def above_face(self):
return self.face1
def roll(self, order):
if order == 'N':
tmp = self.face1
self.face1 = self.face2
self.face2 = self.face6
self.face6 = self.face5
self.face5 = tmp
elif order == 'S':
tmp = self.face1
self.face1 = self.face5
self.face5 = self.face6
self.face6 = self.face2
self.face2 = tmp
elif order == 'W':
tmp = self.face1
self.face1 = self.face3
self.face3 = self.face6
self.face6 = self.face4
self.face4 = tmp
else:
tmp = self.face1
self.face1 = self.face4
self.face4 = self.face6
self.face6 = self.face3
self.face3 = tmp
a,b,c,d,e,f = map(int,input().split())
dice1 = Dice(a,b,c,d,e,f)
l_order = list(input())
for i in range(len(l_order)):
dice1.roll(l_order[i])
print(dice1.above_face())
| 1 | 230,187,681,280 | null | 33 | 33 |
def my_print(s, a, b):
print(s[a:b+1])
def my_reverse(s, a, b):
return s[:a] + s[a:b+1][::-1] + s[b+1:]
def my_replace(s, a, b, p):
return s[:a] + p + s[b+1:]
if __name__ == '__main__':
s = input()
q = int(input())
for i in range(q):
code = input().split()
op, a, b = code[0], int(code[1]), int(code[2])
if op == 'print':
my_print(s, a, b)
elif op == 'reverse':
s = my_reverse(s, a, b)
elif op == 'replace':
s = my_replace(s, a, b, code[3])
|
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [input() for _ in range(n)]
n = int(input())
x = list(map(int, input().split()))
mix_x = min(x)
max_x = max(x)
ans = 10000000000000
for i in range(mix_x, max_x+1):
power = 0
for j in range(n):
power += (x[j] - i)**2
ans = min(ans, power)
print(ans)
| 0 | null | 33,539,044,340,220 | 68 | 213 |
x, y = map(int, input().split())
result = x * y
print(result)
|
import sys
input = sys.stdin.readline
import math
def INT(): return int(input())
def MAPINT(): return map(int, input().split())
def LMAPINT(): return list(map(int, input().split()))
def STR(): return input()
def MAPSTR(): return map(str, input().split())
def LMAPSTR(): return list(map(str, input().split()))
f_inf = float('inf')
x, y = LMAPINT()
for i in range(x+1):
for j in range(x+1):
if i + j == x and i*2 + j*4 == y:
print('Yes')
exit()
print('No')
| 0 | null | 14,693,450,535,940 | 133 | 127 |
# Binary Search
def isOK(i, key):
'''
問題に応じて返り値を設定
'''
cnt = 0
for v in a:
cnt += (v + i - 1) // i - 1
return cnt <= key
def binary_search(key):
'''
条件を満たす最小/最大のindexを求める
O(logN)
'''
ok = 10 ** 9 # 条件を満たすindexの上限値/下限値
ng = 0 # 条件を満たさないindexの下限値-1/上限値+1
while abs(ok - ng) > 1:
mid = (ok + ng) // 2
if isOK(mid, key): # midが条件を満たすか否か
ok = mid
else:
ng = mid
return ok
n, k = map(int, input().split())
a = list(map(int, input().split()))
print(binary_search(k))
|
N, K = map(int,input().split())
A = list(map(int,input().split()))
start = 0
end = 10**9
while end - start > 1:
l = (start + end)/2
l = int(l)
count = 0
for i in range(N):
q = A[i]/l
if q == int(q):
q -= 1
count += int(q)
if count <= K:
end = l
else:
start = l
print(end)
| 1 | 6,477,020,730,620 | null | 99 | 99 |
def min_item_num(weights, num_item, num_vehicle, capacity):
item_idx = 0
for _ in range(num_vehicle):
load = 0
while load + weights[item_idx] <= capacity:
load += weights[item_idx]
item_idx += 1
if item_idx == num_item:
return num_item
return item_idx
def main():
num_item, num_vehicle = list(map(int, input().split(" ")))
weights = [int(input()) for _ in range(num_item)]
left = 0
right = 10000 * 100000
while right - left > 1:
center = (left + right) // 2
min_item = min_item_num(weights, num_item, num_vehicle, center)
if num_item <= min_item:
right = center
else: # min_vehicle > num_item:
left = center
print(right)
if __name__ == "__main__":
main()
|
from sys import stdin
from math import ceil
def is_stackable(k,p,w):
if max(w) > p:
return False
s = w[0]
count = len(w)
for i in range(1, len(w)):
if s + w[i] <= p:
s += w[i]
count -= 1
else:
s = w[i]
return k >= count
def main():
n,k = map(int, stdin.readline().split())
w = [int(line) for line in stdin.readlines()]
left = max(max(w), ceil(sum(w)/k))
right = (max(w) * ceil(n/k)) + 1
while True:
mid = int((left + right) / 2)
if is_stackable(k, mid, w):
if not is_stackable(k, mid - 1, w):
print(mid)
break
right = mid
else:
if is_stackable(k, mid + 1, w):
print(mid + 1)
break
left = mid + 1
main()
| 1 | 93,313,065,600 | null | 24 | 24 |
s = input()
if s == 'RRR':
print(3)
elif s == 'RRS' or s == 'SRR':
print(2)
elif s == 'SSS':
print(0)
else:
print(1)
|
def main():
s = input()
cnt = 0
cnt2 = 0
for i in s:
if i == "R":
cnt2 += 1
else:
cnt = max(cnt, cnt2)
cnt2 = 0
print(max(cnt, cnt2))
main()
| 1 | 4,948,148,002,450 | null | 90 | 90 |
import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)
from numba import njit,i8
@njit(i8(i8,i8,i8,i8[:,:],i8[:],i8,i8))
def give_dp(N,K,mod,LR,dp,l,r):
for i in range(N):
if i > 0:
dp[i] += dp[i-1]
dp[i] %= mod
for k in range(K):
l = LR[k][0]
r = LR[k][1]
if i + l < N:
dp[i+l] += dp[i]
dp[i+1] %= mod
if i + r < N:
dp[i+r+1] -= dp[i]
dp[i+1] %= mod
return dp[-1]
def main():
N,K = map(int,input().split())
LR = [list(map(int,input().split())) for i in range(K)]
LR = np.array(LR)
mod = 998244353
dp = [0 for i in range(N)]
dp[0] = 1
dp[1] = -1
dp = np.array(dp)
res = give_dp(N,K,mod,LR,dp,0,0)
res %= mod
print(res)
main()
|
A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.insert(0, 10**6)
b.insert(0, 10**6)
xyc = [list(map(int, input().split())) for _ in range(M)]
ans = min(a) + min(b)
for n in xyc:
ans = min(ans, a[n[0]] + b[n[1]] - n[2])
print(ans)
| 0 | null | 28,139,330,037,984 | 74 | 200 |
# coding: utf-8
N, Q = map(int,input().split())
#N,Q=map(int,input().split())
#親のノードを格納 2の親が4のとき par[2]=4
par=[0]
#木の深さを格納 根が2の深さが3の時 rank[2]=3
rank=[0]
#初期化
for i in range(1,N+1):
par.append(i)
rank.append(0)
#xの根を探す、同時に通過したノードすべてを根に直接つける
def find(x):
#xの親がxならば根なのでxを返す
if par[x]==x:
return x
else:
#違う場合は親の親を捜し自分の親にする
par[x] = find(par[x])
#再帰的に行うと根が見つかる
return par[x]
#xが属する木とyが属する木を併合する
def unite(x,y):
#根を探す
root_x=find(x)
root_y=find(y)
#根が同じなら元からつながっているので終わり
if root_x == root_y:
return
#ランクが大きい木の根の直下にランクが小さい木を結合
if rank[root_x] < rank[root_y]:
par[root_x] = root_y
else:
par[root_y] = root_x
#ランクが等しいときのみランクが増える#なぜ#yにxが結合?
if rank[root_x]==rank[root_y]:
rank[root_x]+=1
#xとyが同じ木に存在するかを調べる
def same(x,y):
return find(x)==find(y)
#main
for i in range(Q):
a,b=map(int,input().split())
unite(a, b)
res = []
for i in range(1, N+1):
res.append(find(i))
ans = len(set(res))-1
print(ans)
|
for i in range(1,10):
for j in range(1,10):
print('%sx%s=%s'%(i,j,i*j))
| 0 | null | 1,143,789,375,690 | 70 | 1 |
S = raw_input()
n = int(raw_input())
for i in range(n):
command = (raw_input()).split(" ")
if command[0] == "print":
a = int(command[1])
b = int(command[2])
print S[a:b+1]
elif command[0] == "reverse":
a = int(command[1])
b = int(command[2])
T = S[a:b+1]
S = S[0:a] + T[::-1] + S[b+1:]
else:
a = int(command[1])
b = int(command[2])
S = S[0:a] + command[3] + S[b+1:]
|
import sys
MAXINT = 1000000001
n = int(input())
S = list(map(int,input().split()))
def merge(A, l, m, r, cnt):
n1 = m - l
n2 = r - m
L = [A[i] for i in range(l, l+n1)]
R = [A[i] for i in range(m, m+n2)]
L.append(MAXINT)
R.append(MAXINT)
i = 0
j = 0
for k in range(l,r):
cnt += 1
if L[i] <= R[j]:
A[k] = L[i]
i += 1
else:
A[k] = R[j]
j += 1
return cnt
def mergesort(A, l, r, cnt):
if l + 1 < r:
m = (l + r) // 2
cnt = mergesort(A, l, m, cnt)
cnt = mergesort(A, m, r, cnt)
cnt = merge(A,l,m,r, cnt)
return cnt
cnt = 0
cnt = mergesort(S, 0, n, cnt)
print(" ".join(map(str,S)))
print(cnt)
| 0 | null | 1,104,021,456,888 | 68 | 26 |
N = int(input())
mod = 1000000007
def powmod(x,y):
res = 1
for i in range(y):
res = res*x%mod
return res
ans = powmod(10,N)-powmod(9,N)-powmod(9,N)+powmod(8,N)
ans = ans%mod
ans = (ans + mod)%mod
print(ans)
|
import math
n=int(input())
print( (pow(10,n)-pow(9,n)*2+pow(8,n))%1000000007)
| 1 | 3,111,710,911,650 | null | 78 | 78 |
n=int(input())
dic={}
for i in range(1,50001):
p=int(i*1.08)
dic[p]=i
if n in dic:
print(dic[n])
else:
print(":(")
|
n=int(input())
for x in range(n+1):
if x*108//100==n:
print(x)
break
else:
print(":(")
| 1 | 125,596,020,719,392 | null | 265 | 265 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.