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 = [int(i) for i in input().rstrip().split(" ")]
cnt = 0
for i in range(a, b+1):
if c % i == 0:
cnt += 1
print(cnt)
|
a, b, c = map(int, input().split())
l = [n for n in range (a,b+1) if c % n == 0]
print(len(l))
| 1 | 567,595,332,876 | null | 44 | 44 |
import sys
INF = 10**60
inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inm())
instr = lambda: sys.stdin.readline()
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())
n = inint()
min_sum = INF
i = 0
j = 0
for k in range(1,n):
if k > n**0.5:
break
if n/k % 1 != 0:
continue
i = k
j = n/k
if i+j < min_sum:
min_sum = i+j
print(int(min_sum - 2))
|
import sys
def div(x, y):
if x % y == 0:
print y
else:
div(y, x%y)
line = sys.stdin.readline()
x, y = line.split(" ")
x = int(x)
y = int(y)
if x < y:
x, y = y, x
if x == y:
print x
else:
div(x, y)
| 0 | null | 80,687,296,444,610 | 288 | 11 |
from collections import deque,defaultdict
import bisect
N = int(input())
S = [input() for i in range(N)]
def bend(s):
res = 0
k = 0
for th in s:
if th == '(':
k += 1
else:
k -= 1
res = min(k,res)
return res
species = [(bend(s),s.count('(')-s.count(')')) for s in S]
ups = [(b,u) for b,u in species if u > 0]
flats = [(b,u) for b,u in species if u == 0]
downs = [(b,u) for b,u in species if u < 0]
ups.sort()
high = 0
for b,u in ups[::-1] + flats:
if high + b < 0:
print('No')
exit()
high += u
rdowns = [(b-u,-u) for b,u in downs]
rdowns.sort()
high2 = 0
for b,u in rdowns[::-1]:
if high2 + b < 0:
print('No')
exit()
high2 += u
if high == high2:
print('Yes')
else:
print('No')
|
import sys
input = lambda: sys.stdin.readline().rstrip()
n = int(input())
s = []
for _ in range(n):
s.append(input())
inc = []
zeros = []
dec = []
for i, j in enumerate(s):
minimum = 0
now = 0
for k in j:
if k == "(":
now += 1
else:
now -= 1
minimum = min(minimum, now)
if now > 0:
inc.append([minimum, i])
elif now < 0:
dec.append([minimum - now, i])
else:
zeros.append([minimum, i])
inc.sort(reverse=True)
dec.sort()
now = 0
for i, j in inc:
for k in s[j]:
if k == "(":
now += 1
else:
now -= 1
if now < 0:
print("No")
sys.exit()
for i, j in zeros:
for k in s[j]:
if k == "(":
now += 1
else:
now -= 1
if now < 0:
print("No")
sys.exit()
for i, j in dec:
for k in s[j]:
if k == "(":
now += 1
else:
now -= 1
if now < 0:
print("No")
sys.exit()
ans = "Yes" if now == 0 else "No"
print(ans)
| 1 | 23,764,031,770,750 | null | 152 | 152 |
n=int(input())
s=input()
S=[]
for i in s:
if ord(i)+n > 90:
S.append(chr(ord(i)+n-26))
else:
S.append(chr(ord(i)+n))
print(''.join(S))
|
N = int(input())
S = list(input())
a = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
ans = ''
for s in S:
num = a.index(s) + N
if num <= 25: ans += a[num]
else: ans += a[num-26]
print(ans)
| 1 | 134,838,568,376,600 | null | 271 | 271 |
a, b, c, d = map(int, input().split())
taka = 0
aoki = 0
while a > 0:
a = a - d
taka += 1
while c > 0:
c = c - b
aoki += 1
if taka < aoki:
print("No")
else:
print("Yes")
|
A,B,C,D = map(int,input().split())
if -A//D<=-C//B:
print("Yes")
else:
print("No")
| 1 | 29,806,686,823,290 | null | 164 | 164 |
suit = ['S', 'H', 'C', 'D']
cards = [[i, j] for i in suit for j in map(str, range(1, 14))]
for i in range(input()):
cards.remove(raw_input().split())
for i in cards:
print i[0],i[1]
|
pictures = ['S','H','C','D']
card_li = []
for i in range(4):
for j in range(1,14):
card_li.append(pictures[i] + ' ' + str(j))
n = int(input())
for i in range(n): del card_li[card_li.index(input())]
for i in card_li:
print(i)
| 1 | 1,043,082,949,380 | null | 54 | 54 |
n, k, c = map(int, input().split())
s = input()
l = []
r = []
res = []
def fun(string, pos):
counter = 0
consec_counter = c + 1
res = []
while pos < n:
# print(pos)
if counter == k:
break
if consec_counter >= c and string[pos] == 'o':
counter += 1
consec_counter = 0
res.append(pos)
else:
consec_counter += 1
pos += 1;
# print(res)
return res
def invfun(string, pos):
counter = 0
consec_counter = c + 1
res = []
while pos >= 0:
if counter == k:
break
# print(pos)
if consec_counter >= c and string[pos] == 'o':
counter += 1
consec_counter = 0
res.append(pos)
else:
consec_counter += 1
pos -= 1;
# print(res)
return res
little_res = fun(s, 0)
lres = []
if len(little_res) == k:
lres = little_res
else:
exit()
little_res = invfun(s, n - 1)
rres = []
if len(little_res) == k:
rres = little_res
else:
exit()
# print(lres)
# print(rres)
for i in range(k):
if lres[i] == rres[k - 1 - i]:
print(lres[i] + 1)
|
n, k, c = map(int, input().split())
s = input()
left = [-1 for _ in range(k)]
right = [-1 for _ in range(k)]
bef = -10 ** 10
cnt = 0
for i, ss in enumerate(s):
if ss == "o" and i - bef > c:
if cnt == k:
exit()
left[cnt] = i
bef = i
cnt += 1
cnt = 0
bef = -10 ** 10
for i, ss in enumerate(s[::-1]):
if ss == "o" and i - bef > c:
if cnt == k:
exit()
right[k - 1 - cnt] = n - 1 - i
bef = i
cnt += 1
for ll, rr in zip(left, right):
if ll == rr:
print(ll + 1)
| 1 | 40,507,733,472,698 | null | 182 | 182 |
ln = input().split()
print(ln[1]+ln[0])
|
N=input().split(" ");print(N[1]+N[0])
| 1 | 103,024,140,261,440 | null | 248 | 248 |
import sys
sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7
def resolve():
n = int(input())
X = input()
cnt_init = X.count("1")
x_init = int(X, 2)
a = x_init % (cnt_init + 1)
if cnt_init - 1 != 0:
b = x_init % (cnt_init - 1)
for i in range(n):
if X[i] == "0":
cnt = cnt_init + 1
x = (a + pow(2, (n - i - 1), cnt)) % cnt
res = 1
else:
cnt = cnt_init - 1
if cnt != 0:
x = (b - pow(2, (n - i - 1), cnt)) % cnt
res = 1
else:
x = 0
res = 0
while x:
cnt = bin(x).count("1")
x %= cnt
res += 1
print(res)
if __name__ == '__main__':
resolve()
|
def solve(n,m):
if not n:return 1
n%=m
return-~solve(n,bin(n).count('1'))
n=int(input())
s=input()
a=int(s,2)
*x,=map(int,s)
h=sum(x)
try:a,b=a%(h-1),a%(h+1)
except:b=a%(h+1)
for i in range(n):
if x[i]:
if not h-1:
print(0)
continue
x[i]=0
t=(a-pow(2,~i+n,h-1))%(h-1)
else:
x[i]=1
t=(b+pow(2,~i+n,h+1))%(h+1)
r=solve(t,bin(t).count('1'))
x[i]^=1
print(r)
| 1 | 8,267,606,079,260 | null | 107 | 107 |
a,b=[int(x) for x in input().split()]
c=0
for i in range(1,12501):
if int(0.08*i)==a and int(0.1*i)==b:
ans=i
c=1
break
if c==1:
print(ans)
else:
print(-1)
|
import statistics
def main():
while True:
try:
N = int(input())
S = tuple(map(int, input().split()))
print(statistics.pstdev(S))
except EOFError:
break
main()
| 0 | null | 28,366,384,813,760 | 203 | 31 |
import math
X= int(input())
for A in range(-120,121):
for B in range(-120,A+1):
if A**5-B**5 == X:
print(A,B)
exit()
|
def main():
x = int(input())
for b in range(-200, 200):
for a in range(b + 1, 200):
if a ** 5 - b ** 5 == x:
print(a, b)
exit()
if __name__ == "__main__":
main()
| 1 | 25,543,842,549,500 | null | 156 | 156 |
import math
n = int(input())
m = math.floor(math.sqrt(n)) + 1
ans = 10**12
for a in range(1,m+1):
b = n/a
if b.is_integer():
move = a+b-2
else:
continue
ans = min(ans,move)
print(int(ans))
|
N = int(input())
for i in range(1,int(N**(1/2)) + 1)[::-1]:
if N%i == 0:
print(i + N//i - 2)
exit()
| 1 | 161,485,289,811,482 | null | 288 | 288 |
N=int(input())
robot=[]
for _ in range(N):
x,l=map(int,input().split())
robot.append((x+l,x-l))
robot.sort()
tmp=-10**10
ans=0
for i in range(N):
end,start=robot[i]
if tmp<=start:
tmp=end
ans+=1
print(ans)
|
input()
P=map(int,input().split())
ans,mn=0,2*10**5
for x in P:
if mn>=x: ans+=1; mn=x
print(ans)
| 0 | null | 87,651,071,796,892 | 237 | 233 |
def main():
M1, D1 = map(int, input().split())
M2, D2 = map(int, input().split())
print(int(M1 != M2))
if __name__ == '__main__':
main()
|
### -*- coding: utf-8 -*-
prev = input().split()[0]
next = input().split()[0]
if prev != next:
print("1")
else:
print("0")
| 1 | 124,194,103,736,830 | null | 264 | 264 |
def main():
n = int(input())
s = input()
if n < 4:
print(0)
return
ans = s.count("R") * s.count("G") * s.count("B")
for i, si in enumerate(s[:-2]):
for j, sj in enumerate(s[i+1:-1]):
if si == sj:
continue
if i + 2*j+2 < n and si != s[i + 2*j+2] != sj:
ans -= 1
print(ans)
if __name__ == '__main__':
main()
|
H, W, K = map(int, input().split())
S = [list(input()) for _ in range(H)]
ans = [[0] * W for _ in range(H)]
def calc(h, w, k):
#上下方向の限界を決める
up = h
for i in range(1, h + 1):
if S[h - i][w] == '.' and ans[h - i][w] == 0:
if h == i:
up = 0
continue
else:
up = h - (i - 1)
break
down = h
for i in range(1, H - h):
if S[h + i][w] == '.' and ans[h + i][w] == 0:
if i == H - h - 1:
down = H - 1
continue
else:
down = h + (i - 1)
break
# print ('up, down =', up, down, i)
# up から down までの縦の長さの長方形を作る。
for i in range(up, down + 1):
ans[i][w] = k
flag = True
for i in range(1, w + 1):
for j in range(up, down + 1):
if ans[j][w - i] == 0 and S[j][w - i] == '.':
continue
else:
flag = False
break
if not flag:
break
for j in range(up, down + 1):
ans[j][w - i] = k
flag = True
for i in range(1, W - w):
for j in range(up, down + 1):
if ans[j][w + i] == 0 and S[j][w + i] == '.':
continue
else:
flag = False
break
if not flag:
break
for j in range(up, down + 1):
ans[j][w + i] = k
k = 1
for w in range(W):
for h in range(H):
if S[h][w] == '#':
# print (h, w)
calc(h, w, k)
k += 1
for tmp in ans:
print (*tmp, sep = ' ')
| 0 | null | 89,759,989,497,542 | 175 | 277 |
N = int(input())
ans = [0] * (N+1)
for i in range(N+1):
if i % 3 != 0 and i % 5 != 0 and i % 15 != 0:
ans[i] = ans[i-1] + i
else:
ans[i] = ans[i-1]
print(ans[-1])
|
n = int(input())
*a, = map(int, input().split())
ans = 1000
for i, j in zip(a, a[1:]):
if i<j:
ans = ans//i*j + ans%i
print(ans)
| 0 | null | 21,075,179,594,240 | 173 | 103 |
n = int(input())
a = list(map(int, input().split()))
cnt = 0
ave = sum(a)/2
for i in range(n):
cnt += a[i]
if cnt >= ave:
ans = min(cnt*2-ave*2, ave*2-(cnt-a[i])*2)
break
print(int(ans))
|
from itertools import accumulate
n = int(input())
A = list(map(int,input().split()))
B = list(accumulate(A))
m = float("inf")
for i in B:
m = min(m,abs(2*i-B[-1]))
print(m)
| 1 | 142,394,625,262,632 | null | 276 | 276 |
#coding:utf-8
#1_6_A 2015.4.1
n = int(input())
numbers = list(map(int,input().split()))
for i in range(n):
if i == n - 1:
print(numbers[-i-1])
else:
print(numbers[-i-1], end = ' ')
|
i=1
x = int(input())
while x != 0 :
print("Case {0}: {1}".format(i, x))
x = int(input())
i=i+1
| 0 | null | 714,860,459,566 | 53 | 42 |
#B
N, A, B=map(int, input().split())
quotient=N//(A+B)
remainder=N%(A+B)
quo_bball=quotient*A
if remainder <=A:
rem_bball=remainder
else:
rem_bball=A
print(quo_bball+rem_bball)
|
from sys import exit
import math
import collections
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))
n,k = mi()
h = li()
h.sort()
h.reverse()
sum = 0
for i in range(k,n,1):
sum += h[i]
print(sum)
| 0 | null | 67,593,936,856,182 | 202 | 227 |
# エイシング プログラミング コンテスト 2020: B – An Odd Problem
N = int(input())
a = [int(i) for i in input().split()]
num_odd = 0
for idx in range(1, N + 1, 2):
if a[idx -1] % 2 == 1:
num_odd += 1
print(num_odd)
|
# import itertools
import math
# import sys
# import numpy as np
# K = int(input())
# S = input()
# n, *a = map(int, open(0))
A, B, H, M = map(int, input().split())
# H = list(map(int, input().split()))
# Q = list(map(int, input().split()))
# S = input()
# d = sorted(d.items(), key=lambda x:x[0]) # keyでsort
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement([i for i in range(1, M + 1)], N))
# print(a[0][0])
# print(conditions[0])
kakudo_H = (H * 60 + M) / 720 * 360
kakudo_M = M / 60 * 360
print(math.sqrt(A ** 2 + B ** 2 - 2 * A * B * math.cos(math.radians(abs(kakudo_H - kakudo_M)))))
| 0 | null | 13,839,900,436,320 | 105 | 144 |
import sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) #空白あり
N,K = LI()
A = LI()
for i in range(N):
A[i] -= 1
B = [0] # Aの累積和をKで割ったもの
for i in range(N):
B.append((B[-1]+A[i]) % K)
from collections import defaultdict
# 要素の個数がKを超えないことに注意
if N < K:
d = defaultdict(int)
for n in B:
d[n] += 1
ans = 0
for n in d.keys():
m = d[n]
ans += m*(m-1)//2
print(ans)
else:
d = defaultdict(int)
ans = 0
for i in range(N): # A[i]から始まる部分列を考える
if i == 0:
for j in range(K):
d[B[j]] += 1
ans += d[B[i]]-1
elif i <= N-K+1:
d[B[K+i-1]] += 1
d[B[i-1]] -= 1
ans += d[B[i]]-1
else:
d[B[i-1]] -= 1
ans += d[B[i]]-1
print(ans)
|
import sys
from itertools import accumulate
from collections import defaultdict
input = sys.stdin.readline
N,K=map(int,input().split());A=list(map(int,input().split()))
S_=list(map(lambda t: (t[1]-t[0])%K,enumerate(accumulate([0]+A))))
count=0
C=defaultdict(int)
for t in range(1,N+1):
C[S_[t-1]]+=1
if t-K>=0:
C[S_[t-K]]-=1
count+=C[S_[t]]
print(count)
| 1 | 137,041,800,888,668 | null | 273 | 273 |
n = int(input().rstrip())
r = int(n / 2) + (n % 2)
print(r / n)
|
n=int(input())
start=[[[0 for i in range(10)] for j in range(3)] for k in range(4)]
for i in range(n):
b,f,r,v=[int(i) for i in input().split()]
start[b-1][f-1][r-1]+=v
for i in range(4):
for j in range(3):
for k in range(10):
print(" ",end="")
print(start[i][j][k],end="")
print()
if i<=2:
for m in range(20):
print("#",end="")
print()
| 0 | null | 89,079,972,369,600 | 297 | 55 |
K,N=map(int,input().split())
A=[int(i) for i in input().split()]
dlist=[]
for i in range(1,N):
d=A[i]-A[i-1]
dlist.append(d)
dlist.append(A[0]+K-A[N-1])
print(K-max(dlist))
|
k,n=map(int,input().split())
l=list(map(int,input().split()))
diff=[]
l.append(l[0]+k)
for i in range(n):
diff.append(l[i+1]-l[i])
print(sum(diff)-max(diff))
| 1 | 43,468,303,646,812 | null | 186 | 186 |
# -*- coding: utf-8 -*-
S1 = 1
r = input()
R = int(r)
S2 = R ** 2
result = S2 / S1
print(int(result))
|
def resolve():
N, A, B = [int(i) for i in input().split()]
if (B - A) % 2 == 0:
print((B - A) // 2)
else:
print(min(A, N - B + 1) + ((B - A) // 2))
resolve()
| 0 | null | 127,053,033,808,708 | 278 | 253 |
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
x = int(input())
def cond(x):
q, r = x // 100, x % 100
q5, r5 = r // 5, r % 5
if r5 == 0:
return q >= q5
else:
return q >= q5 + 1
if cond(x):
print(1)
else:
print(0)
|
x = int(input())
cnt = x // 100
y = x % 100
print(1 if cnt * 5 >= y else 0)
| 1 | 127,205,475,240,962 | null | 266 | 266 |
n = int(input())
a_list = [int(i) for i in input().split()]
a_dict = {}
for a in a_list:
if a in a_dict:
a_dict[a] += 1
else:
a_dict[a] = 1
q = int(input())
si = 0
for a in a_dict:
si += a * a_dict[a]
for _ in range(q):
bi, ci = [int(i) for i in input().split()]
if not bi in a_dict:
print(si)
continue
if not ci in a_dict:
a_dict[ci] = 0
a_dict[ci] += a_dict[bi]
si += (ci - bi) * a_dict[bi]
a_dict[bi] = 0
print(si)
|
import numpy as np
from collections import Counter
N=int(input())
Alist=list(map(int,input().split()))
Q=int(input())
bc=[]
for _ in range(Q):
bc.append(list(map(int,input().split())))
count=Counter(Alist)
result=0
for key,value in count.items():
result+=key*value
for i in range(Q):
if bc[i][0] in count.keys():
result+=(bc[i][1]-bc[i][0])*count[bc[i][0]]
count[bc[i][1]]+=count[bc[i][0]]
count[bc[i][0]]=0
print(result)
| 1 | 12,128,860,943,768 | null | 122 | 122 |
h = int(input())
ans = 1
while h > 0:
h //= 2
ans *= 2
print(ans-1)
|
print(2**int(input()).bit_length()-1)
| 1 | 80,113,137,261,410 | null | 228 | 228 |
N=int(input())
item={"AC":0,"WA":0,"TLE":0,"RE":0}
for n in range(N):
S=input()
if S=="AC":
item["AC"]+=1
elif S=="WA":
item["WA"]+=1
elif S=="TLE":
item["TLE"]+=1
else:
item["RE"]+=1
print("AC x",end=" ")
print(item["AC"])
print("WA x",end=" ")
print(item["WA"])
print("TLE x",end=" ")
print(item["TLE"])
print("RE x",end=" ")
print(item["RE"])
|
# Problem B - Magic 2
# input
A, B, C = map(int, input().split())
K = int(input())
# B check
while B<=A:
B = B * 2
K -= 1
# C check
while C<=B:
C = C * 2
K -= 1
# output
if K>=0:
print("Yes")
else:
print("No")
| 0 | null | 7,883,493,208,178 | 109 | 101 |
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
import math
DEG60 = math.pi/3
def repl_edge(n, edges):
# print('n=',n, 'edges=', len(edges))
if n == 0:
return edges
new_list = list()
for edge in edges: # single tupple
p1 = edge[0]
p2 = edge[1]
p_angle = edge[2]
s = ((p1[0]*2+p2[0])/3, (p1[1]*2+p2[1])/3)
t = ((p1[0]+p2[0]*2)/3, (p1[1]+p2[1]*2)/3)
r = abs((t[0]-s[0])**2 + (t[1]-s[1])**2) ** 0.5
angle = p_angle + DEG60
u = (s[0] + math.cos(angle)*r, s[1] + math.sin(angle)*r)
# print('r=', r, 'u=', u, 'angle=', angle)
new_list += [(p1, s, p_angle), (s, u, p_angle+DEG60), \
(u, t, p_angle-DEG60), (t, p2, p_angle)]
new_list = repl_edge(n-1, new_list)
return new_list
n = int(sys.stdin.readline())
p1 = (0.0, 0.0)
p2 = (100.0, 0.0)
edges = [(p1, p2, 0.0)]
edges = repl_edge(n, edges)
for edge in edges:
print(round(edge[0][0],6), round(edge[0][1],6))
print(round(edges[-1][1][0],6), round(edges[-1][1][1],6))
|
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)
def main():
h,w,k = LI()
aa = [[int(c) for c in S()] for _ in range(h)]
def f(hs):
l = len(hs)
c = [0] * l
r = l - 1
for i in range(w):
nc = [0] * l
bf = False
for j in range(l):
for h in hs[j]:
nc[j] += aa[h][i]
if nc[j] > k:
return -1
if nc[j] + c[j] > k:
bf = True
if bf:
r += 1
c = nc
else:
for j in range(l):
c[j] += nc[j]
return r
r = inf
for bi in range(2**(h-1)):
hs = [[0]]
for i in range(h-1):
if 2**i & bi > 0:
hs.append([])
hs[-1].append(i+1)
t = f(hs)
if t >= 0 and r > t:
r = t
return r
print(main())
| 0 | null | 24,491,866,564,312 | 27 | 193 |
from math import ceil
H, A = map(int, input().split())
ans = ceil(round(H / A, 6))
print(ans)
|
H,A = input().split()
H = int(H)
A = int(A)
if H <= A :
print('1')
elif not H % A == 0 :
print(str(H//A + 1))
else :
print(str(H//A))
| 1 | 77,048,155,886,428 | null | 225 | 225 |
n = int(input())
a = list(map(int, input().split()))
x = 1
if 0 in a:
x = 0
else:
for i in range(n):
if x != -1:
x = x * a[i]
if x > 10 ** 18:
x = -1
print(x)
|
N = int(input())
A = list(map(int,input().split()))
if 0 in A:
prod = 0
else:
prod = 1
for i in range(N):
prod *= A[i]
if prod>10**18:
prod = -1
break
print(prod)
| 1 | 16,067,295,245,472 | null | 134 | 134 |
x1,x2,x3,x4,x5,x6 = map(int,(input().split()))
t = input()
x=x1
L=[x1,x2,x3,x4,x5,x6]
n= len(t)
T=[]
M=[]
for i in range(n):
T.append(t[:1])
t=t[1:]
for i in T:
if i is "S":
L = [L[4],L[0],L[2],L[3],L[5],L[1]]
elif i is "E":
L = [L[3], L[1], L[0], L[5], L[4], L[2]]
elif i is "N":
L = [L[1], L[5], L[2], L[3], L[0], L[4]]
else:
L = [L[2], L[1], L[5], L[0], L[4], L[3]]
print(L[0])
|
# 23-Structure_and_Class-Dice_I.py
# ???????????? I
# ?¬?????±??????????????????????????????????????????¢??????????????\??¬????????§????????????????????°????????????????????????????????????
# ????????????????????¢??????????????¨????????? 1 ?????? 6 ????????????????????????????????????????????????
# ??\?????¨??????????????????????????¢?????????????????????????????°?????¨?????¢?????????????????????????????????????????§???
# ????????????????????¢?????°?????????????????????????????????
# ????????\??¬????????§??????????????¶?????????????????¨?????????????????????????????§?????????????????????????????????????????¨????????????
# Input
# ?????????????????¢?????°???????????????????????????????????????????????????????????§?????????????????????
# ???????????????????????¨????????????????????????????????????????????????????????????????????????????????????????????¨????????? E???N???S???W ????????????????????§??????
# Output
# ???????????????????????????????????????????????????????????¢?????°???????????????????????????????????????
# Constraints
# 0???0??? ??\?????????????????????????????¢?????°??? ???100???100
# 0???0??? ???????????¨????????????????????? ???100???100
# Note
# ?¶???????????????? Dice III, Dice IV ??§???????????°????????????????????±????????§???????????????????????????????§?????????§?????????????????????????????????
# Sample Input 1
# 1 2 4 8 16 32
# SE
# Sample Output 1
# 8
# ?????¢??? 1, 2, 4, 8, 16, 32 ??¨??????????????????????????? S ??????????????¢???????????????E ??????????????¢????????¨????????¢?????°?????? 8 ??????????????????
# Sample Input 2
# 1 2 4 8 16 32
# EESWN
# Sample Output 2
# 32
class Dice:
def __init__(self, dice_num):
self.side_top=1
self.side_bot=6
self.side_Nor=5
self.side_Eas=3
self.side_Sau=2
self.side_Wes=4
self.dice_num = dice_num
def op_N(self):
self.side_top, self.side_bot, self.side_Nor, self.side_Sau =\
self.side_Sau, self.side_Nor, self.side_top, self.side_bot
def op_E(self):
self.side_top, self.side_bot, self.side_Eas, self.side_Wes =\
self.side_Wes, self.side_Eas, self.side_top, self.side_bot
def op_S(self):
self.side_top, self.side_bot, self.side_Nor, self.side_Sau =\
self.side_Nor, self.side_Sau, self.side_bot, self.side_top
def op_W(self):
self.side_top, self.side_bot, self.side_Eas, self.side_Wes =\
self.side_Eas, self.side_Wes, self.side_bot, self.side_top
def print_side_top(self):
print( dice_num[self.side_top-1] )
def print_side_all(self):
print( "top:{}, bot:{}, Nor:{}, Eas{}, Sau:{}, Wes,{}.".format(self.side_top, self.side_bot, self.side_Nor, self.side_Eas, self.side_Sau, self.side_Wes ) )
dice_num = list( map(int, input().split()))
op = input()
dice_roll = Dice(dice_num)
for i in op:
if i == "N":
dice_roll.op_N()
elif i =="E":
dice_roll.op_E()
elif i =="S":
dice_roll.op_S()
elif i =="W":
dice_roll.op_W()
else:
print("?????°??°")
dice_roll.print_side_top()
| 1 | 226,837,079,322 | null | 33 | 33 |
def nCr(n,r,mod):
p=1
q=1
# print(n,r)
for i in range(r):
q *= i+1
q %= mod
p *= n-i
p %= mod
# print(p,q)
return (p * pow(q,mod-2,mod)) % mod
s=int(input())
mod = 10**9+7
ans = 0
for i in range(1,s//3+1):
if i==1:
h=1
else:
h=nCr(s-3*i+i-1 ,i-1, mod)
ans += h
ans %= mod
# print(h)
print(ans)
|
def main():
a, b = map(int, input().split())
maxv = int((101 / 0.1) - 1)
for i in range(maxv+1):
if (i*8//100 == a) and (i*10//100 == b):
print(i)
exit()
print(-1)
if __name__ == '__main__':
main()
| 0 | null | 29,687,203,260,548 | 79 | 203 |
N, M = map(int, input().split())
nextroom = [[] for _ in range(N)]
for _ in range(M):
A, B = map(int, input().split())
nextroom[A-1].append(B-1)
nextroom[B-1].append(A-1)
sgn = [-1 for _ in range(N)]
sgn[0] = 0
depth = [float('inf') for _ in range(N)]
depth[0] = 0
now = [0]
nextvisit = []
for j in range(1, M+1):
for n in now:
for i in nextroom[n]:
if depth[i] > j:
nextvisit.append(i)
sgn[i] = n
depth[i] = j
now = [] + nextvisit
nextvisit = []
if -1 in sgn:
print('No')
else:
print('Yes')
for k in range(1, len(sgn)):
print(sgn[k]+1)
|
from collections import deque
N, M = [int(x) for x in input().split()]
conn = [[] for _ in range(N + 1)]
for i in range(M):
A, B = [int(x) for x in input().split()]
conn[A].append(B)
conn[B].append(A)
q = deque([1])
signpost = [0] * (N + 1)
while q:
v = q.popleft()
for w in conn[v]:
if signpost[w] == 0:
signpost[w] = v
q.append(w)
print('Yes')
for i in range(2, N + 1):
print(signpost[i])
| 1 | 20,541,399,484,682 | null | 145 | 145 |
A=[]
b=[]
n=list(map(int,input().split()))
for i in range(n[0]):
A.append(list(map(int,input().split())))
for i in range(n[1]):
b.append(int(input()))
for i in range(n[0]):
num=0
for j in range(n[1]):
num=num+A[i][j]*b[j]
print(num)
|
m1, d1 = input().split()
m2, d2 = input().split()
if m1==m2:
print(0)
else:
print(1)
| 0 | null | 62,439,862,367,334 | 56 | 264 |
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y
a,b = input().split()
b = b[:-3] + b[-2:]
ans = int(a) * int(b)
ans = ans // 100
print(ans)
|
x = int(input())
while True:
flag =0
for i in range(2,x//2):
if x%i==0:
flag =1
break
if flag ==0:
print(x)
exit()
x = x+1
| 0 | null | 61,147,209,186,892 | 135 | 250 |
import sys
for i in sys.stdin:
scores = list(map(int, i.split()))
m = scores[0]
f = scores[1]
r = scores[2]
if m == f == r == -1:
break
elif (m == -1) or (f == -1):
print("F")
elif m + f >= 80:
print("A")
elif 65 <= (m + f) < 80:
print("B")
elif 50 <= (m + f) < 65:
print("C")
elif 30 <= (m + f) < 50:
if r >= 50:
print("C")
else:
print("D")
else:
print("F")
|
while True:
a=[int(x) for x in input().split()]
if a[0]==a[1]==0:
break
else:
for x in range(a[0]):
if x%2!=0:
for y in range(a[1]):
if y%2!=0:
print("#", end="")
else:
print(".", end="")
print("\n",end="")
elif x%2==0:
for y in range(a[1]):
if y%2!=0:
print(".", end="")
else:
print("#", end="")
print("\n",end="")
print("")
| 0 | null | 1,062,315,961,300 | 57 | 51 |
import sys
input = sys.stdin.readline
import math
def read():
N, K = map(int, input().strip().split())
A = list(map(int, input().strip().split()))
return N, K, A
def solve(N, K, A):
d = [0 for i in range(N+1)]
for k in range(K):
for i in range(N):
d[i] = 0
for i in range(N):
l = max(0, i-A[i])
r = min(N, i+1+A[i])
d[l] += 1
d[r] -= 1
terminate = True
v = 0
for i in range(N):
v += d[i]
A[i] = v
if terminate and A[i] < N:
terminate = False
if terminate:
break
print(*[a for a in A])
if __name__ == '__main__':
inputs = read()
outputs = solve(*inputs)
if outputs is not None:
print("%s" % str(outputs))
|
n, k = map(int, input().split())
alst = list(map(int, input().split()))
for _ in range(k):
tmp = [0 for _ in range(n + 1)]
for i, num in enumerate(alst):
min_ind = max(i - num, 0)
max_ind = min(i + num + 1, n)
tmp[min_ind] += 1
tmp[max_ind] -= 1
next_num = 0
for i, num in enumerate(tmp[:-1]):
next_num += num
alst[i] = next_num
if tmp[0] == n and tmp[-1] == -n:
break
print(*alst)
| 1 | 15,492,184,739,200 | null | 132 | 132 |
f=lambda:[*map(int,input().split())]
k,n=f()
l=f()
l+=[l[0]+k]
a=0
for i in range(n):
a=max(a,l[i+1]-l[i])
print(k-a)
|
K, N = map(int, input().split())
A = list(map(int, input().split()))
c = abs(K - A[N-1] + A[0])
for i in range(N-1):
d = abs(A[i] - A[i+1])
if c < d:
c = d
else:
c = c
print(K-c)
| 1 | 43,634,101,321,760 | null | 186 | 186 |
# Aizu Problem ALDS_1_3_A: Stack
#
import sys, math, os
# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
sys.stdin = open("sample-input.txt", "rt")
stack = []
for x in input().split():
if x == '+':
b = stack.pop()
a = stack.pop()
stack.append(a + b)
elif x == '-':
b = stack.pop()
a = stack.pop()
stack.append(a - b)
elif x == '*':
b = stack.pop()
a = stack.pop()
stack.append(a * b)
else:
stack.append(int(x))
print(stack[0])
|
s = input().split(" ")
st = []
for i in range(len(s)):
if s[i] == "+":
a = st.pop()
b = st.pop()
st.append(a+b)
elif s[i] == "-":
a = st.pop()
b = st.pop()
st.append(b-a)
elif s[i] == "*":
a = st.pop()
b = st.pop()
st.append(a*b)
else:
st.append(int(s[i]))
print(st[0])
| 1 | 37,205,173,212 | null | 18 | 18 |
from collections import deque
N, D, A = list(map(int, input().split()))
pair_xh = [[-1, -1] for _ in range(N)] # 1回ずつしか使わないけどソートするから必要
for i in range(N):
pair_xh[i][0], pair_xh[i][1] = list(map(int, input().split()))
pair_xh.sort(key = lambda x: x[0]) #座標でソート
q_lim_d = deque() # 累積和に含まれる攻撃範囲とダメージを保存する場所
total = 0 # 喰らいダメージの累積和
count = 0 # 攻撃回数
for i in range(N):
x = pair_xh[i][0]
h = pair_xh[i][1]
while len(q_lim_d) and q_lim_d[-1][0] < x: # 範囲外になったら喰らわないから累積和から外す
total -= q_lim_d[-1][1]
q_lim_d.pop()
h -= total
if h > 0: # 見てる敵の体力が0になる攻撃回数を求めその分を累積和に足す
times = (h + A - 1) // A # 0にするのに必要な回数(切り上げ)
count += times
damage = A * times
total += damage
q_lim_d.appendleft([x + 2 * D, damage])
print(count)
|
n = int(input())
left = []
midplus = []
midzero = []
midminus = []
right = []
L = []
R = []
for i in range(n):
s = input()
l = 0
r = 0
for x in s:
if x == '(':
l += 1
else:
if l > 0:
l -= 1
else:
r += 1
if l > 0 and r == 0:
left.append((l, r))
elif l > 0 and r > 0:
if l > r:
midplus.append((r, l)) # a,b-a
elif l == r:
midzero.append((r,l))
else:
midminus.append((r, l))
elif l == 0 and r > 0:
right.append((l, r))
L.append(l)
R.append(r)
if sum(L) != sum(R):
print('No')
exit()
A = 0
B = 0
for x in left:
A += x[0]
for x in right:
B += x[1]
midplus = sorted(midplus, key=lambda x: (x[0], -x[1]))
midminus = sorted(midminus, key=lambda x: -x[1])
mid = midplus + midzero + midminus
l = A
r = 0
for a, b in mid:
if l < a:
print('No')
exit()
l -= a
l += b
print('Yes')
| 0 | null | 52,883,099,142,760 | 230 | 152 |
n,x,m=map(int,input().split())
f=1
w=[]
d=[0]*(m+2)
t=[0]
chk=n
p=x
for i in range(m+1):
if i!=0: p=p*p%m
if d[p]: break
d[p]=1
w.append(p)
t.append(t[-1]+p)
#f*=2
chk-=1
if chk==0:
print(t[-1])
exit()
ans=0
a=w.index(p)
ans+=t[a]
n-=a
loo=len(w)-a
if n%loo==0:
ans+=(t[-1]-t[a])*(n//loo)
else:
ans+=(t[-1]-t[a])*(n//loo)
ans+=t[a+n%loo]-t[a]
print(ans)
|
N,X,M = map(int,input().split())
i = X%M
L = [i]
S = set([i])
i = i**2 % M
while not i in S:
L.append(i)
S.add(i)
i = i**2 % M
index = L.index(i)
k = (N-index)//len(L[index:])
l = (N-index)%len(L[index:])
# これでうまくいくのたまたまだよ。本当はN<indexの場合分けがひつよう。なんで場合分けするよ
if N<index:
ans = sum(L[:N])
else:
ans = sum(L[:index]) + (k*sum(L[index:])) + sum(L[index:index+l])
print(ans)
| 1 | 2,821,632,725,460 | null | 75 | 75 |
n, a, b = map(int, input().split())
s, e = divmod(n, (a + b))
if e >= a:
print(s * a + a)
else:
print(s * a + e)
|
n = int(input())
memo = {}
for i in range(n):
man_idx = i
count = int(input())
comment = [ list(map(int,input().split())) for _ in range(count) ]
memo[man_idx] = comment
ans = 0
for i in range(2**n):
sub_ans = 0
is_true = True
for j in range(n):
if i >> j & 1:
sub_ans += 1
for x, y in memo[j]:
if (i >> (x-1) & 1) != int(y):
is_true = False
break
if is_true:
ans = max(sub_ans, ans)
print(ans)
| 0 | null | 88,744,371,532,892 | 202 | 262 |
line = raw_input()
line2 = ''
for s in line:
if s == '\n':
break
elif s.islower():
line2 += s.upper()
elif s.isupper():
line2 += s.lower()
else:
line2 += s
print line2
|
inp = input()
print(inp.swapcase())
| 1 | 1,518,363,933,880 | null | 61 | 61 |
a,b,c=map(int,input().split());print('YNeos'[a/b>c::2])
|
import sys
from bisect import *
from collections import deque
pl=1
#from math import *
from copy import *
#sys.setrecursionlimit(10**6)
if pl:
input=sys.stdin.readline
else:
sys.stdin=open('input.txt', 'r')
sys.stdout=open('outpt.txt','w')
def li():
return [int(xxx) for xxx in input().split()]
def fi():
return int(input())
def si():
return list(input().rstrip())
def mi():
return map(int,input().split())
def find(i):
if i==a[i]:
return i
a[i]=find(a[i])
return a[i]
def union(x,y):
xs=find(x)
ys=find(y)
if xs!=ys:
if rank[xs]<rank[ys]:
xs,ys=ys,xs
rank[xs]+=1
a[ys]=xs
t=1
while t>0:
t-=1
n,m=mi()
a=[i for i in range(n+1)]
rank=[0 for i in range(n+1)]
for i in range(m):
x,y=mi()
union(x,y)
s=set()
for i in range(1,n+1):
a[i]=find(i)
s.add(a[i])
print(len(s)-1)
| 0 | null | 2,920,695,727,918 | 81 | 70 |
n = input()
l = input().split()
for a in l:
a = int(a)
x = list()
for i in (reversed(l)):
x.append(i)
x = ' '.join(x)
print(x)
|
#coding:utf-8
import math
r=float(input())
print("%.6f"%(math.pi*r**2)+" %.6f"%(2*math.pi*r))
| 0 | null | 828,643,371,840 | 53 | 46 |
#!/usr/bin/env python
label = list(map(int, input().split()))
command = list(input())
faces = [1, 2, 3, 4, 5, 6]
def dice(command):
if command == "N":
faces[0], faces[1], faces[4], faces[5] = faces[1], faces[5], faces[0], faces[4]
elif command == "S":
faces[0], faces[1], faces[4], faces[5] = faces[4], faces[0], faces[5], faces[1]
elif command == "E":
faces[0], faces[2], faces[3], faces[5] = faces[3], faces[0], faces[5], faces[2]
else: # command W
faces[0], faces[2], faces[3], faces[5] = faces[2], faces[5], faces[0], faces[3]
for c in command:
dice(c)
print(label[faces[0] - 1])
|
class Dice():
def __init__(self, *n):
self.rolls = n
def spin(self, order):
if order == "N":
self.rolls = [
self.rolls[1],
self.rolls[5],
self.rolls[2],
self.rolls[3],
self.rolls[0],
self.rolls[4]
]
if order == "E":
self.rolls = [
self.rolls[3],
self.rolls[1],
self.rolls[0],
self.rolls[5],
self.rolls[4],
self.rolls[2]
]
if order == "S":
self.rolls = [
self.rolls[4],
self.rolls[0],
self.rolls[2],
self.rolls[3],
self.rolls[5],
self.rolls[1]
]
if order == "W":
self.rolls = [
self.rolls[2],
self.rolls[1],
self.rolls[5],
self.rolls[0],
self.rolls[4],
self.rolls[3]
]
return self.rolls[0]
d = Dice(*[int(i) for i in input().split()])
for order in list(input()):
l = d.spin(order)
print(l)
| 1 | 225,645,625,088 | null | 33 | 33 |
S = input()
if S == ('ABC'):
print('ARC')
elif S == ('ARC'):
print('ABC')
else:
print('入力に誤りがあります。ABCまたはARCを入力してください。')
|
X, K, D = map(int,input().split())
X = abs(X)
if X > K*D:
print( X - K*D )
else:
a = X//D
K -= a
X -= D*a
if K%2 == 0:
print(X)
else:
print(abs(X-D))
| 0 | null | 14,763,661,620,232 | 153 | 92 |
from sys import stdin
readline = stdin.readline
def i_input(): return int(readline().rstrip())
def i_map(): return map(int, readline().rstrip().split())
def i_list(): return list(i_map())
class UnionFind:
def __init__(self, n):
self.parents = [-1] * (n + 1)
self.rank = [0] * (n + 1)
def root(self, x):
if(self.parents[x] < 0):
return x
else:
self.parents[x] = self.root(self.parents[x])
return self.parents[x]
def unite(self, x, y) -> bool:
x = self.root(x)
y = self.root(y)
if x == y:
return False
if self.rank[x] > self.rank[y]:
self.parents[x] += self.parents[y]
self.parents[y] = x
else:
self.parents[y] += self.parents[x]
self.parents[x] = y
if self.rank[x] == self.rank[y]:
self.rank[y] += 1
return True
def members(self, x):
root = self.root(x)
return [i for i in range(self.n) if self.root(i) == root]
def roots(self):
return [i for i, x in enumerate(self.parents[1:]) if x < 0]
def is_same(self, x, y) -> bool:
return self.root(x) == self.root(y)
def size(self, x):
return -self.parents[self.root(x)]
def all_group_members(self):
return {r: self.members(r) for r in self.roots()}
def main():
N, M = i_map()
u = UnionFind(N)
for _ in range(M):
A, B = i_map()
u.unite(A, B)
ans = len(u.roots()) - 1
print(ans)
if __name__ == "__main__":
main()
|
import sys
input = sys.stdin.readline
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
class UnionFind:
def __init__(self, N: int):
"""
N:要素数
root:各要素の親要素の番号を格納するリスト.
ただし, root[x] < 0 ならその頂点が根で-root[x]が木の要素数.
rank:ランク
"""
self.N = N
self.root = [-1] * N
self.rank = [0] * N
def __repr__(self):
return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
def find(self, x: int):
"""頂点xの根を見つける"""
if self.root[x] < 0:
return x
else:
while self.root[x] >= 0:
x = self.root[x]
return x
def union(self, x: int, y: int):
"""x,yが属する木をunion"""
# 根を比較する
# すでに同じ木に属していた場合は何もしない.
# 違う木に属していた場合はrankを見てくっつける方を決める.
# rankが同じ時はrankを1増やす
x = self.find(x)
y = self.find(y)
if x == y:
return
elif self.rank[x] > self.rank[y]:
self.root[x] += self.root[y]
self.root[y] = x
else:
self.root[y] += self.root[x]
self.root[x] = y
if self.rank[x] == self.rank[y]:
self.rank[y] += 1
def same(self, x: int, y: int):
"""xとyが同じグループに属するかどうか"""
return self.find(x) == self.find(y)
def count(self, x):
"""頂点xが属する木のサイズを返す"""
return - self.root[self.find(x)]
def members(self, x):
"""xが属する木の要素を列挙"""
_root = self.find(x)
return [i for i in range(self.N) if self.find == _root]
def roots(self):
"""森の根を列挙"""
return [i for i, x in enumerate(self.root) if x < 0]
def group_count(self):
"""連結成分の数"""
return len(self.roots())
def all_group_members(self):
"""{ルート要素: [そのグループに含まれる要素のリスト], ...}の辞書を返す"""
return {r: self.members(r) for r in self.roots()}
N,M=MI()
uf=UnionFind(N)
for _ in range(M):
a,b=MI()
a-=1
b-=1
uf.union(a,b)
cnt=uf.group_count()
print(cnt-1)
main()
| 1 | 2,272,279,535,690 | null | 70 | 70 |
import math
K = int(input())
ans = 0
for i in range(1, K+1):
for j in range(1, i+1):
for k in range(1, j+1):
gcd = math.gcd(i, math.gcd(j, k))
if i == j == k:
ans += gcd
elif i != j and j != k:
ans += 6 * gcd
else:
ans += 3 * gcd
print(ans)
|
print(['win','bust'][sum(list(map(int,input().split())))>=22])
| 0 | null | 77,592,191,019,240 | 174 | 260 |
def ALDS1_3A():
stk, RPexp = [], list(input().split())
for v in RPexp:
if v in '+-*':
n2, n1 = stk.pop(), stk.pop()
stk.append(str(eval(n1+v+n2)))
else:
stk.append(v)
print(stk.pop())
if __name__ == '__main__':
ALDS1_3A()
|
L = input().split()
n = len(L)
A = []
top = -1
for i in range(n):
if L[i] not in {"+","-","*"}:
A.append(int(L[i]))
top += 1
else:
if(L[i] == "+"):
A[top - 1] = A[top - 1] + A[top]
elif(L[i] == "-"):
A[top - 1] = A[top -1] - A[top]
elif(L[i] == "*"):
A[top - 1] = A[top - 1] * A[top]
del A[top]
top -= 1
print(A[top])
| 1 | 39,148,292,260 | null | 18 | 18 |
N,X,M=map(int, input().split())
p=[0]*(M+2)
sum=[0]*(M+2)
p[X]=1
sum[1]=X
f=0
for i in range(2,N+1):
X=(X**2)%M
if p[X]!=0:
j=p[X]
f=i
break
else:
sum[i]=X+sum[i-1]
p[X]+=i
if f==0:
print(sum[N])
else:
d,mod=divmod(N-j+1,i-j)
print(d*(sum[i-1]-sum[j-1])+sum[j+mod-1])
|
from math import log2
n,x,m = map(int, input().split())
logn = int(log2(n))+1
db = [[0]*m for _ in range(logn)]
dbs = [[0]*m for _ in range(logn)]
for i in range(m):
db[0][i] = (i**2)%m
dbs[0][i] = (i**2)%m
for i in range(logn-1):
for j in range(m):
db[i+1][j] = db[i][db[i][j]]
dbs[i+1][j] = dbs[i][db[i][j]] + dbs[i][j]
ans = x
now = x
for i in range(logn):
if (n-1)&(1<<i) > 0:
ans += dbs[i][now]
now = db[i][now]
print(ans)
| 1 | 2,814,510,434,944 | null | 75 | 75 |
import sys
import math
from collections import defaultdict
from collections import deque
sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()
def main():
N, X, M = NMI()
A = []
A_ID = {}
break_flag = False
start = 0
end = N
for i in range(M):
if i == 0:
A.append(X)
A_ID[X] = 0
else:
x = A[-1]**2 % M
if x not in A_ID:
A.append(x)
A_ID[x] = i
else:
A.append(x)
start = A_ID[x]
end = i
break_flag = True
if break_flag:
break
if N < start+1:
print(sum(A[:N]))
exit()
ans = sum(A[:start])
lp = sum(A[start:end])
gap = end - start
lp_n = (N - start) // gap
rem = (N - start) % gap
ans += lp * lp_n + sum(A[start: start + rem])
print(ans)
if __name__ == "__main__":
main()
|
from collections import defaultdict
from collections import deque
from collections import Counter
import math
import itertools
def readInt():
return int(input())
def readInts():
return list(map(int, input().split()))
def readChar():
return input()
def readChars():
return input().split()
n,x,m = readInts()
an = x
ans = an
moddict = {an:1}
for i in range(2,n+1):
an = pow(an,2,m)
if an in moddict:
break
else:
moddict[an]=i
ans += an
#print(ans)
if an in moddict:
rep = (n-len(moddict))//(len(moddict)-moddict[an]+1)
rem = (n-len(moddict))%(len(moddict)-moddict[an]+1)
#print(rep,rem)
rep_sum = 0
boo = 0
for key in moddict:
if boo or key==an:
boo = 1
rep_sum+=key
ans += rep_sum*rep
for i in range(rem):
ans+=an
an=pow(an,2,m)
print(ans)
| 1 | 2,786,237,413,540 | null | 75 | 75 |
D=int(input())
c=list(map(int, input().split()))
s = [list(map(int, input().split())) for i in range(D)]
t= [int(input()) for i in range(D)]
v=0
LD=[0]*26
for i in range(D):
v+=s[i][t[i]-1]
LD[t[i]-1]=i+1
for j in range(26):
v-=c[j]*(i+1-LD[j])
print(v)
|
from itertools import product
n,m,x = map(int, input().split())
al = []
cl = []
for _ in range(n):
row = list(map(int, input().split()))
cl.append(row[0])
al.append(row[1:])
ans = 10**9
bit = 2
ite = list(product(range(bit),repeat=n))
for pattern in ite:
skills = [0]*m
cost = 0
for i, v in enumerate(pattern):
if v == 1:
curr_al = al[i]
cost += cl[i]
for j, a in enumerate(curr_al):
skills[j] += a
if min(skills) >= x:
ans = min(ans,cost)
if ans == 10**9: ans = -1
print(ans)
| 0 | null | 16,018,234,136,436 | 114 | 149 |
X = int(input())
num = X//100
amari = X%100
if num*5 >= amari:
print(1)
else:
print(0)
|
h,n = map(int,input().split())
A = list(map(int,input().split()))
if h-sum(A)>0 :
print("No")
else:
print("Yes")
| 0 | null | 101,998,194,799,292 | 266 | 226 |
input=raw_input().split()
a1=int(input[0])
a2=int(input[1])
print "%d %d"%(a1*a2,2*(a1+a2))
|
params = input().rstrip().split(' ')
square = int(params[0]) * int(params[1])
length = (int(params[0]) + int(params[1])) * 2
print(square, length)
| 1 | 300,979,285,110 | null | 36 | 36 |
n = int(input())
L = []
ans = 0
for i in range(n):
a = int(input())
for j in range(a):
x, y = map(int, input().split())
L.append((i, x-1, y))
for i in range(1<<n):
flg = True
for j, x, y in L:
if i>>j & 1 and i>>x & 1 != y:
flg = False
break
if flg:
ans = max(ans, sum(i>>j & 1 for j in range(n)))
print(ans)
|
# -*- coding: utf-8 -*-
N = int(input())
A = []
data = []
for i in range(N):
a = int(input())
A.append(a)
x = [list(map(int, input().split())) for _ in range(a)]
data.append(x)
ans = 0
for i in range(1<<N):
bit = [0] * N
cnt = 0
for j in range(N):
div = 1 << j
bit[j] = (i // div) % 2
if bit[j] == 1:
cnt += 1
flag = True
for j in range(N):
if bit[j] == 0:
continue
for a in range(A[j]):
x = data[j][a]
if bit[x[0]-1] != x[1]:
flag = False
break
if not flag:
break
if flag:
ans = max(ans, cnt)
print(ans)
| 1 | 121,545,330,650,080 | null | 262 | 262 |
import sys
import math
import time
sys.setrecursionlimit(int(1e6))
if False:
dprint = print
else:
def dprint(*args):
pass
n, k = list(map(int, input().split()))
A = list(map(int, input().split()))
dprint("n, k = ", n, k)
lx = int(0) # NG
rx = int(1e9) # OK
#ans = 0
while ((rx-lx)>1):
x = (rx + lx) // 2
dprint("lx, x, rx = ", lx, x, rx)
n = 0
for i in range(len(A)):
#n += math.ceil(A[i] / x) - 1
n += (A[i]-1) // x
if n > k: break
dprint("n = ", n)
if n <= k:
dprint(" smaller x")
rx = x
else:
dprint(" cut less, bigger x")
lx = x
#ans = math.ceil(rx)
print(rx)
|
from math import gcd
def readinput():
n,m=map(int,input().split())
a=list(map(int,input().split()))
return n,m,a
def lcm(a,b):
return a*b//gcd(a,b)
def main(n,m,a):
#s=set(a)
#print(s)
x=a[0]//2
for i in range(1,n):
x=lcm(x,a[i]//2)
#print(x)
gusubai=False
kisubai=False
for i in range(n):
if x%a[i]!=0:
gusubai=True
else:
kisubai=True
y=m//x
#print(y,x,m)
if gusubai and not kisubai:
ans=y//2+y%2
elif gusubai and kisubai:
ans=0
else:
ans=y
return ans
if __name__=='__main__':
n,m,a=readinput()
ans=main(n,m,a)
print(ans)
| 0 | null | 53,838,344,330,988 | 99 | 247 |
# 配るDP、もらうDP
n, k = map(int, input().split())
mod = 998244353
kukan = []
for _ in range(k):
# 区間の問題は扱いやすいように[ ) の形に直せるなら直す
l, r = map(int, input().split())
l -= 1
kukan.append([l, r])
dp = [0 for i in range(n)]
dp[0] = 1
# 区間のL, Rは数字が大きいため、その差一つ一つを考えると時間がない!
# それゆえにL, Rの端を考えればいいようにするためにそこまでの累和を考える
ruiseki = [0 for i in range(n + 1)]
ruiseki[1] = 1
for i in range(1, n):
for l, r in kukan:
l = i - l
r = i - r
l, r = r, l
# print(l, r)
if r < 0:
continue
elif l >= 0:
dp[i] += (ruiseki[r] - ruiseki[l]) % mod
else:
dp[i] += (ruiseki[r]) % mod
ruiseki[i + 1] = (ruiseki[i] + dp[i])
# print(ruiseki, dp)
print(dp[-1] % mod)
|
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
MOD = 998244353
n, k = map(int, input().split())
s = list()
for i in range(k):
s.append(tuple(map(int, input().split())))
dp = [0] + [0] * n
diff = [0] + [0] * n
dp[1] = 1
diff[2] = -1
for i in range(1, n):
for j, k in s:
if i + j > n:
continue
diff[i + j] += dp[i]
if i + k + 1 > n:
continue
diff[i + k + 1] -= dp[i]
#dp[i] = (dp[i - 1] + diff[i]) % MOD
dp[i + 1] = (dp[i] + diff[i + 1]) % MOD
print(dp[n])
| 1 | 2,701,556,036,960 | null | 74 | 74 |
print(input().translate(str.maketrans('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')))
|
inp = input()
print(inp.swapcase())
| 1 | 1,504,027,689,398 | null | 61 | 61 |
def main():
h,w,k = map(int,input().split())
maze = [['.' for j in range(6)] for i in range(6)]
for i in range(h):
line = list(input())
for j in range(w):
maze[i][j] = line[j]
# print(maze)
ver = [0 for i in range(6)]
hor = [0 for i in range(6)]
horver = [[0 for i in range(6)] for j in range(6)]
total = 0
for i in range(6):
for j in range(6):
if maze[i][j] == '#':
ver[j] += 1
hor[i] += 1
horver[i][j] = 1
total += 1
# print(ver,hor,horver)
ans = 0
for bit in range(2**(h+w)):
tmp = total
valid_hor = []
for i in range(h):
if (bit >> i & 1) == 1:
valid_hor.append(i)
tmp -= hor[i]
for j in range(h,h+w):
if (bit >> j & 1) == 1:
tmp -= ver[j-h]
for i in valid_hor:
tmp += horver[i][j-h]
if tmp == k:
ans += 1
print(ans)
return
if __name__ == '__main__':
main()
|
N = int(input())
A = list(map(int,input().split()))
set_A = list(set(A))
if len(set_A) == N:
print("YES")
else:
print("NO")
| 0 | null | 41,544,600,188,574 | 110 | 222 |
n = int(input())
a = [int(s) for s in input().split()]
t=a[0]
total=0
for i in range(n):
if t>a[i]:
total=total+t-a[i]
else:
t=a[i]
print(total)
|
n = int(input())
a = list(map(int, input().split()))
for v in a:
if v % 2 == 0:
if not (v % 3 == 0 or v % 5 == 0):
print("DENIED")
exit()
print("APPROVED")
| 0 | null | 36,787,277,334,720 | 88 | 217 |
def main():
N = int(input())
zoro = []
for i in range(N):
a, b = map(int, input().split())
if a == b:
zoro.append(1)
if i >= 2:
if zoro[i-2] == zoro[i-1] and zoro[i-1] == zoro[i]:
print('Yes')
return
else:
zoro.append(0)
print('No')
main()
|
h, w, k, = map(int, input().split())
c = [0] * h
for i in range(h):
c[i] = input()
y = 0
for i in range(2 ** (h + w)):
x = 0
a = [0] * (h + w)
for j in range(h + w):
if (i // (2 ** j)) % 2 == 1:
a[j] = 1
for j in range(h):
if a[j] == 0:
for l in range(w):
if a[h+l] == 0:
if c[j][l] == '#':
x += 1
if x == k:
y += 1
print(y)
| 0 | null | 5,707,609,984,488 | 72 | 110 |
MAX_ARRAY_SIZE = 100000
class Queue:
array = [None] * MAX_ARRAY_SIZE
start = end = 0
def enqueue(self, a):
assert not self.isFull(), "オーバーフローが発生しました。"
self.array[self.end] = a
if self.end + 1 == MAX_ARRAY_SIZE:
self.end = 0
else:
self.end += 1
def dequeue(self):
assert not self.isEmpty(), "アンダーフローが発生しました。"
if (self.start + 1) == MAX_ARRAY_SIZE:
self.start = 0
return self.array[MAX_ARRAY_SIZE - 1]
else:
self.start += 1
return self.array[self.start - 1]
def isEmpty(self):
return self.start == self.end
def isFull(self):
return self.start == (self.end + 1) % MAX_ARRAY_SIZE
n, q = list(map(lambda x: int(x), input().strip().split()))
queue = Queue()
for i in range(n):
queue.enqueue(input().strip().split())
sum_time = 0
while not queue.isEmpty():
name, time = queue.dequeue()
if int(time) <= q:
sum_time += int(time)
print(name + ' ' + str(sum_time))
else:
queue.enqueue([name, str(int(time) - q)])
sum_time += q
|
a = input()
if(a[-1] == 's'):
print(a+'es')
else:
print(a+'s')
| 0 | null | 1,237,684,239,150 | 19 | 71 |
data =[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]
line =int(input())
print(data[line -1])
|
data = []
while True:
x = input().strip()
if x == '0':
break
data.append(list(map(int, list(x))))
for i in range(len(data)):
print(sum(data[i]))
| 0 | null | 25,702,525,257,256 | 195 | 62 |
while True:
h, w = map(int, input().split())
if h == 0 and w == 0:
break
for y in range(h):
isOddLine = True
if y % 2 == 0:
isOddLine = False
else:
isOddLine = True
for x in range(w):
if isOddLine:
if x % 2 == 0:
print('.', end='')
else:
print('#', end='')
else:
if x % 2 == 0:
print('#', end='')
else:
print('.', end='')
print('')
print('')
|
while (True):
s = list(map(int, input().strip().split(' ')))
if s[0] == 0 and s[1] == 0:
break
else:
for i in range(s[0]):
for j in range(s[1]):
if j % 2 == 0 and i % 2 == 0:
print("#", end="")
elif j % 2 == 1 and i % 2 == 0:
print(".", end="")
elif j % 2 == 0 and i % 2 == 1:
print(".",end="")
else:
print("#", end="")
print()
print()
| 1 | 873,466,090,812 | null | 51 | 51 |
def main():
n, k = map(int, input().split())
H = list(map(int, input().split()))
cnt = 0
for i in range(n):
if H[i] >= k:
cnt += 1
print(cnt)
if __name__ == "__main__":
main()
|
n, h = map(int, input().split())
hn = map(int, input().split())
cnt = 0
for i in hn:
if i >= h:
cnt += 1
print(cnt)
| 1 | 179,013,673,678,712 | null | 298 | 298 |
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)))
|
K=int(input())
i=0
if K%2==0 or K%5==0:
print('-1')
else:
for d in range(K):
i=(10*i+7)%K
if i==0:
print(d+1)
break
| 0 | null | 25,217,083,425,180 | 187 | 97 |
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
import numpy as np
def main():
n, *a = map(int, read().split())
p = np.array(a)
minp = np.minimum.accumulate(p)
r = np.count_nonzero(minp >= p)
print(r)
if __name__ == '__main__':
main()
|
while 1:
x, y = map(int, input().split())
if x == 0 and y == 0:
break
elif x < y:
print("%d %d" % (x, y))
else:
print("%d %d" % (y, x))
| 0 | null | 43,171,910,783,330 | 233 | 43 |
D = int(input())
C = list(map(int, input().split()))
s = list()
for i in range(D):
s1 = list(map(int, input().split()))
s.append(s1)
t = list()
for i in range(D):
N = int(input())
t.append(N-1)
done = [0] * 26
ans = 0
for d in range(1, D+1):
td = t[d-1]
done[td] = d
ans += s[d-1][td]
for i in range(26):
ans -= C[i]*(d-done[i])
print(ans)
|
# -*- coding: utf-8 -*-
# 入力
D = int(input())
c_i = list(map(int, input().split()))
s_d_i = []
for d in range(D):
s_d_i.append(list(map(int, input().split())))
last_i = []
for i in range(26):
last_i.append(-1)
# tが入力の場合(Mainでは不要)
t_d = []
for d in range(D):
t_d.append(int(input()))
#scoring
r_sum = 0
for d in range(D):
t = t_d[d] - 1
#last更新
last_i[t] = d
#c
c_sum = 0
for i in range(26):
c_sum += c_i[i] * (d - last_i[i])
#s - c
r_d = s_d_i[d][t] - c_sum
r_sum += r_d
print(r_sum)
| 1 | 10,072,527,605,570 | null | 114 | 114 |
import sys
import math
import bisect
def main():
n, m = map(int, input().split())
if n < 10:
m += 100 * (10 - n)
print(m)
if __name__ == "__main__":
main()
|
n = int(input())
c = [[0] * 10 for _ in range(10)]
for i in range(1, n + 1):
a = int(str(i)[0])
b = int(str(i)[-1])
c[a][b] += 1
res = 0
for i in range(10):
for j in range(10):
res += c[i][j]*c[j][i]
print(res)
| 0 | null | 74,860,058,008,860 | 211 | 234 |
count = int(raw_input())
arr = map(int, raw_input().split())
arr.reverse()
print(" ".join(map(str, arr)))
|
n = int(raw_input())
a = map(int,raw_input().split())
a.reverse()
for x in a:
print x,
| 1 | 972,716,593,216 | null | 53 | 53 |
nxt = input().split()
#print(nxt)
N = int(nxt[0])
X = int(nxt[1])
T = int(nxt[2])
#print(N,X,T)
tmp = N/X - int(N/X)
if tmp == 0:
counter = int(N/X)
else:
counter = int(N/X) + 1
#print(counter)
print(counter * T)
|
def aizu007():
xs = map(int,raw_input().split())
a,b,c = xs[0],xs[1],xs[2]
cnt = 0
for i in range(a,b+1):
if c%i == 0:
cnt = cnt + 1
print cnt
aizu007()
| 0 | null | 2,396,588,587,568 | 86 | 44 |
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
for i in range(n):
temp = 0 if a[i][0] - a[i][1] < 0 else a[i][0] - a[i][1]
a[i][1] = a[i][0]+a[i][1]
a[i][0] = temp
a.sort(key = lambda x:x[1])
ans = n
right = 0
for j in a:
if right > j[0]:
ans -= 1
else:
right = j[1]
print(ans)
|
import math
a,b,x=map(float, input().split())
h=b*math.sin(math.pi*x/180)
c=(a**2+b**2-2*a*b*math.cos(math.pi*x/180))**0.5
print(a*h*0.5)
print(a+b+c)
print(h)
| 0 | null | 45,131,990,247,652 | 237 | 30 |
def solve(n, A, q, m):
memo = dict()
for i in range(2**n):
tmp = 0
for j in range(n):
if i >> j & 1:
tmp += A[j]
memo[tmp] = 1
for val in m:
print("yes" if val in memo else "no")
if __name__ == "__main__":
n = int(input())
A = list(map(int, input().split()))
q = int(input())
m = list(map(int, input().split()))
solve(n, A, q, m)
|
a_len = int(input())
a_ar = sorted([int(n) for n in input().split(" ")])
b_len = int(input())
b_ar = [int(n) for n in input().split(" ")]
dp = [0 for n in range(2001)]
for a in a_ar:
new_dp = dp[:]
new_dp[a] = 1
for i,d in enumerate(dp):
if d and i + a <= 2000:
new_dp[i + a] = 1
dp = new_dp
for b in b_ar:
if dp[b]:
print("yes")
else:
print("no")
| 1 | 102,126,674,802 | null | 25 | 25 |
while 1:
m,f,r=map(int, raw_input().split())
if m==f==r==-1: break
s=m+f
if m==-1 or f==-1 or s<30: R="F"
elif s>=80: R="A"
elif s>=65: R="B"
elif s>=50: R="C"
elif r>=50: R="C"
else: R="D"
print R
|
N,M,X = map(int,input().split())
C = []
A = []
for i in range(N):
t = list(map(int,input().split()))
C.append(t[0])
A.append(t[1:])
result = -1
# 1 << N 2^N
#There are 2^N possible ways of choosing books
for i in range(1 << N):
#keep all the understanding level in u
u = [0]*M
c = 0
for j in range(N):
#move j bit from i
if (i>>j)&1 == 0:
continue
c+= C[j]
#listwise sum
for k in range(M):
u[k] += A[j][k]
#X is the desired understanding level
if all(x >= X for x in u):
if result == -1:
result = c
else:
result = min(result,c)
print(result)
| 0 | null | 11,807,383,047,492 | 57 | 149 |
while True:
n = input()
if n == 0: break
print sum(map(int, list(str(n))))
|
x = 1
while x != '0':
x = input()
if x != '0':
sum = 0
for s in x:
sum += int(s)
print(sum)
| 1 | 1,610,275,862,118 | null | 62 | 62 |
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
# INF = float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353
H, W = list(map(int, input().split()))
S = []
for i in range(H):
S.append(input())
def bfs(u):
stack = deque([u])
visited = set()
seen = set()
p = [[INF for j in range(W)] for i in range(H)]
p[u[0]][u[1]] = 0
for i in range(H):
for j in range(W):
if S[i][j] == "#":
p[i][j] = -1
while len(stack) > 0:
v = stack.popleft()
###
visited.add(v)
###
a = (v[0] + 1, v[1])
b = (v[0] , v[1] + 1)
c = (v[0] - 1, v[1])
d = (v[0] , v[1] - 1)
e = [a, b, c, d]
for ee in e:
if ee[0] >= 0 and ee[0] <= H-1 and ee[1] >= 0 and ee[1] <= W-1:
if S[ee[0]][ee[1]] == ".":
if ee not in visited:
p[ee[0]][ee[1]] = min(p[ee[0]][ee[1]], p[v[0]][v[1]] + 1)
if ee not in seen:
stack.append(ee)
seen.add(ee)
ans = 0
for i in range(H):
ans = max(ans, max(p[i]))
return ans
sol = 0
for i in range(H):
for j in range(W):
if S[i][j] == ".":
sol = max(sol, bfs((i,j)))
print(sol)
|
h, w = map(int, input().split())
s = [input() for _ in range(h)]
def bfs(x, y):
q = []
dp = {}
def qpush(x, y, t):
if 0 <= x < w and 0 <= y < h and s[y][x] != '#' and (x, y) not in dp:
q.append((x, y))
dp[(x, y)] = t
qpush(x, y, 0)
while len(q) > 0:
(x, y) = q.pop(0)
qpush(x + 1, y, dp[(x, y)] + 1)
qpush(x, y - 1, dp[(x, y)] + 1)
qpush(x - 1, y, dp[(x, y)] + 1)
qpush(x, y + 1, dp[(x, y)] + 1)
return dp.get((x, y), 0)
t = 0
for y in range(h):
for x in range(w):
t = max(t, bfs(x, y))
print(t)
| 1 | 94,891,790,961,952 | null | 241 | 241 |
N = int(input())
S = input()
revALPH = "".join(list(reversed("ABCDEFGHIJKLMNOPQRSTUVWXYZ")))
A = ""
for s in S:
pos = revALPH.find(s)
A += revALPH[pos - N]
print(A)
|
def solve_loop(loop, K):
S = sum(loop)
L = len(loop)
if S < 0:
acc = 0
K = min(K, L)
minL = 1
elif K//L >= 2:
acc = S * (K//L - 1)
K = K - L * (K//L - 1)
minL = 0
else:
acc = 0
minL = 1
ans = -10 ** 9 - 1
for i in range(L):
#print(loop[i:] + loop[:i], "acc=", acc, "min=", minL, "max=", K)
tmp = 0
if minL:
tmp = loop[i]
for l in range(minL, K):
ans = max(tmp, ans)
tmp += loop[(i+l) % L]
#print(tmp)
#print(i, l, tmp)
ans = max(tmp, ans)
#print(loop, ans, acc)
return acc + ans
if K >= L:
return max(acc, acc + ans)
else:
return ans
def solve():
N, K = map(int, input().split())
P = list(map(lambda n: int(n)-1, input().split())) # 0-based index
C = list(map(int, input().split()))
flag = [False] * N
ans = -10**9-1 # worst case is -10**9
for i in range(N):
if flag[i]:
continue
loop = []
j = P[i]
for _ in range(N):
if flag[j]: # loop end
break
flag[j] = True
loop += [C[j]]
j = P[j]
ans = max(ans, solve_loop(loop, K))
print(ans)
solve()
| 0 | null | 69,872,125,332,822 | 271 | 93 |
N = int(input())
A = [int(x) for x in input().split()]
M = max(A)
count = [0 for _ in range(M + 1)]
for i in range(N):
count[A[i]] += 1
S = 0
for i in range(M + 1):
m = count[i]
S += (m * (m - 1)) // 2
ans = [S for _ in range(N)]
for i in range(N):
m = count[A[i]]
ans[i] -= m - 1
for i in range(N):
print(ans[i])
|
def main():
from sys import stdin
n,a,b = map(int,stdin.readline().rstrip().split())
k = b-a-1
if k%2==1:
print(k//2+1)
else:
print(k//2+1+min(n-b,a-1))
main()
| 0 | null | 78,190,033,906,430 | 192 | 253 |
K=int(input())
i=0
if K%2==0 or K%5==0:
print('-1')
else:
for d in range(K):
i=(10*i+7)%K
if i==0:
print(d+1)
break
|
K = int(input())
num = 0
for i in range(0,K):
num = num*10 + 7
if num % K == 0:
print(i + 1)
exit()
num %= K
print(-1)
| 1 | 6,094,293,082,620 | null | 97 | 97 |
x,y=map(int, raw_input().split())
print x*y,(x+y)*2
|
def main():
k, n = map(int, input().split())
a_lst = list(map(int, input().split()))
d_lst = [0 for _ in range(n)]
for i in range(n - 1):
d_lst[i] = a_lst[i + 1] - a_lst[i]
d_lst.append(a_lst[0] + k - a_lst[n - 1])
ans = k - max(d_lst)
print(ans)
if __name__ == "__main__":
main()
| 0 | null | 21,843,537,638,832 | 36 | 186 |
# -*- coding: utf-8 -*-
def main():
import sys
input = sys.stdin.readline
x = int(input())
if x == 0:
print(1)
else:
print(0)
if __name__ == '__main__':
main()
|
if int(input())==1:print(0)
else:print(1)
| 1 | 2,916,561,566,520 | null | 76 | 76 |
x = int(input())
sum = 100
year = 0
while x > sum:
sum += sum//100
year += 1
print(year)
|
a=int(input())
cnt=0
t=100
while t<a:
t=(t*101)//100
cnt+=1
print(cnt)
| 1 | 27,177,363,235,078 | null | 159 | 159 |
n = int(input())
d = dict()
for i in range(n):
a = int(input())
for j in range(a):
l = d.get(i, [])
p, t = list(map(int, input().split(' ')))
l.append((p-1, t))
d[i] = l
# d[i] に i番の人の証言のlistがあるので 全パターン(=2^15)で矛盾しないか試す?
def get_bit_array(num):
res = []
while num > 0:
res.append(num%2)
num //= 2
while len(res) < n:
res = res + [0]
return res[::-1]
def valid(tfs):
for i in range(len(tfs)):
honest = tfs[i]
if not honest: # 嘘つきは無視する
continue
for (person, tf) in d.get(i, []): # 合ってる?
if tfs[person] != tf:
return False
return True
ans = 0
def f(num):
global ans
if num >= 2**n:
return
tfs = get_bit_array(num) # tfs = [1,0,1,0,...0] true or flase
total = sum(tfs)
if valid(tfs):
ans = max(ans, total)
for i in range(int(2**n)):
f(i)
print(ans)
|
import numpy as np
def solve():
ret = np.array([[1 for _ in range(W)] for _ in range(H)])
row_groups = np.array([0 for _ in range(H)])
row_group_set = set()
prev_row = 0
for row in range(H):
if '#' in s[row]:
row_groups[prev_row: row + 1] = row
row_group_set.add(row)
prev_row = row + 1
row_groups[prev_row: ] = prev_row - 1
cur_group_num = 1
for row in row_group_set:
prev_col = 0
for col in range(W):
if s[row][col] == '#':
ret[row][prev_col: col + 1] = cur_group_num
cur_group_num += 1
prev_col = col + 1
ret[row][prev_col: ] = cur_group_num - 1
for row, row_group_num in enumerate(row_groups):
if row in row_group_set:
continue
for col in range(W):
ret[row][col] = ret[row_group_num][col]
for row in ret:
print(' '.join(map(str, row.tolist())))
if __name__ == "__main__":
H, W, K = map(int, input().split())
s = []
for row in range(H):
s.append(list(input()))
solve()
| 0 | null | 132,719,601,323,228 | 262 | 277 |
n=int(input())
a=list(map(int,input().split()))
mod=10**9+7
ans=0
for k in range(60):
t=1<<k
z=o=0
for i in a:
if i&t:o+=1
else:z+=1
ans=(ans+(z*o*t))%mod
print(ans)
|
n = input()
ans = 0
temp = 0
for i in range(len(n)):
if n[i] == "R":
temp += 1
ans = max(temp,ans)
else:
temp = 0
print(ans)
| 0 | null | 63,922,395,494,370 | 263 | 90 |
'''
参考
https://drken1215.hatenablog.com/entry/2020/06/21/224900
'''
N = int(input())
A = list(map(int, input().split()))
INF = 10 ** 5
cnt = [0] * (INF+1)
Q = int(input())
BC = []
for _ in range(Q):
BC.append(list(map(int, input().split())))
for a in A:
cnt[a] += 1
total = sum(A)
for b, c in BC:
total += (c-b) * cnt[b]
cnt[c] += cnt[b]
cnt[b] = 0
print(total)
|
#!/usr/bin/env python3
n = int(input())
s = input()
print(*[chr((ord(i) + n - 65) % 26 + 65)for i in s], sep="")
| 0 | null | 73,691,528,787,408 | 122 | 271 |
import sys
N = input()
num = [int(x) for x in input().split()]
multiply = 1
if (0 in num):
print(0)
sys.exit()
for i in num:
multiply *= i
if (multiply > 10**18):
print(-1)
sys.exit()
if (0 < multiply and multiply <= 10**18):
print(multiply)
|
N = int(input())
A = list(map(int, input().split()))
X = 10**18
ans = 1
if 0 in A:
ans = 0
else:
for i in range(N):
ans = ans * A[i]
if ans > X:
ans = -1
break
print(ans)
| 1 | 16,200,250,664,542 | null | 134 | 134 |
input()
l = list(map(int,input().split()))
l.sort()
ans = 1000000
n_ans = 0
for i in range(l[0],l[len(l)-1]+1):
for j in range(len(l)):
n_ans += (l[j]-i) ** 2
if n_ans <= ans:
ans = n_ans
n_ans = 0
else:
n_ans = 0
print(ans)
|
N = int(input())
cc = list(map(int,input().split()))
min_sum = float('inf')
for i in range(1,101):
count = 0
for j in cc:
count += (i-j)**2
min_sum = min(min_sum,count)
print(min_sum)
| 1 | 65,097,684,932,698 | null | 213 | 213 |
n=int(input())
cnt=0
ans=0
while(n>=1):
ans+=2**cnt
n//=2
cnt+=1
print(ans)
|
N, K = map(int, input().split())
A = list(map(int, input().split()))
for _ in range(K):
B = [0] * N
for i, d in enumerate(A):
l = max(0, i-d)
r = min(N-1, i+d)
B[l] += 1
if r+1 < N:
B[r+1] -= 1
for i in range(1, N):
B[i] += B[i-1]
A = B.copy()
if min(A) == N:
break
print(" ".join(map(str, A)))
| 0 | null | 47,980,979,408,208 | 228 | 132 |
N = int(input())
S = str(input())
cnt = 1
for i in range(1,N):
if S[i] == S[i-1]:
pass
else:
cnt += 1
print(cnt)
|
H1, M1, H2, M2, K = map(int, input().split())
T1, T2 = (H1 * 60 + M1), (H2 * 60 + M2)
print("{}".format((T2 - K) - T1))
| 0 | null | 93,879,564,306,680 | 293 | 139 |
import sys
def input(): return sys.stdin.readline().strip()
def resolve():
x,y=map(int, input().split())
ans=0
if x<=3:
ans+=100000
if x<=2:
ans+=100000
if x==1:
ans+=100000
if y<=3:
ans+=100000
if y<=2:
ans+=100000
if y==1:
ans+=100000
if x==1 and y==1:
ans+=400000
print(ans)
resolve()
|
D = int(input())
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for i in range(D)]
t = [0]*D
for i in range(D):
t[i] =int(input())
m=[0]*D
last_day=[0]*26
a=[[0 for i in range(26)] for j in range(D)]
for u in range(D):
i=t[u]
for y in range(u,D):
a[y][i-1]=u+1
if u==0:
m[u]=s[u][i-1]
else:
m[u]=m[u-1]+s[u][i-1]
mm=[0]*D
for d in range(D):
for i in range(26):
mm[d]+=c[i]*(d+1-a[d][i])
if not d==0:
mm[d]+=mm[d-1]
for d in range(D):
m[d]-=mm[d]
print(m[d])
| 0 | null | 75,091,810,315,498 | 275 | 114 |
import sys
prm = sys.stdin.readline()
prm = prm.split()
a = int(prm[0])
b = int(prm[1])
c = int(prm[2])
if a < b and b < c :
print 'Yes'
else :
print 'No'
|
import sys
input = sys.stdin.readline
n, res = int(input()), 0
for i in range(1, n + 1): res += 0 if i % 3 == 0 or i % 5 == 0 else i
print(res)
| 0 | null | 17,716,532,643,744 | 39 | 173 |
from queue import deque
h,w = map(int,input().split())
li = [input() for _ in range(h)]
inf = 10**10
dx = [1,0]
dy = [0,1]
que = deque()
#そのマスにたどり着くまでに色が変わった最小の回数を保存するリスト
flip_count = [[inf for i in range(w)]for j in range(h)]
if li[0][0] == "#":
flip_count[0][0] = 1
else:
flip_count[0][0] = 0
que.append([0,0])
while que:
k = que.popleft()
x = k[0]
y = k[1]
for i,j in zip(dx,dy):
nx = x + i
ny = y + j
if nx >= w or ny >= h:
continue
if flip_count[ny][nx] == inf:
que.append([nx,ny])
if li[ny][nx] != li[y][x]:
flip_count[ny][nx] = min(flip_count[ny][nx],flip_count[y][x]+1)
else:
flip_count[ny][nx] = min(flip_count[ny][nx],flip_count[y][x])
if li[-1][-1] == "#":
flip_count[-1][-1] += 1
print(flip_count[-1][-1]//2)
|
# -*- coding:utf-8 -*-
import sys
def gcd(a, b):
if b == 0: return a
return gcd(b, a % b)
def lcm(a, b):
return a * b / gcd(a, b)
array = []
for i in sys.stdin:
array.append(i)
for i in range(len(array)):
n, m = array[i].split()
n, m = int(n), int(m)
print(int(gcd(n, m)),int(lcm(n, m)))
| 0 | null | 24,680,715,634,730 | 194 | 5 |
N ,*S = open(0).read().split()
s = list(set(S))
print(len(s))
|
#! /usr/bin/env python3
import sys
import numpy as np
from itertools import groupby
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)
S = readline().decode().rstrip()
cnt = [0] * (len(S) + 1)
for i in range(len(S)):
if S[i] == '<':
cnt[i + 1] = max(cnt[i + 1], cnt[i] + 1)
for i in range(len(S) - 1, -1, -1):
if S[i] == '>':
cnt[i] = max(cnt[i], cnt[i + 1] + 1)
print(sum(cnt))
| 0 | null | 93,587,653,610,790 | 165 | 285 |
h,a=map(int,input().split());print(-(-h//a))
|
H, A = list(map(int, input().split()))
print(H // A + (H % A != 0))
| 1 | 76,757,065,272,494 | null | 225 | 225 |
from bisect import bisect_left
n = int(input())
L = sorted(list(map(int, input().split())))
# 二分探索
cnt = 0
for i in range(n):
for j in range(i+1, n):
a, b = L[i], L[j]
# c < a + b
r = bisect_left(L, a+b)
l = j+1
cnt += max(0, r-l)
print(cnt)
|
s=raw_input()
ring=s*2
target=raw_input()
f=0
for i in range(len(s)):
if ring[i:i+len(target)]==target:
f=1
break
if f==1:
print "Yes"
else:
print "No"
| 0 | null | 86,894,538,261,440 | 294 | 64 |
X,Y,Z=input().split()
# X,Y,Z=41,59,31
A,B,C=X,Y,Z
A,B,C=Z,X,Y
print(A,B,C)
|
argList = list(map(int, input().split()))
print(str(argList[2])+" "+str(argList[0])+" "+str(argList[1]))
| 1 | 38,072,872,859,890 | null | 178 | 178 |
def main():
while True:
m, f, r = tuple(map(int, input().split()))
if m == f == r == -1:
break
elif m == -1 or f == -1:
print('F')
elif m + f >= 80:
print('A')
elif m + f >= 65:
print('B')
elif m + f >= 50:
print('C')
elif m + f >= 30:
if r >= 50:
print('C')
else:
print('D')
else:
print('F')
if __name__ == '__main__':
main()
|
MOD = 1000000007
def fast_power(base, power):
"""
Returns the result of a^b i.e. a**b
We assume that a >= 1 and b >= 0
Remember two things!
- Divide power by 2 and multiply base to itself (if the power is even)
- Decrement power by 1 to make it even and then follow the first step
"""
result = 1
while power > 0:
# If power is odd
if power % 2 == 1:
result = (result * base) % MOD
# Divide the power by 2
power = power // 2
# Multiply base to itself
base = (base * base) % MOD
return result
n,k = [int(j) for j in input().split()]
d = dict()
ans = k
d[k] = 1
sum_so_far = 1
for i in range(k-1, 0, -1):
d[i] = fast_power(k//(i),n)
for mul in range(i*2, k+1, i):
d[i]-=d[mul]
# d[i] = max(1, d[i])
ans+=(i*d[i])%MOD
# if d[i]>1:
# sum_so_far += d[i]
print(ans%MOD)
| 0 | null | 19,005,301,212,732 | 57 | 176 |
num = list(map(int,input().split()))
print("%d %d %f" %(num[0]/num[1],num[0]%num[1],num[0]/num[1] ))
|
a,b = map(int, input().split())
print(f"{a//b} {a%b} {a/b:.5f}")
| 1 | 604,089,590,958 | null | 45 | 45 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.