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
from collections import deque h, w = map(int, input().split()) S = [list(input()) for _ in range(h)] DP = [[10000]*w for _ in range(h)] DP[0][0] = 0 if S[0][0]=='.' else 1 directs = [[0,1], [1,0]] for hi in range(h): for wi in range(w): for dh,dw in directs: if not (0<=hi+dh<h and 0<=wi+dw<w): continue if S[hi][wi]=='.' and S[hi+dh][wi+dw]=='#': DP[hi+dh][wi+dw] = min(DP[hi+dh][wi+dw], DP[hi][wi]+1) else: DP[hi+dh][wi+dw] = min(DP[hi+dh][wi+dw], DP[hi][wi]) print(DP[-1][-1])
N = int(input()) def factorize(n): result = [] tmp = n for i in range(2, int(n**0.5)+1): if tmp % i == 0: cnt = 0 while tmp % i == 0: cnt += 1 tmp //= i result.append(cnt) if tmp != 1: result.append(1) if len(result) == 0: result.append(1) return result def nearest_sum(n): num_list = [1] if n <= 2: return 1 i = 2 while True: num_list.append(num_list[-1]+i) i += 1 if num_list[-1]+i > n: break return i-1 if N == 1: print(0) else: ind_list = factorize(N) cnt = 0 for ind in ind_list: cnt += nearest_sum(ind) print(cnt)
0
null
32,933,490,106,468
194
136
if __name__ == "__main__": alpha = input() if alpha in ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']: print('A') else: print('a')
a = input() print('A' if a.upper() == a else 'a')
1
11,340,449,419,200
null
119
119
from sys import stdin def ip(): return stdin.readline().rstrip() d,t,s=map(int,ip().split()) if d<=t*s: print('Yes') else: print('No')
input_list = [int(num) for num in input().split()] D = input_list[0] T = input_list[1] S = input_list[2] if D/S <= T: print("Yes") else: print("No")
1
3,512,960,285,368
null
81
81
n = int(input()) s = [None] * n for i in range(n): s[i] = input() print(len(list(set(s))))
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines a, b = map(int, readline().split()) ans = a - b * 2 if ans < 0: print(0) else: print(ans)
0
null
98,412,382,941,072
165
291
def main(): A1, A2, A3 = map(int, input().split()) if A1 + A2 + A3 >= 22: ans = "bust" else: ans = "win" print(ans) if __name__ == "__main__": main()
A1, A2, A3 = (int(x) for x in input().split()) if A1+A2+A3>=22: print("bust") else: print("win")
1
118,350,784,303,368
null
260
260
n = input() minSeq = input() maxv = -float("inf") while True: try: R = input() if R - minSeq > maxv: maxv = R - minSeq if R < minSeq: minSeq = R except EOFError: break print(maxv)
n = int(raw_input()) r = [] for i in range(n): r.append(int(raw_input())) min_v = r[0] max_profit = -1000000000000 for i in range(1,n): if max_profit < r[i] - min_v: max_profit = r[i]-min_v if r[i] < min_v: min_v = r[i] print max_profit
1
13,852,710,608
null
13
13
N, K = map(int, input().split()) LR = [tuple(map(int, input().split())) for _ in range(K)] MOD = 998244353 class Mint: __slots__ = ('value') def __init__(self, value=0): self.value = value % MOD if self.value < 0: self.value += MOD @staticmethod def get_value(x): return x.value if isinstance(x, Mint) else x def inverse(self): a, b = self.value, MOD u, v = 1, 0 while b: t = a // b b, a = a - t * b, b v, u = u - t * v, v if u < 0: u += MOD return u def __repr__(self): return str(self.value) def __eq__(self, other): return self.value == other.value def __neg__(self): return Mint(-self.value) def __hash__(self): return hash(self.value) def __bool__(self): return self.value != 0 def __iadd__(self, other): self.value = (self.value + Mint.get_value(other)) % MOD return self def __add__(self, other): new_obj = Mint(self.value) new_obj += other return new_obj __radd__ = __add__ def __isub__(self, other): self.value = (self.value - Mint.get_value(other)) % MOD if self.value < 0: self.value += MOD return self def __sub__(self, other): new_obj = Mint(self.value) new_obj -= other return new_obj def __rsub__(self, other): new_obj = Mint(Mint.get_value(other)) new_obj -= self return new_obj def __imul__(self, other): self.value = self.value * Mint.get_value(other) % MOD return self def __mul__(self, other): new_obj = Mint(self.value) new_obj *= other return new_obj __rmul__ = __mul__ def __ifloordiv__(self, other): other = other if isinstance(other, Mint) else Mint(other) self *= other.inverse() return self def __floordiv__(self, other): new_obj = Mint(self.value) new_obj //= other return new_obj def __rfloordiv__(self, other): new_obj = Mint(Mint.get_value(other)) new_obj //= self return new_obj LR.sort(key=lambda x: x[1]) pl = pr = -1 LR2 = [] for l, r in LR: if LR2: pl, pr = LR2.pop() if l <= pl: LR2.append((l, r)) elif l <= pr: LR2.append((pl, r)) else: LR2.append((pl, pr)) LR2.append((l, r)) else: LR2.append((l, r)) dp = [Mint() for _ in range(N + 5)] dp[1] += 1 dp[2] -= 1 for i in range(1, N): dp[i] += dp[i - 1] for l, r in LR2: nl = min(i + l, N + 1) nr = min(i + r, N + 1) dp[nl] += dp[i] dp[nr + 1] -= dp[i] dp[N] += dp[N - 1] print(dp[N])
import sys, math from collections import defaultdict, deque, Counter from bisect import bisect_left, bisect_right from itertools import combinations, permutations, product from heapq import heappush, heappop from functools import lru_cache input = sys.stdin.readline rs = lambda: input().strip() ri = lambda: int(input()) rl = lambda: list(map(int, input().split())) mat = lambda x, y, v: [[v]*y for _ in range(x)] ten = lambda x, y, z, v: [mat(y, z, v) for _ in range(x)] mod = 1000000007 sys.setrecursionlimit(1000000) mod = 998244353 N, K = rl() lr = [] for i in range(K): l, r = rl() lr.append((l,r)) lr.sort() dp = [0] * (N+1) dp[1] = 1 dp[2] = -1 ans = 0 s = 0 flags = [1] * K for i in range(1, N+1): s += dp[i] s %= mod for k in range(K): if flags[k] == 0: break l, r = lr[k] if i+l <= N: dp[i+l] += s if i+r+1 <= N: dp[i+r+1] -= s else: ans += s ans %= mod break else: flags[k] = 0 break print(ans)
1
2,653,297,786,232
null
74
74
n = int(raw_input()) N = 100 WHITE = 0 GRAY = 1 BLACK = 2 Color = [WHITE for i in range(n)] #M = [[0 for i in range(N)] for j in range(N)] tt = 0 d = [0 for i in range(n)] f = [0 for i in range(n)] def dfs_visit(u): Color[u] = GRAY global tt tt += 1 d[u] = tt for v in range(n): if M[u][v] == 0: continue if Color[v] == WHITE: dfs_visit(v) Color[u] = BLACK tt += 1 f[u] = tt def dfs(): #Color = [WHITE for i in range(n)] tt = 0 for u in range(n): if Color[u] == WHITE: dfs_visit(u) for u in range(n): print "%d %d %d" %(u + 1, d[u], f[u]) M = [[0 for j in range(n)] for i in range(n)] for i in range(n): tmp = map(int, raw_input().split()) u = tmp[0] k = tmp[1] u -= 1 for j in range(k): v = tmp[j + 2] v -= 1 M[u][v] = 1 dfs()
#時間の無駄なのでクソ問と割り切って飛ばす def prod(array): res = 1 for a, _ in array: res *= a res %= MOD return res MOD = 10**9 + 7 N, K = map(int, input().split()) if N==K: res = 1 for a in map(int, input().split()): res *= a res %= MOD print(res) exit() A = [] allsgn = 0 for a in map(int, input().split()): A.append((abs(a), a < 0)) allsgn += a<0 A.sort(reverse=True) sgn = 0 s0 = -1 lastA = -1 for i, p in enumerate(A[:K]): a, s = p sgn += s if s != s0: lastA = i-1 s0 = s lasts = s0 if allsgn==N: if K%2==1: print((-prod(A[-K:]))%MOD) exit() else: print(prod(A[:K])%MOD) exit() if sgn%2==0: print(prod(A[:K])%MOD) exit() if lasts != A[K][1]: res = prod(A[:K-1])*A[K][0] else: firstA = None for i, p in enumerate(A[K:],start=K): a, s = p if s != lasts: firstA = i break if lastA == -1 or (firstA is not None and A[K][0]/A[lastA][0] < A[firstA][0]/A[K-1][0]): res = prod(A[:K - 1]) * A[firstA][0] else: res = prod(A[:lastA]) * prod(A[lastA + 1:K + 1]) print(res%MOD) """ 6 3 5 -3 -3 -3 -3 -2 10 7 -10 -10 -10 -10 10 10 10 10 10 10 """
0
null
4,700,713,360,808
8
112
N = int(input().rstrip()) S = input().rstrip() r = S.count('ABC') print(r)
n = int(input()) s = input().replace('ABC','1') print(s.count('1'))
1
99,920,095,946,818
null
245
245
def main(): k = int(input()) s = input() s_len = len(s) if s_len <= k: print(s) else: s = s[0:k] + '...' print(s) main()
k=int(input()) s=str(input()) long =len(s) i =0 ans = str() if k>long: print(s) else: for i in range(long): if i<k: ans = str(ans+s[i]) else: ans = str(ans+"...") break print(ans)
1
19,609,033,158,172
null
143
143
n,m=map(int,input().split()) a=list(map(int,input().split())) a.sort(reverse=True) total=0 for i in range(m): if a[i]>=(sum(a)/(4*m)): total+=1 if total==m: print("Yes") else: print("No")
N, M = map(int, input().split()) aaa = list(map(int, input().split())) acc = sum(aaa) print('Yes' if sum(4 * M * a >= acc for a in aaa) >= M else 'No')
1
38,797,947,549,648
null
179
179
num = int(input()) tscore, hscore = 0, 0 for i in range(num): ts, hs = input().split() if ts > hs: tscore += 3 elif ts < hs: hscore += 3 else: tscore += 1 hscore += 1 print(tscore, hscore)
#init N = int(input()) S=[input() for i in range(N)] for word in ['AC', 'WA', 'TLE', 'RE']: print('{0} x {1}'.format(word, S.count(word)))
0
null
5,328,048,478,400
67
109
import sys from collections import defaultdict N,S=map(int, sys.stdin.readline().split()) A=map(int, sys.stdin.readline().split()) mod=998244353 cur=defaultdict(lambda: 0) new=defaultdict(lambda: 0) cur[0]=1 for a in A: for i in cur.keys(): if i+a<=S: new[i+a]+=cur[i] new[i+a]%=mod new[i]+=cur[i]*2 new[i]%=mod cur=new new=defaultdict(lambda: 0) print cur[S]
N = int(input()) a = (N + 2 - 1) // 2 print(a)
0
null
38,160,280,899,566
138
206
K, X = map(int, input().split()) print('Yes' if 500 * K >= X else 'No')
from sys import exit x, n = map(int, input().split()) p = list(map(int, input().split())) for d in range(x+1): #0に達した時点で0を出力して終了するので、大きい側は気にしなくてよい for s in [-1, 1]: a = x + d*s if not a in p: print(a) exit()
0
null
56,018,047,824,146
244
128
n = (int)(input()) ret = 0 for i in range(n+1): if i % 3 != 0 and i % 5 != 0: ret += i print("{}".format(ret))
print(sum([i for i in range(int(input()) + 1) if (i % 3 != 0 and i % 5 != 0)]))
1
34,933,456,942,208
null
173
173
s=input() for c in s: if 'A' <= c and c <= 'Z': print(c.lower(), end='') elif 'a' <= c and c <= 'z': print(c.upper(), end='') else: print(c,end='') print()
S = input() Slist = list(S) big = ord('A') small = ord('a') diff = big-small for i in range(len(Slist)): w = ord(Slist[i]) if small <= w: #?°????????????? Slist[i] = chr(w+diff) elif big <= w: Slist[i] = chr(w-diff) print(''.join(Slist))
1
1,529,379,364,412
null
61
61
h, w, z = map(int ,input().split()) s = [] for i in range(h): s.append(list(input())) ans = int(1e9+7) for i2 in range(2**(h-1)): d = [True] * (h-1) for j2 in range(h-1): if (i2 >> j2) & 1: d[j2] = False d.append(False) val = 0 for i in range(len(d)-1): if d[i] == False: val += 1 #print(val,d) l = [] lis = [] for i in range(h): l.append(i) if d[i] == False: lis.append(l) l = [] flg2 = True#横がiのときにcnt2の要素がkを超えるとFalse flg = True#cntの要素がkを超えるとFalse cnt = [0] * len(lis)#全体での1の個数 for i in range(w): cnt2 = [0] * len(lis)#iでの1の個数 for j in range(len(lis)): for k in lis[j]: if s[k][i] == '1':#高さがkで横がiの時、 cnt[j] += 1#lis[j]を1たす cnt2[j] += 1 if cnt[j] > z: flg = False if cnt2[j] > z: flg2 = False if flg == False: for j in range(len(cnt)): cnt[j] = cnt2[j] flg = True val += 1 #print(cnt,cnt2) #print(i,val) if flg2 == True: ans = min(ans,val) #print(val,d,lis,flg) print(ans)
# -*- coding: utf-8 -*- import math import itertools import sys import copy #import numpy as np # 入力 #A, B, C, D = map(int, input().split()) #L = list(map(int, input().split())) #S = list(str(input())) #N = int(input()) #S = str(input()) K = int(input()) l = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51] print(l[K-1])
0
null
49,522,261,750,468
193
195
from sys import stdin import sys import math from functools import reduce import functools import itertools from collections import deque x = int(stdin.readline().rstrip()) for i in range(-150,150): for j in range(-150,150): if i**5-j**5 == x: print(i,j); sys.exit()
from collections import defaultdict d = defaultdict(list) for i in range(-120, 121): for j in range(-120, 121): x = i**5 - j**5 if x >= 1: d[x] = [i, j] X = int(input()) print('{0[0]} {0[1]}'.format(d[X]))
1
25,515,935,452,282
null
156
156
s=input() ch=False for i in s: if i=="7":ch=True if ch:print("Yes") else:print("No")
import math x1,y1,x2,y2=map(float,input().split()) A=abs(y1-y2) B=abs(x1-x2) C=math.sqrt(A**2+B**2) print(f'{C:.8f}')
0
null
17,087,807,306,830
172
29
import collections MOD = 998244353 def main(): N = int(input()) D = [int(_) for _ in input().split()] Dmax = max(D) cc = collections.Counter(D) if D[0] != 0: return 0 if cc[0] != 1: return 0 for i in range(Dmax+1): if cc[i] == 0: return 0 output = 1 for i in sorted(cc.keys()): if i in (0, 1): continue output *= pow(cc[i-1], cc[i], MOD) output %= MOD return output if __name__ == '__main__': print(main())
from collections import Counter N = int(input()) D = list(map(int, input().split())) mod = 998244353 c = Counter(D) m = max(c.keys()) if D[0] == 0 and c[0] == 1: ans = 1 for i in range(1, m+1): ans *= c[i-1]**c[i] ans %= mod print(ans) else: print(0)
1
155,053,976,621,440
null
284
284
N = int(input()) S, T = map(str,input().split()) for s, t in zip(S, T): print(s+t, end='')
h1, m1, h2, m2, k = map(int, input().split()) dh = h2 - h1 dm = m2 - m1 if dm < 0: dh -= 1 dm += 60 wm = dh*60 + dm ans = wm - k print(ans)
0
null
65,135,189,067,520
255
139
import math while True: try: a = list(map(int,input().split())) b = (math.gcd(a[0],a[1])) c = ((a[0]*a[1])//math.gcd(a[0],a[1])) print(b,c) except EOFError: break
import math while True: try: a,b=list(map(int,input().split())) x=math.gcd(a,b) y=int(a*b/x) print("%d %d"%(x,y)) except: break
1
634,557,000
null
5
5
# -*- coding: utf-8 -*- """ Created on Sun Sep 6 12:21:24 2020 @author: liang """ import math pi = 3.141592653589793238 h, m, H, M = map(int,input().split()) print(math.sqrt(h**2+m**2-2*h*m*math.cos(2*pi*(30*H-5.5*M)/360)))
n, d = map(int, input().split()) import math count = 0 for i in range(n): x, y = map(int, input().split()) distance = math.sqrt(x**2+y**2) if distance <= d: count += 1 print(count)
0
null
13,045,261,920,512
144
96
a,b,c,k = map(int,input().split()) #l = [1 for i in range(a)] + [0 for i in range(b)] + [-1 for i in range(c)] #ans = sum(l[:k]) if k <= a: ans = k elif (a < k) & (k <= a + b): ans = a elif (a + b < k) & (k <= a + b + c): ans = a - (k - (a + b)) print(ans)
import sys def input(): return sys.stdin.readline().strip() def I(): return int(input()) def LI(): return list(map(int, input().split())) def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def S(): return input() def LS(): return input().split() INF = float('inf') s = S() n = len(s) ans = 0 cnt = 0 before_cnt = 0 now = s[0] for x in s: if now == x: # 同じ不等号が連続しているとき cnt += 1 else: # 不等号が変わるとき ans += cnt * (cnt + 1) // 2 if now == '>': # > から < に変わるとき if before_cnt < cnt: ans -= before_cnt else: ans -= cnt before_cnt = cnt cnt = 1 now = x ans += cnt * (cnt + 1) // 2 if now == '>': if before_cnt < cnt: ans -= before_cnt else: ans -= cnt print(ans)
0
null
89,323,149,021,312
148
285
import math while True: try: a, b = [int(i) for i in input().split()] gcd = math.gcd(a, b) lcm = a * b // gcd print(f'{gcd} {lcm}') except EOFError: break
# -*- coding: utf-8 -*- import sys def gcd(a, b): if a < b: a, b = b, a while b!=0: c = b b = a % b a = c return a def lcm(a, b, g): return a * b / g def main(): for line in sys.stdin: a, b = map(int, line.split()) ng = gcd(a, b) nl = lcm(a, b, ng) print('{0:d} {1:d}'.format(int(ng), int(nl))) if __name__ == '__main__': main()
1
682,741,840
null
5
5
a,b,k=map(int,input().split()) if a>=k: print(a-k,b) elif a+b>=k: print(0,a+b-k) else: print(0,0)
A, B, K = map(int, input().split()) if A < K: K = K - A A = 0 elif A == K: A = 0 K = 0 else: # K < A A = A - K K = 0 if B < K: K = K - B B = 0 elif B == K: B = 0 K = 0 else: # K < B B = B - K K = 0 print(A, B)
1
104,388,242,393,402
null
249
249
T=input() print(T.replace('?','D'))
K=int(input()) import math l=[] for i in range(1,K+1): for j in range(1,K+1): for k in range(1,K+1): x=math.gcd(i,math.gcd(j,k)) l.append(x) print(sum(l))
0
null
26,952,347,637,930
140
174
from operator import mul from functools import reduce def cmb(n,r): #n<10^4で最速s r = min(n-r,r) if r == 0: return 1 over = reduce(mul, range(n, n - r, -1)) under = reduce(mul, range(1,r + 1)) return over // under n,m=map(int,input().split()) if n>=2 and m>=2:print(cmb(n,2)+cmb(m,2)) elif n>=2:print(cmb(n,2)) elif m>=2:print(cmb(m,2)) else:print(0)
def main(): n, m = list(map(int, input().split(" "))) ans = 0 ans += n*(n-1)/2 ans += m*(m-1)/2 print(int(ans)) if __name__ == "__main__": main()
1
45,703,815,892,670
null
189
189
import sys, re from collections import deque, defaultdict, Counter from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement from operator import itemgetter, mul from copy import deepcopy from string import ascii_lowercase, ascii_uppercase, digits from bisect import bisect, bisect_left from heapq import heappush, heappop from functools import reduce def input(): return sys.stdin.readline().strip() def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(): return list(map(int, input().split())) def ZIP(n): return zip(*(MAP() for _ in range(n))) sys.setrecursionlimit(10 ** 9) INF = float('inf') mod = 10 ** 9 + 7 lim = 2*(10**6) # 必要そうな階乗の限界を入れる fact = [1] * (lim+1) fact_inv = [1] * (lim+1) for n in range(1, lim+1): fact[n] = (fact[n-1] * n) % mod fact_inv[lim] = pow(fact[lim], mod-2, mod) for n in range(lim, 0, -1): fact_inv[n-1] = (n * fact_inv[n]) % mod def C(n, r): return (((fact[n] * fact_inv[r]) % mod) * fact_inv[n-r]) % mod K = INT() S = input() L = len(S) ans = 0 for i in range(K+1): ans += pow(26, K-i, mod)*pow(25, i, mod)*C(i+L-1, i) ans %= mod print(ans)
k = int(input()) s = input() MOD = 10**9+7 class Facts(): # O(max_num) def __init__(self, max_num=10**5, p=10**9+7): self.p = p self.max_num = max_num self.fact = [1] * (self.max_num + 1) self.rev = [1] * (self.max_num + 1) for i in range(1, self.max_num + 1): self.fact[i] = (self.fact[i-1] * i) % self.p self.rev[self.max_num] = self.power_func(self.fact[self.max_num], self.p-2) for i in range(self.max_num-1, 0, -1): self.rev[i] = self.rev[i+1] * (i + 1) % self.p def comb(self, n, k): if n < 0 or k < 0 or n < k: return 0 if n == 0 or k == 0: return 1 res = ((self.fact[n] * self.rev[k] % self.p) * self.rev[n - k]) % self.p return res def power_func(self, a, b): # a^b mod p # O(log(b)) ans = 1 while b > 0: if b & 1: ans = ans * a % self.p a = a * a % self.p b >>= 1 return ans r26 = [1 for _ in range(k + 1)] r25 = [1 for _ in range(k + 1)] for i in range(1, k+1): r26[i] = (r26[i-1] * 26) % MOD r25[i] = (r25[i-1] * 25) % MOD facts = Facts(max_num=len(s)+k+1, p=MOD) total = len(s) + k ans = 0 for s_n in range(len(s), total+1): d = total - s_n ans += facts.comb(total - d - 1, len(s) - 1) * r25[k-d] * r26[d] ans %= MOD print(ans)
1
12,848,966,599,040
null
124
124
a,b,h,w=map(int,input().split()) import math theta=math.radians(abs((h+w/60)*360/12-w*360/60)) c=(a**2+b**2-2*a*b*math.cos(theta))**0.5 print(c)
n,k = map(int,input().split()) p = list(map(int,input().split())) p.sort() print(sum(p[:k]))
0
null
15,846,196,966,670
144
120
#a問題 n=int(input()) print(n+(n**2)+(n**3))
a = int(input()) f = a + a**2 + a**3 print(f)
1
10,180,565,840,292
null
115
115
a=[] for i in range(10): a.append(int(input())) a=sorted(a)[::-1] for i in range(3): print(a[i])
MOD = 998244353 N, M, K = map(int, input().split()) fac = [1] * N for i in range(1, N): fac[i] = (fac[i - 1] * i) % MOD pow_mmm = [1] * N for i in range(1, N): pow_mmm[i] = (pow_mmm[i - 1] * (M - 1)) % MOD ans = 0 for i in range(K + 1): t = (M * pow_mmm[N - 1 - i]) % MOD comb = (fac[N - 1] * pow(fac[N - 1 - i], MOD - 2, MOD) * pow(fac[i], MOD - 2, MOD)) % MOD t = (t * comb) % MOD ans = (ans + t) % MOD print(ans)
0
null
11,511,042,030,880
2
151
# Pythonのスライスは最後が開区間だから癖がある t = input() for i in range(int(input())): cmd = list(input().split()) cmd[1] = int(cmd[1]) cmd[2] = int(cmd[2]) if cmd[0] == "print": print(t[cmd[1] : cmd[2]] + t[cmd[2]]) elif cmd[0] == "reverse": tmp = t[cmd[1] : cmd[2]] + t[cmd[2]] tmp = tmp[::-1] t = t[:cmd[1]] + tmp + t[cmd[2]+1:] elif cmd[0] == "replace": t = t[:cmd[1]] + cmd[3] + t[cmd[2]+1:]
s = input() n = int(input()) for _ in range(n): O = list(map(str, input().split())) i = int(O[1]) j = int(O[2]) if O[0] == 'print': print(s[i:j + 1]) elif O[0] == 'reverse': ss = s[i:j + 1] s = s[:i] + ss[::-1] + s[j + 1:] else: p = O[3] s = s[:i] + p + s[j + 1:]
1
2,070,777,746,150
null
68
68
n, k = map(int, input().split()) w = [int(input()) for _ in range(n)] left = 0 right = 100000*10000 while (right - left > 1): mid = (left + right)//2 s = 0 j = 1 for i in range(n): if mid < w[i]: j = 100000 break s += w[i] if s > mid: j += 1 s = w[i] if j > k: left = mid else: right = mid print(right)
import math n, k = map(int, input().split()) w = [int(input()) for i in range(n)] def check(P): # 最駄積載量Pのk台のトラックで何個目の荷物まで積めるか。積める荷物の数をreturn i = 0 for j in range(k): s = 0 while (s + w[i]) <= P: s += w[i] i += 1 if (i==n): return n return i # 今、条件より1<=w<=10,000であり、1<=n<=100000 # つまり、トラックの最大積載量の取りうる最大値はこれらが一つのトラックに乗る時、つまり100000*10000 # 従ってこの範囲でPを探索すればよい # 二分探索を使うことができる(これらのmidをPと仮定してcheckで積める個数を調べる #→これがnよりも大きければPはもっと小さくて良いと言うことなのでrightをmidに、小さければleftをmidに! def solve(): left = 0 right = 100000*10000 while (right - left) > 1: mid = (left + right) / 2 v = check(mid) if (v >= n): # ? right = mid else: left = mid return right ans = math.floor(solve()) print(ans)
1
88,799,710,780
null
24
24
class Combination: def __init__(self, max_n, _mod): self.mod = _mod self.fac = [0 for _ in range(max_n + 10)] self.finv = [0 for _ in range(max_n + 10)] self.inv = [0 for _ in range(max_n + 10)] self.fac[0], self.fac[1] = 1, 1 self.finv[0], self.finv[1] = 1, 1 self.inv[1] = 1 for i in range(2, max_n + 10): self.fac[i] = self.fac[i - 1] * i % self.mod self.inv[i] = self.mod self.inv[i] -= self.inv[self.mod % i] * (self.mod // i) % self.mod self.finv[i] = self.finv[i - 1] * self.inv[i] % self.mod def mod_comb(self, n, k): if n < k or n < 0 or k < 0: return 0 res = self.fac[n] * (self.finv[k] * self.finv[n - k] % self.mod) res %= self.mod return res def main(): mod = 998244353 N, M, K = map(int, input().split()) comb = Combination(N, mod) ans = 0 for i in range(K + 1): add_v = M * pow(M - 1, N - i - 1, mod) % mod add_v = (add_v * comb.mod_comb(N - 1, i)) % mod ans = (ans + add_v) % mod print(ans) main()
n,K = list(map(int,input().split())) mod = 10**9+7 ans = 0 for k in range(K,n+2): #ans += (n+1)*n//2 -(n+1-k)*(n-k)//2-k*(k-1)//2 + 1 #ans += (n**2+n)//2 - (n**2-n*k+n-k-n*k+k**2)//2 - (k**2-k)//2 + 1 #ans += (n**2+n - (n**2-n*k+n-k-n*k+k**2) -k**2 + k)//2 + 1 #ans += (n**2 + n - n**2 + n*k - n + k + n*k - k**2 - k**2 + k)//2 + 1 #ans += (2*n*k + 2*k - 2*k**2)//2 + 1 ans += n*k - k**2 + k + 1 print(ans%mod)
0
null
28,342,825,211,748
151
170
while True : x = input() if x==0: break else: Count = 0 while x>0 : Count += x%10 x /= 10 print '%d' %Count
while True: input = raw_input() if input == "0": break sum = 0 for i in xrange(len(input)): sum += int(input[i]) print sum
1
1,589,088,990,412
null
62
62
a,b = map(int,input().split()) l = [str(a)*b, str(b)*a] l.sort() print(l[0])
n = int(input()) a = [0] + list(map(int, input().split())) ''' ans = 1000 for i in range(n - 1): x = a[i] y = a[i + 1] if x < y: k = ans // x ans %= x ans += k * y print(ans) ''' dp = [0] * (n + 10) dp[1] = 1000 for i in range(2, n + 1): dp[i] = dp[i - 1] for j in range(1, i): x = dp[j] // a[j] y = dp[j] + (a[i] - a[j]) * x dp[i] = max(dp[i], y) ans = dp[n] print(ans)
0
null
45,844,124,050,820
232
103
import sys data=list(map(int,input().split())) order=sys.stdin.readline() char_list=list(order) for i in char_list: if i=='N': k=data[0] m=data[4] data[0]=data[1] data[1]=data[5] data[4]=k data[5]=m elif i=='E': k=data[0] m=data[2] data[0]=data[3] data[2]=k data[3]=data[5] data[5]=m elif i=='S': k=data[0] m=data[1] data[0]=data[4] data[1]=k data[4]=data[5] data[5]=m elif i=='W': k=data[0] m=data[3] data[0]=data[2] data[2]=data[5] data[3]=k data[5]=m print(data[0])
import collections N, K = map(int, input().split()) A = list(map(int, input().split())) table = collections.defaultdict(int) ans = 0 s = [0]*(N+1) a = set() for i in range(N): s[i+1] = s[i]+A[i] s = [(s[i]-i) % K for i in range(N+1)] for i in range(N+1): ans += table[s[i]] # i番目まで取る組み合わせの数 table[s[i]] += 1 if i >= K-1: # Kで割ったあまりがKを超えることはないので、期限切れのものを引く table[s[i-K+1]] -= 1 print(ans)
0
null
69,258,337,730,342
33
273
import math print(math.ceil(int(input())/2))
ans = (int(input()) - 1) // 2 + 1 print(ans)
1
59,024,660,653,668
null
206
206
N=int(input()) A=list(map(int,input().split())) B=sum(A) b=0 ans=202020202020 for a in A: b+=a ans=min(ans,abs(B-b*2)) print(ans)
a = str(input()) alpha2num = lambda c: ord(c) - ord('A') + 1 b = alpha2num(a) c = int(b) + 1 num2alpha = lambda c: chr(c+64) print(num2alpha(c))
0
null
117,472,032,436,672
276
239
import sys def input(): return sys.stdin.readline().strip() def mapint(): return map(int, input().split()) sys.setrecursionlimit(10**9) N, M = mapint() S = list(input()) def solve(): now = N choice = [] while 1: if now==0: return choice[::-1] for m in range(M, 0, -1): nx = now-m if nx<0: continue if S[nx]=='1': continue now = nx choice.append(m) break else: return [-1] print(*solve())
import sys sys.setrecursionlimit(10**6) n,m=map(int,input().split()) s=input() ans=[] def f(i,j): if i==0: print(" ".join(map(str,ans[::-1]))) return if j>i: j=i while s[i-j]=="1": j-=1 if j==0: print(-1) return ans.append(j) f(i-j,m) f(n,m)
1
138,777,071,919,540
null
274
274
H, W = map(int, input().split()) arr = [input().rstrip() for h in range(H)] ans = [[0] * W for _ in range(H)] for h, row in enumerate(arr): for w, val in enumerate(row): if h == 0 and w == 0: if arr[h][w] == "#": ans[h][w] = 1 continue nv = 1e+9 if h > 0: nv = ans[h-1][w] if arr[h-1][w] == "." and val == "#": nv += 1 if w > 0: tmp = ans[h][w-1] if arr[h][w-1] == "." and val == "#": tmp += 1 nv = min(nv, tmp) ans[h][w] = nv print(ans[-1][-1])
tmp = input().split(" ") N = int(tmp[0]) K = int(tmp[1]) ary = list(map(lambda x: int(x), input().split(" "))) ary.sort() print(sum(ary[0:K]))
0
null
30,598,413,610,642
194
120
a, b, c = map(int, input().split()) if(a==b or a==c or c==b): if(a==b and b==c): pass else: print("Yes") exit() print("No")
print( 'Yes' if len(set(input().split())) == 2 else 'No')
1
68,242,069,808,682
null
216
216
from itertools import combinations_with_replacement def main(): N, M, Q = map(int, input().split()) qs = [list(map(int,input().split())) for _ in range(Q)] seq = [i for i in range(1,M+1)] C = combinations_with_replacement(seq, N) max_score = 0 for A in C: A = sorted(A) score = 0 for q in qs: if A[q[1]-1] - A[q[0]-1] == q[2]: score += q[3] max_score = max(score, max_score) print(max_score) if __name__ == '__main__': main()
n = int(input()) s = input() m = len(s.replace("ABC", "")) print((n - m) // 3)
0
null
63,822,315,266,360
160
245
N, S = map(int, input().split()) A = list(map(int, input().split())) mod = 998244353 dp = [[0 for j in range(S+1)] for i in range(N+1)] dp[0][0] = 1 for i in range(N): for j in range(S+1): dp[i+1][j] += 2*dp[i][j] dp[i+1][j] %= mod if j + A[i] <= S: dp[i+1][j+A[i]] += dp[i][j] dp[i+1][j+A[i]] %= mod print(dp[N][S])
n, s = map(int, input().split()) a = list(map(int, input().split())) dp = [[0]*(s+1) for _ in range(n+1)] dp[0][0] = 1 mod = 998244353 for i in range(n): for j in range(s+1): dp[i+1][j] = dp[i][j]*2 if a[i]<=j: dp[i+1][j] += dp[i][j-a[i]] dp[i+1][j] %= mod print(dp[n][s])
1
17,649,667,599,580
null
138
138
#!/usr/bin/env python3 # -*- coding: utf-8 -*- def main(): numbers = input().split(' ') height = int(numbers[0]) width = int(numbers[1]) print(height * width ,height * 2 + width * 2) return if __name__ == '__main__': main()
N = int(input()) A = list(map(int, input().split())) count = 0 now = 1 for i in range(len(A)): if A[i] == now: now += 1 else: count += 1 print(count if now != 1 else -1)
0
null
57,485,106,426,818
36
257
X, N = map(int, input().split()) ps = list(map(int, input().split())) for i in range(100): if X - i not in ps: print(X-i) exit() elif X + i not in ps: print(X+i) exit()
n = int(input()) nums = list(map(int, input().split())) q = int(input()) m = list(map(int, input().split())) li = [] for i in range(2**n): an = format(i, 'b').zfill(n) li.append(an) ans = [] for lis in li: num = 0 for i in range(n): if lis[i] == '1': num += nums[i] ans.append(num) for j in m: if j in ans: print('yes') else: print('no')
0
null
7,017,301,885,770
128
25
from collections import deque n = int(input()) dll = deque() for i in range(n): input_line = input().split() if input_line[0] == "insert": dll.appendleft(input_line[1]) elif len(dll) == 0: continue elif input_line[0] == "delete": try: dll.remove(input_line[1]) except: pass elif input_line[0] == "deleteFirst": dll.popleft() else: dll.pop() print(*dll)
import sys n,k=map(int,input().split()) w=list(map(int,sys.stdin.readlines())) def f(): i=0 for _ in[0]*k: s=0 while s+w[i]<=m: s+=w[i];i+=1 if i==n:return n return i l,r=max(w),sum(w) while l<r: m=(l+r)//2 if f()>=n:r=m else:l=m+1 print(r)
0
null
69,597,923,150
20
24
N = int(input()) A = list(map(int,input().split())) A.insert(0,0) C = {i:0 for i in range(1,N+1)} for i in range(1,N+1): C[A[i]] += 1 tot = 0 for i in range(1,N+1): tot += (C[i]*(C[i]-1))//2 for i in range(1,N+1): c = C[A[i]]-1 ans = tot-(C[A[i]]*(C[A[i]]-1))//2+(c*(c-1))//2 print(ans)
import collections n = int(input()) num_list = list(map(int, input().split())) all = 0 c = collections.Counter(num_list) for i in c: all += c[i]*(c[i]-1)//2 for k in range(n): print(all - c[num_list[k]] + 1)
1
47,589,394,403,850
null
192
192
x = int(input()) if x == 2: print(2) exit() for i in range(1000000): for j in range(2, x+i): if (x+i) % j == 0: break if j == x+i-1: print(x+i) exit()
import math def main(): X = int(input()) if X == 2: return 2 if X == 3: return 3 if X == 4 or X == 5: return 5 if X == 6 or X == 7: return 7 if X == 8: return 11 while True: if X % 2 != 0: for i in range(3, int(math.sqrt(X))+1, 2): if X % i == 0: break else: return X X += 1 ans = main() print(ans)
1
105,342,673,618,980
null
250
250
import sys from collections import deque def main(): h, w = map(int, input().split()) area = [[s for s in sys.stdin.readline().strip()] for _ in range(h)] start_point = {0:set(), 1:set(), 2:set(), 3:set(), 4:set()} min_neighbor = 4 for i in range(h): for j in range(w): if area[i][j] == '#': continue roads = 0 if i > 0 and area[i - 1][j] == '.': roads += 1 if i < h - 1 and area[i + 1][j] == '.': roads += 1 if j > 0 and area[i][j - 1] == '.': roads += 1 if j < w - 1 and area[i][j + 1] == '.': roads += 1 min_neighbor = min(min_neighbor, roads) start_point[roads].add((i, j)) max_cost = 0 for start in start_point[min_neighbor]: queue = deque() queue.append(start) cost = 0 visited = set() while len(queue) > 0: roads = len(queue) found = False for i in range(roads): s = queue.popleft() if s in visited: continue found = True visited.add(s) i = s[0] j = s[1] if i > 0 and area[i - 1][j] == '.': queue.append((i - 1, j)) if i < h - 1 and area[i + 1][j] == '.': queue.append((i + 1, j)) if j > 0 and area[i][j - 1] == '.': queue.append((i, j - 1)) if j < w - 1 and area[i][j + 1] == '.': queue.append((i, j + 1)) if not found: cost -= 1 max_cost = max(cost, max_cost) if found: cost += 1 print(max_cost) if __name__ == '__main__': main()
while(True): m, f, r = map(int, input().split(" ")) if (m == -1) and (f == -1) and (r == -1): exit() if (m == -1) or (f == -1): print("F") elif (m + f >= 80): print("A") elif (m + f >= 65) and (m + f < 80): print("B") elif (m + f >= 50) and (m + f < 65): print("C") elif (m + f >= 30) and (m + f < 50): if (r >= 50): print("C") else: print("D") else: print("F")
0
null
47,724,606,249,120
241
57
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN inp = input() a = inp.split() b = int(a[0])//int(a[1]) c = int(a[0])%int(a[1]) d = Decimal(a[0])/Decimal(a[1]) d = d.quantize(Decimal('0.000001'),rounding=ROUND_HALF_UP) print(b,c,d)
import collections N = int(input()) D = list(map(int, input().split())) mod = 998244353 if(D[0] != 0): print(0) else: c = collections.Counter(D) ans = 1 for val in D[1:]: ans = (ans*c[val-1])%mod print(ans)
0
null
77,388,473,491,812
45
284
a, b = input().split() a = int(a) b1, b2 = b.split('.') b3 = int(b1)*100 + int(b2) print(a*b3//100)
N = int(input()) odd = (N+1)//2 print(odd/N)
0
null
97,063,666,477,480
135
297
x = sorted([int(i) for i in input().split()]) print("{0} {1} {2}".format(x[0],x[1],x[2]))
l = [1,2,3] A = int(input()) B = int(input()) for i in range(3): if l[i]!=A and l[i]!=B: print(l[i])
0
null
55,317,484,706,862
40
254
n = int(input()) a = list(map(int, input().split())) #print(a) a.reverse() print(*a)
n = int(input()) s = input().split() s.reverse() print(' '.join(s))
1
983,181,256,160
null
53
53
import string a=string.ascii_lowercase A=string.ascii_uppercase n=input() if n in a : print("a") else : print("A")
H,W,K=map(int,input().split()) S=[[int(s) for s in input()] for i in range(H)] ans=1000000 for i in range(2**(H-1)): tmp=0 L=[] for j in range(H-1): if i>>j&1: L.append(j) tmp+=len(L) L.append(H-1) c=[0]*len(L) for k in range(W): h=0 c1=[0]*len(L) for l in range(len(L)): for m in range(h,L[l]+1): c1[l]+=S[m][k] h=L[l]+1 p=0 for l in range(len(L)): if c1[l]>K: p=2 break elif c[l]+c1[l]>K: p=1 else: c[l]+=c1[l] if p==2: break elif p==1: c=[i for i in c1] tmp+=1 else: ans=min(ans,tmp) print(ans)
0
null
30,093,226,008,670
119
193
import sys class DoubleLinkedList: class Node: def __init__(self, value=None, prev=None, _next=None): self.value = value self.prev = prev self.next = _next def __init__(self): self.head = self.Node() self.tail = self.Node() self.head.next = self.tail self.tail.prev = self.head def insert(self, v): head = self.head node = self.Node(v, head, head.next) head.next.prev = node head.next = node def delete(self, node): if node in (self.head, self.tail): return else: node.prev.next, node.next.prev = node.next, node.prev def delete_first(self): self.delete(self.head.next) def delete_last(self): self.delete(self.tail.prev) def delete_first_value(self, v): node = self.find(v, self.head) if node is not None: self.delete(node) def find(self, v, cur=None): if cur is None: cur = self.head while cur != self.tail: cur = cur.next if cur.value == v: return cur return None def __str__(self): cur = self.head.next vs = '' while cur != self.tail: vs += cur.value cur = cur.next if cur != self.tail: vs += ' ' return vs def run(): steps = int(input()) # flake8: noqa dl = DoubleLinkedList() for command in sys.stdin: if command.startswith('insert'): dl.insert(command[7:-1]) elif command.startswith('deleteFirst'): dl.delete_first() elif command.startswith('deleteLast'): dl.delete_last() elif command.startswith('delete'): dl.delete_first_value(command[7:-1]) else: raise ValueError() print(dl) if __name__ == '__main__': run()
import sys from collections import deque n = int(input()) q = deque() for i in range(n): c = sys.stdin.readline()[:-1] if c[0] == 'i': q.appendleft(c[7:]) elif c[6] == ' ': try: q.remove(c[7:]) except: pass elif c[6] == 'F': q.popleft() else: q.pop() print(*q)
1
48,355,074,970
null
20
20
n,a,b=map(int, input().split()) if (b-a)%2==0: print((b-a)//2) else: print(min(a+(b-a-1)//2, n-b+1+(b-a-1)//2))
n, a, b = map(int, input().split()) if (a+b) % 2 == 0: print((b-a)//2) else: if b == a + 1: print(min(b-1, n-a)) else: cnt_1 = a + (b-a-1)//2 cnt_n = n - b + 1 + (b-a-1)//2 print(min(cnt_1, cnt_n))
1
109,527,775,863,100
null
253
253
x=int(input()) k=100 a=1 for i in range(1,4000): k=(101*k)//100 if k<x: a=a+1 print(a)
import sys import math def Ii():return int(sys.stdin.buffer.readline()) def Mi():return map(int,sys.stdin.buffer.readline().split()) def Li():return list(map(int,sys.stdin.buffer.readline().split())) x = Ii() ans = 0 k = 100 while x > k: tax = k//100 k += tax ans += 1 print(ans)
1
26,937,155,105,680
null
159
159
S = input() A = ["", ""] Q = int(input()) b = 0 for _ in range(Q): query = input() if query[0] == "1": b ^= 1 else: F = int(query[2]) A[b^F-1] += query[4] ans = A[0][::-1] + S + A[1] if b: ans = ans[::-1] print(ans)
import sys ''' ??\????????????????????? ''' _allinput = [] for inp in sys.stdin: _allinput += inp.strip().split(" ") def _input(): for put in _allinput: yield put _obj = _input(); def __input(): return _obj.__next__() def nextInt(): return int(__input()) def check(lst,L): tot = 1 curSum = 0 for i in lst: if curSum + i <= L: curSum += i else: curSum = i tot += 1 return tot def solve(lst,m): l,r = max(lst), 100000 * 10000 while l != r: mid = int((l+r)/2) if check(lst,mid) <= m: r = mid else: l = mid + 1 return l n,m = nextInt(),nextInt() if m > 0 or n > 0: lst = [nextInt() for i in range(0,n)] print(solve(lst,m))
0
null
28,608,866,295,088
204
24
# coding: UTF-8 CONTEST_NUM = 26 class Optimizer: def __init__(self, days, s, c): self._days = days self._s = s self._c = c def optimize(self): # 貪欲法による手法 t = [] for d in range(self._days): max_d_value = 0 max_contest = 0 for i in range(CONTEST_NUM): d_value = self._s[d][i] + self._c[i] * (d + 1) if d_value > max_d_value: max_d_value = d_value max_contest = i t.append(max_contest + 1) return t def main(): ## 引数読み込み days = int(input()) # D c = tuple(map(int, input().split())) # c[i] s = [] # s[d][i] for i in range(days): s.append(tuple(map(int, input().split()))) ## 試しに指標計算 t = Optimizer(days, s, c).optimize() ## print out for v in t: print(v) if __name__ == "__main__": main()
# 貪欲法 + 山登り法 + スワップ操作 import time s__ = time.time() limit = 1.9 #limit = 10 from numba import njit import numpy as np d = int(input()) cs = list(map(int, input().split())) cs = np.array(cs, dtype=np.int64) sm = [list(map(int, input().split())) for _ in range(d)] sm = np.array(sm, dtype=np.int64) @njit('i8(i8[:], i8)', cache=True) def total_satisfaction(ts, d): ls = np.zeros(26, dtype=np.int64) s = 0 for i in range(d): t = ts[i] t -= 1 s += sm[i][t] ls[t] = i + 1 dv = cs * ((i+1) - ls) s -= dv.sum() return s @njit('i8[:]()', cache=True) def greedy(): ts = np.array([0] * d, dtype=np.int64) for i in range(d): mx = -1e10 mxt = None for t in range(1, 26+1): ts[i] = t s = total_satisfaction(ts, i + 1) if s > mx: mx = s mxt = t ts[i] = mxt return ts @njit('i8(i8, i8[:])', cache=True) def loop(mxsc, ts): it = 50 rds = np.random.randint(0, 4, (it,)) rdd = np.random.randint(1, d, (it,)) rdq = np.random.randint(1, 26, (it,)) rdx = np.random.randint(1, 12, (it,)) for i in range(it): bk1 = 0 bk2 = 0 if rds[0] == 0: # trailing di = rdd[i] qi = rdq[i] bk1 = ts[di] ts[di] = qi else: # swap di = rdd[i] xi = rdx[i] if di + xi >= d: xi = di - xi else: xi = di + xi bk1 = ts[di] bk2 = ts[xi] ts[di] = bk2 ts[xi] = bk1 sc = total_satisfaction(ts, d) if sc > mxsc: #print(mxsc, '->', sc) mxsc = sc else: # 最大値を更新しなかったら戻す if rds[0] == 0: ts[di] = bk1 else: ts[di] = bk1 ts[xi] = bk2 return mxsc ts = greedy() mxsc = total_satisfaction(ts, d) mxbk = mxsc s_ = time.time() mxsc = loop(mxsc, ts) e_ = time.time() consume = s_ - s__ elapsed = e_ - s_ #print('consume:', consume) #print('elapsed:', elapsed) if consume < limit: lp = int((limit - consume)/ elapsed) #print('loop', lp) for _ in range(lp): mxsc = loop(mxsc, ts) for t in ts: print(t) #print(mxbk, mxsc) #print(time.time() - s__)
1
9,774,963,469,408
null
113
113
s = input() p = input() s += s if p in s: print("Yes") else: print("No")
s = input() p = input() flg = False for i in range(len(s)): s = s[1:] + s[:1] if p in s: flg = True break if flg: print('Yes') else: print('No')
1
1,715,027,067,832
null
64
64
W = input().lower() count = 0 while True: a = input() if a == "END_OF_TEXT": break a = a.lower() a = a.split() for i in range(len(a)): if W == a[i]: count += 1 print(count)
while True: H,W = list(map(int, input().split())) if (H == 0 and W == 0): break else: for n in range(H): s = "" for m in range(W): if (0 < n and n < H-1) and (0 < m and m < W-1): s += "." else: s += "#" print(s) print("")
0
null
1,327,544,873,092
65
50
N, M = map(int,input().split()) ans = [["",0] for i in range(N)] for i in range(M): p,S = map(str,input().split()) p = int(p)-1 if ans[p][0] != "AC": if S == "AC": ans[p][0] = "AC" elif S == "WA": ans[p][1] += 1 AC = 0 WA = 0 for i in ans: if i[0] == "AC": AC += 1 WA += i[1] print(AC,WA)
N, M = map(int, input().split(' ')) ac_set = set() wa_cnt, wa_cnt_ls = 0, [ 0 for i in range(N) ] for i in range(M): p, s = input().split(' ') idx = int(p) - 1 if not idx in ac_set: if s == 'AC': ac_set.add(idx) wa_cnt += wa_cnt_ls[idx] else: wa_cnt_ls[idx] += 1 print(len(ac_set), wa_cnt)
1
93,557,346,599,052
null
240
240
n, k = map(int, input().split()) if n == k: print(0) elif n < k: print(min(n, k - n)) else: n = n % k print(min(n, k - n))
#C N, K = map(int,input().split()) if N%K==0: print(0) else: print(min(N%K,-(N%(K)-K)))
1
39,403,220,293,540
null
180
180
from collections import deque n, q = map(int, input().split()) queue = deque() for i in range(n): name, time = input().split() queue.append({"name": name, "time": int(time)}) t = 0 while len(queue) != 0: #print([queue[i]["time"] for i in range(len(queue))]) head = queue.popleft() if head["time"] <= q: t += head["time"] name = head["name"] print("{} {}".format(name, t)) else: queue.append({"name": head["name"], "time": head["time"] - q}) t += q
pnum,mtime = map(int,input().split(" ")) total = [list(input().split(" ")) for _ in range(pnum)] cnt=0 tcnt=0 while len(total) > 0: ztime = int(total[0][1]) - mtime if ztime <= 0: tcnt += int(total[0][1]) print(total[0][0],int(tcnt)) total.pop(0) else: total.append([total[0][0],ztime]) total.pop(0) tcnt += mtime cnt += 1
1
41,744,895,260
null
19
19
S = input() K = int(input()) ss = [] seq = 1 for a,b in zip(S,S[1:]): if a==b: seq += 1 else: ss.append(seq) seq = 1 ss.append(seq) if len(ss)==1: print(len(S)*K//2) exit() if S[0] != S[-1]: ans = sum([v//2 for v in ss]) * K print(ans) else: ans = sum([v//2 for v in ss[1:-1]]) * K ans += (ss[0]+ss[-1])//2 * (K-1) ans += ss[0]//2 + ss[-1]//2 print(ans)
import sys s = sys.stdin.readline().rstrip() k = int(sys.stdin.readline()) cnt = 0 i = 0 l = len(s) if len(set(s)) == 1: print(int(l*k/2)) exit() while i < l-1: if s[i] == s[i+1]: cnt += 1 i += 2 continue i += 1 cnt1 = 0 i1 = 0 s *= 2 while i1 < l*2-1: if s[i1] == s[i1+1]: cnt1 += 1 i1 += 2 continue i1 += 1 print((cnt1 - cnt)*(k-1) + cnt)
1
175,726,347,423,682
null
296
296
a, b, k = map(int, input().split()) n = min(a,k) k -= n print(a-n, max(0,b-k))
import sys from collections import defaultdict, deque, Counter import math # import copy from bisect import bisect_left, bisect_right import heapq # sys.setrecursionlimit(1000000) # input aliases input = sys.stdin.readline getS = lambda: input().strip() getN = lambda: int(input()) getList = lambda: list(map(int, input().split())) getZList = lambda: [int(x) - 1 for x in input().split()] INF = 10 ** 20 MOD = 10**9 + 7 divide = lambda x: pow(x, MOD-2, MOD) def nck(n, k, kaijyo): return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD def npk(n, k, kaijyo): if k == 0 or k == n: return n % MOD return (kaijyo[n] * divide(kaijyo[n-k])) % MOD def kaijyo(n): ret = [1] for i in range(1, n + 1): ret.append((ret[-1] * i)% MOD) return ret def cut(pt, grid, k, h, w): pat = [] for i in range(h-1): if pt % 2 == 1: pat.append("1") else: pat.append("0") pt //= 2 # print("pat", pat) slice = Counter(pat)["1"] pat = pat + ["0"] white = [0] * (slice + 1) ret = 0 for i in range(w): tw = [0] * (slice + 1) dan = 0 for j in range(h): if grid[j][i] == "1": tw[dan] += 1 if pat[j] == "1": dan += 1 if max(tw) > k: return INF reset = False for w1, w2 in zip(white, tw): if w1 + w2 > k: ret += 1 reset = True break if reset: white = tw else: for up in range(slice + 1): white[up] += tw[up] return ret + slice def solve(): h, w, k = getList() grid = [] for i in range(h): grid.append(getS()) ans = INF for i in range(2 ** (h-1)): tmp = cut(i, grid, k, h, w) # print(tmp) ans = min(ans, tmp) print(ans) def main(): n = getN() for _ in range(n): solve() if __name__ == "__main__": # main() solve()
0
null
76,428,386,387,222
249
193
a,b,n = list(map(int,input().split())) if n < b : x = n else : x = b-1 print(a*x//b-a*(x//b))
import math A, B, N = map(int,input().split(' ')) def f(x): return math.floor(A*x/B)-A*math.floor(x/B) def main(): if N >= B: print(f(B-1)) else: print(f(N)) if __name__ == "__main__": main()
1
28,360,791,595,652
null
161
161
import sys readline = sys.stdin.readline N = int(readline()) S = [readline().rstrip() for _ in range(N)] def count_cb_ob(s): st = [] for i, si in enumerate(s): if si == '(' or len(st) == 0 or st[-1] != '(': st.append(si) else: st.pop() return st.count(')'), st.count('(') cb_obs = map(count_cb_ob, S) f, b, s = [], [], [] for down, up in cb_obs: (f if down < up else (s if down == up else b)).append((down, up)) f = sorted(f) b = sorted(b, key=lambda x:x[1], reverse=True) count = 0 ans = 'Yes' for down, up in (*f, *s, *b): count -= down if count < 0: ans = 'No' count += up if count != 0: ans = 'No' print(ans)
N = int(input()) pl = [] mi = [] for i in range(N): _m = 0 _t = 0 s = input() for c in s: if c == ")": _t -= 1 _m = min(_m, _t) else: _t += 1 if _t > 0: pl.append([-_m, -_t]) # sortの調整のために負にしている else: mi.append([- _t + _m, _m, _t]) pl = sorted(pl) mi = sorted(mi) #import IPython;IPython.embed() if len(pl) == 0: if mi[0][0] == 0: print("Yes") else: print("No") exit() if len(mi) == 0: print("No") exit() tot = 0 for m, nt in pl: if tot - m < 0: # これはひくではないのでは? print("No") exit() else: tot -= nt for d, m, nt in mi: if tot + m <0: print("No") exit() else: tot += nt if tot != 0: print("No") else: print("Yes") # どういう時できるか # で並べ方 # tot, minの情報が必要 # totが+のものはminとtotを気をつける必要がある # minがsum以上であればどんどん足していけばいい. # なのでminが大きい順にsortし,totの大きい順にsort # その次はtotがminusの中ではminが小さい順に加える # 合計が0になる時Yes,それ意外No.
1
23,771,841,757,742
null
152
152
inputted = list(map(int, input().split())) K = inputted[0] X = inputted[1] total = 500 * K; answer = 'Yes' if total >= X else 'No'; print(answer);
def bubble_sort(card_list): cards = [i for i in card_list] list_length = len(cards) for i in range(list_length): for j in range(list_length-1,i,-1): if int(cards[j][1:]) < int(cards[j-1][1:]): cards[j],cards[j-1] = cards[j-1],cards[j] return cards def selection_sort(card_list): cards = [i for i in card_list] list_length = len(cards) for i in range(list_length): min_element = i for j in range(i+1,list_length): if int(cards[j][1:]) < int(cards[min_element][1:]): min_element = j cards[i],cards[min_element] = cards[min_element],cards[i] return cards n = int(input()) cards = input().split() bubble_cards = bubble_sort(cards) selection_cards = selection_sort(cards) print(' '.join(bubble_cards)) print('Stable') print(' '.join(selection_cards)) if bubble_cards == selection_cards: print('Stable') else: print('Not stable')
0
null
49,039,408,596,370
244
16
import math from numba import jit k=int(input()) @jit def f(): ans=0 for a in range(1,k+1): for b in range(1,k+1): for c in range(1,k+1): g=math.gcd(math.gcd(a,b),c) ans+=g return ans print(f())
#!usr/bin/env pypy3 from collections import defaultdict, deque from heapq import heappush, heappop from itertools import permutations, accumulate import sys import math import bisect def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) def main(): K = I() ans = 0 for i in range(1, K+1): for j in range(1, K+1): for k in range(1, K+1): ans += math.gcd(math.gcd(i, j), k) print(ans) main()
1
35,306,422,322,368
null
174
174
X,K,D = map(int,input().split()) A = abs(X) ans = 0 if A >= D*K: print( A - (D * K)) else : e = A//D K -= e A %= D if K%2 == 0: print(A) else: print(D-A)
*abc, k = map(int, input().split()) n = [0] * 3 for i, v in enumerate(abc): if k >= v: n[i] = v k -= v else: n[i] = k break print(n[0] + n[2] * -1)
0
null
13,582,507,470,788
92
148
# 7 from collections import Counter import collections import sys def input(): return sys.stdin.readline().strip() def I(): return int(input()) def LI(): return list(map(int, input().split())) def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def S(): return input() def LS(): return input().split() INF = float('inf') n = I() d = LI() mod = 998244353 if d[0] != 0: print(0) exit(0) # 頂点 i に隣接する頂点のうち、頂点 1 に近い頂点を pi とすると # d_pi = d_i - 1 となる # 各 i (i >= 2) について上の条件を満たす pi の個数の総乗を求める c = Counter(d) counts = [0] * (max(d)+1) for k, v in c.items(): counts[k] = v if counts[0] > 1: print(0) exit(0) ans = 1 for i in range(1, len(counts)): ans *= pow(counts[i-1], counts[i]) ans %= mod print(ans)
#!/usr/bin/env python # -*- coding: utf-8 -*- # # FileName: C_fix_2 # CreatedDate: 2020-08-08 01:59:29 +0900 # LastModified: 2020-08-08 02:11:36 +0900 # import os import sys # import numpy as np # import pandas as pd def main(): n, m = map(int, input().split()) h = [0] + list(map(int, input().split())) path = [[] for _ in range(n+1)] for _ in range(m): a, b = map(int, input().split()) path[a].append(h[b]) path[b].append(h[a]) ans = 0 for index in range(1, n+1): if path[index] and h[index] > max(path[index]): ans += 1 elif not path[index]: ans += 1 print(ans) if __name__ == "__main__": main()
0
null
89,496,199,762,112
284
155
import sys, re from collections import deque, defaultdict, Counter from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement from operator import itemgetter, mul from copy import deepcopy from string import ascii_lowercase, ascii_uppercase, digits from bisect import bisect, bisect_left from heapq import heappush, heappop from functools import reduce def input(): return sys.stdin.readline().strip() def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(): return list(map(int, input().split())) def ZIP(n): return zip(*(MAP() for _ in range(n))) sys.setrecursionlimit(10 ** 9) INF = float('inf') mod = 10 ** 9 + 7 N = INT() xy = [LIST() for _ in range(N)] zw = [[x+y, x-y] for x, y in xy] z, w = zip(*zw) print(max(max(z)-min(z), max(w)-min(w)))
(a,b)=[int(x) for x in input().split()] (c,d)=[int(x) for x in input().split()] if d == 1: print(1) else: print(0)
0
null
63,576,969,858,048
80
264
# row = [int(x) for x in input().rstrip().split(" ")] # n = int(input().rstrip()) # s = input().rstrip() def make_divisors(n: int): lower_divisors , upper_divisors = [], [] i = 1 while i*i <= n: if n % i == 0: lower_divisors.append(i) if i != n // i: upper_divisors.append(n//i) i += 1 return lower_divisors, upper_divisors[::-1] def resolve(): import sys input = sys.stdin.readline n = int(input().rstrip()) lower_divisors, _ = make_divisors(n) print(min(lower_divisor + (n // lower_divisor) - 2 for lower_divisor in lower_divisors)) if __name__ == "__main__": resolve()
def solve(n): l = [] for i in range(int(n**(1/2))): if n % (i+1) == 0: a = i + 1 b = n // a l.append(a+b-2) return min(l) print(solve(int(input())))
1
161,820,520,593,752
null
288
288
while 1: n,x=map(int,input().split()) if n == 0 and x == 0: break print(len([1 for a in range(1,n+1) for b in range(a+1,n+1) for c in range(b+1,n+1) if a + b + c == x]))
import math while True: n = input() if n == 0: break s = map(int, raw_input().split()) sumS = 0.0 sum = 0.0 for i in s: sumS += i**2 sum += i v = sumS / n - (sum / n)**2 print math.sqrt(v)
0
null
740,325,801,098
58
31
# 解説 import sys pin = sys.stdin.readline pout = sys.stdout.write perr = sys.stderr.write N, M, K = map(int, pin().split()) A = list(map(int, pin().split())) B = list(map(int, pin().split())) a = [0] b = [0] for i in range(N): a.append(a[i] + A[i]) for i in range(M): b.append(b[i] + B[i]) ans = 0 for i in range(N + 1): #Aの合計がKを超えるまで比較する if a[i] > K: break # 超えていた場合に本の個数を減らす while b[M] > K - a[i]: M -= 1 ans = max(ans, M + i) print(ans)
n,m,k = map(int,input().split()) a = [0] + list(map(int,input().split())) b = [0] + list(map(int,input().split())) for i in range(1,n+1): a[i] += a[i-1] for i in range(1,m+1): b[i] += b[i-1] ans,ai,bi = 0,0,0 for i in range(n+1): if a[i]<=k: ai = i for j in range(m+1): if a[ai]+b[j]<=k: bi = j ans = ai+bi for i in range(ai): for j in range(bi,m+1): if a[ai-i-1]+b[j]>k: bi = j break else: ans = max(ans,ai-i-1+j) print(ans)
1
10,865,501,849,410
null
117
117
from fractions import gcd def lcm(p, q): return (p * q // int(gcd(p, q))) N, M = map(int, input().split()) A = list(map(int,input().split())) B = [] for i in range(N): b = 0 while A[i] % 2 == 0: A[i] >>= 1 b += 1 B.append(b) S = set(B) if len(S) != 1: print(0) else: P = 1 for i in range(N): P = lcm(P, A[i]) print((M // (2 ** (list(S)[0] - 1)) + P) // (2 * P))
import sys from math import gcd from functools import reduce import numpy as np read = sys.stdin.read readline = sys.stdin.buffer.readline sys.setrecursionlimit(10 ** 8) INF = float('inf') MOD = 10 ** 9 + 7 def lcm_base(a, b): return (a * b) // gcd(a, b) def lcm_list(numbers): return reduce(lcm_base, numbers, 1) def main(): n, m = map(int, readline().split()) a = np.array(input().split(), np.int64) a //= 2 b = np.copy(a) while True: c = b % 2 if c.sum() == 0: b //= 2 elif c.sum() == n: break else: print(0) sys.exit() d = lcm_list(a) if d > 10 ** 9: ans = 0 else: ans = (m // d) - (m // (d + d)) print(ans) if __name__ == '__main__': main()
1
101,696,978,764,220
null
247
247
N, K = map(int, input().split()) A = list(map(int, input().split())) MOD = 10 ** 9 + 7 X = sorted([a for a in A if a >= 0]) Y = sorted([a for a in A if a < 0], key=lambda x: abs(x)) ans = 1 if 2 * (min(K, len(Y)) // 2) + len(X) >= K: if K % 2 == 1: ans *= X.pop() K -= 1 XX = [(x1 * x2) for x1, x2 in zip(*[iter(X[::-1])] * 2)] YY = [(y1 * y2) for y1, y2 in zip(*[iter(Y[::-1])] * 2)] ZZ = sorted(XX + YY) for i in range(K // 2): ans *= ZZ.pop() ans %= MOD else: Z = sorted(X + Y, key=lambda x: abs(x), reverse=True) for i in range(K): ans *= Z.pop() ans %= MOD print(ans)
DE = 10**9 + 7 N, K = list(map(lambda x: int(x), input().split(' '))) A = list(map(lambda x: int(x), input().split(' '))) def main(): A_posi = [] A_nega = [] for a in A: if a > 0: A_posi.append(a) elif a < 0: A_nega.append(-a) len_posi = len(A_posi) len_nega = len(A_nega) if len_posi + len_nega < K: return 0 if (len_nega % 2 == 1 and K == len_posi + len_nega) or (K % 2 == 1 and len_posi == 0): if len_posi + len_nega == N: A_posi.sort() A_nega.sort() answer = 1 k = 0 for a in A_nega: answer *= (- a) % DE answer %= DE k += 1 if k >= K: break else: for a in A_posi: answer *= a % DE answer %= DE k += 1 if k >= K: break return answer else: return 0 A_posi.sort(reverse=True) A_nega.sort(reverse=True) posi = 0 nega = 0 answer = 1 if K % 2 == 1: answer = A_posi[0] % DE posi = 1 while posi + nega + 2 <= K: p = A_posi[posi] * A_posi[posi + 1] if posi + 1 < len_posi else 0 n = A_nega[nega] * A_nega[nega + 1] if nega + 1 < len_nega else 0 if p > n: answer *= p % DE answer %= DE posi += 2 else: answer *= n % DE answer %= DE nega += 2 return answer print(main())
1
9,495,142,818,532
null
112
112
import sys sys.setrecursionlimit(10**7) input = sys.stdin.readline s = input().rstrip() n = len(s) t = s[0:(n-1)//2] q = s[(n+2)//2:n] if s == s[::-1] and t == t[::-1] and q == q[::-1]: print("Yes") else: print("No")
import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10 ** 9) INF = 10 ** 19 MOD = 10 ** 9 + 7 EPS = 10 ** -10 D = INT() C = LIST() S = [[]] * D for i in range(D): S[i] = LIST() M = 26 def check(i, score, t, last): score += S[i][t] for j in range(M): last[j] += 1 last[t] = 0 for j in range(M): score -= C[j] * last[j] return score last = [0] * M ans = [] score = 0 for i in range(D): mx = -INF idx = -1 for t in range(M): res = check(i, score, t, last[:]) if res > mx: mx = res idx = t t = idx score += S[i][t] for j in range(M): last[j] += 1 last[t] = 0 for j in range(M): score -= C[j] * last[j] ans.append(t+1) for t in ans: print(t)
0
null
27,924,928,943,440
190
113
from itertools import permutations as perm N = int(input()) P = tuple(map(int, input().split())) Q = tuple(map(int, input().split())) comb = list(perm(range(1, N+1))) def find(x): for i, n in enumerate(comb): if x == n: return i a = find(P) b = find(Q) ans = abs(a - b) print(ans)
from collections import deque n, u, v = map(int, input().split()) edge = [[] for _ in range(n)] for _ in range(n-1): a, b = map(int, input().split()) edge[a-1].append(b-1) edge[b-1].append(a-1) d_1 = deque() d_1.append(u-1) dist_1 = [-1] * n dist_1[u-1] = 0 while d_1: node = d_1.popleft() for next_node in edge[node]: if dist_1[next_node] < 0: dist_1[next_node] = dist_1[node] + 1 d_1.append(next_node) d_2 = deque() d_2.append(v-1) dist_2 = [-1] * n dist_2[v-1] = 0 while d_2: node = d_2.popleft() for next_node in edge[node]: if dist_2[next_node] < 0: dist_2[next_node] = dist_2[node] + 1 d_2.append(next_node) res = 0 for i in range(n): if dist_1[i] < dist_2[i]: res = max(res, dist_2[i]-1) print(res)
0
null
108,565,781,863,712
246
259
n = int(input()) lis = list(map(int, input().split())) ans = 0 for i in range(0, n-1): for t in range(i+1, n): con = lis[i] * lis[t] ans += con print(ans)
#!/usr/bin/env python # -*- coding: utf-8 -*- """ input: 6 1 2 2 3 2 2 3 4 3 1 5 4 1 6 5 1 6 6 0 output: 1 1 12 2 2 11 3 3 8 4 9 10 5 4 7 6 5 6 """ import sys UNVISITED, VISITED_IN_STACK, POPPED_OUT = 0, 1, 2 def dfs(v_init): global timer # init the first node of overall graph iterations(times >= 1) stack = list() stack.append(v_init) d_time[v_init] += timer # init end while stack: current = stack[-1] v_table = adj_table[current] visited[current] = VISITED_IN_STACK # if adj is None, current's adj(s) have been all visited adj = v_table.pop() if v_table else None if adj: if visited[adj] is UNVISITED: visited[adj] = VISITED_IN_STACK timer += 1 d_time[adj] += timer stack.append(adj) else: stack.pop() visited[current] = POPPED_OUT timer += 1 f_time[current] += timer return None def dfs_init(): global timer for v in range(v_num): if visited[v + 1] == UNVISITED: dfs(v + 1) timer += 1 if __name__ == '__main__': _input = sys.stdin.readlines() v_num = int(_input[0]) vertices = map(lambda x: x.split(), _input[1:]) # config length = (v_num + 1) # stack = [] visited = [UNVISITED] * (v_num + 1) d_time, f_time = ([0] * (v_num + 1) for _ in range(2)) adj_table = tuple([] for _ in range(v_num + 1)) for v_info in vertices: v_index, adj_num, *adj_list = map(int, v_info) # assert len(adj_list) == adj_num adj_table[v_index].extend(sorted(adj_list, reverse=True)) # timing start from 1 timer = 1 dfs_init() for index, time_info in enumerate(zip(d_time[1:], f_time[1:]), 1): print(index, *time_info)
0
null
84,138,326,783,196
292
8
def ok(p,w,k): track= 1 weight= 0 flag= True for i in w: if i > p: flag=False break elif weight+ i> p: weight= i track+= 1 else: weight+= i if track<=k and flag: return True else: return False n,k= map(int, input().split()) w=[] for i in range(n): w.append(int(input())) l=0 h=10**11 while l+1 < h: p= (l+h)//2 if ok(p,w,k): h= p else: l= p print(h)
def calc(s, i): global n global anslst global nlst if i == n: anslst[s] = 'yes' return calc(s, i+1) calc(s + nlst[i], i+1) anslst = ['no']*2001 n = int(input()) nlst = list(map(int, input().split())) q = int(input()) qlst = list(map(int, input().split())) calc(0, 0) for i in qlst: print(anslst[i])
0
null
93,312,007,064
24
25
import os, sys, re, math import itertools N = int(input()) P = tuple([int(n) for n in input().split()]) Q = tuple([int(n) for n in input().split()]) nums = [n for n in range(1, N + 1)] patterns = itertools.permutations(nums, N) i = 0 for pattern in patterns: if pattern == P: pi = i if pattern == Q: qi = i i += 1 print(abs(pi - qi))
def main(): k, n = map(int, input().split(" ")) a = list(map(int, input().split(" "))) d =[min(a)+k-max(a)] for i in range(n-1): d.append(a[i+1]-a[i]) print(k-max(d)) if __name__ == "__main__": main()
0
null
71,716,822,560,764
246
186
n = int(input()) lst = list(map(int,input().split())) c = 0 for i in range(n): if (lst[i]%2 == 0): if (lst[i]%3 != 0 and lst[i]%5 != 0): c = 1 if (c == 0): print("APPROVED") else: print("DENIED")
ans=0 n=int(input()) li=list(map(int,input().split())) for i in range(len(li)): for j in range(len(li)): if i==j: break ans+=li[i]*li[j] print(ans)
0
null
118,276,778,971,780
217
292
import sys, bisect, math, itertools, string, queue, copy import numpy as np import scipy from collections import Counter,defaultdict,deque from itertools import permutations, combinations from heapq import heappop, heappush # input = sys.stdin.readline sys.setrecursionlimit(10**8) mod = 10**9+7 def inp(): return int(input()) def inpm(): return map(int,input().split()) def inpl(): return list(map(int, input().split())) def inpls(): return list(input().split()) def inplm(n): return list(int(input()) for _ in range(n)) def inplL(n): return [list(input()) for _ in range(n)] def inplT(n): return [tuple(input()) for _ in range(n)] def inpll(n): return [list(map(int, input().split())) for _ in range(n)] def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) n,m = inpm() P = [] S = [] for i in range(m): tmp = input().split() P.append(int(tmp[0])) S.append(tmp[1]) AC = [0]*100001 WA = [0]*100001 for i in range(m): if S[i] == 'AC': AC[P[i]] = 1 elif S[i] == 'WA' and AC[P[i]] != 1: WA[P[i]] += 1 cnt_ac = 0 cnt_wa = 0 for i in range(1,n+1): if AC[i] == 1: cnt_ac += 1 cnt_wa += WA[i] print(cnt_ac,cnt_wa)
a,b,c=map(int,input().split()) d={"a":a,"b":b,"c":c} for i in range(int(input())+1): if d["a"]<d["b"]<d["c"]: print("Yes") exit() elif d["a"]>=d["c"]: d["c"]*=2 elif d["a"]>=d["b"]: d["b"]*=2 elif d["b"]>=d["c"]: d["c"]*=2 print("No")
0
null
50,144,025,079,680
240
101
n = int(input()) a = list(map(int,input().split())) money = 1000 kabu = 0 if a[0] < a[1]: flg = 'kau' elif a[0] > a[1]: flg = 'uru' else: flg = 'f' for i in range(1,n): if a[i-1] < a[i]: if flg != 'uru': kabu = money // a[i-1] money -= kabu * a[i-1] flg = 'uru' elif a[i-1] > a[i]: if flg != 'kau': money += kabu * a[i-1] kabu = 0 flg = 'kau' else: pass if kabu > 0: money += kabu * a[-1] print(money)
N, M, K = map(int, input().split()) mod = 998244353 inv = [0,1] for i in range(2, N): inv.append((-inv[mod%i]*(mod//i))%mod) if N == 1: print(M) exit() m = [1] s = 1 for _ in range(N-1): s = s*(M-1)%mod m.append(s) ncombi = [1] c = 1 for k in range(K): c = c*(N-1-k)*inv[k+1] c %= mod ncombi.append(c) ans = 0 for k in range(K+1): ans = ans + m[-k -1]*ncombi[k] ans %= mod ans = ans*M%mod print(ans)
0
null
15,270,729,943,872
103
151
A,B,M=map(int,input().split()) *a,=map(int,input().split()) *b,=map(int,input().split()) xyc = [tuple(map(int,input().split())) for _ in range(M)] ans = min(a)+min(b) for i in range(M): x,y,c=xyc[i] ans = min(ans,a[x-1]+b[y-1]-c) print(ans)
a, b, m = map(int, input().split()) a_price = list(map(int, input().split())) b_price = list(map(int, input().split())) coupon = [] for _ in range(m): coupon.append(list(map(int, input().split()))) a_copy = a_price.copy() a_copy.sort() b_copy = b_price.copy() b_copy.sort() a_min = a_copy[0] b_min = b_copy[0] min = a_min + b_min totals = [min] for list in coupon: tmp = a_price[list[0] - 1] + b_price[list[1] - 1] - list[2] totals.append(tmp) totals.sort() ans = totals[0] print(ans)
1
53,897,577,454,994
null
200
200
# coding: utf-8 cnt = int() m = int() g = [] def insersion_sort(a, n, g): global cnt for i in xrange(g, n): v = a[i] j = i - g while j >= 0 and a[j] > v: a[j+g] = a[j] j = j - g cnt += 1 a[j+g] = v return list(a) def shell_sort(a, n): global cnt global m global g cnt = 0 m = 0 g = [] nn = n while True: nn /= 2 if nn <= 0: break g.append(nn) m += 1 if n == 1: m = 1 g = [1] for i in g: a = insersion_sort(a, n, i) return a def main(): global cnt global m global g n = input() a = [] for i in xrange(n): a.append(input()) a = shell_sort(a, n) print m print " ".join(map(str, g)) print cnt for i in xrange(n): print a[i] main()
#coding: UTF-8 buf = input() word = input() buf_buf = buf + buf if word in buf_buf: print("Yes") else: print("No")
0
null
883,739,858,792
17
64
def nextPrime(x): for i in range(x, x*2): a = 0 for w in range(2, i+1): if i%w ==0: break else: a = w if i == a + 1: return i if x != 2 else 2 def main(): x = int(input()) print(nextPrime(x)) if __name__ == '__main__': main()
n, r = map(int,input().split()) if n < 10: print(int(r+100*(10-n))) elif n >= 10: print(r)
0
null
84,524,171,235,570
250
211
a, b, c = map(int, input().split()) k = int(input()) while a >= b and k > 0: b *= 2 k -= 1 while b >= c and k > 0: c *= 2 k -= 1 print('Yes' if a < b < c else 'No')
A,B,C = map(int, input().split()) K = int(input()) n = 0 while (A >= B): B = 2*B n +=1 while B>=C: C = 2*C n += 1 #print(A,B,C,n) if n <= K: print('Yes') else: print('No')
1
6,941,503,189,060
null
101
101
import bisect,collections,copy,heapq,itertools,math,numpy,string import sys sys.setrecursionlimit(10**7) def S(): return sys.stdin.readline().rstrip() def I(): return int(sys.stdin.readline().rstrip()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LS(): return list(sys.stdin.readline().rstrip().split()) def main(): a,b = LI() a_b = str(a)*b b_a = str(b)*a print(sorted([a_b,b_a])[0]) main()
a,b = map(int,input().split()) n = min(a,b) m = max(a,b) ans = '' for i in range(m): ans = ans + str(n) print(ans)
1
84,801,715,545,582
null
232
232
def inverse_mod(a, mod=10**9+7): """ Calculate inverse of the integer a modulo mod. """ return pow(a, mod-2, mod) def combination_mod(n, r, mod=10**9+7): """ Calculate nCr modulo mod. """ r = min(r, n-r) numerator = denominator = 1 for i in range(r): numerator = numerator * (n - i) % mod denominator = denominator * (i + 1) % mod return numerator * inverse_mod(denominator, mod) % mod def create_inverses_table(n, mod=10**9+7): """ Create table for inverses of the integers 0 to n modulo mod. """ inv_table = [0] + [1] * n for x in range(2, n+1): inv_table[x] = -(mod//x) * inv_table[mod % x] % mod return inv_table def solve(): n_blocks, n_colors, n_max_pairs = map(int, input().split()) if n_colors == 1: return int(n_blocks - n_max_pairs == 1) mod = 998244353 inverses = create_inverses_table(max(n_colors, n_max_pairs), mod) e1 = n_colors * pow(n_colors-1, n_blocks-1, mod) % mod e2 = 1 total = e1 * e2 for k in range(1, n_max_pairs+1): e1 = e1 * inverses[n_colors - 1] % mod e2 = e2 * (n_blocks - k) * inverses[k] % mod total += e1 * e2 total %= mod return total def main(): print(solve()) if __name__ == "__main__": main()
import sys # import math # import bisect # import numpy as np # from decimal import Decimal # from numba import njit, i8, u1, b1 #JIT compiler # from itertools import combinations, product # from collections import Counter, deque, defaultdict # sys.setrecursionlimit(10 ** 6) MOD = 10 ** 9 + 7 INF = 10 ** 9 PI = 3.14159265358979323846 def read_str(): return sys.stdin.readline().strip() def read_int(): return int(sys.stdin.readline().strip()) def read_ints(): return map(int, sys.stdin.readline().strip().split()) def read_ints2(x): return map(lambda num: int(num) - x, sys.stdin.readline().strip().split()) def read_str_list(): return list(sys.stdin.readline().strip().split()) def read_int_list(): return list(map(int, sys.stdin.readline().strip().split())) def GCD(a: int, b: int) -> int: return b if a%b==0 else GCD(b, a%b) def LCM(a: int, b: int) -> int: return (a * b) // GCD(a, b) class Combination: def __init__(self, n, p): self.n = n self.p = p self.note = [1, 1] self.noteinv = [1, 1] self.inv = [0, 1] self.calc() def calc(self): for i in range(2, self.n + 1): self.note.append((self.note[-1] * i) % self.p) self.inv.append((-self.inv[self.p % i] * (self.p // i)) % self.p) self.noteinv.append((self.noteinv[-1] * self.inv[-1]) % self.p) def nCr(self, n, r): n = self.n if r < 0 or n < r: return 0 r = min(r, n - r) return (self.note[n] * self.noteinv[r] * self.noteinv[n - r]) % self.p def Main(): n, m, k = read_ints() # m * _{n-1}C_k * 1^k * (m-1)^{n-1-k} p = 998244353 modm = [1] for _ in range(1, n): modm.append((modm[-1] * (m - 1)) % p) cmb = Combination(n - 1, p) ans = 0 for i in range(k + 1): ans += (cmb.nCr(n - 1, i) * m * modm[n - 1 - i]) % p ans %= p print(ans) if __name__ == '__main__': Main()
1
23,041,981,322,388
null
151
151
def binary_search(n, k, lw): left = 0 right = max(lw) * n while left < right: mid = (left + right)//2 if is_underweight(n, k, lw, p=mid): right = mid else: left = mid + 1 return left def is_underweight(n, k, lw, p): i = 0 w = 0 ck = 1 while i < n: if lw[i] > p: return False rest = p - w if lw[i] <= rest: w += lw[i] i += 1 else: w = 0 ck += 1 return ck <= k def main(): n, k = map(int, input().split()) lw = [] for _ in range(n): lw.append(int(input())) p = binary_search(n, k, lw) print(p) 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,818,365,772
null
24
24
#!/usr/bin/env python3 n = int(input()) matrix = [] while n > 0: values = [int(x) for x in input().split()] matrix.append(values) n -= 1 official_house = [[[0 for z in range(10)] for y in range(3)] for x in range(4)] Min = 0 Max = 9 for b, f, r, v in matrix: num = official_house[b - 1][f - 1][r - 1] if Min >= num or Max >= num: official_house[b - 1][f - 1][r - 1] += v for i in range(4): for j in range(3): print('',' '.join(str(x) for x in official_house[i][j])) if 3 > i: print('#' * 20)
import sys n = int(raw_input()) building_a = [[0 for j in range(11)] for i in range(4)] building_b = [[0 for j in range(11)] for i in range(4)] building_c = [[0 for j in range(11)] for i in range(4)] building_d = [[0 for j in range(11)] for i in range(4)] for i in range(n): b, f, r, v = map(int, raw_input().split()) if b == 1: building_a[f][r] += v elif b == 2: building_b[f][r] += v elif b == 3: building_c[f][r] += v elif b == 4: building_d[f][r] += v for i in range(1, 4): print "", for j in range(1, 11): print building_a[i][j], print for i in range(20): sys.stdout.write("#") print for i in range(1, 4): print "", for j in range(1, 11): print building_b[i][j], print for i in range(20): sys.stdout.write ("#") print for i in range(1, 4): print "", for j in range(1, 11): print building_c[i][j], print for i in range(20): sys.stdout.write("#") print for i in range(1, 4): print "", for j in range(1, 11): print building_d[i][j], print
1
1,110,915,705,120
null
55
55
N = int(input()) L = list(map(int, input().split())) L.sort() cnt = 0 for i in range(N-2): for j in range(i, N-1): for k in range(j, N): a, b, c = L[i], L[j], L[k] if a == b or b == c or c == a: continue elif a+b>c and b+c>a and c+a>b: cnt+=1 print(cnt)
import math n = int(input()) s = 0 for i in range(1,n+1): for j in range(1,n+1): for k in range(1,n+1): s += math.gcd(math.gcd(i,j),k) print(s)
0
null
20,147,692,996,768
91
174
from collections import Counter from math import factorial N = int(input()) A = list(map(int, input().split())) Acount = Counter(A) Chose = {} total = 0 for k, v in Acount.items(): if Acount[k] >= 2: x = int(v*(v-1)/2) Chose[k] = x else: x = 0 Chose[k] = x total = total+x for i in range(N): ans=total-Chose[A[i]] if Acount[A[i]]-1>=2: ans=ans+(Acount[A[i]]-1)*(Acount[A[i]]-2)/2 print(int(ans))
n = int(input()) a = list(map(int, input().split())) d = {} for i in a: if i in d: d[i] += 1 else: d[i] = 1 ans = 0 for i in d.values(): ans += i*(i-1)//2 for i in a: print(ans - (d[i]-1))
1
47,460,433,376,746
null
192
192
import numpy as np import itertools N, M, Q = map(int, input().split()) G = [] for i in range(Q): a, b, c, d = map(int, input().split()) G.append([a, b, c, d]) ans = 0 A = np.array(list(itertools.combinations_with_replacement(range(1, M + 1), N))) n = len(A) score = np.zeros(n, np.int32) for a, b, c, d in G: cond = A[:, b - 1] - A[:, a - 1] == c score += d * cond print(score.max())
# -*- coding: utf-8 -*- from itertools import combinations_with_replacement def main(): n, m, q = map(int, input().split()) xs = [] for i in range(q): xs.append(list(map(int, input().split()))) max = 0 for suretu in combinations_with_replacement(range(1, m + 1), n): wa = 0 for x in xs: a, b, c, d = x if suretu[b - 1] - suretu[a - 1] == c: wa += d if wa > max: max = wa print(max) if __name__ == "__main__": main()
1
27,651,480,529,018
null
160
160
from decimal import Decimal, getcontext a,b,c = input().split() getcontext().prec = 65 a = Decimal(a).sqrt() b = Decimal(b).sqrt() c = Decimal(c).sqrt() if a+b < c: print("Yes") else: print("No")
A, B, M = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) xyc = [map(int, input().split()) for _ in range(M)] x, y, c = [list(x) for x in zip(*xyc)] m = min(a)+min(b) for i in range(M): tmp = a[x[i]-1]+b[y[i]-1]-c[i] m = min(m, tmp) print(m)
0
null
52,871,364,885,252
197
200