code1
stringlengths
16
24.5k
code2
stringlengths
16
24.5k
similar
int64
0
1
pair_id
int64
2
181,637B
question_pair_id
float64
3.71M
180,628B
code1_group
int64
1
299
code2_group
int64
1
299
import itertools,math n = int(input()) points = [list(map(int,input().split())) for _ in range(n)] roots = list(itertools.permutations(points)) ans = 0 for root in roots: for i in range(n-1): ans += math.sqrt(pow(root[i+1][0] - root[i][0],2) + pow(root[i+1][1] - root[i][1],2)) ans /= len(roots) print(ans)
import math import itertools def calc(x1y1, x2y2): x1, y1 = x1y1 x2, y2 = x2y2 return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2) N = int(input()) addresses = [] for _ in range(N): addresses.append(list(map(int, input().split()))) ways = list(itertools.permutations(list(range(N)))) ans = 0 for x in ways: dist = 0 for i in range(N - 1): dist += calc(addresses[x[i]], addresses[x[i + 1]]) ans += dist print(ans / len(ways))
1
148,419,595,925,078
null
280
280
# -*- coding: utf-8 -*- import sys # sys.setrecursionlimit(10**6) # buff_readline = sys.stdin.buffer.readline buff_readline = sys.stdin.readline readline = sys.stdin.readline INF = 2**62-1 def read_int(): return int(buff_readline()) def read_int_n(): return list(map(int, buff_readline().split())) def read_float(): return float(buff_readline()) def read_float_n(): return list(map(float, buff_readline().split())) def read_str(): return readline().strip() def read_str_n(): return readline().strip().split() def error_print(*args): print(*args, file=sys.stderr) def mt(f): import time def wrap(*args, **kwargs): s = time.time() ret = f(*args, **kwargs) e = time.time() error_print(e - s, 'sec') return ret return wrap class Mod: def __init__(self, m): self.m = m def add(self, a, b): return (a + b) % self.m def sub(self, a, b): return (a - b) % self.m def mul(self, a, b): return ((a % self.m) * (b % self.m)) % self.m def div(self, a, b): return self.mul(a, pow(b, self.m-2, self.m)) def pow(self, a, b): return pow(a, b, self.m) class Combination: def __init__(self, n, mod): g1 = [1, 1] g2 = [1, 1] inverse = [0, 1] for i in range(2, n + 1): g1.append((g1[-1] * i) % mod) inverse.append((-inverse[mod % i] * (mod//i)) % mod) g2.append((g2[-1] * inverse[-1]) % mod) self.MOD = mod self.N = n self.g1 = g1 self.g2 = g2 self.inverse = inverse def __call__(self, n, r): if (r < 0 or r > n): return 0 r = min(r, n-r) return self.g1[n] * self.g2[r] * self.g2[n-r] % self.MOD @mt def slv(K, S): M = 10**9+7 C = Combination(2*(10**6), M) MOD = Mod(M) ans = 0 S = len(S) N = S + K for k in range(K+1): r = K -k a = MOD.pow(26, r) b = MOD.mul(MOD.pow(25, k), C(k+S-1, k)) ans = MOD.add(ans, MOD.mul(a, b)) return ans def main(): K = read_int() S = read_str() print(slv(K, S)) if __name__ == '__main__': main()
#!python3 import sys iim = lambda: map(int, sys.stdin.readline().rstrip().split()) def resolve(): K = int(input()) s = input() ls = len(s) - 1 mod = 10**9+7 N = ls + K N1 = N + 1 fact = [1] * N1 inv = [1] * N1 for i in range(2, N1): fact[i] = fact[i-1] * i % mod inv[N] = pow(fact[N], mod-2, mod) for i in range(N-1, 1, -1): inv[i] = inv[i+1]*(i+1) % mod def cmb(a, b): return fact[a] * inv[b] % mod * inv[a-b] % mod ans = 0 p25 = 1 p26 = pow(26, K, mod) p26inv = 576923081 #pow(26, mod-2, mod) for i in range(K+1): ans = (ans + cmb(ls+i, ls) * p25 % mod * p26 % mod) % mod p25 = p25 * 25 % mod p26 = p26 * p26inv % mod print(ans) if __name__ == "__main__": resolve()
1
12,843,907,629,700
null
124
124
N = int(input()) lst = list(map(int, input().split())) c = True for i in lst: if i%2 ==0: if i%3 != 0 and i%5 != 0: c = False break if c: print('APPROVED') else: print('DENIED')
N = int(input()) A_list = list(map(int, input().split())) for i in range(N): if A_list[i] % 2 == 0: if A_list[i] % 3 == 0 or A_list[i] % 5 == 0: continue else: print("DENIED") exit() if i == N-1: print("APPROVED")
1
68,862,761,027,060
null
217
217
f=input n,l,q=int(f()),list(f()),int(f()) B=[[0]*-~n for i in range(26)] def S(o,i): s=0 while i: s+=B[o][i]; i-=i&-i return s def A(o,i,x): while i<=n: B[o][i]+=x; i+=i&-i for i in range(n): A(ord(l[i])-97,i+1,1) for i in range(q): a,b,c=f().split(); b=int(b) if a>'1': print(sum(1 for o in range(26) if S(o,int(c))-S(o,b-1))) else: A(ord(l[b-1])-97,b,-1); l[b-1]=c; A(ord(c)-97,b,1)
A,B = map(int, input().split()) tmp_A, tmp_B = max(A,B), min(A,B) while 1: if tmp_A % tmp_B != 0: tmp = tmp_B tmp_B = tmp_A%tmp_B tmp_A = tmp else: if tmp_B == 1: result = A*B break else: result = int(A*B/tmp_B) break print(result)
0
null
87,945,981,575,100
210
256
a = 0 a += int(input()) a += int(input()) print(6 - a)
A = int(input()) B = int(input()) ans = 6-A-B print(ans)
1
110,769,735,871,500
null
254
254
t1, t2 = map(int, input().split()) a1, a2 = map(int, input().split()) b1, b2 = map(int, input().split()) if (a1 > b1 and a2 > b2) or (a1 < b1 and a2 < b2): print(0) else: if a1 * t1 + a2 * t2 == b1 * t1 + b2 * t2: print("infinity") else: d_all = (a1 * t1 + a2 * t2) - (b1 * t1 + b2 * t2) d_1 = a1 * t1 - b1 * t1 if (d_all > 0 and d_1 > 0) or (d_all < 0 and d_1 < 0): print(0) else: if abs(d_all) > abs(d_1): print(1) else: ans = 1 ans += abs(d_1) // abs(d_all) * 2 - (1 if d_1 % d_all == 0 else 0) print(ans)
import sys def input(): return sys.stdin.readline()[:-1] t=list(map(int,input().split())) a=list(map(int,input().split())) b=list(map(int,input().split())) at1=a[0]*t[0] at2=a[1]*t[1] bt1=b[0]*t[0] bt2=b[1]*t[1] if at1+at2==bt1+bt2: print("infinity") quit() if at1>bt1: if at1+at2>bt1+bt2: print(0) quit() sa1=at1-bt1 sa2=bt1+bt2-at1-at2 # print(sa1//sa2*2+1) else: if bt1+bt2>at1+at2: print(0) quit() sa1=bt1-at1 sa2=-(bt1+bt2-at1-at2) # print(sa1//sa2*2+1) if sa1%sa2==0: print(sa1//sa2*2) else: print(sa1//sa2*2+1)
1
132,050,023,323,090
null
269
269
import sys from sys import stdin input = stdin.readline n,k = (int(x) for x in input().split()) w = [int(input()) for i in range(n)] # 最大積載Pのトラックで何個の荷物を積めるか def check(P): 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 def solve(): left = 0 right = 100000 * 10000 #荷物の個数 * 1個当たりの最大重量 while right - left > 1 : mid = (right + left)//2 v = check(mid) if v >= n: right = mid else: left = mid return right print(solve())
# coding:utf-8 n, k = map(int,raw_input().split()) ws = [] for _ in xrange(n): ws.append(int(raw_input())) def possible_put_p(p): # P??\????????????????????????????????? current = 1 weight = 0 for w in ws: if weight + w <= p: weight += w else: current += 1 if current > k: return False weight = w if current <= k: return True else: return False low = max(ws) high = sum(ws) while(high - low > 0): mid = (low + high) / 2 if(possible_put_p(mid)): high = mid else: low = mid + 1 print high
1
86,681,582,528
null
24
24
from sys import stdin nii=lambda:map(int,stdin.readline().split()) lnii=lambda:list(map(int,stdin.readline().split())) n,m=nii() l=[lnii() for i in range(m)] class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def same(self, x, y): return self.find(x) == self.find(y) def size(self, x): return -self.parents[self.find(x)] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] uf=UnionFind(n) for a,b, in l: a-=1 b-=1 if uf.same(a,b)==False: uf.union(a,b) ans=0 roots=uf.roots() for i in uf.roots(): ans=max(ans,uf.size(i)) print(ans)
if __name__ == '__main__': W = input().lower() words = [] while True: line = input() [words.append(i) for i in line.lower().split()] if 'END_OF_TEXT' in line: break print(words.count(W))
0
null
2,925,425,434,230
84
65
N = int(input()) myans = [ 0 for n in range(N+1)] upper = int(N**(2/3)) #print(upper) for x in range(1, 105): for y in range(1, 105): for z in range(1, 105): v = x**2 + y**2 + z**2 + x*y + y*z + z*x if v < N+1: myans[v] += 1 #print(myans) for a,b in enumerate(myans): if a != 0: print(b)
a = str(input()) if a=="ABC": print("ARC") elif a=="ARC": print("ABC")
0
null
16,202,068,031,888
106
153
[n, m] = [int(x) for x in raw_input().split()] A = [] counter = 0 while counter < n: A.append([int(x) for x in raw_input().split()]) counter += 1 B = [int(raw_input()) for j in range(m)] counter = 0 while counter < n: result = 0 for j in range(m): result += A[counter][j] * B[j] print(result) counter += 1
n,m = map(int,input().split()) A = [] B = [] for i in range(n): tmp = list(map(int,input().split())) A.append(tmp) tmp=[] for i in range(m): num = int(input()) B.append(num) for i in range(n): c = 0 for j in range(m): c += A[i][j]*B[j] print(c)
1
1,163,547,378,848
null
56
56
import sys input = sys.stdin.readline rs = lambda: input().strip() ri = lambda: int(input()) rl = lambda: list(map(int, input().split())) mod = 10**9 + 7 S, T = rs().split() print(T+S)
a=input().split() print(a[1]+a[0])
1
102,808,619,072,448
null
248
248
H,W,K = map(int,input().split()) c = [] row = [0]*H col = [0]*W total = 0 for i in range(H): c.append(list(input())) for j in range(W): if c[i][j] == '#': row[i] += 1 col[j] += 1 total += 1 #print(total) ans = 0 for i in range(2**(H+W)): x = [0]*(H+W) ii = i for j in range(H+W): x[j] = ii % 2 ii//=2 #print(x) D = 0 for j in range(H): D += row[j]*x[j] for j in range(H,H+W): D += col[j-H]*x[j] for j in range(H): for k in range(W): if c[j][k] == '#': D -= x[j]*x[H+k] if total -D == K: ans += 1 #print(x,ans) print(ans)
import math n = int(input()) address = list(map(int, input().split())) min_hp = math.inf for i in range(1, 101): hp = sum(map(lambda x: (x - i) ** 2, address)) if hp < min_hp: min_hp = hp print(min_hp)
0
null
37,105,782,470,400
110
213
def main(): s = input() dp = [0, 1] for c in s: x = int(c) a = dp[0] + x if a > dp[1] + 10 - x: a = dp[1] + 10 - x b = dp[0] + x + 1 if b > dp[1] + 10 - x - 1: b = dp[1] + 10 - x - 1 dp[0] = a dp[1] = b dp[1] += 1 print(min(dp)) if __name__ == "__main__": main()
import sys input = sys.stdin.readline def main(): n,k = map(int, input().split()) A = list(map(int, input().split())) for i in range(k,n): if A[i] > A[i-k]: print('Yes') else: print('No') main()
0
null
39,042,891,008,402
219
102
def main(): N = int(input()) l = list(map(int, input().split())) ans = 0 m = 10**9 + 7 for i in range(60): x = 0 for j in l: x += 1 & j >> i tmp = x * (N - x) % m tmp *= 2 ** i % m ans += tmp ans %= m print(ans) if __name__ == '__main__': main()
#from statistics import median #import collections #aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0] from fractions import gcd from itertools import combinations,permutations,accumulate # (string,3) 3回 #from collections import deque from collections import deque,defaultdict,Counter import decimal import re #import bisect # # d = m - k[i] - k[j] # if kk[bisect.bisect_right(kk,d) - 1] == d: # # # # pythonで無理なときは、pypyでやると正解するかも!! # # import sys sys.setrecursionlimit(10000000) mod = 10**9 + 7 #mod = 9982443453 def readInts(): return list(map(int,input().split())) def I(): return int(input()) n = I() dp = [0] * (500000) for x in range(1,101): for y in range(1,101): for z in range(1,101): pe = x**2 + y**2 + z**2 + x*y + y*z + z*x dp[pe] += 1 print(*dp[1:n+1],sep='\n')
0
null
65,186,669,984,028
263
106
N, M, K = map(int, input().split()) MOD = 998244353 # 階乗 & 逆元計算 factorial = [1] inverse = [1] for i in range(1, N+2): factorial.append(factorial[-1] * i % MOD) inverse.append(pow(factorial[-1], MOD - 2, MOD)) # 組み合わせ計算 def nCr(n, r): if n < r or r < 0: return 0 elif r == 0: return 1 return factorial[n] * inverse[r] * inverse[n - r] % MOD ans = 0 for k in range(K + 1): ans += M * nCr(N - 1, k) * pow(M - 1, N - 1 - k, MOD) % MOD ans %= MOD print(ans % MOD)
import sys read = sys.stdin.read readline = sys.stdin.readline def cmb(n, r, mod): if r < 0 or r > n: return 0 r = min(r, n - r) return g1[n] * g2[r] * g2[n - r] % mod def make_table2(n, mod=10 ** 9 + 7): # 元テーブル g1 = [0] * (n + 1) g1[0] = 1 g1[1] = 1 tmp = 1 for i in range(2, n + 1): tmp = tmp * i % mod g1[i] = tmp # 逆元テーブル g2 = [0] * (n + 1) g2[-1] = pow(g1[-1], mod - 2, mod) tmp = g2[-1] for i in range(n - 1, -1, -1): tmp = tmp * (i + 1) % mod g2[i] = tmp return g1, g2 N, M, K = map(int, read().split()) mod = 998244353 g1, g2 = make_table2(N, mod) answer = 0 for i in range(K + 1): answer = (answer + cmb(N - 1, i, mod) * M * pow(M - 1, N - i - 1, mod)) % mod print(answer)
1
23,213,786,191,736
null
151
151
n,m = list(map(int, input("").split())) h = list(map(int, input("").split())) p = [1] * n for i in range(m): a,b = map(int, input().split()) if h[a-1] <= h[b-1]: p[a-1]=0 if h[a-1] >= h[b-1]: p[b-1]=0 print(sum(p))
from itertools import accumulate def main(): N, K = map(int, input().split()) A = list(map(int, input().split())) for _ in range(K): arr = [0]*(N+1) for i, a in enumerate(A): left = max(0, i-a) right = min(N, i+a+1) arr[left] += 1 arr[right] -= 1 A = list(accumulate(arr[:-1])) if all(a == N for a in A): break print(*A, sep=" ") if __name__ == "__main__": main()
0
null
20,162,674,956,050
155
132
import sys N = int(input()) D = [list(map(int, input().split())) for _ in range(N)] now = D[0] zorome = 0 if now[0] == now[1]: zorome += 1 else: zorome = 0 for i in range(1, N): now = D[i] if now[0] == now[1]: zorome += 1 if zorome == 3: print('Yes') sys.exit() else: zorome = 0 print('No')
N = int(input()) x = 0 for i in range(N): a, b = map(int, input().split()) if a == b: x += 1 else: x = 0 if x >= 3: print("Yes") break else: print("No")
1
2,495,707,456,928
null
72
72
# coding: utf-8 # Here your code ! S = int(input()) N = list(map(int,input().split())) count=0 def bubblesort(n,s): global count flag = 1 while flag: flag = 0 for i in range(s-1,0,-1): key = n[i] if n[i]<n[i-1]: n[i]=n[i-1] n[i-1]=key count+=1 flag=1 print(" ".join(list(map(str,n)))) print(count) bubblesort(N,S)
class PrintAChessboard: def output(self, H, W): s1 = "" s2 = "" for w in range(W): if w % 2 == 0: s1 += "#" s2 += "." else: s1 += "." s2 += "#" for h in range(H): if h % 2 == 0: print s1 else: print s2 print if __name__ == "__main__": pc = PrintAChessboard() while True: h, w = map(int, raw_input().split()) if h == 0 and w == 0: break pc.output(h, w)
0
null
449,481,492,322
14
51
n, m = map(int, input().split()) days = input().split() x = 0 for day in days: x = x + int(day) if x <= n: print(n - x) else: print(-1)
N,X = map(int,input().split()) Y = N - sum(map(int,input().split())) print(("-1",Y)[Y >= 0])
1
31,903,503,650,658
null
168
168
N = int(input()) for i in range(1, 10): if N % i == 0: j = N // i if 1 <= j and j <= 9: print("Yes") exit() print("No")
n=int(input()) ans = False for j in range(1,10): for k in range(1,10): if j*k == n: ans = True break if ans: break if ans: print("Yes") else: print("No")
1
159,447,195,228,502
null
287
287
n = int(input()) a = [int(i) for i in input().split()] def trace(a): a = map(str, a) print(' '.join(a)) def insertion(a): for i in range(1, n): v = a[i] j = i -1 while j >= 0 and a[j] > v: a[j+1] = a[j] j -= 1 a[j+1] = v trace(a) trace(a) insertion(a)
list_len = int(input().rstrip()) num_list = list(map(int, input().rstrip().split())) for i in range(list_len): v = num_list[i] j = i - 1 while j >= 0 and num_list[j] > v: num_list[j + 1] = num_list[j] j -= 1 num_list[j+1] = v output_str = ' '.join(map(str, num_list)) print(output_str)
1
6,110,747,204
null
10
10
N = int(input()) if N%2 == 1 : print(0) exit() n = 0 i = 0 for i in range(1,26) : n += N//((5**i)*2) print(n)
def solve(n,x): count = 0 for i in range(1,n+1): for j in range(1,n+1): if i == j : continue for k in range(1,n+1): if j == k or i == k: continue if i+j+k == x: count += 1 return count//6 while True: n,x = map(int,input().split()) if n == x == 0: break; print(solve(n,x))
0
null
58,432,192,602,920
258
58
n = int(input()) a = [] b = [] for i in range(n): s = input() x = 0 y = 0 for c in s: if c == '(': y += 1 else: if y >= 1: y -= 1 else: x += 1 if x < y: a.append([x, abs(x-y)]) else: b.append([y, abs(x-y)]) def calc(a): ret = 0 for x, y in sorted(a): if ret < x: return -1 ret += y return ret res1 = calc(a) res2 = calc(b) print("Yes" if res1 >= 0 and res1 == res2 else "No")
from math import sin, cos, pi class Point: def __init__(self, x, y): self.x = x self.y = y def __str__(self): return f"{self.x:.8f} {self.y:.8f}" def kock(n, p1: Point, p2: Point): if n == 0: return s = Point( x=(2*p1.x + p2.x)/3, y=(2*p1.y + p2.y)/3 ) t = Point( x=(p1.x + 2*p2.x)/3, y=(p1.y + 2*p2.y)/3 ) u = Point( x=(t.x - s.x) * cos(pi / 3) - (t.y - s.y) * sin(pi / 3) + s.x, y=(t.x - s.x) * sin(pi / 3) + (t.y - s.y) * cos(pi / 3) + s.y ) kock(n - 1, p1, s) print(s) kock(n - 1, s, u) print(u) kock(n - 1, u, t) print(t) kock(n - 1, t, p2) def main(): n = int(input()) p1 = Point(0.0, 0.0) p2 = Point(100.0, 0.0) print(p1) kock(n, p1, p2) print(p2) main()
0
null
11,984,298,902,920
152
27
count = 0 for i in range(int(input())): l = list(map(int, input().split())) if l[0] == l[1]: count += 1 elif l[0] != l[1]: count = 0 if count >= 3: print("Yes") break if count < 3: print("No")
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() D = [len(set(LIST()))==1 for _ in range(N)] for i in range(N): if sum(D[i:i+3]) == 3: print("Yes") break else: print("No")
1
2,483,606,236,930
null
72
72
N, K = map(int, input().split()) p = list(map(int, input().split())) q = sum(p[:K]) ans = q for i in range(K, N): q = q + p[i] - p[i - K] ans = max(ans, q) print((ans+K)/2)
n, k = map(int, input().split()) p = list(map(int, input().split())) def ev(n): return n * (n + 1) / (2 * n) q = [0] for i in range(n): q.append(ev(p[i])) q[i + 1] = q[i] + ev(p[i]) #print(q) tmp = 0 for j in range(n - k + 1): if tmp <= q[j + k] - q[j]: tmp = q[j + k] - q[j] print(tmp)
1
74,566,359,802,022
null
223
223
import sys n = int(input()) dic = set() for i in range(n): cmd = sys.stdin.readline().split() if cmd[0] == "insert": dic.add(cmd[1]) else: print("yes" if cmd[1] in dic else "no")
d={} for _ in[0]*int(input()): c,g=input().split() if'i'==c[0]:d[g]=0 else:print(['no','yes'][g in d])
1
74,119,034,176
null
23
23
A,B = map(int,input().split()) num = min(A,B) num1 = max(A,B) count=0 for i in range(num1): count += num*(10**i) print(count)
a,b=map(int,input().split()) if a<b: ans=str(a)*b else: ans=str(b)*a print(ans)
1
84,404,765,052,928
null
232
232
N = int(input()) A = list(map(int, input().split())) INF = 10 ** 18 if N % 2: dp = [[-INF for j in range(N)] for i in range(3)] dp[0][0] = A[0] dp[1][1] = A[1] dp[2][2] = A[2] for j in range(N): for i in range(3): if dp[i][j] == -INF: continue for k in range(3): if i + k < 3 and j + k + 2 < N: dp[i + k][j + k + 2] = max(dp[i + k][j + k + 2], dp[i][j] + A[j + k + 2]) print(max(max(dp[0][-3:-1]), max(dp[1][-2:]), max(dp[2][-1:]))) else: dp = [[-INF for j in range(N)] for i in range(2)] dp[0][0] = A[0] dp[1][1] = A[1] for j in range(N): for i in range(2): if dp[i][j] == -INF: continue for k in range(2): if i + k < 2 and j + k + 2 < N: dp[i + k][j + k + 2] = max(dp[i + k][j + k + 2], dp[i][j] + A[j + k + 2]) print(max(max(dp[0][-2:]), max(dp[1][-1:])))
r = input() if len(r) & 1: print("No") else: for i in range(0, len(r), 2): if not (r[i] == 'h' and r[i + 1]=='i'): print("No") exit() print("Yes")
0
null
45,357,030,509,348
177
199
# Easy Liniear Programming A,B,C,K = map(int,input().split()) ans = min(A,K)*1 + max(0, K-A)*0 + max(0, K-A-B)*(-1) print(ans)
n = int(input()) x = input() popcnt = x.count("1") def powmod(lst,mod): if not mod: return d = 1 for i in range(n)[::-1]: d %= mod lst[i] = d d *= 2 def xmod(mod): if not mod: return res = 0 d = 1 for i in range(n)[::-1]: d %= mod if int(x[i]): res += d res %= mod d *= 2 return res def solve(x): res = 1 while x != 0: popcnt = 0 for i in range(20): if x>>i & 1: popcnt += 1 x %= popcnt res += 1 return res powlst0 = [0]*n powlst1 = [0]*n powmod(powlst0,popcnt+1) x0 = xmod(popcnt+1) powmod(powlst1,popcnt-1) x1 = xmod(popcnt-1) for i in range(n): if int(x[i]): if popcnt == 1: print(0) else: print(solve((x1-powlst1[i])%(popcnt-1))) else: print(solve((x0+powlst0[i])%(popcnt+1)))
0
null
14,965,112,201,472
148
107
from sys import stdin input=stdin.readline input() a=list(map(int,input().split())) ans=sum(a) b=[0]*(10**5+1) for i in a: b[i]+=1 c=int(input()) for i in range(c): n,m=map(int,input().split()) if b[n]>0: x=b[n] if x>0: b[m]+=x b[n]=0 ans+=(m-n)*x print(ans)
def main(): import sys from collections import Counter input = sys.stdin.readline input() a = tuple(map(int, input().split())) ans = sum(a) a = Counter(a) for _ in range(int(input())): b, c = map(int, input().split()) if b in a: ans += (c - b) * a[b] a[c] = a[c] + a[b] if c in a else a[b] del a[b] print(ans) if __name__ == "__main__": main()
1
12,179,225,446,020
null
122
122
n, k = map(int, input().split()) scores = list(map(int, input().split())) evals = [] cnt = 0 for i in range(k, n, 1): edge = scores[cnt] new_edge = scores[i] if edge < new_edge: print('Yes') else: print('No') cnt += 1
import copy # a = get_int() def get_int(): return int(input()) # a = get_string() def get_string(): return input() # a_list = get_int_list() def get_int_list(): return [int(x) for x in input().split()] # a_list = get_string_list(): def get_string_list(): return input().split() # a, b = get_int_multi() def get_int_multi(): # a, b = get_int_multi() return map(int, input().split()) # a_list = get_string_char_list() def get_string_char_list(): return list(str(input())) # print("{} {}".format(a, b)) # for num in range(0, a): # a_list[idx] # a_list = [0] * a ''' while (idx < n) and (): idx += 1 ''' def main(): n, k = get_int_multi() a_list = get_int_list() for i in range(0, n-k): if a_list[i] < a_list[k+i]: print("Yes") else: print("No") if __name__ == '__main__': main()
1
7,088,304,897,470
null
102
102
X = int(input()) num_item = int(X // 100) remain = X - num_item * 100 if remain <= num_item * 5: print(1) else: print(0)
import sys, math from functools import lru_cache from collections import deque sys.setrecursionlimit(500000) MOD = 10**9+7 def input(): return sys.stdin.readline()[:-1] def mi(): return map(int, input().split()) def ii(): return int(input()) def i2(n): tmp = [list(mi()) for i in range(n)] return [list(i) for i in zip(*tmp)] @lru_cache(maxsize=None) def f(x): if x < 0: return False if (x%100==0) or (x%101==0) or (x%102==0) \ or (x%103==0) or (x%104==0) or (x%105==0): return True return f(x-100) or f(x-101) or f(x-102) \ or f(x-103) or f(x-104) or f(x-105) def main(): X = ii() print(1 if f(X) else 0) if __name__ == '__main__': main()
1
127,779,361,555,748
null
266
266
from collections import deque n, m = map(int, input().split()) name = ['']*n time = ['']*n for i in range(n): name[i], time[i] = input().split() time = list(map(int, time)) Q = deque([i for i in range(n)]) t = 0 while Q!=deque([]): q = Q.popleft() if time[q]<=m: t += time[q] print(name[q] + ' ' + str(t)) else: t += m time[q] -= m Q.append(q)
# walking takahashi def walk(X, K, D): abX = abs(X) if abX >= K*D: return abX - K*D itr = round(abX/D) ret = K - itr res = abs(abX - D*itr) if (ret % 2) == 0: return res return abs(res - D) if __name__ == "__main__": inputs = list(map(int, input().split())) print(walk(inputs[0], inputs[1], inputs[2]))
0
null
2,655,852,592,340
19
92
import math M=list(map(int, input().split())) N=M[0] K=M[1] A=list(map(int, input().split())) def cancut(A,N,K,X): #A1..ANをK回で全てX以下に切れるか?Yesなら1、Noなら2を返す。 cut=0 for a in A: n = (a + X - 1) // X # a/x の少数切上げ cut += n - 1 if cut<=K: return 1 else: return 0 left=0 right=max(A) while(abs(right - left) > 1): mid=(right+left)//2 if cancut(A,N,K,mid)==1: right=mid else: left=mid print(right)
N = int(input()) A = [] for _ in range(N): a = int(input()) b = [] for _ in range(a): b.append(list(map(int, input().split()))) A.append(b) # 証言リスト A[i人目][j個目の証言] -> [誰が, bit(1は正、0は誤)] # bitが1であれば正しい証言、0であれば間違った証言とする # 正しい証言だけ確認して、[i, 1]と証言した i も1かどうか、[j,0]と証言したjが0かどうか def F(i): cnt = 0 B = [-1]*N #B = [-1,-1,-1] r = 0 for j in range(N): # i=1, j=0,1,2 if (i>>j)&1: # 001 -> 1,0,0 j=0 r += 1 # r = 1 if B[j] == 0: #B[0] == -1 return 0 B[j]=1 # B = [1,-1,-1] for p,q in A[j]: # A[0] = [[2, 1], [3, 0]] if q == 0: if B[p-1]==1: # B[2] == -1 return 0 B[p-1] = 0 # B = [1,1,0] else: if B[p-1]==0: # B[1] == -1 return 0 B[p-1] = 1 # B = [1,1,-1] else: cnt += 1 # cnt = 1 else: # j=1 if B[j]==1: #B[1] ==1 return 0 B[j]=0 if cnt == r: return cnt ans = 0 for i in range(1<<N): ans = max(ans,F(i)) print(ans)
0
null
63,911,688,594,396
99
262
def main(): N, P = map(int, input().split()) S = input() if P == 2 or P == 5: ans = 0 for i in range(N): if int(S[i]) % P == 0: ans += (i + 1) print(ans) exit() sum_rem = [0] * N sum_rem[0] = int(S[N - 1]) % P ten = 10 for i in range(1, N): a = (int(S[N - 1 - i]) * ten) % P sum_rem[i] = (a + sum_rem[i - 1]) % P ten = (ten * 10) % P ans = 0 count = [0] * P count[0] = 1 for i in range(N): ans += count[sum_rem[i]] count[sum_rem[i]] += 1 print(ans) if __name__ == '__main__': main()
import sys from bisect import * from heapq import * from collections import * from itertools import * from functools import * sys.setrecursionlimit(100000000) input = lambda: sys.stdin.readline().rstrip() N, P = map(int, input().split()) S = list(map(int, input())) if P == 2: print(sum(i + 1 for i in range(N) if S[i] % 2 == 0)) elif P == 5: print(sum(i + 1 for i in range(N) if S[i] % 5 == 0)) else: x = [None] * (N + 1) x[N] = 0 tmp = 1 for i in reversed(range(N)): x[i] = (x[i + 1] + S[i] * tmp) % P tmp = tmp * 10 % P print(sum(i * (i - 1) // 2 for i in Counter(x).values()))
1
58,314,369,297,840
null
205
205
n = int(input()) a = list(map(int, input().split())) for n in a: if n % 2 == 0: if not n % 3 == 0 and not n % 5 == 0: print("DENIED") break else: print("APPROVED")
N = int(input()) A = list(map(int,input().split())) B = len([i for i in A if i % 2==0]) count = 0 for i in A: if i % 2 !=0: continue elif i % 3 ==0 or i % 5==0: count +=1 if count == B: print('APPROVED') else: print('DENIED')
1
69,174,066,760,680
null
217
217
n=int(input()) mod=998244353 A=list(map(int,input().split())) import sys # mod=10**9+7 ; inf=float("inf") from math import sqrt, ceil from collections import deque, Counter, defaultdict #すべてのkeyが用意されてる defaultdict(int)で初期化 input=lambda: sys.stdin.readline().strip() sys.setrecursionlimit(11451419) from decimal import ROUND_HALF_UP,Decimal #変換後の末尾桁を0や0.01で指定 #Decimal((str(0.5)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)) from functools import lru_cache from bisect import bisect_left as bileft, bisect_right as biright from fractions import Fraction as frac #frac(a,b)で正確なa/b #メモ化再帰defの冒頭に毎回 @lru_cache(maxsize=10**10) #引数にlistはだめ #######ここまでテンプレ####### #ソート、"a"+"b"、再帰ならPython3の方がいい #######ここから天ぷら######## C=Counter(A) if A[0]!=0 or A.count(0)>1: print(0);exit() Q=[C[i] for i in range(max(A)+1)] #print(Q) ans=1 for i in range(len(Q)-1): ans*= pow(Q[i],Q[i+1],mod) ans%=mod print(ans)
import sys def MI(): return map(int,sys.stdin.readline().rstrip().split()) N,u,v = MI() Graph = [[] for _ in range(N+1)] for i in range(N-1): a,b = MI() Graph[a].append(b) Graph[b].append(a) dist_T = [-1]*(N+1) # uからの最短距離 dist_A = [-1]*(N+1) # vからの最短距離 dist_T[u] = 0 dist_A[v] = 0 from collections import deque q1 = deque([u]) q2 = deque([v]) while q1: n = q1.pop() for d in Graph[n]: if dist_T[d] == -1: dist_T[d] = dist_T[n] + 1 q1.appendleft(d) while q2: n = q2.pop() for d in Graph[n]: if dist_A[d] == -1: dist_A[d] = dist_A[n] + 1 q2.appendleft(d) ans = 0 for i in range(1,N+1): if dist_T[i] < dist_A[i]: ans = max(ans,dist_A[i]-1) print(ans)
0
null
135,637,913,533,738
284
259
s = input() if s.count('B') == 3 or s.count("A") == 3: print("No") else: print("Yes")
n = int(input()) dp = [0] * (n + 1) for x in range(1, n + 1): for y in range(x, n + 1, x): dp[y] += 1 print(sum(dp[1:n]))
0
null
28,784,786,037,428
201
73
n, m = map(int, input().split()) a = list(map(int, input().split())) ans = n - sum(a) print(-1 if ans < 0 else ans)
X, Y = [int(arg) for arg in input().split()] def f(X, Y): for i in range(X + 1): for j in range(X + 1): kame = i tsuru = j if kame + tsuru == X and kame * 4 + tsuru * 2 == Y: return 'Yes' return 'No' print(f(X, Y))
0
null
22,962,934,236,480
168
127
n, k = list(map(int, input().split())) max_len = 2 * n - 1 # 適宜変更する mod = 10**9 + 7 def modinv(x): ''' xの逆元を求める。フェルマーの小定理より、 x の逆元は x ^ (mod - 2) に等しい。計算時間はO(log(mod))程度。 Python標準のpowは割った余りを出すことも可能。 ''' return pow(x, mod-2, mod) # 二項係数の左側の数字の最大値を max_len とする。nとかだと他の変数と被りそうなので。 # factori_table = [1, 1, 2, 6, 24, 120, ...] 要は factori_table[n] = n! # 計算時間はO(max_len * log(mod)) modinv_table = [-1] * (max_len + 1) modinv_table[0] = None # 万が一使っていたときにできるだけ早期に原因特定できるようにしたいので、Noneにしておく。 factori_table = [1] * (max_len + 1) factori_inv_table = [1] * (max_len + 1) for i in range(1, max_len + 1): factori_table[i] = factori_table[i-1] * (i) % mod modinv_table[1] = 1 for i in range(2, max_len + 1): modinv_table[i] = (-modinv_table[mod % i] * (mod // i)) % mod factori_inv_table[i] = factori_inv_table[i-1] * modinv_table[i] % mod def binomial_coefficients(n, k): ''' n! / (k! * (n-k)! ) 0 <= k <= nを満たさないときは変な値を返してしまうので、先にNoneを返すことにする。 場合によっては0のほうが適切かもしれない。 ''' if not 0 <= k <= n: return None return (factori_table[n] * factori_inv_table[k] * factori_inv_table[n-k]) % mod def binomial_coefficients2(n, k): ''' (n * (n-1) * ... * (n-k+1)) / (1 * 2 * ... * k) ''' ans = 1 for i in range(k): ans *= n-i ans *= modinv_table[i + 1] ans %= mod return ans if k >= n-1: # nHn = 2n-1 C n print(binomial_coefficients(2 * n - 1, n)) else: # 移動がk回←→ 人数0の部屋がk個以下 # 人数0の部屋がちょうどj個のものは # nCj(人数0の部屋の選び方) * jH(n-j) (余剰のj人を残りの部屋に入れる) ans = 0 for j in range(k+1): if j == 0: ans += 1 else: ans += binomial_coefficients(n, j) * binomial_coefficients(n-1, j) ans %= mod print(ans)
import sys sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python import math from copy import copy, deepcopy from operator import itemgetter from bisect import bisect_left, bisect, bisect_right#2分探索 #bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下 from collections import deque #deque(l), pop(), append(x), popleft(), appendleft(x) ##listでqueの代用をするとO(N)の計算量がかかってしまうので注意 from collections import Counter#文字列を個数カウント辞書に、 #S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items() from itertools import accumulate#累積和 #list(accumulate(l)) from heapq import heapify,heappop,heappush #heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone #import fractions#古いatcoderコンテストの場合GCDなどはここからimportする from functools import lru_cache#pypyでもうごく #@lru_cache(maxsize = None)#maxsizeは保存するデータ数の最大値、2**nが最も高効率 def input(): return sys.stdin.readline()[:-1] def printl(li): print(*li, sep="\n") def argsort(s, return_sorted=False): inds=sorted(range(len(s)), key=lambda k: s[k]) if return_sorted: return inds, [s[i] for i in inds] return inds def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65 def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65) def matmat(A,B): K,N,M=len(B),len(A),len(B[0]) return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)] def matvec(M,v): N,size=len(v),len(M) return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)] def T(M): n,m=len(M),len(M[0]) return [[M[j][i] for j in range(n)] for i in range(m)] def main(): mod = 10**9+7 #w.sort(key=itemgetter(1),reversed=True) #二個目の要素で降順並び替え #N = int(input()) N, K = map(int, input().split()) #A = tuple(map(int, input().split())) #1行ベクトル #L = tuple(int(input()) for i in range(N)) #改行ベクトル #S = tuple(tuple(map(int, input().split())) for i in range(N)) #改行行列 #2n-1Cn fac1=1 fac2=1 facs=[1,1] for i in range(2,2*N): facs.append(facs[-1]*i%mod) # fac1=pow(facs[N],mod-2,mod) # fac2=pow(facs[N-1],mod-2,mod) # tot=facs[2*N-1]*fac1%mod # tot*=fac2 # tot%=mod def comb(N,r): fac1=pow(facs[r],mod-2,mod) fac2=pow(facs[N-r],mod-2,mod) tot=(facs[N]*fac1%mod)*fac2%mod return tot tot=comb(2*N-1,N) if N-1<=K: print(tot) else:#0のへやがk+1より多くあるとボツ r=0 for i in range(1,N-(K+1)+1): n=N-i r+=comb(N,i)*comb(n+i-1,i-1)%mod r%=mod print((tot-r)%mod) if __name__ == "__main__": main()
1
67,059,382,471,964
null
215
215
def main(): N, K = [int(i) for i in input().split()] A = [int(i) for i in input().split()] v = {0: 1} n = [0] r = 0 t = 0 for i, a in enumerate(A, 1): if i >= K: v[n[i - K]] -= 1 t += a j = (t - i) % K r += v.get(j, 0) v[j] = v.get(j, 0) + 1 n.append(j) return r print(main())
N, K = map(int, input().split()) A = list(map(int, input().split())) X = [0 for _ in range(N)] D = {0: [-1]} E = {0: 1} X[0] = (A[0] - 1) % K if X[0] in D: D[X[0]].append(0) E[X[0]] += 1 else: D[X[0]] = [0] E[X[0]] = 1 for i in range(1, N): X[i] = (X[i-1] + A[i] - 1) % K if X[i] in D: D[X[i]].append(i) E[X[i]] += 1 else: D[X[i]] = [i] E[X[i]] = 1 S = 0 for i in D: n = E[i] if n > 1: L = D[i][:] m = L[-1] for j in range(n-1): x = L[j] if m - x < K: S += n - 1 - j else: l, r = j, n d = (l + r) // 2 tmp = 2 * n while tmp != 0: if L[d] - x <= K - 1: l = d d = (l + r) // 2 else: r = d d = (l + r) // 2 tmp //= 2 S += d - j print(S)
1
137,237,804,837,240
null
273
273
def solve(): X = int(input()) p = int(X / 100) q = X % 100 if 0 <= q <= 5 * p: print(1) else: print(0) if __name__ == "__main__": solve()
while True: H,W = map(int,input().split()) if H == W == 0: break for i in range(H): print(('#.'*W)[i%2:][:W]) print()
0
null
64,146,058,032,518
266
51
x = input() if 1 <= x <= 100: print(x*x*x)
import sys input = sys.stdin.readline from collections import deque def main(): N, D, A = map(int, input().split()) monsters = sorted([list(map(int, input().split())) for _ in range(N)]) for i in range(N): monsters[i][1] = (monsters[i][1] + A - 1) // A attacks = deque([]) # [(有効範囲, 攻撃回数)] ans = 0 atk = 0 for i in range(N): atk_this = monsters[i][1] while attacks and monsters[i][0] > attacks[0][0]: _, atk_ = attacks.popleft() atk -= atk_ if atk_this - atk > 0: ans += atk_this - atk attacks.append((monsters[i][0]+2*D, atk_this-atk)) atk += atk_this - atk print(ans) if __name__ == '__main__': main()
0
null
41,164,400,235,908
35
230
import sys input = sys.stdin.readline sys.setrecursionlimit(10**9) N = int(input()) adj = [ [] for _ in range(N) ] for i in range(N-1): a,b = map(int,input().split()) a -= 1 b -= 1 adj[a].append((b,i)) res = [None] * (N-1) def dfs(n, c=-1, p=-1): if c==-1 or c>1: nc = 1 else: nc = c+1 for nei,i in adj[n]: if nei == p: continue res[i] = nc dfs(nei, nc, n) nc += 1 if nc==c: nc += 1 dfs(0) print(max(res)) for r in res: print(r)
N, X, Y = map(int, input().split()) X -= 1 Y -= 1 ans = [0]*(N-1) for i in range(N): for j in range(i+1, N): if i == j: continue cnt = min(abs(i-j), abs(X-i)+abs(j-Y)+1, abs(j-X)+abs(Y-i)+1) ans[cnt-1] += 1 for i in range(N-1): print(ans[i])
0
null
89,969,499,128,788
272
187
import numpy as np n = int(input()) s = np.array(input().split(), np.int64) mod = 10 ** 9 + 7 res = 0 po2 = 1 for i in range(61): bit = (s >> i) & 1 x = np.count_nonzero(bit) y = n - x res += x * y % mod * po2 res %= mod po2 *= 2 % mod print(res)
n = int(input()) a = list(map(int, input().split())) mod = 10**9 + 7 pows = [pow(2, i, mod) for i in range(60)] count = [0]*60 num = [0]*60 ans = 0 for i in range(n): bit = a[i] for g in range(60): if bit % 2 == 1: count[g] += 1 num[g] += i+1 - count[g] else: num[g] += count[g] bit //= 2 for i in range(60): ans += num[i]*pows[i] print(ans % mod)
1
122,713,091,967,536
null
263
263
H = [[[0 for x in xrange(10)] for j in xrange(3)] for i in xrange(4)] n = input() for x in range(n): b,f,r,v = map(int, raw_input().split()) H[b-1][f-1][r-1] += v for b in range(4): for f in range(3): print(' '+' '.join(map(str,H[b][f]))) if b != 3: print ('#'*20)
n = int(input()) count = list() for b in range(4): count.append(list()) for f in range(3): count[b].append(list()) for r in range(10): count[b][f].append(0) read = 0 while read < n: b,f,r,v = [int(x) for x in input().split(" ")] count[b-1][f-1][r-1] += v read += 1 for b in range(len(count)): for f in range(len(count[b])): for r in range(len(count[b][f])): print("", count[b][f][r], end="") print() if b < len(count) - 1: print("####################")
1
1,113,888,611,190
null
55
55
x = input().split() s = int(x[0]) w = int(x[1]) if s > w: print('safe') else: print('unsafe')
N,K=list(map(int,input().split())) l=[0]*(K+1) ans=0 mod=10**9+7 for x in range(K,0,-1): l[x]=pow((K//x),N,mod) for y in range(2*x,K+1,x): l[x]-=l[y] l[x]=pow(l[x],1,mod) ans+=l[x]*x ans=pow(ans,1,mod) print(ans)
0
null
32,934,945,798,260
163
176
from collections import Counter, defaultdict # n = int(input()) # li = list(map(int, input().split())) # n = int(input()) a, b = map(int, input().split()) c, d = map(int, input().split()) # d = defaultdict(lambda: 0) # s = input() print("1" if c == a+1 else "0")
x,y=list(map(int, input().split())) x,y=list(map(int, input().split())) if y==1: print(1) else: print(0)
1
124,551,447,125,700
null
264
264
def solve(): A,B,N = map(int, input().split()) if N < B-1: print((A*N)//B - A * (N//B)) else: print(((A*(B-1))//B) - A* ((B-1)//B)) if __name__ == "__main__": solve()
import sys,math,collections,itertools input = sys.stdin.readline A,B,N = list(map(int,input().split())) if B <= N: x =B-1 else: x = N print(math.floor(A*x/B) - A * math.floor(x/B))
1
28,000,404,186,082
null
161
161
a,b,c,d = map(int, input().split()) x1=a*c x2=a*d x3=b*c x4=b*d print(max(x1,x2,x3,x4))
#!/usr/bin/env python3 # -*- coding: utf-8 -*- def update(x): y = x * 105 // 100 r = y % 1000 y = y // 1000 * 1000 if r: y += 1000 return y def main(): n = int(input()) x = 100000 for _ in range(n): x = update(x) print(x) if __name__ == "__main__": main()
0
null
1,500,528,300,962
77
6
import sys num = [] for i in sys.stdin: H, W = i.split() if H == W == '0': break num.append((int(H), int(W))) for cnt in range(len(num)): for h in range(num[cnt][0]): for w in range(num[cnt][1]): print('#',end='') print() print()
b = [] c = [] while True: a = input().split() a[0] = int(a[0]) a[1] = int(a[1]) if a[0] == 0 and a[1] == 0: break else: b.append("#"*a[1]) c.append(a[0]) for i in range(len(c)): for j in range(c[i]): print(b[i]) print("\n",end="")
1
785,588,827,718
null
49
49
a = input() if a.isupper() == True: print("A") else: print("a")
cc = input().rstrip() r = 'a' if ord('a') <= ord(cc) and ord(cc) <= ord('z') else 'A' print(r)
1
11,267,201,137,610
null
119
119
def getN(): return int(input()) def getNM(): return map(int, input().split()) def getList(): return list(map(int, input().split())) def getArray(intn): return [int(input()) for i in range(intn)] def input(): return sys.stdin.readline().rstrip() def rand_N(ran1, ran2): return random.randint(ran1, ran2) def rand_List(ran1, ran2, rantime): return [random.randint(ran1, ran2) for i in range(rantime)] def rand_ints_nodup(ran1, ran2, rantime): ns = [] while len(ns) < rantime: n = random.randint(ran1, ran2) if not n in ns: ns.append(n) return sorted(ns) def rand_query(ran1, ran2, rantime): r_query = [] while len(r_query) < rantime: n_q = rand_ints_nodup(ran1, ran2, 2) if not n_q in r_query: r_query.append(n_q) return sorted(r_query) from collections import defaultdict, deque, Counter from sys import exit from decimal import * import heapq import math from fractions import gcd import random import string import copy from itertools import combinations, permutations, product from operator import mul from functools import reduce from bisect import bisect_left, bisect_right import sys sys.setrecursionlimit(1000000000) mod = 10 ** 9 + 7 ############# # Main Code # ############# N, X, Y = getNM() dist = [[] for i in range(N)] for i in range(N - 1): dist[i].append(i + 1) dist[i + 1].append(i) dist[X - 1].append(Y - 1) dist[Y - 1].append(X - 1) distance = [0] * N def counter(sta): ignore = [0] * N ignore[sta] = 1 pos = deque([[sta, 0]]) while len(pos) > 0: u, dis = pos.popleft() for i in dist[u]: if ignore[i] == 0: ignore[i] = 1 distance[dis + 1] += 1 pos.append([i, dis + 1]) for i in range(N): counter(i) for i in distance[1:]: print(i // 2)
n = int(input()) steps = [] for x in range(int(n ** 0.5)): if n % (x + 1) == 0: steps.append(x + n // (x + 1) - 1) print(min(steps))
0
null
102,833,053,926,418
187
288
arr = list([input() for _ in range(10)]) arr.sort(reverse=True) for i in range(3): print arr[i]
def solve(h, w): print('#'*w,end='') print(('\n#'+'.'*(w-2)+'#')*(h-2)) print('#'*w) print() if __name__ == '__main__': while True: h, w = [int(i) for i in input().split()] if h == w == 0: break solve(h, w)
0
null
418,535,197,850
2
50
ans = input() if ans == "SUN": print(7) if ans == "MON": print(6) if ans == "TUE": print(5) if ans == "WED": print(4) if ans == "THU": print(3) if ans == "FRI": print(2) if ans == "SAT": print(1)
S=100000 n=int(input()) for i in range(n): S*=1.05 S=int(S) if S%1000==0: S=S else: S=S+1000 S=S//1000 S=S*1000 print(S)
0
null
66,894,297,289,428
270
6
import math def make_stu(p1, p2): s = [(2 * p1[0] + p2[0]) / 3, (2 * p1[1] + p2[1]) / 3] t = [(p1[0] + 2 * p2[0]) / 3, (p1[1] + 2 * p2[1]) / 3] u = [s[0] + (t[0] - s[0]) / 2 - math.sqrt(3) * (t[1] - s[1]) / 2, s[1] + (t[1] - s[1]) / 2 + math.sqrt(3) * (t[0] - s[0]) / 2] return s, t, u def koch_curve(n, p1, p2): if n >= 1: s, t, u = make_stu(p1, p2) if n == 1: print(s[0], s[1]) print(u[0], u[1]) print(t[0], t[1]) else: koch_curve(n - 1, p1, s) print(s[0], s[1]) koch_curve(n - 1, s, u) print(u[0], u[1]) koch_curve(n - 1, u, t) print(t[0], t[1]) koch_curve(n - 1, t, p2) n = int(input()) print(0.0, 0.0) koch_curve(n, [0.0, 0.0], [100.0, 0.0]) print(100.0, 0.0)
def main(): N = int(input()) A = [] B = [] for _ in range(N): a, b = (int(i) for i in input().split()) A.append(a) B.append(b) A.sort() B.sort() ans = -1 if N % 2 == 1: ans = B[N//2] - A[N//2] + 1 else: a = (A[N//2] + A[N//2 - 1])/2 b = (B[N//2] + B[N//2 - 1])/2 ans = b - a + 1 ans += ans - 1 print(int(ans)) if __name__ == '__main__': main()
0
null
8,674,288,545,950
27
137
def cmb(n, r, p): if (r < 0) or (n < r): return 0 r = min(r, n - r) return fac[n]*finv[r]*finv[n-r]%p N = pow(10,6) MOD = pow(10,9)+7 fac = [-1]*(N+1); fac[0] = 1; fac[1] = 1 #階乗 finv = [-1]*(N+1); finv[0] = 1; finv[1] = 1 #階乗の逆元 inv = [-1]*(N+1); inv[0] = 0; inv[1] = 1 #逆元 for i in range(2,N+1): fac[i] = fac[i-1]*i%MOD inv[i] = MOD - inv[MOD%i]*(MOD//i)%MOD finv[i] = finv[i-1]*inv[i]%MOD X,Y = map(int,input().split()) if (X+Y)%3 != 0: print(0) exit() n = (X+Y)//3 if min(X,Y) < n: print(0) exit() ans = cmb(n,min(X,Y)-n,MOD) print(ans)
class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def group_count(self): return len(self.roots()) def all_group_members(self): return {r: self.members(r) for r in self.roots()} def __str__(self): return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots()) N, M ,K = map(int, input().split()) ndic = {i:set() for i in range(1,N+1)} bdic = {i:set() for i in range(1,N+1)} uf = UnionFind(N+1) rlt = [] for _ in range(M): a,b = map(int, input().split()) ndic[a].add(b) ndic[b].add(a) uf.union(a,b) for _ in range(K): a,b = map(int, input().split()) bdic[a].add(b) bdic[b].add(a) for i in range(1,N+1): tmp = uf.size(i) for a in bdic[i]: if uf.find(a) == uf.find(i): tmp -= 1 tmp -= len(ndic[i]) rlt.append(tmp-1) print(" ".join(map(str, rlt)))
0
null
106,205,579,421,722
281
209
n=input() a=n[-1] if a in "24579": print('hon') elif a in "0168": print('pon') else: print('bon')
import math n = float(input()) m = n / 1.08 if math.floor(math.floor(m) * 1.08) == n: print(math.floor(m)) elif math.floor(math.ceil(m) * 1.08) == n: print(math.ceil(m)) else: print(':(')
0
null
72,331,057,140,608
142
265
def main(): n = int(input()) S = list(map(int,input().split())) q = int(input()) T = list(map(int,input().split())) cnt = 0 S.sort() for i in range(n): if i == 0: if S[i] in T: cnt += 1 continue if (S[i] in T) and (S[i] != S[i-1]): cnt += 1 return cnt print(main())
n = int(raw_input()) a = 1 b = 1 for i in range(n-1): tmp = a a = b b += tmp print b
0
null
32,316,005,888
22
7
from itertools import accumulate N, M = map(int, input().split()) A = sorted(list(map(int, input().split()))) ruiseki = list(accumulate(A[::-1])) def syakutori(mid): x = 0 cnt = 0 ans = 0 for i in range(N)[::-1]: # 大きい方から while x < N: if A[i] + A[x] >= mid: cnt += N - x if N - x >= 1: ans += A[i] * (N - x) + ruiseki[N - x - 1] break else: x += 1 return cnt, ans # 自分以上の数の個数がM個以上になる値のうち最大のもの ok = 0 ng = 10 ** 15 while ok + 1 < ng: mid = (ok + ng) // 2 cnt, ans = syakutori(mid) if cnt >= M: ok = mid else: ng = mid # okで終わってる場合はcntがM個以上の場合があるから過分を引く # ngで終わってる場合は残りM-cnt個のokを足す print(ans + ok * (M - cnt))
#!/usr/bin/env python3 import sys import math import decimal import itertools from itertools import product from functools import reduce def input(): return sys.stdin.readline()[:-1] def sort_zip(a:list, b:list): z = zip(a, b) z = sorted(z) a, b = zip(*z) a = list(a) b = list(b) return a, b def main(): H, W, K = map(int, input().split()) c = [] for i in range(H): c.append(input()) ans = 0 for i in product([0, 1], repeat=H): for j in product([0, 1], repeat=W): black = 0 for h in range(H): for w in range(W): if i[h] == 1 and j[w] == 1 and c[h][w] == '#': black += 1 if black == K: ans += 1 print(ans) if __name__ == '__main__': main()
0
null
58,485,246,034,290
252
110
W,H,x,y,r=map(int, input().split(" ")) if x<r or y<r or x+r>W or y+r>H : print('No') else : print('Yes')
inl=map(int, raw_input().split()) if inl[0]>=inl[2]+inl[4] and inl[2]-inl[4]>=0 and inl[1]>=inl[3]+inl[4] and inl[3]-inl[4]>=0: print "Yes" else: print "No"
1
461,051,397,850
null
41
41
from collections import deque d = deque() n = int(input()) for i in range(n): s = input() if s == "deleteFirst": d.popleft() elif s == "deleteLast": d.pop() elif s[:6] == "insert": d.appendleft(int(s[7:])) else: delkey = int(s[7:]) if delkey in d: d.remove(delkey) print(" ".join(map(str,d)))
# encoding: utf-8 import sys from collections import deque class Solution: @staticmethod def doubly_linked_list(): # write your code here array_length = int(input()) task_deque = deque() _input = sys.stdin.readlines() for i in range(array_length): l_arg = _input[i].split() action = l_arg[0] if len(l_arg) > 1: value = l_arg[-1] # print(action, 'v', value) if action == 'insert': task_deque.appendleft(value) elif action == 'deleteFirst': task_deque.popleft() elif action == 'deleteLast': task_deque.pop() elif action == 'delete': try: task_deque.remove(value) except Exception as e: pass print(" ".join(task_deque)) if __name__ == '__main__': solution = Solution() solution.doubly_linked_list()
1
50,303,548,132
null
20
20
c=input() o=ord(c) o+=1 ans=chr(o) print(ans)
s = 'abcdefghijklmnopqrstuvwxyz' c = input() for i in range(26): if c == s[i]: print(s[i+1]) exit() exit()
1
91,919,332,812,960
null
239
239
def main(): n = int(input()) d = list(map(int,input().split())) md = 998244353 if d[0] != 0: print(0) return 0 dp = [0]*n for i in d: dp[i]+=1 res = 1 for i in d[1:]: res = (res * dp[i-1]) % md print(res%md) if __name__ == '__main__': main()
n = int(input()) d = list(map(int, input().split())) dist = [0]*n for i in range(n): dist[d[i]] += 1 ans = 1 if d[0]!=0: ans = 0 elif dist[0] != 1: ans = 0 else: for i in range(2,n): if dist[i-1]==0 and dist[i]!=0: ans = 0 break elif dist[i] == 0: continue else: for j in range(dist[i]): ans *= dist[i-1] if ans > 998244353: ans %= 998244353 print(ans)
1
154,541,640,113,948
null
284
284
def solve(h, w): print('#'*w,end='') print(('\n#'+'.'*(w-2)+'#')*(h-2)) print('#'*w) print() if __name__ == '__main__': while True: h, w = [int(i) for i in input().split()] if h == w == 0: break solve(h, w)
while True: h, w = map(int, input().split()) if h == 0 and w == 0: break print("#" * w) for _ in range(h-2): print("#","." * (w-2), "#", sep="") print("#" * w) print()
1
841,228,319,610
null
50
50
str1, str2 = input()*2, input() if str2 in str1: print("Yes") else: print("No")
def main(): n,m = map(int,input().split()) print(n * (n - 1) // 2 + m * (m - 1) // 2) main()
0
null
23,644,641,198,640
64
189
k=int(input()) a,b=map(int,input().split(' ')) flag=0 for i in range(a,b+1): if i%k==0: flag=1 if flag==1: print('OK') else: print('NG')
import math k=int(input()) [a,b]=list(map(int, input().split())) A=math.floor(a/k) B=math.floor(b/k) if A!=B: print("OK") elif k==1: print("OK") elif a%k==0 or b%k==0: print("OK") else: print("NG")
1
26,479,766,345,978
null
158
158
import math def solve(x): y = 0 for i in range(n): if a[i] >= x / f[i]: y += math.ceil(a[i] - x / f[i]) if y <= k: return True else: return False def binary_search(c1, c2): m = (c1 + c2 + 1) // 2 if abs(c1 - c2) <= 1: return m else: if solve(m): c1 = m else: c2 = m return binary_search(c1, c2) n, k = map(int, input().split()) a = list(map(int, input().split())) f = list(map(int, input().split())) a.sort() f.sort(reverse = True) M = 0 for i in range(n): M = max(M, a[i] * f[i]) ans = binary_search(M, -1) print(ans)
''' A[i]<A[j] F[i]<F[j] となる組み合わせが存在したと仮定 max(A[i]*F[i],A[j]*F[j])=A[j]*F[j] >A[i]*F[j] >A[j]*F[i] より、この場合はiとjを入れ替えたほうが最適となる。 結局 Aを昇順ソート Fを降順ソート するのが最適となる。 問題は修行の割当である。 順序を保ったまま修行していっても問題ない 3,2->1,2と 3.2->2,1は同じとみなせるため 二分探索 成績をmidにするために必要な修行コスト lowならK回より真に大 highならK回以下 ''' N,K=map(int,input().split()) A=[int(i) for i in input().split()] F=[int(i) for i in input().split()] A.sort() F.sort(reverse=True) low=-1 high=max(A)*max(F)+1 while(high-low>1): mid=(high+low)//2 tmp=0 for i in range(N): #A[i]*F[i]-mid<=X*F[i] x=(A[i]*F[i]-mid+F[i]-1)//F[i] if x<0: continue tmp+=x if tmp<=K: high=mid else: low=mid print(high)
1
165,182,736,109,732
null
290
290
a = input().split() b = str(a[0]) c = str(a[1]) print(c+b)
def main(): S, T = input().split() print(T + S) if __name__ == "__main__": main()
1
103,515,002,524,462
null
248
248
A, B, M = [int(a) for a in input().split()] a = [int(ai) for ai in input().split()] b = [int(bi) for bi in input().split()] x = [] y = [] c = [] for _ in range(M): xi, yi, ci = [int(xyc) for xyc in input().split()] x.append(xi) y.append(yi) c.append(ci) min_price = min(a) + min(b) for i in range(M): price = a[x[i]-1] + b[y[i]-1] - c[i] min_price = min(min_price, price) print(min_price)
A , B, M = map(int,input().split(" ")) a = list(map(int,input().split(" "))) b = list(map(int,input().split(" "))) res = min(a) + min(b) for _ in range(M): x,y, c = list(map(int,input().split(" "))) x-=1 y-=1 res = min(res,a[x]+b[y]-c) print(res)
1
54,146,205,345,822
null
200
200
s=input() l=len(s) l2=l//2 count=0 for i in range(l2): if s[i]!=s[l-1-i]: count+=1 print(count)
s = list(input()) n = len(s) ans = 0 for i in range(n//2): if s[i] != s[-1-i]: ans += 1 print(ans)
1
119,883,756,822,784
null
261
261
import sys # sys.setrecursionlimit(100000) from collections import deque from collections import defaultdict def input(): return sys.stdin.readline().strip() def input_int(): return int(input()) def input_int_list(): return [int(i) for i in input().split()] def main(): # 各ノードで幅優先探索を行う # 計算量(O(n^2)) n, x, y = input_int_list() g = defaultdict(list) g[x].append(y) g[y].append(x) for i in range(1, n): g[i].append(i + 1) g[i + 1].append(i) ans = [0] * (n + 1) for i in range(1, n + 1): # 幅優先探索 seen = set() dq = deque() dq.append((i, 0)) # node,depth while dq: p, d = dq.popleft() if p > i: # 整数の組(i,j) (1 <= i <= j <= N) ans[d] += 1 for q in g[p]: if q not in seen: dq.append((q, d + 1)) seen.add(q) for i in range(1, n): print(ans[i]) return if __name__ == "__main__": main()
n, x, y = list(map(int, input().split())) x = x-1 y = y-1 ans = [0] * (n-1) for i in range(n): for j in range(i+1, n): shortest = min(abs(j-i), abs(x-i)+abs(y-j)+1, abs(y-i)+abs(x-j)+1) ans[shortest-1] += 1 for a in ans: print(a)
1
44,352,435,899,190
null
187
187
##### # B # ##### N = input() sum = sum(map(int,N)) if sum % 9 == 0: print('Yes') else: print('No')
H = int(input()) W = int(input()) N = int(input()) for i in range(min(H,W)): N -= max(H, W) if N <= 0: print(i+1) exit()
0
null
46,895,548,326,940
87
236
H, N = map(int, input().split()) A = [int(x) for x in input().split()] AA = sum(A) if AA >= H: print("Yes") else: print("No")
n, k, c = map(int, input().split()) s = input() left = [] i = 0 while len(left) < k: if s[i] == 'o': left.append(i) i += c + 1 else: i += 1 right = [] i = n - 1 while len(right) < k: if s[i] == 'o': right.append(i) i -= c + 1 else: i -= 1 right = right[::-1] for i in range(k): if left[i] == right[i]: print(left[i] + 1)
0
null
59,692,072,444,662
226
182
from statistics import median #import collections #aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0] from fractions import gcd from itertools import combinations # (string,3) 3回 from collections import deque from collections import defaultdict import bisect # # d = m - k[i] - k[j] # if kk[bisect.bisect_right(kk,d) - 1] == d: # # # # pythonで無理なときは、pypyでやると正解するかも!! # # import sys sys.setrecursionlimit(10000000) mod = 10**9 + 7 def readInts(): return list(map(int,input().split())) def I(): return int(input()) x,y = readInts() a = [300000,200000,100000] + [0] * 2000 print(a[0] + a[0] + 400000 if x == y == 1 else a[x-1] + a[y-1])
#!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop import sys import math import bisect import random 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) mod = 1000000007 flush = sys.stdin.flush() def solve(): x,y = LI() ans = 0 f = [300000,200000,100000,0] if x == 1 and y == 1: print(1000000) else: ans = f[min(3,x-1)]+f[min(3,y-1)] print(ans) return #Solve if __name__ == "__main__": solve()
1
140,639,941,852,788
null
275
275
import sys def input(): return sys.stdin.readline().rstrip() def main(): a, b, c = map(int,input().split()) k = int(input()) cunt = 0 while a >= b: b *= 2 cunt += 1 while b >= c: c *= 2 cunt += 1 if cunt > k: print("No") else: print("Yes") if __name__=='__main__': main()
N,K = map(int,input().split()) A = list(map(int,input().split())) F = list(map(int,input().split())) A = sorted(A)[::-1] F = sorted(F) sumA = sum(A) if sumA <= K: print(0) else: maxC = 0 for i in range(N): tmp = A[i]*F[i] if maxC < tmp: maxC = tmp L = 0 R = maxC visited = {} visited[maxC]=True while 1: nowC = (L+R)//2 nowA = [] for i in range(N): nowA.append(nowC//F[i]) shugyou = 0 for i in range(N): shugyou += max(A[i]-nowA[i],0) if shugyou <= K: visited[nowC] = True R = nowC if (nowC-1) in visited: if visited[nowC-1] == False: ans = nowC break else: visited[nowC] = False L = nowC if (nowC+1) in visited: if visited[nowC+1] == True: ans = nowC+1 break if nowC == 0: if visited[nowC]==True: ans = 0 break print(ans)
0
null
86,350,283,827,460
101
290
import sys input = lambda : sys.stdin.readline().rstrip() sys.setrecursionlimit(max(1000, 10**9)) write = lambda x: sys.stdout.write(x+"\n") n = int(input()) s = input() def snx(s, target=None, reverse=False, loop=False): """文字列sの各要素について、cがiより→で最初に現れる位置n[i][c] loop=Trueのとき周回した先のインデックスまで求める """ if target is None: target = [chr(v) for v in range(ord("a"), ord("z")+1)] d = {c:None for c in target} nx = [[None]*len(target) for _ in range(len(s))] if not reverse: i = len(s)-1 for c in reversed(s): d[c] = i for j,t in enumerate(target): nx[i][j] = d[t] i -= 1 if loop: for i in range(len(s)): for j in range(len(target)): if nx[i][j] is None: nx[i][j] = nx[0][j] else: i = 0 for c in s: d[c] = i for j,t in enumerate(target): nx[i][j] = d[t] i += 1 if loop: for i in range(len(s)): for j in range(len(target)): if nx[i][j] is None: nx[i][j] = nx[-1][j] return nx nx = snx(s, target=list(map(str, range(10)))) ans = 0 for i in range(10): for j in range(10): for k in range(10): try: if nx[0][i] is not None and nx[nx[0][i]+1][j] is not None and nx[nx[nx[0][i]+1][j]+1][k] is not None: # print(i, j, k) ans += 1 except IndexError: pass print(ans)
def sumitrust2019_d(): n = int(input()) s = input() ans = 0 for i in range(1000): # 確認するpinの数字の組み合わせと桁の初期化 # pin = list(str(i).zfill(3)) current_digit = 0 pin = f'{i:0>3}' search_num = pin[current_digit] for num in s: # 見つけると桁を右に一つずらす # 毎回リストにアクセスするより変数に取り出しておくほうがいいかも。 # if num == pin[current_digit]: if num == search_num: current_digit += 1 # indexが3(桁が3→2→1→0)になったときはそのPINは作れる if current_digit == 3: ans += 1 break # 見つけ終わるまではチェックする数字を置き換え。 elif current_digit < 3: search_num = pin[current_digit] print(ans) if __name__ == '__main__': sumitrust2019_d()
1
128,105,640,885,918
null
267
267
N = int(input()) D = list(map(int, input().split())) DL = [0] * N mod = 998244353 if D[0] != 0: print(0) exit() for d in D: DL[d] += 1 if DL[0] != 1: print(0) exit() ans = 1 for i in range(1, N): if DL[i] == 0: if sum(DL[i:]) != 0: print(0) exit() else: print(ans%mod) exit() ans *= pow(DL[i-1], DL[i], mod) ans %= mod print(ans % mod)
from collections import Counter n = int(input()) d = list(map(int, input().split())) mod = 998244353 a = sorted(list(set(d))) if d[0] != 0: ans = 0 elif a[-1] != len(a)-1: ans = 0 else: c = sorted(Counter(d).items()) if c[0] != (0, 1): ans = 0 else: ans = 1 pre = 1 for k, v in c: ans *= pre ** v ans %= mod pre = v print(ans)
1
155,114,216,159,540
null
284
284
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ コンテスト後に解説記事を見てAC Python 3だとTLE """ import array from bisect import * from collections import * import fractions import heapq from itertools import * import math import random import re import string import sys def solve(N_str): """ https://betrue12.hateblo.jp/ の通り実装 上の桁から順番に見ていく dp[i][j] = 上から i 桁目までの硬貨の支払いとお釣りを処理し終わって、 N の i 桁目までの金額よりも j 枚分多く支払っているときの、 これまでの支払い・お釣り硬貨の最小合計枚数 """ N_str = '0' + N_str dp = [[0] * 2 for _ in range(len(N_str))] # 0376円のとき # dp[1][0] = 0 0円払う(0) # dp[1][1] = 1 1000円払う(1) # # dp[2][0] = 3 0円の状態から300円払う(3) vs 1000円の状態から700円お釣りをもらう(8) # dp[2][1] = 4 0円の状態から400円払う(4) vs 1000円の状態から600円お釣りをもらう(7) # # dp[3][0] = 7 300円の状態から追加で70円払う(3+7) vs 400円の状態から30円もらう(4+3) # dp[3][1] = 6 300円の状態から追加で80円払う(3+8) vs 400円の状態から20円もらう(4+2) # # dp[4][0] = 10 370円の状態から追加で6円払う(7+6) vs 380円の状態から4円もらう(6+4) # dp[4][1] = 9 370円の状態から追加で7円払う(7+7) vs 380円の状態から3円もらう(6+3) for i, ch in enumerate(N_str): if i == 0: dp[0][0] = 0 dp[0][1] = 1 else: dp[i][0] = min(dp[i - 1][0] + int(ch), dp[i - 1][1] + 10 - int(ch)) dp[i][1] = min(dp[i - 1][0] + int(ch) + 1, dp[i - 1][1] + 9 - int(ch)) return dp[len(N_str) - 1][0] def wrong(N_str): """最初に間違って書いた貪欲 1の位から見ていき、0~4なら支払い、5~9なら10払ってお釣りをもらう N = 65のとき、 この関数だと70払って5お釣りをもらうことになるが 実際は100払って35お釣りをもらうべきなので誤り """ N_str = N_str[::-1] ans = 0 prev = 0 for ch in N_str: n = int(ch) n += prev if n <= 5: ans += n prev = 0 else: ans += 10 - n prev = 1 ans += prev return ans # for n in range(1, 101): # print() # n_str = str(n) # w = wrong(n_str) # gt = solve(n_str) # if w != gt: # print("n, gt, wrong = ", n, gt, w) N_str = input() print(solve(N_str))
''' 自宅用PCでの解答 ''' import math #import numpy as np import itertools import queue import bisect from collections import deque,defaultdict import heapq as hpq from sys import stdin,setrecursionlimit #from scipy.sparse.csgraph import dijkstra #from scipy.sparse import csr_matrix ipt = stdin.readline setrecursionlimit(10**7) mod = 10**9+7 # mod = 998244353 dir = [(-1,0),(0,-1),(1,0),(0,1)] alp = "abcdefghijklmnopqrstuvwxyz" INF = 1<<32-1 # INF = 10**18 def main(): ans = 0 n = input() dp = [0,1] #0,1多く入れる for i in n: ni = int(i) pdp = dp*1 dp[0] = min(pdp[0]+ni,pdp[1]+(10-ni)) dp[1] = min(pdp[0]+ni+1,pdp[1]+(10-ni-1)) print(dp[0]) return None if __name__ == '__main__': main()
1
70,697,884,684,292
null
219
219
h1, m1, h2, m2, k = map(int, input().split()) h_dif = h2 - h1 m_dif = m2 - m1 time = 60*h_dif + m_dif print(time - k)
A,B = map(int,input().split()) if A<=9 and B<=9:print(A*B) else : print(-1)
0
null
87,759,126,035,490
139
286
n=int(input()); print(10-(n//200));
from math import * H = int(input()) print(2**(int(log2(H))+1)-1)
0
null
43,270,836,943,840
100
228
n=int(input()) s,t=input().split() ans='' for i in range(2*n): ans=ans+(s[i//2] if i%2==0 else t[(i-1)//2]) print(ans)
N = input() S,T = input().split() txt = '' for si,ti in zip(S,T): txt += si+ti print(txt)
1
112,566,345,110,158
null
255
255
X = int(input()) yen_500 = X // 500 yen_5 = (X % 500) // 5 print(yen_500 * 1000 + yen_5 * 5)
x = int(input()) answer = 0 answer = (x//500)*1000 x = x%500 answer += (x//5)*5 print(answer)
1
42,863,544,509,370
null
185
185
INF = int(1e18) def merge(A, left, mid, right): n1 = mid - left n2 = right - mid L = [A[left + i] for i in range(n1)] R = [A[mid + i] for i in range(n2)] L.append(INF) R.append(INF) i, j = 0, 0 count = 0 for k in range(left, right): count += 1 if L[i] <= R[j]: A[k] = L[i] i += 1 else: A[k] = R[j] j += 1 return count def merge_sort(A, left, right): if left + 1 < right: mid = (left + right) // 2 c1 = merge_sort(A, left, mid) c2 = merge_sort(A, mid, right) c = merge(A, left, mid, right) return c + c1 + c2 else: return 0 if __name__ == '__main__': n = int(input()) A = list(map(int, input().split())) c = merge_sort(A, 0, n) print(" ".join(map(str, A))) print(c)
INF = 10 ** 10 def merge(A, left, mid, right): count = 0 L = A[left:mid] + [INF] R = A[mid:right] + [INF] i, j = 0, 0 for k in range(left, right): count += 1 if L[i] <= R[j]: A[k] = L[i] i += 1 else: A[k] = R[j] j += 1 return count def merge_sort(A, left, right): if left + 1 < right: mid = (left + right) // 2 countL = merge_sort(A, left, mid) countR = merge_sort(A, mid, right) return merge(A, left, mid, right) + countL + countR return 0 N = int(input()) A = list(map(int, input().split())) count = merge_sort(A, 0, N) A = list(map(str, A)) print(' '.join(A)) print(count)
1
108,927,765,282
null
26
26
import sys (n, m, l) = [int(i) for i in sys.stdin.readline().split()] a = [] for i in range(n): a.append([int(i) for i in sys.stdin.readline().split()]) b = [] for i in range(m): b.append([int(j) for j in sys.stdin.readline().split()]) for i in range(n): row = [] for j in range(l): c_ij = 0 for k in range(m): c_ij += a[i][k] * b[k][j] row.append(str(c_ij)) print(" ".join(row))
class Matrix: def __init__(self, rows, cols, values=None): self.rows = rows self.cols = cols if values is not None: self.values = values else: row = [0 for i in range(cols)] self.values = [row[:] for j in range(rows)] def row(self, i): return self.values[i-1] def col(self, j): return [row[j-1] for row in self.values] def __mul__(self, other): assert self.cols == other.rows m = self.__class__(self.rows, other.cols) for i in range(self.rows): for j in range(other.cols): m[i, j] = sum(a * b for (a, b) in zip(self.row(i), other.col(j))) return m def __getitem__(self, idx): row, col = idx return self.values[row-1][col-1] def __setitem__(self, idx, val): row, col = idx self.values[row-1][col-1] = val def __str__(self): s = [] for row in self.values: s.append(" ".join([str(i) for i in row])) return "\n".join(s) def run(): n, m, l = [int(v) for v in input().split()] m1 = [] for i in range(n): m1.append([int(v) for v in input().split()]) m2 = [] for i in range(m): m2.append([int(v) for v in input().split()]) print(Matrix(n, m, m1) * Matrix(m, l, m2)) run()
1
1,429,035,916,920
null
60
60
from bisect import bisect_left from itertools import accumulate def resolve(): def getCount(x): count = 0 for Ai in A: idx = bisect_left(A, x - Ai) count += N - idx return count >= M N, M = map(int, input().split()) A = sorted(map(int, input().split())) A_r = A[::-1] B = [0] + list(accumulate(A_r)) MIN = 0 MAX = 2 * 10 ** 5 + 1 while MAX - MIN > 1: MID = (MIN + MAX) // 2 if getCount(MID): MIN = MID else: MAX = MID ans = 0 count = 0 for Ai in A_r: idx = bisect_left(A, MIN - Ai) ans += Ai * (N - idx) + B[N - idx] count += N - idx print(ans - (count - M) * MIN) if __name__ == "__main__": resolve()
from numpy import* n,m,*a=int_(open(0).read().split()) a=int_(fft.irfft(fft.rfft(bincount(a,[1]*n,2**18))**2)+.5) c=0 for i in where(a>0)[0][::-1]: t=min(m,a[i]) c+=i*t m-=t if m<1:break print(c)
1
107,784,914,180,560
null
252
252
n = int(input()) s, t = [], [] for i in range(n): ss = input().split() s.append(ss[0]) t.append(int(ss[1])) x = input() start = n for i in range(n): if s[i] == x: start = i + 1 res = 0 for i in range(start, n): res += t[i] print(res)
n = int(input()) s = [] t = [] ans = 0 for i in range(n): s1,t1 = input().split() s.append(str(s1)) t.append(int(t1)) x = str(input()) for j in range(n): if s[j] == x: print(sum(t[j+1:n+1]))
1
97,166,350,039,722
null
243
243
n,k = map(int,input().split()) p = list(map(int,input().split())) ps = [p[0]] + [0]*(n-1) for i in range(1,n): ps[i] = ps[i-1] + p[i] maxs = ps[k-1] for i in range(1,n-k+1): maxs = max(maxs,ps[i+k-1]-ps[i-1]) print((maxs+k)/2)
X = int(input()) num1000 = X // 500 r500 = X % 500 num5 = r500 // 5 print(num1000 * 1000 + num5 * 5)
0
null
58,721,833,837,840
223
185
x = int(input()) if 360 % x == 0: print(360//x) else: for i in range(1, 10000): if x*i % 360 == 0: print(i) exit()
import sys X = int(input()) for i in range(1,180): if int(360*i/X) == 360*i/X: print(int(360*i/X)) sys.exit()
1
13,102,761,929,930
null
125
125
a, b, c =map(int, input().split()) d=(a-1)//c e=b//c ans=e-d print(ans)
import sys sys.setrecursionlimit(10**7) readline = sys.stdin.buffer.readline def readstr():return readline().rstrip().decode() def readstrs():return list(readline().decode().split()) def readint():return int(readline()) def readints():return list(map(int,readline().split())) def printrows(x):print('\n'.join(map(str,x))) def printline(x):print(' '.join(map(str,x))) from operator import itemgetter n = readint() a = [] for i in range(n): x,l = readints() a.append([x-l,x+l]) a.sort(key = itemgetter(1)) ans = 1 now = a[0][1] for l,r in a[1:]: if now<=l: ans += 1 now = r print(ans)
0
null
48,653,868,613,566
104
237
s,t = list(map(str, input().split())) print(t+s)
import sys import decimal # 10進法に変換,正確な計算 def input(): return sys.stdin.readline().strip() def main(): s, t = input().split() print(t, end="") print(s) main()
1
103,161,315,365,882
null
248
248
__author__ = 'CIPHER' _project_ = 'PythonLehr' class BoundingBox: def __init__(self, width, height): self.width = width; self.height = height; def CalculateBox(self, x, y, r): if x>= r and x<=(self.width-r) and y>=r and y<=(self.height-r): print("Yes") else: print("No") ''' width = int(input("Width of the Box: ")) height = int(input("Height of the Box: ")) x = int(input("location of x: ")) y = int(input("location of y: ")) r = int(input("radius of the circle; ")) ''' # alternative input inputList = input() inputList = inputList.split(' ') inputList = [int(x) for x in inputList] # print(inputList) width = inputList[0] height = inputList[1] x = inputList[2] y = inputList[3] r = inputList[4] Box = BoundingBox(width, height) Box.CalculateBox(x, y, r)
from collections import defaultdict, deque, Counter from heapq import heappush, heappop, heapify import bisect import random from itertools import permutations, accumulate, combinations, product import sys import string from bisect import bisect_left, bisect_right from math import factorial, ceil, floor, acos, asin, atan, sqrt, tan, cos, pi from operator import mul from functools import reduce from pprint import pprint sys.setrecursionlimit(10 ** 7) INF = 10 ** 20 def LI(): return list(map(int, sys.stdin.readline().split())) def I(): return int(sys.stdin.readline()) def LS(): return sys.stdin.readline().split() def S(): return sys.stdin.readline().strip() def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] def SRL(n): return [list(S()) for i in range(n)] def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)] mod = 10 ** 9 + 7 n = I() plus = [] minus = [] for _ in range(n): s = S() ret = 0 ret2 = 0 for i in s: if ret and i == ')': ret -= 1 elif i == '(': ret += 1 else: ret2 += 1 if ret2 > ret: plus += [(ret, ret2)] else: minus += [(ret, ret2)] plus.sort() minus.sort(key=lambda x:x[1], reverse=True) L = plus + minus now = 0 for x, y in L: now -= x if now < 0: print("No") exit() now += y if now: print("No") else: print("Yes")
0
null
12,061,016,114,180
41
152
for i in range(1, 10): for j in range(1, 10): print (u"%dx%d=%d"%(i,j,i*j))
t = int(input()) print(str(t//3600)+':'+str(t%3600//60)+':'+str(t%60))
0
null
160,103,451,482
1
37
def main(): from operator import itemgetter N = int(input()) a = map(int, input().split()) *ea, = enumerate(a, 1) ea.sort(key=itemgetter(1), reverse=True) dp = [[0] * (N + 1) for _ in range(N + 1)] # dp[left][right]:=活発な幼児から順に左端からleft,右端からright並べた際の最大うれしさ for i, (p, x) in enumerate(ea, 1): for left in range(i + 1): right = i - left if left > 0: dp[left][right] = max( dp[left][right], dp[left - 1][right] + x * (p - left) ) if right > 0: dp[left][right] = max( dp[left][right], dp[left][right - 1] + x * (N - right + 1 - p) ) ans = max(dp[i][N - i] for i in range(N + 1)) print(ans) if __name__ == '__main__': main()
#from statistics import median #import collections #aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0] from math import gcd from itertools import combinations,permutations,accumulate, product # (string,3) 3回 #from collections import deque from collections import deque,defaultdict,Counter import decimal import re import math import bisect import heapq # # # # pythonで無理なときは、pypyでやると正解するかも!! # # # my_round_int = lambda x:np.round((x*2 + 1)//2) # 四捨五入g # # インデックス系 # int min_y = max(0, i - 2), max_y = min(h - 1, i + 2); # int min_x = max(0, j - 2), max_x = min(w - 1, j + 2); # # import sys sys.setrecursionlimit(10000000) mod = 10**9 + 7 #mod = 9982443453 #mod = 998244353 INF = float('inf') from sys import stdin readline = stdin.readline def readInts(): return list(map(int,readline().split())) def readTuples(): return tuple(map(int,readline().split())) def I(): return int(readline()) H,W,M = readInts() dic1 = Counter() dic2 = Counter() s = set() for i in range(M): h,w = map(lambda x:int(x)-1, input().split()) dic1[h] += 1 dic2[w] += 1 s.add((h,w)) # print(dic) ans = 0 # 重なっているもので最大値がある時もあれば # 行、列の交差点でボムなしが一番大きいものがある for h,w in s: ans = max(ans, dic1[h] + dic2[w] - 1) dic1 = dic1.most_common() dic2 = dic2.most_common() max1 = dic1[0][1] max2 = dic2[0][1] for k1,v1 in dic1: if v1 < max1: break # continueする必要がない most_commonで大きい方から集めてるので for k2,v2 in dic2: if v2 < max2: break # 同じく if (k1,k2) in s: # 一度計算したもの continue # 両方とも最大であればok ans = max(ans, v1 + v2) break print(ans)
0
null
19,203,764,688,400
171
89
a,b,c,k=[int(x) for x in input().split()] ans=0 if a<=k: ans+=a k-=a elif k>0: ans+=k k=0 if b<=k: k-=b elif k>0: k=0 if k>0: ans-=k print(ans)
a, b = map(int, input().split()) ans = '' if a <= b: for i in range(b): ans = ans + str(a) else: for i in range(a): ans = ans + str(b) print(ans)
0
null
53,229,469,407,488
148
232
import math n=int(input()) s=0 for i in range(n): a=int(input()) #b=int(a**0.5) b=math.ceil(a**0.5) #print(str(b), '??O') if a==2: s+=1 # print(str(a),'!!!!') elif a%2==0 or a < 2: continue else: j=3 c=0 while j <= b: if a%j==0: c+=1 break j+=2 if c==0: s+=1 """ for j in range(3,b+1,2): if a%j==0: break if j==b: s+=1 print(str(a),'!!!!') """ print(s)
import math N = int(input()) Z = [int(input()) for i in range(N)] ctr = 0 for j in range(N): if Z[j] == 2: ctr += 1 elif Z[j] < 2 or (Z[j] % 2) == 0: pass else: Up = math.sqrt(Z[j]) k = 3 while k<= Up: if (Z[j] % k) == 0: break else: k += 1 else: ctr += 1 print(ctr)
1
10,137,168,380
null
12
12
n=int(input()) cs = [ (s,k) for s in ['S','H','C','D'] for k in range(1,14) ] for _ in range(n): s,k=input().split() cs.remove((s,int(k))) for (s, k) in cs: print(s, k)
import sys sr = lambda: sys.stdin.readline().rstrip() ir = lambda: int(sr()) lr = lambda: list(map(int, sr().split())) N, M = lr() S = list(sr())[::-1] answer = [] count = 0 cur = 0 while True: next = cur for i in range(M, 0, -1): if cur + i > N: continue if S[cur+i] == '0': count += 1 next = cur + i answer.append(i) break else: continue if next == cur: print(-1); exit() cur = next if next == N: break answer = answer[::-1] print(*answer)
0
null
70,369,637,392,802
54
274
# PDF参考 # スタックS1 S1 = [] # スタックS2 S2 = [] tmp_total = 0 counter = 0 upper = 0 # 総面積 total_layer = 0 for i,symbol in enumerate(input()): if (symbol == '\\'): S1.append(i) elif (symbol == '/') and S1: i_p = S1.pop() total_layer = i - i_p if S2 : while S2 and S2[-1][0] > i_p: cal = S2.pop() total_layer += cal[1] S2.append((i, total_layer)) print(sum([j[1] for j in S2])) if (len(S2) != 0): print(str(len(S2)) + ' ' + ' '.join([str(i[1]) for i in S2])) else: print(str(len(S2)))
# -*- coding:utf-8 -*- import sys def cross_section_diagram(diagram): stack = [] area = [] for i, ch in enumerate(diagram): if ch == "\\": stack.append(i) elif ch == "/": if stack: point = stack.pop() area.insert(0, (point, i - point)) else: pass result = [] if area: p1, cnt1 = area.pop(0) for p2, cnt2 in area: if p1 < p2: cnt1 += cnt2 else: result.insert(0, cnt1) p1 = p2 cnt1 = cnt2 result.insert(0, cnt1) print(sum(result)) result.insert(0, len(result)) print(" ".join([str(n)for n in result])) return result if __name__ == "__main__": diagram = [val for val in sys.stdin.read().splitlines()] cross_section_diagram(diagram[0])
1
58,362,904,898
null
21
21
# # # author : samars_diary # # 16-09-2020 │ 18:28:11 # # # import sys, os.path, math #if(os.path.exists('input.txt')): #sys.stdin = open('input.txt',"r") #sys.stdout = open('output.txt',"w") sys.setrecursionlimit(10 ** 5) mod = 10**9+7 def i(): return sys.stdin.readline().strip() def ii(): return int(sys.stdin.readline()) def li(): return list(sys.stdin.readline().strip()) def mii(): return map(int, sys.stdin.readline().split()) def lii(): return list(map(int, sys.stdin.readline().strip().split())) #print=sys.stdout.write def solve(): a=ii();print((pow(10,a,mod)+pow(8,a,mod)-2*pow(9,a,mod))%mod) for _ in range(1): solve()
m=10**9+7 n=int(input()) a=(8**n)%m b=(9**n)%m c=(10**n)%m print((c-2*b+a)%m)
1
3,181,588,377,590
null
78
78
#!/usr/bin/env python3 import numpy as np # def input(): # return sys.stdin.readline().rstrip() def main(): n, k = map(int, input().split()) warps = list(map(int, input().split())) warps = [0] + warps warps = np.array(warps, dtype=int) dp = np.zeros((k.bit_length() + 1, n + 1), dtype=int) dp[0, :] = warps for h in range(1, len(dp)): # dp[h] = dp[h - 1][dp[h - 1]] dp[h] = np.take(dp[h - 1], dp[h - 1]) node = 1 # for i in reversed(range(k.bit_length())): for i in range(k.bit_length(), -1, -1): if k >> i & 1: node = dp[i][node] print(node) main()
n, k = map(int, input().split()) a = list(map(lambda x: int(x) - 1, input().split())) ls = [-1] * n now = 0 cnt = 0 path = [] while 1: if ls[now] != -1: b = ls[now] break ls[now] = cnt path.append(now) now = a[now] cnt += 1 loop = path[b:] if k <= b: print(path[k] + 1) else: print(loop[(k - b) % len(loop)] + 1)
1
22,737,924,381,608
null
150
150