id
stringlengths
24
27
content
stringlengths
37
384k
max_stars_repo_path
stringlengths
51
51
condefects-python_data_1
import sys def main(): input = sys.stdin.readline N = int(input()) P = [int(p) for p in input().strip().split()] ans = [] def calc1(i): if i <= 2: return True if P[i-3] % 2 != (i-2) % 2: return True ans.append(('B', i-1)) P[i-3], P[i-1] = P[i-1], P[i-3] calc1(i-2) return True def calc2(): for i in range(1, N, 2): if P[i-1] % 2 != i % 2: ans.append(('A', i)) P[i-1], P[i] = P[i], P[i-1] else: return True def calc3(n): i = N while P[i-1] != n: i -= 1 while i != n: ans.append(('B', i-2)) P[i-3], P[i-1] = P[i-1], P[i-3] i -= 2 return True for i in range(1, N+1): if P[i-1] % 2 != i % 2: calc1(i) calc2() for n in range(1, N+1): calc3(n) print(len(ans)) for a in ans: print(*a) if __name__ == '__main__': main() import sys def main(): input = sys.stdin.readline N = int(input()) P = [int(p) for p in input().strip().split()] ans = [] def calc1(i): if i <= 2: return True if P[i-3] % 2 != (i-2) % 2: return True ans.append(('B', i-2)) P[i-3], P[i-1] = P[i-1], P[i-3] calc1(i-2) return True def calc2(): for i in range(1, N, 2): if P[i-1] % 2 != i % 2: ans.append(('A', i)) P[i-1], P[i] = P[i], P[i-1] else: return True def calc3(n): i = N while P[i-1] != n: i -= 1 while i != n: ans.append(('B', i-2)) P[i-3], P[i-1] = P[i-1], P[i-3] i -= 2 return True for i in range(1, N+1): if P[i-1] % 2 != i % 2: calc1(i) calc2() for n in range(1, N+1): calc3(n) print(len(ans)) for a in ans: print(*a) if __name__ == '__main__': main()
ConDefects/ConDefects/Code/arc147_b/Python/42082334
condefects-python_data_2
N = int(input()) P = list(map(int, input().split())) A = list(range(1,N+1)) ans = [] B = [[] for _ in range(2)] for i, p in enumerate(P): if (p - i) % 2 == 0: B[i % 2].append(i) for i in range(len(B[0])): b0, b1 = B[0][i], B[1][i] if b0 > b1: b0, b1 = b1, b0 while b0 + 1 < b1: ans.append(("B", b1 - 2)) P[b1 - 2], P[b1] = P[b1], P[b1 - 2] b1 -= 2 ans.append(("A", b0 + 1)) P[b0], P[b0 + 1] = P[b0 + 1], P[b0] def e_sort(): flag = False for i in range(0, N - 2, 2): if P[i] > P[i + 2]: P[i], P[i + 2] = P[i + 2], P[i] ans.append(("B", i + 1)) flag = True return flag def o_sort(): flag = False for i in range(1, N - 2, 2): if P[i] > P[i + 2]: P[i], P[i + 2] = P[i + 2], P[i] ans.append(("B", i + 1)) flag = True return flag os = True es = True while P != A: if es: es = e_sort() if os: os = o_sort() print(len(ans)) for a in ans: print(*a) N = int(input()) P = list(map(int, input().split())) A = list(range(1,N+1)) ans = [] B = [[] for _ in range(2)] for i, p in enumerate(P): if (p - i) % 2 == 0: B[i % 2].append(i) for i in range(len(B[0])): b0, b1 = B[0][i], B[1][i] if b0 > b1: b0, b1 = b1, b0 while b0 + 1 < b1: ans.append(("B", b1 - 1)) P[b1 - 2], P[b1] = P[b1], P[b1 - 2] b1 -= 2 ans.append(("A", b0 + 1)) P[b0], P[b0 + 1] = P[b0 + 1], P[b0] def e_sort(): flag = False for i in range(0, N - 2, 2): if P[i] > P[i + 2]: P[i], P[i + 2] = P[i + 2], P[i] ans.append(("B", i + 1)) flag = True return flag def o_sort(): flag = False for i in range(1, N - 2, 2): if P[i] > P[i + 2]: P[i], P[i + 2] = P[i + 2], P[i] ans.append(("B", i + 1)) flag = True return flag os = True es = True while P != A: if es: es = e_sort() if os: os = o_sort() print(len(ans)) for a in ans: print(*a)
ConDefects/ConDefects/Code/arc147_b/Python/45545981
condefects-python_data_3
N = int(input()) A = list(map(int, input().split())) st = ['o' if A[i]%2 == (i+1)%2 else 'x' for i in range(N)] ng = st.count(0) ans = [] for i in range(N): flag = False if st[i] == 'o': q = [i] for j in range(i+2, N, 2): if st[j] == 'o': q.append(j) else: flag = True break if not flag: break while q: j = q.pop(-1) A[j], A[j+2] = A[j+2], A[j] st[j], st[j+2] = st[j+2], st[j] ans.append(f'B {j+1}') for i in range(0, N, 2): if st[i] == 'o': break A[i], A[i+1] = A[i+1], A[i] ans.append(f'A {i+1}') for i in range(N, 0, -1): p = A.index(i) for j in range(i, p-1, 2): A[j], A[j+2] = A[j+2], A[j] ans.append(f'B {j+1}') print(len(ans)) for a in ans: print(a) N = int(input()) A = list(map(int, input().split())) st = ['o' if A[i]%2 == (i+1)%2 else 'x' for i in range(N)] ng = st.count(0) ans = [] for i in range(N): flag = False if st[i] == 'o': q = [i] for j in range(i+2, N, 2): if st[j] == 'o': q.append(j) else: flag = True break if not flag: break while q: j = q.pop(-1) A[j], A[j+2] = A[j+2], A[j] st[j], st[j+2] = st[j+2], st[j] ans.append(f'B {j+1}') for i in range(0, N, 2): if st[i] == 'o': break A[i], A[i+1] = A[i+1], A[i] ans.append(f'A {i+1}') for i in range(N, 0, -1): p = A.index(i) for j in range(p, i-1, 2): A[j], A[j+2] = A[j+2], A[j] ans.append(f'B {j+1}') print(len(ans)) for a in ans: print(a)
ConDefects/ConDefects/Code/arc147_b/Python/39591338
condefects-python_data_4
MOD, MAXN = 998244353, 2 * pow(10, 5) N = int(input()) p, inv = [1] * (MAXN + 10), [1] * (MAXN + 10) p[1] = 2 for i in range(2, MAXN + 1): p[i] = p[i - 1] * 2 % MOD inv[i] = -(MOD // i) * inv[MOD % i] % MOD ans, res = 2, 1 for i in range(1, N + 1): res = res * (p[N] - p[i - 1] + MOD) % MOD * inv[i] % MOD ans = (ans + res * (p[i] * inv[i + 1] + 1) % MOD) % MOD print(ans) MOD, MAXN = 998244353, 2 * pow(10, 5) N = int(input()) p, inv = [1] * (MAXN + 10), [1] * (MAXN + 10) p[1] = 2 for i in range(2, MAXN + 5): p[i] = p[i - 1] * 2 % MOD inv[i] = -(MOD // i) * inv[MOD % i] % MOD ans, res = 2, 1 for i in range(1, N + 1): res = res * (p[N] - p[i - 1] + MOD) % MOD * inv[i] % MOD ans = (ans + res * (p[i] * inv[i + 1] + 1) % MOD) % MOD print(ans)
ConDefects/ConDefects/Code/arc146_c/Python/34191586
condefects-python_data_5
def factorization(n): arr = [] temp = n for i in range(2, int(-(-n**0.5//1))+1): if temp%i==0: cnt=0 while temp%i==0: cnt+=1 temp //= i arr.append([i, cnt]) if temp!=1: arr.append([temp, 1]) if arr==[]: arr.append([n, 1]) return arr n=int(input()) A=list(map(int,input().split())) L=[] s=set() for i in range(n): if A[i] not in s: L.append(A[i]) s.add(A[i]) L=sorted(L) ans=2 first=0 k=0 if len(L)!=1: first=L[1]-L[0] p=factorization(first) k=len(p) else: ans=1 for i in range(k): if ans==1: break for j in range(2,len(L)): other=A[j]-A[0] if other%p[i][0]!=0: break if j==len(L)-1: ans=1 if first==1: print(2) elif len(L)==2: print(1) else: print(ans) def factorization(n): arr = [] temp = n for i in range(2, int(-(-n**0.5//1))+1): if temp%i==0: cnt=0 while temp%i==0: cnt+=1 temp //= i arr.append([i, cnt]) if temp!=1: arr.append([temp, 1]) if arr==[]: arr.append([n, 1]) return arr n=int(input()) A=list(map(int,input().split())) L=[] s=set() for i in range(n): if A[i] not in s: L.append(A[i]) s.add(A[i]) L=sorted(L) ans=2 first=0 k=0 if len(L)!=1: first=L[1]-L[0] p=factorization(first) k=len(p) else: ans=1 for i in range(k): if ans==1: break for j in range(2,len(L)): other=L[j]-L[0] if other%p[i][0]!=0: break if j==len(L)-1: ans=1 if first==1: print(2) elif len(L)==2: print(1) else: print(ans)
ConDefects/ConDefects/Code/arc148_a/Python/44882552
condefects-python_data_6
from math import gcd n = int(input()) a = list(map(int,input().split())) g = 0 for i in range (n-1): g = gcd(g,abs(a[i]-a[i+1])) if g >= 2: ans = 1 else: ans = 2 print(ans) from math import gcd n = int(input()) a = list(map(int,input().split())) g = 0 for i in range (n-1): g = gcd(g,abs(a[i]-a[i+1])) if g >= 2 or g == 0: ans = 1 else: ans = 2 print(ans)
ConDefects/ConDefects/Code/arc148_a/Python/42795362
condefects-python_data_7
import math n=int(input()) a=list(map(int,input().split())) a=list(set(a)) a.sort() l=len(a) x=a[0] for i in range(l): a[i]-=x x=a[-1] for i in range(2,l): x = math.gcd(a[i],x) if x==1: print(2) else: print(1) import math n=int(input()) a=list(map(int,input().split())) a=list(set(a)) a.sort() l=len(a) x=a[0] for i in range(l): a[i]-=x x=a[-1] for i in range(1,l): x = math.gcd(a[i],x) if x==1: print(2) else: print(1)
ConDefects/ConDefects/Code/arc148_a/Python/44660957
condefects-python_data_8
from math import gcd n = int(input()) a = list(map(int, input().split())) for i in range(n - 1): if a[i] == a[i + 1]: exit(print(1)) g = abs(a[0] - a[1]) for i in range(1, n - 1): g = gcd(g, abs(a[1] - a[2])) print(2 if g == 1 else 1) from math import gcd n = int(input()) a = list(map(int, input().split())) for i in range(n - 1): if a[i] == a[i + 1]: exit(print(1)) g = abs(a[0] - a[1]) for i in range(1, n - 1): g = gcd(g, abs(a[i] - a[i + 1])) print(2 if g == 1 else 1)
ConDefects/ConDefects/Code/arc148_a/Python/44544084
condefects-python_data_9
from math import gcd import os import sys import numpy as np def solve(inp): n = inp[0] m = inp[1] ddd = inp[2:] MOD = 998244353 n2 = 1 << n INF = 1 << 60 def pop_count_32(n): c = (n & 0x55555555) + ((n >> 1) & 0x55555555) c = (c & 0x33333333) + ((c >> 2) & 0x33333333) c = (c & 0x0f0f0f0f) + ((c >> 4) & 0x0f0f0f0f) c = (c & 0x00ff00ff) + ((c >> 8) & 0x00ff00ff) c = (c & 0x0000ffff) + ((c >> 16) & 0x0000ffff) return c def bit_length(n): l = 0 while n: n >>= 1 l += 1 return l lcms = np.ones(n2, np.int64) all_same = np.ones(n2, np.int64) pop_count = np.zeros(n2, np.int64) for i in range(1, n2): lsb = i & -i ot = lcms[i ^ lsb] if ot == INF: lcms[i] = INF else: d = ddd[bit_length(lsb) - 1] g = gcd(ot, d) if INF // ot <= d // g: lcms[i] = INF else: lcms[i] = d // g * ot all_same[i] = (m // lcms[i]) % MOD pop_count[i] = pop_count_32(i) facts = np.ones(n + 1, np.int64) for i in range(2, n + 1): facts[i] = facts[i - 1] * i % MOD dp = np.zeros(n2, np.int64) dp[0] = 1 for i in range(1, n2): lsb = i & -i j = i ^ lsb k = j res = all_same[lsb] * dp[j] % MOD while k: p = pop_count[k] if p % 2 == 0: res += all_same[k | lsb] * facts[p] % MOD * dp[j ^ k] % MOD # print(f'+ i:{i:04b} j:{j:04b} k:{k:04b} p:{p}', all_same[k | lsb], facts[p], dp[j ^ k]) else: res -= all_same[k | lsb] * facts[p] % MOD * dp[j ^ k] % MOD # print(f'- i:{i:04b} j:{j:04b} k:{k:04b} p:{p}', all_same[k | lsb], facts[p], dp[j ^ k]) res %= MOD k = (k - 1) & j dp[i] = res # print(dp) return dp[-1] SIGNATURE = '(i8[:],)' if sys.argv[-1] == 'ONLINE_JUDGE': from numba.pycc import CC cc = CC('my_module') cc.export('solve', SIGNATURE)(solve) cc.compile() exit() if os.name == 'posix': # noinspection PyUnresolvedReferences from my_module import solve else: from numba import njit solve = njit(SIGNATURE, cache=True)(solve) print('compiled', file=sys.stderr) inp = np.fromstring(sys.stdin.read(), dtype=np.int64, sep=' ') ans = solve(inp) print(ans) from math import gcd import os import sys import numpy as np def solve(inp): n = inp[0] m = inp[1] ddd = inp[2:] MOD = 998244353 n2 = 1 << n INF = 1 << 60 def pop_count_32(n): c = (n & 0x55555555) + ((n >> 1) & 0x55555555) c = (c & 0x33333333) + ((c >> 2) & 0x33333333) c = (c & 0x0f0f0f0f) + ((c >> 4) & 0x0f0f0f0f) c = (c & 0x00ff00ff) + ((c >> 8) & 0x00ff00ff) c = (c & 0x0000ffff) + ((c >> 16) & 0x0000ffff) return c def bit_length(n): l = 0 while n: n >>= 1 l += 1 return l lcms = np.ones(n2, np.int64) all_same = np.ones(n2, np.int64) pop_count = np.zeros(n2, np.int64) for i in range(1, n2): lsb = i & -i ot = lcms[i ^ lsb] if ot == INF: lcms[i] = INF else: d = ddd[bit_length(lsb) - 1] g = gcd(ot, d) if float(INF) / d <= ot // g: lcms[i] = INF else: lcms[i] = d // g * ot all_same[i] = (m // lcms[i]) % MOD pop_count[i] = pop_count_32(i) facts = np.ones(n + 1, np.int64) for i in range(2, n + 1): facts[i] = facts[i - 1] * i % MOD dp = np.zeros(n2, np.int64) dp[0] = 1 for i in range(1, n2): lsb = i & -i j = i ^ lsb k = j res = all_same[lsb] * dp[j] % MOD while k: p = pop_count[k] if p % 2 == 0: res += all_same[k | lsb] * facts[p] % MOD * dp[j ^ k] % MOD # print(f'+ i:{i:04b} j:{j:04b} k:{k:04b} p:{p}', all_same[k | lsb], facts[p], dp[j ^ k]) else: res -= all_same[k | lsb] * facts[p] % MOD * dp[j ^ k] % MOD # print(f'- i:{i:04b} j:{j:04b} k:{k:04b} p:{p}', all_same[k | lsb], facts[p], dp[j ^ k]) res %= MOD k = (k - 1) & j dp[i] = res # print(dp) return dp[-1] SIGNATURE = '(i8[:],)' if sys.argv[-1] == 'ONLINE_JUDGE': from numba.pycc import CC cc = CC('my_module') cc.export('solve', SIGNATURE)(solve) cc.compile() exit() if os.name == 'posix': # noinspection PyUnresolvedReferences from my_module import solve else: from numba import njit solve = njit(SIGNATURE, cache=True)(solve) print('compiled', file=sys.stderr) inp = np.fromstring(sys.stdin.read(), dtype=np.int64, sep=' ') ans = solve(inp) print(ans)
ConDefects/ConDefects/Code/abc236_h/Python/28807713
condefects-python_data_10
N,M=map(int,input().split()) L=list(map(int,input().split())) def isok(n): dist=-1 count=0 for i in range(N): if L[i]>n: return False if dist+1+L[i]>n: dist=-1 count+=1 dist+=(1+L[i]) if count+1<=M: return True return False left,right=0,10**10 while right-left>1: mid=(right+left)//2 if isok(mid): right=mid else: left=mid print(right) N,M=map(int,input().split()) L=list(map(int,input().split())) def isok(n): dist=-1 count=0 for i in range(N): if L[i]>n: return False if dist+1+L[i]>n: dist=-1 count+=1 dist+=(1+L[i]) if count+1<=M: return True return False left,right=0,10**15 while right-left>1: mid=(right+left)//2 if isok(mid): right=mid else: left=mid print(right)
ConDefects/ConDefects/Code/abc319_d/Python/45752844
condefects-python_data_11
import sys input = lambda: sys.stdin.readline().rstrip() INF = 10**18 N,M = map(int,input().split()) L = list(map(int,input().split())) def is_ok(arg): nrow = 1 x = 0 for i in range(N): if x == 0: if x + L[i] <= arg: x += L[i] else: nrow += 1 x = L[i] else: if x + L[i] + 1 <= arg: x += L[i]+1 else: nrow += 1 x = L[i] if nrow <= M: return True else: return False def meguru_bisect(ng, ok): ''' 初期値のng,okを受け取り,is_okを満たす最小(最大)のokを返す まずis_okを定義すべし ng ok は とり得る最小の値-1 とり得る最大の値+1 最大最小が逆の場合はよしなにひっくり返す ''' while (abs(ok - ng) > 1): mid = (ok + ng) // 2 if is_ok(mid): ok = mid else: ng = mid return ok ans = meguru_bisect(0,10**18) print(ans) import sys input = lambda: sys.stdin.readline().rstrip() INF = 10**18 N,M = map(int,input().split()) L = list(map(int,input().split())) def is_ok(arg): nrow = 1 x = 0 for i in range(N): if L[i] > arg: return False if x == 0: if x + L[i] <= arg: x += L[i] else: nrow += 1 x = L[i] else: if x + L[i] + 1 <= arg: x += L[i]+1 else: nrow += 1 x = L[i] if nrow <= M: return True else: return False def meguru_bisect(ng, ok): ''' 初期値のng,okを受け取り,is_okを満たす最小(最大)のokを返す まずis_okを定義すべし ng ok は とり得る最小の値-1 とり得る最大の値+1 最大最小が逆の場合はよしなにひっくり返す ''' while (abs(ok - ng) > 1): mid = (ok + ng) // 2 if is_ok(mid): ok = mid else: ng = mid return ok ans = meguru_bisect(0,10**18) print(ans)
ConDefects/ConDefects/Code/abc319_d/Python/45778019
condefects-python_data_12
n, m = map(int, input().split()) word_lengths = list(map(int, input().split())) total_chars = sum(word_lengths) + (n - 1) left, right = max(max(word_lengths), (total_chars + m - 1) // m), total_chars while left < right: mid = (left + right) // 2 lines_needed, current_line_length = 1, 0 for length in word_lengths: if current_line_length + length <= mid: current_line_length += length + 1 else: lines_needed += 1 current_line_length = length + 1 if lines_needed <= m: right = mid else: left = mid + 1 print(left) n, m = map(int, input().split()) word_lengths = list(map(int, input().split())) total_chars = sum(word_lengths) + (n - 1) left, right = max(word_lengths), total_chars while left < right: mid = (left + right) // 2 lines_needed, current_line_length = 1, 0 for length in word_lengths: if current_line_length + length <= mid: current_line_length += length + 1 else: lines_needed += 1 current_line_length = length + 1 if lines_needed <= m: right = mid else: left = mid + 1 print(left)
ConDefects/ConDefects/Code/abc319_d/Python/45943488
condefects-python_data_13
import heapq import itertools from collections import deque from sys import stdin N, M = map(int, input().split()) L = list(map(int, input().split())) for i in range(N): L[i] += 1 ok = 10 ** 15 ng = -1 def check(num): wordLen = -1 wordLines = 1 for i in range(N): if wordLen+L[i] > num: wordLen = -1 wordLines += 1 wordLen += L[i] if wordLines <= M: return True else: return False while ok-ng >= 2: srch = (ng+ok)//2 c = check(srch) if c: ok = srch else: ng = srch # print(ok, ng) ans = ng+1 print(ans) import heapq import itertools from collections import deque from sys import stdin N, M = map(int, input().split()) L = list(map(int, input().split())) for i in range(N): L[i] += 1 ok = 10 ** 15 ng = max(L) - 2 def check(num): wordLen = -1 wordLines = 1 for i in range(N): if wordLen+L[i] > num: wordLen = -1 wordLines += 1 wordLen += L[i] if wordLines <= M: return True else: return False while ok-ng >= 2: srch = (ng+ok)//2 c = check(srch) if c: ok = srch else: ng = srch # print(ok, ng) ans = ng+1 print(ans)
ConDefects/ConDefects/Code/abc319_d/Python/45753057
condefects-python_data_14
n, m = map(int, input().split()) L = list(map(int, input().split())) left, right = max(L), sum(L) while left < right: mid = (left + right) // 2 cur = 0 level = 0 # print("lr", left, right, mid) flag = False for i in range(n): # print("lc", level, cur, L[i]) if L[i] > mid: left = mid + 1 flag = True break if cur + L[i] > mid: level += 1 cur = 0 cur += L[i] + 1 if level >= m: left = mid + 1 flag = True break if not flag: right = mid print(right) n, m = map(int, input().split()) L = list(map(int, input().split())) left, right = max(L), sum(L) + len(L) while left < right: mid = (left + right) // 2 cur = 0 level = 0 # print("lr", left, right, mid) flag = False for i in range(n): # print("lc", level, cur, L[i]) if L[i] > mid: left = mid + 1 flag = True break if cur + L[i] > mid: level += 1 cur = 0 cur += L[i] + 1 if level >= m: left = mid + 1 flag = True break if not flag: right = mid print(right)
ConDefects/ConDefects/Code/abc319_d/Python/45926522
condefects-python_data_15
n, m = map(int, input().split()) l = list(map(int, input().split())) def search(width: int) -> bool: length = 0 lines = 1 for e in l: if length > 0: length += 1 if length + e > width: lines += 1 length = 0 count = 0 length += e # print(width, lines) return lines <= m ok = 1 << 60 ng = max(l) while abs(ok - ng) > 1: mid = (ok + ng) // 2 if search(mid): ok = mid else: ng = mid print(ok) n, m = map(int, input().split()) l = list(map(int, input().split())) def search(width: int) -> bool: length = 0 lines = 1 for e in l: if length > 0: length += 1 if length + e > width: lines += 1 length = 0 count = 0 length += e # print(width, lines) return lines <= m ok = 1 << 60 ng = max(l) - 1 while abs(ok - ng) > 1: mid = (ok + ng) // 2 if search(mid): ok = mid else: ng = mid print(ok)
ConDefects/ConDefects/Code/abc319_d/Python/45764630
condefects-python_data_16
condition = list(map(int, input().split())) word_lengths = list(map(int, input().split())) # # 単語数 # N = condition[0] # 行数 M = condition[1] # Wの最小値(単語の長さの最大値を取得) lower = max(word_lengths) # # すべての単語をすべての行に隙間なく埋めた場合の1行の長さ # min_length = int(sum(word_lengths) / M) # Wの最大値(すべて1行に並べたときの長さ) upper = sum(word_lengths) + len(word_lengths) - 1 # for w in range(lower, upper): while lower + 1 < upper: column = 1 row_len = 0 # 2分探索 # middle = int((lower+upper)/2) middle = (lower+upper) // 2 # print(w) # for i in range(0, N): for l in word_lengths: len = l # 超えた時点で次の行にして、該当行の長さを0にする # ※ここでのwは範囲の最小値ではなく、範囲の中間地点 if row_len + len > middle: column += 1 row_len = 0 row_len += len + 1 # row_len += 1 # # 該当行が中間地点を超えていない場合空欄補充 # if middle - row_len > 1: # 横幅と同じ長さであればもちろん不要、横幅と1文字しか空いていない場合もそれ以上入れられないので空欄不要 # row_len += 1 if column > M: # 行数が超えた場合 lower = middle # 横幅が小さすぎるので、範囲の最小値をmiddleにあげる else: # 行数が超えなかった場合 upper = middle # 横幅が大きすぎるので、範囲の最大値をmiddleに下げる print(upper) condition = list(map(int, input().split())) word_lengths = list(map(int, input().split())) # # 単語数 # N = condition[0] # 行数 M = condition[1] # Wの最小値(単語の長さの最大値を取得) lower = max(word_lengths) - 1 # # すべての単語をすべての行に隙間なく埋めた場合の1行の長さ # min_length = int(sum(word_lengths) / M) # Wの最大値(すべて1行に並べたときの長さ) upper = sum(word_lengths) + len(word_lengths) - 1 # for w in range(lower, upper): while lower + 1 < upper: column = 1 row_len = 0 # 2分探索 # middle = int((lower+upper)/2) middle = (lower+upper) // 2 # print(w) # for i in range(0, N): for l in word_lengths: len = l # 超えた時点で次の行にして、該当行の長さを0にする # ※ここでのwは範囲の最小値ではなく、範囲の中間地点 if row_len + len > middle: column += 1 row_len = 0 row_len += len + 1 # row_len += 1 # # 該当行が中間地点を超えていない場合空欄補充 # if middle - row_len > 1: # 横幅と同じ長さであればもちろん不要、横幅と1文字しか空いていない場合もそれ以上入れられないので空欄不要 # row_len += 1 if column > M: # 行数が超えた場合 lower = middle # 横幅が小さすぎるので、範囲の最小値をmiddleにあげる else: # 行数が超えなかった場合 upper = middle # 横幅が大きすぎるので、範囲の最大値をmiddleに下げる print(upper)
ConDefects/ConDefects/Code/abc319_d/Python/45793373
condefects-python_data_17
N,M=map(int,input().split()) L=list(map(int,input().split())) def jadge(col_num): row_num=1 count=-1 for i in range(N): count+=L[i]+1 if count>col_num: row_num+=1 count=L[i] #print(L[i],count,row_num) return row_num def bs(): left=max(L) right=sum(L)+N+1 center=(left+right)//2 #print(left,right,center) while right-left>1: #print(M,center,jadge(center),M<=jadge(center)) if M>=jadge(center): right=min(center,right-1) center=(left+right)//2 else: left=max(center,left+1) center=(left+right)//2 return right print(bs()) """test N,M=map(int,input().split()) L=list(map(int,input().split())) cumsum_L=[L[0]] for i in range(1,N): cumsum_L.append(cumsum_L[i-1]+L[i]+1) print(cumsum_L) #print(len(cumsum_L),N,cumsum_L[-1]) def jadge(col_num): left_i=-1 right_i=N-1 #one_col_num=col_num minus=0 for i in range(M): left_i=min(N-1,left_i+1) right_i=N-1 center_i=(left_i+right_i)//2 if cumsum_L[left_i]-minus>col_num: #その行の累積和が許容できる列数より大きいとき #print(cumsum_L[left_i],minus,col_num) return False #print("col_num",col_num) while right_i-left_i>1: if cumsum_L[left_i]-minus==col_num: #print(0,right_i,cumsum_L[right_i]-minus) #print(0) #left_i=right_i break elif cumsum_L[center_i]-minus<=col_num: #print(1,left_i,center_i,right_i,cumsum_L[center_i]-minus) left_i=max(center_i,left_i+1) left_i=center_i center_i=(left_i+right_i)//2 else: #print("col_num",col_num) #print(2,left_i,center_i,right_i,cumsum_L[center_i]-minus) #right_i=center_i right_i=min(center_i,right_i-1) center_i=(left_i+right_i)//2 #col_num+=one_col_num #print(left_i,cumsum_L[left_i]) #print(i,"--",left_i,center_i,right_i) if left_i>N-1: return False minus=cumsum_L[left_i] #print("------") if cumsum_L[center_i]-minus<=col_num: return True else: return False print(jadge(188)) left_col=1 right_col=cumsum_L[-1] center_col=(left_col+right_col)//2 ans=-1 #print(right_col) #print(jadge(26)) """ """ while right_col-left_col>0: if jadge(left_col): left_col=right_col break elif jadge(center_col)==True: right_col=min(right_col-1,center_col) center_col=(left_col+right_col)//2 else: left_col=max(left_col+1,center_col) center_col=(left_col+right_col)//2 #print(left_col,center_col,right_col,jadge(center_col)) print(right_col) """ """ N, M = map(int, input().split()) L = list(map(int, input().split())) max_L = max(L)#文字の横幅の最大値 def judge(m): #もし、列の幅が文字の単語の長さより小さければFalse if max_L > m: return False now_width = 1 << 63#巨大数 cnt = 0 #単語の数だけ実施 for l in L: if now_width + l + 1 > m: cnt += 1 now_width = l else: now_width += l + 1 return cnt <= M ok = 1 << 63 ng = 0 while ok - ng > 1: mid = (ok + ng) // 2 if judge(mid): ok = mid else: ng = mid print(ok) """ N,M=map(int,input().split()) L=list(map(int,input().split())) def jadge(col_num): row_num=1 count=-1 for i in range(N): count+=L[i]+1 if count>col_num: row_num+=1 count=L[i] #print(L[i],count,row_num) return row_num def bs(): left=max(L)-1 right=sum(L)+N+1 center=(left+right)//2 #print(left,right,center) while right-left>1: #print(M,center,jadge(center),M<=jadge(center)) if M>=jadge(center): right=min(center,right-1) center=(left+right)//2 else: left=max(center,left+1) center=(left+right)//2 return right print(bs()) """test N,M=map(int,input().split()) L=list(map(int,input().split())) cumsum_L=[L[0]] for i in range(1,N): cumsum_L.append(cumsum_L[i-1]+L[i]+1) print(cumsum_L) #print(len(cumsum_L),N,cumsum_L[-1]) def jadge(col_num): left_i=-1 right_i=N-1 #one_col_num=col_num minus=0 for i in range(M): left_i=min(N-1,left_i+1) right_i=N-1 center_i=(left_i+right_i)//2 if cumsum_L[left_i]-minus>col_num: #その行の累積和が許容できる列数より大きいとき #print(cumsum_L[left_i],minus,col_num) return False #print("col_num",col_num) while right_i-left_i>1: if cumsum_L[left_i]-minus==col_num: #print(0,right_i,cumsum_L[right_i]-minus) #print(0) #left_i=right_i break elif cumsum_L[center_i]-minus<=col_num: #print(1,left_i,center_i,right_i,cumsum_L[center_i]-minus) left_i=max(center_i,left_i+1) left_i=center_i center_i=(left_i+right_i)//2 else: #print("col_num",col_num) #print(2,left_i,center_i,right_i,cumsum_L[center_i]-minus) #right_i=center_i right_i=min(center_i,right_i-1) center_i=(left_i+right_i)//2 #col_num+=one_col_num #print(left_i,cumsum_L[left_i]) #print(i,"--",left_i,center_i,right_i) if left_i>N-1: return False minus=cumsum_L[left_i] #print("------") if cumsum_L[center_i]-minus<=col_num: return True else: return False print(jadge(188)) left_col=1 right_col=cumsum_L[-1] center_col=(left_col+right_col)//2 ans=-1 #print(right_col) #print(jadge(26)) """ """ while right_col-left_col>0: if jadge(left_col): left_col=right_col break elif jadge(center_col)==True: right_col=min(right_col-1,center_col) center_col=(left_col+right_col)//2 else: left_col=max(left_col+1,center_col) center_col=(left_col+right_col)//2 #print(left_col,center_col,right_col,jadge(center_col)) print(right_col) """ """ N, M = map(int, input().split()) L = list(map(int, input().split())) max_L = max(L)#文字の横幅の最大値 def judge(m): #もし、列の幅が文字の単語の長さより小さければFalse if max_L > m: return False now_width = 1 << 63#巨大数 cnt = 0 #単語の数だけ実施 for l in L: if now_width + l + 1 > m: cnt += 1 now_width = l else: now_width += l + 1 return cnt <= M ok = 1 << 63 ng = 0 while ok - ng > 1: mid = (ok + ng) // 2 if judge(mid): ok = mid else: ng = mid print(ok) """
ConDefects/ConDefects/Code/abc319_d/Python/45968743
condefects-python_data_18
# ------------------------------------------------- # 基本ライブラリ(PyPy3対応) # ------------------------------------------------- import sys import math import bisect #二分探索 import itertools as itert from queue import Queue import heapq from collections import deque from decimal import Decimal, ROUND_HALF_UP sys.setrecursionlimit(10**7) # ------------------------------------------------- # 入力メソッド # ------------------------------------------------- def ST(): return input() #str型 def IN(): return int(input()) #int型 def FL(): return float(input()) #float型 def SL(): return input().split() #strリスト・複数列併用 def IM(): return map(int, input().split()) #int複数列 def IL(): return list(map(int, input().split())) #intリスト def Lis(): return list(input()) #ひとつひとつの「文字」をリストにしてインプット def IR(N): #N行整数をリスト化 x = [] for _ in range(N): x.append(int(input())) return x def SR(N): #N行文字列をリスト化 x = [] for _ in range(N): x.append(input()) return x def IRM(N): #列に変数が並ぶN行データ z = [map(int, input().split()) for _ in range(N)] return [list(i) for i in zip(*z)] def SGrid(N): #N行文字列二次元グリッドをリスト化 x = [] for i in range(N): x.append(list(input())) return x def IMatrix(N): #一般のN行行列 return [list(map(int, input().split())) for l in range(N)] # ------------------------------------------------- N, M = IM() L = IL() S = sum(L) ans = 0 def index_count(L, W): ## #不適合なWが選ばれた場合-1を出力する cnt = 0 ans = 0 for i in range(len(L)): if cnt >= W: ans = 10**10 break if cnt == 0: cnt += L[i] else: cnt += 1 + L[i] if cnt >= W: ans += 1 cnt = L[i] return ans + 1 left = 0 right = S + N while left + 1 < right: W = (left+right)//2 if index_count(L, W) > M: left = W else: right = W #print(index_count(L, W), left, right) print(left) # ------------------------------------------------- # 基本ライブラリ(PyPy3対応) # ------------------------------------------------- import sys import math import bisect #二分探索 import itertools as itert from queue import Queue import heapq from collections import deque from decimal import Decimal, ROUND_HALF_UP sys.setrecursionlimit(10**7) # ------------------------------------------------- # 入力メソッド # ------------------------------------------------- def ST(): return input() #str型 def IN(): return int(input()) #int型 def FL(): return float(input()) #float型 def SL(): return input().split() #strリスト・複数列併用 def IM(): return map(int, input().split()) #int複数列 def IL(): return list(map(int, input().split())) #intリスト def Lis(): return list(input()) #ひとつひとつの「文字」をリストにしてインプット def IR(N): #N行整数をリスト化 x = [] for _ in range(N): x.append(int(input())) return x def SR(N): #N行文字列をリスト化 x = [] for _ in range(N): x.append(input()) return x def IRM(N): #列に変数が並ぶN行データ z = [map(int, input().split()) for _ in range(N)] return [list(i) for i in zip(*z)] def SGrid(N): #N行文字列二次元グリッドをリスト化 x = [] for i in range(N): x.append(list(input())) return x def IMatrix(N): #一般のN行行列 return [list(map(int, input().split())) for l in range(N)] # ------------------------------------------------- N, M = IM() L = IL() S = sum(L) ans = 0 def index_count(L, W): ## #不適合なWが選ばれた場合-1を出力する cnt = 0 ans = 0 for i in range(len(L)): if cnt >= W: ans = 10**10 break if cnt == 0: cnt += L[i] else: cnt += 1 + L[i] if cnt >= W: ans += 1 cnt = L[i] return ans + 1 left = 0 right = S + N while left + 1 < right: W = (left+right)//2 if index_count(L, W) > M: left = W else: right = W #print(index_count(L, W), left, right) print(max(left,L[N-1]) )
ConDefects/ConDefects/Code/abc319_d/Python/46194473
condefects-python_data_19
import sys input = sys.stdin.readline n, m = map(int, input().split()) L = list(map(int, input().split())) ng, ok = max(L) - 1, sum(L) + n - 1 while ok - ng > 1: mid = (ok + ng) // 2 res = 1 temp = 0 for i in range(n): temp += L[i] if temp > mid: res += 1 temp = L[i] else: temp += 1 if res <= m: ok = mid else: ng = mid print(ok) import sys input = sys.stdin.readline n, m = map(int, input().split()) L = list(map(int, input().split())) ng, ok = max(L) - 1, sum(L) + n - 1 while ok - ng > 1: mid = (ok + ng) // 2 res = 1 temp = 0 for i in range(n): temp += L[i] if temp > mid: res += 1 temp = L[i] + 1 else: temp += 1 if res <= m: ok = mid else: ng = mid print(ok)
ConDefects/ConDefects/Code/abc319_d/Python/45944386
condefects-python_data_20
N,M = map(int,input().split()) L = list(map(int,input().split())) def f(w) -> bool: # 0の場合を特別化する n_row = 0 # 残り文字数を管理 rem = 0 for i in range(N): # もし残り文字数が十分であるならば、それをremから引く if rem >= L[i]+1: rem -= L[i]+1 else: n_row += 1 rem = w - L[i] if rem < 0 : return False if n_row <= M: return True else: return False l = 1 r = 1000000000000000 while (r-l) > 1: m = (l+r)//2 if f(m) == True: r = m else: #f(m) == False l = m print(r) N,M = map(int,input().split()) L = list(map(int,input().split())) def f(w) -> bool: # 0の場合を特別化する n_row = 0 # 残り文字数を管理 rem = 0 for i in range(N): # もし残り文字数が十分であるならば、それをremから引く if rem >= L[i]+1: rem -= L[i]+1 else: n_row += 1 rem = w - L[i] if rem < 0 : return False if n_row <= M: return True else: return False l = 0 r = 1000000000000000 while (r-l) > 1: m = (l+r)//2 if f(m) == True: r = m else: #f(m) == False l = m print(r)
ConDefects/ConDefects/Code/abc319_d/Python/45948664
condefects-python_data_21
import itertools # permutation import heapq # queue if __name__ == '__main__': n, m = map(int, input().split()) lis = list(map(int, input().split())) for i in range(n): lis[i] += 1 # 二分探索 l = max(lis) r = sum(lis) while l+1 < r: mid = (l+r)//2 row = 1 cnt = 0 for i in range(n): if mid < cnt + lis[i]: row += 1 cnt = lis[i] else: cnt += lis[i] if row <= m: r = mid else: l = mid print(r-1) import itertools # permutation import heapq # queue if __name__ == '__main__': n, m = map(int, input().split()) lis = list(map(int, input().split())) for i in range(n): lis[i] += 1 # 二分探索 l = max(lis)-1 r = sum(lis) while l+1 < r: mid = (l+r)//2 row = 1 cnt = 0 for i in range(n): if mid < cnt + lis[i]: row += 1 cnt = lis[i] else: cnt += lis[i] if row <= m: r = mid else: l = mid print(r-1)
ConDefects/ConDefects/Code/abc319_d/Python/45774151
condefects-python_data_22
def check_ans(W): ''' ある値でOKか否かの判定関数。問題に合わせて変更する。 ''' line_cnt = 1 line_char_cnt = 0 if L[0] > W: return False else: line_char_cnt = L[0] for i in range(1,N): if L[i] > W: return False else: if line_char_cnt + 1 + L[i] <= W: line_char_cnt += 1 + L[i] else: line_char_cnt = L[i] line_cnt += 1 if line_cnt > M: return False return True def binary_search(maxi,mini): ''' mini~maxi間で判定用関数の結果がOKとなる最小値を求める ''' if maxi - mini <= 4: for ans in range(maxi,mini,-1): if check_ans(ans) == False: return ans + 1 else: mid = (maxi+mini)//2 #int((max+min)//2)だと浮動小数点を介して、誤差になるので注意 #判定用関数に投げて、条件により最大・最少を狭める if check_ans(mid) == True: # midでOKだったので、OKとなる最小値はmidより小さい値である maxi = mid else: # midでNGだったので、OKとなる最小値はmidより大きい値である mini = mid return binary_search(maxi,mini) N,M = map(int, input().split()) L = [int(e) for e in input().split()] print(binary_search(10**16,0)) def check_ans(W): ''' ある値でOKか否かの判定関数。問題に合わせて変更する。 ''' line_cnt = 1 line_char_cnt = 0 if L[0] > W: return False else: line_char_cnt = L[0] for i in range(1,N): if L[i] > W: return False else: if line_char_cnt + 1 + L[i] <= W: line_char_cnt += 1 + L[i] else: line_char_cnt = L[i] line_cnt += 1 if line_cnt > M: return False return True def binary_search(maxi,mini): ''' mini~maxi間で判定用関数の結果がOKとなる最小値を求める ''' if maxi - mini <= 4: for ans in range(maxi,mini-1,-1): if check_ans(ans) == False: return ans + 1 else: mid = (maxi+mini)//2 #int((max+min)//2)だと浮動小数点を介して、誤差になるので注意 #判定用関数に投げて、条件により最大・最少を狭める if check_ans(mid) == True: # midでOKだったので、OKとなる最小値はmidより小さい値である maxi = mid else: # midでNGだったので、OKとなる最小値はmidより大きい値である mini = mid return binary_search(maxi,mini) N,M = map(int, input().split()) L = [int(e) for e in input().split()] print(binary_search(10**16,0))
ConDefects/ConDefects/Code/abc319_d/Python/45993248
condefects-python_data_23
n, m = list(map(int, input().split())) l = list(map(int, input().split())) def check(x): cnt = 1 s = l[0] for i in range(1, n): if s + l[i] + 1 > x: cnt += 1 s = l[i] else: s += l[i] + 1 if cnt > m: return False return True min_x = max(l) max_x = sum(l) + n - 1 while True: mid = (min_x + max_x) // 2 if check(mid): max_x = mid else: min_x = mid if max_x - min_x <= 1: break print(max_x) n, m = list(map(int, input().split())) l = list(map(int, input().split())) def check(x): cnt = 1 s = l[0] for i in range(1, n): if s + l[i] + 1 > x: cnt += 1 s = l[i] else: s += l[i] + 1 if cnt > m: return False return True min_x = max(l)-1 max_x = sum(l) + n - 1 while True: mid = (min_x + max_x) // 2 if check(mid): max_x = mid else: min_x = mid if max_x - min_x <= 1: break print(max_x)
ConDefects/ConDefects/Code/abc319_d/Python/45933392
condefects-python_data_24
#!/usr/bin/env python3 import math import sys from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right # type: ignore from collections import Counter, defaultdict, deque # type: ignore from heapq import heapify, heappop, heappush, heappushpop, heapreplace, merge # type: ignore from itertools import accumulate, combinations, permutations, product # type: ignore def InputI(): return int(sys.stdin.buffer.readline()) def InputIM(): return map(int, sys.stdin.buffer.readline().split()) def InputIL(): return list(map(int, sys.stdin.buffer.readline().split())) def InputS(): return sys.stdin.buffer.readline().rstrip().decode("utf-8") def InputSS(): return sys.stdin.buffer.readline().rstrip().decode("utf-8").split() def InputIR(n): return [InputI() for _ in range(n)] def InputILR(n): return [InputIL() for _ in range(n)] def InputSR(n): return [InputS() for _ in range(n)] def InputSSR(n): return [InputSS() for _ in range(n)] def InputSLR(n): return [list(InputS()) for _ in range(n)] def InputSLIR(n): return [[int(i) for i in list(InputS())] for _ in range(n)] n, m = InputIM() l = InputIL() def num_required_row(x): tmp = 0 res = 1 for i in range(n): tmp += l[i] if tmp > x: tmp = l[i] + 1 res += 1 else: tmp += 1 return res ng = max(l); ok = 10**9*2*10**5+2*10**5 while ok - ng > 1: mid = (ok + ng) // 2 if num_required_row(mid) <= m: ok = mid else: ng = mid print(ok) #!/usr/bin/env python3 import math import sys from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right # type: ignore from collections import Counter, defaultdict, deque # type: ignore from heapq import heapify, heappop, heappush, heappushpop, heapreplace, merge # type: ignore from itertools import accumulate, combinations, permutations, product # type: ignore def InputI(): return int(sys.stdin.buffer.readline()) def InputIM(): return map(int, sys.stdin.buffer.readline().split()) def InputIL(): return list(map(int, sys.stdin.buffer.readline().split())) def InputS(): return sys.stdin.buffer.readline().rstrip().decode("utf-8") def InputSS(): return sys.stdin.buffer.readline().rstrip().decode("utf-8").split() def InputIR(n): return [InputI() for _ in range(n)] def InputILR(n): return [InputIL() for _ in range(n)] def InputSR(n): return [InputS() for _ in range(n)] def InputSSR(n): return [InputSS() for _ in range(n)] def InputSLR(n): return [list(InputS()) for _ in range(n)] def InputSLIR(n): return [[int(i) for i in list(InputS())] for _ in range(n)] n, m = InputIM() l = InputIL() def num_required_row(x): tmp = 0 res = 1 for i in range(n): tmp += l[i] if tmp > x: tmp = l[i] + 1 res += 1 else: tmp += 1 return res ng = max(l)-1; ok = 10**9*2*10**5+2*10**5 while ok - ng > 1: mid = (ok + ng) // 2 if num_required_row(mid) <= m: ok = mid else: ng = mid print(ok)
ConDefects/ConDefects/Code/abc319_d/Python/46138292
condefects-python_data_25
import sys read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines N, M, *L = map(int, read().split()) left = max(L) right = 10 ** 18 while right - left > 1: mid = (left + right) // 2 cnt = 1 idx = 0 for i, l in enumerate(L): if idx + l <= mid: idx += l else: cnt += 1 idx = l idx += 1 if i != N - 1 and idx > mid: cnt += 1 idx = 0 if cnt <= M: right = mid else: left = mid print(right) import sys read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines N, M, *L = map(int, read().split()) left = max(L) - 1 right = 10 ** 18 while right - left > 1: mid = (left + right) // 2 cnt = 1 idx = 0 for i, l in enumerate(L): if idx + l <= mid: idx += l else: cnt += 1 idx = l idx += 1 if i != N - 1 and idx > mid: cnt += 1 idx = 0 if cnt <= M: right = mid else: left = mid print(right)
ConDefects/ConDefects/Code/abc319_d/Python/45768613
condefects-python_data_26
N, M = map(int, input().split()) L = list(map(int, input().split())) ng, ok = max(L), sum(L) + N - 1 while ng + 1 < ok: chk = (ng + ok) // 2 m = 1 w = L[0] for l in L[1:]: if (w + 1 + l) > chk: m += 1 w = l else: w += 1 + l if m <= M: ok = chk else: ng = chk print(ok) N, M = map(int, input().split()) L = list(map(int, input().split())) ng, ok = max(L) - 1, sum(L) + N - 1 while ng + 1 < ok: chk = (ng + ok) // 2 m = 1 w = L[0] for l in L[1:]: if (w + 1 + l) > chk: m += 1 w = l else: w += 1 + l if m <= M: ok = chk else: ng = chk print(ok)
ConDefects/ConDefects/Code/abc319_d/Python/46172725
condefects-python_data_27
from collections import defaultdict from collections import deque class UnionFind(): def __init__(self, n): self.n = n self.root = [-1]*n self.rank = [0]*n def find(self, x): if(self.root[x] < 0): return x else: self.root[x] = self.find(self.root[x]) return self.root[x] def unite(self, x, y): x = self.find(x) y = self.find(y) if(x == y): return elif(self.rank[x] > self.rank[y]): self.root[x] += self.root[y] self.root[y] = x else: self.root[y] += self.root[x] self.root[x] = y if(self.rank[x] == self.rank[y]): self.rank[y] += 1 def same(self, x, y): return self.find(x) == self.find(y) def size(self, x): return -self.root[self.find(x)] def roots(self): return [i for i, x in enumerate(self.root) if x < 0] def group_size(self): return len(self.roots()) def group_members(self): group_members = defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members N = int(input()) P = list(map(int,input().split())) for i in range(N): P[i] -= 1 M = int(input()) uf = UnionFind(N) E = [] for i in range(M): a,b = map(int,input().split()) a -= 1 b -= 1 uf.unite(a,b) E.append((a,b)) R = uf.roots() n = len(R) V = [[] for i in range(n)] d = {} for i in range(n): d[R[i]] = i for v in range(N): V[d[uf.find(v)]].append(v) ans = [] for i in range(n): X = [] for v in V[i]: X.append(P[v]) X.sort() # 無理な時 if X != V[i]: print(-1) exit() r = R[i] EE = [] for j in range(M): u,v = E[j] if uf.find(u) == r: EE.append(j) if len(EE) == 0: continue uff = UnionFind(N) G = [[] for i in range(N)] D = [-1 for i in range(N)] for v in V[i]: D[v] = 0 # 連結部分で木を作る for j in EE: u,v = E[j] if uff.same(u,v): continue else: uff.unite(u,v) G[u].append((v,j+1)) G[v].append((u,j+1)) D[u] += 1 D[v] += 1 # 次数が1のものから順に todo = [] for v in V[i]: if D[v] == 1: todo.append(v) while len(todo): v = todo.pop() X = [-1 for i in range(N)] visit = deque([v]) X[v] = 0 if P[v] == v: u = v visit = [] # vの位置を探す while len(visit): u = visit.popleft() for r,j in G[u]: if X[r] >= 0: continue X[r] = j visit.append(r) if P[r] == v: u = r break # vにもってくる while u != v: j = X[u] p,q = E[j-1] r = p if p == u: r = q P[u],P[r] = P[r],P[u] ans.append(j) u = r for u,j in G[v]: G[u].remove((v,j)) D[u] -= 1 if D[u] == 1: todo.append(u) print(len(ans)) print(*ans) from collections import defaultdict from collections import deque class UnionFind(): def __init__(self, n): self.n = n self.root = [-1]*n self.rank = [0]*n def find(self, x): if(self.root[x] < 0): return x else: self.root[x] = self.find(self.root[x]) return self.root[x] def unite(self, x, y): x = self.find(x) y = self.find(y) if(x == y): return elif(self.rank[x] > self.rank[y]): self.root[x] += self.root[y] self.root[y] = x else: self.root[y] += self.root[x] self.root[x] = y if(self.rank[x] == self.rank[y]): self.rank[y] += 1 def same(self, x, y): return self.find(x) == self.find(y) def size(self, x): return -self.root[self.find(x)] def roots(self): return [i for i, x in enumerate(self.root) if x < 0] def group_size(self): return len(self.roots()) def group_members(self): group_members = defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members N = int(input()) P = list(map(int,input().split())) for i in range(N): P[i] -= 1 M = int(input()) uf = UnionFind(N) E = [] for i in range(M): a,b = map(int,input().split()) a -= 1 b -= 1 uf.unite(a,b) E.append((a,b)) R = uf.roots() n = len(R) V = [[] for i in range(n)] d = {} for i in range(n): d[R[i]] = i for v in range(N): V[d[uf.find(v)]].append(v) ans = [] for i in range(n): X = [] for v in V[i]: X.append(P[v]) X.sort() # 無理な時 if X != V[i]: print(-1) exit() r = R[i] EE = [] for j in range(M): u,v = E[j] if uf.find(u) == r: EE.append(j) if len(EE) == 0: continue uff = UnionFind(N) G = [[] for i in range(N)] D = [-1 for i in range(N)] for v in V[i]: D[v] = 0 # 連結部分で木を作る for j in EE: u,v = E[j] if uff.same(u,v): continue else: uff.unite(u,v) G[u].append((v,j+1)) G[v].append((u,j+1)) D[u] += 1 D[v] += 1 # 次数が1のものから順に todo = [] for v in V[i]: if D[v] == 1: todo.append(v) while len(todo): v = todo.pop() X = [-1 for i in range(N)] visit = deque([v]) X[v] = 0 if P[v] == v: u = v visit = [] # vの位置を探す while len(visit): u = visit.popleft() for r,j in G[u]: if X[r] >= 0: continue X[r] = j visit.append(r) if P[r] == v: u = r break if P[u] == v: break # vにもってくる while u != v: j = X[u] p,q = E[j-1] r = p if p == u: r = q P[u],P[r] = P[r],P[u] ans.append(j) u = r for u,j in G[v]: G[u].remove((v,j)) D[u] -= 1 if D[u] == 1: todo.append(u) print(len(ans)) print(*ans)
ConDefects/ConDefects/Code/abc233_f/Python/50214757
condefects-python_data_28
t = int(input()) for iiiii in range(t): n = int(input()) s =(input()) flag = True b = False for i in range(n): if s[i] == "B": b=True if s[i] == "A" and b: flag = False if(flag): print("B") else : print("A") t = int(input()) for iiiii in range(t): n = int(input()) s =(input()) flag = True b = False for i in range(n): if s[i] == "B": b=True if s[i] == "A" and b: flag = False if(flag and b): print("B") else : print("A")
ConDefects/ConDefects/Code/agc062_a/Python/43213602
condefects-python_data_29
n=int(input()) for _ in range(n): t=input() s=input() if('A' in s[1:]): print('A') else: print('B') n=int(input()) for _ in range(n): t=input() s=input() if('A'==s[-1] or 'BA' in s): print('A') else: print('B')
ConDefects/ConDefects/Code/agc062_a/Python/44927989
condefects-python_data_30
def solve(N, S): if S[-1] == 'A': return 'A' else: if S.count('AB') == 1: return 'B' else: return 'A' T = int(input()) for _ in range(T): N = int(input()) S = input() print(solve(N, S)) def solve(N, S): if S[-1] == 'A': return 'A' else: if S.count('A') == 0 or S.count('BA') == 0: return 'B' else: return 'A' T = int(input()) for _ in range(T): N = int(input()) S = input() print(solve(N, S))
ConDefects/ConDefects/Code/agc062_a/Python/42875065
condefects-python_data_31
t = int(input()) for _ in range(t): n = int(input()) c = input() i = 0 j = n - 1 while i < n and c[i] == 'A': i += 1 while j > 0 and c[j] == 'B': j -= 1 if i - j == 1 and c[-1] == 'B': print('B') else: print('A') t = int(input()) for _ in range(t): n = int(input()) c = input() i = 0 j = n - 1 while i < n and c[i] == 'A': i += 1 while j > 0 and c[j] == 'B': j -= 1 if i == j == 0 or (i - j == 1 and c[-1] == 'B'): print('B') else: print('A')
ConDefects/ConDefects/Code/agc062_a/Python/44110722
condefects-python_data_32
import sys read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines T = int(readline()) for _ in range(T): N = int(readline()) S = readline().rstrip() if not 'B' in S: print('A') continue index_ = S.index('B') if S == 'A' * index_ and 'B' * (N - index_): print('B') else: print('A') import sys read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines T = int(readline()) for _ in range(T): N = int(readline()) S = readline().rstrip() if not 'B' in S: print('A') continue index_ = S.index('B') if S == 'A' * index_ + 'B' * (N - index_): print('B') else: print('A')
ConDefects/ConDefects/Code/agc062_a/Python/43015792
condefects-python_data_33
t = int(input()) ans = [] for _ in range(t): n = int(input()) s = input() if s[-1] == "A": ans.append("A") else: if all(i == "B" for i in s): ans.append("B") if any(s[i] == "B" and s[i+1] == "A" for i in range(n-1)): ans.append("A") else: ans.append("B") print("\n".join(ans)) t = int(input()) ans = [] for _ in range(t): n = int(input()) s = input() if s[-1] == "A": ans.append("A") else: if all(i == "B" for i in s): ans.append("B") elif any((s[i] == "B" and s[i+1] == "A") for i in range(n-1)): ans.append("A") else: ans.append("B") print("\n".join(ans))
ConDefects/ConDefects/Code/agc062_a/Python/44358952
condefects-python_data_34
t=int(input()) for i in range(t): n=int(input()) s=list(map(str,input())) cnt=0 for i in range(n-1): if s[i]=="A" and s[i+1]=="B": cnt+=1 if cnt==0 and s[0]=="B" and s[-1]=="B": print("B") elif cnt==1 and s[0]=="A": print("B") else: print("A") t=int(input()) for i in range(t): n=int(input()) s=list(map(str,input())) cnt=0 for i in range(n-1): if s[i]=="A" and s[i+1]=="B": cnt+=1 if cnt==0 and s[0]=="B" and s[-1]=="B": print("B") elif cnt==1 and s[0]=="A" and s[-1]=="B": print("B") else: print("A")
ConDefects/ConDefects/Code/agc062_a/Python/43035982
condefects-python_data_35
from itertools import product N,S,M,L=map(int,input().split()) ans=10**20 for i,j,k in product(range(N),repeat=3): if 6*i+8*j+12*k<N: continue ans=min(ans,S*i+M*j+L*k) print(ans) from itertools import product N,S,M,L=map(int,input().split()) ans=10**20 for i,j,k in product(range(N+1),repeat=3): if 6*i+8*j+12*k<N: continue ans=min(ans,S*i+M*j+L*k) print(ans)
ConDefects/ConDefects/Code/abc331_b/Python/54148195
condefects-python_data_36
n, s, m, l = map(int,input().split()) sum = 10000 for j in range(20): for k in range(15): for a in range(10): if n <= 6*j + 8*k + 12* a <= n + 11: if s*j + m*k + l*a < sum: sum = s*j + m*k + l*a print(sum) n, s, m, l = map(int,input().split()) sum = 10**10 for j in range(20): for k in range(15): for a in range(10): if n <= 6*j + 8*k + 12* a <= n + 11: if s*j + m*k + l*a < sum: sum = s*j + m*k + l*a print(sum)
ConDefects/ConDefects/Code/abc331_b/Python/54957730
condefects-python_data_37
n,s,m,l = map(int,input().split()) ans = float('inf') for i in range(101): for j in range(101): for k in range(101): if i+j+l>=n: ans=min(ans,i*s+j*m+l*k) print(ans) n,s,m,l = map(int,input().split()) ans = float('inf') for i in range(101): for j in range(101): for k in range(101): if 6*i+8*j+12*k>=n: ans=min(ans,i*s+j*m+l*k) print(ans)
ConDefects/ConDefects/Code/abc331_b/Python/54769953
condefects-python_data_38
import sys from collections import defaultdict out = [] input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) l = input().split() d = {} for v in l: if v not in d: d[v] = 1 else: d[v] += 1 big2 = [] big = [] br = [] smol = [] for v in d: if d[v] == 1: smol.append(v) else: big.extend([v] * (d[v] - 1)) big2.extend([v] * d[v]) br.append(v) big = big + br[::-1] m = len(big) oo = [] for i in range(m): if big[i] == big[i - 1] and smol: oo.append(smol.pop()) oo.append(big[i]) if smol: out.append(' '.join(big2 + smol)) else: out.append(' '.join(oo)) print('\n'.join(out)) import sys from collections import defaultdict out = [] input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) l = input().split() d = {} for v in l: if v not in d: d[v] = 1 else: d[v] += 1 big2 = [] big = [] br = [] smol = [] for v in d: if d[v] == 1: smol.append(v) else: big.extend([v] * (d[v] - 1)) big2.extend([v] * d[v]) br.append(v) big = big + br[::-1] m = len(big) oo = [] for i in range(m): if big[i] == big[i - 1] and smol: oo.append(smol.pop()) oo.append(big[i]) if smol: out.append(' '.join(map(str, sorted(map(int, l))))) else: out.append(' '.join(oo)) print('\n'.join(out))
ConDefects/ConDefects/Code/agc059_b/Python/37027269
condefects-python_data_39
from collections import Counter, deque import sys sys.setrecursionlimit(2*10**5) def dfs(v): for _ in range(D[v+1]-len(G[v])+1): ans.append(v+1) D[v+1]-=1 seen[v] = True for nv in G[v]: if seen[nv]: continue dfs(nv) if D[v+1]>0: ans.append(v+1) D[v+1]-=1 T = int(input()) for _ in range(T): N = int(input()) C = list(map(int, input().split())) cnt = Counter(C) if len(cnt)<=2: C.sort() print(*C) else: G = [[] for _ in range(N)] a = deque() b = deque() X = 0 for c in cnt.most_common(): if c[1]>=2: b.append([c[0]-1, c[1]]) X += c[1] else: a.append(c[0]-1) if X+len(a)<2*len(cnt)-2: C.sort() print(*C) else: while len(b)>0 and len(a)>0: k, v = b.popleft() while v>1 and len(a)>0: l = a.popleft() G[k].append(l) G[l].append(k) v-=1 if v==1: a.append(k) if len(b)>0: k, v = b.popleft() while len(b)>0: kk, vv = b.popleft() G[k].append(kk) G[kk].append(k) k = kk if len(a)>0: k = a.popleft() while len(a)>0: kk = a.popleft() G[k].append(kk) G[kk].append(k) k = kk #print(G) #print(cnt) for i in range(N): if len(G[i])>0: break D = dict(cnt) #print(D) seen = [False for _ in range(N)] ans = [] v = i dfs(v) #print(D) print(*ans) from collections import Counter, deque import sys sys.setrecursionlimit(2*10**5) def dfs(v): for _ in range(D[v+1]-len(G[v])+1): ans.append(v+1) D[v+1]-=1 seen[v] = True for nv in G[v]: if seen[nv]: continue dfs(nv) if D[v+1]>0: ans.append(v+1) D[v+1]-=1 T = int(input()) for _ in range(T): N = int(input()) C = list(map(int, input().split())) cnt = Counter(C) if len(cnt)<=2: C.sort() print(*C) else: G = [[] for _ in range(N)] a = deque() b = deque() X = 0 for c in cnt.most_common(): if c[1]>=2: b.append([c[0]-1, c[1]]) X += c[1] else: a.append(c[0]-1) if X+len(a)<2*len(cnt)-2: C.sort() print(*C) else: while len(b)>0 and len(a)>0: k, v = b.popleft() while v>1 and len(a)>0: l = a.popleft() G[k].append(l) G[l].append(k) v-=1 if v==1: a.append(k) elif v>1: b.append([k, v]) if len(b)>0: k, v = b.popleft() while len(b)>0: kk, vv = b.popleft() G[k].append(kk) G[kk].append(k) k = kk if len(a)>0: k = a.popleft() while len(a)>0: kk = a.popleft() G[k].append(kk) G[kk].append(k) k = kk #print(G) #print(cnt) for i in range(N): if len(G[i])>0: break D = dict(cnt) #print(D) seen = [False for _ in range(N)] ans = [] v = i dfs(v) #print(D) print(*ans)
ConDefects/ConDefects/Code/agc059_b/Python/37047746
condefects-python_data_40
from collections import defaultdict T = int(input()) for _ in range(T): N = int(input()) C = list(map(int,input().split())) dc = {} for c in C: if c not in dc: dc[c] = 0 dc[c] += 1 lst = [] cnt = 0 for k in dc: lst.append((dc[k],k)) cnt += 1 lst.sort(reverse=True) if N//2 < cnt-1: ans = [] for i in range(cnt): for _ in range(lst[i][0]): ans.append(lst[i][1]) print(" ".join(list(map(str,ans)))) else: children = defaultdict(list) chs = {} for v in dc: chs[v] = dc[v] - 1 chs[lst[0][1]] += 1 j = 1 for i in range(cnt): for _ in range(chs[lst[i][1]]): if j >= cnt: break children[lst[i][1]].append(lst[j][1]) j += 1 def search(v): ans.append(v) dc[v] -= 1 for w in children[v]: search(w) if dc[v] > 0: ans.append(v) dc[v] -= 1 ans = [] search(lst[0][1]) tmp = [] for n in ans: tmp.append(n) if dc[n] > 0: tmp.append(n) dc[n] -= 1 print(" ".join(list(map(str,tmp)))) from collections import defaultdict T = int(input()) for _ in range(T): N = int(input()) C = list(map(int,input().split())) dc = {} for c in C: if c not in dc: dc[c] = 0 dc[c] += 1 lst = [] cnt = 0 for k in dc: lst.append((dc[k],k)) cnt += 1 lst.sort(reverse=True) if N//2 < cnt-1: ans = [] for i in range(cnt): for _ in range(lst[i][0]): ans.append(lst[i][1]) print(" ".join(list(map(str,ans)))) else: children = defaultdict(list) chs = {} for v in dc: chs[v] = dc[v] - 1 chs[lst[0][1]] += 1 j = 1 for i in range(cnt): for _ in range(chs[lst[i][1]]): if j >= cnt: break children[lst[i][1]].append(lst[j][1]) j += 1 def search(v): ans.append(v) dc[v] -= 1 for w in children[v]: search(w) if dc[v] > 0: ans.append(v) dc[v] -= 1 ans = [] search(lst[0][1]) tmp = [] for n in ans: tmp.append(n) while dc[n] > 0: tmp.append(n) dc[n] -= 1 print(" ".join(list(map(str,tmp))))
ConDefects/ConDefects/Code/agc059_b/Python/37028649
condefects-python_data_41
g=lambda:map(int,input().split());N,M=g();P=list(g());D=998244353;f=lambda i:i*i+i;dp=[0,0,1];h=lambda x:pow(max(1,x),-1,D);A=0 for m in range(M):a,b,c=dp;X=(N-2)*(N-3)//2+1;dp=[(a*(X+4*N-11)+b*(N-3))%D,(a*4+b*(X+N-2)+c*(2*N-4))%D,(b+c*X)%D] for i,j in zip(P,P[1:]):s=f(i-1)+f(j-1)+f(N-i)+f(N-j);t=abs(i-j);A=(A+dp[2]*t+dp[1]*(s-t*4)*h(4*N-8)+dp[0]*((N**3-N)//3-s+t*2)*h(N*N-5*N+6))%D print(A) g=lambda:map(int,input().split());N,M=g();P=list(g());D=998244353;f=lambda i:i*i+i;dp=[0,0,1];h=lambda x:pow(max(1,x),-1,D);A=0 for m in range(M):a,b,c=dp;X=(N-2)*(N-3)//2+1;dp=[(a*(X+2*N-8)+b*(N-3))%D,(a*4+b*(X+N-2)+c*(2*N-4))%D,(b+c*X)%D] for i,j in zip(P,P[1:]):s=f(i-1)+f(j-1)+f(N-i)+f(N-j);t=abs(i-j);A=(A+dp[2]*t+dp[1]*(s-t*4)*h(4*N-8)+dp[0]*((N**3-N)//3-s+t*2)*h(N*N-5*N+6))%D print(A)
ConDefects/ConDefects/Code/arc176_d/Python/52672133
condefects-python_data_42
import bisect import copy import decimal import fractions import heapq import itertools import math import random import sys import time from collections import Counter,deque,defaultdict from functools import lru_cache,reduce from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max def _heappush_max(heap,item): heap.append(item) heapq._siftdown_max(heap, 0, len(heap)-1) def _heappushpop_max(heap, item): if heap and item < heap[0]: item, heap[0] = heap[0], item heapq._siftup_max(heap, 0) return item from math import gcd as GCD read=sys.stdin.read readline=sys.stdin.readline readlines=sys.stdin.readlines write=sys.stdout.write #import pypyjit #pypyjit.set_param('max_unroll_recursion=-1') #sys.set_int_max_str_digits(10**9) def Berlekamp_Massey(A): n = len(A) B, C = [1], [1] l, m, p = 0, 1, 1 for i in range(n): d = A[i] for j in range(1, l + 1): d += C[j] * A[i - j] d %= mod if d == 0: m += 1 continue T = C.copy() q = pow(p, mod - 2, mod) * d % mod if len(C) < len(B) + m: C += [0] * (len(B) + m - len(C)) for j, b in enumerate(B): C[j + m] -= q * b C[j + m] %= mod if 2 * l <= i: B = T l, m, p = i + 1 - l, 1, d else: m += 1 res = [-c % mod for c in C[1:]] return res def BMBM(A,N,mod=0): deno=[1]+[-c for c in Berlekamp_Massey(A)] nume=[0]*(len(deno)-1) for i in range(len(A)): for j in range(len(deno)): if i+j<len(nume): nume[i+j]+=A[i]*deno[j] nume[i+j]%=mod return Bostan_Mori(nume,deno,N,mod=mod) def Bostan_Mori(poly_nume,poly_deno,N,mod=0,convolve=None): #if type(poly_nume)==Polynomial: # poly_nume=poly_nume.polynomial #if type(poly_deno)==Polynomial: # poly_deno=poly_deno.polynomial if convolve==None: def convolve(poly_nume,poly_deno): conv=[0]*(len(poly_nume)+len(poly_deno)-1) for i in range(len(poly_nume)): for j in range(len(poly_deno)): x=poly_nume[i]*poly_deno[j] if mod: x%=mod conv[i+j]+=x if mod: for i in range(len(conv)): conv[i]%=mod return conv while N: poly_deno_=[-x if i%2 else x for i,x in enumerate(poly_deno)] if N%2: poly_nume=convolve(poly_nume,poly_deno_)[1::2] else: poly_nume=convolve(poly_nume,poly_deno_)[::2] poly_deno=convolve(poly_deno,poly_deno_)[::2] if mod: for i in range(len(poly_nume)): poly_nume[i]%=mod for i in range(len(poly_deno)): poly_deno[i]%=mod N//=2 return poly_nume[0] N,M=map(int,input().split()) P=list(map(int,input().split())) for i in range(N): P[i]-=1 ans=0 mod=998244353 inve0=pow(N*(N-1)-4*(N-2)-2,mod-2,mod) inve1=pow(4*(N-2),mod-2,mod) def f0(N,i,j): if N<=3: return 0 retu=N*(N+1)*(N-1)//3 retu-=(i*(i+1)//2+(N-1-i)*(N-i)//2-abs(i-j))*2 retu-=(j*(j+1)//2+(N-1-j)*(N-j)//2-abs(i-j))*2 retu-=abs(i-j)*2 return retu*inve0%mod def f1(N,i,j): if N<=2: return 0 retu=0 retu+=(i*(i+1)//2+(N-1-i)*(N-i)//2-abs(i-j))*2 retu+=(j*(j+1)//2+(N-1-j)*(N-j)//2-abs(i-j))*2 return retu*inve1%mod def f2(N,i,j): return abs(i-j) cnt=[0]*3 cnt[0]+=(N*(N-1)//2-4) cnt[1]+=(N-3) cnt[0]+=2*2 cnt[1]+=((N-2)*(N-3)//2+N-2+1) cnt[2]+=2*(N-2) cnt[1]+=1 cnt[2]+=(N-2)*(N-3)//2+1 dp=[0,0,1] lst=[] for m in range(5): lst.append(sum((f0(N,P[i],P[i+1])*dp[0]+f1(N,P[i],P[i+1])*dp[1]+f2(N,P[i],P[i+1])*dp[2])%mod for i in range(N-1))) prev=dp dp=[0]*3 dp[0]+=prev[0]*(N*(N-1)//2-4) dp[0]+=prev[1]*(N-3) dp[1]+=prev[0]*2*2 dp[1]+=prev[1]*((N-2)*(N-3)//2+N-2+1) dp[1]+=prev[2]*2*(N-2) dp[2]+=prev[1]*1 dp[2]+=prev[2]*((N-2)*(N-3)//2+1) for c in range(3): dp[c]%=mod ans+=BMBM(lst,M,mod) ans%=mod print(ans) import bisect import copy import decimal import fractions import heapq import itertools import math import random import sys import time from collections import Counter,deque,defaultdict from functools import lru_cache,reduce from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max def _heappush_max(heap,item): heap.append(item) heapq._siftdown_max(heap, 0, len(heap)-1) def _heappushpop_max(heap, item): if heap and item < heap[0]: item, heap[0] = heap[0], item heapq._siftup_max(heap, 0) return item from math import gcd as GCD read=sys.stdin.read readline=sys.stdin.readline readlines=sys.stdin.readlines write=sys.stdout.write #import pypyjit #pypyjit.set_param('max_unroll_recursion=-1') #sys.set_int_max_str_digits(10**9) def Berlekamp_Massey(A): n = len(A) B, C = [1], [1] l, m, p = 0, 1, 1 for i in range(n): d = A[i] for j in range(1, l + 1): d += C[j] * A[i - j] d %= mod if d == 0: m += 1 continue T = C.copy() q = pow(p, mod - 2, mod) * d % mod if len(C) < len(B) + m: C += [0] * (len(B) + m - len(C)) for j, b in enumerate(B): C[j + m] -= q * b C[j + m] %= mod if 2 * l <= i: B = T l, m, p = i + 1 - l, 1, d else: m += 1 res = [-c % mod for c in C[1:]] return res def BMBM(A,N,mod=0): deno=[1]+[-c for c in Berlekamp_Massey(A)] nume=[0]*(len(deno)-1) for i in range(len(A)): for j in range(len(deno)): if i+j<len(nume): nume[i+j]+=A[i]*deno[j] nume[i+j]%=mod return Bostan_Mori(nume,deno,N,mod=mod) def Bostan_Mori(poly_nume,poly_deno,N,mod=0,convolve=None): #if type(poly_nume)==Polynomial: # poly_nume=poly_nume.polynomial #if type(poly_deno)==Polynomial: # poly_deno=poly_deno.polynomial if convolve==None: def convolve(poly_nume,poly_deno): conv=[0]*(len(poly_nume)+len(poly_deno)-1) for i in range(len(poly_nume)): for j in range(len(poly_deno)): x=poly_nume[i]*poly_deno[j] if mod: x%=mod conv[i+j]+=x if mod: for i in range(len(conv)): conv[i]%=mod return conv while N: poly_deno_=[-x if i%2 else x for i,x in enumerate(poly_deno)] if N%2: poly_nume=convolve(poly_nume,poly_deno_)[1::2] else: poly_nume=convolve(poly_nume,poly_deno_)[::2] poly_deno=convolve(poly_deno,poly_deno_)[::2] if mod: for i in range(len(poly_nume)): poly_nume[i]%=mod for i in range(len(poly_deno)): poly_deno[i]%=mod N//=2 return poly_nume[0] N,M=map(int,input().split()) P=list(map(int,input().split())) for i in range(N): P[i]-=1 ans=0 mod=998244353 inve0=pow(N*(N-1)-4*(N-2)-2,mod-2,mod) inve1=pow(4*(N-2),mod-2,mod) def f0(N,i,j): if N<=3: return 0 retu=N*(N+1)*(N-1)//3 retu-=(i*(i+1)//2+(N-1-i)*(N-i)//2-abs(i-j))*2 retu-=(j*(j+1)//2+(N-1-j)*(N-j)//2-abs(i-j))*2 retu-=abs(i-j)*2 return retu*inve0%mod def f1(N,i,j): if N<=2: return 0 retu=0 retu+=(i*(i+1)//2+(N-1-i)*(N-i)//2-abs(i-j))*2 retu+=(j*(j+1)//2+(N-1-j)*(N-j)//2-abs(i-j))*2 return retu*inve1%mod def f2(N,i,j): return abs(i-j) cnt=[0]*3 cnt[0]+=(N*(N-1)//2-4) cnt[1]+=(N-3) cnt[0]+=2*2 cnt[1]+=((N-2)*(N-3)//2+N-2+1) cnt[2]+=2*(N-2) cnt[1]+=1 cnt[2]+=(N-2)*(N-3)//2+1 dp=[0,0,1] lst=[] for m in range(6): lst.append(sum((f0(N,P[i],P[i+1])*dp[0]+f1(N,P[i],P[i+1])*dp[1]+f2(N,P[i],P[i+1])*dp[2])%mod for i in range(N-1))) prev=dp dp=[0]*3 dp[0]+=prev[0]*(N*(N-1)//2-4) dp[0]+=prev[1]*(N-3) dp[1]+=prev[0]*2*2 dp[1]+=prev[1]*((N-2)*(N-3)//2+N-2+1) dp[1]+=prev[2]*2*(N-2) dp[2]+=prev[1]*1 dp[2]+=prev[2]*((N-2)*(N-3)//2+1) for c in range(3): dp[c]%=mod ans+=BMBM(lst,M,mod) ans%=mod print(ans)
ConDefects/ConDefects/Code/arc176_d/Python/52671232
condefects-python_data_43
import sys input = sys.stdin.readline mod=998244353 # 行列の計算(numpyを使えないとき,modを使用) def prod(A,B,k,l,m):# A:k*l,B:l*m C=[[None for i in range(m)] for j in range(k)] for i in range(k): for j in range(m): ANS=0 for pl in range(l): ANS=(ANS+A[i][pl]*B[pl][j])%mod C[i][j]=ANS return C def plus(A,B,k,l):# a,B:k*l C=[[None for i in range(l)] for j in range(k)] for i in range(k): for j in range(l): C[i][j]=(A[i][j]+B[i][j])%mod return C N,M=map(int,input().split()) P=list(map(int,input().split())) if N==2: print(1) exit() if N==3: A=[[2,1],[2,1]] POWA=[A] for i in range(60): POWA.append(prod(POWA[-1],POWA[-1],2,2,2)) # ベキを求めて n=M X=[0,0] X[0]+=abs(P[1]-P[0])+abs(P[2]-P[1]) X[1]=4-X[0] X=[X] while n: X=prod(X,POWA[n.bit_length()-1],1,2,2) # n乗の場合 n-=1<<(n.bit_length()-1) print(X[0]) exit() ALL=N*(N-1)//2 A=[[ALL-(N-2)*2,(N-2)*2,0], [1,ALL-1-(N-3),N-3], [0,4,ALL-4] ] POWA=[A] # 漸化式を行列累乗で求める(ダブリング) for i in range(60): POWA.append(prod(POWA[-1],POWA[-1],3,3,3)) # ベキを求めて X=[[1,0,0]] n=M while n: X=prod(X,POWA[n.bit_length()-1],1,3,3) # n乗の場合 n-=1<<(n.bit_length()-1) #print(X) AS=0 for i in range(N): AS=AS+i*(i+1)//2 PX=[0] for i in range(1,N+1): plus=(N-i)*(N-i+1)//2 minus=(i-1)*(i-1+1)//2 PX.append(plus+minus) ANS=0 for i in range(N-1): a,b=P[i],P[i+1] ANS+=abs(b-a)*X[0][0] k=PX[a]-abs(b-a)+PX[b]-abs(b-a) ANS+=k*X[0][1]*pow((N-2)*2,mod-2,mod)%mod ANS+=(AS-abs(b-a)-k)*X[0][2]*pow(ALL-1-(N-2)*2,mod-2,mod)%mod ANS%=mod print(ANS%mod) import sys input = sys.stdin.readline mod=998244353 # 行列の計算(numpyを使えないとき,modを使用) def prod(A,B,k,l,m):# A:k*l,B:l*m C=[[None for i in range(m)] for j in range(k)] for i in range(k): for j in range(m): ANS=0 for pl in range(l): ANS=(ANS+A[i][pl]*B[pl][j])%mod C[i][j]=ANS return C def plus(A,B,k,l):# a,B:k*l C=[[None for i in range(l)] for j in range(k)] for i in range(k): for j in range(l): C[i][j]=(A[i][j]+B[i][j])%mod return C N,M=map(int,input().split()) P=list(map(int,input().split())) if N==2: print(1) exit() if N==3: A=[[2,1],[2,1]] POWA=[A] for i in range(60): POWA.append(prod(POWA[-1],POWA[-1],2,2,2)) # ベキを求めて n=M X=[0,0] X[0]+=abs(P[1]-P[0])+abs(P[2]-P[1]) X[1]=4-X[0] X=[X] while n: X=prod(X,POWA[n.bit_length()-1],1,2,2) # n乗の場合 n-=1<<(n.bit_length()-1) print(X[0][0]) exit() ALL=N*(N-1)//2 A=[[ALL-(N-2)*2,(N-2)*2,0], [1,ALL-1-(N-3),N-3], [0,4,ALL-4] ] POWA=[A] # 漸化式を行列累乗で求める(ダブリング) for i in range(60): POWA.append(prod(POWA[-1],POWA[-1],3,3,3)) # ベキを求めて X=[[1,0,0]] n=M while n: X=prod(X,POWA[n.bit_length()-1],1,3,3) # n乗の場合 n-=1<<(n.bit_length()-1) #print(X) AS=0 for i in range(N): AS=AS+i*(i+1)//2 PX=[0] for i in range(1,N+1): plus=(N-i)*(N-i+1)//2 minus=(i-1)*(i-1+1)//2 PX.append(plus+minus) ANS=0 for i in range(N-1): a,b=P[i],P[i+1] ANS+=abs(b-a)*X[0][0] k=PX[a]-abs(b-a)+PX[b]-abs(b-a) ANS+=k*X[0][1]*pow((N-2)*2,mod-2,mod)%mod ANS+=(AS-abs(b-a)-k)*X[0][2]*pow(ALL-1-(N-2)*2,mod-2,mod)%mod ANS%=mod print(ANS%mod)
ConDefects/ConDefects/Code/arc176_d/Python/52897913
condefects-python_data_44
import sys import heapq def main(): N = int(input()) tl = [] for _ in range(N): T, D = map(int, input().split()) tl.append([T, T+D]) tl.sort() q = [] time = tl[0][0] ind = 0 heapq.heapify(q) ans = 0 while ind < N or q: if ind < N and time < tl[ind][0]: # 次の時間が先 time = tl[ind][0] # その時間にする while ind < N and time == tl[ind][0]: heapq.heappush(q, tl[ind][1]) ind += 1 while q: g = heapq.heappop(q) if g >= time: ans += 1 break time += 1 print(ans) if __name__ == '__main__': main() import sys import heapq def main(): N = int(input()) tl = [] for _ in range(N): T, D = map(int, input().split()) tl.append([T, T+D]) tl.sort() q = [] time = tl[0][0] ind = 0 heapq.heapify(q) ans = 0 while ind < N or q: if len(q) == 0 and ind < N and time < tl[ind][0]: # 次の時間が先 time = tl[ind][0] # その時間にする while ind < N and time == tl[ind][0]: heapq.heappush(q, tl[ind][1]) ind += 1 while q: g = heapq.heappop(q) if g >= time: ans += 1 break time += 1 print(ans) if __name__ == '__main__': main()
ConDefects/ConDefects/Code/abc325_d/Python/52936269
condefects-python_data_45
N = int(input()) inout = [tuple(map(int, input().split())) for _ in range(N)] inout.sort() import heapq from collections import defaultdict D = defaultdict(list) for t, d in inout: D[t].append(t + d) INF = 10 ** 18 + 1 event = sorted(D.keys()) event.append(INF) ans = 0 now = 1 cnt = 0 hq = [] while now < INF: if now in D: cnt += 1 for t in D[now]: heapq.heappush(hq, t) while hq: t = heapq.heappop(hq) if now <= t: ans += 1 now += 1 break else: now = event[cnt] print(ans) N = int(input()) inout = [tuple(map(int, input().split())) for _ in range(N)] inout.sort() import heapq from collections import defaultdict D = defaultdict(list) for t, d in inout: D[t].append(t + d) INF = 10 ** 20 event = sorted(D.keys()) event.append(INF) ans = 0 now = 1 cnt = 0 hq = [] while now < INF: if now in D: cnt += 1 for t in D[now]: heapq.heappush(hq, t) while hq: t = heapq.heappop(hq) if now <= t: ans += 1 now += 1 break else: now = event[cnt] print(ans)
ConDefects/ConDefects/Code/abc325_d/Python/53574782
condefects-python_data_46
from heapq import heappush, heappop n = int(input()) arr = [list(map(int, input().split())) for _ in range(n)] arr = sorted(arr) heap = [] i, cnt, t = 0, 0, 1 while i < len(arr): if t < arr[i][0]: t = arr[i][0] while i < len(arr) and arr[i][0] <= t: heappush(heap, (arr[i][0] + arr[i][1])) i += 1 while len(heap): x = heappop(heap) if x < t: continue t += 1 cnt += 1 break while len(heap): if heappop(heap) >= t: t += 1 cnt += 1 print(cnt) from heapq import heappush, heappop n = int(input()) arr = [list(map(int, input().split())) for _ in range(n)] arr = sorted(arr) heap = [] i, cnt, t = 0, 0, 1 while i < len(arr): if len(heap) == 0 and t < arr[i][0]: t = arr[i][0] while i < len(arr) and arr[i][0] <= t: heappush(heap, (arr[i][0] + arr[i][1])) i += 1 while len(heap): x = heappop(heap) if x < t: continue t += 1 cnt += 1 break while len(heap): if heappop(heap) >= t: t += 1 cnt += 1 print(cnt)
ConDefects/ConDefects/Code/abc325_d/Python/53458224
condefects-python_data_47
import sys input = lambda: sys.stdin.readline().strip() from collections import defaultdict d = defaultdict(int) n=int(input()) a=list(map(int,input().split())) m=200005 for i in range(n): d[a[i]]+=1 ans=0 for i in range(1,m): for j in range((m-1)//i): k=i*j ans+=d[k]*d[i]*d[j] print(ans) import sys input = lambda: sys.stdin.readline().strip() from collections import defaultdict d = defaultdict(int) n=int(input()) a=list(map(int,input().split())) m=200005 for i in range(n): d[a[i]]+=1 ans=0 for i in range(1,m): for j in range(1,1+m//i): k=i*j ans+=d[k]*d[i]*d[j] print(ans)
ConDefects/ConDefects/Code/abc249_d/Python/44452633
condefects-python_data_48
n = 10**12 def primes(n): ass = [] is_prime = [True] * (n+1) is_prime[0] = False is_prime[1] = False for i in range(2, int(n**0.5)+1): if not is_prime[i]: continue for j in range(i*2, n+1, i): is_prime[j] = False for i in range(len(is_prime)): if is_prime[i]: ass.append(i) return ass x = int((n/12)**0.5) p = primes(x) m = len(p) ans = 0 for b in range(1,m-1): for c in range(b + 1, m): if p[b] * p[c]**2 > n: break for a in range(b): if p[a] **2 * p[b] * p[c]**2 > n: break else: ans += 1 print(ans) n = int(input()) def primes(n): ass = [] is_prime = [True] * (n+1) is_prime[0] = False is_prime[1] = False for i in range(2, int(n**0.5)+1): if not is_prime[i]: continue for j in range(i*2, n+1, i): is_prime[j] = False for i in range(len(is_prime)): if is_prime[i]: ass.append(i) return ass x = int((n/12)**0.5) p = primes(x) m = len(p) ans = 0 for b in range(1,m-1): for c in range(b + 1, m): if p[b] * p[c]**2 > n: break for a in range(b): if p[a] **2 * p[b] * p[c]**2 > n: break else: ans += 1 print(ans)
ConDefects/ConDefects/Code/abc300_d/Python/45256387
condefects-python_data_49
def extgcd(a, b): if b: d, y, x = extgcd(b, a % b) y -= (a // b) * x return d, x, y return a, 1, 0 # V = [(X_i, Y_i), ...]: X_i (mod Y_i) def remainder(V): x = 0; d = 1 for X, Y in V: g, a, b = extgcd(d, Y) x, d = (Y*b*x + d*a*X) // g, d*(Y // g) x %= d return x, d #以下modinv def mod_inv(a, m): g, x, y = extgcd(a, m) if g != 1: raise Exception("a and m are not coprime.") if x < 0: x += m return x #####segfunc##### def Sum(x, y): return x+y ################# class SegTree: """ init(init_val, ide_ele): 配列init_valで初期化 O(N) update(k, x): k番目の値をxに更新 O(logN) query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN) """ def __init__(self, init_val, segfunc, ide_ele): """ init_val: 配列の初期値 segfunc: 区間にしたい操作 ide_ele: 単位元 n: 要素数 num: n以上の最小の2のべき乗 tree: セグメント木(1-index) """ n = len(init_val) self.segfunc = segfunc self.ide_ele = ide_ele self.num = 1 << (n - 1).bit_length() self.tree = [ide_ele] * 2 * self.num # 配列の値を葉にセット for i in range(n): self.tree[self.num + i] = init_val[i] # 構築していく for i in range(self.num - 1, 0, -1): self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1]) def update(self, k, x): """ k番目の値をxに更新 k: index(0-index) x: update value """ k += self.num self.tree[k] = x while k > 1: self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1]) k >>= 1 def query(self, l, r): """ [l, r)のsegfuncしたものを得る l: index(0-index) r: index(0-index) """ res = self.ide_ele l += self.num r += self.num while l < r: if l & 1: res = self.segfunc(res, self.tree[l]) l += 1 if r & 1: res = self.segfunc(res, self.tree[r - 1]) l >>= 1 r >>= 1 return res def setValue(self,x,i): self.update(x-1,i) def getMax(self,i,j): return self.query(i-1,j) import sys input=sys.stdin.readline N,Q=map(int,input().split()) A=list(map(int,input().split())) Ai=[] Aii=[] for i in range(N): Ai.append(A[i]*(i+1)) Aii.append(A[i]*(i+1)*(i+1)) sta=SegTree(A,Sum,0) stai=SegTree(Ai,Sum,0) staii=SegTree(Aii,Sum,0) p=998244353 for _ in range(Q): q=list(map(int,input().split())) if q[0]==1: q,x,v=q sta.update(x-1,v) stai.update(x-1,v*x) staii.update(x-1,v*x*x) else: q,x=q print((sta.getMax(0,x-1)*(x+1)*(x+2)-stai.getMax(0,x-1)*(2*x+3)+staii.getMax(0,x-1))*mod_inv(2,p)%p) def extgcd(a, b): if b: d, y, x = extgcd(b, a % b) y -= (a // b) * x return d, x, y return a, 1, 0 # V = [(X_i, Y_i), ...]: X_i (mod Y_i) def remainder(V): x = 0; d = 1 for X, Y in V: g, a, b = extgcd(d, Y) x, d = (Y*b*x + d*a*X) // g, d*(Y // g) x %= d return x, d #以下modinv def mod_inv(a, m): g, x, y = extgcd(a, m) if g != 1: raise Exception("a and m are not coprime.") if x < 0: x += m return x #####segfunc##### def Sum(x, y): return x+y ################# class SegTree: """ init(init_val, ide_ele): 配列init_valで初期化 O(N) update(k, x): k番目の値をxに更新 O(logN) query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN) """ def __init__(self, init_val, segfunc, ide_ele): """ init_val: 配列の初期値 segfunc: 区間にしたい操作 ide_ele: 単位元 n: 要素数 num: n以上の最小の2のべき乗 tree: セグメント木(1-index) """ n = len(init_val) self.segfunc = segfunc self.ide_ele = ide_ele self.num = 1 << (n - 1).bit_length() self.tree = [ide_ele] * 2 * self.num # 配列の値を葉にセット for i in range(n): self.tree[self.num + i] = init_val[i] # 構築していく for i in range(self.num - 1, 0, -1): self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1]) def update(self, k, x): """ k番目の値をxに更新 k: index(0-index) x: update value """ k += self.num self.tree[k] = x while k > 1: self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1]) k >>= 1 def query(self, l, r): """ [l, r)のsegfuncしたものを得る l: index(0-index) r: index(0-index) """ res = self.ide_ele l += self.num r += self.num while l < r: if l & 1: res = self.segfunc(res, self.tree[l]) l += 1 if r & 1: res = self.segfunc(res, self.tree[r - 1]) l >>= 1 r >>= 1 return res def setValue(self,x,i): self.update(x-1,i) def getMax(self,i,j): return self.query(i-1,j) import sys input=sys.stdin.readline N,Q=map(int,input().split()) A=list(map(int,input().split())) Ai=[] Aii=[] for i in range(N): Ai.append(A[i]*(i+1)) Aii.append(A[i]*(i+1)*(i+1)) sta=SegTree(A,Sum,0) stai=SegTree(Ai,Sum,0) staii=SegTree(Aii,Sum,0) p=998244353 for _ in range(Q): q=list(map(int,input().split())) if q[0]==1: q,x,v=q sta.update(x-1,v) stai.update(x-1,v*x) staii.update(x-1,v*x*x) else: q,x=q print((sta.getMax(1,x)*(x+1)*(x+2)-stai.getMax(1,x)*(2*x+3)+staii.getMax(1,x))*mod_inv(2,p)%p)
ConDefects/ConDefects/Code/abc256_f/Python/41932099
condefects-python_data_50
#!/usr/bin/env python3 import sys from bisect import bisect_left, bisect_right, insort_left, insort_right # type: ignore from collections import Counter, defaultdict, deque # type: ignore from math import gcd, sqrt # type: ignore from heapq import heapify, heappop, heappush, heappushpop, heapreplace, merge # type: ignore from itertools import accumulate, combinations, permutations, product # type: ignore def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def I(): return int(sys.stdin.buffer.readline()) def LS(): return sys.stdin.buffer.readline().rstrip().decode("utf-8").split() def S(): return sys.stdin.buffer.readline().rstrip().decode("utf-8") def IR(n): return [I() for _ in range(n)] def LIR(n): return [LI() for _ in range(n)] def SR(n): return [S() for _ in range(n)] def LSR(n): return [LS() for _ in range(n)] def SRL(n): return [list(S()) for _ in range(n)] #####segfunc##### def segfunc(x, y): return (x+y)%MOD ################# #####ide_ele##### ide_ele = 0 ################# class SegTree: """ init(init_val, ide_ele): 配列init_valで初期化 O(N) update(k, x): k番目の値をxに更新 O(logN) query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN) """ def __init__(self, init_val, segfunc, ide_ele): """ init_val: 配列の初期値 segfunc: 区間にしたい操作 ide_ele: 単位元 n: 要素数 num: n以上の最小の2のべき乗 tree: セグメント木(1-index) """ n = len(init_val) self.segfunc = segfunc self.ide_ele = ide_ele self.num = 1 << (n - 1).bit_length() self.tree = [ide_ele] * 2 * self.num # 配列の値を葉にセット for i in range(n): self.tree[self.num + i] = init_val[i] # 構築していく for i in range(self.num - 1, 0, -1): self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1]) def update(self, k, x): """ k番目の値をxに更新 k: index(0-index) x: update value """ k += self.num self.tree[k] = x while k > 1: self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1]) k >>= 1 def query(self, l, r): """ [l, r)のsegfuncしたものを得る l: index(0-index) r: index(0-index) """ res = self.ide_ele l += self.num r += self.num while l < r: if l & 1: res = self.segfunc(res, self.tree[l]) l += 1 if r & 1: res = self.segfunc(res, self.tree[r - 1]) l >>= 1 r >>= 1 return res N,Q = LI() A = [0]+LI() MOD = 998244353 aj = SegTree(A,segfunc,ide_ele) jaj = SegTree([i*v for i,v in enumerate(A)],segfunc,ide_ele) jjaj = SegTree([i*i*v for i,v in enumerate(A)],segfunc,ide_ele) for _ in range(Q): q = LI() assert jaj.query(0,1)==0 assert jjaj.query(0,1)==0 if q[0]==1: x,v = q[1:] aj.update(x,v) jaj.update(x,x*v) jjaj.update(x,x*x*v) else: x = q[1] tmp = jjaj.query(0,x+1)-(2*x+3)*jaj.query(0,x+1)+(x+1)*(x+2)*aj.query(0,x+1) print((tmp//2)%MOD) #!/usr/bin/env python3 import sys from bisect import bisect_left, bisect_right, insort_left, insort_right # type: ignore from collections import Counter, defaultdict, deque # type: ignore from math import gcd, sqrt # type: ignore from heapq import heapify, heappop, heappush, heappushpop, heapreplace, merge # type: ignore from itertools import accumulate, combinations, permutations, product # type: ignore def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def I(): return int(sys.stdin.buffer.readline()) def LS(): return sys.stdin.buffer.readline().rstrip().decode("utf-8").split() def S(): return sys.stdin.buffer.readline().rstrip().decode("utf-8") def IR(n): return [I() for _ in range(n)] def LIR(n): return [LI() for _ in range(n)] def SR(n): return [S() for _ in range(n)] def LSR(n): return [LS() for _ in range(n)] def SRL(n): return [list(S()) for _ in range(n)] #####segfunc##### def segfunc(x, y): return (x+y)%MOD ################# #####ide_ele##### ide_ele = 0 ################# class SegTree: """ init(init_val, ide_ele): 配列init_valで初期化 O(N) update(k, x): k番目の値をxに更新 O(logN) query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN) """ def __init__(self, init_val, segfunc, ide_ele): """ init_val: 配列の初期値 segfunc: 区間にしたい操作 ide_ele: 単位元 n: 要素数 num: n以上の最小の2のべき乗 tree: セグメント木(1-index) """ n = len(init_val) self.segfunc = segfunc self.ide_ele = ide_ele self.num = 1 << (n - 1).bit_length() self.tree = [ide_ele] * 2 * self.num # 配列の値を葉にセット for i in range(n): self.tree[self.num + i] = init_val[i] # 構築していく for i in range(self.num - 1, 0, -1): self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1]) def update(self, k, x): """ k番目の値をxに更新 k: index(0-index) x: update value """ k += self.num self.tree[k] = x while k > 1: self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1]) k >>= 1 def query(self, l, r): """ [l, r)のsegfuncしたものを得る l: index(0-index) r: index(0-index) """ res = self.ide_ele l += self.num r += self.num while l < r: if l & 1: res = self.segfunc(res, self.tree[l]) l += 1 if r & 1: res = self.segfunc(res, self.tree[r - 1]) l >>= 1 r >>= 1 return res N,Q = LI() A = [0]+LI() MOD = 998244353 aj = SegTree(A,segfunc,ide_ele) jaj = SegTree([i*v for i,v in enumerate(A)],segfunc,ide_ele) jjaj = SegTree([i*i*v for i,v in enumerate(A)],segfunc,ide_ele) for _ in range(Q): q = LI() assert jaj.query(0,1)==0 assert jjaj.query(0,1)==0 if q[0]==1: x,v = q[1:] aj.update(x,v) jaj.update(x,x*v) jjaj.update(x,x*x*v) else: x = q[1] tmp = jjaj.query(0,x+1)-(2*x+3)*jaj.query(0,x+1)+(x+1)*(x+2)*aj.query(0,x+1) print((tmp*pow(2,MOD-2,MOD))%MOD)
ConDefects/ConDefects/Code/abc256_f/Python/42749759
condefects-python_data_51
if __name__ == '__main__': s = str(input()) n = int(input()) m = len(s) res = 0 for i in range(m): if s[i] == "1": res += (1 << (m - 1 - i)) if res > n: res = -1 else: for i in range(m - 1, -1, -1): if s[i] == "?" and res + (1 << (m - 1 - i)) <= n: res += (1 << (m - 1 - i)) print(res) if __name__ == '__main__': s = str(input()) n = int(input()) m = len(s) res = 0 for i in range(m): if s[i] == "1": res += (1 << (m - 1 - i)) if res > n: res = -1 else: for i in range(m): if s[i] == "?" and res + (1 << (m - 1 - i)) <= n: res += (1 << (m - 1 - i)) print(res)
ConDefects/ConDefects/Code/abc301_d/Python/45723653
condefects-python_data_52
def solve(): s = input() n = int(input()) ans = int(s.replace('?', '0'), 2) print(ans) if ans > n: print(-1) return m = len(s) for i, c in enumerate(s): if c == '?' and ans + (1 << (m - 1 - i)) <= n: ans += (1 << (m - 1 - i)) return solve() def solve(): s = input() n = int(input()) ans = int(s.replace('?', '0'), 2) if ans > n: print(-1) return m = len(s) for i, c in enumerate(s): if c == '?' and ans + (1 << (m - 1 - i)) <= n: ans += (1 << (m - 1 - i)) print(ans) return solve()
ConDefects/ConDefects/Code/abc301_d/Python/45732372
condefects-python_data_53
import sys from functools import lru_cache, cmp_to_key from itertools import accumulate from collections import defaultdict, Counter, deque from math import inf, sqrt, isqrt, ceil, comb from bisect import bisect_left, bisect_right from sortedcontainers import SortedSet, SortedList # mod = 998244353 def main(): # 2023-09-19 15:39:55 s = input() n = int(input()) res = int(s.replace("?", "0"), 2) if res > n: print(-1) return for i, ch in enumerate(s): d = len(s) - 1 - i if ch == "?" and (res + 1) << d <= n: res += 1 << d print(res) def input() -> str: return sys.stdin.readline().rstrip() def read_ints(): return map(int, input().split()) if __name__ == "__main__": main() import sys from functools import lru_cache, cmp_to_key from itertools import accumulate from collections import defaultdict, Counter, deque from math import inf, sqrt, isqrt, ceil, comb from bisect import bisect_left, bisect_right from sortedcontainers import SortedSet, SortedList # mod = 998244353 def main(): # 2023-09-19 15:39:55 s = input() n = int(input()) res = int(s.replace("?", "0"), 2) if res > n: print(-1) return for i, ch in enumerate(s): d = len(s) - 1 - i if ch == "?" and res + (1 << d) <= n: res += 1 << d print(res) def input() -> str: return sys.stdin.readline().rstrip() def read_ints(): return map(int, input().split()) if __name__ == "__main__": main()
ConDefects/ConDefects/Code/abc301_d/Python/45725346
condefects-python_data_54
import sys from collections import deque, defaultdict from math import * from bisect import bisect_left, bisect_right input = sys.stdin.readline def get(s1, s2): global n i = 0 while i < len(s1) and (s1[i] == '?' or s1[i] == s2[i]): i += 1 if i == len(s1): return n else: if s1[i] < s2[i]: v = 0 for j in range(i): v = v * 2 + int(s2[j]) for j in range(i, len(s1)): if s1[j] == '0': v = v * 2 else: v = v * 2 + 1 return v else: j = i - 1 while j >= 0 and (s1[j] != '?' or s2[j] == '0'): j -= 1 if j == -1: return -1 else: v = 0 for k in range(j): v = v * 2 + int(s2[k]) v = v * 2 for k in range(j+1, len(s1)): if s1[k] == '0': v = v << 1 else: v = v * 2 + 1 return v if __name__ == "__main__": s1 = input().strip() n = int(input()) s2 = bin(n)[2:] i = 0 while i < len(s1) - 1 and s1[i] == '0': i += 1 s1 = s1[i:] v = 0 if len(s1) < len(s2): for x in s1: if x == '0': v = v * 2 else: v = v * 2 + 1 elif len(s1) == len(s2): v = get(s1, s2) else: le = len(s1) - len(s2) if s1[:le] != '?' * le: v = -1 else: v = get(s1[le:], s2) print(v) import sys from collections import deque, defaultdict from math import * from bisect import bisect_left, bisect_right input = sys.stdin.readline def get(s1, s2): global n i = 0 while i < len(s1) and (s1[i] == '?' or s1[i] == s2[i]): i += 1 if i == len(s1): return n else: if s1[i] < s2[i]: v = 0 for j in range(i): v = v * 2 + int(s2[j]) for j in range(i, len(s1)): if s1[j] == '0': v = v * 2 else: v = v * 2 + 1 return v else: j = i - 1 while j >= 0 and (s1[j] != '?' or s2[j] == '0'): j -= 1 if j == -1: return -1 else: v = 0 for k in range(j): v = v * 2 + int(s2[k]) v = v * 2 for k in range(j+1, len(s1)): if s1[k] == '0': v = v << 1 else: v = v * 2 + 1 return v if __name__ == "__main__": s1 = input().strip() n = int(input()) s2 = bin(n)[2:] i = 0 while i < len(s1) - 1 and s1[i] == '0': i += 1 s1 = s1[i:] v = 0 if len(s1) < len(s2): for x in s1: if x == '0': v = v * 2 else: v = v * 2 + 1 elif len(s1) == len(s2): v = get(s1, s2) else: le = len(s1) - len(s2) if s1[:le].count('1') > 0: v = -1 else: v = get(s1[le:], s2) print(v)
ConDefects/ConDefects/Code/abc301_d/Python/45747617
condefects-python_data_55
def get_min(s): mask = 1 val = 0 for ch in s[::-1]: if ch == '1': val += mask mask <<= 1 return val def I(): return input() def II(): return int(input()) def solve(): s = I() n = II() min_val = get_min(s) mask = 1 << (len(s) - 1) if min_val > n: return -1 ans = 0 for ch in s: if ch == '?' and min_val + mask <= n: ans += mask n -= mask if ch == '1': min_val -= mask mask >>= 1 return ans print(solve()) def get_min(s): mask = 1 val = 0 for ch in s[::-1]: if ch == '1': val += mask mask <<= 1 return val def I(): return input() def II(): return int(input()) def solve(): s = I() n = II() min_val = get_min(s) mask = 1 << (len(s) - 1) if min_val > n: return -1 ans = 0 for ch in s: if ch == '?' and min_val + mask <= n: ans += mask n -= mask if ch == '1': min_val -= mask ans += mask n -= mask mask >>= 1 return ans print(solve())
ConDefects/ConDefects/Code/abc301_d/Python/45718315
condefects-python_data_56
n,m = map(int,input().split()) d = [[0] * (n+2) for _ in range(n+2)] c = [0] * (n+1) d[0][0] = c[0] = 1 M = 998244353 for i in range(1,n+1): for s in range(i,n+1): d[i][s] += d[i][s-i] + c[s-i] for s in range(n+1): c[s] += d[i][s] - d[i-m][s] c[s] %= M print(d[i][n]) n,m = map(int,input().split()) d = [[0] * (n+2) for _ in range(n+2)] c = [0] * (n+1) d[0][0] = c[0] = 1 M = 998244353 for i in range(1,n+1): for s in range(i,n+1): d[i][s] += d[i][s-i] + c[s-i] for s in range(n+1): c[s] += d[i][s] - d[i-m][s] c[s] %= M print(d[i][n] % M)
ConDefects/ConDefects/Code/abc221_h/Python/40746464
condefects-python_data_57
N,M = map(int,input().split()) MOD = 998244353 dp = [[0]*(N+1) for _ in range(N+1)] #dp[i][j]: i個使ってその和がj dp[0][0] = 1 S = [[0,0] for _ in range(N+1)] S[0][1] = 1 for i in range(1,N+1): for j in range(N+1): nj = j-i if nj < 0: S[j].append(S[j][-1]) continue k_max = min(M,i) #1が最大で何個入っているか print(i,j,nj,k_max) dp[i][j] = (S[nj][-1]-S[nj][-1-k_max-1])%MOD S[j].append((S[j][-1]+dp[i][j])%MOD) for i in range(1,N+1): print(dp[i][-1]) N,M = map(int,input().split()) MOD = 998244353 dp = [[0]*(N+1) for _ in range(N+1)] #dp[i][j]: i個使ってその和がj dp[0][0] = 1 S = [[0,0] for _ in range(N+1)] S[0][1] = 1 for i in range(1,N+1): for j in range(N+1): nj = j-i if nj < 0: S[j].append(S[j][-1]) continue k_max = min(M,i) #1が最大で何個入っているか #print(i,j,nj,k_max) dp[i][j] = (S[nj][-1]-S[nj][-1-k_max-1])%MOD S[j].append((S[j][-1]+dp[i][j])%MOD) for i in range(1,N+1): print(dp[i][-1])
ConDefects/ConDefects/Code/abc221_h/Python/26325908
condefects-python_data_58
import sys readline = sys.stdin.readline n,m = map(int,readline().split()) dp = [[0]*(n+1) for _ in range(n+1)] acc = [[0]*(n+1) for _ in range(n+1)] dp[0][0] = 1 acc[0] = [1]*(n+1) MOD = 9998244353 for x in range(1,n+1): dpx = dp[x] for y in range(1,n+1): dpx[y] = (acc[x-y][y] - (0 if y-m-1 < 0 else acc[x-y][y-m-1]))%MOD acc[x][y] = (acc[x][y-1]+dpx[y])%MOD print(*dp[n][1:],sep="\n") import sys readline = sys.stdin.readline n,m = map(int,readline().split()) dp = [[0]*(n+1) for _ in range(n+1)] acc = [[0]*(n+1) for _ in range(n+1)] dp[0][0] = 1 acc[0] = [1]*(n+1) MOD = 998244353 for x in range(1,n+1): dpx = dp[x] for y in range(1,n+1): dpx[y] = (acc[x-y][y] - (0 if y-m-1 < 0 else acc[x-y][y-m-1]))%MOD acc[x][y] = (acc[x][y-1]+dpx[y])%MOD print(*dp[n][1:],sep="\n")
ConDefects/ConDefects/Code/abc221_h/Python/30564189
condefects-python_data_59
#!usr/bin/env python3 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 IR(n): return [I() for _ in range(n)] def LIR(n): return [LI() for _ in range(n)] sys.setrecursionlimit(1000000) mod = 998244353 def solve(): n,m = LI() dp = [[0]*(n+1) for _ in range(n+1)] dp[0][0] = 1 for i in range(1,n+1): ndp = dp[i] ndp[1] = 1 for j in range(2,i+1): ndp[j] = dp[i-j][j]+dp[i-1][j-1] if i > m: ndp[j] -= dp[i-j][j-1-m] ndp[j] %= mod for i in dp[n][1:]: print(i) return if __name__ == "__main__": solve() #!usr/bin/env python3 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 IR(n): return [I() for _ in range(n)] def LIR(n): return [LI() for _ in range(n)] sys.setrecursionlimit(1000000) mod = 998244353 def solve(): n,m = LI() dp = [[0]*(n+1) for _ in range(n+1)] dp[0][0] = 1 for i in range(1,n+1): ndp = dp[i] ndp[1] = 1 for j in range(2,i+1): ndp[j] = dp[i-j][j]+dp[i-1][j-1] if j >= m: ndp[j] -= dp[i-j][j-1-m] ndp[j] %= mod for i in dp[n][1:]: print(i) return if __name__ == "__main__": solve()
ConDefects/ConDefects/Code/abc221_h/Python/26320903
condefects-python_data_60
import sys input = sys.stdin.readline N,M=map(int,input().split()) mod=998244353 # ヤング図形を用いて考えるのが重要 # ヤング図形に置き換えた後はDP DP=[[0]*(N+5) for i in range(N+5)] # DP[i][j]で、今までの総和がi、今まで使った段差がj個。 # DP[i+j][j]+=DP[i][j] : 今までの段差全てに+1 # DP[i+k+j][j+k] += DP[i][j] : 今までの段差全てに+1し、さらにk<=M個一段のものを加える。 DP[1][1]=1 DP[1+M+1][1+M+1]=-1 for i in range(1,N+5): for j in range(1,N+5): DP[i][j]+=DP[i-1][j-1] DP[i][j]%=mod if i+j<N+5: DP[i+j][j]+=DP[i][j] DP[i+j][j]%=mod if i+M+1+j<N+5 and j+M+1<N+5: DP[i+M+1+j][j+M+1]-=DP[i][j] DP[i+M+1+j][j+M+1]%=mod print(*DP[N][1:N+1]) import sys input = sys.stdin.readline N,M=map(int,input().split()) mod=998244353 # ヤング図形を用いて考えるのが重要 # ヤング図形に置き換えた後はDP DP=[[0]*(N+5) for i in range(N+5)] # DP[i][j]で、今までの総和がi、今まで使った段差がj個。 # DP[i+j][j]+=DP[i][j] : 今までの段差全てに+1 # DP[i+k+j][j+k] += DP[i][j] : 今までの段差全てに+1し、さらにk<=M個一段のものを加える。 DP[1][1]=1 DP[1+M][1+M]=-1 for i in range(1,N+5): for j in range(1,N+5): DP[i][j]+=DP[i-1][j-1] DP[i][j]%=mod if i+j<N+5: DP[i+j][j]+=DP[i][j] DP[i+j][j]%=mod if i+M+1+j<N+5 and j+M+1<N+5: DP[i+M+1+j][j+M+1]-=DP[i][j] DP[i+M+1+j][j+M+1]%=mod print(*DP[N][1:N+1])
ConDefects/ConDefects/Code/abc221_h/Python/26785022
condefects-python_data_61
import math from heapq import heapify, heappop, heappush # import bisect import sys input = sys.stdin.readline sys.setrecursionlimit(10**6) def ipt(num=0): if num==1:return int(input()) elif num==2:return map(int, input().split()) elif num==3:return input().rstrip() return list(map(int, input().split())) # mod = 998244353 # d = [[] for i in range(n)] # for i in range(n): n=ipt(1) a=ipt() class SegTree(): def __init__(self,n) -> None: self.N0 = 2**(n-1).bit_length() self.INF = 2**60 self.data = [self.INF]*(2*self.N0) # a_k の値を x に更新 def update(self, k, x): k += self.N0-1 self.data[k] = x while k >= 0: k = (k - 1) // 2 self.data[k] = min(self.data[2*k+1], self.data[2*k+2]) # 区間[l, r)の最小値 def query(self, l, r): L = l + self.N0; R = r + self.N0 s = self.INF while L < R: if R & 1: R -= 1 s = min(s, self.data[R-1]) if L & 1: s = min(s, self.data[L-1]) L += 1 L >>= 1; R >>= 1 return s inf=1<<40 l=[inf]*n st=SegTree(n+1);st2=SegTree(n+1) st.update(a[0],-a[0]-1) st2.update(a[0],a[0]-1) for i in range(1,n): s=st.query(0,a[i])+a[i]+i+1 ss=st2.query(a[i]+1,n+1)-a[i]+i+1 l[i]=min(s,ss) st.update(a[i],-a[i]-i-1) st2.update(a[i],a[i]-i-1) st=SegTree(n+1);st2=SegTree(n+1) st.update(a[n-1],-a[n-1]+n) st.update(a[n-1],a[n-1]+n) for i in range(n-2,-1,-1): s=st.query(0,a[i])+a[i]-i-1 ss=st2.query(a[i]+1,n+1)-a[i]-i-1 s=min(s,ss);l[i]=min(s,l[i]) st.update(a[i],-a[i]+i+1) st2.update(a[i],a[i]+i+1) print(*l) import math from heapq import heapify, heappop, heappush # import bisect import sys input = sys.stdin.readline sys.setrecursionlimit(10**6) def ipt(num=0): if num==1:return int(input()) elif num==2:return map(int, input().split()) elif num==3:return input().rstrip() return list(map(int, input().split())) # mod = 998244353 # d = [[] for i in range(n)] # for i in range(n): n=ipt(1) a=ipt() class SegTree(): def __init__(self,n) -> None: self.N0 = 2**(n-1).bit_length() self.INF = 2**60 self.data = [self.INF]*(2*self.N0) # a_k の値を x に更新 def update(self, k, x): k += self.N0-1 self.data[k] = x while k >= 0: k = (k - 1) // 2 self.data[k] = min(self.data[2*k+1], self.data[2*k+2]) # 区間[l, r)の最小値 def query(self, l, r): L = l + self.N0; R = r + self.N0 s = self.INF while L < R: if R & 1: R -= 1 s = min(s, self.data[R-1]) if L & 1: s = min(s, self.data[L-1]) L += 1 L >>= 1; R >>= 1 return s inf=1<<40 l=[inf]*n st=SegTree(n+1);st2=SegTree(n+1) st.update(a[0],-a[0]-1) st2.update(a[0],a[0]-1) for i in range(1,n): s=st.query(0,a[i])+a[i]+i+1 ss=st2.query(a[i]+1,n+1)-a[i]+i+1 l[i]=min(s,ss) st.update(a[i],-a[i]-i-1) st2.update(a[i],a[i]-i-1) st=SegTree(n+1);st2=SegTree(n+1) st.update(a[n-1],-a[n-1]+n) st2.update(a[n-1],a[n-1]+n) for i in range(n-2,-1,-1): s=st.query(0,a[i])+a[i]-i-1 ss=st2.query(a[i]+1,n+1)-a[i]-i-1 s=min(s,ss);l[i]=min(s,l[i]) st.update(a[i],-a[i]+i+1) st2.update(a[i],a[i]+i+1) print(*l)
ConDefects/ConDefects/Code/abc283_f/Python/46910621
condefects-python_data_62
from collections import deque from collections import defaultdict import sys sys.setrecursionlimit(1000000) s=input() t=[] for i in s: t.append(i) k=0 for i in range(len(t)): if t[i]=="(": k+=1 elif t[i]==")": k-=1 else: if k%2==1: if ord(t[i])-ord("A")<26: t[i]=chr(ord(t[i])-ord("A")+ord("a")) else: t[i]=chr(ord(t[i])+ord("A")-ord("a")) #print(t) pi=defaultdict(int) ppi=defaultdict(int) kakko=[0 for i in range(len(t))] #kk=0 qu=[] for i in range(len(t)): if t[i]=="(": #kk+=1 qu.append(i) #pi[kk]=i #kakko[i]=kk elif t[i]==")": r=qu.pop(-1) ppi[i]=r ppi[r]=i print(ppi) #kakko[i]=-qu.pop(-1) #print(kakko) def kai(s,f,u): #print("p",s,f,u,"p") #if s==f: #return if u==0: i=s while s<=i<f: #for i in range(s,f): if t[i]=="(": p=ppi[i] #p=kakko.index(-kakko[i]) kai(i,p,1) i=p+1 elif t[i]==")": break else: print(t[i],end="") i+=1 else: i=f-1 while s<=i<f: #for i in reversed(range(s,f)): if t[i]==")": p=ppi[i] #p=kakko.index(-kakko[i]) kai(p+1,i,0) i=p-1 elif t[i]=="(": break else: print(t[i],end="") i-=1 kai(0,len(t),0) print() from collections import deque from collections import defaultdict import sys sys.setrecursionlimit(1000000) s=input() t=[] for i in s: t.append(i) k=0 for i in range(len(t)): if t[i]=="(": k+=1 elif t[i]==")": k-=1 else: if k%2==1: if ord(t[i])-ord("A")<26: t[i]=chr(ord(t[i])-ord("A")+ord("a")) else: t[i]=chr(ord(t[i])+ord("A")-ord("a")) #print(t) pi=defaultdict(int) ppi=defaultdict(int) kakko=[0 for i in range(len(t))] #kk=0 qu=[] for i in range(len(t)): if t[i]=="(": #kk+=1 qu.append(i) #pi[kk]=i #kakko[i]=kk elif t[i]==")": r=qu.pop(-1) ppi[i]=r ppi[r]=i #print(ppi) #kakko[i]=-qu.pop(-1) #print(kakko) def kai(s,f,u): #print("p",s,f,u,"p") #if s==f: #return if u==0: i=s while s<=i<f: #for i in range(s,f): if t[i]=="(": p=ppi[i] #p=kakko.index(-kakko[i]) kai(i,p,1) i=p+1 elif t[i]==")": break else: print(t[i],end="") i+=1 else: i=f-1 while s<=i<f: #for i in reversed(range(s,f)): if t[i]==")": p=ppi[i] #p=kakko.index(-kakko[i]) kai(p+1,i,0) i=p-1 elif t[i]=="(": break else: print(t[i],end="") i-=1 kai(0,len(t),0) print()
ConDefects/ConDefects/Code/abc350_f/Python/54219104
condefects-python_data_63
from collections import deque def swap_char(x, swap): if swap == 0: return x else: xu = x.upper() if x == xu: return x.lower() else: return xu S = list(input()) left_to_right = dict() d = deque() for i, s in enumerate(S): if s == "(": d.append(i) if s == ")": left_to_right[d.pop()] = i right_to_left = {v: k for k, v in left_to_right.items()} print(left_to_right) print(right_to_left) i = 0 swap = 0 while i < len(S): if swap == 0: if S[i] == "(": i = left_to_right[i] - 1 swap = 1 elif S[i] == ")": i = right_to_left[i] - 1 swap = 1 else: print(S[i], end="") i += 1 else: if S[i] == ")": i = right_to_left[i] + 1 swap = 0 elif S[i] == "(": i = left_to_right[i] + 1 swap = 0 else: print(swap_char(S[i], 1), end="") i -= 1 print("") from collections import deque def swap_char(x, swap): if swap == 0: return x else: xu = x.upper() if x == xu: return x.lower() else: return xu S = list(input()) left_to_right = dict() d = deque() for i, s in enumerate(S): if s == "(": d.append(i) if s == ")": left_to_right[d.pop()] = i right_to_left = {v: k for k, v in left_to_right.items()} i = 0 swap = 0 while i < len(S): if swap == 0: if S[i] == "(": i = left_to_right[i] - 1 swap = 1 elif S[i] == ")": i = right_to_left[i] - 1 swap = 1 else: print(S[i], end="") i += 1 else: if S[i] == ")": i = right_to_left[i] + 1 swap = 0 elif S[i] == "(": i = left_to_right[i] + 1 swap = 0 else: print(swap_char(S[i], 1), end="") i -= 1 print("")
ConDefects/ConDefects/Code/abc350_f/Python/53750325
condefects-python_data_64
N, X = map(int, input().split()) A = list(map(int,input().split())) Aset = set(A) ok = False for a in A: if X-a in Aset: ok = True break if ok: print('Yes') else: print('No') N, X = map(int, input().split()) A = list(map(int,input().split())) Aset = set(A) ok = False for a in A: if a + X in Aset: ok = True break if ok: print('Yes') else: print('No')
ConDefects/ConDefects/Code/abc296_c/Python/46012599
condefects-python_data_65
import heapq import math def dijkstra(graph,start): N = len(graph) dist = [float('inf')] * len(graph) dist[start] = 0 q = [(0,start)] while q: cost,current = heapq.heappop(q) if dist[current] < cost: continue if math.isfinite(dist[goal]): print(dist[goal]) exit() for i, weight in graph[current]: if cost + cost < dist[i]: dist[i] = cost + weight heapq.heappush(q, (dist[i],i,)) return dist N,M = map(int,input().split()) A = list(map(int,input().split())) B = list(map(int,input().split())) order = sorted(range(N), key=lambda i:B[i]) A = [A[i] for i in order] B = [B[i] for i in order] start = order.index(0) goal = order.index(N-1) graph = [[] for _ in range(2*N)] for i in range(N-1): graph[N+i].append((N+i+1,B[i+1]-B[i],)) import bisect for i in range(N): graph[i ].append((N,A[i]+B[0])) graph[i+N].append((i,0 )) idx = bisect.bisect_left(B,M-A[i]) if idx < N: graph[i].append((N+idx,A[i]+B[idx]-M)) dist = dijkstra(graph, start) print(dist[goal]) import heapq import math def dijkstra(graph,start): N = len(graph) dist = [float('inf')] * len(graph) dist[start] = 0 q = [(0,start)] while q: cost,current = heapq.heappop(q) if dist[current] < cost: continue if math.isfinite(dist[goal]): print(dist[goal]) exit() for i, weight in graph[current]: if cost + weight < dist[i]: dist[i] = cost + weight heapq.heappush(q, (dist[i],i,)) return dist N,M = map(int,input().split()) A = list(map(int,input().split())) B = list(map(int,input().split())) order = sorted(range(N), key=lambda i:B[i]) A = [A[i] for i in order] B = [B[i] for i in order] start = order.index(0) goal = order.index(N-1) graph = [[] for _ in range(2*N)] for i in range(N-1): graph[N+i].append((N+i+1,B[i+1]-B[i],)) import bisect for i in range(N): graph[i ].append((N,A[i]+B[0])) graph[i+N].append((i,0 )) idx = bisect.bisect_left(B,M-A[i]) if idx < N: graph[i].append((N+idx,A[i]+B[idx]-M)) dist = dijkstra(graph, start) print(dist[goal])
ConDefects/ConDefects/Code/abc232_g/Python/28970603
condefects-python_data_66
from bisect import bisect_left as bl from bisect import bisect_right as br from heapq import heapify, heappush as hpush, heappop as hpop def dijkstra(n, E, i0=0): kk = 18 mm = (1 << kk) - 1 h = [i0] D = [-1] * n done = [0] * n D[i0] = 0 while h: x = hpop(h) d, i = x >> kk, x & mm # di = hpop(h) # d, i = unpack("d", pack("q", (di | mm) - mm))[0], di & mm if done[i]: continue done[i] = 1 for j, w in E[i]: nd = d + w if D[j] < 0 or D[j] > nd: if done[j] == 0: hpush(h, (nd << kk) + j) # hpush(h, (unpack("q", pack("d", nd))[0] | mm) - (mm - j)) D[j] = nd return D N, M = map(int, input().split()) A = [int(a) for a in input().split()] B = [int(a) for a in input().split()] AB = sorted([(a, b, i) for i, (a, b) in enumerate(zip(A, B))], key = lambda x: x[1]) A = [ab[0] for ab in AB] B = [ab[1] for ab in AB] for j, (a, b, i) in enumerate(AB): if i == 0: s = j elif i == N - 1: t = j B.append(1 << 60) b0 = B[0] X = [[] for _ in range(2 * N + 1)] for i in range(N): X[i+N].append((i + 1 + N, B[i+1] - B[i])) X[i+N].append((i, 0)) for i, a in enumerate(A): j = bl(B, M - a) X[i].append((N, a + b0)) X[i].append((j + N, a + B[j] - M)) print(dijkstra(N * 2 + 1, X, s)[t]) from bisect import bisect_left as bl from bisect import bisect_right as br from heapq import heapify, heappush as hpush, heappop as hpop def dijkstra(n, E, i0=0): kk = 30 mm = (1 << kk) - 1 h = [i0] D = [-1] * n done = [0] * n D[i0] = 0 while h: x = hpop(h) d, i = x >> kk, x & mm # di = hpop(h) # d, i = unpack("d", pack("q", (di | mm) - mm))[0], di & mm if done[i]: continue done[i] = 1 for j, w in E[i]: nd = d + w if D[j] < 0 or D[j] > nd: if done[j] == 0: hpush(h, (nd << kk) + j) # hpush(h, (unpack("q", pack("d", nd))[0] | mm) - (mm - j)) D[j] = nd return D N, M = map(int, input().split()) A = [int(a) for a in input().split()] B = [int(a) for a in input().split()] AB = sorted([(a, b, i) for i, (a, b) in enumerate(zip(A, B))], key = lambda x: x[1]) A = [ab[0] for ab in AB] B = [ab[1] for ab in AB] for j, (a, b, i) in enumerate(AB): if i == 0: s = j elif i == N - 1: t = j B.append(1 << 60) b0 = B[0] X = [[] for _ in range(2 * N + 1)] for i in range(N): X[i+N].append((i + 1 + N, B[i+1] - B[i])) X[i+N].append((i, 0)) for i, a in enumerate(A): j = bl(B, M - a) X[i].append((N, a + b0)) X[i].append((j + N, a + B[j] - M)) print(dijkstra(N * 2 + 1, X, s)[t])
ConDefects/ConDefects/Code/abc232_g/Python/28189928
condefects-python_data_67
N,M=map(int,input().split()) A=list(map(int,input().split())) B=list(map(int,input().split())) C=list() for i in range(N): C.append([B[i]-M,i]) C.sort() for i in range(N): if C[i][1]==0: st=i if C[i][1]==N-1: fin=i G=[list() for i in range(N)] for i in range(N): if C[i][1]==0: G[i].append([(i+2)%N,(C[(i+2)%N][0]-C[i][0])%M]) else: G[i].append([(i+1)%N,(C[(i+1)%N][0]-C[i][0])%M]) K=list() d=dict() for i in range(N): K.append(-A[C[i][1]]) K.sort() this=0 for i in range(N): if this==N: d[K[i]]=this continue while C[this][0]<K[i]: this+=1 if this==N: break d[K[i]]=this for i in range(N): to=d[-A[C[i][1]]] if to==N: to=0 if C[to][1]!=0: G[i].append([to,(A[C[i][1]]+C[to][0])%M]) else: G[i].append([(to+1)%N,(A[C[i][1]]+C[(to+1)%N][0])%M]) import heapq INF=float('inf') dist=[INF]*N dist[st]=0 hq=[(0,st)] heapq.heapify(hq) while hq: #print(hq) cost1,a=heapq.heappop(hq) for to,cost in G[a]: if dist[to]>dist[a]+cost: dist[to]=dist[a]+cost heapq.heappush(hq,(dist[to],to)) print(dist[fin]) N,M=map(int,input().split()) A=list(map(int,input().split())) B=list(map(int,input().split())) C=list() for i in range(N): C.append([B[i]-M,i]) C.sort() for i in range(N): if C[i][1]==0: st=i if C[i][1]==N-1: fin=i G=[list() for i in range(N)] for i in range(N): if C[i][1]==0: continue if C[(i+1)%N][1]==0: G[i].append([(i+2)%N,(C[(i+2)%N][0]-C[i][0])%M]) else: G[i].append([(i+1)%N,(C[(i+1)%N][0]-C[i][0])%M]) K=list() d=dict() for i in range(N): K.append(-A[C[i][1]]) K.sort() this=0 for i in range(N): if this==N: d[K[i]]=this continue while C[this][0]<K[i]: this+=1 if this==N: break d[K[i]]=this for i in range(N): to=d[-A[C[i][1]]] if to==N: to=0 if C[to][1]!=0: G[i].append([to,(A[C[i][1]]+C[to][0])%M]) else: G[i].append([(to+1)%N,(A[C[i][1]]+C[(to+1)%N][0])%M]) import heapq INF=float('inf') dist=[INF]*N dist[st]=0 hq=[(0,st)] heapq.heapify(hq) while hq: #print(hq) cost1,a=heapq.heappop(hq) for to,cost in G[a]: if dist[to]>dist[a]+cost: dist[to]=dist[a]+cost heapq.heappush(hq,(dist[to],to)) print(dist[fin])
ConDefects/ConDefects/Code/abc232_g/Python/28190550
condefects-python_data_68
n,m = map(int, input().split()) xl = list(map(int, input().split())) cyl = [list(map(int, input().split())) for _ in range(m)] yl = [0] * n for c,y in cyl: yl[c-1] = y dp = [[0] * (n) for i in range(n)] for i in range(n): if i == 0: dp[i][0] = xl[0] + yl[0] else: for j in range(1,i+1): dp[i][j] = dp[i-1][j-1] + xl[i] + yl[j] if i != 1: dp[i][0] = max(dp[i-2]) + xl[i] + yl[j] print(max(dp[n-1])) n,m = map(int, input().split()) xl = list(map(int, input().split())) cyl = [list(map(int, input().split())) for _ in range(m)] yl = [0] * n for c,y in cyl: yl[c-1] = y dp = [[0] * (n) for i in range(n)] for i in range(n): if i == 0: dp[i][0] = xl[0] + yl[0] else: for j in range(1,i+1): dp[i][j] = dp[i-1][j-1] + xl[i] + yl[j] if i != 1: dp[i][0] = max(dp[i-2]) + xl[i] + yl[0] print(max(dp[n-1]))
ConDefects/ConDefects/Code/abc261_d/Python/45285878
condefects-python_data_69
# ref: https://qiita.com/Kota-Y/items/396ab3c57830dad65cfb import sys import re from math import ceil, floor, sqrt, pi, factorial, gcd from copy import deepcopy from collections import Counter, deque, defaultdict from heapq import heapify, heappop, heappush from itertools import accumulate, product, combinations, combinations_with_replacement, permutations from bisect import bisect, bisect_left, bisect_right from functools import reduce from decimal import Decimal, getcontext def input(): return sys.stdin.readline().strip() def i_input(): return int(input()) def i_map(): return map(int, input().split()) def i_list(): return list(i_map()) def i_row(N): return [i_input() for _ in range(N)] def i_row_list(N): return [i_list() for _ in range(N)] def s_input(): return input() def s_map(): return input().split() def s_list(): return list(s_map()) def s_row(N): return [s_input() for _ in range(N)] def s_row_str(N): return [s_list() for _ in range(N)] def s_row_list(N): return [list(s_input()) for _ in range(N)] def lcm(a, b): return a * b // gcd(a, b) def rotate90(S): return list(zip(*S[::-1])) def rotate180(S): return list(zip(*list(zip(*S[::-1]))[::-1])) def rotate270(S): return list(zip(*S))[::-1] sys.setrecursionlimit(10 ** 6) INF = float('inf') MOD = 10 ** 9 + 7 DIRECTION = [ (1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1), ] def main(): N, M = i_map() X = i_list() CY = i_row_list(M) Y = [0 for _ in range(N+1)] for c, y in CY: Y[c] = y # print(Y) dp = [[-MOD for _ in range(N+1)] for _ in range(N+1)] dp[0][0] = 0 for i in range(N): for j in range(N): # 0のとき dp[i+1][0] = max(dp[i][j], dp[i+1][0]) # 1のとき dp[i+1][j+1] = max(dp[i][j]+X[i]+Y[j+1], dp[i+1][j+1]) # for d in dp: # print(d) print(max(dp[-1])) if __name__ == '__main__': main() # ref: https://qiita.com/Kota-Y/items/396ab3c57830dad65cfb import sys import re from math import ceil, floor, sqrt, pi, factorial, gcd from copy import deepcopy from collections import Counter, deque, defaultdict from heapq import heapify, heappop, heappush from itertools import accumulate, product, combinations, combinations_with_replacement, permutations from bisect import bisect, bisect_left, bisect_right from functools import reduce from decimal import Decimal, getcontext def input(): return sys.stdin.readline().strip() def i_input(): return int(input()) def i_map(): return map(int, input().split()) def i_list(): return list(i_map()) def i_row(N): return [i_input() for _ in range(N)] def i_row_list(N): return [i_list() for _ in range(N)] def s_input(): return input() def s_map(): return input().split() def s_list(): return list(s_map()) def s_row(N): return [s_input() for _ in range(N)] def s_row_str(N): return [s_list() for _ in range(N)] def s_row_list(N): return [list(s_input()) for _ in range(N)] def lcm(a, b): return a * b // gcd(a, b) def rotate90(S): return list(zip(*S[::-1])) def rotate180(S): return list(zip(*list(zip(*S[::-1]))[::-1])) def rotate270(S): return list(zip(*S))[::-1] sys.setrecursionlimit(10 ** 6) INF = float('inf') MOD = 10 ** 9 + 7 DIRECTION = [ (1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1), ] def main(): N, M = i_map() X = i_list() CY = i_row_list(M) Y = [0 for _ in range(N+1)] for c, y in CY: Y[c] = y # print(Y) dp = [[-INF for _ in range(N+1)] for _ in range(N+1)] dp[0][0] = 0 for i in range(N): for j in range(N): # 0のとき dp[i+1][0] = max(dp[i][j], dp[i+1][0]) # 1のとき dp[i+1][j+1] = max(dp[i][j]+X[i]+Y[j+1], dp[i+1][j+1]) # for d in dp: # print(d) print(max(dp[-1])) if __name__ == '__main__': main()
ConDefects/ConDefects/Code/abc261_d/Python/45506869
condefects-python_data_70
n,m=map(int,input().split()) x=list(map(int,input().split())) s=[list(map(int,input().split())) for i in range(m)] ans=0 d=[0]*(n+1) for i in range(m): d[s[i][0]]=s[i][1] dp=[[0]*(n+1) for i in range(n+1)] for i in range(n): for j in range(1,n+1): dp[i+1][j]=dp[i][j-1]+x[i]+d[j] dp[i+1][0]=max(dp[i]) print(max(dp[-1])) n,m=map(int,input().split()) x=list(map(int,input().split())) s=[list(map(int,input().split())) for i in range(m)] ans=0 d=[0]*(n+1) for i in range(m): d[s[i][0]]=s[i][1] dp=[[0]*(n+1) for i in range(n+1)] for i in range(n): for j in range(1,i+2): dp[i+1][j]=dp[i][j-1]+x[i]+d[j] dp[i+1][0]=max(dp[i]) print(max(dp[-1]))
ConDefects/ConDefects/Code/abc261_d/Python/46044177
condefects-python_data_71
n = int(input()) adict = dict(zip(list(range(1,n+1)), list(map(int, input().split())))) iset = set(adict.keys()) aset = set(adict.values()) last = (iset - aset).pop() acc = [] while last != -1: acc.append(last) last = adict[last] acc.reverse() print(acc) n = int(input()) adict = dict(zip(list(range(1,n+1)), list(map(int, input().split())))) iset = set(adict.keys()) aset = set(adict.values()) last = (iset - aset).pop() acc = [] while last != -1: acc.append(last) last = adict[last] acc.reverse() print(*acc)
ConDefects/ConDefects/Code/abc337_c/Python/54271526
condefects-python_data_72
N = int(input()) A = list(map(int, input().split())) s = [i for i in range(1,N+1)] d = dict(zip(A, s)) next = d[-1] while next!=N: print(next, end = ' ') next = d[next] print(next) N = int(input()) A = list(map(int, input().split())) s = [i for i in range(1,N+1)] d = dict(zip(A, s)) next = d[-1] for _ in range(N-1): print(next, end = ' ') next = d[next] print(next)
ConDefects/ConDefects/Code/abc337_c/Python/54225038
condefects-python_data_73
def solve(): H, W, N = map(int, input().split()) Grid = [[1] * (W + 1) for _ in range(H + 1)] for i in range(N): a, b = map(int, input().split()) Grid[a][b] = 0 ans = 0 ODP = [0] * (W + 1) DP = [0] * (W + 1) for i in range(1, H + 1): ODP, DP = DP, ODP Grid_i = Grid[i] for j in range(1, W + 1): if Grid_i[j]: DP[j] = min(ODP[j - 1], ODP[j], ODP[j - 1]) + 1 else: DP[j] = 0 ans += DP[j] return ans #================================================== import sys input=sys.stdin.readline write=sys.stdout.write print(solve()) def solve(): H, W, N = map(int, input().split()) Grid = [[1] * (W + 1) for _ in range(H + 1)] for i in range(N): a, b = map(int, input().split()) Grid[a][b] = 0 ans = 0 ODP = [0] * (W + 1) DP = [0] * (W + 1) for i in range(1, H + 1): ODP, DP = DP, ODP Grid_i = Grid[i] for j in range(1, W + 1): if Grid_i[j]: DP[j] = min(DP[j - 1], ODP[j], ODP[j - 1]) + 1 else: DP[j] = 0 ans += DP[j] return ans #================================================== import sys input=sys.stdin.readline write=sys.stdout.write print(solve())
ConDefects/ConDefects/Code/abc311_e/Python/45970897
condefects-python_data_74
import sys, math, itertools, heapq, copy, collections, bisect, random, time from collections import deque, defaultdict, Counter from decimal import Decimal from functools import lru_cache def MI(): return map(int, sys.stdin.buffer.readline().split()) def MI1(): return map(lambda x:int(x)-1, sys.stdin.buffer.readline().split()) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def I(): return int(sys.stdin.buffer.readline()) def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split() def SL(): return list(sys.stdin.buffer.readline().rstrip().decode('utf-8')) def IR(n): return [I() for _ in range(n)] def LIR(n): return [LI() for _ in range(n)] def LSR(n): return [LS() for _ in range(n)] def SLR(n): return [SL() for _ in range(n)] def resolve(): X = 10**18 mod = 998244353 @lru_cache(maxsize=None) def dfs(now): if now <= 4: return now return dfs((now//2))*dfs(((now+1)//2))%mod print(dfs(X)) if __name__ == "__main__": resolve() import sys, math, itertools, heapq, copy, collections, bisect, random, time from collections import deque, defaultdict, Counter from decimal import Decimal from functools import lru_cache def MI(): return map(int, sys.stdin.buffer.readline().split()) def MI1(): return map(lambda x:int(x)-1, sys.stdin.buffer.readline().split()) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def I(): return int(sys.stdin.buffer.readline()) def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split() def SL(): return list(sys.stdin.buffer.readline().rstrip().decode('utf-8')) def IR(n): return [I() for _ in range(n)] def LIR(n): return [LI() for _ in range(n)] def LSR(n): return [LS() for _ in range(n)] def SLR(n): return [SL() for _ in range(n)] def resolve(): X = I() mod = 998244353 @lru_cache(maxsize=None) def dfs(now): if now <= 4: return now return dfs((now//2))*dfs(((now+1)//2))%mod print(dfs(X)) if __name__ == "__main__": resolve()
ConDefects/ConDefects/Code/arc135_a/Python/43694743
condefects-python_data_75
from collections import deque N, M = map(int, input().split()) A = deque(map(int, input().split())) C = deque(map(int, input().split())) answer = [] while A[0] == 0: A.popleft() A = list(A) # 多項式を筆算で割り算する動きをシミュレーションする while len(C) >= len(A): c = C.popleft() answer.append(c // A[0]) for i, a in enumerate(A[1:]): C[i] = C[i] - a * (c // A[0]) # 割り切れること assert sum(C) == 0 print(' '.join(map(str, answer[-M:]))) from collections import deque N, M = map(int, input().split()) A = deque(map(int, input().split())) C = deque(map(int, input().split())) answer = [] while A[0] == 0: A.popleft() A = list(A) # 多項式を筆算で割り算する動きをシミュレーションする while len(C) >= len(A): c = C.popleft() answer.append(c // A[0]) for i, a in enumerate(A[1:]): C[i] = C[i] - a * (c // A[0]) # 割り切れること assert sum(C) == 0 print(' '.join(map(str, answer[-(M + 1):])))
ConDefects/ConDefects/Code/abc245_d/Python/52210355
condefects-python_data_76
# B n = int(input()) a = list(map(int, input().split())) #n = 5 #a = [3, 1, 4, 5, 4] #print(a) call = set() for i in range(n): #print(i) #print(a[i]) if i+1 in call: pass else: call.add(a[i]) print(sorted(call)) no_call =[] for i in range(1, n+1): if i not in call: #print(i) no_call.append(i) print(len(no_call)) print(*no_call) # B n = int(input()) a = list(map(int, input().split())) #n = 5 #a = [3, 1, 4, 5, 4] #print(a) call = set() for i in range(n): #print(i) #print(a[i]) if i+1 in call: pass else: call.add(a[i]) #print(sorted(call)) no_call =[] for i in range(1, n+1): if i not in call: #print(i) no_call.append(i) print(len(no_call)) print(*no_call)
ConDefects/ConDefects/Code/abc293_b/Python/46050349
condefects-python_data_77
import bisect, heapq, sys, math, copy, itertools, decimal from collections import defaultdict, deque sys.setrecursionlimit(10**7) def INT(): return int(input()) def MI(): return map(int, input().split()) def MS(): return map(str, input().split()) def LI(): return list(map(int, input().split())) def LS(): return list(map(str, input().split())) def pr_line(itr): print(*itr, sep='\n') def pr_mtx(matrix): [print(*row) for row in matrix] INF = 1<<62 ''' 操作:(segfunc, 単位元) = { 最小値:(min(x, y), INF), 最大値:(max(x, y), -INF), 区間和:(x + y, 0), 区間積:(x * y, 1), 最大公約数:(math.gcd(x, y), 0), 論理和:(x | y, 0) 論理積:(x & y, 0) 排他的論理和:(x ^ y, 0) } ''' #############################################################s def segfunc(x, y): return [x[0] + y[0], min(x[1], x[0] + y[1])] ide_ele = [0, INF] class SegTree: def __init__(self, init_val, segfunc, ide_ele): n = len(init_val) #要素数 self.segfunc = segfunc #区間にしたい操作 self.ide_ele = ide_ele #単位元 self.num = 1 << (n-1).bit_length() #n以上最小の2のべき乗 self.tree = [ide_ele] * 2 * self.num #セグメント木 #配列の値を歯にセット for i in range(n): self.tree[self.num + i] = init_val[i] #構築していく for i in range(self.num - 1, 0, -1): self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1]) def update(self, k, x): #k番目(0-index)の値をxに更新 k += self.num #葉に移動 self.tree[k] = x while k > 1: #隣同士の子ノードの処理結果を親ノードに更新 self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k | 1]) k >>= 1 def query(self, l ,r): #[l, r)のsegfunc結果を得る(0-index) res_l, res_r = self.ide_ele, self.ide_ele l += self.num r += self.num while l < r: if l & 1: res_l = self.segfunc(res_l, self.tree[l]) l += 1 if r & 1: res_r = self.segfunc(self.tree[r - 1], res_r) l >>= 1 r >>= 1 res = self.segfunc(res_l, res_r) return res def __getitem__(self, x): x += self.num #葉に移動 return self.tree[x] ############################################################### N, Q = MI() S = list(input()) A = [[1, 1] if s=='(' else [-1, -1] for s in S] seg = SegTree(A, segfunc=segfunc, ide_ele=ide_ele) ans = [] for _ in range(Q): q, l, r = MI() l -= 1 r -= 1 if q == 1: a, b = seg[l], seg[r] seg.update(l, b) seg.update(r, a) else: a, b = seg.query(l, r+1) #a:l~rの累積和, b:l~rの最小値 if a == 0 and b == 0: ans.append('Yes') else: ans.append('No') pr_line(ans) import bisect, heapq, sys, math, copy, itertools, decimal from collections import defaultdict, deque sys.setrecursionlimit(10**7) def INT(): return int(input()) def MI(): return map(int, input().split()) def MS(): return map(str, input().split()) def LI(): return list(map(int, input().split())) def LS(): return list(map(str, input().split())) def pr_line(itr): print(*itr, sep='\n') def pr_mtx(matrix): [print(*row) for row in matrix] INF = 1<<62 ''' 操作:(segfunc, 単位元) = { 最小値:(min(x, y), INF), 最大値:(max(x, y), -INF), 区間和:(x + y, 0), 区間積:(x * y, 1), 最大公約数:(math.gcd(x, y), 0), 論理和:(x | y, 0) 論理積:(x & y, 0) 排他的論理和:(x ^ y, 0) } ''' #############################################################s def segfunc(x, y): return [x[0] + y[0], min(x[1], x[0] + y[1])] ide_ele = [0, INF] class SegTree: def __init__(self, init_val, segfunc, ide_ele): n = len(init_val) #要素数 self.segfunc = segfunc #区間にしたい操作 self.ide_ele = ide_ele #単位元 self.num = 1 << (n-1).bit_length() #n以上最小の2のべき乗 self.tree = [ide_ele] * 2 * self.num #セグメント木 #配列の値を歯にセット for i in range(n): self.tree[self.num + i] = init_val[i] #構築していく for i in range(self.num - 1, 0, -1): self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1]) def update(self, k, x): #k番目(0-index)の値をxに更新 k += self.num #葉に移動 self.tree[k] = x while k > 1: #隣同士の子ノードの処理結果を親ノードに更新 k >>= 1 self.tree[k] = self.segfunc(self.tree[k<<1], self.tree[k<<1 | 1]) def query(self, l ,r): #[l, r)のsegfunc結果を得る(0-index) res_l, res_r = self.ide_ele, self.ide_ele l += self.num r += self.num while l < r: if l & 1: res_l = self.segfunc(res_l, self.tree[l]) l += 1 if r & 1: res_r = self.segfunc(self.tree[r - 1], res_r) l >>= 1 r >>= 1 res = self.segfunc(res_l, res_r) return res def __getitem__(self, x): x += self.num #葉に移動 return self.tree[x] ############################################################### N, Q = MI() S = list(input()) A = [[1, 1] if s=='(' else [-1, -1] for s in S] seg = SegTree(A, segfunc=segfunc, ide_ele=ide_ele) ans = [] for _ in range(Q): q, l, r = MI() l -= 1 r -= 1 if q == 1: a, b = seg[l], seg[r] seg.update(l, b) seg.update(r, a) else: a, b = seg.query(l, r+1) #a:l~rの累積和, b:l~rの最小値 if a == 0 and b == 0: ans.append('Yes') else: ans.append('No') pr_line(ans)
ConDefects/ConDefects/Code/abc223_f/Python/46212630
condefects-python_data_78
from atcoder import lazysegtree n,q=map(int,input().split()) s=list(input()) st=lazysegtree.LazySegTree( lambda x,y:min(x,y), n, lambda dd,d:dd+d, lambda ddd,dd:ddd+dd, 0, [0]*n ) for i in range(n): st.apply(i,n,2*(s[i]=="(")-1) for i in range(q): t,l,r=map(int,input().split()) t-=1 l-=1 r-=1 if t: print(["No","Yes"][st.get(l)-1==st.prod(l,r+1)==st.get(r)]) else: st.apply(l,n,-2*(s[l]=="(")+1) st.apply(r,n,-2*(s[r]=="(")+1) s[l],s[r]=s[r],s[l] st.apply(l,n,2*(s[l]=="(")-1) st.apply(r,n,2*(s[r]=="(")-1) from atcoder import lazysegtree n,q=map(int,input().split()) s=list(input()) st=lazysegtree.LazySegTree( lambda x,y:min(x,y), n, lambda dd,d:dd+d, lambda ddd,dd:ddd+dd, 0, [0]*n ) for i in range(n): st.apply(i,n,2*(s[i]=="(")-1) for i in range(q): t,l,r=map(int,input().split()) t-=1 l-=1 r-=1 if t: print(["No","Yes"][s[l]=="(" and s[r]==")" and st.get(l)-1==st.prod(l,r+1)==st.get(r)]) else: st.apply(l,n,-2*(s[l]=="(")+1) st.apply(r,n,-2*(s[r]=="(")+1) s[l],s[r]=s[r],s[l] st.apply(l,n,2*(s[l]=="(")-1) st.apply(r,n,2*(s[r]=="(")-1)
ConDefects/ConDefects/Code/abc223_f/Python/45728797
condefects-python_data_79
import sys sys.setrecursionlimit(10**6) # import resource # resource.setrlimit(resource.RLIMIT_STACK, (1073741824//4, 1073741824//4)) from collections import deque, Counter, defaultdict from itertools import accumulate, permutations, combinations from bisect import bisect_left, bisect_right from heapq import heapify, heappush, heappop from string import ascii_letters, ascii_lowercase, ascii_uppercase, digits, printable from operator import itemgetter from functools import lru_cache, cmp_to_key from math import gcd, sqrt from atcoder.dsu import DSU from atcoder.scc import SCCGraph from atcoder.segtree import SegTree from sortedcontainers import SortedList, SortedDict, SortedSet INFTY = sys.maxsize MOD10 = 10**9+7 MOD99 = 998244353 MOD = MOD99 YES = 'Yes' NO = 'No' DRDC = [[-1, 0], [1, 0], [0, -1], [0, 1]] DRDC2 = [[1, 1], [-1, 1], [1, -1], [-1, -1]] def chr2num(c): return printable.index(c) def num2chr(i): return printable[i] def bisect_lt(a, x): '''Return rightmost index less than x, if not exits return -1''' return bisect_left(a, x) - 1 def bisect_le(a, x): '''Return rightmost index less than or equal to x, if not exists return -1''' return bisect_right(a, x) - 1 def bisect_gt(a, x): '''Return leftmost index greater than x, if not exists return len(a)''' return bisect_right(a, x) def bisect_ge(a, x): '''Return leftmost index greater than or equal to x, if not exists return len(a)''' return bisect_left(a, x) def bisect_lecount(a, x): return bisect_right(a, x) def bisect_ltcount(a, x): return bisect_left(a, x) def bisect_gecount(a, x): return len(a) - bisect_left(a, x) def bisect_gtcount(a, x): return len(a) - bisect_right(a, x) def sc_bisect_lt(sc, x): return sc.bisect_left(x) - 1 def sc_bisect_le(sc, x): return sc.bisect_right(x) - 1 def sc_bisect_gt(sc, x): return sc.bisect_right(x) def sc_bisect_ge(sc, x): return sc.bisect_left(x) def sc_bisect_lecount(sc, x): return sc.bisect_right(x) def sc_bisect_ltcount(sc, x): return sc.bisect_left(x) def sc_bisect_gecount(sc, x): return len(sc) - sc.bisect_left(x) def sc_bisect_gtcount(sc, x): return len(sc) - sc.bisect_right(x) def cmp_for_key(x, y): '''functools.cmp_to_key()に渡すための比較関数。 タプルのソートなどでは x, y にタプルまるごと渡すようにする(遅くなるので)。 key=cmp_to_key(cmp_for_key)''' s = x - y # ここを必要に応じて書き換える。 if s < 0: return -1 # x < y (x が y より前) elif s == 0: return 0 # x == y else: return 1 # x > y (x が y より後) # input = sys.stdin.readline def iinput(): return int(input()) def minput(): return map(int, input().split()) def linput(): return list(map(int, input().split())) # DEBUG = False # def printd(*args): # if DEBUG: # print(*args) def readinput(): n,k=minput() return n,k def subsolve1(n): a = [0]*n for i in range(n): print(f'? {i+1}') a[i] = iinput() print('! ', end='') print(*a) print() sys.exit() def subsolve2(n, k): a = [0]*n b = [0]*(k+1) for i in range(k+1): print('?', end='') for j in range(k+1): if j == i: continue print(f' {j+1}', end='') print() b[i] = iinput() c = sum(b) % 2 for i in range(k+1): a[i] = (c - b[i]) % 2 c = sum(a[:k-1]) % 2 for i in range(k+1, n): print('?', end='') for j in range(k-1): print(f' {j+1}', end='') print(f' {i+1}') t = iinput() a[i] = (t - c) % 2 print('!') for i in range(n): print(f' {a[i]}', end='') print() sys.exit() def solve(args): n,k=args if k == 1: subsolve1(n) else: subsolve2(n, k) def printans(ans): if isinstance(ans, list) or isinstance(ans, tuple): print(*ans, sep='\n') else: print(ans) if __name__=='__main__': args=readinput() ans=solve(args) printans(ans) import sys sys.setrecursionlimit(10**6) # import resource # resource.setrlimit(resource.RLIMIT_STACK, (1073741824//4, 1073741824//4)) from collections import deque, Counter, defaultdict from itertools import accumulate, permutations, combinations from bisect import bisect_left, bisect_right from heapq import heapify, heappush, heappop from string import ascii_letters, ascii_lowercase, ascii_uppercase, digits, printable from operator import itemgetter from functools import lru_cache, cmp_to_key from math import gcd, sqrt from atcoder.dsu import DSU from atcoder.scc import SCCGraph from atcoder.segtree import SegTree from sortedcontainers import SortedList, SortedDict, SortedSet INFTY = sys.maxsize MOD10 = 10**9+7 MOD99 = 998244353 MOD = MOD99 YES = 'Yes' NO = 'No' DRDC = [[-1, 0], [1, 0], [0, -1], [0, 1]] DRDC2 = [[1, 1], [-1, 1], [1, -1], [-1, -1]] def chr2num(c): return printable.index(c) def num2chr(i): return printable[i] def bisect_lt(a, x): '''Return rightmost index less than x, if not exits return -1''' return bisect_left(a, x) - 1 def bisect_le(a, x): '''Return rightmost index less than or equal to x, if not exists return -1''' return bisect_right(a, x) - 1 def bisect_gt(a, x): '''Return leftmost index greater than x, if not exists return len(a)''' return bisect_right(a, x) def bisect_ge(a, x): '''Return leftmost index greater than or equal to x, if not exists return len(a)''' return bisect_left(a, x) def bisect_lecount(a, x): return bisect_right(a, x) def bisect_ltcount(a, x): return bisect_left(a, x) def bisect_gecount(a, x): return len(a) - bisect_left(a, x) def bisect_gtcount(a, x): return len(a) - bisect_right(a, x) def sc_bisect_lt(sc, x): return sc.bisect_left(x) - 1 def sc_bisect_le(sc, x): return sc.bisect_right(x) - 1 def sc_bisect_gt(sc, x): return sc.bisect_right(x) def sc_bisect_ge(sc, x): return sc.bisect_left(x) def sc_bisect_lecount(sc, x): return sc.bisect_right(x) def sc_bisect_ltcount(sc, x): return sc.bisect_left(x) def sc_bisect_gecount(sc, x): return len(sc) - sc.bisect_left(x) def sc_bisect_gtcount(sc, x): return len(sc) - sc.bisect_right(x) def cmp_for_key(x, y): '''functools.cmp_to_key()に渡すための比較関数。 タプルのソートなどでは x, y にタプルまるごと渡すようにする(遅くなるので)。 key=cmp_to_key(cmp_for_key)''' s = x - y # ここを必要に応じて書き換える。 if s < 0: return -1 # x < y (x が y より前) elif s == 0: return 0 # x == y else: return 1 # x > y (x が y より後) # input = sys.stdin.readline def iinput(): return int(input()) def minput(): return map(int, input().split()) def linput(): return list(map(int, input().split())) # DEBUG = False # def printd(*args): # if DEBUG: # print(*args) def readinput(): n,k=minput() return n,k def subsolve1(n): a = [0]*n for i in range(n): print(f'? {i+1}') a[i] = iinput() print('! ', end='') print(*a) print() sys.exit() def subsolve2(n, k): a = [0]*n b = [0]*(k+1) for i in range(k+1): print('?', end='') for j in range(k+1): if j == i: continue print(f' {j+1}', end='') print() b[i] = iinput() c = sum(b) % 2 for i in range(k+1): a[i] = (c - b[i]) % 2 c = sum(a[:k-1]) % 2 for i in range(k+1, n): print('?', end='') for j in range(k-1): print(f' {j+1}', end='') print(f' {i+1}') t = iinput() a[i] = (t - c) % 2 print('!', end='') for i in range(n): print(f' {a[i]}', end='') print() sys.exit() def solve(args): n,k=args if k == 1: subsolve1(n) else: subsolve2(n, k) def printans(ans): if isinstance(ans, list) or isinstance(ans, tuple): print(*ans, sep='\n') else: print(ans) if __name__=='__main__': args=readinput() ans=solve(args) printans(ans)
ConDefects/ConDefects/Code/abc313_d/Python/45777524
condefects-python_data_80
N,K = map(int,input().split()) A = [0] * N S = [] def request(i): if i <= K: X = [(k+i)%(K+1)+1 for k in range(K)] else: X = [k+1 for k in range(K-1)] + [i+1] print("?",*X) s = int(input()) S.append(s) for i in range(N): request(i) T = sum(S[:K+1]) % 2 for i in range(K+1): A[(i-1)%(K+1)] = (T-S[i]) % 2 for i in range(K+1,N): A[i] = (S[0]-A[2]-S[i]) % 2 print("!",*A) N,K = map(int,input().split()) A = [0] * N S = [] def request(i): if i <= K: X = [(k+i)%(K+1)+1 for k in range(K)] else: X = [k+1 for k in range(K-1)] + [i+1] print("?",*X) s = int(input()) S.append(s) for i in range(N): request(i) T = sum(S[:K+1]) % 2 for i in range(K+1): A[(i-1)%(K+1)] = (T-S[i]) % 2 for i in range(K+1,N): A[i] = (S[0]-A[K-1]-S[i]) % 2 print("!",*A)
ConDefects/ConDefects/Code/abc313_d/Python/45654990
condefects-python_data_81
n,q = map(int,input().split()) s = str(input()) cnt = 0 for i in range(q): t,x = map(int,input().split()) if t == 1: cnt += 1 cnt = cnt % n if t == 2: print(s[(n - cnt + (x-1))%n]) n,q = map(int,input().split()) s = str(input()) cnt = 0 for i in range(q): t,x = map(int,input().split()) if t == 1: cnt += x cnt = cnt % n if t == 2: print(s[(n - cnt + (x-1))%n])
ConDefects/ConDefects/Code/abc258_c/Python/44704874
condefects-python_data_82
N = int(input()) H = list(map(int, input().split())) ans = 0 for i in range(1, N): if H[i] > H[i-1]: ans = H[i] else: break print(ans) N = int(input()) H = list(map(int, input().split())) ans = H[0] for i in range(1, N): if H[i] > H[i-1]: ans = H[i] else: break print(ans)
ConDefects/ConDefects/Code/abc235_b/Python/44832943
condefects-python_data_83
N = int(input()) H = list(map(int, input().split())) ans = 0 for i in range(N -1): if H[i] < H[i + 1] : ans = H[i + 1] if H[0] >= H[1]: ans = H[0] else: break print(ans) N = int(input()) H = list(map(int, input().split())) ans = 0 for i in range(N -1): if H[i] < H[i + 1] : ans = H[i + 1] elif H[0] >= H[1]: ans = H[0] else: break print(ans)
ConDefects/ConDefects/Code/abc235_b/Python/45931351
condefects-python_data_84
n = int(input()) H = list(map(int, input().split())) count = 0 for i in range(n-1): if H[i] < H[i+1]: count += 1 print(H[count]) n = int(input()) H = list(map(int, input().split())) count = 0 for i in range(n-1): if H[i] < H[i+1]: count += 1 else: break print(H[count])
ConDefects/ConDefects/Code/abc235_b/Python/45498250
condefects-python_data_85
N = int(input()) H = list(map(int,input().split())) ans=0 for n in range(N-1): if H[n+1]>H[n]: ans=H[n+1] else: break print(ans) N = int(input()) H = list(map(int,input().split())) ans=H[0] for n in range(N-1): if H[n+1]>H[n]: ans=H[n+1] else: break print(ans)
ConDefects/ConDefects/Code/abc235_b/Python/44433073
condefects-python_data_86
import sys input = sys.stdin.readline mod=998244353 N,S=map(int,input().split()) A=sorted(map(int,input().split())) if 1 in A: DP=[1,0] else: DP=[1,1] DP=[DP] # 行列の計算(numpyを使えないとき,modを使用) def prod(A,B,k,l,m):# A:k*l,B:l*m C=[[None for i in range(m)] for j in range(k)] for i in range(k): for j in range(m): ANS=0 for pl in range(l): ANS=(ANS+A[i][pl]*B[pl][j])%mod C[i][j]=ANS return C def plus(A,B,k,l):# a,B:k*l C=[[None for i in range(l)] for j in range(k)] for i in range(k): for j in range(l): C[i][j]=(A[i][j]+B[i][j])%mod return C # 漸化式を行列累乗で求める(ダブリング) POWA=[[[1,1],[1,2]]] for i in range(60): POWA.append(prod(POWA[-1],POWA[-1],2,2,2)) # ベキを求めて now=0 ind=0 if A[ind]==1: ind+=1 while ind<len(A): #print(now,DP) a=A[ind] if a%2==0 and ind+1<len(A) and A[ind+1]==a+1: X=DP n=(a-now)//2-1 while n: X=prod(X,POWA[n.bit_length()-1],1,2,2) # n乗の場合 n-=1<<(n.bit_length()-1) DP=X ind+=2 now=a elif a%2==0: X=DP n=(a-now)//2-1 while n: X=prod(X,POWA[n.bit_length()-1],1,2,2) # n乗の場合 n-=1<<(n.bit_length()-1) DP=X DP[0][1]=DP[0][0]+DP[0][1] now=a ind+=1 elif a%2==1: X=DP n=(a-now)//2-1 while n: X=prod(X,POWA[n.bit_length()-1],1,2,2) # n乗の場合 n-=1<<(n.bit_length()-1) DP=X DP[0][0]=DP[0][0]+DP[0][1] now=a-1 ind+=1 #print(now,DP) if S==now+1: print(DP[0][0]) exit() n=(S-now)//2-1 X=DP #print(n) while n: X=prod(X,POWA[n.bit_length()-1],1,2,2) # n乗の場合 n-=1<<(n.bit_length()-1) DP=X #print(now,DP) MINUS=DP[0][0] X=DP n=1 while n: X=prod(X,POWA[n.bit_length()-1],1,2,2) # n乗の場合 n-=1<<(n.bit_length()-1) DP=X if S%2==1: print(DP[0][0]) else: print(DP[0][0]-MINUS) import sys input = sys.stdin.readline mod=998244353 N,S=map(int,input().split()) A=sorted(map(int,input().split())) if 1 in A: DP=[1,0] else: DP=[1,1] DP=[DP] # 行列の計算(numpyを使えないとき,modを使用) def prod(A,B,k,l,m):# A:k*l,B:l*m C=[[None for i in range(m)] for j in range(k)] for i in range(k): for j in range(m): ANS=0 for pl in range(l): ANS=(ANS+A[i][pl]*B[pl][j])%mod C[i][j]=ANS return C def plus(A,B,k,l):# a,B:k*l C=[[None for i in range(l)] for j in range(k)] for i in range(k): for j in range(l): C[i][j]=(A[i][j]+B[i][j])%mod return C # 漸化式を行列累乗で求める(ダブリング) POWA=[[[1,1],[1,2]]] for i in range(60): POWA.append(prod(POWA[-1],POWA[-1],2,2,2)) # ベキを求めて now=0 ind=0 if A[ind]==1: ind+=1 while ind<len(A): #print(now,DP) a=A[ind] if a%2==0 and ind+1<len(A) and A[ind+1]==a+1: X=DP n=(a-now)//2-1 while n: X=prod(X,POWA[n.bit_length()-1],1,2,2) # n乗の場合 n-=1<<(n.bit_length()-1) DP=X ind+=2 now=a elif a%2==0: X=DP n=(a-now)//2-1 while n: X=prod(X,POWA[n.bit_length()-1],1,2,2) # n乗の場合 n-=1<<(n.bit_length()-1) DP=X DP[0][1]=DP[0][0]+DP[0][1] now=a ind+=1 elif a%2==1: X=DP n=(a-now)//2-1 while n: X=prod(X,POWA[n.bit_length()-1],1,2,2) # n乗の場合 n-=1<<(n.bit_length()-1) DP=X DP[0][0]=DP[0][0]+DP[0][1] now=a-1 ind+=1 #print(now,DP) if S==now+1: print(DP[0][0]) exit() n=(S-now)//2-1 X=DP #print(n) while n: X=prod(X,POWA[n.bit_length()-1],1,2,2) # n乗の場合 n-=1<<(n.bit_length()-1) DP=X #print(now,DP) MINUS=DP[0][0] X=DP n=1 while n: X=prod(X,POWA[n.bit_length()-1],1,2,2) # n乗の場合 n-=1<<(n.bit_length()-1) DP=X if S%2==1: print(DP[0][0]) else: print((DP[0][0]-MINUS)%mod)
ConDefects/ConDefects/Code/abc258_h/Python/33045301
condefects-python_data_87
import sys def mul(A, v, mod): m = len(A) n = len(v) w = [0]*m for i in range(m): s = 0 for k in range(n): s += A[i][k] * v[k] w[i] = s % mod return w def p2(A, mod): n = len(A) C = [[0]*n for _ in range(n)] for i in range(n): for j in range(n): s = 0 for k in range(n): s += A[i][k] * A[k][j] s %= mod C[i][j] = s return C def pow(A, v, e, mod): MUL = A while e > 0: if e&1: v = mul(MUL, v, mod) MUL = p2(MUL, mod) e >>= 1 return v def solve(): sys.setrecursionlimit(500005) stdin = sys.stdin ni = lambda: int(ns()) na = lambda: list(map(int, stdin.readline().split())) ns = lambda: stdin.readline().strip() # dp = [0] * 70 # dp[0] = 1 # for i in range(1, len(dp)): # for j in range(1, i+1, 2): # dp[i] += dp[i-j] n, S = na() a = [0] + na() + [S] ep = [0] * len(a) ep[0] = -1 M = [[1, 1], [1, 0]] v = [-1, 0] mod = 998244353 for i in range(1, len(a)): v = pow(M, v, a[i] - a[i-1], mod) ep[i] -= v[1] ep[i] %= 998244353 v[0] += ep[i] # v[1] -= ep[i] # 3a+5b # 5a+8b # print(ep[-1]) # ep = [0] * len(a) # ep[0] = -1 # M = [[1, 1], [1, 0]] # mod = 998244353 # for i in range(1, len(a)): # for j in range(i): # u = pow(M, [1, 0], a[i] - a[j], mod) # # print(a[i] - a[j], u) # ep[i] -= ep[j] * u[1] # ep[i] %= 998244353 # print(ep) # print(dp) if __name__ == "__main__": solve() import sys def mul(A, v, mod): m = len(A) n = len(v) w = [0]*m for i in range(m): s = 0 for k in range(n): s += A[i][k] * v[k] w[i] = s % mod return w def p2(A, mod): n = len(A) C = [[0]*n for _ in range(n)] for i in range(n): for j in range(n): s = 0 for k in range(n): s += A[i][k] * A[k][j] s %= mod C[i][j] = s return C def pow(A, v, e, mod): MUL = A while e > 0: if e&1: v = mul(MUL, v, mod) MUL = p2(MUL, mod) e >>= 1 return v def solve(): sys.setrecursionlimit(500005) stdin = sys.stdin ni = lambda: int(ns()) na = lambda: list(map(int, stdin.readline().split())) ns = lambda: stdin.readline().strip() # dp = [0] * 70 # dp[0] = 1 # for i in range(1, len(dp)): # for j in range(1, i+1, 2): # dp[i] += dp[i-j] n, S = na() a = [0] + na() + [S] ep = [0] * len(a) ep[0] = -1 M = [[1, 1], [1, 0]] v = [-1, 0] mod = 998244353 for i in range(1, len(a)): v = pow(M, v, a[i] - a[i-1], mod) ep[i] -= v[1] ep[i] %= 998244353 v[0] += ep[i] # v[1] -= ep[i] # 3a+5b # 5a+8b print(ep[-1]) # ep = [0] * len(a) # ep[0] = -1 # M = [[1, 1], [1, 0]] # mod = 998244353 # for i in range(1, len(a)): # for j in range(i): # u = pow(M, [1, 0], a[i] - a[j], mod) # # print(a[i] - a[j], u) # ep[i] -= ep[j] * u[1] # ep[i] %= 998244353 # print(ep) # print(dp) if __name__ == "__main__": solve()
ConDefects/ConDefects/Code/abc258_h/Python/32929696
condefects-python_data_88
M=998244353 n,s=map(int,input().split()) f=lambda a,b,c,d:[(a*c+b*d*5)%M,(a*d+b*c)%M] CD=[cd:=[M//2+1]*2]+[cd:=f(*cd,*cd)for i in range(60)] g=1 k=0 a=[0,*map(int,input().split()),s] for i in range(1,n+2): x,y=0,598946612 r=a[i]-a[i-1]-1 for j in range(60): if r&1:x,y=f(x,y,*CD[j]) r>>=1 c,d=x*2%M,f(x,y,*CD[0])[0]*2%M b=(d-c)%M g,k=(c*g+b*k)%M,(d*g+c*k)%M print(g if s&1 else k) M=998244353 n,s=map(int,input().split()) f=lambda a,b,c,d:[(a*c+b*d*5)%M,(a*d+b*c)%M] CD=[cd:=[M//2+1]*2]+[cd:=f(*cd,*cd)for i in range(60)] g=1 k=0 a=[0,*map(int,input().split()),s] for i in range(1,n+2): x,y=0,598946612 r=a[i]-a[i-1]-1 for j in range(60): if r&1:x,y=f(x,y,*CD[j]) r>>=1 c,d=x*2%M,f(x,y,*CD[0])[0]*2%M b=(d-c)%M g,k=(c*g+b*k)%M,(d*g+c*k)%M print(k)
ConDefects/ConDefects/Code/abc258_h/Python/50522493
condefects-python_data_89
def ip():return int(input()) def mp():return map(int, input().split()) def lmp():return list(map(int, input().split())) # 20230610 ABC305 E 1158 - Art Gallery on Graph PyPyで提出 N, M, K = mp() edge = [[] for _ in range(N)] for _ in range(M): a, b = mp() a -= 1 b -= 1 edge[a].append(b) edge[b].append(a) from heapq import heappop, heappush, heapify H = [-1] * N for _ in range(K): p, h = mp() p -= 1 H[p] = h que = [(-H[i], i) for i in range(N)] heapify(que) while que: h, v = heappop(que) h *= -1 if h < H[v] or h == 0: continue for to in edge[v]: if H[to] != -1 or H[to] >= h-1: continue H[to] = h - 1 heappush(que, (-H[to], to)) ans = [] for i in range(N): if H[i] != -1: ans.append(i+1) print(len(ans)) print(*ans) def ip():return int(input()) def mp():return map(int, input().split()) def lmp():return list(map(int, input().split())) # 20230610 ABC305 E 1158 - Art Gallery on Graph PyPyで提出 N, M, K = mp() edge = [[] for _ in range(N)] for _ in range(M): a, b = mp() a -= 1 b -= 1 edge[a].append(b) edge[b].append(a) from heapq import heappop, heappush, heapify H = [-1] * N for _ in range(K): p, h = mp() p -= 1 H[p] = h que = [(-H[i], i) for i in range(N)] heapify(que) while que: h, v = heappop(que) h *= -1 if h < H[v] or h == 0: continue for to in edge[v]: if H[to] >= h-1: continue H[to] = h - 1 heappush(que, (-H[to], to)) ans = [] for i in range(N): if H[i] != -1: ans.append(i+1) print(len(ans)) print(*ans)
ConDefects/ConDefects/Code/abc305_e/Python/46045921
condefects-python_data_90
N=int(input()) S=[input() for _ in range(N)] #i行目のl列目からr列目までに"#"は含まれるか pre_h = [0]*N**3 for i in range(N): for l in range(N): for r in range(N): for k in range(l,r+1): if S[i][k]=="#": pre_h[i*N**2+l*N+r]=1 #i列目のl行目からr行目までに"#"は含まれるか pre_w = [0]*N**3 for i in range(N): for l in range(N): for r in range(N): for k in range(l,r+1): if S[k][i]=="#": pre_w[i*N**2+l*N+r]=1 INF=N+10 di=[INF]*N**4 def dp(lx,ly,rx,ry): if lx>rx or ly>ry: return 0 C=lx*N**3+ly*N**2+rx*N+ry if di[C]!=INF: return di[C] ans=max(rx-lx+1,ry-ly+1) for i in range(lx,rx+1): if not pre_h[i*N**2+ly*N+ry]: ans=min(ans,dp(lx,ly,i-1,ry)+dp(i+1,ly,rx,ry)) for j in range(ly,ry): if not pre_w[j*N**2+lx*N+rx]: ans=min(ans,dp(lx,ly,rx,j-1)+dp(lx,j+1,rx,ry)) di[C]=ans ; return ans print(dp(0,0,N-1,N-1)) N=int(input()) S=[input() for _ in range(N)] #i行目のl列目からr列目までに"#"は含まれるか pre_h = [0]*N**3 for i in range(N): for l in range(N): for r in range(N): for k in range(l,r+1): if S[i][k]=="#": pre_h[i*N**2+l*N+r]=1 #i列目のl行目からr行目までに"#"は含まれるか pre_w = [0]*N**3 for i in range(N): for l in range(N): for r in range(N): for k in range(l,r+1): if S[k][i]=="#": pre_w[i*N**2+l*N+r]=1 INF=N+10 di=[INF]*N**4 def dp(lx,ly,rx,ry): if lx>rx or ly>ry: return 0 C=lx*N**3+ly*N**2+rx*N+ry if di[C]!=INF: return di[C] ans=max(rx-lx+1,ry-ly+1) for i in range(lx,rx+1): if not pre_h[i*N**2+ly*N+ry]: ans=min(ans,dp(lx,ly,i-1,ry)+dp(i+1,ly,rx,ry)) for j in range(ly,ry+1): if not pre_w[j*N**2+lx*N+rx]: ans=min(ans,dp(lx,ly,rx,j-1)+dp(lx,j+1,rx,ry)) di[C]=ans ; return ans print(dp(0,0,N-1,N-1))
ConDefects/ConDefects/Code/abc233_g/Python/38349532
condefects-python_data_91
def f(n): return n**2+2*n+3 n=int(input()) print(f(f(f(n)+n))+f(f(n))) def f(n): return n**2+2*n+3 n=int(input()) print(f(f(f(n)+n)+f(f(n))))
ConDefects/ConDefects/Code/abc234_a/Python/45030219
condefects-python_data_92
def f(x): return x**2+2*x+3 t = int(input()) print((f(f(t)+t)+f(f(t)))) def f(x): return x**2+2*x+3 t = int(input()) print(f(f(f(t)+t)+f(f(t))))
ConDefects/ConDefects/Code/abc234_a/Python/44890153
condefects-python_data_93
S = input() def inverse(n, d): return n * pow(d, -1, MOD) % MOD MOD = 998244353 comb = [[0]*len(S) for _ in range(len(S))] for i in range(len(S)): for j in range((i+1)//2+1): if j == 0: comb[i][j] = i+1 else: comb[i][j] = comb[i][j-1] comb[i][j] *= inverse(i+1-j, j+1) comb[i][j] %= MOD cnt = [0]*26 for i in range(len(S)): cnt[ord(S[i])-ord("a")] += 1 dp = [[0]*(len(S)+1) for _ in range(27)] dp[0][0] = 1 SUM = cnt[0] for i in range(1, 27): for j in range(SUM+1): dp[i][j] += dp[i-1][j] dp[i][j] %= MOD for k in range(1, min(j+1, cnt[i-1]+1)): J, K = j, k if J == K: dp[i][j] += dp[i-1][j-k] dp[i][j] %= MOD else: if j % 2 == 0 and K > j//2: K -= (K-j//2)*2 elif j & 2 == 1 and K > j//2: K -= (K-j//2-1)*2+1 dp[i][j] += comb[J-1][K-1]*dp[i-1][j-k] dp[i][j] %= MOD if i < 26: SUM += cnt[i] ans = 0 for i in range(1, len(S)+1): ans += dp[-1][i] ans %= MOD print(ans) S = input() def inverse(n, d): return n * pow(d, -1, MOD) % MOD MOD = 998244353 comb = [[0]*len(S) for _ in range(len(S))] for i in range(len(S)): for j in range((i+1)//2+1): if j == 0: comb[i][j] = i+1 else: comb[i][j] = comb[i][j-1] comb[i][j] *= inverse(i+1-j, j+1) comb[i][j] %= MOD cnt = [0]*26 for i in range(len(S)): cnt[ord(S[i])-ord("a")] += 1 dp = [[0]*(len(S)+1) for _ in range(27)] dp[0][0] = 1 SUM = cnt[0] for i in range(1, 27): for j in range(SUM+1): dp[i][j] += dp[i-1][j] dp[i][j] %= MOD for k in range(1, min(j+1, cnt[i-1]+1)): J, K = j, k if J == K: dp[i][j] += dp[i-1][j-k] dp[i][j] %= MOD else: if j % 2 == 0 and K > j//2: K -= (K-j//2)*2 elif j % 2 == 1 and K > j//2: K -= (K-j//2-1)*2+1 dp[i][j] += comb[J-1][K-1]*dp[i-1][j-k] dp[i][j] %= MOD if i < 26: SUM += cnt[i] ans = 0 for i in range(1, len(S)+1): ans += dp[-1][i] ans %= MOD print(ans)
ConDefects/ConDefects/Code/abc234_f/Python/53824155
README.md exists but content is empty. Use the Edit dataset card button to edit it.
Downloads last month
30
Edit dataset card