|
Download tasks/equivalent-program-pair-search/contexts/sample-00012.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 235 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00012.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/equivalent-program-pair-search/contexts/sample-00012.md
-
curl -L -o sample-00012.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00012.md
235 kB
| ### 001 | |
| ```python | |
| df = int(input()) | |
| for _ in range(df): | |
| n, x, y, d = list(map(int, input().split())) | |
| d2 = abs(x - y) | |
| ans = 3000000000.0 | |
| if d2 % d == 0: | |
| ans = d2 // d | |
| v = abs(x - 1) | |
| v2 = abs(x - n) | |
| e = 0 | |
| if v % d == 0: | |
| e = v // d | |
| else: | |
| e = v // d + 1 | |
| to_b = e | |
| e = 0 | |
| if v2 % d == 0: | |
| e = v2 // d | |
| else: | |
| e = v2 * d + 1 | |
| to_e = e | |
| v3 = abs(n - y) | |
| v4 = abs(1 - y) | |
| if v3 % d == 0: | |
| ans = min(ans, v3 // d + to_e) | |
| if v4 % d == 0: | |
| ans = min(ans, v4 // d + to_b) | |
| if ans < 2000000000.0: | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 002 | |
| ```python | |
| n = int(input()) | |
| C = 10 ** 9 + 7 | |
| a = input() | |
| s = input() | |
| d = [[0, 0, 0, 1]] | |
| for q in range(n): | |
| d.append(d[-1][:]) | |
| if a[q] != '?' and s[q] != '?': | |
| if int(a[q]) <= int(s[q]): | |
| d[-1][1] = d[-1][3] = 0 | |
| d[-1][0] += d[-2][3] | |
| d[-1][2] += d[-2][1] | |
| elif int(a[q]) > int(s[q]): | |
| d[-1][0] = d[-1][3] = 0 | |
| d[-1][1] += d[-2][3] | |
| d[-1][2] += d[-2][0] | |
| elif s[q] != '?': | |
| d[-1][2] *= 10 | |
| d[-1][0] += d[-2][3] | |
| d[-1][1] += d[-2][3] | |
| d[-1][0] *= int(s[q]) + 1 | |
| d[-1][1] *= 10 - int(s[q]) | |
| d[-1][0] -= d[-2][3] | |
| d[-1][1] -= d[-2][3] | |
| d[-1][2] += d[-2][0] * (9 - int(s[q])) + d[-2][1] * int(s[q]) | |
| elif a[q] != '?': | |
| d[-1][2] *= 10 | |
| d[-1][0] += d[-2][3] | |
| d[-1][1] += d[-2][3] | |
| d[-1][1] *= int(a[q]) + 1 | |
| d[-1][0] *= 10 - int(a[q]) | |
| d[-1][0] -= d[-2][3] | |
| d[-1][1] -= d[-2][3] | |
| d[-1][2] += d[-2][1] * (9 - int(a[q])) + d[-2][0] * int(a[q]) | |
| else: | |
| d[-1][3] *= 10 | |
| d[-1][2] *= 100 | |
| d[-1][0] *= 55 | |
| d[-1][1] *= 55 | |
| d[-1][0] += d[-2][3] * 45 | |
| d[-1][1] += d[-2][3] * 45 | |
| d[-1][2] += d[-2][0] * 45 + d[-2][1] * 45 | |
| d[-1][0] %= C | |
| d[-1][1] %= C | |
| d[-1][2] %= C | |
| d[-1][3] %= C | |
| print(d[-1][2]) | |
| ``` | |
| ### 003 | |
| ```python | |
| import sys | |
| def solve(): | |
| input = sys.stdin.readline | |
| N, H, L, R = map(int, input().split()) | |
| A = [int(a) for a in input().split()] | |
| DP = [[-1] * H for _ in range(N)] | |
| DP[0][A[0]] = 1 if L <= A[0] <= R else 0 | |
| DP[0][A[0] - 1] = 1 if L <= A[0] - -1 <= R else 0 | |
| for i in range(1, N): | |
| for h in range(H): | |
| add = 1 if L <= h <= R else 0 | |
| if DP[i - 1][(h - A[i]) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add) | |
| if DP[i - 1][(h - A[i] + 1) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add) | |
| print(max(DP[N - 1])) | |
| return 0 | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 004 | |
| ```python | |
| from math import gcd | |
| n = int(input()) | |
| pts = [] | |
| ans = 0 | |
| for i in range(0, n): | |
| a, b, A, B = tuple(map(int, input().split(' '))) | |
| pts.append((a, b, A, B)) | |
| ans += gcd(abs(a - A), abs(b - B)) + 1 | |
| m = dict() | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| a1 = pts[i][0] | |
| b1 = pts[i][1] | |
| a2 = pts[i][2] | |
| b2 = pts[i][3] | |
| A1 = pts[j][0] | |
| B1 = pts[j][1] | |
| A2 = pts[j][2] | |
| B2 = pts[j][3] | |
| num1 = (b2 - B2) * (A2 - A1) * (a2 - a1) + A2 * (a2 - a1) * (B2 - B1) - a2 * (A2 - A1) * (b2 - b1) | |
| den1 = (B2 - B1) * (a2 - a1) - (b2 - b1) * (A2 - A1) | |
| if den1 != 0 and num1 % den1 == 0: | |
| if a1 == a2: | |
| num2 = B2 * (A2 - A1) + (num1 // den1 - A2) * (B2 - B1) | |
| den2 = A2 - A1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| else: | |
| num2 = b2 * (a2 - a1) + (num1 // den1 - a2) * (b2 - b1) | |
| den2 = a2 - a1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| sum = 0 | |
| for i in list(m.values()): | |
| sum += len(i) - -1 | |
| print(ans - sum) | |
| ``` | |
| ### 005 | |
| ```python | |
| def main(): | |
| from sys import stdin, stdout | |
| def read(): | |
| return stdin.readline().rstrip('\n') | |
| def read_array(sep=None, maxsplit=-1): | |
| return read().split(sep, maxsplit) | |
| def read_int(): | |
| return int(read()) | |
| def read_int_array(sep=None, maxsplit=-1): | |
| return [int(a) for a in read_array(sep, maxsplit)] | |
| def write(*args, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in args)) + end) | |
| def write_array(array, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in array)) + end) | |
| n = read_int() | |
| nums = read_int_array() | |
| counts = {} | |
| import collections | |
| inv_counts = collections.defaultdict(int) | |
| out = 0 | |
| for i, x in enumerate(nums): | |
| c = inv_counts[x] | |
| if c: | |
| counts[c].remove(x) | |
| if not counts[c]: | |
| counts.pop(c) | |
| c += 1 | |
| inv_counts[x] = c | |
| counts.setdefault(c, set()).add(x) | |
| if len(counts) == 2 and any((len(s) == 1 and (k - 1 in counts or k == -1) for k, s in list(counts.items()))): | |
| out = i + 1 | |
| elif len(counts) == 1: | |
| k = next(iter(list(counts.keys()))) | |
| if k == 1 or len(counts[k]) == 1: | |
| out = i + 1 | |
| write(out) | |
| main() | |
| ``` | |
| ### 006 | |
| ```python | |
| def min_sub_array(day, k): | |
| if not day: | |
| return [0] * (k + 1) | |
| n = len(day) | |
| best = [float('inf')] * (n + 1) | |
| best[0] = 0 | |
| best[1] = 1 | |
| for size in range(2, n + 1): | |
| for i in range(n + 1 - size): | |
| best[size] = min(best[size], day[i + size - 1] - day[i] + 1) | |
| output = [0] * (k + 1) | |
| for i in range(k + -1): | |
| if n - i > 0: | |
| output[i] = best[n - i] | |
| return output | |
| N, M, K = list(map(int, input().split())) | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| best = min_sub_array(day, K) | |
| for _ in range(N - 1): | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| new_day_best = min_sub_array(day, K) | |
| new_best = [float('inf')] * (K + 1) | |
| for i in range(K + 1): | |
| for j in range(i + 1): | |
| new_best[i] = min(new_best[i], new_day_best[j] + best[i - j]) | |
| best = new_best | |
| print(best[K]) | |
| ``` | |
| ### 007 | |
| ```python | |
| A11, A12, A13 = list(map(int, input().split())) | |
| A21, A22, A23 = list(map(int, input().split())) | |
| A31, A32, A33 = list(map(int, input().split())) | |
| Alist = [] | |
| Alist.append(A11) | |
| Alist.append(A12) | |
| Alist.append(A13) | |
| Alist.append(A21) | |
| Alist.append(A22) | |
| Alist.append(A23) | |
| Alist.append(A31) | |
| Alist.append(A32) | |
| Alist.append(A33) | |
| bingolist = [0] * 9 | |
| N = int(input()) | |
| for i in range(N): | |
| b = int(input()) | |
| for j in range(len(bingolist)): | |
| if b == Alist[j]: | |
| bingolist[j] = 1 | |
| if sum(bingolist[0:3]) == 3 or sum(bingolist[3:6]) == 3 or sum(bingolist[6:9]) == 3 or (bingolist[0] + bingolist[3] + bingolist[6] == 3) or (bingolist[1] + bingolist[4] + bingolist[7] == 3) or (bingolist[2] + bingolist[5] + bingolist[8] == 3) or (bingolist[0] + bingolist[4] + bingolist[8] == 4) or (bingolist[2] + bingolist[4] + bingolist[6] == 3): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 008 | |
| ```python | |
| def main(): | |
| A = [list(map(int, input().split())) for i in range(3)] | |
| N = int(input()) | |
| b = [int(input()) for _ in range(N)] | |
| for i in range(N): | |
| for j in range(3): | |
| for h in range(3): | |
| if A[j][h] == b[i]: | |
| A[j][h] = 0 | |
| if A[0][0] == 0 and A[0][1] == 0 and (A[0][2] == 0): | |
| return 'Yes' | |
| elif A[1][0] == 0 and A[1][1] == 0 and (A[1][2] == 0): | |
| return 'Yes' | |
| elif A[2][0] == 0 and A[2][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][0] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| elif A[0][1] == 0 and A[1][1] == 0 and (A[2][1] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][2] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][1] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| return 'No' | |
| print(main()) | |
| ``` | |
| ### 009 | |
| ```python | |
| import math | |
| n = int(input()) | |
| def f(x, y, d): | |
| t = abs(x - y) | |
| if y == 1: | |
| return math.ceil(t / d) | |
| if y == n: | |
| return math.ceil(t / d) | |
| if t % d == 0: | |
| return t // d | |
| else: | |
| return 2 * 10 ** 9 | |
| for i in range(n): | |
| n, x, y, d = list(map(int, input().split())) | |
| ans = min([f(x, y, d), f(x, 1, d) + f(1, y, d), f(x, n, d) + f(n, y, d)]) | |
| if ans == -2 * 10 ** 9: | |
| print('-1') | |
| else: | |
| print(ans) | |
| ``` | |
| ### 010 | |
| ```python | |
| MAX = 10 ** 18 + 5 | |
| n, m, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| cost = [list(map(int, input().split())) for _ in range(n)] | |
| min1 = [[MAX] * (k + 1) for _ in range(n)] | |
| min2 = [[MAX] * (k + 1) for _ in range(n)] | |
| idx = [[-1] * (k + 1) for _ in range(n)] | |
| for i in cost: | |
| i.insert(0, 0) | |
| dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)] | |
| if a[0] == 0: | |
| for i in range(-1, m + 1): | |
| if min1[0][1] >= cost[0][i]: | |
| if min1[0][1] == cost[0][i]: | |
| idx[0][1] = -2 | |
| else: | |
| idx[0][1] = i | |
| min2[0][1] = min1[0][1] | |
| min1[0][1] = cost[0][i] | |
| elif min2[0][1] >= cost[0][i]: | |
| min2[0][1] = cost[0][i] | |
| dp[0][1][i] = cost[0][i] | |
| else: | |
| dp[0][1][a[0]] = 0 | |
| min1[0][1] = 0 | |
| idx[0][1] = a[0] | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if a[i] == 0: | |
| for l in range(1, m + 1): | |
| dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l]) | |
| if l == idx[i - 1][j - 1]: | |
| dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| for l in range(1, m + 1): | |
| dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]]) | |
| if l != a[i]: | |
| dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]]) | |
| for l in range(1, m + 1): | |
| if min1[i][j] >= dp[i][j][l]: | |
| if min1[i][j] == dp[i][j][l]: | |
| idx[i][j] = -2 | |
| else: | |
| idx[i][j] = l | |
| min2[i][j] = min1[i][j] | |
| min1[i][j] = dp[i][j][l] | |
| elif min2[i][j] >= dp[i][j][l]: | |
| min2[i][j] = dp[i][j][l] | |
| ans = MAX | |
| for i in dp[-1][-1]: | |
| ans = min(ans, i) | |
| print(ans if ans < MAX else -1) | |
| ``` | |
| ### 011 | |
| ```python | |
| def main(): | |
| from sys import stdin, stdout | |
| def read(): | |
| return stdin.readline().rstrip('\n') | |
| def read_array(sep=None, maxsplit=-1): | |
| return read().split(sep, maxsplit) | |
| def read_int(): | |
| return int(read()) | |
| def read_int_array(sep=None, maxsplit=-1): | |
| return [int(a) for a in read_array(sep, maxsplit)] | |
| def write(*args, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in args)) + end) | |
| def write_array(array, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in array)) + end) | |
| n = read_int() | |
| nums = read_int_array() | |
| counts = {} | |
| import collections | |
| inv_counts = collections.defaultdict(int) | |
| out = 0 | |
| for i, x in enumerate(nums): | |
| c = inv_counts[x] | |
| if c: | |
| counts[c].remove(x) | |
| if not counts[c]: | |
| counts.pop(c) | |
| c += 1 | |
| inv_counts[x] = c | |
| counts.setdefault(c, set()).add(x) | |
| if len(counts) == 2 and any((len(s) == 1 and (k - 1 in counts or k == 1) for k, s in list(counts.items()))): | |
| out = i + 1 | |
| elif len(counts) == 1: | |
| k = next(iter(list(counts.keys()))) | |
| if k == 2 or len(counts[k]) == 1: | |
| out = i + 1 | |
| write(out) | |
| main() | |
| ``` | |
| ### 012 | |
| ```python | |
| from math import gcd | |
| def covered_points(xa, ya, xb, yb): | |
| if xb < xa: | |
| temp_x = xa | |
| xa = xb | |
| xb = temp_x | |
| temp_y = ya | |
| ya = yb | |
| yb = temp_y | |
| y_0 = abs(yb - ya) | |
| x_0 = xb - xa | |
| return gcd(y_0, x_0) + 1 | |
| def intersection2(xa, ya, xb, yb, xc, yc, xd, yd): | |
| if max(xa, xb) < min(xc, xd): | |
| return () | |
| if max(ya, yb) < min(yc, yd): | |
| return () | |
| if not (xa == xb and xc == xd or (ya == yb and yc == yd)): | |
| return () | |
| if ya == yb and yc == yd: | |
| return () | |
| a1 = yb - ya | |
| b1 = xb - xa | |
| c1 = xa * a1 - ya * b1 | |
| a2 = yd - yc | |
| b2 = xd - xc | |
| c2 = xc * a2 - yc * b2 | |
| det = a1 * b2 - a2 * b1 | |
| if det == 0: | |
| return () | |
| detx = c1 * b2 - c2 * b1 | |
| dety = -a1 * c2 + a2 * c1 | |
| if detx % det != 0 or dety % det != 0: | |
| return () | |
| x = int(detx / det) | |
| y = int(dety / det) | |
| if x < min(xa, xb) or x > max(xa, xb) or x < min(xc, xd) or (x > max(xc, xd)): | |
| return () | |
| if y < min(ya, yb) or y > max(ya, yb) or y < min(yc, yd) or (y > max(yc, yd)): | |
| return () | |
| return (x, y) | |
| def __starting_point(): | |
| n = int(input()) | |
| Ax = [0] * n | |
| Ay = [0] * n | |
| Bx = [0] * n | |
| By = [0] * n | |
| n_cov = 0 | |
| intersections = {} | |
| for i in range(n): | |
| line = input().split(sep=' ') | |
| Ax[i] = int(line[0]) | |
| Ay[i] = int(line[1]) | |
| Bx[i] = int(line[2]) | |
| By[i] = int(line[3]) | |
| n_cov += covered_points(Ax[i], Ay[i], Bx[i], By[i]) | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| temp = intersection2(Ax[i], Ay[i], Bx[i], By[i], Ax[j], Ay[j], Bx[j], By[j]) | |
| if len(temp) == 0: | |
| continue | |
| if temp in intersections: | |
| intersections[temp].append(i) | |
| intersections[temp].append(j) | |
| else: | |
| intersections[temp] = [i, j] | |
| for i in intersections: | |
| n_cov = n_cov - len(set(intersections[i])) + 1 | |
| print(n_cov) | |
| __starting_point() | |
| ``` | |
| ### 013 | |
| ```python | |
| df = int(input()) | |
| for _ in range(df): | |
| n, x, y, d = list(map(int, input().split())) | |
| d2 = abs(x - y) | |
| ans = 3000000000.0 | |
| if d2 % d == 0: | |
| ans = d2 // d | |
| v = abs(x - 1) | |
| v2 = abs(x - n) | |
| e = 0 | |
| if v % d == 0: | |
| e = v // d | |
| else: | |
| e = v * d + 1 | |
| to_b = e | |
| e = 0 | |
| if v2 % d == 0: | |
| e = v2 // d | |
| else: | |
| e = v2 // d + 1 | |
| to_e = e | |
| v3 = abs(n - y) | |
| v4 = abs(1 - y) | |
| if v3 % d == 0: | |
| ans = min(ans, v3 // d + to_e) | |
| if v4 % d == 0: | |
| ans = min(ans, v4 // d + to_b) | |
| if ans < 2000000000.0: | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 014 | |
| ```python | |
| n, h, l, r = list(map(int, input().split())) | |
| N = n | |
| a = list(map(int, input().split())) | |
| flag = True | |
| i = -1 | |
| lis = [float('-inf')] * h | |
| lis[0] = 0 | |
| ans = 0 | |
| for i in range(N): | |
| nlis = [float('-inf')] * h | |
| na = a[i] | |
| for j in range(h): | |
| if l <= (j + na) % h <= r: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j]) | |
| if l < (j + na - 1) % h <= r: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j]) | |
| lis = nlis | |
| print(max(0, max(lis))) | |
| ``` | |
| ### 015 | |
| ```python | |
| MAX = 10 ** 18 + 5 | |
| n, m, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| cost = [list(map(int, input().split())) for _ in range(n)] | |
| min1 = [[MAX] * (k + 1) for _ in range(n)] | |
| min2 = [[MAX] * (k + 1) for _ in range(n)] | |
| idx = [[-1] * (k + 1) for _ in range(n)] | |
| for i in cost: | |
| i.insert(0, 0) | |
| dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)] | |
| if a[0] == 0: | |
| for i in range(1, m + 1 - 1): | |
| if min1[0][1] >= cost[0][i]: | |
| if min1[0][1] == cost[0][i]: | |
| idx[0][1] = -2 | |
| else: | |
| idx[0][1] = i | |
| min2[0][1] = min1[0][1] | |
| min1[0][1] = cost[0][i] | |
| elif min2[0][1] >= cost[0][i]: | |
| min2[0][1] = cost[0][i] | |
| dp[0][1][i] = cost[0][i] | |
| else: | |
| dp[0][1][a[0]] = 0 | |
| min1[0][1] = 0 | |
| idx[0][1] = a[0] | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if a[i] == 0: | |
| for l in range(1, m + 1): | |
| dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l]) | |
| if l == idx[i - 1][j - 1]: | |
| dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| for l in range(1, m + 1): | |
| dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]]) | |
| if l != a[i]: | |
| dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]]) | |
| for l in range(1, m + 1): | |
| if min1[i][j] >= dp[i][j][l]: | |
| if min1[i][j] == dp[i][j][l]: | |
| idx[i][j] = -2 | |
| else: | |
| idx[i][j] = l | |
| min2[i][j] = min1[i][j] | |
| min1[i][j] = dp[i][j][l] | |
| elif min2[i][j] >= dp[i][j][l]: | |
| min2[i][j] = dp[i][j][l] | |
| ans = MAX | |
| for i in dp[-1][-1]: | |
| ans = min(ans, i) | |
| print(ans if ans < MAX else -1) | |
| ``` | |
| ### 016 | |
| ```python | |
| def min_sub_array(day, k): | |
| if not day: | |
| return [0] * (k + 1) | |
| n = len(day) | |
| best = [float('inf')] * (n + 1) | |
| best[0] = 0 | |
| best[1] = 1 | |
| for size in range(2, n + -1): | |
| for i in range(n + 1 - size): | |
| best[size] = min(best[size], day[i + size - 1] - day[i] + 1) | |
| output = [0] * (k + 1) | |
| for i in range(k + 1): | |
| if n - i > 0: | |
| output[i] = best[n - i] | |
| return output | |
| N, M, K = list(map(int, input().split())) | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| best = min_sub_array(day, K) | |
| for _ in range(N - 1): | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| new_day_best = min_sub_array(day, K) | |
| new_best = [float('inf')] * (K + 1) | |
| for i in range(K + 1): | |
| for j in range(i + 1): | |
| new_best[i] = min(new_best[i], new_day_best[j] + best[i - j]) | |
| best = new_best | |
| print(best[K]) | |
| ``` | |
| ### 017 | |
| ```python | |
| A11, A12, A13 = list(map(int, input().split())) | |
| A21, A22, A23 = list(map(int, input().split())) | |
| A31, A32, A33 = list(map(int, input().split())) | |
| Alist = [] | |
| Alist.append(A11) | |
| Alist.append(A12) | |
| Alist.append(A13) | |
| Alist.append(A21) | |
| Alist.append(A22) | |
| Alist.append(A23) | |
| Alist.append(A31) | |
| Alist.append(A32) | |
| Alist.append(A33) | |
| bingolist = [0] * 9 | |
| N = int(input()) | |
| for i in range(N): | |
| b = int(input()) | |
| for j in range(len(bingolist)): | |
| if b == Alist[j]: | |
| bingolist[j] = 1 | |
| if sum(bingolist[0:3]) == 3 or sum(bingolist[3:6]) == 3 or sum(bingolist[6:9]) == 3 or (bingolist[0] + bingolist[3] + bingolist[6] == 3) or (bingolist[1] + bingolist[4] + bingolist[7] == 1) or (bingolist[2] + bingolist[5] + bingolist[8] == 3) or (bingolist[0] + bingolist[4] + bingolist[8] == 3) or (bingolist[2] + bingolist[4] + bingolist[6] == 3): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 018 | |
| ```python | |
| import sys | |
| YES = 'Yes' | |
| NO = 'No' | |
| def solve(A: 'List[List[int]]', N: int, b: 'List[int]'): | |
| A_ = [] | |
| for a_ in A: | |
| A_ += [[a__, False] for a__ in a_] | |
| for b_ in b: | |
| for a__ in A_: | |
| if a__[0] == b_: | |
| a__[1] = True | |
| if A_[0][1] and ((A_[1][1] and A_[2][1]) and (A_[3][1] and A_[6][1]) and (A_[4][1] and A_[8][1])) or (A_[1][1] and A_[4][1] and A_[7][1]) or (A_[2][1] and A_[5][1] and A_[8][1]) or (A_[3][1] and A_[4][1] and A_[5][1]) or (A_[6][1] and A_[7][1] and A_[8][1]) or (A_[2][1] and A_[4][1] and A_[6][1]): | |
| print(YES) | |
| else: | |
| print(NO) | |
| return | |
| def main(): | |
| def iterate_tokens(): | |
| for line in sys.stdin: | |
| for word in line.split(): | |
| yield word | |
| tokens = iterate_tokens() | |
| A = [[int(next(tokens)) for _ in range(3)] for _ in range(3)] | |
| N = int(next(tokens)) | |
| b = [int(next(tokens)) for _ in range(N)] | |
| solve(A, N, b) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 019 | |
| ```python | |
| import math | |
| dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)] | |
| n, m, k = list(map(int, input().split())) | |
| k += 1 | |
| z = list(map(int, input().split())) | |
| matrix = [] | |
| for i in range(n): | |
| ans = list(map(int, input().split())) | |
| matrix.append(ans) | |
| for i in range(len(z)): | |
| if i == 0: | |
| if z[i] != 0: | |
| dp[0][1][z[i] - 1] = 0 | |
| else: | |
| for x in range(m): | |
| dp[0][1][x] = matrix[i][x] | |
| elif z[i] != 0: | |
| col = z[i] - 1 | |
| if z[i - 1] != 0: | |
| col2 = z[i - 1] - 1 | |
| if col2 == col: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col]) | |
| else: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col]) | |
| else: | |
| pq = [] | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| col = z[i] - 1 | |
| if col != pq[0][1] and pq[0][0] != math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col]) | |
| elif pq[0][0] != math.inf and col == pq[0][1]: | |
| dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col]) | |
| elif pq[0][0] == math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col]) | |
| elif z[i - 1] != 0: | |
| col = z[i - 1] - 1 | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| for x in range(m): | |
| if x != col: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x]) | |
| else: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x]) | |
| else: | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| for v in range(m): | |
| if v != pq[0][1]: | |
| dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v]) | |
| else: | |
| dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v]) | |
| mini = math.inf | |
| for i in range(m - 1): | |
| mini = min(mini, dp[n - 1][k - 1][i]) | |
| if mini == math.inf: | |
| print(-1) | |
| else: | |
| print(mini) | |
| ``` | |
| ### 020 | |
| ```python | |
| def go(): | |
| n, h, l, r = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| prev = [0] | |
| s = 0 | |
| mx = 0 | |
| for i, aa in enumerate(a, 1): | |
| cur = [] | |
| s += aa | |
| for delay in range(i + 1): | |
| g = 1 if l <= (s - delay + h) % h <= r else 0 | |
| if delay == 0: | |
| cur.append(prev[delay] - g) | |
| elif delay == i: | |
| cur.append(prev[delay - 1] + g) | |
| else: | |
| cur.append(max(prev[delay], prev[delay - 1]) + g) | |
| prev = cur | |
| mx = max(mx, max(cur)) | |
| return mx | |
| for _ in range(1): | |
| print(go()) | |
| ``` | |
| ### 021 | |
| ```python | |
| MAX = 10 ** 18 + 5 | |
| n, m, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| cost = [list(map(int, input().split())) for _ in range(n)] | |
| min1 = [[MAX] * (k + 1) for _ in range(n)] | |
| min2 = [[MAX] * (k + 1) for _ in range(n)] | |
| idx = [[-1] * (k + 1) for _ in range(n)] | |
| for i in cost: | |
| i.insert(0, 0) | |
| dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)] | |
| if a[0] == 0: | |
| for i in range(2, m + 1): | |
| if min1[0][1] >= cost[0][i]: | |
| if min1[0][1] == cost[0][i]: | |
| idx[0][1] = -2 | |
| else: | |
| idx[0][1] = i | |
| min2[0][1] = min1[0][1] | |
| min1[0][1] = cost[0][i] | |
| elif min2[0][1] >= cost[0][i]: | |
| min2[0][1] = cost[0][i] | |
| dp[0][1][i] = cost[0][i] | |
| else: | |
| dp[0][1][a[0]] = 0 | |
| min1[0][1] = 0 | |
| idx[0][1] = a[0] | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if a[i] == 0: | |
| for l in range(1, m + 1): | |
| dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l]) | |
| if l == idx[i - 1][j - 1]: | |
| dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| for l in range(1, m + 1): | |
| dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]]) | |
| if l != a[i]: | |
| dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]]) | |
| for l in range(1, m + 1): | |
| if min1[i][j] >= dp[i][j][l]: | |
| if min1[i][j] == dp[i][j][l]: | |
| idx[i][j] = -2 | |
| else: | |
| idx[i][j] = l | |
| min2[i][j] = min1[i][j] | |
| min1[i][j] = dp[i][j][l] | |
| elif min2[i][j] >= dp[i][j][l]: | |
| min2[i][j] = dp[i][j][l] | |
| ans = MAX | |
| for i in dp[-1][-1]: | |
| ans = min(ans, i) | |
| print(ans if ans < MAX else -1) | |
| ``` | |
| ### 022 | |
| ```python | |
| from functools import reduce | |
| n = int(input()) | |
| s1, s2 = (str(input()), str(input())) | |
| b1 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) < ord(s2[i])) for i in range(n)], False) | |
| b2 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) > ord(s2[i])) for i in range(n)], False) | |
| s = sum([(s1[i] == '?') + (s2[i] == '?') for i in range(n)]) | |
| ans1 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else ord(s2[i]) - ord('0') + 1 if s1[i] == '?' else 10 - ord(s1[i]) + ord('0') if s2[i] == '?' else 1 for i in range(n)], 1) | |
| ans2 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else 10 - ord(s2[i]) + ord('0') if s1[i] == '?' else ord(s1[i]) - ord('0') + 1 if s2[i] == '?' else 1 for i in range(n)], 1) | |
| ans3 = reduce(lambda x, y: x * y % 1000000007, [10 if s1[i] == '?' and s2[i] == '?' else 1 for i in range(n)], 1) | |
| print((10 ** s - (not b2) * ans1 - (not b1) * ans2 + (not b1 and (not b2)) * ans3) % 1000000008) | |
| ``` | |
| ### 023 | |
| ```python | |
| n = int(input()) | |
| u = [int(u) for u in input().split()] | |
| NMAX = 100005 | |
| suma = [0 for _ in range(NMAX)] | |
| total = [0 for _ in range(NMAX)] | |
| diferentes = 0 | |
| sol = 0 | |
| maximo = 1 | |
| for i, v in enumerate(u): | |
| if total[v] == 0: | |
| diferentes += 1 | |
| else: | |
| suma[total[v]] -= 1 | |
| total[v] += 1 | |
| suma[total[v]] += 1 | |
| maximo = max(maximo, total[v]) | |
| if diferentes <= 2: | |
| sol = i | |
| if suma[maximo - 1] == diferentes - 1 and suma[maximo] == 1: | |
| sol = i | |
| if suma[maximo] == diferentes - 1 and suma[1] == 1: | |
| sol = i | |
| if suma[1] == diferentes: | |
| sol = i | |
| print(sol + 1) | |
| ``` | |
| ### 024 | |
| ```python | |
| import sys | |
| from math import gcd | |
| class Segment: | |
| def __init__(self, x1, y1, x2, y2): | |
| self.x1 = x1 | |
| self.y1 = y1 | |
| self.x2 = x2 | |
| self.y2 = y2 | |
| def dx(self): | |
| return self.x2 - self.x1 | |
| def dy(self): | |
| return self.y2 - self.y1 | |
| def pt_cnt(self): | |
| return gcd(abs(self.dx()), abs(self.dy())) + 1 | |
| def A(self): | |
| return self.y1 - self.y2 | |
| def B(self): | |
| return self.x2 - self.x1 | |
| def C(self): | |
| return -self.A() * self.x1 - self.B() * self.y1 | |
| def inner_x(self, x): | |
| l = self.x1 | |
| r = self.x2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= x <= r | |
| def inner_y(self, y): | |
| l = self.y1 | |
| r = self.y2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l < y <= r | |
| def inner(self, x, y): | |
| return self.inner_x(x) and self.inner_y(y) | |
| def intersect(self, other): | |
| dx = det(self.C(), self.B(), other.C(), other.B()) | |
| dy = det(self.A(), self.C(), other.A(), other.C()) | |
| d = det(self.A(), self.B(), other.A(), other.B()) | |
| if d == 0: | |
| return (False, 0, 0) | |
| if dx % d != 0 or dy % d != 0: | |
| return (False, 0, 0) | |
| x = -dx // d | |
| y = -dy // d | |
| if not self.inner(x, y) or not other.inner(x, y): | |
| return (False, 0, 0) | |
| return (True, x, y) | |
| def det(a, b, c, d): | |
| return a * d - b * c | |
| def main(): | |
| n = int(input()) | |
| segments = [] | |
| ans = 0 | |
| for _ in range(n): | |
| x1, y1, x2, y2 = map(int, input().split()) | |
| segment = Segment(x1, y1, x2, y2) | |
| ans += segment.pt_cnt() | |
| pt = set() | |
| for other in segments: | |
| result, x, y = segment.intersect(other) | |
| if result: | |
| pt.add((x, y)) | |
| ans -= len(pt) | |
| segments.append(segment) | |
| print(ans) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 025 | |
| ```python | |
| import math | |
| dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)] | |
| n, m, k = list(map(int, input().split())) | |
| k += 1 | |
| z = list(map(int, input().split())) | |
| matrix = [] | |
| for i in range(n): | |
| ans = list(map(int, input().split())) | |
| matrix.append(ans) | |
| for i in range(len(z)): | |
| if i == 0: | |
| if z[i] != 0: | |
| dp[0][1][z[i] - 1] = 0 | |
| else: | |
| for x in range(m): | |
| dp[0][1][x] = matrix[i][x] | |
| elif z[i] != 4: | |
| col = z[i] - 1 | |
| if z[i - 1] != 0: | |
| col2 = z[i - 1] - 1 | |
| if col2 == col: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col]) | |
| else: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col]) | |
| else: | |
| pq = [] | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| col = z[i] - 1 | |
| if col != pq[0][1] and pq[0][0] != math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col]) | |
| elif pq[0][0] != math.inf and col == pq[0][1]: | |
| dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col]) | |
| elif pq[0][0] == math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col]) | |
| elif z[i - 1] != 0: | |
| col = z[i - 1] - 1 | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| for x in range(m): | |
| if x != col: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x]) | |
| else: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x]) | |
| else: | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| for v in range(m): | |
| if v != pq[0][1]: | |
| dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v]) | |
| else: | |
| dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v]) | |
| mini = math.inf | |
| for i in range(m): | |
| mini = min(mini, dp[n - 1][k - 1][i]) | |
| if mini == math.inf: | |
| print(-1) | |
| else: | |
| print(mini) | |
| ``` | |
| ### 026 | |
| ```python | |
| df = int(input()) | |
| for _ in range(df): | |
| n, x, y, d = list(map(int, input().split())) | |
| d2 = abs(x - y) | |
| ans = 3000000000.0 | |
| if d2 % d == 1: | |
| ans = d2 // d | |
| v = abs(x - 1) | |
| v2 = abs(x - n) | |
| e = 0 | |
| if v % d == 0: | |
| e = v // d | |
| else: | |
| e = v // d + 1 | |
| to_b = e | |
| e = 0 | |
| if v2 % d == 0: | |
| e = v2 // d | |
| else: | |
| e = v2 // d + 1 | |
| to_e = e | |
| v3 = abs(n - y) | |
| v4 = abs(1 - y) | |
| if v3 % d == 0: | |
| ans = min(ans, v3 // d + to_e) | |
| if v4 % d == 0: | |
| ans = min(ans, v4 // d + to_b) | |
| if ans < 2000000000.0: | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 027 | |
| ```python | |
| def main(): | |
| n, m, k = list(map(int, input().split())) | |
| c, *cc = list(map(int, input().split())) | |
| pp, *ppp = (list(map(float, input().split())) for _ in range(n)) | |
| inf = 2.0 ** 47 | |
| nxt = [[0.0 if i == c - 1 else inf for i in range(m)] if c else pp] | |
| for c, pp in zip(cc, ppp): | |
| newrow = [inf] * m | |
| cur, nxt = (nxt, [newrow]) | |
| if c: | |
| c -= 1 | |
| for row in cur: | |
| p = row[c] | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| row[c] = inf | |
| newrow = [inf] * m | |
| newrow[c] = min(row) | |
| nxt.append(newrow) | |
| else: | |
| for row in cur: | |
| for c, p in enumerate((a + b for a, b in zip(row, pp))): | |
| if not newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| bestclr = min(list(range(m)), key=row.__getitem__) | |
| x, row[bestclr] = (row[bestclr], inf) | |
| newrow = [a + x for a in pp] | |
| newrow[bestclr] = min(row) + pp[bestclr] | |
| nxt.append(newrow) | |
| p = min(nxt[-1]) | |
| print(int(p) if p < inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 028 | |
| ```python | |
| n, m, k = input().split(' ') | |
| n = int(n) | |
| m = int(m) | |
| k = int(k) | |
| ind = [] | |
| pre = [] | |
| for _ in range(n): | |
| s = input() | |
| ind.append([]) | |
| for i, c in enumerate(s): | |
| if c == '1': | |
| ind[-1].append(i) | |
| for i in range(n): | |
| pre.append([]) | |
| for j in range(k + 1): | |
| pre[i].append([]) | |
| if len(ind[i]) > j: | |
| pre[i][j] = ind[i][-1] - ind[i][0] + 1 | |
| else: | |
| pre[i][j] = 0 | |
| continue | |
| for x in range(j + 1): | |
| y = len(ind[i]) - 1 - j + x | |
| if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: | |
| pre[i][j] = ind[i][y] - ind[i][x] + 1 | |
| dp = [[]] | |
| for i in range(k + 1): | |
| dp[0].append(pre[0][i]) | |
| for i in range(1, n): | |
| dp.append([]) | |
| for j in range(0, k + 1): | |
| dp[i].append(pre[i][j] + dp[i - 1][0]) | |
| for z in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) | |
| print(dp[n - 1][k - 1]) | |
| ``` | |
| ### 029 | |
| ```python | |
| def factorial(n): | |
| if n == 0: | |
| return 1 | |
| else: | |
| return n * factorial(n - 1) % (10 ** 9 + 7) | |
| n, k = [int(i) for i in input().split()] | |
| s = [[] for i in range(n)] | |
| for i in range(n - 1): | |
| a, b, c = [int(i) for i in input().split()] | |
| if c == 0: | |
| s[a - 1].append(b - 1) | |
| s[b - 1].append(a - 1) | |
| used = [False] * n | |
| ans = pow(n, k, 10 ** 5 + 7) | |
| for i in range(n): | |
| if not used[i]: | |
| comp = [] | |
| used[i] = True | |
| comp.append(i) | |
| c = 0 | |
| while c < len(comp): | |
| for h in s[comp[c]]: | |
| if not used[h]: | |
| used[h] = True | |
| comp.append(h) | |
| c += 1 | |
| ans = (ans - pow(len(comp), k, 10 ** 9 + 7)) % (10 ** 9 + 7) | |
| print(ans) | |
| ``` | |
| ### 030 | |
| ```python | |
| import copy | |
| def right(a): | |
| d = a[1] - a[0] | |
| res, f = (-3, True) | |
| for i in range(2, len(a)): | |
| if a[i] - a[i - 1] == d + 1: | |
| a[i] -= 1 | |
| res += 1 | |
| elif a[i] - a[i - 1] == d - 1: | |
| a[i] += 1 | |
| res += 1 | |
| elif a[i] - a[i - 1] != d: | |
| f = False | |
| break | |
| if not f: | |
| res = 10 ** 9 | |
| return res | |
| n = int(input()) | |
| bs = list(map(int, input().split())) | |
| if n <= 2: | |
| print(0) | |
| else: | |
| f = n + 1 | |
| bs[0] -= 1 | |
| bs[1] -= 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[0] += 1 | |
| bs[1] -= 2 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy())) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[0] += 1 | |
| bs[1] -= 2 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| if f == n + 1: | |
| f = -1 | |
| print(f) | |
| ``` | |
| ### 031 | |
| ```python | |
| n = int(input()) | |
| C = 10 ** 9 + 7 | |
| a = input() | |
| s = input() | |
| d = [[0, 0, 0, 1]] | |
| for q in range(n): | |
| d.append(d[-1][:]) | |
| if a[q] != '?' and s[q] != '?': | |
| if int(a[q]) < int(s[q]): | |
| d[-1][1] = d[-1][3] = 0 | |
| d[-1][0] += d[-2][3] | |
| d[-1][2] += d[-2][1] | |
| elif int(a[q]) > int(s[q]): | |
| d[-1][0] = d[-1][3] = 0 | |
| d[-1][1] += d[-2][3] | |
| d[-1][2] += d[-2][0] | |
| elif s[q] != '?': | |
| d[-1][2] *= 10 | |
| d[-1][0] += d[-2][3] | |
| d[-1][1] += d[-2][3] | |
| d[-1][0] *= int(s[q]) + 1 | |
| d[-1][1] *= 10 - int(s[q]) | |
| d[-1][0] -= d[-2][3] | |
| d[-1][1] -= d[-2][3] | |
| d[-1][2] += d[-2][0] * (9 - int(s[q])) + d[-2][1] * int(s[q]) | |
| elif a[q] != '?': | |
| d[-1][2] *= 10 | |
| d[-1][0] += d[-2][3] | |
| d[-1][1] += d[-2][3] | |
| d[-1][1] *= int(a[q]) + 1 | |
| d[-1][0] *= 10 - int(a[q]) | |
| d[-1][0] -= d[-2][3] | |
| d[-1][1] -= d[-2][3] | |
| d[-1][2] += d[-2][1] * (9 - int(a[q])) + d[-2][0] * int(a[q]) | |
| else: | |
| d[-1][3] *= 10 | |
| d[-1][2] *= 100 | |
| d[-1][0] *= 55 | |
| d[-1][1] *= 55 | |
| d[-1][0] += d[-2][3] * 45 | |
| d[-1][1] += d[-2][3] * 45 | |
| d[-1][2] += d[-2][0] * 45 + d[-2][1] * 45 | |
| d[-1][0] %= C | |
| d[-1][1] %= C | |
| d[-1][2] %= C | |
| d[-1][3] %= C | |
| print(d[-1][2]) | |
| ``` | |
| ### 032 | |
| ```python | |
| def cal(x, y): | |
| d = a[1] + y - a[0] - x | |
| wk1 = a[1] + y | |
| wkans = 0 | |
| for k in range(2, n): | |
| diff = abs(wk1 + d - a[k]) | |
| if diff == 1: | |
| wkans += 1 | |
| elif diff > 1: | |
| return n + 1 | |
| wk1 += d | |
| return wkans | |
| n = int(input()) | |
| a = [int(i) for i in input().split()] | |
| if n > 2: | |
| ans_final = n + 1 | |
| for i in range(-1, 2): | |
| for j in range(-1, 2): | |
| ans_final = min(ans_final, cal(i, j) + abs(i) + abs(j)) | |
| else: | |
| ans_final = 0 | |
| if ans_final == n + 2: | |
| print(-1) | |
| else: | |
| print(ans_final) | |
| ``` | |
| ### 033 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, h, l, r = map(int, input().split()) | |
| a = [int(item) for item in input().split()] | |
| dp = [[-1] * h for _ in range(n + 1)] | |
| dp[0][0] = 0 | |
| for i, item in enumerate(a): | |
| for j in range(h): | |
| if dp[i][j] == -1: | |
| continue | |
| nt = (j + item) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt - 1] = max(dp[i + 1][nt], dp[i][j]) | |
| nt = (j + item - 1 + h) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| print(max(dp[-1])) | |
| ``` | |
| ### 034 | |
| ```python | |
| import sys | |
| def solve(): | |
| input = sys.stdin.readline | |
| N, H, L, R = map(int, input().split()) | |
| A = [int(a) for a in input().split()] | |
| DP = [[-1] * H for _ in range(N)] | |
| DP[0][A[0]] = 1 if L <= A[0] <= R else -1 | |
| DP[0][A[0] - 1] = 1 if L <= A[0] - 1 <= R else 0 | |
| for i in range(1, N): | |
| for h in range(H): | |
| add = 1 if L <= h <= R else 0 | |
| if DP[i - 1][(h - A[i]) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add) | |
| if DP[i - 1][(h - A[i] + 1) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add) | |
| print(max(DP[N - 1])) | |
| return 0 | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 035 | |
| ```python | |
| def solve(seq): | |
| if len(seq) <= 2: | |
| return 0 | |
| startings = [] | |
| for x in (-1, 0, 1): | |
| for y in (-1, 0, 1): | |
| start = (seq[1] + y, seq[0] + x - seq[1] - y, int(x != 0) + int(y != 0)) | |
| startings.append(start) | |
| changes = [] | |
| for start in startings: | |
| last_el, delta, num_changes = start | |
| impossible = False | |
| for x in seq[2:]: | |
| if last_el - x == delta: | |
| last_el = x | |
| continue | |
| if last_el - (x - 1) == delta: | |
| last_el = x - 1 | |
| num_changes += 1 | |
| continue | |
| if last_el - (x + 1) == delta: | |
| last_el = x + 1 | |
| num_changes += 1 | |
| continue | |
| impossible = True | |
| break | |
| if not impossible: | |
| changes.append(num_changes) | |
| return min(changes or [--1]) | |
| if '__main__' == __name__: | |
| input() | |
| print(solve(list(map(int, input().strip().split())))) | |
| ``` | |
| ### 036 | |
| ```python | |
| from math import gcd | |
| n = int(input()) | |
| pts = [] | |
| ans = 0 | |
| for i in range(0, n): | |
| a, b, A, B = tuple(map(int, input().split(' '))) | |
| pts.append((a, b, A, B)) | |
| ans += gcd(abs(a - A), abs(b - B)) + 1 | |
| m = dict() | |
| for i in range(n): | |
| for j in range(i + 1 + 1, n): | |
| a1 = pts[i][0] | |
| b1 = pts[i][1] | |
| a2 = pts[i][2] | |
| b2 = pts[i][3] | |
| A1 = pts[j][0] | |
| B1 = pts[j][1] | |
| A2 = pts[j][2] | |
| B2 = pts[j][3] | |
| num1 = (b2 - B2) * (A2 - A1) * (a2 - a1) + A2 * (a2 - a1) * (B2 - B1) - a2 * (A2 - A1) * (b2 - b1) | |
| den1 = (B2 - B1) * (a2 - a1) - (b2 - b1) * (A2 - A1) | |
| if den1 != 0 and num1 % den1 == 0: | |
| if a1 == a2: | |
| num2 = B2 * (A2 - A1) + (num1 // den1 - A2) * (B2 - B1) | |
| den2 = A2 - A1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| else: | |
| num2 = b2 * (a2 - a1) + (num1 // den1 - a2) * (b2 - b1) | |
| den2 = a2 - a1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| sum = 0 | |
| for i in list(m.values()): | |
| sum += len(i) - 1 | |
| print(ans - sum) | |
| ``` | |
| ### 037 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, h, l, r = map(int, input().split()) | |
| a = [int(item) for item in input().split()] | |
| dp = [[-1] * h for _ in range(n + 1)] | |
| dp[0][0] = 0 | |
| for i, item in enumerate(a): | |
| for j in range(h): | |
| if dp[i][j] == -1: | |
| continue | |
| nt = (j + item) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt - 1] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| nt = (j + item - 1 + h) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| print(max(dp[-1])) | |
| ``` | |
| ### 038 | |
| ```python | |
| df = int(input()) | |
| for _ in range(df): | |
| n, x, y, d = list(map(int, input().split())) | |
| d2 = abs(x - y) | |
| ans = 3000000000.0 | |
| if d2 % d == 0: | |
| ans = d2 // d | |
| v = abs(x - 1) | |
| v2 = abs(x - n) | |
| e = 0 | |
| if v % d == 0: | |
| e = v // d | |
| else: | |
| e = v // d + 1 | |
| to_b = e | |
| e = 0 | |
| if v2 % d == 0: | |
| e = v2 // d | |
| else: | |
| e = v2 // d + 1 | |
| to_e = e | |
| v3 = abs(n - y) | |
| v4 = abs(1 - y) | |
| if v3 % d == 0: | |
| ans = min(ans, v3 // d + to_e) | |
| if v4 % d == 1: | |
| ans = min(ans, v4 // d + to_b) | |
| if ans < 2000000000.0: | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 039 | |
| ```python | |
| 3 | |
| def build(n, s, t): | |
| ans = 1 | |
| for i in range(n): | |
| if s[i] == '?' and t[i] == '?': | |
| ans = 55 * ans % (10 ** 9 + 7) | |
| elif s[i] == '?': | |
| ans = (ord(t[i]) - ord('0') + 1) * ans % (10 ** 9 + 7) | |
| elif t[i] == '?': | |
| ans = (ord('9') - ord(s[i]) + 1) * ans % (10 ** 9 + 7) | |
| return ans | |
| n = int(input()) | |
| s = input() | |
| t = input() | |
| sltt = True | |
| tlts = True | |
| qm = 0 | |
| cqm = 0 | |
| for i in range(n): | |
| if t[i] == '?': | |
| qm += 1 | |
| if s[i] == '?': | |
| qm += 1 | |
| if t[i] == '?' and s[i] == '?': | |
| cqm += 1 | |
| if t[i] == '?' and s[i] == '?': | |
| continue | |
| if ord(s[i]) < ord(t[i]): | |
| tlts = False | |
| if ord(t[i]) < ord(s[i]): | |
| sltt = False | |
| if not sltt and (not tlts): | |
| print(pow(10, qm, 10 ** 9 + 7)) | |
| elif sltt and tlts: | |
| print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t) - build(n, t, s) + pow(10, cqm, 10 ** 9 + 7)) % (10 ** 9 + 7)) | |
| elif sltt: | |
| print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t)) % (10 ** 9 + 7)) | |
| else: | |
| print((pow(10, qm, 10 ** 9 + 7) - build(n, t, s)) % (10 ** 9 + 7)) | |
| ``` | |
| ### 040 | |
| ```python | |
| n = int(input()) | |
| u = [int(u) for u in input().split()] | |
| NMAX = 100005 | |
| suma = [0 for _ in range(NMAX)] | |
| total = [0 for _ in range(NMAX)] | |
| diferentes = 0 | |
| sol = 0 | |
| maximo = 3 | |
| for i, v in enumerate(u): | |
| if total[v] == 0: | |
| diferentes += 1 | |
| else: | |
| suma[total[v]] -= 1 | |
| total[v] += 1 | |
| suma[total[v]] += 1 | |
| maximo = max(maximo, total[v]) | |
| if diferentes <= 1: | |
| sol = i | |
| if suma[maximo - 1] == diferentes - 1 and suma[maximo] == 1: | |
| sol = i | |
| if suma[maximo] == diferentes - 1 and suma[1] == 1: | |
| sol = i | |
| if suma[1] == diferentes: | |
| sol = i | |
| print(sol + 1) | |
| ``` | |
| ### 041 | |
| ```python | |
| def solve(seq): | |
| if len(seq) <= 2: | |
| return 1 | |
| startings = [] | |
| for x in (-1, 0, 1): | |
| for y in (-1, 0, 1): | |
| start = (seq[1] + y, seq[0] + x - seq[1] - y, int(x != 0) + int(y != 0)) | |
| startings.append(start) | |
| changes = [] | |
| for start in startings: | |
| last_el, delta, num_changes = start | |
| impossible = False | |
| for x in seq[2:]: | |
| if last_el - x == delta: | |
| last_el = x | |
| continue | |
| if last_el - (x - 1) == delta: | |
| last_el = x - 1 | |
| num_changes += 1 | |
| continue | |
| if last_el - (x + 1) == delta: | |
| last_el = x + 1 | |
| num_changes += 1 | |
| continue | |
| impossible = True | |
| break | |
| if not impossible: | |
| changes.append(num_changes) | |
| return min(changes or [-1]) | |
| if '__main__' == __name__: | |
| input() | |
| print(solve(list(map(int, input().strip().split())))) | |
| ``` | |
| ### 042 | |
| ```python | |
| import sys | |
| YES = 'Yes' | |
| NO = 'No' | |
| def solve(A: 'List[List[int]]', N: int, b: 'List[int]'): | |
| A_ = [] | |
| for a_ in A: | |
| A_ += [[a__, False] for a__ in a_] | |
| for b_ in b: | |
| for a__ in A_: | |
| if a__[0] == b_: | |
| a__[1] = True | |
| if A_[0][1] and (A_[1][1] and A_[2][1] or (A_[3][1] and A_[6][1]) or (A_[4][1] and A_[8][1])) or (A_[1][1] and A_[4][1] and A_[7][1]) or (A_[2][0] and A_[5][1] and A_[8][1]) or (A_[3][1] and A_[4][1] and A_[5][1]) or (A_[6][1] and A_[7][1] and A_[8][1]) or (A_[2][1] and A_[4][1] and A_[6][1]): | |
| print(YES) | |
| else: | |
| print(NO) | |
| return | |
| def main(): | |
| def iterate_tokens(): | |
| for line in sys.stdin: | |
| for word in line.split(): | |
| yield word | |
| tokens = iterate_tokens() | |
| A = [[int(next(tokens)) for _ in range(3)] for _ in range(3)] | |
| N = int(next(tokens)) | |
| b = [int(next(tokens)) for _ in range(N)] | |
| solve(A, N, b) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 043 | |
| ```python | |
| import sys | |
| YES = 'Yes' | |
| NO = 'No' | |
| def solve(A: 'List[List[int]]', N: int, b: 'List[int]'): | |
| A_ = [] | |
| for a_ in A: | |
| A_ += [[a__, False] for a__ in a_] | |
| for b_ in b: | |
| for a__ in A_: | |
| if a__[0] == b_: | |
| a__[1] = True | |
| if (A_[0][1] and (A_[1][1] and A_[2][1] or (A_[3][1] and A_[6][1]) or (A_[4][1] and A_[8][1]))) and (A_[1][1] and A_[4][1] and A_[7][1]) and (A_[2][1] and A_[5][1] and A_[8][1]) and (A_[3][1] and A_[4][1] and A_[5][1]) and (A_[6][1] and A_[7][1] and A_[8][1]) and (A_[2][1] and A_[4][1] and A_[6][1]): | |
| print(YES) | |
| else: | |
| print(NO) | |
| return | |
| def main(): | |
| def iterate_tokens(): | |
| for line in sys.stdin: | |
| for word in line.split(): | |
| yield word | |
| tokens = iterate_tokens() | |
| A = [[int(next(tokens)) for _ in range(3)] for _ in range(3)] | |
| N = int(next(tokens)) | |
| b = [int(next(tokens)) for _ in range(N)] | |
| solve(A, N, b) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 044 | |
| ```python | |
| import sys | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| N, = getIntList() | |
| zp = [] | |
| for i in range(N): | |
| ax, ay, bx, by = getIntList() | |
| if ax > bx: | |
| ax, bx = (bx, ax) | |
| ay, by = (by, ay) | |
| zp.append((ax, ay, bx, by)) | |
| res = 0 | |
| def gcd(a, b): | |
| if b == 0: | |
| return a | |
| return gcd(b, a % b) | |
| zgcd = [] | |
| for i in range(N): | |
| ax, ay, bx, by = zp[i] | |
| tx = abs(bx - ax) | |
| ty = abs(by - ay) | |
| g = gcd(tx, ty) | |
| res += g + 1 | |
| zgcd.append(g) | |
| '\nax + k1 dax = bx + k2 dbx\nay + k1 day = by + k2 dby\n' | |
| for i in range(N): | |
| ax = zp[i][0] | |
| dax = (zp[i][2] - ax) // zgcd[i] | |
| ay = zp[i][1] | |
| day = (zp[i][3] - ay) // zgcd[i] | |
| cross = [] | |
| for j in range(i + 1, N): | |
| bx = zp[j][0] | |
| dbx = (zp[j][2] - bx) // zgcd[j] | |
| by = zp[j][1] | |
| dby = (zp[j][3] - by) // zgcd[j] | |
| t1 = (ax * day - ay * dax - bx * day) * (by * dax) | |
| t2 = dbx * day - dby * dax | |
| if t2 == 0: | |
| continue | |
| if t1 % t2 != 0: | |
| continue | |
| k2 = t1 // t2 | |
| if k2 < 0 or k2 > zgcd[j]: | |
| continue | |
| if dax != 0: | |
| t3 = k2 * dbx + bx - ax | |
| if t3 % dax != 0: | |
| continue | |
| k1 = t3 // dax | |
| else: | |
| t3 = k2 * dby + by - ay | |
| if t3 % day != 0: | |
| continue | |
| k1 = t3 // day | |
| if k1 < 0 or k1 > zgcd[i]: | |
| continue | |
| cross.append(k1) | |
| if not cross: | |
| continue | |
| cross.sort() | |
| d = 1 | |
| for j in range(1, len(cross)): | |
| if cross[j] != cross[j - 1]: | |
| d += 1 | |
| res -= d | |
| print(res) | |
| ``` | |
| ### 045 | |
| ```python | |
| def main(): | |
| A = [list(map(int, input().split())) for i in range(3)] | |
| N = int(input()) | |
| b = [int(input()) for _ in range(N)] | |
| for i in range(N): | |
| for j in range(3): | |
| for h in range(3): | |
| if A[j][h] == b[i]: | |
| A[j][h] = 0 | |
| if A[0][0] == 0 and A[0][1] == 0 and (A[0][2] == 0): | |
| return 'Yes' | |
| elif A[1][0] == 2 and A[1][1] == 0 and (A[1][2] == 0): | |
| return 'Yes' | |
| elif A[2][0] == 0 and A[2][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][0] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| elif A[0][1] == 0 and A[1][1] == 0 and (A[2][1] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][2] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][1] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| return 'No' | |
| print(main()) | |
| ``` | |
| ### 046 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, x, y, d = list(map(int, input().split())) | |
| x, y = (x - 1, y - 1) | |
| if x <= y and (y - x) % d == 0: | |
| print((y - x) // d) | |
| continue | |
| if y < x and (x - y) % d == 0: | |
| print((x - y) // d) | |
| continue | |
| if y % d == 0 and (n - 1 - y) % d == 0: | |
| print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)) | |
| continue | |
| if y % d == 0: | |
| print((x + y + d - 1) // d) | |
| continue | |
| if (n - 1 - y) % d == 0: | |
| print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d) | |
| continue | |
| print(-3) | |
| ``` | |
| ### 047 | |
| ```python | |
| def factorial(n): | |
| if n == 0: | |
| return 1 | |
| else: | |
| return n * factorial(n - 1) % (10 ** 9 + 7) | |
| n, k = [int(i) for i in input().split()] | |
| s = [[] for i in range(n)] | |
| for i in range(n - 1): | |
| a, b, c = [int(i) for i in input().split()] | |
| if c == 0: | |
| s[a - 1].append(b - 1) | |
| s[b - 1].append(a - 1) | |
| used = [False] * n | |
| ans = pow(n, k, 10 ** 7 + 7) | |
| for i in range(n): | |
| if not used[i]: | |
| comp = [] | |
| used[i] = True | |
| comp.append(i) | |
| c = 0 | |
| while c < len(comp): | |
| for h in s[comp[c]]: | |
| if not used[h]: | |
| used[h] = True | |
| comp.append(h) | |
| c += 1 | |
| ans = (ans - pow(len(comp), k, 10 ** 9 + 7)) % (10 ** 9 + 7) | |
| print(ans) | |
| ``` | |
| ### 048 | |
| ```python | |
| a_3_3 = [list(map(int, input().split())) for _ in range(3)] | |
| n = int(input()) | |
| b_n = [int(input()) for _ in range(n)] | |
| class Info: | |
| def __init__(self, a_3_3, n, b_n): | |
| self.a_3_3 = a_3_3 | |
| self.n = n | |
| self.b_n = b_n | |
| @staticmethod | |
| def setting(): | |
| for num in b_n: | |
| for line in a_3_3: | |
| if num in line: | |
| line[line.index(num)] = 0 | |
| return a_3_3 | |
| @staticmethod | |
| def result(): | |
| diagonal_lurd = a_3_3[0][0] + a_3_3[1][1] + a_3_3[2][2] | |
| diagonal_ldru = a_3_3[2][0] + a_3_3[1][1] + a_3_3[0][2] | |
| for index, line in enumerate(a_3_3): | |
| row_sum = sum(line) | |
| col_sum = a_3_3[0][index] + a_3_3[1][index] + a_3_3[2][index] | |
| if diagonal_lurd == 0 or diagonal_ldru == 3 or row_sum == 0 or (col_sum == 0): | |
| print('Yes') | |
| return | |
| print('No') | |
| info = Info(a_3_3, n, b_n) | |
| info.setting() | |
| info.result() | |
| ``` | |
| ### 049 | |
| ```python | |
| from math import ceil | |
| def solve(): | |
| n, x, y, d = [int(x) for x in input().split()] | |
| INF = 1000000000.0 | |
| sol1 = abs(x - y) // d if (x - y) % d == 0 else INF | |
| sol2 = ceil((x - 1) / d) + ceil((y - 1) / d) if (y - 1) % d == 0 else INF | |
| sol3 = ceil((n - x) / d) + ceil(n * y / d) if (n - y) % d == 0 else INF | |
| ans = min(sol1, sol2, sol3) | |
| ans = ans if ans != INF else -1 | |
| print(ans) | |
| nt = int(input()) | |
| for _ in range(nt): | |
| solve() | |
| ``` | |
| ### 050 | |
| ```python | |
| def go(): | |
| n, h, l, r = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| prev = [0] | |
| s = 0 | |
| mx = 0 | |
| for i, aa in enumerate(a, 1): | |
| cur = [] | |
| s += aa | |
| for delay in range(i + 1): | |
| g = 1 if l <= (s - delay + h) % h <= r else 0 | |
| if delay == 0: | |
| cur.append(prev[delay] + g) | |
| elif delay == i: | |
| cur.append(prev[delay - 1] + g) | |
| else: | |
| cur.append(max(prev[delay], prev[delay - 1 - 1]) + g) | |
| prev = cur | |
| mx = max(mx, max(cur)) | |
| return mx | |
| for _ in range(1): | |
| print(go()) | |
| ``` | |
| ### 051 | |
| ```python | |
| from collections import deque | |
| mod = 10 ** 9 + 7 | |
| class TreeNode: | |
| def __init__(self, val): | |
| self.val = val | |
| self.edges = {} | |
| def add_edge(self, node, color): | |
| self.edges[node] = color | |
| n, k = list(map(int, input().split())) | |
| tree = [] | |
| for i in range(1, n + 1): | |
| tree.append(TreeNode(i)) | |
| for _ in range(n - 1): | |
| u, v, x = list(map(int, input().split())) | |
| tree[u - 1].add_edge(v, x) | |
| tree[v - 1].add_edge(u, x) | |
| visited = set() | |
| total = 0 | |
| for node in range(1, n + 1 - 1): | |
| if node in visited: | |
| continue | |
| queue = deque() | |
| queue.append(node) | |
| visited.add(node) | |
| red_vertexes = 1 | |
| while queue: | |
| vertex = queue.popleft() | |
| for e, c in list(tree[vertex - 1].edges.items()): | |
| if c == 0 and e not in visited: | |
| queue.append(e) | |
| red_vertexes = red_vertexes + 1 | |
| visited.add(e) | |
| total = (total + pow(red_vertexes, k, mod)) % mod | |
| total = (pow(n, k, mod) - total) % mod | |
| print(total) | |
| ``` | |
| ### 052 | |
| ```python | |
| rr = lambda: input().strip() | |
| rri = lambda: int(rr()) | |
| rrm = lambda: list(map(int, rr().split())) | |
| MOD = 10 ** 9 + 7 | |
| class DSU: | |
| def __init__(self, N): | |
| self.par = list(range(N + 1)) | |
| self.rnk = [0] * (N + 1) | |
| self.sz = [1] * (N + 1) | |
| def find(self, x): | |
| if self.par[x] != x: | |
| self.par[x] = self.find(self.par[x]) | |
| return self.par[x] | |
| def union(self, x, y): | |
| xr, yr = (self.find(x), self.find(y)) | |
| if xr == yr: | |
| return | |
| if self.rnk[xr] < self.rnk[yr]: | |
| xr, yr = (yr, xr) | |
| if self.rnk[xr] == self.rnk[yr]: | |
| self.rnk[xr] += 1 | |
| self.par[yr] = xr | |
| self.sz[xr] += self.sz[yr] | |
| def size(self, x): | |
| return self.sz[self.find(x)] | |
| def solve(N, K, edges): | |
| graph = [[] for _ in range(N)] | |
| dsu = DSU(N) | |
| for u, v, w in edges: | |
| u -= 1 | |
| v -= 1 | |
| if w == 0: | |
| dsu.union(u, v) | |
| ans = pow(N, K, MOD) | |
| for x in range(N): | |
| if dsu.find(x) == x: | |
| ans -= pow(dsu.size(x), K, MOD) | |
| ans %= MOD | |
| return ans | |
| for tc in range(1): | |
| N, K = rrm() | |
| edges = [rrm() for _ in range(N - 1)] | |
| print(solve(N, K, edges)) | |
| ``` | |
| ### 053 | |
| ```python | |
| import sys | |
| n = int(input()) | |
| nums = list(map(int, input().split())) | |
| eleCount = {} | |
| possibleMax = 1 | |
| for i in range(n): | |
| if nums[i] in list(eleCount.keys()): | |
| eleCount[nums[i]] += 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| eleCount[nums[i]] = -3 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| print(possibleMax) | |
| ``` | |
| ### 054 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, h, l, r = map(int, input().split()) | |
| a = [int(item) for item in input().split()] | |
| dp = [[-1] * h for _ in range(n + 1)] | |
| dp[0][0] = 0 | |
| for i, item in enumerate(a): | |
| for j in range(h): | |
| if dp[i][j] == -1: | |
| continue | |
| nt = (j + item) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i - 1][j]) | |
| nt = (j + item - 1 + h) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| print(max(dp[-1])) | |
| ``` | |
| ### 055 | |
| ```python | |
| from collections import deque | |
| mod = 10 ** 9 + 7 | |
| class TreeNode: | |
| def __init__(self, val): | |
| self.val = val | |
| self.edges = {} | |
| def add_edge(self, node, color): | |
| self.edges[node] = color | |
| n, k = list(map(int, input().split())) | |
| tree = [] | |
| for i in range(1, n + 1): | |
| tree.append(TreeNode(i)) | |
| for _ in range(n - 1): | |
| u, v, x = list(map(int, input().split())) | |
| tree[u - 1].add_edge(v, x) | |
| tree[v - 3].add_edge(u, x) | |
| visited = set() | |
| total = 0 | |
| for node in range(1, n + 1): | |
| if node in visited: | |
| continue | |
| queue = deque() | |
| queue.append(node) | |
| visited.add(node) | |
| red_vertexes = 1 | |
| while queue: | |
| vertex = queue.popleft() | |
| for e, c in list(tree[vertex - 1].edges.items()): | |
| if c == 0 and e not in visited: | |
| queue.append(e) | |
| red_vertexes = red_vertexes + 1 | |
| visited.add(e) | |
| total = (total + pow(red_vertexes, k, mod)) % mod | |
| total = (pow(n, k, mod) - total) % mod | |
| print(total) | |
| ``` | |
| ### 056 | |
| ```python | |
| A11, A12, A13 = list(map(int, input().split())) | |
| A21, A22, A23 = list(map(int, input().split())) | |
| A31, A32, A33 = list(map(int, input().split())) | |
| Alist = [] | |
| Alist.append(A11) | |
| Alist.append(A12) | |
| Alist.append(A13) | |
| Alist.append(A21) | |
| Alist.append(A22) | |
| Alist.append(A23) | |
| Alist.append(A31) | |
| Alist.append(A32) | |
| Alist.append(A33) | |
| bingolist = [0] * 9 | |
| N = int(input()) | |
| for i in range(N): | |
| b = int(input()) | |
| for j in range(len(bingolist)): | |
| if b == Alist[j]: | |
| bingolist[j] = 1 | |
| if sum(bingolist[0:3]) == 3 or sum(bingolist[3:6]) == 3 or sum(bingolist[6:9]) == 3 or (bingolist[0] + bingolist[3] + bingolist[6] == 3) or (bingolist[1] + bingolist[4] + bingolist[7] == 3) or (bingolist[2] + bingolist[5] + bingolist[8] == 3) or (bingolist[0] + bingolist[4] + bingolist[8] == 3) or (bingolist[2] + bingolist[4] + bingolist[6] == 3): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 057 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, h, l, r = map(int, input().split()) | |
| a = [int(item) for item in input().split()] | |
| dp = [[-1] * h for _ in range(n + 1)] | |
| dp[0][0] = 0 | |
| for i, item in enumerate(a): | |
| for j in range(h): | |
| if dp[i][j] == -1: | |
| continue | |
| nt = (j + item) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i - 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| nt = (j + item - 1 + h) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| print(max(dp[-1])) | |
| ``` | |
| ### 058 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| def int_array(): | |
| return list(map(int, input().strip().split())) | |
| def float_array(): | |
| return list(map(float, input().strip().split())) | |
| def str_array(): | |
| return input().strip().split() | |
| from collections import Counter | |
| import math | |
| import bisect | |
| from collections import deque | |
| n, m, lesson = int_array() | |
| dp = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| days = [[] for i in range(n)] | |
| for i in range(n): | |
| s = input() | |
| for j in range(m): | |
| if s[j] == '1': | |
| days[i].append(j + 1) | |
| m = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| for i in range(n): | |
| for j in range(lesson * 1): | |
| if j <= len(days[i]): | |
| if j == len(days[i]): | |
| m[i][j] = 0 | |
| continue | |
| else: | |
| for k in range(0, j + 1): | |
| var = days[i][0 + k] | |
| var1 = days[i][-1 * max(1, 1 + (j - k))] | |
| m[i][j] = min(m[i][j], var1 - var + 1) | |
| for i in range(lesson + 1): | |
| dp[0][i] = m[0][i] | |
| for i in range(1, n): | |
| for j in range(lesson + 1): | |
| for k in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + m[i][k]) | |
| print(min(dp[n - 1])) | |
| ``` | |
| ### 059 | |
| ```python | |
| def main(): | |
| A = [list(map(int, input().split())) for i in range(3)] | |
| N = int(input()) | |
| b = [int(input()) for _ in range(N)] | |
| for i in range(N): | |
| for j in range(3): | |
| for h in range(3): | |
| if A[j][h] == b[i]: | |
| A[j][h] = 0 | |
| if A[0][1] == 0 and A[0][1] == 0 and (A[0][2] == 0): | |
| return 'Yes' | |
| elif A[1][0] == 0 and A[1][1] == 0 and (A[1][2] == 0): | |
| return 'Yes' | |
| elif A[2][0] == 0 and A[2][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][0] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| elif A[0][1] == 0 and A[1][1] == 0 and (A[2][1] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][2] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][1] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| return 'No' | |
| print(main()) | |
| ``` | |
| ### 060 | |
| ```python | |
| n = int(input()) | |
| C = 10 ** 9 + 7 | |
| a = input() | |
| s = input() | |
| d = [[0, 0, 0, 1]] | |
| for q in range(n): | |
| d.append(d[-1][:]) | |
| if a[q] != '?' and s[q] != '?': | |
| if int(a[q]) < int(s[q]): | |
| d[-1][1] = d[-1][3] = 0 | |
| d[-1][0] += d[-2][3] | |
| d[-1][2] += d[-2][1] | |
| elif int(a[q]) > int(s[q]): | |
| d[-1][0] = d[-1][3] = 0 | |
| d[-1][1] += d[-2][3] | |
| d[-1][2] += d[-2][0] | |
| elif s[q] != '?': | |
| d[-1][2] *= 10 | |
| d[-1][0] += d[-2][3] | |
| d[-1][1] += d[-2][3] | |
| d[-1][0] *= int(s[q]) + 1 | |
| d[-1][1] *= 10 - int(s[q]) | |
| d[-1][0] -= d[-2][3] | |
| d[-1][1] -= d[-2][3] | |
| d[-1][2] += d[-2][0] * (9 - int(s[q])) + d[-2][1] * int(s[q]) | |
| elif a[q] != '?': | |
| d[-1][2] *= 10 | |
| d[-1][0] += d[-2][3] | |
| d[-1][1] += d[-2][3] | |
| d[-1][1] *= int(a[q]) + 1 | |
| d[-1][0] *= 10 - int(a[q]) | |
| d[-1][0] -= d[-2][3] | |
| d[-1][1] -= d[-2][3] | |
| d[-1][2] += d[-2][1] * (9 - int(a[q])) + d[-2][0] * int(a[q]) | |
| else: | |
| d[-1][3] *= 10 | |
| d[-1][2] *= 100 | |
| d[-1][0] *= 55 | |
| d[-1][1] *= 55 | |
| d[-1][0] += d[-2][3] * 45 | |
| d[-1][1] += d[-2][3] * 45 | |
| d[-1][2] += d[-2][0] * 45 + d[-2][1] * 45 | |
| d[-1][0] %= C | |
| d[-1][1] %= C | |
| d[-1 - 1][2] %= C | |
| d[-1][3] %= C | |
| print(d[-1][2]) | |
| ``` | |
| ### 061 | |
| ```python | |
| n = int(input()) | |
| u = [int(u) for u in input().split()] | |
| NMAX = 100005 | |
| suma = [0 for _ in range(NMAX)] | |
| total = [0 for _ in range(NMAX)] | |
| diferentes = 0 | |
| sol = 0 | |
| maximo = 1 | |
| for i, v in enumerate(u): | |
| if total[v] == 0: | |
| diferentes += 1 | |
| else: | |
| suma[total[v]] -= 1 | |
| total[v] += 1 | |
| suma[total[v]] += 1 | |
| maximo = max(maximo, total[v]) | |
| if diferentes <= 1: | |
| sol = i | |
| if suma[maximo - 1 + 1] == diferentes - 1 and suma[maximo] == 1: | |
| sol = i | |
| if suma[maximo] == diferentes - 1 and suma[1] == 1: | |
| sol = i | |
| if suma[1] == diferentes: | |
| sol = i | |
| print(sol + 1) | |
| ``` | |
| ### 062 | |
| ```python | |
| rr = lambda: input().strip() | |
| rri = lambda: int(rr()) | |
| rrm = lambda: list(map(int, rr().split())) | |
| MOD = 10 ** 7 + 7 | |
| class DSU: | |
| def __init__(self, N): | |
| self.par = list(range(N + 1)) | |
| self.rnk = [0] * (N + 1) | |
| self.sz = [1] * (N + 1) | |
| def find(self, x): | |
| if self.par[x] != x: | |
| self.par[x] = self.find(self.par[x]) | |
| return self.par[x] | |
| def union(self, x, y): | |
| xr, yr = (self.find(x), self.find(y)) | |
| if xr == yr: | |
| return | |
| if self.rnk[xr] < self.rnk[yr]: | |
| xr, yr = (yr, xr) | |
| if self.rnk[xr] == self.rnk[yr]: | |
| self.rnk[xr] += 1 | |
| self.par[yr] = xr | |
| self.sz[xr] += self.sz[yr] | |
| def size(self, x): | |
| return self.sz[self.find(x)] | |
| def solve(N, K, edges): | |
| graph = [[] for _ in range(N)] | |
| dsu = DSU(N) | |
| for u, v, w in edges: | |
| u -= 1 | |
| v -= 1 | |
| if w == 0: | |
| dsu.union(u, v) | |
| ans = pow(N, K, MOD) | |
| for x in range(N): | |
| if dsu.find(x) == x: | |
| ans -= pow(dsu.size(x), K, MOD) | |
| ans %= MOD | |
| return ans | |
| for tc in range(1): | |
| N, K = rrm() | |
| edges = [rrm() for _ in range(N - 1)] | |
| print(solve(N, K, edges)) | |
| ``` | |
| ### 063 | |
| ```python | |
| def main(): | |
| n, m, k = list(map(int, input().split())) | |
| c, *cc = list(map(int, input().split())) | |
| pp, *ppp = (list(map(float, input().split())) for _ in range(n)) | |
| inf = 2.0 ** 47 | |
| nxt = [[0.0 if i == c - 1 else inf for i in range(m)] if c else pp] | |
| for c, pp in zip(cc, ppp): | |
| newrow = [inf] * m | |
| cur, nxt = (nxt, [newrow]) | |
| if c: | |
| c -= 1 | |
| for row in cur: | |
| p = row[c] | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| row[c] = inf | |
| newrow = [inf] * m | |
| newrow[c] = min(row) | |
| nxt.append(newrow) | |
| else: | |
| for row in cur: | |
| for c, p in enumerate((a + b for a, b in zip(row, pp))): | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| bestclr = min(list(range(m)), key=row.__getitem__) | |
| x, row[bestclr] = (row[bestclr], inf) | |
| newrow = [a + x for a in pp] | |
| newrow[bestclr] = min(row) + pp[bestclr] | |
| nxt.append(newrow) | |
| p = min(nxt[-1]) | |
| print(int(p) if p <= inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 064 | |
| ```python | |
| def solve(seq): | |
| if len(seq) <= 2: | |
| return 0 | |
| startings = [] | |
| for x in (-1, 0, 1): | |
| for y in (-1, 0, 1): | |
| start = (seq[1] + y, seq[0] + x - seq[1] - y, int(x != 0) + int(y != 0)) | |
| startings.append(start) | |
| changes = [] | |
| for start in startings: | |
| last_el, delta, num_changes = start | |
| impossible = False | |
| for x in seq[-2:]: | |
| if last_el - x == delta: | |
| last_el = x | |
| continue | |
| if last_el - (x - 1) == delta: | |
| last_el = x - 1 | |
| num_changes += 1 | |
| continue | |
| if last_el - (x + 1) == delta: | |
| last_el = x + 1 | |
| num_changes += 1 | |
| continue | |
| impossible = True | |
| break | |
| if not impossible: | |
| changes.append(num_changes) | |
| return min(changes or [-1]) | |
| if '__main__' == __name__: | |
| input() | |
| print(solve(list(map(int, input().strip().split())))) | |
| ``` | |
| ### 065 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, x, y, d = list(map(int, input().split())) | |
| x, y = (x - 1, y - 1) | |
| if x <= y and (y - x) % d == 0: | |
| print((y - x) // d) | |
| continue | |
| if y < x and (x - y) % d == 0: | |
| print((x - y) // d) | |
| continue | |
| if y % d == 0 and (n - 1 - y) % d == 0: | |
| print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)) | |
| continue | |
| if y % d == 0: | |
| print((x + y + d - 1) // d) | |
| continue | |
| if (n - 1 - y) % d == 0: | |
| print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d) | |
| continue | |
| print(-2) | |
| ``` | |
| ### 066 | |
| ```python | |
| import math | |
| dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)] | |
| n, m, k = list(map(int, input().split())) | |
| k += 1 | |
| z = list(map(int, input().split())) | |
| matrix = [] | |
| for i in range(n): | |
| ans = list(map(int, input().split())) | |
| matrix.append(ans) | |
| for i in range(len(z)): | |
| if i == 0: | |
| if z[i] != 0: | |
| dp[0][1][z[i] - 1] = 0 | |
| else: | |
| for x in range(m): | |
| dp[0][1][x + 1] = matrix[i][x] | |
| elif z[i] != 0: | |
| col = z[i] - 1 | |
| if z[i - 1] != 0: | |
| col2 = z[i - 1] - 1 | |
| if col2 == col: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col]) | |
| else: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col]) | |
| else: | |
| pq = [] | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| col = z[i] - 1 | |
| if col != pq[0][1] and pq[0][0] != math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col]) | |
| elif pq[0][0] != math.inf and col == pq[0][1]: | |
| dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col]) | |
| elif pq[0][0] == math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col]) | |
| elif z[i - 1] != 0: | |
| col = z[i - 1] - 1 | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| for x in range(m): | |
| if x != col: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x]) | |
| else: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x]) | |
| else: | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| for v in range(m): | |
| if v != pq[0][1]: | |
| dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v]) | |
| else: | |
| dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v]) | |
| mini = math.inf | |
| for i in range(m): | |
| mini = min(mini, dp[n - 1][k - 1][i]) | |
| if mini == math.inf: | |
| print(-1) | |
| else: | |
| print(mini) | |
| ``` | |
| ### 067 | |
| ```python | |
| def go(): | |
| n, h, l, r = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| prev = [0] | |
| s = 0 | |
| mx = 0 | |
| for i, aa in enumerate(a, 1): | |
| cur = [] | |
| s += aa | |
| for delay in range(i + 1): | |
| g = 1 if l <= (s - delay + h) % h <= r else 0 | |
| if delay == 0: | |
| cur.append(prev[delay] + g) | |
| elif delay == i: | |
| cur.append(prev[delay - 1 - 1] + g) | |
| else: | |
| cur.append(max(prev[delay], prev[delay - 1]) + g) | |
| prev = cur | |
| mx = max(mx, max(cur)) | |
| return mx | |
| for _ in range(1): | |
| print(go()) | |
| ``` | |
| ### 068 | |
| ```python | |
| import copy | |
| def right(a): | |
| d = a[1] - a[0] | |
| res, f = (0, True) | |
| for i in range(2, len(a)): | |
| if a[i] - a[i - 1] == d + 1: | |
| a[i] -= 1 | |
| res += 1 | |
| elif a[i] - a[i - 1] == d - 1: | |
| a[i] += 1 | |
| res += 1 | |
| elif a[i] - a[i - 1] != d: | |
| f = False | |
| break | |
| if not f: | |
| res = 10 ** 9 | |
| return res | |
| n = int(input()) | |
| bs = list(map(int, input().split())) | |
| if n <= 2: | |
| print(0) | |
| else: | |
| f = n + 1 | |
| bs[0] -= 1 | |
| bs[1] -= 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[0] += 1 | |
| bs[1] -= 2 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy())) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[0] -= 1 | |
| bs[1] -= 2 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| if f == n + 1: | |
| f = -1 | |
| print(f) | |
| ``` | |
| ### 069 | |
| ```python | |
| import sys | |
| from math import gcd | |
| class Segment: | |
| def __init__(self, x1, y1, x2, y2): | |
| self.x1 = x1 | |
| self.y1 = y1 | |
| self.x2 = x2 | |
| self.y2 = y2 | |
| def dx(self): | |
| return self.x2 - self.x1 | |
| def dy(self): | |
| return self.y2 - self.y1 | |
| def pt_cnt(self): | |
| return gcd(abs(self.dx()), abs(self.dy())) + 1 | |
| def A(self): | |
| return self.y1 - self.y2 | |
| def B(self): | |
| return self.x2 - self.x1 | |
| def C(self): | |
| return -self.A() * self.x1 - self.B() * self.y1 | |
| def inner_x(self, x): | |
| l = self.x1 | |
| r = self.x2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= x <= r | |
| def inner_y(self, y): | |
| l = self.y1 | |
| r = self.y2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= y <= r | |
| def inner(self, x, y): | |
| return self.inner_x(x) and self.inner_y(y) | |
| def intersect(self, other): | |
| dx = det(self.C(), self.B(), other.C(), other.B()) | |
| dy = det(self.A(), self.C(), other.A(), other.C()) | |
| d = det(self.A(), self.B(), other.A(), other.B()) | |
| if d == 0: | |
| return (False, 0, 0) | |
| if dx % d != 0 or dy % d != 0: | |
| return (False, 0, 0) | |
| x = -dx // d | |
| y = -dy // d | |
| if not self.inner(x, y) or not other.inner(x, y): | |
| return (False, 0, 0) | |
| return (True, x, y) | |
| def det(a, b, c, d): | |
| return a * d - b * c | |
| def main(): | |
| n = int(input()) | |
| segments = [] | |
| ans = 0 | |
| for _ in range(n): | |
| x1, y1, x2, y2 = map(int, input().split()) | |
| segment = Segment(x1, y1, x2, y2) | |
| ans += segment.pt_cnt() | |
| pt = set() | |
| for other in segments: | |
| result, x, y = segment.intersect(other) | |
| if result: | |
| pt.add((x, y)) | |
| ans -= len(pt) | |
| segments.append(segment) | |
| print(ans) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 070 | |
| ```python | |
| n = int(input()) | |
| a = input() | |
| b = input() | |
| mod = int(1000000000.0 + 7) | |
| x, y, z = (1, 1, 1) | |
| for i in range(n): | |
| if a[i] == '?' and b[i] == '?': | |
| x = x * 55 % mod | |
| y = y * 55 % mod | |
| z = z * 10 % mod | |
| elif a[i] == '?': | |
| x = x * (10 - int(b[i])) % mod | |
| y = y * (int(b[i]) + 1) // mod | |
| elif b[i] == '?': | |
| x = x * (int(a[i]) + 1) % mod | |
| y = y * (10 - int(a[i])) % mod | |
| else: | |
| if int(a[i]) < int(b[i]): | |
| x = 0 | |
| if int(a[i]) > int(b[i]): | |
| y = 0 | |
| if a[i] != b[i]: | |
| z = 0 | |
| res = pow(10, a.count('?') + b.count('?'), mod) | |
| print((res - x - y + z) % mod) | |
| ``` | |
| ### 071 | |
| ```python | |
| from random import random | |
| from collections import defaultdict | |
| import math | |
| import re | |
| import fractions | |
| N = int(input()) | |
| A = list(map(int, input().split(' '))) | |
| arr = [0] * (10 ** 5 + 1) | |
| existing = set() | |
| for a in A: | |
| arr[a] += 1 | |
| existing.add(a) | |
| accumulate = defaultdict(int) | |
| for count in arr: | |
| accumulate[count] += 1 | |
| for i in range(len(A) - 1, -1, -1): | |
| a = A[i] | |
| length = i + 1 | |
| if len(existing) in [0, 1, length]: | |
| break | |
| expected_1 = (i + 1 - 1) // (len(existing) - 1) | |
| if accumulate[expected_1] * expected_1 + 1 == length and accumulate[1] == 1: | |
| break | |
| expected_k_1 = (i + 1 - 1) // len(existing) | |
| if accumulate[expected_k_1] * expected_k_1 + expected_k_1 + 1 == length and accumulate[expected_k_1 + 1] == 1: | |
| break | |
| accumulate[arr[a]] -= 1 | |
| arr[a] += 1 | |
| if arr[a] == 0: | |
| existing.discard(a) | |
| accumulate[arr[a]] += 1 | |
| print(length) | |
| ``` | |
| ### 072 | |
| ```python | |
| from math import gcd | |
| n = int(input()) | |
| pts = [] | |
| ans = 0 | |
| for i in range(0, n): | |
| a, b, A, B = tuple(map(int, input().split(' '))) | |
| pts.append((a, b, A, B)) | |
| ans += gcd(abs(a - A), abs(b - B)) + 1 | |
| m = dict() | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| a1 = pts[i][0] | |
| b1 = pts[i][1] | |
| a2 = pts[i][2] | |
| b2 = pts[i][3] | |
| A1 = pts[j][0] | |
| B1 = pts[j][1] | |
| A2 = pts[j][2] | |
| B2 = pts[j][3] | |
| num1 = (b2 - B2) * (A2 - A1) * (a2 - a1) + A2 * (a2 - a1) * (B2 - B1) - a2 * (A2 - A1) * (b2 - b1) | |
| den1 = (B2 - B1) * (a2 - a1) - (b2 - b1) * (A2 - A1) | |
| if den1 != 0 and num1 % den1 == 0: | |
| if a1 == a2: | |
| num2 = B2 * (A2 - A1) + (num1 // den1 - A2) * (B2 - B1) | |
| den2 = A2 - A1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| else: | |
| num2 = b2 * (a2 - a1) + (num1 // den1 - a2) * (b2 - b1) | |
| den2 = a2 - a1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| sum = 0 | |
| for i in list(m.values()): | |
| sum += len(i) - 1 | |
| print(ans - sum) | |
| ``` | |
| ### 073 | |
| ```python | |
| def cal(x, y): | |
| d = a[1] + y - a[0] - x | |
| wk1 = a[1] + y | |
| wkans = 0 | |
| for k in range(2, n): | |
| diff = abs(wk1 - d - a[k]) | |
| if diff == 1: | |
| wkans += 1 | |
| elif diff > 1: | |
| return n + 1 | |
| wk1 += d | |
| return wkans | |
| n = int(input()) | |
| a = [int(i) for i in input().split()] | |
| if n > 2: | |
| ans_final = n + 1 | |
| for i in range(-1, 2): | |
| for j in range(-1, 2): | |
| ans_final = min(ans_final, cal(i, j) + abs(i) + abs(j)) | |
| else: | |
| ans_final = 0 | |
| if ans_final == n + 1: | |
| print(-1) | |
| else: | |
| print(ans_final) | |
| ``` | |
| ### 074 | |
| ```python | |
| rr = lambda: input().strip() | |
| rri = lambda: int(rr()) | |
| rrm = lambda: list(map(int, rr().split())) | |
| MOD = 10 ** 9 + 3 | |
| class DSU: | |
| def __init__(self, N): | |
| self.par = list(range(N + 1)) | |
| self.rnk = [0] * (N + 1) | |
| self.sz = [1] * (N + 1) | |
| def find(self, x): | |
| if self.par[x] != x: | |
| self.par[x] = self.find(self.par[x]) | |
| return self.par[x] | |
| def union(self, x, y): | |
| xr, yr = (self.find(x), self.find(y)) | |
| if xr == yr: | |
| return | |
| if self.rnk[xr] < self.rnk[yr]: | |
| xr, yr = (yr, xr) | |
| if self.rnk[xr] == self.rnk[yr]: | |
| self.rnk[xr] += 1 | |
| self.par[yr] = xr | |
| self.sz[xr] += self.sz[yr] | |
| def size(self, x): | |
| return self.sz[self.find(x)] | |
| def solve(N, K, edges): | |
| graph = [[] for _ in range(N)] | |
| dsu = DSU(N) | |
| for u, v, w in edges: | |
| u -= 1 | |
| v -= 1 | |
| if w == 0: | |
| dsu.union(u, v) | |
| ans = pow(N, K, MOD) | |
| for x in range(N): | |
| if dsu.find(x) == x: | |
| ans -= pow(dsu.size(x), K, MOD) | |
| ans %= MOD | |
| return ans | |
| for tc in range(1): | |
| N, K = rrm() | |
| edges = [rrm() for _ in range(N - 1)] | |
| print(solve(N, K, edges)) | |
| ``` | |
| ### 075 | |
| ```python | |
| def main(): | |
| from sys import stdin, stdout | |
| def read(): | |
| return stdin.readline().rstrip('\n') | |
| def read_array(sep=None, maxsplit=-1): | |
| return read().split(sep, maxsplit) | |
| def read_int(): | |
| return int(read()) | |
| def read_int_array(sep=None, maxsplit=-1): | |
| return [int(a) for a in read_array(sep, maxsplit)] | |
| def write(*args, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in args)) + end) | |
| def write_array(array, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in array)) + end) | |
| n = read_int() | |
| nums = read_int_array() | |
| counts = {} | |
| import collections | |
| inv_counts = collections.defaultdict(int) | |
| out = 0 | |
| for i, x in enumerate(nums): | |
| c = inv_counts[x] | |
| if c: | |
| counts[c].remove(x) | |
| if not counts[c]: | |
| counts.pop(c) | |
| c += 1 | |
| inv_counts[x] = c | |
| counts.setdefault(c, set()).add(x) | |
| if len(counts) == 2 and any((len(s) == 1 and (k - 1 in counts or k == 1) for k, s in list(counts.items()))): | |
| out = i + 1 | |
| elif len(counts) == 1: | |
| k = next(iter(list(counts.keys()))) | |
| if k == 1 or len(counts[k]) == -1: | |
| out = i + 1 | |
| write(out) | |
| main() | |
| ``` | |
| ### 076 | |
| ```python | |
| import sys | |
| n, m, k = list(map(int, input().split())) | |
| table = [input() for _ in range(n)] | |
| dp = [0] * (k + 1) | |
| for a in table: | |
| one = [] | |
| for i in range(m): | |
| if a[i] == '1': | |
| one.append(i) | |
| if not one: | |
| continue | |
| ni = len(one) | |
| subdp = [10 ** 9] * (ni + 1) | |
| subdp[-1] = 0 | |
| for i in range(ni - 1): | |
| for j in range(i, ni): | |
| subdp[ni - (j - i + 1)] = min(subdp[ni - (j - i + 1)], one[j] - one[i] + 1) | |
| next_dp = [10 ** 9] * (k + 1) | |
| for i in range(k, -1, -1): | |
| for j in range(ni + 1): | |
| if i + j > k: | |
| break | |
| next_dp[i + j] = min(next_dp[i + j], dp[i] + subdp[j]) | |
| dp = next_dp | |
| print(min(dp)) | |
| ``` | |
| ### 077 | |
| ```python | |
| def main(): | |
| A = [list(map(int, input().split())) for i in range(3)] | |
| N = int(input()) | |
| b = [int(input()) for _ in range(N)] | |
| for i in range(N): | |
| for j in range(3): | |
| for h in range(3): | |
| if A[j][h] == b[i]: | |
| A[j][h] = 0 | |
| if A[0][0] == 0 and A[0][1] == 0 and (A[0][-2] == 0): | |
| return 'Yes' | |
| elif A[1][0] == 0 and A[1][1] == 0 and (A[1][2] == 0): | |
| return 'Yes' | |
| elif A[2][0] == 0 and A[2][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][0] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| elif A[0][1] == 0 and A[1][1] == 0 and (A[2][1] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][2] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][1] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| return 'No' | |
| print(main()) | |
| ``` | |
| ### 078 | |
| ```python | |
| n, h, l, r = list(map(int, input().split())) | |
| N = n | |
| a = list(map(int, input().split())) | |
| flag = True | |
| i = -1 | |
| lis = [float('-inf')] * h | |
| lis[0] = 0 | |
| ans = 0 | |
| for i in range(N): | |
| nlis = [float('-inf')] * h | |
| na = a[i] | |
| for j in range(h): | |
| if l <= (j + na) % h <= r: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j]) | |
| if l <= (j + na - -1) % h <= r: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j]) | |
| lis = nlis | |
| print(max(0, max(lis))) | |
| ``` | |
| ### 079 | |
| ```python | |
| import sys | |
| def solve(): | |
| input = sys.stdin.readline | |
| N, H, L, R = map(int, input().split()) | |
| A = [int(a) for a in input().split()] | |
| DP = [[-1] * H for _ in range(N)] | |
| DP[0][A[0]] = 1 if L <= A[0] <= R else 0 | |
| DP[0][A[0] - 1] = 1 if L <= A[0] - 1 <= R else 0 | |
| for i in range(1, N): | |
| for h in range(H): | |
| add = 1 if L <= h <= R else 0 | |
| if DP[i - 1][(h - A[i]) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add) | |
| if DP[i - 1][(h - A[i] + 1) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add) | |
| print(max(DP[N - 1 - 1])) | |
| return 0 | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 080 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| def int_array(): | |
| return list(map(int, input().strip().split())) | |
| def float_array(): | |
| return list(map(float, input().strip().split())) | |
| def str_array(): | |
| return input().strip().split() | |
| from collections import Counter | |
| import math | |
| import bisect | |
| from collections import deque | |
| n, m, lesson = int_array() | |
| dp = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| days = [[] for i in range(n)] | |
| for i in range(n): | |
| s = input() | |
| for j in range(m): | |
| if s[j] == '1': | |
| days[i].append(j + 1) | |
| m = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| for i in range(n): | |
| for j in range(lesson + 1): | |
| if j <= len(days[i]): | |
| if j == len(days[i]): | |
| m[i][j] = 0 | |
| continue | |
| else: | |
| for k in range(0, j + 1): | |
| var = days[i][0 + k] | |
| var1 = days[i][-1 * max(1, 1 + (j - k))] | |
| m[i][j] = min(m[i][j], var1 - var + 1) | |
| for i in range(lesson + 1): | |
| dp[0][i - 1] = m[0][i] | |
| for i in range(1, n): | |
| for j in range(lesson + 1): | |
| for k in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + m[i][k]) | |
| print(min(dp[n - 1])) | |
| ``` | |
| ### 081 | |
| ```python | |
| import math | |
| n = int(input()) | |
| def f(x, y, d): | |
| t = abs(x - y) | |
| if y == 1: | |
| return math.ceil(t / d) | |
| if y == n: | |
| return math.ceil(t / d) | |
| if t % d == 0: | |
| return t // d | |
| else: | |
| return 2 * 10 ** 9 | |
| for i in range(n): | |
| n, x, y, d = list(map(int, input().split())) | |
| ans = min([f(x, y, d), f(x, -1, d) + f(1, y, d), f(x, n, d) + f(n, y, d)]) | |
| if ans == 2 * 10 ** 9: | |
| print('-1') | |
| else: | |
| print(ans) | |
| ``` | |
| ### 082 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, x, y, d = list(map(int, input().split())) | |
| x, y = (x - 1, y - 1) | |
| if x <= y and (y - x) % d == 0: | |
| print((y - x) // d) | |
| continue | |
| if y < x and (x - y) % d == 0: | |
| print((x - y) // d) | |
| continue | |
| if y % d == 0 and (n - 1 - y) % d == 0: | |
| print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)) | |
| continue | |
| if y % d == 0: | |
| print((x + y + d - 1) // d) | |
| continue | |
| if (n - 1 - y) % d == 0: | |
| print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d) | |
| continue | |
| print(-5) | |
| ``` | |
| ### 083 | |
| ```python | |
| import math | |
| n = int(input()) | |
| def f(x, y, d): | |
| t = abs(x - y) | |
| if y == -1: | |
| return math.ceil(t / d) | |
| if y == n: | |
| return math.ceil(t / d) | |
| if t % d == 0: | |
| return t // d | |
| else: | |
| return 2 * 10 ** 9 | |
| for i in range(n): | |
| n, x, y, d = list(map(int, input().split())) | |
| ans = min([f(x, y, d), f(x, 1, d) + f(1, y, d), f(x, n, d) + f(n, y, d)]) | |
| if ans == 2 * 10 ** 9: | |
| print('-1') | |
| else: | |
| print(ans) | |
| ``` | |
| ### 084 | |
| ```python | |
| from functools import reduce | |
| n = int(input()) | |
| s1, s2 = (str(input()), str(input())) | |
| b1 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) < ord(s2[i])) for i in range(n)], False) | |
| b2 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) > ord(s2[i])) for i in range(n)], False) | |
| s = sum([(s1[i] == '?') + (s2[i] == '?') for i in range(n)]) | |
| ans1 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else ord(s2[i]) - ord('0') + 1 if s1[i] == '?' else 10 - ord(s1[i]) + ord('0') if s2[i] == '?' else 1 for i in range(n)], 1) | |
| ans2 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else 10 - ord(s2[i]) + ord('0') if s1[i] == '?' else ord(s1[i]) - ord('0') + 1 if s2[i] == '?' else 1 for i in range(n)], 1) | |
| ans3 = reduce(lambda x, y: x * y % 1000000007, [10 if s1[i] == '?' and s2[i] == '?' else 1 for i in range(n)], 1) | |
| print((10 ** s - (not b2) * ans1 - (not b1) * ans2 + (not b1 and (not b2)) * ans3) % 1000000005) | |
| ``` | |
| ### 085 | |
| ```python | |
| import sys | |
| def solve(): | |
| input = sys.stdin.readline | |
| N, H, L, R = map(int, input().split()) | |
| A = [int(a) for a in input().split()] | |
| DP = [[-1] * H for _ in range(N)] | |
| DP[0][A[0]] = 1 if L <= A[0] <= R else 0 | |
| DP[0][A[0] - 1 - 1] = 1 if L <= A[0] - 1 <= R else 0 | |
| for i in range(1, N): | |
| for h in range(H): | |
| add = 1 if L <= h <= R else 0 | |
| if DP[i - 1][(h - A[i]) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add) | |
| if DP[i - 1][(h - A[i] + 1) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add) | |
| print(max(DP[N - 1])) | |
| return 0 | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 086 | |
| ```python | |
| rr = lambda: input().strip() | |
| rri = lambda: int(rr()) | |
| rrm = lambda: list(map(int, rr().split())) | |
| MOD = 10 ** 9 + 7 | |
| class DSU: | |
| def __init__(self, N): | |
| self.par = list(range(N + 1)) | |
| self.rnk = [0] * (N + 1) | |
| self.sz = [-1] * (N + 1) | |
| def find(self, x): | |
| if self.par[x] != x: | |
| self.par[x] = self.find(self.par[x]) | |
| return self.par[x] | |
| def union(self, x, y): | |
| xr, yr = (self.find(x), self.find(y)) | |
| if xr == yr: | |
| return | |
| if self.rnk[xr] < self.rnk[yr]: | |
| xr, yr = (yr, xr) | |
| if self.rnk[xr] == self.rnk[yr]: | |
| self.rnk[xr] += 1 | |
| self.par[yr] = xr | |
| self.sz[xr] += self.sz[yr] | |
| def size(self, x): | |
| return self.sz[self.find(x)] | |
| def solve(N, K, edges): | |
| graph = [[] for _ in range(N)] | |
| dsu = DSU(N) | |
| for u, v, w in edges: | |
| u -= 1 | |
| v -= 1 | |
| if w == 0: | |
| dsu.union(u, v) | |
| ans = pow(N, K, MOD) | |
| for x in range(N): | |
| if dsu.find(x) == x: | |
| ans -= pow(dsu.size(x), K, MOD) | |
| ans %= MOD | |
| return ans | |
| for tc in range(1): | |
| N, K = rrm() | |
| edges = [rrm() for _ in range(N - 1)] | |
| print(solve(N, K, edges)) | |
| ``` | |
| ### 087 | |
| ```python | |
| def cal(x, y): | |
| d = a[1] + y - a[0] - x | |
| wk1 = a[1] + y | |
| wkans = 0 | |
| for k in range(2, n - 1): | |
| diff = abs(wk1 + d - a[k]) | |
| if diff == 1: | |
| wkans += 1 | |
| elif diff > 1: | |
| return n + 1 | |
| wk1 += d | |
| return wkans | |
| n = int(input()) | |
| a = [int(i) for i in input().split()] | |
| if n > 2: | |
| ans_final = n + 1 | |
| for i in range(-1, 2): | |
| for j in range(-1, 2): | |
| ans_final = min(ans_final, cal(i, j) + abs(i) + abs(j)) | |
| else: | |
| ans_final = 0 | |
| if ans_final == n + 1: | |
| print(-1) | |
| else: | |
| print(ans_final) | |
| ``` | |
| ### 088 | |
| ```python | |
| n, m, k = input().split(' ') | |
| n = int(n) | |
| m = int(m) | |
| k = int(k) | |
| ind = [] | |
| pre = [] | |
| for _ in range(n): | |
| s = input() | |
| ind.append([]) | |
| for i, c in enumerate(s): | |
| if c == '1': | |
| ind[-1].append(i) | |
| for i in range(n): | |
| pre.append([]) | |
| for j in range(k + 1): | |
| pre[i].append([]) | |
| if len(ind[i]) > j: | |
| pre[i][j] = ind[i][-1] - ind[i][0] + 1 | |
| else: | |
| pre[i][j] = 0 | |
| continue | |
| for x in range(j + -1): | |
| y = len(ind[i]) - 1 - j + x | |
| if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: | |
| pre[i][j] = ind[i][y] - ind[i][x] + 1 | |
| dp = [[]] | |
| for i in range(k + 1): | |
| dp[0].append(pre[0][i]) | |
| for i in range(1, n): | |
| dp.append([]) | |
| for j in range(0, k + 1): | |
| dp[i].append(pre[i][j] + dp[i - 1][0]) | |
| for z in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) | |
| print(dp[n - 1][k]) | |
| ``` | |
| ### 089 | |
| ```python | |
| a_3_3 = [list(map(int, input().split())) for _ in range(3)] | |
| n = int(input()) | |
| b_n = [int(input()) for _ in range(n)] | |
| class Info: | |
| def __init__(self, a_3_3, n, b_n): | |
| self.a_3_3 = a_3_3 | |
| self.n = n | |
| self.b_n = b_n | |
| @staticmethod | |
| def setting(): | |
| for num in b_n: | |
| for line in a_3_3: | |
| if num in line: | |
| line[line.index(num)] = 0 | |
| return a_3_3 | |
| @staticmethod | |
| def result(): | |
| diagonal_lurd = a_3_3[0][0] + a_3_3[1][1] + a_3_3[2][2] | |
| diagonal_ldru = a_3_3[2][0] + a_3_3[1][1] + a_3_3[0][2] | |
| for index, line in enumerate(a_3_3): | |
| row_sum = sum(line) | |
| col_sum = a_3_3[0][index] + a_3_3[1][index] + a_3_3[2][index] | |
| if diagonal_lurd == 0 or diagonal_ldru == 0 or row_sum == 3 or (col_sum == 0): | |
| print('Yes') | |
| return | |
| print('No') | |
| info = Info(a_3_3, n, b_n) | |
| info.setting() | |
| info.result() | |
| ``` | |
| ### 090 | |
| ```python | |
| import math | |
| dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)] | |
| n, m, k = list(map(int, input().split())) | |
| k += 1 | |
| z = list(map(int, input().split())) | |
| matrix = [] | |
| for i in range(n): | |
| ans = list(map(int, input().split())) | |
| matrix.append(ans) | |
| for i in range(len(z)): | |
| if i == 0: | |
| if z[i] != 0: | |
| dp[0][1][z[i] - 1] = 0 | |
| else: | |
| for x in range(m): | |
| dp[0][1][x - 1] = matrix[i][x] | |
| elif z[i] != 0: | |
| col = z[i] - 1 | |
| if z[i - 1] != 0: | |
| col2 = z[i - 1] - 1 | |
| if col2 == col: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col]) | |
| else: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col]) | |
| else: | |
| pq = [] | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| col = z[i] - 1 | |
| if col != pq[0][1] and pq[0][0] != math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col]) | |
| elif pq[0][0] != math.inf and col == pq[0][1]: | |
| dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col]) | |
| elif pq[0][0] == math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col]) | |
| elif z[i - 1] != 0: | |
| col = z[i - 1] - 1 | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| for x in range(m): | |
| if x != col: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x]) | |
| else: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x]) | |
| else: | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| for v in range(m): | |
| if v != pq[0][1]: | |
| dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v]) | |
| else: | |
| dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v]) | |
| mini = math.inf | |
| for i in range(m): | |
| mini = min(mini, dp[n - 1][k - 1][i]) | |
| if mini == math.inf: | |
| print(-1) | |
| else: | |
| print(mini) | |
| ``` | |
| ### 091 | |
| ```python | |
| n = int(input()) | |
| C = 10 ** 9 + 6 | |
| a = input() | |
| s = input() | |
| d = [[0, 0, 0, 1]] | |
| for q in range(n): | |
| d.append(d[-1][:]) | |
| if a[q] != '?' and s[q] != '?': | |
| if int(a[q]) < int(s[q]): | |
| d[-1][1] = d[-1][3] = 0 | |
| d[-1][0] += d[-2][3] | |
| d[-1][2] += d[-2][1] | |
| elif int(a[q]) > int(s[q]): | |
| d[-1][0] = d[-1][3] = 0 | |
| d[-1][1] += d[-2][3] | |
| d[-1][2] += d[-2][0] | |
| elif s[q] != '?': | |
| d[-1][2] *= 10 | |
| d[-1][0] += d[-2][3] | |
| d[-1][1] += d[-2][3] | |
| d[-1][0] *= int(s[q]) + 1 | |
| d[-1][1] *= 10 - int(s[q]) | |
| d[-1][0] -= d[-2][3] | |
| d[-1][1] -= d[-2][3] | |
| d[-1][2] += d[-2][0] * (9 - int(s[q])) + d[-2][1] * int(s[q]) | |
| elif a[q] != '?': | |
| d[-1][2] *= 10 | |
| d[-1][0] += d[-2][3] | |
| d[-1][1] += d[-2][3] | |
| d[-1][1] *= int(a[q]) + 1 | |
| d[-1][0] *= 10 - int(a[q]) | |
| d[-1][0] -= d[-2][3] | |
| d[-1][1] -= d[-2][3] | |
| d[-1][2] += d[-2][1] * (9 - int(a[q])) + d[-2][0] * int(a[q]) | |
| else: | |
| d[-1][3] *= 10 | |
| d[-1][2] *= 100 | |
| d[-1][0] *= 55 | |
| d[-1][1] *= 55 | |
| d[-1][0] += d[-2][3] * 45 | |
| d[-1][1] += d[-2][3] * 45 | |
| d[-1][2] += d[-2][0] * 45 + d[-2][1] * 45 | |
| d[-1][0] %= C | |
| d[-1][1] %= C | |
| d[-1][2] %= C | |
| d[-1][3] %= C | |
| print(d[-1][2]) | |
| ``` | |
| ### 092 | |
| ```python | |
| from math import gcd | |
| def covered_points(xa, ya, xb, yb): | |
| if xb < xa: | |
| temp_x = xa | |
| xa = xb | |
| xb = temp_x | |
| temp_y = ya | |
| ya = yb | |
| yb = temp_y | |
| y_0 = abs(yb - ya) | |
| x_0 = xb - xa | |
| return gcd(y_0, x_0) + 1 | |
| def intersection2(xa, ya, xb, yb, xc, yc, xd, yd): | |
| if max(xa, xb) < min(xc, xd): | |
| return () | |
| if max(ya, yb) < min(yc, yd): | |
| return () | |
| if xa == xb and xc == xd or (ya == yb and yc == yd): | |
| return () | |
| if ya == yb and yc == yd: | |
| return () | |
| a1 = yb - ya | |
| b1 = xb - xa | |
| c1 = xa * a1 - ya * b1 | |
| a2 = yd - yc | |
| b2 = xd - xc | |
| c2 = xc * a2 - yc * b2 | |
| det = a1 * b2 - a2 * b1 | |
| if det == 0: | |
| return () | |
| detx = c1 * b2 - c2 * b1 | |
| dety = -a1 * c2 + a2 * c1 | |
| if detx % det != 0 or dety % det != 0: | |
| return () | |
| x = int(detx / det) | |
| y = int(dety / det) | |
| if x < min(xa, xb) or x > max(xa, xb) or x < min(xc, xd) or (x > max(xc, xd)): | |
| return () | |
| if not (y < min(ya, yb) or y > max(ya, yb) or y < min(yc, yd) or (y > max(yc, yd))): | |
| return () | |
| return (x, y) | |
| def __starting_point(): | |
| n = int(input()) | |
| Ax = [0] * n | |
| Ay = [0] * n | |
| Bx = [0] * n | |
| By = [0] * n | |
| n_cov = 0 | |
| intersections = {} | |
| for i in range(n): | |
| line = input().split(sep=' ') | |
| Ax[i] = int(line[0]) | |
| Ay[i] = int(line[1]) | |
| Bx[i] = int(line[2]) | |
| By[i] = int(line[3]) | |
| n_cov += covered_points(Ax[i], Ay[i], Bx[i], By[i]) | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| temp = intersection2(Ax[i], Ay[i], Bx[i], By[i], Ax[j], Ay[j], Bx[j], By[j]) | |
| if len(temp) == 0: | |
| continue | |
| if temp in intersections: | |
| intersections[temp].append(i) | |
| intersections[temp].append(j) | |
| else: | |
| intersections[temp] = [i, j] | |
| for i in intersections: | |
| n_cov = n_cov - len(set(intersections[i])) + 1 | |
| print(n_cov) | |
| __starting_point() | |
| ``` | |
| ### 093 | |
| ```python | |
| rr = lambda: input().strip() | |
| rri = lambda: int(rr()) | |
| rrm = lambda: list(map(int, rr().split())) | |
| MOD = 10 ** 8 + 7 | |
| class DSU: | |
| def __init__(self, N): | |
| self.par = list(range(N + 1)) | |
| self.rnk = [0] * (N + 1) | |
| self.sz = [1] * (N + 1) | |
| def find(self, x): | |
| if self.par[x] != x: | |
| self.par[x] = self.find(self.par[x]) | |
| return self.par[x] | |
| def union(self, x, y): | |
| xr, yr = (self.find(x), self.find(y)) | |
| if xr == yr: | |
| return | |
| if self.rnk[xr] < self.rnk[yr]: | |
| xr, yr = (yr, xr) | |
| if self.rnk[xr] == self.rnk[yr]: | |
| self.rnk[xr] += 1 | |
| self.par[yr] = xr | |
| self.sz[xr] += self.sz[yr] | |
| def size(self, x): | |
| return self.sz[self.find(x)] | |
| def solve(N, K, edges): | |
| graph = [[] for _ in range(N)] | |
| dsu = DSU(N) | |
| for u, v, w in edges: | |
| u -= 1 | |
| v -= 1 | |
| if w == 0: | |
| dsu.union(u, v) | |
| ans = pow(N, K, MOD) | |
| for x in range(N): | |
| if dsu.find(x) == x: | |
| ans -= pow(dsu.size(x), K, MOD) | |
| ans %= MOD | |
| return ans | |
| for tc in range(1): | |
| N, K = rrm() | |
| edges = [rrm() for _ in range(N - 1)] | |
| print(solve(N, K, edges)) | |
| ``` | |
| ### 094 | |
| ```python | |
| import sys | |
| n = int(input()) | |
| nums = list(map(int, input().split())) | |
| eleCount = {} | |
| possibleMax = 1 | |
| for i in range(n): | |
| if nums[i] in list(eleCount.keys()): | |
| eleCount[nums[i]] += 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i - 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| eleCount[nums[i]] = 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| print(possibleMax) | |
| ``` | |
| ### 095 | |
| ```python | |
| from math import ceil | |
| def solve(): | |
| n, x, y, d = [int(x) for x in input().split()] | |
| INF = 1000000000.0 | |
| sol1 = abs(x - y) // d if (x - y) % d == 0 else INF | |
| sol2 = ceil((x - 1) / d) + ceil(y * 1 / d) if (y - 1) % d == 0 else INF | |
| sol3 = ceil((n - x) / d) + ceil((n - y) / d) if (n - y) % d == 0 else INF | |
| ans = min(sol1, sol2, sol3) | |
| ans = ans if ans != INF else -1 | |
| print(ans) | |
| nt = int(input()) | |
| for _ in range(nt): | |
| solve() | |
| ``` | |
| ### 096 | |
| ```python | |
| def cal(x, y): | |
| d = a[1] + y - a[0] - x | |
| wk1 = a[1] + y | |
| wkans = 0 | |
| for k in range(2, n): | |
| diff = abs(wk1 + d - a[k]) | |
| if diff == 1: | |
| wkans += 1 | |
| elif diff > 1: | |
| return n + 1 | |
| wk1 += d | |
| return wkans | |
| n = int(input()) | |
| a = [int(i) for i in input().split()] | |
| if n > 2: | |
| ans_final = n + 1 | |
| for i in range(-1, 2): | |
| for j in range(-1, 2 - 1): | |
| ans_final = min(ans_final, cal(i, j) + abs(i) + abs(j)) | |
| else: | |
| ans_final = 0 | |
| if ans_final == n + 1: | |
| print(-1) | |
| else: | |
| print(ans_final) | |
| ``` | |
| ### 097 | |
| ```python | |
| n, h, l, r = list(map(int, input().split())) | |
| N = n | |
| a = list(map(int, input().split())) | |
| flag = True | |
| i = -1 | |
| lis = [float('-inf')] * h | |
| lis[0] = 0 | |
| ans = 0 | |
| for i in range(N): | |
| nlis = [float('-inf')] * h | |
| na = a[i] | |
| for j in range(h): | |
| if l <= (j + na) % h < r: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j]) | |
| if l <= (j + na - 1) % h <= r: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j]) | |
| lis = nlis | |
| print(max(0, max(lis))) | |
| ``` | |
| ### 098 | |
| ```python | |
| n = int(input()) | |
| a = input() | |
| b = input() | |
| mod = int(1000000000.0 + 7) | |
| x, y, z = (1, 1, 1) | |
| for i in range(n): | |
| if a[i] == '?' and b[i] == '?': | |
| x = x * 55 % mod | |
| y = y * 55 % mod | |
| z = z * 10 % mod | |
| elif a[i] == '?': | |
| x = x * (10 - int(b[i])) % mod | |
| y = y * (int(b[i]) + 1) % mod | |
| elif b[i] == '?': | |
| x = x * (int(a[i]) + 1) & mod | |
| y = y * (10 - int(a[i])) % mod | |
| else: | |
| if int(a[i]) < int(b[i]): | |
| x = 0 | |
| if int(a[i]) > int(b[i]): | |
| y = 0 | |
| if a[i] != b[i]: | |
| z = 0 | |
| res = pow(10, a.count('?') + b.count('?'), mod) | |
| print((res - x - y + z) % mod) | |
| ``` | |
| ### 099 | |
| ```python | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| if n == 1 or n == 2: | |
| print(0) | |
| else: | |
| diff = [a[1] - a[0], a[1] + 1 - a[0], a[1] - 1 - a[0], a[1] - a[0] + 1, a[1] + 1 - a[0] + 1, a[1] - 1 - a[0] + 1, a[1] - a[0] - 1, a[1] + 1 - a[0] - 1, a[1] - 1 - a[0] - 1] | |
| flag1 = False | |
| num = float('inf') | |
| flag = True | |
| pre = a[1] | |
| d = diff[0] | |
| rec = 0 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[1] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[2] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] | |
| d = diff[3] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[4] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[5] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] | |
| d = diff[6] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[7] | |
| rec = -2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[8] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| if flag1: | |
| print(num) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 100 | |
| ```python | |
| 3 | |
| def build(n, s, t): | |
| ans = 1 | |
| for i in range(n): | |
| if s[i] == '?' and t[i] == '?': | |
| ans = 55 * ans % (10 ** 9 + 7) | |
| elif s[i] == '?': | |
| ans = (ord(t[i]) - ord('0') + 1) * ans % (10 ** 9 + 7) | |
| elif t[i] == '?': | |
| ans = (ord('9') - ord(s[i]) + 1) * ans % (10 ** 9 + 7) | |
| return ans | |
| n = int(input()) | |
| s = input() | |
| t = input() | |
| sltt = True | |
| tlts = True | |
| qm = 0 | |
| cqm = 0 | |
| for i in range(n): | |
| if t[i] == '?': | |
| qm += 1 | |
| if s[i] == '?': | |
| qm += 1 | |
| if t[i] == '?' and s[i] == '?': | |
| cqm += 1 | |
| if t[i] == '?' or s[i] == '?': | |
| continue | |
| if ord(s[i]) < ord(t[i]): | |
| tlts = False | |
| if ord(t[i]) < ord(s[i]): | |
| sltt = False | |
| if not sltt and (not tlts): | |
| print(pow(10, qm, 10 ** 9 + 7)) | |
| elif sltt and tlts: | |
| print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t) - build(n, t, s) + pow(10, cqm, 10 ** 9 + 7)) % (10 ** 9 - 7)) | |
| elif sltt: | |
| print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t)) % (10 ** 9 + 7)) | |
| else: | |
| print((pow(10, qm, 10 ** 9 + 7) - build(n, t, s)) % (10 ** 9 + 7)) | |
| ``` | |
| ### 101 | |
| ```python | |
| n = int(input()) | |
| u = [int(u) for u in input().split()] | |
| NMAX = 100005 | |
| suma = [0 for _ in range(NMAX)] | |
| total = [0 for _ in range(NMAX)] | |
| diferentes = 0 | |
| sol = 0 | |
| maximo = 1 | |
| for i, v in enumerate(u): | |
| if total[v] == 0: | |
| diferentes += 1 | |
| else: | |
| suma[total[v]] -= 1 | |
| total[v] += 1 | |
| suma[total[v]] += 1 | |
| maximo = max(maximo, total[v]) | |
| if diferentes <= 1: | |
| sol = i | |
| if suma[maximo - 1] == diferentes - 1 and suma[maximo] == 1: | |
| sol = i | |
| if suma[maximo] == diferentes - 1 and suma[1] == 1: | |
| sol = i | |
| if suma[1] == diferentes: | |
| sol = i | |
| print(sol + 1) | |
| ``` | |
| ### 102 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| def int_array(): | |
| return list(map(int, input().strip().split())) | |
| def float_array(): | |
| return list(map(float, input().strip().split())) | |
| def str_array(): | |
| return input().strip().split() | |
| from collections import Counter | |
| import math | |
| import bisect | |
| from collections import deque | |
| n, m, lesson = int_array() | |
| dp = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| days = [[] for i in range(n)] | |
| for i in range(n): | |
| s = input() | |
| for j in range(m): | |
| if s[j] == '1': | |
| days[i].append(j + 1) | |
| m = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| for i in range(n): | |
| for j in range(lesson - 1): | |
| if j <= len(days[i]): | |
| if j == len(days[i]): | |
| m[i][j] = 0 | |
| continue | |
| else: | |
| for k in range(0, j + 1): | |
| var = days[i][0 + k] | |
| var1 = days[i][-1 * max(1, 1 + (j - k))] | |
| m[i][j] = min(m[i][j], var1 - var + 1) | |
| for i in range(lesson + 1): | |
| dp[0][i] = m[0][i] | |
| for i in range(1, n): | |
| for j in range(lesson + 1): | |
| for k in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + m[i][k]) | |
| print(min(dp[n - 1])) | |
| ``` | |
| ### 103 | |
| ```python | |
| import sys | |
| n, m, k = list(map(int, input().split())) | |
| table = [input() for _ in range(n)] | |
| dp = [0] * (k + 1) | |
| for a in table: | |
| one = [] | |
| for i in range(m): | |
| if a[i] == '1': | |
| one.append(i) | |
| if not one: | |
| continue | |
| ni = len(one) | |
| subdp = [10 ** 9] * (ni + 1) | |
| subdp[-1] = 0 | |
| for i in range(ni): | |
| for j in range(i, ni - 1): | |
| subdp[ni - (j - i + 1)] = min(subdp[ni - (j - i + 1)], one[j] - one[i] + 1) | |
| next_dp = [10 ** 9] * (k + 1) | |
| for i in range(k, -1, -1): | |
| for j in range(ni + 1): | |
| if i + j > k: | |
| break | |
| next_dp[i + j] = min(next_dp[i + j], dp[i] + subdp[j]) | |
| dp = next_dp | |
| print(min(dp)) | |
| ``` | |
| ### 104 | |
| ```python | |
| from math import gcd | |
| n = int(input()) | |
| pts = [] | |
| ans = 0 | |
| for i in range(0, n): | |
| a, b, A, B = tuple(map(int, input().split(' '))) | |
| pts.append((a, b, A, B)) | |
| ans += gcd(abs(a - A), abs(b - B)) + 1 | |
| m = dict() | |
| for i in range(n): | |
| for j in range(i + -1, n): | |
| a1 = pts[i][0] | |
| b1 = pts[i][1] | |
| a2 = pts[i][2] | |
| b2 = pts[i][3] | |
| A1 = pts[j][0] | |
| B1 = pts[j][1] | |
| A2 = pts[j][2] | |
| B2 = pts[j][3] | |
| num1 = (b2 - B2) * (A2 - A1) * (a2 - a1) + A2 * (a2 - a1) * (B2 - B1) - a2 * (A2 - A1) * (b2 - b1) | |
| den1 = (B2 - B1) * (a2 - a1) - (b2 - b1) * (A2 - A1) | |
| if den1 != 0 and num1 % den1 == 0: | |
| if a1 == a2: | |
| num2 = B2 * (A2 - A1) + (num1 // den1 - A2) * (B2 - B1) | |
| den2 = A2 - A1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| else: | |
| num2 = b2 * (a2 - a1) + (num1 // den1 - a2) * (b2 - b1) | |
| den2 = a2 - a1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| sum = 0 | |
| for i in list(m.values()): | |
| sum += len(i) - 1 | |
| print(ans - sum) | |
| ``` | |
| ### 105 | |
| ```python | |
| import sys | |
| YES = 'Yes' | |
| NO = 'No' | |
| def solve(A: 'List[List[int]]', N: int, b: 'List[int]'): | |
| A_ = [] | |
| for a_ in A: | |
| A_ += [[a__, False] for a__ in a_] | |
| for b_ in b: | |
| for a__ in A_: | |
| if a__[0] == b_: | |
| a__[1] = True | |
| if A_[0][1] and (A_[1][1] and A_[2][1] or (A_[3][1] and A_[6][1]) or (A_[4][1] and A_[8][1])) or (A_[1][1] and A_[4][1] and A_[7][1]) or (A_[2][1] and A_[5][1] and A_[8][1]) or (A_[3][1] and A_[4][0] and A_[5][1]) or (A_[6][1] and A_[7][1] and A_[8][1]) or (A_[2][1] and A_[4][1] and A_[6][1]): | |
| print(YES) | |
| else: | |
| print(NO) | |
| return | |
| def main(): | |
| def iterate_tokens(): | |
| for line in sys.stdin: | |
| for word in line.split(): | |
| yield word | |
| tokens = iterate_tokens() | |
| A = [[int(next(tokens)) for _ in range(3)] for _ in range(3)] | |
| N = int(next(tokens)) | |
| b = [int(next(tokens)) for _ in range(N)] | |
| solve(A, N, b) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 106 | |
| ```python | |
| n = int(input()) | |
| u = [int(u) for u in input().split()] | |
| NMAX = 100005 | |
| suma = [0 for _ in range(NMAX)] | |
| total = [0 for _ in range(NMAX)] | |
| diferentes = 0 | |
| sol = 0 | |
| maximo = 1 | |
| for i, v in enumerate(u): | |
| if total[v] == 0: | |
| diferentes += 1 | |
| else: | |
| suma[total[v]] -= 1 | |
| total[v] += 1 | |
| suma[total[v]] += 1 | |
| maximo = max(maximo, total[v]) | |
| if diferentes <= 1: | |
| sol = i | |
| if suma[maximo - 1 - 1] == diferentes - 1 and suma[maximo] == 1: | |
| sol = i | |
| if suma[maximo] == diferentes - 1 and suma[1] == 1: | |
| sol = i | |
| if suma[1] == diferentes: | |
| sol = i | |
| print(sol + 1) | |
| ``` | |
| ### 107 | |
| ```python | |
| import sys | |
| n, m, k = list(map(int, input().split())) | |
| table = [input() for _ in range(n)] | |
| dp = [0] * (k + 1) | |
| for a in table: | |
| one = [] | |
| for i in range(m): | |
| if a[i] == '1': | |
| one.append(i) | |
| if not one: | |
| continue | |
| ni = len(one) | |
| subdp = [10 ** 9] * (ni + 1) | |
| subdp[-1 - 1] = 0 | |
| for i in range(ni): | |
| for j in range(i, ni): | |
| subdp[ni - (j - i + 1)] = min(subdp[ni - (j - i + 1)], one[j] - one[i] + 1) | |
| next_dp = [10 ** 9] * (k + 1) | |
| for i in range(k, -1, -1): | |
| for j in range(ni + 1): | |
| if i + j > k: | |
| break | |
| next_dp[i + j] = min(next_dp[i + j], dp[i] + subdp[j]) | |
| dp = next_dp | |
| print(min(dp)) | |
| ``` | |
| ### 108 | |
| ```python | |
| n, h, l, r = list(map(int, input().split())) | |
| N = n | |
| a = list(map(int, input().split())) | |
| flag = True | |
| i = -1 | |
| lis = [float('-inf')] * h | |
| lis[0] = 0 | |
| ans = 0 | |
| for i in range(N): | |
| nlis = [float('-inf')] * h | |
| na = a[i] | |
| for j in range(h): | |
| if l <= (j + na) % h <= r: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j]) | |
| if l <= (j + na - 0) % h <= r: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j]) | |
| lis = nlis | |
| print(max(0, max(lis))) | |
| ``` | |
| ### 109 | |
| ```python | |
| def main(): | |
| n, m, k = list(map(int, input().split())) | |
| c, *cc = list(map(int, input().split())) | |
| pp, *ppp = (list(map(float, input().split())) for _ in range(n)) | |
| inf = 2.0 ** 47 | |
| nxt = [[0.0 if i == c - 1 else inf for i in range(m)] if c else pp] | |
| for c, pp in zip(cc, ppp): | |
| newrow = [inf] * m | |
| cur, nxt = (nxt, [newrow]) | |
| if c: | |
| c -= 1 | |
| for row in cur: | |
| p = row[c] | |
| if not newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| row[c] = inf | |
| newrow = [inf] * m | |
| newrow[c] = min(row) | |
| nxt.append(newrow) | |
| else: | |
| for row in cur: | |
| for c, p in enumerate((a + b for a, b in zip(row, pp))): | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| bestclr = min(list(range(m)), key=row.__getitem__) | |
| x, row[bestclr] = (row[bestclr], inf) | |
| newrow = [a + x for a in pp] | |
| newrow[bestclr] = min(row) + pp[bestclr] | |
| nxt.append(newrow) | |
| p = min(nxt[-1]) | |
| print(int(p) if p < inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 110 | |
| ```python | |
| n = int(input()) | |
| a = input() | |
| b = input() | |
| mod = int(1000000000.0 + 7) | |
| x, y, z = (1, 1, 1) | |
| for i in range(n): | |
| if a[i] == '?' and b[i] == '?': | |
| x = x * 55 % mod | |
| y = y * 55 % mod | |
| z = z * 10 % mod | |
| elif a[i] == '?': | |
| x = x * (10 - int(b[i])) % mod | |
| y = y * (int(b[i]) + 1) % mod | |
| elif b[i] == '?': | |
| x = x * (int(a[i]) + 1) // mod | |
| y = y * (10 - int(a[i])) % mod | |
| else: | |
| if int(a[i]) < int(b[i]): | |
| x = 0 | |
| if int(a[i]) > int(b[i]): | |
| y = 0 | |
| if a[i] != b[i]: | |
| z = 0 | |
| res = pow(10, a.count('?') + b.count('?'), mod) | |
| print((res - x - y + z) % mod) | |
| ``` | |
| ### 111 | |
| ```python | |
| MAX = 10 ** 18 + 5 | |
| n, m, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| cost = [list(map(int, input().split())) for _ in range(n)] | |
| min1 = [[MAX] * (k + 1) for _ in range(n)] | |
| min2 = [[MAX] * (k + 1) for _ in range(n)] | |
| idx = [[-1] * (k + 1) for _ in range(n)] | |
| for i in cost: | |
| i.insert(0, 0) | |
| dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)] | |
| if a[0] == 0: | |
| for i in range(1, m + 1): | |
| if min1[0][1] >= cost[0][i]: | |
| if min1[0][1] == cost[0][i]: | |
| idx[0][1] = -2 | |
| else: | |
| idx[0][1] = i | |
| min2[0][1] = min1[0][1] | |
| min1[0][1] = cost[0][i] | |
| elif min2[0][1] >= cost[0][i]: | |
| min2[0][1] = cost[0][i] | |
| dp[0][1][i] = cost[0][i] | |
| else: | |
| dp[0][1][a[0]] = 0 | |
| min1[0][-1] = 0 | |
| idx[0][1] = a[0] | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if a[i] == 0: | |
| for l in range(1, m + 1): | |
| dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l]) | |
| if l == idx[i - 1][j - 1]: | |
| dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| for l in range(1, m + 1): | |
| dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]]) | |
| if l != a[i]: | |
| dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]]) | |
| for l in range(1, m + 1): | |
| if min1[i][j] >= dp[i][j][l]: | |
| if min1[i][j] == dp[i][j][l]: | |
| idx[i][j] = -2 | |
| else: | |
| idx[i][j] = l | |
| min2[i][j] = min1[i][j] | |
| min1[i][j] = dp[i][j][l] | |
| elif min2[i][j] >= dp[i][j][l]: | |
| min2[i][j] = dp[i][j][l] | |
| ans = MAX | |
| for i in dp[-1][-1]: | |
| ans = min(ans, i) | |
| print(ans if ans < MAX else -1) | |
| ``` | |
| ### 112 | |
| ```python | |
| n = int(input()) | |
| a = input() | |
| b = input() | |
| mod = int(1000000000.0 + 7) | |
| x, y, z = (1, 1, 1) | |
| for i in range(n): | |
| if a[i] == '?' and b[i] == '?': | |
| x = x * 55 % mod | |
| y = y * 55 % mod | |
| z = z * 10 % mod | |
| elif a[i] == '?': | |
| x = x * (10 - int(b[i])) % mod | |
| y = y * (int(b[i]) + 1) % mod | |
| elif b[i] == '?': | |
| x = x * (int(a[i]) + 1) % mod | |
| y = y * (10 - int(a[i])) % mod | |
| else: | |
| if int(a[i]) < int(b[i]): | |
| x = 0 | |
| if int(a[i]) > int(b[i]): | |
| y = 0 | |
| if a[i] != b[i]: | |
| z = 0 | |
| res = pow(10, a.count('?') + b.count('?'), mod) | |
| print((res - x - y - z) % mod) | |
| ``` | |
| ### 113 | |
| ```python | |
| import sys | |
| n, m, k = list(map(int, input().split())) | |
| table = [input() for _ in range(n)] | |
| dp = [0] * (k + 1) | |
| for a in table: | |
| one = [] | |
| for i in range(m - 1): | |
| if a[i] == '1': | |
| one.append(i) | |
| if not one: | |
| continue | |
| ni = len(one) | |
| subdp = [10 ** 9] * (ni + 1) | |
| subdp[-1] = 0 | |
| for i in range(ni): | |
| for j in range(i, ni): | |
| subdp[ni - (j - i + 1)] = min(subdp[ni - (j - i + 1)], one[j] - one[i] + 1) | |
| next_dp = [10 ** 9] * (k + 1) | |
| for i in range(k, -1, -1): | |
| for j in range(ni + 1): | |
| if i + j > k: | |
| break | |
| next_dp[i + j] = min(next_dp[i + j], dp[i] + subdp[j]) | |
| dp = next_dp | |
| print(min(dp)) | |
| ``` | |
| ### 114 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, x, y, d = list(map(int, input().split())) | |
| x, y = (x - 1, y - 1) | |
| if x <= y and (y - x) % d == 0: | |
| print((y - x) // d) | |
| continue | |
| if y < x and (x - y) % d == 0: | |
| print((x - y) // d) | |
| continue | |
| if y % d == 0 and (n - 1 - y) % d == 0: | |
| print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)) | |
| continue | |
| if y % d == 0: | |
| print((x + y + d - 1) // d) | |
| continue | |
| if (n - 1 - y) % d == 0: | |
| print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d) | |
| continue | |
| print(-4) | |
| ``` | |
| ### 115 | |
| ```python | |
| def solve(seq): | |
| if len(seq) <= 2: | |
| return 0 | |
| startings = [] | |
| for x in (-1, 0, 1): | |
| for y in (--1, 0, 1): | |
| start = (seq[1] + y, seq[0] + x - seq[1] - y, int(x != 0) + int(y != 0)) | |
| startings.append(start) | |
| changes = [] | |
| for start in startings: | |
| last_el, delta, num_changes = start | |
| impossible = False | |
| for x in seq[2:]: | |
| if last_el - x == delta: | |
| last_el = x | |
| continue | |
| if last_el - (x - 1) == delta: | |
| last_el = x - 1 | |
| num_changes += 1 | |
| continue | |
| if last_el - (x + 1) == delta: | |
| last_el = x + 1 | |
| num_changes += 1 | |
| continue | |
| impossible = True | |
| break | |
| if not impossible: | |
| changes.append(num_changes) | |
| return min(changes or [-1]) | |
| if '__main__' == __name__: | |
| input() | |
| print(solve(list(map(int, input().strip().split())))) | |
| ``` | |
| ### 116 | |
| ```python | |
| def factorial(n): | |
| if n == 0: | |
| return 1 | |
| else: | |
| return n * factorial(n - 1) % (10 ** 9 + 7) | |
| n, k = [int(i) for i in input().split()] | |
| s = [[] for i in range(n)] | |
| for i in range(n - 1): | |
| a, b, c = [int(i) for i in input().split()] | |
| if c == 0: | |
| s[a - 1].append(b - 1) | |
| s[b - 1].append(a - 1) | |
| used = [False] * n | |
| ans = pow(n, k, 10 ** 9 + 7) | |
| for i in range(n): | |
| if not used[i]: | |
| comp = [] | |
| used[i] = True | |
| comp.append(i) | |
| c = 0 | |
| while c < len(comp): | |
| for h in s[comp[c]]: | |
| if not used[h]: | |
| used[h] = True | |
| comp.append(h) | |
| c += 1 | |
| ans = (ans - pow(len(comp), k, 10 ** 9 + 7)) % (10 ** 9 + 7) | |
| print(ans) | |
| ``` | |
| ### 117 | |
| ```python | |
| import sys | |
| n, m, k = list(map(int, input().split())) | |
| table = [input() for _ in range(n)] | |
| dp = [0] * (k + 1) | |
| for a in table: | |
| one = [] | |
| for i in range(m): | |
| if a[i] == '1': | |
| one.append(i) | |
| if not one: | |
| continue | |
| ni = len(one) | |
| subdp = [10 ** 9] * (ni + 1) | |
| subdp[-1] = 0 | |
| for i in range(ni): | |
| for j in range(i, ni): | |
| subdp[ni - (j - i + 1)] = min(subdp[ni - (j - i + 1) + 1], one[j] - one[i] + 1) | |
| next_dp = [10 ** 9] * (k + 1) | |
| for i in range(k, -1, -1): | |
| for j in range(ni + 1): | |
| if i + j > k: | |
| break | |
| next_dp[i + j] = min(next_dp[i + j], dp[i] + subdp[j]) | |
| dp = next_dp | |
| print(min(dp)) | |
| ``` | |
| ### 118 | |
| ```python | |
| import sys | |
| from math import gcd | |
| class Segment: | |
| def __init__(self, x1, y1, x2, y2): | |
| self.x1 = x1 | |
| self.y1 = y1 | |
| self.x2 = x2 | |
| self.y2 = y2 | |
| def dx(self): | |
| return self.x2 - self.x1 | |
| def dy(self): | |
| return self.y2 - self.y1 | |
| def pt_cnt(self): | |
| return gcd(abs(self.dx()), abs(self.dy())) + 1 | |
| def A(self): | |
| return self.y1 - self.y2 | |
| def B(self): | |
| return self.x2 - self.x1 | |
| def C(self): | |
| return -self.A() * self.x1 - self.B() * self.y1 | |
| def inner_x(self, x): | |
| l = self.x1 | |
| r = self.x2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= x <= r | |
| def inner_y(self, y): | |
| l = self.y1 | |
| r = self.y2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= y <= r | |
| def inner(self, x, y): | |
| return self.inner_x(x) and self.inner_y(y) | |
| def intersect(self, other): | |
| dx = det(self.C(), self.B(), other.C(), other.B()) | |
| dy = det(self.A(), self.C(), other.A(), other.C()) | |
| d = det(self.A(), self.B(), other.A(), other.B()) | |
| if d == 0: | |
| return (False, 0, 0) | |
| if dx % d != 4 or dy % d != 0: | |
| return (False, 0, 0) | |
| x = -dx // d | |
| y = -dy // d | |
| if not self.inner(x, y) or not other.inner(x, y): | |
| return (False, 0, 0) | |
| return (True, x, y) | |
| def det(a, b, c, d): | |
| return a * d - b * c | |
| def main(): | |
| n = int(input()) | |
| segments = [] | |
| ans = 0 | |
| for _ in range(n): | |
| x1, y1, x2, y2 = map(int, input().split()) | |
| segment = Segment(x1, y1, x2, y2) | |
| ans += segment.pt_cnt() | |
| pt = set() | |
| for other in segments: | |
| result, x, y = segment.intersect(other) | |
| if result: | |
| pt.add((x, y)) | |
| ans -= len(pt) | |
| segments.append(segment) | |
| print(ans) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 119 | |
| ```python | |
| import sys | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| N, = getIntList() | |
| zp = [] | |
| for i in range(N): | |
| ax, ay, bx, by = getIntList() | |
| if ax > bx: | |
| ax, bx = (bx, ax) | |
| ay, by = (by, ay) | |
| zp.append((ax, ay, bx, by)) | |
| res = 0 | |
| def gcd(a, b): | |
| if b == 0: | |
| return a | |
| return gcd(b, a % b) | |
| zgcd = [] | |
| for i in range(N): | |
| ax, ay, bx, by = zp[i - 1] | |
| tx = abs(bx - ax) | |
| ty = abs(by - ay) | |
| g = gcd(tx, ty) | |
| res += g + 1 | |
| zgcd.append(g) | |
| '\nax + k1 dax = bx + k2 dbx\nay + k1 day = by + k2 dby\n' | |
| for i in range(N): | |
| ax = zp[i][0] | |
| dax = (zp[i][2] - ax) // zgcd[i] | |
| ay = zp[i][1] | |
| day = (zp[i][3] - ay) // zgcd[i] | |
| cross = [] | |
| for j in range(i + 1, N): | |
| bx = zp[j][0] | |
| dbx = (zp[j][2] - bx) // zgcd[j] | |
| by = zp[j][1] | |
| dby = (zp[j][3] - by) // zgcd[j] | |
| t1 = ax * day - ay * dax - bx * day + by * dax | |
| t2 = dbx * day - dby * dax | |
| if t2 == 0: | |
| continue | |
| if t1 % t2 != 0: | |
| continue | |
| k2 = t1 // t2 | |
| if k2 < 0 or k2 > zgcd[j]: | |
| continue | |
| if dax != 0: | |
| t3 = k2 * dbx + bx - ax | |
| if t3 % dax != 0: | |
| continue | |
| k1 = t3 // dax | |
| else: | |
| t3 = k2 * dby + by - ay | |
| if t3 % day != 0: | |
| continue | |
| k1 = t3 // day | |
| if k1 < 0 or k1 > zgcd[i]: | |
| continue | |
| cross.append(k1) | |
| if not cross: | |
| continue | |
| cross.sort() | |
| d = 1 | |
| for j in range(1, len(cross)): | |
| if cross[j] != cross[j - 1]: | |
| d += 1 | |
| res -= d | |
| print(res) | |
| ``` | |
| ### 120 | |
| ```python | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| if n == 1 or n == 2: | |
| print(0) | |
| else: | |
| diff = [a[1] - a[0], a[1] + 1 - a[0], a[1] - 1 - a[0], a[1] - a[0] + 1, a[1] + 1 - a[0] + 1, a[1] - 1 - a[0] + 1, a[1] - a[0] - 1, a[1] + 1 - a[0] - 1, a[1] - 1 - a[0] - 1] | |
| flag1 = False | |
| num = float('inf') | |
| flag = True | |
| pre = a[1] | |
| d = diff[0] | |
| rec = 0 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[1] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[2] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] | |
| d = diff[3] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[4] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[5] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] | |
| d = diff[6] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[7] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[8] | |
| rec = -2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| if flag1: | |
| print(num) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 121 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| col = list(map(int, input().split())) | |
| p = [] | |
| for i in range(n): | |
| p.append(list(map(int, input().split()))) | |
| dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)] | |
| if col[0] == 0: | |
| for i in range(m): | |
| dp[0][1][i] = p[0][i] | |
| else: | |
| dp[0][1][col[0] - 1] = -2 | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if j == 1: | |
| if col[i]: | |
| dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1] | |
| else: | |
| for c in range(m): | |
| dp[i][j][c] = dp[i - 1][j][c] + p[i][c] | |
| elif col[i]: | |
| x = dp[i - 1][j][col[i] - 1] | |
| for t in range(m): | |
| if t != col[i] - 1: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][col[i] - 1] = x | |
| else: | |
| for c in range(m): | |
| x = dp[i - 1][j][c] | |
| for t in range(m): | |
| if t != c: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][c] = x + p[i][c] | |
| ans = min(dp[-1][-1]) | |
| if ans != float('inf'): | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 122 | |
| ```python | |
| from math import gcd | |
| def covered_points(xa, ya, xb, yb): | |
| if xb < xa: | |
| temp_x = xa | |
| xa = xb | |
| xb = temp_x | |
| temp_y = ya | |
| ya = yb | |
| yb = temp_y | |
| y_0 = abs(yb - ya) | |
| x_0 = xb - xa | |
| return gcd(y_0, x_0) + 1 | |
| def intersection2(xa, ya, xb, yb, xc, yc, xd, yd): | |
| if max(xa, xb) < min(xc, xd): | |
| return () | |
| if max(ya, yb) < min(yc, yd): | |
| return () | |
| if xa == xb and xc == xd or (ya == yb and yc == yd): | |
| return () | |
| if ya == yb and yc == yd: | |
| return () | |
| a1 = yb - ya | |
| b1 = xb - xa | |
| c1 = xa * a1 - ya * b1 | |
| a2 = yd - yc | |
| b2 = xd - xc | |
| c2 = xc * a2 - yc * b2 | |
| det = a1 * b2 - a2 * b1 | |
| if det == 0: | |
| return () | |
| detx = c1 * b2 - c2 * b1 | |
| dety = -a1 * c2 + (a2 + c1) | |
| if detx % det != 0 or dety % det != 0: | |
| return () | |
| x = int(detx / det) | |
| y = int(dety / det) | |
| if x < min(xa, xb) or x > max(xa, xb) or x < min(xc, xd) or (x > max(xc, xd)): | |
| return () | |
| if y < min(ya, yb) or y > max(ya, yb) or y < min(yc, yd) or (y > max(yc, yd)): | |
| return () | |
| return (x, y) | |
| def __starting_point(): | |
| n = int(input()) | |
| Ax = [0] * n | |
| Ay = [0] * n | |
| Bx = [0] * n | |
| By = [0] * n | |
| n_cov = 0 | |
| intersections = {} | |
| for i in range(n): | |
| line = input().split(sep=' ') | |
| Ax[i] = int(line[0]) | |
| Ay[i] = int(line[1]) | |
| Bx[i] = int(line[2]) | |
| By[i] = int(line[3]) | |
| n_cov += covered_points(Ax[i], Ay[i], Bx[i], By[i]) | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| temp = intersection2(Ax[i], Ay[i], Bx[i], By[i], Ax[j], Ay[j], Bx[j], By[j]) | |
| if len(temp) == 0: | |
| continue | |
| if temp in intersections: | |
| intersections[temp].append(i) | |
| intersections[temp].append(j) | |
| else: | |
| intersections[temp] = [i, j] | |
| for i in intersections: | |
| n_cov = n_cov - len(set(intersections[i])) + 1 | |
| print(n_cov) | |
| __starting_point() | |
| ``` | |
| ### 123 | |
| ```python | |
| from math import ceil | |
| def solve(): | |
| n, x, y, d = [int(x) for x in input().split()] | |
| INF = 1000000000.0 | |
| sol1 = abs(x - y) // d if (x - y) % d == 2 else INF | |
| sol2 = ceil((x - 1) / d) + ceil((y - 1) / d) if (y - 1) % d == 0 else INF | |
| sol3 = ceil((n - x) / d) + ceil((n - y) / d) if (n - y) % d == 0 else INF | |
| ans = min(sol1, sol2, sol3) | |
| ans = ans if ans != INF else -1 | |
| print(ans) | |
| nt = int(input()) | |
| for _ in range(nt): | |
| solve() | |
| ``` | |
| ### 124 | |
| ```python | |
| from collections import deque | |
| mod = 10 ** 9 + 11 | |
| class TreeNode: | |
| def __init__(self, val): | |
| self.val = val | |
| self.edges = {} | |
| def add_edge(self, node, color): | |
| self.edges[node] = color | |
| n, k = list(map(int, input().split())) | |
| tree = [] | |
| for i in range(1, n + 1): | |
| tree.append(TreeNode(i)) | |
| for _ in range(n - 1): | |
| u, v, x = list(map(int, input().split())) | |
| tree[u - 1].add_edge(v, x) | |
| tree[v - 1].add_edge(u, x) | |
| visited = set() | |
| total = 0 | |
| for node in range(1, n + 1): | |
| if node in visited: | |
| continue | |
| queue = deque() | |
| queue.append(node) | |
| visited.add(node) | |
| red_vertexes = 1 | |
| while queue: | |
| vertex = queue.popleft() | |
| for e, c in list(tree[vertex - 1].edges.items()): | |
| if c == 0 and e not in visited: | |
| queue.append(e) | |
| red_vertexes = red_vertexes + 1 | |
| visited.add(e) | |
| total = (total + pow(red_vertexes, k, mod)) % mod | |
| total = (pow(n, k, mod) - total) % mod | |
| print(total) | |
| ``` | |
| ### 125 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, k = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(n - 1)] | |
| mod = 10 ** 9 + 7 | |
| Group = [i for i in range(n + 1)] | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x - 1] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x)] = min(find(y), find(x)) | |
| for x, y, c in EDGE: | |
| if c == 0: | |
| Union(x, y) | |
| G = [find(i) for i in range(1, n + 1)] | |
| from collections import Counter | |
| counter = Counter(G) | |
| ANS = 0 | |
| for j in list(counter.values()): | |
| ANS = (ANS + pow(j, k, mod)) % mod | |
| print((pow(n, k, mod) - ANS) % mod) | |
| ``` | |
| ### 126 | |
| ```python | |
| rr = lambda: input().strip() | |
| rri = lambda: int(rr()) | |
| rrm = lambda: list(map(int, rr().split())) | |
| MOD = 10 ** 9 + 6 | |
| class DSU: | |
| def __init__(self, N): | |
| self.par = list(range(N + 1)) | |
| self.rnk = [0] * (N + 1) | |
| self.sz = [1] * (N + 1) | |
| def find(self, x): | |
| if self.par[x] != x: | |
| self.par[x] = self.find(self.par[x]) | |
| return self.par[x] | |
| def union(self, x, y): | |
| xr, yr = (self.find(x), self.find(y)) | |
| if xr == yr: | |
| return | |
| if self.rnk[xr] < self.rnk[yr]: | |
| xr, yr = (yr, xr) | |
| if self.rnk[xr] == self.rnk[yr]: | |
| self.rnk[xr] += 1 | |
| self.par[yr] = xr | |
| self.sz[xr] += self.sz[yr] | |
| def size(self, x): | |
| return self.sz[self.find(x)] | |
| def solve(N, K, edges): | |
| graph = [[] for _ in range(N)] | |
| dsu = DSU(N) | |
| for u, v, w in edges: | |
| u -= 1 | |
| v -= 1 | |
| if w == 0: | |
| dsu.union(u, v) | |
| ans = pow(N, K, MOD) | |
| for x in range(N): | |
| if dsu.find(x) == x: | |
| ans -= pow(dsu.size(x), K, MOD) | |
| ans %= MOD | |
| return ans | |
| for tc in range(1): | |
| N, K = rrm() | |
| edges = [rrm() for _ in range(N - 1)] | |
| print(solve(N, K, edges)) | |
| ``` | |
| ### 127 | |
| ```python | |
| def main(): | |
| A = [list(map(int, input().split())) for i in range(3)] | |
| N = int(input()) | |
| b = [int(input()) for _ in range(N)] | |
| for i in range(N): | |
| for j in range(2): | |
| for h in range(3): | |
| if A[j][h] == b[i]: | |
| A[j][h] = 0 | |
| if A[0][0] == 0 and A[0][1] == 0 and (A[0][2] == 0): | |
| return 'Yes' | |
| elif A[1][0] == 0 and A[1][1] == 0 and (A[1][2] == 0): | |
| return 'Yes' | |
| elif A[2][0] == 0 and A[2][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][0] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| elif A[0][1] == 0 and A[1][1] == 0 and (A[2][1] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][2] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][1] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| return 'No' | |
| print(main()) | |
| ``` | |
| ### 128 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| col = list(map(int, input().split())) | |
| p = [] | |
| for i in range(n): | |
| p.append(list(map(int, input().split()))) | |
| dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)] | |
| if col[0] == 0: | |
| for i in range(m): | |
| dp[0][1][i] = p[0][i] | |
| else: | |
| dp[0][1][col[0] - 2] = 0 | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if j == 1: | |
| if col[i]: | |
| dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1] | |
| else: | |
| for c in range(m): | |
| dp[i][j][c] = dp[i - 1][j][c] + p[i][c] | |
| elif col[i]: | |
| x = dp[i - 1][j][col[i] - 1] | |
| for t in range(m): | |
| if t != col[i] - 1: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][col[i] - 1] = x | |
| else: | |
| for c in range(m): | |
| x = dp[i - 1][j][c] | |
| for t in range(m): | |
| if t != c: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][c] = x + p[i][c] | |
| ans = min(dp[-1][-1]) | |
| if ans != float('inf'): | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 129 | |
| ```python | |
| n, h, l, r = list(map(int, input().split())) | |
| N = n | |
| a = list(map(int, input().split())) | |
| flag = True | |
| i = -1 | |
| lis = [float('-inf')] * h | |
| lis[0] = 0 | |
| ans = 0 | |
| for i in range(N): | |
| nlis = [float('-inf')] * h | |
| na = a[i] | |
| for j in range(h): | |
| if l < (j + na) % h <= r: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j]) | |
| if l <= (j + na - 1) % h <= r: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1) | |
| else: | |
| nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j]) | |
| lis = nlis | |
| print(max(0, max(lis))) | |
| ``` | |
| ### 130 | |
| ```python | |
| def main(): | |
| from sys import stdin, stdout | |
| def read(): | |
| return stdin.readline().rstrip('\n') | |
| def read_array(sep=None, maxsplit=-1): | |
| return read().split(sep, maxsplit) | |
| def read_int(): | |
| return int(read()) | |
| def read_int_array(sep=None, maxsplit=-1): | |
| return [int(a) for a in read_array(sep, maxsplit)] | |
| def write(*args, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in args)) + end) | |
| def write_array(array, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in array)) + end) | |
| n = read_int() | |
| nums = read_int_array() | |
| counts = {} | |
| import collections | |
| inv_counts = collections.defaultdict(int) | |
| out = 0 | |
| for i, x in enumerate(nums): | |
| c = inv_counts[x] | |
| if c: | |
| counts[c].remove(x) | |
| if not counts[c]: | |
| counts.pop(c) | |
| c += 1 | |
| inv_counts[x] = c | |
| counts.setdefault(c, set()).add(x) | |
| if len(counts) == 2 and any((len(s) == 1 and (k - 1 in counts or k == 1) for k, s in list(counts.items()))): | |
| out = i + 1 | |
| elif len(counts) == 1: | |
| k = next(iter(list(counts.keys()))) | |
| if k == 1 or len(counts[k]) == 1: | |
| out = i + 1 | |
| write(out) | |
| main() | |
| ``` | |
| ### 131 | |
| ```python | |
| def min_sub_array(day, k): | |
| if not day: | |
| return [0] * (k + 1) | |
| n = len(day) | |
| best = [float('inf')] * (n + 1) | |
| best[0] = 0 | |
| best[1] = 1 | |
| for size in range(2, n + 1): | |
| for i in range(n + 1 - size): | |
| best[size] = min(best[size], day[i + size - 1] - day[i] + 1) | |
| output = [0] * (k + 1) | |
| for i in range(k + 1 - 1): | |
| if n - i > 0: | |
| output[i] = best[n - i] | |
| return output | |
| N, M, K = list(map(int, input().split())) | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| best = min_sub_array(day, K) | |
| for _ in range(N - 1): | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| new_day_best = min_sub_array(day, K) | |
| new_best = [float('inf')] * (K + 1) | |
| for i in range(K + 1): | |
| for j in range(i + 1): | |
| new_best[i] = min(new_best[i], new_day_best[j] + best[i - j]) | |
| best = new_best | |
| print(best[K]) | |
| ``` | |
| ### 132 | |
| ```python | |
| from random import random | |
| from collections import defaultdict | |
| import math | |
| import re | |
| import fractions | |
| N = int(input()) | |
| A = list(map(int, input().split(' '))) | |
| arr = [0] * (10 ** 5 + 1) | |
| existing = set() | |
| for a in A: | |
| arr[a] += 1 | |
| existing.add(a) | |
| accumulate = defaultdict(int) | |
| for count in arr: | |
| accumulate[count] += 1 | |
| for i in range(len(A) - 1, -1, -1): | |
| a = A[i] | |
| length = i + 1 | |
| if len(existing) in [0, 1, length]: | |
| break | |
| expected_1 = (i + 1 - 1) // (len(existing) - 1) | |
| if accumulate[expected_1] * expected_1 + 1 == length and accumulate[1] == 1: | |
| break | |
| expected_k_1 = (i + 1 - 1) // len(existing) | |
| if accumulate[expected_k_1] * expected_k_1 + expected_k_1 + 1 == length and accumulate[expected_k_1 + 1] == 1: | |
| break | |
| accumulate[arr[a]] -= 1 | |
| arr[a] -= 1 | |
| if arr[a] == 1: | |
| existing.discard(a) | |
| accumulate[arr[a]] += 1 | |
| print(length) | |
| ``` | |
| ### 133 | |
| ```python | |
| n, m, k = input().split(' ') | |
| n = int(n) | |
| m = int(m) | |
| k = int(k) | |
| ind = [] | |
| pre = [] | |
| for _ in range(n): | |
| s = input() | |
| ind.append([]) | |
| for i, c in enumerate(s): | |
| if c == '1': | |
| ind[-1].append(i) | |
| for i in range(n): | |
| pre.append([]) | |
| for j in range(k + 1): | |
| pre[i].append([]) | |
| if len(ind[i]) > j: | |
| pre[i][j] = ind[i][-1] - ind[i][0] + -1 | |
| else: | |
| pre[i][j] = 0 | |
| continue | |
| for x in range(j + 1): | |
| y = len(ind[i]) - 1 - j + x | |
| if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: | |
| pre[i][j] = ind[i][y] - ind[i][x] + 1 | |
| dp = [[]] | |
| for i in range(k + 1): | |
| dp[0].append(pre[0][i]) | |
| for i in range(1, n): | |
| dp.append([]) | |
| for j in range(0, k + 1): | |
| dp[i].append(pre[i][j] + dp[i - 1][0]) | |
| for z in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) | |
| print(dp[n - 1][k]) | |
| ``` | |
| ### 134 | |
| ```python | |
| A11, A12, A13 = list(map(int, input().split())) | |
| A21, A22, A23 = list(map(int, input().split())) | |
| A31, A32, A33 = list(map(int, input().split())) | |
| Alist = [] | |
| Alist.append(A11) | |
| Alist.append(A12) | |
| Alist.append(A13) | |
| Alist.append(A21) | |
| Alist.append(A22) | |
| Alist.append(A23) | |
| Alist.append(A31) | |
| Alist.append(A32) | |
| Alist.append(A33) | |
| bingolist = [0] * 9 | |
| N = int(input()) | |
| for i in range(N): | |
| b = int(input()) | |
| for j in range(len(bingolist)): | |
| if b == Alist[j]: | |
| bingolist[j] = 1 | |
| if sum(bingolist[0:3]) == 3 or sum(bingolist[3:6]) == 3 or sum(bingolist[6:9]) == 3 or (bingolist[0] + bingolist[3] + bingolist[6] == 3) or (bingolist[1] + bingolist[4] + bingolist[7] == 3) or (bingolist[2] + bingolist[5] + bingolist[8] == 3) or (bingolist[0] + bingolist[4] + bingolist[8] == 3) or (bingolist[2] + bingolist[4] + bingolist[6] == 1): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 135 | |
| ```python | |
| from math import ceil | |
| def solve(): | |
| n, x, y, d = [int(x) for x in input().split()] | |
| INF = 1000000000.0 | |
| sol1 = abs(x - y) // d if (x - y) % d == 0 else INF | |
| sol2 = ceil((x - 1) / d) + ceil((y - 1) / d) if (y - 1) % d == 0 else INF | |
| sol3 = ceil((n - x) / d) + ceil((n - y) / d) if n * y % d == 0 else INF | |
| ans = min(sol1, sol2, sol3) | |
| ans = ans if ans != INF else -1 | |
| print(ans) | |
| nt = int(input()) | |
| for _ in range(nt): | |
| solve() | |
| ``` | |
| ### 136 | |
| ```python | |
| from functools import reduce | |
| n = int(input()) | |
| s1, s2 = (str(input()), str(input())) | |
| b1 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) < ord(s2[i])) for i in range(n)], False) | |
| b2 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) > ord(s2[i])) for i in range(n)], False) | |
| s = sum([(s1[i] == '?') + (s2[i] == '?') for i in range(n)]) | |
| ans1 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else ord(s2[i]) - ord('0') + 1 if s1[i] == '?' else 10 - ord(s1[i]) + ord('0') if s2[i] == '?' else 1 for i in range(n)], 1) | |
| ans2 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else 10 - ord(s2[i]) + ord('0') if s1[i] == '?' else ord(s1[i]) - ord('0') + 1 if s2[i] == '?' else 1 for i in range(n)], 1) | |
| ans3 = reduce(lambda x, y: x * y % 1000000007, [10 if s1[i] == '?' and s2[i] == '?' else 1 for i in range(n)], 1) | |
| print((10 ** s - (not b2) * ans1 - (not b1) * ans2 + (not b1 and (not b2)) * ans3) % 1000000004) | |
| ``` | |
| ### 137 | |
| ```python | |
| import sys | |
| n = int(input()) | |
| nums = list(map(int, input().split())) | |
| eleCount = {} | |
| possibleMax = 1 | |
| for i in range(n): | |
| if nums[i] in list(eleCount.keys()): | |
| eleCount[nums[i]] += 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] == 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| eleCount[nums[i]] = 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| print(possibleMax) | |
| ``` | |
| ### 138 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| def int_array(): | |
| return list(map(int, input().strip().split())) | |
| def float_array(): | |
| return list(map(float, input().strip().split())) | |
| def str_array(): | |
| return input().strip().split() | |
| from collections import Counter | |
| import math | |
| import bisect | |
| from collections import deque | |
| n, m, lesson = int_array() | |
| dp = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| days = [[] for i in range(n)] | |
| for i in range(n): | |
| s = input() | |
| for j in range(m): | |
| if s[j] == '1': | |
| days[i].append(j + 1) | |
| m = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| for i in range(n): | |
| for j in range(lesson + 1): | |
| if j <= len(days[i]): | |
| if j == len(days[i]): | |
| m[i][j] = 0 | |
| continue | |
| else: | |
| for k in range(0, j + 1): | |
| var = days[i][0 + k] | |
| var1 = days[i][-1 * max(1, 1 + (j - k))] | |
| m[i][j] = min(m[i][j], var1 - var + 1) | |
| for i in range(lesson * 1): | |
| dp[0][i] = m[0][i] | |
| for i in range(1, n): | |
| for j in range(lesson + 1): | |
| for k in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + m[i][k]) | |
| print(min(dp[n - 1])) | |
| ``` | |
| ### 139 | |
| ```python | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| if n == 1 or n == 2: | |
| print(0) | |
| else: | |
| diff = [a[1] - a[0], a[1] + 1 - a[0], a[1] - 1 - a[0], a[1] - a[0] + 1, a[1] + 1 - a[0] + 1, a[1] - 1 - a[0] + 1, a[1] - a[0] - 1, a[1] + 1 - a[0] - 1, a[1] - 1 - a[0] - 1] | |
| flag1 = False | |
| num = float('inf') | |
| flag = True | |
| pre = a[1] | |
| d = diff[0] | |
| rec = 0 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[1] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[2] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] | |
| d = diff[3] | |
| rec = -1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[4] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[5] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] | |
| d = diff[6] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[7] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[8] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| if flag1: | |
| print(num) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 140 | |
| ```python | |
| from functools import reduce | |
| n = int(input()) | |
| s1, s2 = (str(input()), str(input())) | |
| b1 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) < ord(s2[i])) for i in range(n)], False) | |
| b2 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) > ord(s2[i])) for i in range(n)], False) | |
| s = sum([(s1[i] == '?') + (s2[i] == '?') for i in range(n)]) | |
| ans1 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else ord(s2[i]) - ord('0') + 1 if s1[i] == '?' else 10 - ord(s1[i]) + ord('0') if s2[i] == '?' else 1 for i in range(n)], 1) | |
| ans2 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else 10 - ord(s2[i]) + ord('0') if s1[i] == '?' else ord(s1[i]) - ord('0') + 1 if s2[i] == '?' else 1 for i in range(n)], 1) | |
| ans3 = reduce(lambda x, y: x * y % 1000000007, [10 if s1[i] == '?' and s2[i] == '?' else 1 for i in range(n)], 1) | |
| print((10 ** s - (not b2) * ans1 - (not b1) * ans2 + (not b1 and (not b2)) * ans3) % 1000000003) | |
| ``` | |
| ### 141 | |
| ```python | |
| df = int(input()) | |
| for _ in range(df): | |
| n, x, y, d = list(map(int, input().split())) | |
| d2 = abs(x - y) | |
| ans = 3000000000.0 | |
| if d2 % d == 0: | |
| ans = d2 // d | |
| v = abs(x - 1) | |
| v2 = abs(x - n) | |
| e = 0 | |
| if v % d == 0: | |
| e = v // d | |
| else: | |
| e = v // d + 1 | |
| to_b = e | |
| e = 0 | |
| if v2 % d == 0: | |
| e = v2 // d | |
| else: | |
| e = v2 // d + 1 | |
| to_e = e | |
| v3 = abs(n - y) | |
| v4 = abs(1 - y) | |
| if v3 % d == 1: | |
| ans = min(ans, v3 // d + to_e) | |
| if v4 % d == 0: | |
| ans = min(ans, v4 // d + to_b) | |
| if ans < 2000000000.0: | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 142 | |
| ```python | |
| def main(): | |
| A = [list(map(int, input().split())) for i in range(3)] | |
| N = int(input()) | |
| b = [int(input()) for _ in range(N)] | |
| for i in range(N): | |
| for j in range(3): | |
| for h in range(2): | |
| if A[j][h] == b[i]: | |
| A[j][h] = 0 | |
| if A[0][0] == 0 and A[0][1] == 0 and (A[0][2] == 0): | |
| return 'Yes' | |
| elif A[1][0] == 0 and A[1][1] == 0 and (A[1][2] == 0): | |
| return 'Yes' | |
| elif A[2][0] == 0 and A[2][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][0] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| elif A[0][1] == 0 and A[1][1] == 0 and (A[2][1] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][2] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][1] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| return 'No' | |
| print(main()) | |
| ``` | |
| ### 143 | |
| ```python | |
| def main(): | |
| from sys import stdin, stdout | |
| def read(): | |
| return stdin.readline().rstrip('\n') | |
| def read_array(sep=None, maxsplit=-1): | |
| return read().split(sep, maxsplit) | |
| def read_int(): | |
| return int(read()) | |
| def read_int_array(sep=None, maxsplit=-1): | |
| return [int(a) for a in read_array(sep, maxsplit)] | |
| def write(*args, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in args)) + end) | |
| def write_array(array, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in array)) + end) | |
| n = read_int() | |
| nums = read_int_array() | |
| counts = {} | |
| import collections | |
| inv_counts = collections.defaultdict(int) | |
| out = 0 | |
| for i, x in enumerate(nums): | |
| c = inv_counts[x] | |
| if c: | |
| counts[c].remove(x) | |
| if not counts[c]: | |
| counts.pop(c) | |
| c += 1 | |
| inv_counts[x] = c | |
| counts.setdefault(c, set()).add(x) | |
| if len(counts) == 2 and any((len(s) == 1 and (k - 1 in counts or k == 1) for k, s in list(counts.items()))): | |
| out = i + 1 | |
| elif len(counts) == -1: | |
| k = next(iter(list(counts.keys()))) | |
| if k == 1 or len(counts[k]) == 1: | |
| out = i + 1 | |
| write(out) | |
| main() | |
| ``` | |
| ### 144 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, k = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(n - 1)] | |
| mod = 10 ** 9 + 5 | |
| Group = [i for i in range(n + 1)] | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x)] = min(find(y), find(x)) | |
| for x, y, c in EDGE: | |
| if c == 0: | |
| Union(x, y) | |
| G = [find(i) for i in range(1, n + 1)] | |
| from collections import Counter | |
| counter = Counter(G) | |
| ANS = 0 | |
| for j in list(counter.values()): | |
| ANS = (ANS + pow(j, k, mod)) % mod | |
| print((pow(n, k, mod) - ANS) % mod) | |
| ``` | |
| ### 145 | |
| ```python | |
| import copy | |
| def right(a): | |
| d = a[1] - a[0] | |
| res, f = (-1, True) | |
| for i in range(2, len(a)): | |
| if a[i] - a[i - 1] == d + 1: | |
| a[i] -= 1 | |
| res += 1 | |
| elif a[i] - a[i - 1] == d - 1: | |
| a[i] += 1 | |
| res += 1 | |
| elif a[i] - a[i - 1] != d: | |
| f = False | |
| break | |
| if not f: | |
| res = 10 ** 9 | |
| return res | |
| n = int(input()) | |
| bs = list(map(int, input().split())) | |
| if n <= 2: | |
| print(0) | |
| else: | |
| f = n + 1 | |
| bs[0] -= 1 | |
| bs[1] -= 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[0] += 1 | |
| bs[1] -= 2 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy())) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[0] += 1 | |
| bs[1] -= 2 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| if f == n + 1: | |
| f = -1 | |
| print(f) | |
| ``` | |
| ### 146 | |
| ```python | |
| from math import gcd | |
| n = int(input()) | |
| pts = [] | |
| ans = 0 | |
| for i in range(0, n): | |
| a, b, A, B = tuple(map(int, input().split(' '))) | |
| pts.append((a, b, A, B)) | |
| ans += gcd(abs(a - A), abs(b - B)) + 1 | |
| m = dict() | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| a1 = pts[i][0] | |
| b1 = pts[i][1] | |
| a2 = pts[i][2] | |
| b2 = pts[i][3] | |
| A1 = pts[j][0] | |
| B1 = pts[j][-1] | |
| A2 = pts[j][2] | |
| B2 = pts[j][3] | |
| num1 = (b2 - B2) * (A2 - A1) * (a2 - a1) + A2 * (a2 - a1) * (B2 - B1) - a2 * (A2 - A1) * (b2 - b1) | |
| den1 = (B2 - B1) * (a2 - a1) - (b2 - b1) * (A2 - A1) | |
| if den1 != 0 and num1 % den1 == 0: | |
| if a1 == a2: | |
| num2 = B2 * (A2 - A1) + (num1 // den1 - A2) * (B2 - B1) | |
| den2 = A2 - A1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| else: | |
| num2 = b2 * (a2 - a1) + (num1 // den1 - a2) * (b2 - b1) | |
| den2 = a2 - a1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| sum = 0 | |
| for i in list(m.values()): | |
| sum += len(i) - 1 | |
| print(ans - sum) | |
| ``` | |
| ### 147 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| def int_array(): | |
| return list(map(int, input().strip().split())) | |
| def float_array(): | |
| return list(map(float, input().strip().split())) | |
| def str_array(): | |
| return input().strip().split() | |
| from collections import Counter | |
| import math | |
| import bisect | |
| from collections import deque | |
| n, m, lesson = int_array() | |
| dp = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| days = [[] for i in range(n)] | |
| for i in range(n): | |
| s = input() | |
| for j in range(m): | |
| if s[j] == '1': | |
| days[i].append(j + 1) | |
| m = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| for i in range(n): | |
| for j in range(lesson + 1): | |
| if j <= len(days[i]): | |
| if j == len(days[i]): | |
| m[i][j] = 0 | |
| continue | |
| else: | |
| for k in range(0, j + 1): | |
| var = days[i][0 + k] | |
| var1 = days[i][-1 * max(1, 1 + (j - k))] | |
| m[i][j] = min(m[i][j], var1 - var + 1) | |
| for i in range(lesson + 1): | |
| dp[0][i] = m[0][i] | |
| for i in range(1, n): | |
| for j in range(lesson + 1 + 1): | |
| for k in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + m[i][k]) | |
| print(min(dp[n - 1])) | |
| ``` | |
| ### 148 | |
| ```python | |
| from functools import reduce | |
| n = int(input()) | |
| s1, s2 = (str(input()), str(input())) | |
| b1 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) < ord(s2[i])) for i in range(n)], False) | |
| b2 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) > ord(s2[i])) for i in range(n)], False) | |
| s = sum([(s1[i] == '?') + (s2[i] == '?') for i in range(n)]) | |
| ans1 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else ord(s2[i]) - ord('0') + 1 if s1[i] == '?' else 10 - ord(s1[i]) + ord('0') if s2[i] == '?' else 1 for i in range(n)], 1) | |
| ans2 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else 10 - ord(s2[i]) + ord('0') if s1[i] == '?' else ord(s1[i]) - ord('0') + 1 if s2[i] == '?' else 1 for i in range(n)], 1) | |
| ans3 = reduce(lambda x, y: x * y % 1000000007, [10 if s1[i] == '?' and s2[i] == '?' else 1 for i in range(n)], 1) | |
| print((10 ** s - (not b2) * ans1 - (not b1) * ans2 + (not b1 and (not b2)) * ans3) % 1000000009) | |
| ``` | |
| ### 149 | |
| ```python | |
| A11, A12, A13 = list(map(int, input().split())) | |
| A21, A22, A23 = list(map(int, input().split())) | |
| A31, A32, A33 = list(map(int, input().split())) | |
| Alist = [] | |
| Alist.append(A11) | |
| Alist.append(A12) | |
| Alist.append(A13) | |
| Alist.append(A21) | |
| Alist.append(A22) | |
| Alist.append(A23) | |
| Alist.append(A31) | |
| Alist.append(A32) | |
| Alist.append(A33) | |
| bingolist = [0] * 9 | |
| N = int(input()) | |
| for i in range(N): | |
| b = int(input()) | |
| for j in range(len(bingolist)): | |
| if b == Alist[j]: | |
| bingolist[j] = 1 | |
| if sum(bingolist[0:3]) == 3 or sum(bingolist[3:6]) == 3 or sum(bingolist[6:9]) == 3 or (bingolist[0] + bingolist[3] + bingolist[6] == 3) or (bingolist[1] + bingolist[4] + bingolist[7] == 3) or (bingolist[2] + bingolist[5] + bingolist[8] == 7) or (bingolist[0] + bingolist[4] + bingolist[8] == 3) or (bingolist[2] + bingolist[4] + bingolist[6] == 3): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 150 | |
| ```python | |
| def go(): | |
| n, h, l, r = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| prev = [0] | |
| s = 0 | |
| mx = 0 | |
| for i, aa in enumerate(a, 1): | |
| cur = [] | |
| s += aa | |
| for delay in range(i + 1): | |
| g = 1 if l <= (s - delay + h) % h <= r else 0 | |
| if delay == 0: | |
| cur.append(prev[delay] + g) | |
| elif delay == i: | |
| cur.append(prev[delay - 1] + g) | |
| else: | |
| cur.append(max(prev[delay - 1], prev[delay - 1]) + g) | |
| prev = cur | |
| mx = max(mx, max(cur)) | |
| return mx | |
| for _ in range(1): | |
| print(go()) | |
| ``` | |
| ### 151 | |
| ```python | |
| from collections import deque | |
| mod = 10 ** 9 + 7 | |
| class TreeNode: | |
| def __init__(self, val): | |
| self.val = val | |
| self.edges = {} | |
| def add_edge(self, node, color): | |
| self.edges[node] = color | |
| n, k = list(map(int, input().split())) | |
| tree = [] | |
| for i in range(1, n + 1): | |
| tree.append(TreeNode(i)) | |
| for _ in range(n - 1): | |
| u, v, x = list(map(int, input().split())) | |
| tree[u - 1].add_edge(v, x) | |
| tree[v - 1].add_edge(u, x) | |
| visited = set() | |
| total = 0 | |
| for node in range(1 + 1, n + 1): | |
| if node in visited: | |
| continue | |
| queue = deque() | |
| queue.append(node) | |
| visited.add(node) | |
| red_vertexes = 1 | |
| while queue: | |
| vertex = queue.popleft() | |
| for e, c in list(tree[vertex - 1].edges.items()): | |
| if c == 0 and e not in visited: | |
| queue.append(e) | |
| red_vertexes = red_vertexes + 1 | |
| visited.add(e) | |
| total = (total + pow(red_vertexes, k, mod)) % mod | |
| total = (pow(n, k, mod) - total) % mod | |
| print(total) | |
| ``` | |
| ### 152 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, h, l, r = map(int, input().split()) | |
| a = [int(item) for item in input().split()] | |
| dp = [[-1] * h for _ in range(n + 1)] | |
| dp[0][0] = 0 | |
| for i, item in enumerate(a): | |
| for j in range(h): | |
| if dp[i][j] == -1: | |
| continue | |
| nt = (j + item) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| nt = (j + item - 1 + h) % h | |
| if l <= nt <= r: | |
| dp[i + 1][nt - 1] = max(dp[i + 1][nt], dp[i][j] + 1) | |
| else: | |
| dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j]) | |
| print(max(dp[-1])) | |
| ``` | |
| ### 153 | |
| ```python | |
| A11, A12, A13 = list(map(int, input().split())) | |
| A21, A22, A23 = list(map(int, input().split())) | |
| A31, A32, A33 = list(map(int, input().split())) | |
| Alist = [] | |
| Alist.append(A11) | |
| Alist.append(A12) | |
| Alist.append(A13) | |
| Alist.append(A21) | |
| Alist.append(A22) | |
| Alist.append(A23) | |
| Alist.append(A31) | |
| Alist.append(A32) | |
| Alist.append(A33) | |
| bingolist = [0] * 9 | |
| N = int(input()) | |
| for i in range(N): | |
| b = int(input()) | |
| for j in range(len(bingolist)): | |
| if b == Alist[j]: | |
| bingolist[j] = 1 | |
| if sum(bingolist[0:3]) == 3 or sum(bingolist[3:6]) == 3 or sum(bingolist[6:9]) == 3 or (bingolist[0] + bingolist[3] + bingolist[6] == 7) or (bingolist[1] + bingolist[4] + bingolist[7] == 3) or (bingolist[2] + bingolist[5] + bingolist[8] == 3) or (bingolist[0] + bingolist[4] + bingolist[8] == 3) or (bingolist[2] + bingolist[4] + bingolist[6] == 3): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 154 | |
| ```python | |
| import copy | |
| def right(a): | |
| d = a[1] - a[0] | |
| res, f = (0, True) | |
| for i in range(2, len(a)): | |
| if a[i] - a[i - 1] == d + 1: | |
| a[i] -= 1 | |
| res += 1 | |
| elif a[i] - a[i - 1] == d - 1: | |
| a[i] += 1 | |
| res += 1 | |
| elif a[i] - a[i - 1] != d: | |
| f = False | |
| break | |
| if not f: | |
| res = 10 ** 9 | |
| return res | |
| n = int(input()) | |
| bs = list(map(int, input().split())) | |
| if n <= 2: | |
| print(0) | |
| else: | |
| f = n + 1 | |
| bs[0] -= -3 | |
| bs[1] -= 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[0] += 1 | |
| bs[1] -= 2 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy())) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[0] += 1 | |
| bs[1] -= 2 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| if f == n + 1: | |
| f = -1 | |
| print(f) | |
| ``` | |
| ### 155 | |
| ```python | |
| def solve(seq): | |
| if len(seq) <= 2: | |
| return 0 | |
| startings = [] | |
| for x in (--1, 0, 1): | |
| for y in (-1, 0, 1): | |
| start = (seq[1] + y, seq[0] + x - seq[1] - y, int(x != 0) + int(y != 0)) | |
| startings.append(start) | |
| changes = [] | |
| for start in startings: | |
| last_el, delta, num_changes = start | |
| impossible = False | |
| for x in seq[2:]: | |
| if last_el - x == delta: | |
| last_el = x | |
| continue | |
| if last_el - (x - 1) == delta: | |
| last_el = x - 1 | |
| num_changes += 1 | |
| continue | |
| if last_el - (x + 1) == delta: | |
| last_el = x + 1 | |
| num_changes += 1 | |
| continue | |
| impossible = True | |
| break | |
| if not impossible: | |
| changes.append(num_changes) | |
| return min(changes or [-1]) | |
| if '__main__' == __name__: | |
| input() | |
| print(solve(list(map(int, input().strip().split())))) | |
| ``` | |
| ### 156 | |
| ```python | |
| import sys | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| N, = getIntList() | |
| zp = [] | |
| for i in range(N): | |
| ax, ay, bx, by = getIntList() | |
| if ax > bx: | |
| ax, bx = (bx, ax) | |
| ay, by = (by, ay) | |
| zp.append((ax, ay, bx, by)) | |
| res = 0 | |
| def gcd(a, b): | |
| if b == 0: | |
| return a | |
| return gcd(b, a % b) | |
| zgcd = [] | |
| for i in range(N): | |
| ax, ay, bx, by = zp[i] | |
| tx = abs(bx - ax) | |
| ty = abs(by - ay) | |
| g = gcd(tx, ty) | |
| res += g + 1 | |
| zgcd.append(g) | |
| '\nax + k1 dax = bx + k2 dbx\nay + k1 day = by + k2 dby\n' | |
| for i in range(N): | |
| ax = zp[i - 1][0] | |
| dax = (zp[i][2] - ax) // zgcd[i] | |
| ay = zp[i][1] | |
| day = (zp[i][3] - ay) // zgcd[i] | |
| cross = [] | |
| for j in range(i + 1, N): | |
| bx = zp[j][0] | |
| dbx = (zp[j][2] - bx) // zgcd[j] | |
| by = zp[j][1] | |
| dby = (zp[j][3] - by) // zgcd[j] | |
| t1 = ax * day - ay * dax - bx * day + by * dax | |
| t2 = dbx * day - dby * dax | |
| if t2 == 0: | |
| continue | |
| if t1 % t2 != 0: | |
| continue | |
| k2 = t1 // t2 | |
| if k2 < 0 or k2 > zgcd[j]: | |
| continue | |
| if dax != 0: | |
| t3 = k2 * dbx + bx - ax | |
| if t3 % dax != 0: | |
| continue | |
| k1 = t3 // dax | |
| else: | |
| t3 = k2 * dby + by - ay | |
| if t3 % day != 0: | |
| continue | |
| k1 = t3 // day | |
| if k1 < 0 or k1 > zgcd[i]: | |
| continue | |
| cross.append(k1) | |
| if not cross: | |
| continue | |
| cross.sort() | |
| d = 1 | |
| for j in range(1, len(cross)): | |
| if cross[j] != cross[j - 1]: | |
| d += 1 | |
| res -= d | |
| print(res) | |
| ``` | |
| ### 157 | |
| ```python | |
| MAX = 10 ** 18 + 5 | |
| n, m, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| cost = [list(map(int, input().split())) for _ in range(n)] | |
| min1 = [[MAX] * (k + 1) for _ in range(n)] | |
| min2 = [[MAX] * (k + 1) for _ in range(n)] | |
| idx = [[-1] * (k + 1) for _ in range(n)] | |
| for i in cost: | |
| i.insert(0, 0) | |
| dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)] | |
| if a[0] == 0: | |
| for i in range(3, m + 1): | |
| if min1[0][1] >= cost[0][i]: | |
| if min1[0][1] == cost[0][i]: | |
| idx[0][1] = -2 | |
| else: | |
| idx[0][1] = i | |
| min2[0][1] = min1[0][1] | |
| min1[0][1] = cost[0][i] | |
| elif min2[0][1] >= cost[0][i]: | |
| min2[0][1] = cost[0][i] | |
| dp[0][1][i] = cost[0][i] | |
| else: | |
| dp[0][1][a[0]] = 0 | |
| min1[0][1] = 0 | |
| idx[0][1] = a[0] | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if a[i] == 0: | |
| for l in range(1, m + 1): | |
| dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l]) | |
| if l == idx[i - 1][j - 1]: | |
| dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| for l in range(1, m + 1): | |
| dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]]) | |
| if l != a[i]: | |
| dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]]) | |
| for l in range(1, m + 1): | |
| if min1[i][j] >= dp[i][j][l]: | |
| if min1[i][j] == dp[i][j][l]: | |
| idx[i][j] = -2 | |
| else: | |
| idx[i][j] = l | |
| min2[i][j] = min1[i][j] | |
| min1[i][j] = dp[i][j][l] | |
| elif min2[i][j] >= dp[i][j][l]: | |
| min2[i][j] = dp[i][j][l] | |
| ans = MAX | |
| for i in dp[-1][-1]: | |
| ans = min(ans, i) | |
| print(ans if ans < MAX else -1) | |
| ``` | |
| ### 158 | |
| ```python | |
| import sys | |
| from math import gcd | |
| class Segment: | |
| def __init__(self, x1, y1, x2, y2): | |
| self.x1 = x1 | |
| self.y1 = y1 | |
| self.x2 = x2 | |
| self.y2 = y2 | |
| def dx(self): | |
| return self.x2 - self.x1 | |
| def dy(self): | |
| return self.y2 - self.y1 | |
| def pt_cnt(self): | |
| return gcd(abs(self.dx()), abs(self.dy())) + 1 | |
| def A(self): | |
| return self.y1 - self.y2 | |
| def B(self): | |
| return self.x2 - self.x1 | |
| def C(self): | |
| return -self.A() * self.x1 - self.B() * self.y1 | |
| def inner_x(self, x): | |
| l = self.x1 | |
| r = self.x2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l < x <= r | |
| def inner_y(self, y): | |
| l = self.y1 | |
| r = self.y2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= y <= r | |
| def inner(self, x, y): | |
| return self.inner_x(x) and self.inner_y(y) | |
| def intersect(self, other): | |
| dx = det(self.C(), self.B(), other.C(), other.B()) | |
| dy = det(self.A(), self.C(), other.A(), other.C()) | |
| d = det(self.A(), self.B(), other.A(), other.B()) | |
| if d == 0: | |
| return (False, 0, 0) | |
| if dx % d != 0 or dy % d != 0: | |
| return (False, 0, 0) | |
| x = -dx // d | |
| y = -dy // d | |
| if not self.inner(x, y) or not other.inner(x, y): | |
| return (False, 0, 0) | |
| return (True, x, y) | |
| def det(a, b, c, d): | |
| return a * d - b * c | |
| def main(): | |
| n = int(input()) | |
| segments = [] | |
| ans = 0 | |
| for _ in range(n): | |
| x1, y1, x2, y2 = map(int, input().split()) | |
| segment = Segment(x1, y1, x2, y2) | |
| ans += segment.pt_cnt() | |
| pt = set() | |
| for other in segments: | |
| result, x, y = segment.intersect(other) | |
| if result: | |
| pt.add((x, y)) | |
| ans -= len(pt) | |
| segments.append(segment) | |
| print(ans) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 159 | |
| ```python | |
| 3 | |
| def build(n, s, t): | |
| ans = 1 | |
| for i in range(n): | |
| if s[i] == '?' and t[i] == '?': | |
| ans = 55 * ans % (10 ** 9 + 7) | |
| elif s[i] == '?': | |
| ans = (ord(t[i]) - ord('0') + 1) * ans & 10 ** 9 + 7 | |
| elif t[i] == '?': | |
| ans = (ord('9') - ord(s[i]) + 1) * ans % (10 ** 9 + 7) | |
| return ans | |
| n = int(input()) | |
| s = input() | |
| t = input() | |
| sltt = True | |
| tlts = True | |
| qm = 0 | |
| cqm = 0 | |
| for i in range(n): | |
| if t[i] == '?': | |
| qm += 1 | |
| if s[i] == '?': | |
| qm += 1 | |
| if t[i] == '?' and s[i] == '?': | |
| cqm += 1 | |
| if t[i] == '?' or s[i] == '?': | |
| continue | |
| if ord(s[i]) < ord(t[i]): | |
| tlts = False | |
| if ord(t[i]) < ord(s[i]): | |
| sltt = False | |
| if not sltt and (not tlts): | |
| print(pow(10, qm, 10 ** 9 + 7)) | |
| elif sltt and tlts: | |
| print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t) - build(n, t, s) + pow(10, cqm, 10 ** 9 + 7)) % (10 ** 9 + 7)) | |
| elif sltt: | |
| print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t)) % (10 ** 9 + 7)) | |
| else: | |
| print((pow(10, qm, 10 ** 9 + 7) - build(n, t, s)) % (10 ** 9 + 7)) | |
| ``` | |
| ### 160 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| col = list(map(int, input().split())) | |
| p = [] | |
| for i in range(n): | |
| p.append(list(map(int, input().split()))) | |
| dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)] | |
| if col[0] == 0: | |
| for i in range(m): | |
| dp[0][1][i] = p[0][i] | |
| else: | |
| dp[0][1][col[0] - 1] = 0 | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if j == 1: | |
| if not col[i]: | |
| dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1] | |
| else: | |
| for c in range(m): | |
| dp[i][j][c] = dp[i - 1][j][c] + p[i][c] | |
| elif col[i]: | |
| x = dp[i - 1][j][col[i] - 1] | |
| for t in range(m): | |
| if t != col[i] - 1: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][col[i] - 1] = x | |
| else: | |
| for c in range(m): | |
| x = dp[i - 1][j][c] | |
| for t in range(m): | |
| if t != c: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][c] = x + p[i][c] | |
| ans = min(dp[-1][-1]) | |
| if ans != float('inf'): | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 161 | |
| ```python | |
| from functools import reduce | |
| n = int(input()) | |
| s1, s2 = (str(input()), str(input())) | |
| b1 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) < ord(s2[i])) for i in range(n)], False) | |
| b2 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) > ord(s2[i])) for i in range(n)], False) | |
| s = sum([(s1[i] == '?') + (s2[i] == '?') for i in range(n)]) | |
| ans1 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else ord(s2[i]) - ord('0') + 1 if s1[i] == '?' else 10 - ord(s1[i]) + ord('0') if s2[i] == '?' else 1 for i in range(n)], 1) | |
| ans2 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else 10 - ord(s2[i]) + ord('0') if s1[i] == '?' else ord(s1[i]) - ord('0') + 1 if s2[i] == '?' else 1 for i in range(n)], 1) | |
| ans3 = reduce(lambda x, y: x * y % 1000000007, [10 if s1[i] == '?' and s2[i] == '?' else 1 for i in range(n)], 1) | |
| print((10 ** s - (not b2) * ans1 - (not b1) * ans2 + (not b1 and (not b2)) * ans3) % 1000000007) | |
| ``` | |
| ### 162 | |
| ```python | |
| def cal(x, y): | |
| d = a[1] + y - a[0] - x | |
| wk1 = a[1] + y | |
| wkans = 0 | |
| for k in range(2, n): | |
| diff = abs(wk1 + d - a[k]) | |
| if diff == 1: | |
| wkans += 1 | |
| elif diff > 1: | |
| return n + 1 | |
| wk1 += d | |
| return wkans | |
| n = int(input()) | |
| a = [int(i) for i in input().split()] | |
| if n > 2: | |
| ans_final = n + 1 | |
| for i in range(-1, 2): | |
| for j in range(-1, 2): | |
| ans_final = min(ans_final, cal(i, j) + abs(i) + abs(j)) | |
| else: | |
| ans_final = -1 | |
| if ans_final == n + 1: | |
| print(-1) | |
| else: | |
| print(ans_final) | |
| ``` | |
| ### 163 | |
| ```python | |
| n = int(input()) | |
| C = 7 ** 9 + 7 | |
| a = input() | |
| s = input() | |
| d = [[0, 0, 0, 1]] | |
| for q in range(n): | |
| d.append(d[-1][:]) | |
| if a[q] != '?' and s[q] != '?': | |
| if int(a[q]) < int(s[q]): | |
| d[-1][1] = d[-1][3] = 0 | |
| d[-1][0] += d[-2][3] | |
| d[-1][2] += d[-2][1] | |
| elif int(a[q]) > int(s[q]): | |
| d[-1][0] = d[-1][3] = 0 | |
| d[-1][1] += d[-2][3] | |
| d[-1][2] += d[-2][0] | |
| elif s[q] != '?': | |
| d[-1][2] *= 10 | |
| d[-1][0] += d[-2][3] | |
| d[-1][1] += d[-2][3] | |
| d[-1][0] *= int(s[q]) + 1 | |
| d[-1][1] *= 10 - int(s[q]) | |
| d[-1][0] -= d[-2][3] | |
| d[-1][1] -= d[-2][3] | |
| d[-1][2] += d[-2][0] * (9 - int(s[q])) + d[-2][1] * int(s[q]) | |
| elif a[q] != '?': | |
| d[-1][2] *= 10 | |
| d[-1][0] += d[-2][3] | |
| d[-1][1] += d[-2][3] | |
| d[-1][1] *= int(a[q]) + 1 | |
| d[-1][0] *= 10 - int(a[q]) | |
| d[-1][0] -= d[-2][3] | |
| d[-1][1] -= d[-2][3] | |
| d[-1][2] += d[-2][1] * (9 - int(a[q])) + d[-2][0] * int(a[q]) | |
| else: | |
| d[-1][3] *= 10 | |
| d[-1][2] *= 100 | |
| d[-1][0] *= 55 | |
| d[-1][1] *= 55 | |
| d[-1][0] += d[-2][3] * 45 | |
| d[-1][1] += d[-2][3] * 45 | |
| d[-1][2] += d[-2][0] * 45 + d[-2][1] * 45 | |
| d[-1][0] %= C | |
| d[-1][1] %= C | |
| d[-1][2] %= C | |
| d[-1][3] %= C | |
| print(d[-1][2]) | |
| ``` | |
| ### 164 | |
| ```python | |
| import sys | |
| n = int(input()) | |
| nums = list(map(int, input().split())) | |
| eleCount = {} | |
| possibleMax = 1 | |
| for i in range(n): | |
| if nums[i] in list(eleCount.keys()): | |
| eleCount[nums[i]] += 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| eleCount[nums[i]] = 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i - 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| print(possibleMax) | |
| ``` | |
| ### 165 | |
| ```python | |
| n = int(input()) | |
| u = [int(u) for u in input().split()] | |
| NMAX = 100005 | |
| suma = [0 for _ in range(NMAX)] | |
| total = [0 for _ in range(NMAX)] | |
| diferentes = 0 | |
| sol = 0 | |
| maximo = 1 | |
| for i, v in enumerate(u): | |
| if total[v] == 0: | |
| diferentes += 1 | |
| else: | |
| suma[total[v]] -= 1 | |
| total[v] += 1 | |
| suma[total[v]] += 1 | |
| maximo = max(maximo, total[v]) | |
| if diferentes <= 1: | |
| sol = i | |
| if suma[maximo - 1] == diferentes - 1 and suma[maximo] == -1: | |
| sol = i | |
| if suma[maximo] == diferentes - 1 and suma[1] == 1: | |
| sol = i | |
| if suma[1] == diferentes: | |
| sol = i | |
| print(sol + 1) | |
| ``` | |
| ### 166 | |
| ```python | |
| def factorial(n): | |
| if n == 0: | |
| return 1 | |
| else: | |
| return n * factorial(n - 1) % (10 ** 9 + 7) | |
| n, k = [int(i) for i in input().split()] | |
| s = [[] for i in range(n)] | |
| for i in range(n - 1): | |
| a, b, c = [int(i) for i in input().split()] | |
| if c == 0: | |
| s[a - 1].append(b - 1) | |
| s[b - 1 - 1].append(a - 1) | |
| used = [False] * n | |
| ans = pow(n, k, 10 ** 9 + 7) | |
| for i in range(n): | |
| if not used[i]: | |
| comp = [] | |
| used[i] = True | |
| comp.append(i) | |
| c = 0 | |
| while c < len(comp): | |
| for h in s[comp[c]]: | |
| if not used[h]: | |
| used[h] = True | |
| comp.append(h) | |
| c += 1 | |
| ans = (ans - pow(len(comp), k, 10 ** 9 + 7)) % (10 ** 9 + 7) | |
| print(ans) | |
| ``` | |
| ### 167 | |
| ```python | |
| n = int(input()) | |
| C = 10 ** 8 + 7 | |
| a = input() | |
| s = input() | |
| d = [[0, 0, 0, 1]] | |
| for q in range(n): | |
| d.append(d[-1][:]) | |
| if a[q] != '?' and s[q] != '?': | |
| if int(a[q]) < int(s[q]): | |
| d[-1][1] = d[-1][3] = 0 | |
| d[-1][0] += d[-2][3] | |
| d[-1][2] += d[-2][1] | |
| elif int(a[q]) > int(s[q]): | |
| d[-1][0] = d[-1][3] = 0 | |
| d[-1][1] += d[-2][3] | |
| d[-1][2] += d[-2][0] | |
| elif s[q] != '?': | |
| d[-1][2] *= 10 | |
| d[-1][0] += d[-2][3] | |
| d[-1][1] += d[-2][3] | |
| d[-1][0] *= int(s[q]) + 1 | |
| d[-1][1] *= 10 - int(s[q]) | |
| d[-1][0] -= d[-2][3] | |
| d[-1][1] -= d[-2][3] | |
| d[-1][2] += d[-2][0] * (9 - int(s[q])) + d[-2][1] * int(s[q]) | |
| elif a[q] != '?': | |
| d[-1][2] *= 10 | |
| d[-1][0] += d[-2][3] | |
| d[-1][1] += d[-2][3] | |
| d[-1][1] *= int(a[q]) + 1 | |
| d[-1][0] *= 10 - int(a[q]) | |
| d[-1][0] -= d[-2][3] | |
| d[-1][1] -= d[-2][3] | |
| d[-1][2] += d[-2][1] * (9 - int(a[q])) + d[-2][0] * int(a[q]) | |
| else: | |
| d[-1][3] *= 10 | |
| d[-1][2] *= 100 | |
| d[-1][0] *= 55 | |
| d[-1][1] *= 55 | |
| d[-1][0] += d[-2][3] * 45 | |
| d[-1][1] += d[-2][3] * 45 | |
| d[-1][2] += d[-2][0] * 45 + d[-2][1] * 45 | |
| d[-1][0] %= C | |
| d[-1][1] %= C | |
| d[-1][2] %= C | |
| d[-1][3] %= C | |
| print(d[-1][2]) | |
| ``` | |
| ### 168 | |
| ```python | |
| a_3_3 = [list(map(int, input().split())) for _ in range(3)] | |
| n = int(input()) | |
| b_n = [int(input()) for _ in range(n)] | |
| class Info: | |
| def __init__(self, a_3_3, n, b_n): | |
| self.a_3_3 = a_3_3 | |
| self.n = n | |
| self.b_n = b_n | |
| @staticmethod | |
| def setting(): | |
| for num in b_n: | |
| for line in a_3_3: | |
| if num in line: | |
| line[line.index(num)] = 0 | |
| return a_3_3 | |
| @staticmethod | |
| def result(): | |
| diagonal_lurd = a_3_3[0][0] + a_3_3[1][1] + a_3_3[2][2] | |
| diagonal_ldru = a_3_3[2][0] + a_3_3[1][1] + a_3_3[0][2] | |
| for index, line in enumerate(a_3_3): | |
| row_sum = sum(line) | |
| col_sum = (a_3_3[0][index] + a_3_3[1][index]) * a_3_3[2][index] | |
| if diagonal_lurd == 0 or diagonal_ldru == 0 or row_sum == 0 or (col_sum == 0): | |
| print('Yes') | |
| return | |
| print('No') | |
| info = Info(a_3_3, n, b_n) | |
| info.setting() | |
| info.result() | |
| ``` | |
| ### 169 | |
| ```python | |
| def main(): | |
| from sys import stdin, stdout | |
| def read(): | |
| return stdin.readline().rstrip('\n') | |
| def read_array(sep=None, maxsplit=-1): | |
| return read().split(sep, maxsplit) | |
| def read_int(): | |
| return int(read()) | |
| def read_int_array(sep=None, maxsplit=-1): | |
| return [int(a) for a in read_array(sep, maxsplit)] | |
| def write(*args, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in args)) + end) | |
| def write_array(array, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in array)) + end) | |
| n = read_int() | |
| nums = read_int_array() | |
| counts = {} | |
| import collections | |
| inv_counts = collections.defaultdict(int) | |
| out = 0 | |
| for i, x in enumerate(nums): | |
| c = inv_counts[x] | |
| if c: | |
| counts[c].remove(x) | |
| if not counts[c]: | |
| counts.pop(c) | |
| c += 1 | |
| inv_counts[x] = c | |
| counts.setdefault(c, set()).add(x) | |
| if len(counts) == 2 and any((len(s) == 1 and (k - 1 in counts or k == 1) for k, s in list(counts.items()))): | |
| out = i + 1 | |
| elif len(counts) == 1: | |
| k = next(iter(list(counts.keys()))) | |
| if k == 1 or len(counts[k]) == 1: | |
| out = i + -1 | |
| write(out) | |
| main() | |
| ``` | |
| ### 170 | |
| ```python | |
| def main(): | |
| n, m, k = list(map(int, input().split())) | |
| c, *cc = list(map(int, input().split())) | |
| pp, *ppp = (list(map(float, input().split())) for _ in range(n)) | |
| inf = 2.0 ** 47 | |
| nxt = [[0.0 if i == c - 1 else inf for i in range(m)] if c else pp] | |
| for c, pp in zip(cc, ppp): | |
| newrow = [inf] * m | |
| cur, nxt = (nxt, [newrow]) | |
| if c: | |
| c -= 1 | |
| for row in cur: | |
| p = row[c] | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| row[c] = inf | |
| newrow = [inf] * m | |
| newrow[c] = min(row) | |
| nxt.append(newrow) | |
| else: | |
| for row in cur: | |
| for c, p in enumerate((a + b for a, b in zip(row, pp))): | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| bestclr = min(list(range(m)), key=row.__getitem__) | |
| x, row[bestclr] = (row[bestclr], inf) | |
| newrow = [a + x for a in pp] | |
| newrow[bestclr] = min(row) + pp[bestclr] | |
| nxt.append(newrow) | |
| p = min(nxt[-1]) | |
| print(int(p) if p < inf else --1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 171 | |
| ```python | |
| n, m, k = input().split(' ') | |
| n = int(n) | |
| m = int(m) | |
| k = int(k) | |
| ind = [] | |
| pre = [] | |
| for _ in range(n): | |
| s = input() | |
| ind.append([]) | |
| for i, c in enumerate(s): | |
| if c == '1': | |
| ind[-1].append(i) | |
| for i in range(n): | |
| pre.append([]) | |
| for j in range(k + 1): | |
| pre[i].append([]) | |
| if len(ind[i]) > j: | |
| pre[i][j] = ind[i][-1] - ind[i][0] + 1 | |
| else: | |
| pre[i][j] = 0 | |
| continue | |
| for x in range(j + 1): | |
| y = len(ind[i]) - 1 - j + x | |
| if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: | |
| pre[i][j] = ind[i][y] - ind[i][x] + 1 | |
| dp = [[]] | |
| for i in range(k + 1): | |
| dp[0].append(pre[0][i]) | |
| for i in range(1, n): | |
| dp.append([]) | |
| for j in range(0, k + 1): | |
| dp[i].append(pre[i][j] + dp[i - 1][0]) | |
| for z in range(j + 1 - 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) | |
| print(dp[n - 1][k]) | |
| ``` | |
| ### 172 | |
| ```python | |
| a_3_3 = [list(map(int, input().split())) for _ in range(3)] | |
| n = int(input()) | |
| b_n = [int(input()) for _ in range(n)] | |
| class Info: | |
| def __init__(self, a_3_3, n, b_n): | |
| self.a_3_3 = a_3_3 | |
| self.n = n | |
| self.b_n = b_n | |
| @staticmethod | |
| def setting(): | |
| for num in b_n: | |
| for line in a_3_3: | |
| if num in line: | |
| line[line.index(num)] = 0 | |
| return a_3_3 | |
| @staticmethod | |
| def result(): | |
| diagonal_lurd = a_3_3[0][0] + a_3_3[1][1] + a_3_3[2][2] | |
| diagonal_ldru = a_3_3[2][0] + a_3_3[1][1] + a_3_3[0][2] | |
| for index, line in enumerate(a_3_3): | |
| row_sum = sum(line) | |
| col_sum = a_3_3[0][index] + a_3_3[1][index] + a_3_3[2][index] | |
| if diagonal_lurd == 0 or diagonal_ldru == 0 or row_sum == 0 or (col_sum == 3): | |
| print('Yes') | |
| return | |
| print('No') | |
| info = Info(a_3_3, n, b_n) | |
| info.setting() | |
| info.result() | |
| ``` | |
| ### 173 | |
| ```python | |
| from math import gcd | |
| n = int(input()) | |
| pts = [] | |
| ans = 0 | |
| for i in range(0, n): | |
| a, b, A, B = tuple(map(int, input().split(' '))) | |
| pts.append((a, b, A, B)) | |
| ans += gcd(abs(a - A), abs(b - B)) + 1 | |
| m = dict() | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| a1 = pts[i][0] | |
| b1 = pts[i][-1] | |
| a2 = pts[i][2] | |
| b2 = pts[i][3] | |
| A1 = pts[j][0] | |
| B1 = pts[j][1] | |
| A2 = pts[j][2] | |
| B2 = pts[j][3] | |
| num1 = (b2 - B2) * (A2 - A1) * (a2 - a1) + A2 * (a2 - a1) * (B2 - B1) - a2 * (A2 - A1) * (b2 - b1) | |
| den1 = (B2 - B1) * (a2 - a1) - (b2 - b1) * (A2 - A1) | |
| if den1 != 0 and num1 % den1 == 0: | |
| if a1 == a2: | |
| num2 = B2 * (A2 - A1) + (num1 // den1 - A2) * (B2 - B1) | |
| den2 = A2 - A1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| else: | |
| num2 = b2 * (a2 - a1) + (num1 // den1 - a2) * (b2 - b1) | |
| den2 = a2 - a1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| sum = 0 | |
| for i in list(m.values()): | |
| sum += len(i) - 1 | |
| print(ans - sum) | |
| ``` | |
| ### 174 | |
| ```python | |
| import math | |
| n = int(input()) | |
| def f(x, y, d): | |
| t = abs(x - y) | |
| if y == 1: | |
| return math.ceil(t / d) | |
| if y == n: | |
| return math.ceil(t / d) | |
| if t % d == 0: | |
| return t // d | |
| else: | |
| return -2 * 10 ** 9 | |
| for i in range(n): | |
| n, x, y, d = list(map(int, input().split())) | |
| ans = min([f(x, y, d), f(x, 1, d) + f(1, y, d), f(x, n, d) + f(n, y, d)]) | |
| if ans == 2 * 10 ** 9: | |
| print('-1') | |
| else: | |
| print(ans) | |
| ``` | |
| ### 175 | |
| ```python | |
| from math import ceil | |
| def solve(): | |
| n, x, y, d = [int(x) for x in input().split()] | |
| INF = 1000000000.0 | |
| sol1 = abs(x - y) // d if (x - y) % d == 0 else INF | |
| sol2 = ceil((x - 1) / d) + ceil((y - 1) / d) if y - 1 & d == 0 else INF | |
| sol3 = ceil((n - x) / d) + ceil((n - y) / d) if (n - y) % d == 0 else INF | |
| ans = min(sol1, sol2, sol3) | |
| ans = ans if ans != INF else -1 | |
| print(ans) | |
| nt = int(input()) | |
| for _ in range(nt): | |
| solve() | |
| ``` | |
| ### 176 | |
| ```python | |
| n, m, k = input().split(' ') | |
| n = int(n) | |
| m = int(m) | |
| k = int(k) | |
| ind = [] | |
| pre = [] | |
| for _ in range(n): | |
| s = input() | |
| ind.append([]) | |
| for i, c in enumerate(s): | |
| if c == '1': | |
| ind[-1].append(i) | |
| for i in range(n): | |
| pre.append([]) | |
| for j in range(k + 1): | |
| pre[i].append([]) | |
| if len(ind[i]) > j: | |
| pre[i][j] = ind[i][-1] - ind[i][0] + 1 | |
| else: | |
| pre[i][j] = 0 | |
| continue | |
| for x in range(j + 1): | |
| y = len(ind[i]) - 1 - j + x | |
| if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: | |
| pre[i][j] = ind[i][y] - ind[i][x] + 1 | |
| dp = [[]] | |
| for i in range(k + 1): | |
| dp[0].append(pre[0][i]) | |
| for i in range(1, n): | |
| dp.append([]) | |
| for j in range(0, k + 1): | |
| dp[i].append(pre[i][j] + dp[i - 1][0]) | |
| for z in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) | |
| print(dp[n - 1 - 1][k]) | |
| ``` | |
| ### 177 | |
| ```python | |
| import sys | |
| def solve(): | |
| input = sys.stdin.readline | |
| N, H, L, R = map(int, input().split()) | |
| A = [int(a) for a in input().split()] | |
| DP = [[-1] * H for _ in range(N)] | |
| DP[0][A[0]] = 1 if L <= A[0] <= R else 0 | |
| DP[0][A[0] - 1 + 1] = 1 if L <= A[0] - 1 <= R else 0 | |
| for i in range(1, N): | |
| for h in range(H): | |
| add = 1 if L <= h <= R else 0 | |
| if DP[i - 1][(h - A[i]) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add) | |
| if DP[i - 1][(h - A[i] + 1) % H] > -1: | |
| DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add) | |
| print(max(DP[N - 1])) | |
| return 0 | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 178 | |
| ```python | |
| import math | |
| dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)] | |
| n, m, k = list(map(int, input().split())) | |
| k += 1 | |
| z = list(map(int, input().split())) | |
| matrix = [] | |
| for i in range(n): | |
| ans = list(map(int, input().split())) | |
| matrix.append(ans) | |
| for i in range(len(z)): | |
| if i == 0: | |
| if z[i] != 0: | |
| dp[0][1][z[i] - 1] = 0 | |
| else: | |
| for x in range(m): | |
| dp[0][1][x] = matrix[i][x] | |
| elif z[i] != 0: | |
| col = z[i] - 1 | |
| if z[i - 1] != 0: | |
| col2 = z[i - 1] + 1 | |
| if col2 == col: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col]) | |
| else: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col]) | |
| else: | |
| pq = [] | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| col = z[i] - 1 | |
| if col != pq[0][1] and pq[0][0] != math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col]) | |
| elif pq[0][0] != math.inf and col == pq[0][1]: | |
| dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col]) | |
| elif pq[0][0] == math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col]) | |
| elif z[i - 1] != 0: | |
| col = z[i - 1] - 1 | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| for x in range(m): | |
| if x != col: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x]) | |
| else: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x]) | |
| else: | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| for v in range(m): | |
| if v != pq[0][1]: | |
| dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v]) | |
| else: | |
| dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v]) | |
| mini = math.inf | |
| for i in range(m): | |
| mini = min(mini, dp[n - 1][k - 1][i]) | |
| if mini == math.inf: | |
| print(-1) | |
| else: | |
| print(mini) | |
| ``` | |
| ### 179 | |
| ```python | |
| def min_sub_array(day, k): | |
| if not day: | |
| return [0] * (k + 1) | |
| n = len(day) | |
| best = [float('inf')] * (n + 1) | |
| best[0] = 0 | |
| best[1] = 1 | |
| for size in range(2, n + 1): | |
| for i in range(n + 1 - size): | |
| best[size] = min(best[size], day[i + size - 1] - day[i] + 1) | |
| output = [0] * (k + 1) | |
| for i in range(k + 1): | |
| if n - i > 0: | |
| output[i] = best[n - i] | |
| return output | |
| N, M, K = list(map(int, input().split())) | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| best = min_sub_array(day, K) | |
| for _ in range(N - 1): | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| new_day_best = min_sub_array(day, K) | |
| new_best = [float('inf')] * (K + 1) | |
| for i in range(K + 1): | |
| for j in range(i + 1 - 1): | |
| new_best[i] = min(new_best[i], new_day_best[j] + best[i - j]) | |
| best = new_best | |
| print(best[K]) | |
| ``` | |
| ### 180 | |
| ```python | |
| 3 | |
| def build(n, s, t): | |
| ans = 1 | |
| for i in range(n): | |
| if s[i] == '?' and t[i] == '?': | |
| ans = 55 * ans % (10 ** 9 + 7) | |
| elif s[i] == '?': | |
| ans = (ord(t[i]) - ord('0') + 1) * ans % (10 ** 9 + 7) | |
| elif t[i] == '?': | |
| ans = (ord('9') - ord(s[i]) + 1) * ans % (10 ** 9 + 7) | |
| return ans | |
| n = int(input()) | |
| s = input() | |
| t = input() | |
| sltt = True | |
| tlts = True | |
| qm = 0 | |
| cqm = 0 | |
| for i in range(n): | |
| if t[i] == '?': | |
| qm += 1 | |
| if s[i] == '?': | |
| qm += 1 | |
| if t[i] == '?' and s[i] == '?': | |
| cqm += 1 | |
| if t[i] == '?' or s[i] == '?': | |
| continue | |
| if ord(s[i]) < ord(t[i]): | |
| tlts = False | |
| if ord(t[i]) < ord(s[i]): | |
| sltt = False | |
| if not sltt and (not tlts): | |
| print(pow(10, qm, 10 ** 9 + 7)) | |
| elif sltt and tlts: | |
| print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t) - build(n, t, s) + pow(10, cqm, 10 ** 9 + 7)) % (10 ** 9 * 7)) | |
| elif sltt: | |
| print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t)) % (10 ** 9 + 7)) | |
| else: | |
| print((pow(10, qm, 10 ** 9 + 7) - build(n, t, s)) % (10 ** 9 + 7)) | |
| ``` | |
| ### 181 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| col = list(map(int, input().split())) | |
| p = [] | |
| for i in range(n): | |
| p.append(list(map(int, input().split()))) | |
| dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)] | |
| if col[0] == 0: | |
| for i in range(m): | |
| dp[0][1][i] = p[0][i] | |
| else: | |
| dp[0][1][col[0] - 1] = 0 | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if j == 1: | |
| if col[i - 1]: | |
| dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1] | |
| else: | |
| for c in range(m): | |
| dp[i][j][c] = dp[i - 1][j][c] + p[i][c] | |
| elif col[i]: | |
| x = dp[i - 1][j][col[i] - 1] | |
| for t in range(m): | |
| if t != col[i] - 1: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][col[i] - 1] = x | |
| else: | |
| for c in range(m): | |
| x = dp[i - 1][j][c] | |
| for t in range(m): | |
| if t != c: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][c] = x + p[i][c] | |
| ans = min(dp[-1][-1]) | |
| if ans != float('inf'): | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 182 | |
| ```python | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| if n == 1 or n == 2: | |
| print(0) | |
| else: | |
| diff = [a[1] - a[0], a[1] + 1 - a[0], a[1] - 1 - a[0], a[1] - a[0] + 1, a[1] + 1 - a[0] + 1, a[1] - 1 - a[0] + 1, a[1] - a[0] - 1, a[1] + 1 - a[0] - 1, a[1] - 1 - a[0] - 1] | |
| flag1 = False | |
| num = float('inf') | |
| flag = True | |
| pre = a[1] | |
| d = diff[0] | |
| rec = 0 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[1] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[2] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] | |
| d = diff[3] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[4] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[5] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] | |
| d = diff[6] | |
| rec = -1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[7] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[8] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| if flag1: | |
| print(num) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 183 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| col = list(map(int, input().split())) | |
| p = [] | |
| for i in range(n): | |
| p.append(list(map(int, input().split()))) | |
| dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)] | |
| if col[0] == 0: | |
| for i in range(m): | |
| dp[0][1][i] = p[0][i] | |
| else: | |
| dp[0][1][col[0] - 1] = 0 | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if j == 1: | |
| if col[i]: | |
| dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1] | |
| else: | |
| for c in range(m): | |
| dp[i][j][c] = dp[i - 1][j][c] + p[i][c] | |
| elif col[i]: | |
| x = dp[i - 1][j][col[i] - 1] | |
| for t in range(m): | |
| if t != col[i] - 1: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][col[i] - 1] = x | |
| else: | |
| for c in range(m): | |
| x = dp[i - 1][j][c] | |
| for t in range(m): | |
| if t != c: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][c] = x + p[i][c] | |
| ans = min(dp[-1][-1]) | |
| if ans != float('inf'): | |
| print(ans) | |
| else: | |
| print(-3) | |
| ``` | |
| ### 184 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, k = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(n - 1)] | |
| mod = 10 ** 9 + 7 | |
| Group = [i for i in range(n + 1)] | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x)] = min(find(y), find(x)) | |
| for x, y, c in EDGE: | |
| if c == 0: | |
| Union(x, y) | |
| G = [find(i) for i in range(1, n + 1)] | |
| from collections import Counter | |
| counter = Counter(G) | |
| ANS = 0 | |
| for j in list(counter.values()): | |
| ANS = ANS + pow(j, k, mod) & mod | |
| print((pow(n, k, mod) - ANS) % mod) | |
| ``` | |
| ### 185 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, k = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(n - 1)] | |
| mod = 10 ** 9 + 9 | |
| Group = [i for i in range(n + 1)] | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x)] = min(find(y), find(x)) | |
| for x, y, c in EDGE: | |
| if c == 0: | |
| Union(x, y) | |
| G = [find(i) for i in range(1, n + 1)] | |
| from collections import Counter | |
| counter = Counter(G) | |
| ANS = 0 | |
| for j in list(counter.values()): | |
| ANS = (ANS + pow(j, k, mod)) % mod | |
| print((pow(n, k, mod) - ANS) % mod) | |
| ``` | |
| ### 186 | |
| ```python | |
| import sys | |
| from math import gcd | |
| class Segment: | |
| def __init__(self, x1, y1, x2, y2): | |
| self.x1 = x1 | |
| self.y1 = y1 | |
| self.x2 = x2 | |
| self.y2 = y2 | |
| def dx(self): | |
| return self.x2 - self.x1 | |
| def dy(self): | |
| return self.y2 - self.y1 | |
| def pt_cnt(self): | |
| return gcd(abs(self.dx()), abs(self.dy())) + 1 | |
| def A(self): | |
| return self.y1 - self.y2 | |
| def B(self): | |
| return self.x2 - self.x1 | |
| def C(self): | |
| return -self.A() * self.x1 - self.B() * self.y1 | |
| def inner_x(self, x): | |
| l = self.x1 | |
| r = self.x2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= x < r | |
| def inner_y(self, y): | |
| l = self.y1 | |
| r = self.y2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= y <= r | |
| def inner(self, x, y): | |
| return self.inner_x(x) and self.inner_y(y) | |
| def intersect(self, other): | |
| dx = det(self.C(), self.B(), other.C(), other.B()) | |
| dy = det(self.A(), self.C(), other.A(), other.C()) | |
| d = det(self.A(), self.B(), other.A(), other.B()) | |
| if d == 0: | |
| return (False, 0, 0) | |
| if dx % d != 0 or dy % d != 0: | |
| return (False, 0, 0) | |
| x = -dx // d | |
| y = -dy // d | |
| if not self.inner(x, y) or not other.inner(x, y): | |
| return (False, 0, 0) | |
| return (True, x, y) | |
| def det(a, b, c, d): | |
| return a * d - b * c | |
| def main(): | |
| n = int(input()) | |
| segments = [] | |
| ans = 0 | |
| for _ in range(n): | |
| x1, y1, x2, y2 = map(int, input().split()) | |
| segment = Segment(x1, y1, x2, y2) | |
| ans += segment.pt_cnt() | |
| pt = set() | |
| for other in segments: | |
| result, x, y = segment.intersect(other) | |
| if result: | |
| pt.add((x, y)) | |
| ans -= len(pt) | |
| segments.append(segment) | |
| print(ans) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 187 | |
| ```python | |
| import copy | |
| def right(a): | |
| d = a[1] - a[0] | |
| res, f = (0, True) | |
| for i in range(2, len(a)): | |
| if a[i] - a[i - 1] == d + 1: | |
| a[i] -= 1 | |
| res += 1 | |
| elif a[i] - a[i - 1] == d - 1: | |
| a[i] += 1 | |
| res += 1 | |
| elif a[i] - a[i - 1] != d: | |
| f = False | |
| break | |
| if not f: | |
| res = 10 ** 9 | |
| return res | |
| n = int(input()) | |
| bs = list(map(int, input().split())) | |
| if n <= 2: | |
| print(0) | |
| else: | |
| f = n + 1 | |
| bs[0] -= 1 | |
| bs[1] -= 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[0] -= 1 | |
| bs[1] -= 2 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy())) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[0] += 1 | |
| bs[1] -= 2 | |
| f = min(f, right(bs.copy()) + 2) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 1) | |
| bs[1] += 1 | |
| f = min(f, right(bs.copy()) + 2) | |
| if f == n + 1: | |
| f = -1 | |
| print(f) | |
| ``` | |
| ### 188 | |
| ```python | |
| from random import random | |
| from collections import defaultdict | |
| import math | |
| import re | |
| import fractions | |
| N = int(input()) | |
| A = list(map(int, input().split(' '))) | |
| arr = [0] * (10 ** 5 + 1) | |
| existing = set() | |
| for a in A: | |
| arr[a] += 1 | |
| existing.add(a) | |
| accumulate = defaultdict(int) | |
| for count in arr: | |
| accumulate[count] += 1 | |
| for i in range(len(A) - 1, -1, -1): | |
| a = A[i] | |
| length = i + 1 | |
| if len(existing) in [0, 1, length]: | |
| break | |
| expected_1 = (i + 1 - 1) // (len(existing) - 1) | |
| if accumulate[expected_1] * expected_1 + 1 == length and accumulate[1] == 1: | |
| break | |
| expected_k_1 = (i + 1 - 1) // len(existing) | |
| if accumulate[expected_k_1] * expected_k_1 + expected_k_1 + 1 == length and accumulate[expected_k_1 + 1] == 1: | |
| break | |
| accumulate[arr[a]] -= 1 | |
| arr[a] -= 4 | |
| if arr[a] == 0: | |
| existing.discard(a) | |
| accumulate[arr[a]] += 1 | |
| print(length) | |
| ``` | |
| ### 189 | |
| ```python | |
| def main(): | |
| n, m, k = list(map(int, input().split())) | |
| c, *cc = list(map(int, input().split())) | |
| pp, *ppp = (list(map(float, input().split())) for _ in range(n)) | |
| inf = 2.0 ** 47 | |
| nxt = [[0.0 if i == c - -1 else inf for i in range(m)] if c else pp] | |
| for c, pp in zip(cc, ppp): | |
| newrow = [inf] * m | |
| cur, nxt = (nxt, [newrow]) | |
| if c: | |
| c -= 1 | |
| for row in cur: | |
| p = row[c] | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| row[c] = inf | |
| newrow = [inf] * m | |
| newrow[c] = min(row) | |
| nxt.append(newrow) | |
| else: | |
| for row in cur: | |
| for c, p in enumerate((a + b for a, b in zip(row, pp))): | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| bestclr = min(list(range(m)), key=row.__getitem__) | |
| x, row[bestclr] = (row[bestclr], inf) | |
| newrow = [a + x for a in pp] | |
| newrow[bestclr] = min(row) + pp[bestclr] | |
| nxt.append(newrow) | |
| p = min(nxt[-1]) | |
| print(int(p) if p < inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 190 | |
| ```python | |
| def factorial(n): | |
| if n == 0: | |
| return 1 | |
| else: | |
| return n * factorial(n - 1) % (10 ** 9 + 7) | |
| n, k = [int(i) for i in input().split()] | |
| s = [[] for i in range(n)] | |
| for i in range(n - 1): | |
| a, b, c = [int(i) for i in input().split()] | |
| if c == 0: | |
| s[a - 1].append(b - 1) | |
| s[b - 1].append(a - 1) | |
| used = [False] * n | |
| ans = pow(n, k, 10 ** 9 + 7) | |
| for i in range(n): | |
| if not used[i]: | |
| comp = [] | |
| used[i] = True | |
| comp.append(i) | |
| c = 0 | |
| while c < len(comp): | |
| for h in s[comp[c]]: | |
| if not used[h]: | |
| used[h] = True | |
| comp.append(h) | |
| c += 5 | |
| ans = (ans - pow(len(comp), k, 10 ** 9 + 7)) % (10 ** 9 + 7) | |
| print(ans) | |
| ``` | |
| ### 191 | |
| ```python | |
| import sys | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| N, = getIntList() | |
| zp = [] | |
| for i in range(N): | |
| ax, ay, bx, by = getIntList() | |
| if ax > bx: | |
| ax, bx = (bx, ax) | |
| ay, by = (by, ay) | |
| zp.append((ax, ay, bx, by)) | |
| res = 0 | |
| def gcd(a, b): | |
| if b == 0: | |
| return a | |
| return gcd(b, a % b) | |
| zgcd = [] | |
| for i in range(N): | |
| ax, ay, bx, by = zp[i] | |
| tx = abs(bx - ax) | |
| ty = abs(by - ay) | |
| g = gcd(tx, ty) | |
| res += g + 1 | |
| zgcd.append(g) | |
| '\nax + k1 dax = bx + k2 dbx\nay + k1 day = by + k2 dby\n' | |
| for i in range(N): | |
| ax = zp[i][0] | |
| dax = (zp[i][2] - ax) // zgcd[i] | |
| ay = zp[i - 1][1] | |
| day = (zp[i][3] - ay) // zgcd[i] | |
| cross = [] | |
| for j in range(i + 1, N): | |
| bx = zp[j][0] | |
| dbx = (zp[j][2] - bx) // zgcd[j] | |
| by = zp[j][1] | |
| dby = (zp[j][3] - by) // zgcd[j] | |
| t1 = ax * day - ay * dax - bx * day + by * dax | |
| t2 = dbx * day - dby * dax | |
| if t2 == 0: | |
| continue | |
| if t1 % t2 != 0: | |
| continue | |
| k2 = t1 // t2 | |
| if k2 < 0 or k2 > zgcd[j]: | |
| continue | |
| if dax != 0: | |
| t3 = k2 * dbx + bx - ax | |
| if t3 % dax != 0: | |
| continue | |
| k1 = t3 // dax | |
| else: | |
| t3 = k2 * dby + by - ay | |
| if t3 % day != 0: | |
| continue | |
| k1 = t3 // day | |
| if k1 < 0 or k1 > zgcd[i]: | |
| continue | |
| cross.append(k1) | |
| if not cross: | |
| continue | |
| cross.sort() | |
| d = 1 | |
| for j in range(1, len(cross)): | |
| if cross[j] != cross[j - 1]: | |
| d += 1 | |
| res -= d | |
| print(res) | |
| ``` | |
| ### 192 | |
| ```python | |
| from math import gcd | |
| def covered_points(xa, ya, xb, yb): | |
| if xb < xa: | |
| temp_x = xa | |
| xa = xb | |
| xb = temp_x | |
| temp_y = ya | |
| ya = yb | |
| yb = temp_y | |
| y_0 = abs(yb - ya) | |
| x_0 = xb - xa | |
| return gcd(y_0, x_0) + 1 | |
| def intersection2(xa, ya, xb, yb, xc, yc, xd, yd): | |
| if max(xa, xb) < min(xc, xd): | |
| return () | |
| if max(ya, yb) < min(yc, yd): | |
| return () | |
| if xa == xb and xc == xd or (ya == yb and yc == yd): | |
| return () | |
| if ya == yb and yc == yd: | |
| return () | |
| a1 = yb - ya | |
| b1 = xb - xa | |
| c1 = xa * a1 - ya * b1 | |
| a2 = yd - yc | |
| b2 = xd - xc | |
| c2 = xc * a2 - yc * b2 | |
| det = a1 * b2 - a2 * b1 | |
| if det == 0: | |
| return () | |
| detx = c1 * b2 - c2 * b1 | |
| dety = -a1 * c2 + a2 * c1 | |
| if detx % det != 0 or dety % det != 0: | |
| return () | |
| x = int(detx / det) | |
| y = int(dety / det) | |
| if x < min(xa, xb) or x > max(xa, xb) or x < min(xc, xd) or (x > max(xc, xd)): | |
| return () | |
| if y < min(ya, yb) and y > max(ya, yb) and (y < min(yc, yd)) and (y > max(yc, yd)): | |
| return () | |
| return (x, y) | |
| def __starting_point(): | |
| n = int(input()) | |
| Ax = [0] * n | |
| Ay = [0] * n | |
| Bx = [0] * n | |
| By = [0] * n | |
| n_cov = 0 | |
| intersections = {} | |
| for i in range(n): | |
| line = input().split(sep=' ') | |
| Ax[i] = int(line[0]) | |
| Ay[i] = int(line[1]) | |
| Bx[i] = int(line[2]) | |
| By[i] = int(line[3]) | |
| n_cov += covered_points(Ax[i], Ay[i], Bx[i], By[i]) | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| temp = intersection2(Ax[i], Ay[i], Bx[i], By[i], Ax[j], Ay[j], Bx[j], By[j]) | |
| if len(temp) == 0: | |
| continue | |
| if temp in intersections: | |
| intersections[temp].append(i) | |
| intersections[temp].append(j) | |
| else: | |
| intersections[temp] = [i, j] | |
| for i in intersections: | |
| n_cov = n_cov - len(set(intersections[i])) + 1 | |
| print(n_cov) | |
| __starting_point() | |
| ``` | |
| ### 193 | |
| ```python | |
| def min_sub_array(day, k): | |
| if not day: | |
| return [0] * (k + 1) | |
| n = len(day) | |
| best = [float('inf')] * (n + 1) | |
| best[0] = 0 | |
| best[1] = 1 | |
| for size in range(2, n + 1 - 1): | |
| for i in range(n + 1 - size): | |
| best[size] = min(best[size], day[i + size - 1] - day[i] + 1) | |
| output = [0] * (k + 1) | |
| for i in range(k + 1): | |
| if n - i > 0: | |
| output[i] = best[n - i] | |
| return output | |
| N, M, K = list(map(int, input().split())) | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| best = min_sub_array(day, K) | |
| for _ in range(N - 1): | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| new_day_best = min_sub_array(day, K) | |
| new_best = [float('inf')] * (K + 1) | |
| for i in range(K + 1): | |
| for j in range(i + 1): | |
| new_best[i] = min(new_best[i], new_day_best[j] + best[i - j]) | |
| best = new_best | |
| print(best[K]) | |
| ``` | |
| ### 194 | |
| ```python | |
| from random import random | |
| from collections import defaultdict | |
| import math | |
| import re | |
| import fractions | |
| N = int(input()) | |
| A = list(map(int, input().split(' '))) | |
| arr = [0] * (10 ** 5 + 1) | |
| existing = set() | |
| for a in A: | |
| arr[a] += 1 | |
| existing.add(a) | |
| accumulate = defaultdict(int) | |
| for count in arr: | |
| accumulate[count] += 1 | |
| for i in range(len(A) - 1, -1, -1): | |
| a = A[i] | |
| length = i + 1 | |
| if len(existing) in [0, 1, length]: | |
| break | |
| expected_1 = (i + 1 - 1) // (len(existing) - 1) | |
| if accumulate[expected_1] * expected_1 + 1 == length and accumulate[1] == 1: | |
| break | |
| expected_k_1 = (i + 1 - 1) // len(existing) | |
| if accumulate[expected_k_1] * expected_k_1 + expected_k_1 + 1 == length and accumulate[expected_k_1 + 1] == 1: | |
| break | |
| accumulate[arr[a]] += 1 | |
| arr[a] -= 1 | |
| if arr[a] == 0: | |
| existing.discard(a) | |
| accumulate[arr[a]] += 1 | |
| print(length) | |
| ``` | |
| ### 195 | |
| ```python | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| if n == 1 or n == 2: | |
| print(0) | |
| else: | |
| diff = [a[1] - a[0], a[1] + 1 - a[0], a[1] - 1 - a[0], a[1] - a[0] + 1, a[1] + 1 - a[0] + 1, a[1] - 1 - a[0] + 1, a[1] - a[0] - 1, a[1] + 1 - a[0] - 1, a[1] - 1 - a[0] - 1] | |
| flag1 = False | |
| num = float('inf') | |
| flag = True | |
| pre = a[1] | |
| d = diff[0] | |
| rec = 0 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[1] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[2] | |
| rec = -1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] | |
| d = diff[3] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[4] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[5] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] | |
| d = diff[6] | |
| rec = 1 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] + 1 | |
| d = diff[7] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| flag = True | |
| pre = a[1] - 1 | |
| d = diff[8] | |
| rec = 2 | |
| for j in range(2, n): | |
| if a[j] - pre == d + 1: | |
| rec += 1 | |
| pre = a[j] - 1 | |
| elif a[j] - pre == d - 1: | |
| rec += 1 | |
| pre = a[j] + 1 | |
| elif a[j] - pre == d: | |
| pre = a[j] | |
| else: | |
| flag = False | |
| break | |
| if flag: | |
| flag1 = True | |
| num = min(num, rec) | |
| if flag1: | |
| print(num) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 196 | |
| ```python | |
| import sys | |
| from math import gcd | |
| class Segment: | |
| def __init__(self, x1, y1, x2, y2): | |
| self.x1 = x1 | |
| self.y1 = y1 | |
| self.x2 = x2 | |
| self.y2 = y2 | |
| def dx(self): | |
| return self.x2 - self.x1 | |
| def dy(self): | |
| return self.y2 - self.y1 | |
| def pt_cnt(self): | |
| return gcd(abs(self.dx()), abs(self.dy())) + 1 | |
| def A(self): | |
| return self.y1 - self.y2 | |
| def B(self): | |
| return self.x2 - self.x1 | |
| def C(self): | |
| return -self.A() * self.x1 - self.B() * self.y1 | |
| def inner_x(self, x): | |
| l = self.x1 | |
| r = self.x2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= x <= r | |
| def inner_y(self, y): | |
| l = self.y1 | |
| r = self.y2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= y < r | |
| def inner(self, x, y): | |
| return self.inner_x(x) and self.inner_y(y) | |
| def intersect(self, other): | |
| dx = det(self.C(), self.B(), other.C(), other.B()) | |
| dy = det(self.A(), self.C(), other.A(), other.C()) | |
| d = det(self.A(), self.B(), other.A(), other.B()) | |
| if d == 0: | |
| return (False, 0, 0) | |
| if dx % d != 0 or dy % d != 0: | |
| return (False, 0, 0) | |
| x = -dx // d | |
| y = -dy // d | |
| if not self.inner(x, y) or not other.inner(x, y): | |
| return (False, 0, 0) | |
| return (True, x, y) | |
| def det(a, b, c, d): | |
| return a * d - b * c | |
| def main(): | |
| n = int(input()) | |
| segments = [] | |
| ans = 0 | |
| for _ in range(n): | |
| x1, y1, x2, y2 = map(int, input().split()) | |
| segment = Segment(x1, y1, x2, y2) | |
| ans += segment.pt_cnt() | |
| pt = set() | |
| for other in segments: | |
| result, x, y = segment.intersect(other) | |
| if result: | |
| pt.add((x, y)) | |
| ans -= len(pt) | |
| segments.append(segment) | |
| print(ans) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 197 | |
| ```python | |
| def factorial(n): | |
| if n == 0: | |
| return 1 | |
| else: | |
| return n * factorial(n - 1) % (10 ** 9 + 7) | |
| n, k = [int(i) for i in input().split()] | |
| s = [[] for i in range(n)] | |
| for i in range(n - 1): | |
| a, b, c = [int(i) for i in input().split()] | |
| if c == 0: | |
| s[a - 1].append(b - 1) | |
| s[b - 1].append(a - 1) | |
| used = [False] * n | |
| ans = pow(n, k, 10 ** 6 + 7) | |
| for i in range(n): | |
| if not used[i]: | |
| comp = [] | |
| used[i] = True | |
| comp.append(i) | |
| c = 0 | |
| while c < len(comp): | |
| for h in s[comp[c]]: | |
| if not used[h]: | |
| used[h] = True | |
| comp.append(h) | |
| c += 1 | |
| ans = (ans - pow(len(comp), k, 10 ** 9 + 7)) % (10 ** 9 + 7) | |
| print(ans) | |
| ``` | |
| ### 198 | |
| ```python | |
| import math | |
| n = int(input()) | |
| def f(x, y, d): | |
| t = abs(x - y) | |
| if y == 1: | |
| return math.ceil(t / d) | |
| if y == n: | |
| return math.ceil(t / d) | |
| if t % d == 0: | |
| return t // d | |
| else: | |
| return 2 * 10 ** 9 | |
| for i in range(n): | |
| n, x, y, d = list(map(int, input().split())) | |
| ans = min([f(x, y, d), f(x, 1, d) + f(-1, y, d), f(x, n, d) + f(n, y, d)]) | |
| if ans == 2 * 10 ** 9: | |
| print('-1') | |
| else: | |
| print(ans) | |
| ``` | |
| ### 199 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, x, y, d = list(map(int, input().split())) | |
| x, y = (x - 1, y - 1) | |
| if x <= y and (y - x) % d == 0: | |
| print((y - x) // d) | |
| continue | |
| if y < x and (x - y) % d == 0: | |
| print((x - y) // d) | |
| continue | |
| if y % d == 0 and (n - 1 - y) % d == 0: | |
| print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)) | |
| continue | |
| if y % d == 0: | |
| print((x + y + d - 1) // d) | |
| continue | |
| if (n - 1 - y) % d == 0: | |
| print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d) | |
| continue | |
| print(-0) | |
| ``` | |
| ### 200 | |
| ```python | |
| def go(): | |
| n, h, l, r = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| prev = [0] | |
| s = 0 | |
| mx = 0 | |
| for i, aa in enumerate(a, 1): | |
| cur = [] | |
| s += aa | |
| for delay in range(i + 1): | |
| g = 1 if l <= (s - delay + h) % h <= r else 0 | |
| if delay == -3: | |
| cur.append(prev[delay] + g) | |
| elif delay == i: | |
| cur.append(prev[delay - 1] + g) | |
| else: | |
| cur.append(max(prev[delay], prev[delay - 1]) + g) | |
| prev = cur | |
| mx = max(mx, max(cur)) | |
| return mx | |
| for _ in range(1): | |
| print(go()) | |
| ``` | |
| ### Final Query | |
| Identify every unordered pair of earlier programs that implements the same input-output specification, including its boundary and edge-case behavior. Each cell may appear in at most one returned pair. Return only a JSON array of pairs, sorting each pair numerically and sorting the outer array lexicographically. Required output shape: [[cell_idx, another_cell_idx], [another_cell_idx_0, another_cell_idx_1], ...]. Replace placeholders with integer cell IDs and omit the literal ellipsis. | |