description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
ttt = int(input()) for _ in range(ttt): S = [(ord(c) - ord("0")) for c in input()] x = int(input()) n = len(S) anss = [1] * n for i in range(n): if S[i] == 0: if 0 <= i - x: anss[i - x] = 0 if i + x < n: anss[i + x] = 0 T = [0] * n for i in range(n): if 0 <= i - x and anss[i - x]: T[i] = 1 if i + x < n and anss[i + x]: T[i] = 1 if S == T: print("".join(str(x) for x in anss)) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
def multiple_input(): return map(int, input().split()) def list_input(): return list(map(int, input().split())) for _ in range(int(input())): s = input() x = int(input()) n = len(s) w = ["1"] * n for i in range(n): if s[i] == "0": if i - x >= 0: w[i - x] = "0" if i + x < n: w[i + x] = "0" flag = 0 for i in range(n): if s[i] == "1": if i - x >= 0 and w[i - x] == "1" or i + x < n and w[i + x] == "1": continue else: flag = 1 break if flag == 0: print(*w, sep="") else: print(-1)
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR STRING BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
import sys MOD = 10**9 + 7 INF = 10**9 PI = 3.141592653589793 def read_str(): return sys.stdin.readline().strip() def read_int(): return int(sys.stdin.readline().strip()) def read_ints(): return map(int, sys.stdin.readline().strip().split()) def read_ints2(x): return map(lambda num: int(num) - x, sys.stdin.readline().strip().split()) def read_str_list(): return list(sys.stdin.readline().strip().split()) def read_int_list(): return list(map(int, sys.stdin.readline().strip().split())) def GCD(a: int, b: int) -> int: return b if a % b == 0 else GCD(b, a % b) def LCM(a: int, b: int) -> int: return a * b // GCD(a, b) def Main(): for _ in range(read_int()): s = list(read_str()) x = read_int() n = len(s) ans = [1] * n for i, ss in enumerate(s): if ss == "0": if 0 <= i - x: ans[i - x] = 0 if x + i < n: ans[x + i] = 0 for i, ss in enumerate(s): if ss == "1": flag = True if i - x >= 0 and ans[i - x] == 1: flag = False if i + x < n and ans[i + x] == 1: flag = False if flag: print(-1) break else: print("".join(map(str, ans))) Main()
IMPORT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF VAR VAR RETURN BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_DEF VAR VAR RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING IF NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
import sys input = sys.stdin.readline def print(val): sys.stdout.write(str(val) + "\n") def prog(): for _ in range(int(input())): s = input().strip() x = int(input()) place = [2] * len(s) fail = False for i in range(len(s)): if i + x > len(s) - 1 and i - x < 0: if s[i] == "1": fail = True break elif i + x > len(s) - 1: if s[i] == "0": if place[i - x] == 1: fail = True break place[i - x] = 0 else: if place[i - x] == 0: fail = True break place[i - x] = 1 elif i - x < 0: if s[i] == "0": if place[i + x] == 1: fail = True break place[i + x] = 0 else: if place[i + x] == 0: fail = True break place[i + x] = 1 elif s[i] == "0": if place[i + x] == 1: fail = True break place[i + x] = 0 if place[i - x] == 1: fail = True break place[i - x] = 0 for i in range(len(s)): if i - x >= 0 and i + x <= len(s) - 1 and s[i] == "1": if place[i + x] == 0 and place[i - x] == 0: fail = True break if fail: print(-1) else: output = ["1"] * len(s) for i in range(len(s)): if place[i] == 0: output[i] = "0" print("".join(output)) prog()
IMPORT ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR STRING IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
from sys import stdin ip = stdin.readline for _ in range(int(ip())): a = ip().strip() x = int(ip()) n = len(a) ans = ["1"] * n for i in range(n): val = a[i] if val == "0": if i - x >= 0: ans[i - x] = "0" if i + x < n: ans[i + x] = "0" reconstruct = ["0"] * n flag = True for i in range(n): if i - x >= 0 and ans[i - x] == "1" or i + x < n and ans[i + x] == "1": reconstruct[i] = "1" if not a[i] == reconstruct[i]: flag = False break if flag: print(*ans, sep="") else: print("-1")
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR STRING BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR STRING ASSIGN VAR VAR STRING IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
def solve(s, x): n = len(s) need_0 = set() for i in range(1, n + 1): if s[i - 1] == "0": if i - x > 0: need_0.add(i - x) if i + x <= n: need_0.add(i + x) w = "" for j in range(1, n + 1): if s[j - 1] == "1": back_test = j - x <= 0 or j - x in need_0 front_test = j + x > n or j + x in need_0 if back_test and front_test: return -1 for k in range(1, n + 1): if k in need_0: w += "0" else: w += "1" return w def main(): t = int(input()) for _ in range(t): s = input() x = int(input()) print(solve(s, x)) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR STRING VAR STRING RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
def iin(): return int(input()) def lin(): return list(map(int, input().split())) def main(): T = iin() while T: T -= 1 s = list(map(int, input())) x = iin() n = len(s) w = [-1] * n for i in range(n): if not s[i]: if i + x < n: w[i + x] = 0 if i - x >= 0: w[i - x] = 0 br = 0 for i in range(n): if s[i]: ch = 0 if i + x < n and w[i + x] == -1: ch += 1 if i - x >= 0 and w[i - x] == -1: ch += 1 if ch: pass else: br = 1 break if br: print(-1) continue w = [(i if i != -1 else 1) for i in w] print(*w, sep="") main()
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
def stringslicing(string, index): if index >= 0 and index < len(string): return string[index] else: return "not defined" t = int(input()) for _ in range(t): string = input().strip() s = int(input()) answer = {} answer = answer.fromkeys(range(0, len(string)), "1") for i in range(len(string)): if string[i] == "0": a = i - s b = i + s if a >= 0 and a < len(string): answer[a] = "0" if b >= 0 and b < len(string): answer[b] = "0" ans = "".join(answer.values()) wrong = 0 for k in range(len(string)): if string[k] == "1": if string[k] == stringslicing(ans, k + s) or string[k] == stringslicing( ans, k - s ): continue else: print(-1) wrong = 1 break if wrong == 0: print(ans)
FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR STRING IF VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
t = int(input()) for t_index in range(t): s, x = input(), int(input()) n = len(s) w, wr = "", ["0" for i in range(n + 2 * x)] for i in range(n): if s[i] == "1": wr[x + (i - x)] = wr[x + (i + x)] = "1" for i in range(n): if s[i] == "0": wr[x + (i - x)] = wr[x + (i + x)] = "0" for item in wr[x:][:-x]: w += item s_check, sr = "", ["0" for i in range(n + 2 * x)] for i in range(n): if w[i] == "1": sr[x + (i - x)] = sr[x + (i + x)] = "1" for item in sr[x:][:-x]: s_check += item print(w if s == s_check else -1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR STRING STRING VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR STRING FOR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR STRING STRING VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR STRING FOR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
t = int(input()) for i in range(t): s = input() s = list(s) n = int(input()) s1 = [] s2 = [] for i in range(len(s)): s1.append("1") s2.append("1") for i in range(len(s)): if s[i] == "0": if i - n >= 0: s1[i - n] = "0" if i + n < len(s): s1[i + n] = "0" for i in range(len(s1)): if i + n < len(s) and s1[i + n] == "1" or i - n >= 0 and s1[i - n] == "1": continue else: s2[i] = "0" if s == s2: print("".join(s1)) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR STRING BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR STRING ASSIGN VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
t = int(input()) for _ in range(t): a = list(input()) x = int(input()) ans = ["no" for i in range(len(a))] n = len(a) flag = 0 for i in range(n): if a[i] == "0": if i - x >= 0: ans[i - x] = "0" if i + x < n: ans[i + x] = "0" if i - x < 0 and i + x >= n: if a[i] == "1": flag = 1 break if flag == 1: print(-1) continue for i in range(n): if a[i] == "1": if i - x >= 0 and i + x < n: if ans[i - x] == "0" and ans[i + x] == "0": flag = 1 break elif i - x >= 0: if ans[i - x] == "0": flag = 1 break elif i + x < n: if ans[i + x] == "0": flag = 1 break if flag == 1: print(-1) else: for i in ans: if i == "no": print("1", end="") else: print(i, end="") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR STRING VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
t = int(input()) for _ in range(t): s = [0] + list(map(int, input().strip())) x = int(input()) n = len(s) - 1 ans = [(1) for _ in range(n + 1)] for i in range(1, n + 1): if s[i] == 0: if i > x: ans[i - x] = 0 if i + x <= n: ans[i + x] = 0 for i in range(1, n + 1): bit = False bit |= i > x and ans[i - x] == 1 bit |= i + x <= n and ans[i + x] == 1 if int(bit) != s[i]: print(-1) break else: print("".join(map(str, ans[1:])))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
import sys input = lambda: sys.stdin.readline().strip() t = int(input()) while t: t -= 1 s = input() n = len(s) x = int(input()) w = [""] * n for i in range(n): if s[i] == "0": if i - x >= 0: w[i - x] = "0" if i + x < n: w[i + x] = "0" for i in range(n): if w[i] == "": w[i] = "1" temp = [""] * n for i in range(n): ok = True if i - x >= 0: if w[i - x] == "1": temp[i] = "1" ok = False if i + x < len(s): if w[i + x] == "1": temp[i] = "1" ok = False if ok: temp[i] = "0" if "".join(temp) == s: print("".join(w)) else: print(-1)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR STRING IF FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
def solution(): s = list(input()) x = int(input()) n = len(s) flag = 0 l = ["2"] * n for i in range(n): p = i - x q = i + x if p < 0 and q > n - 1: if s[i] == "1": flag = 1 break elif p < 0: if s[i] == "1": if l[q] == "0": flag = 1 break l[q] = "1" else: if l[q] == "1": flag = 1 break l[q] = "0" elif q > n - 1: if s[i] == "1": if l[p] == "0": flag = 1 break l[p] = "1" else: if l[p] == "1": flag = 1 break l[p] = "0" elif s[i] == "0": if l[p] == "1" or l[q] == "1": flag = 1 break l[p] = "0" l[q] = "0" elif l[p] == "0" and l[q] == "0": flag = 1 break elif l[p] == "0" and l[q] == "2": l[q] = "1" elif l[p] == "2" and l[q] == "0": l[p] = "1" if flag: print(-1) else: ans = "" for i in range(n): if l[i] == "2": ans += "1" else: ans += l[i] print(ans) t = int(input()) for _ in range(t): solution()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR STRING IF VAR BIN_OP VAR NUMBER IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR STRING IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR STRING VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
import sys input = sys.stdin.readline for _ in range(int(input())): l = list(input()) x = int(input()) l = l[:-1] res = [] n = len(l) ans1 = ["1"] * n for i in range(n): if l[i] == "0": if i + x < n: ans1[i + x] = "0" if i - x >= 0: ans1[i - x] = "0" flag = 1 cnt = 0 for i in range(n): if l[i] == "1": cnt = 0 if i + x < n and ans1[i + x] == "1": cnt += 1 if i - x >= 0 and ans1[i - x] == "1": cnt += 1 if cnt == 0: flag = 0 break if flag == 0: print(-1) else: print("".join(ans1))
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR STRING VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
for _ in range(int(input())): s = [] for x in input(): s.append(int(x)) n = int(input()) ans = [None] * len(s) for i in range(len(s)): if i - n >= 0 and i + n < len(s): if s[i] == 0: if ans[i - n] == 1: ans = -1 break ans[i - n] = ans[i + n] = 0 elif ans[i - n] == 0: ans[i + n] = 1 else: ans[i - n] = 1 elif i - n < 0 and i + n >= len(s): if s[i] == 1: ans = -1 break elif i - n >= 0: if ans[i - n] == None: ans[i - n] = s[i] elif ans[i - n] != s[i]: ans = -1 break else: ans[i + n] = s[i] if ans == -1: print(-1) continue for i, x in enumerate(ans): if x == None: x = 1 ans[i] = str(x) print("".join(ans))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NONE ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
t = int(input()) for _ in range(t): s = list(input()) x = int(input()) n = len(s) a = ["1"] * n tr = 1 for i in range(n): if s[i] == "0": if i + x < n and i - x >= 0: a[i + x] = "0" a[i - x] = "0" elif i + x < n: a[i + x] = "0" elif i - x >= 0: a[i - x] = "0" for i in range(n): if s[i] == "1": if i + x < n and a[i + x] == "1" or i - x >= 0 and a[i - x] == "1": continue else: tr = 0 break if tr == 0: print("-1") else: print("".join(a))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR STRING BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
t = int(input()) for _ in range(t): s = input() n = len(s) x = int(input()) res = [-1] * len(s) for i in range(len(s)): num = int(s[i]) if s[i] == "0": if i - x < 0 and i + x >= n: continue elif i - x >= 0 and i + x >= n: if res[i - x] == -1 or res[i - x] == 0: res[i - x] = 0 else: print(-1) break elif i - x < 0 and i + x < n: if res[i + x] == -1 or res[i + x] == 0: res[i + x] = 0 else: print(-1) break elif i - x >= 0 and i + x < n: if res[i + x] == -1 or res[i + x] == 0: res[i + x] = 0 else: print(-1) break if res[i - x] == -1 or res[i - x] == 0: res[i - x] = 0 else: print(-1) break elif i - x < 0 and i + x >= n: print(-1) break elif i - x >= 0 and i + x >= n: if res[i - x] == -1 or res[i - x] == 1: res[i - x] = 1 else: print(-1) break elif i - x >= 0 and i + x < n: if res[i - x] == -1 or res[i - x] == 1: res[i - x] = 1 elif res[i + x] == -1 or res[i + x] == 1: res[i + x] = 1 else: print(-1) break elif i - x < 0 and i + x < n: if res[i + x] == -1 or res[i + x] == 1: res[i + x] = 1 else: print(-1) break if i == len(s) - 1: s = "" for j in range(len(res)): if res[j] == -1: s += "0" else: s += str(res[j]) print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR STRING VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
t = int(input()) for qwerty in range(t): s = input() k = int(input()) n = len(s) ans = ["1" for i in range(n)] for i in range(n): if s[i] == "0": if i - k >= 0: ans[i - k] = "0" if i + k < n: ans[i + k] = "0" v = "" for i in range(n): if i - k >= 0: if ans[i - k] == "1": v += "1" elif i + k < n: if ans[i + k] == "1": v += "1" else: v += "0" else: v += "0" elif i + k < n: if ans[i + k] == "1": v += "1" else: v += "0" else: v += "0" if v != s: print("-1") else: for i in range(n): print(ans[i], end="") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR STRING VAR STRING IF BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR STRING VAR STRING VAR STRING VAR STRING IF BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR STRING VAR STRING VAR STRING VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
def string(arr, x): for i in range(len(arr)): arr[i] = int(arr[i]) lst = [1] * len(arr) for i in range(len(arr)): if arr[i] == 0: if i - x >= 0: lst[i - x] = 0 if i + x <= len(arr) - 1: lst[i + x] = 0 if check(arr, lst, x) != -1: for i in lst: print(i, end="") print() else: print(-1) def check(arr, lst, x): for i in range(len(arr)): k1, k2 = 0, 0 if i - x >= 0: k1 = lst[i - x] if i + x <= len(arr) - 1: k2 = lst[i + x] if arr[i] != k1 | k2: return -1 return lst n = int(input()) for i in range(n): st = input() x = int(input()) string(list(st), x)
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR RETURN NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
for _ in range(int(input())): S = input() s = [] for i in S: s.append(int(i)) n = len(s) x = int(input()) a = [0] * n b = [False] * n check = False for i in range(min(x, n)): if i + x < n: b[i + x] = True a[i + x] = s[i] for i in range(n - x, n): if i - x >= 0: if not b[i - x]: b[i - x] = True a[i - x] = s[i] elif a[i - x] != s[i]: check = True break for i in range(x, n - x): if s[i] == 0: if b[i - x] and a[i - x] == 1 or b[i + x] and a[i + x] == 1: check = True break else: b[i - x] = True b[i + x] = True a[i - x] = 0 a[i + x] = 0 for i in range(n - x, x): b[i] = True if s[i] == 1: check = True break for i in range(n): if not b[i]: a[i] = 1 for i in range(x, n - x): if s[i] == 1: if (b[i - x] or b[i + x]) and a[i - x] + a[i + x] == 0: check = True break if check: print("-1") else: for i in a: print(i, end="") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
tc = int(input()) for t in range(tc): s = input() x = int(input()) n = len(s) w = [(-1) for _ in range(len(s))] later = [] pos = True for i in range(len(s)): if i + x >= n and i - x < 0: if s[i] == "1": pos = False print(-1) break else: continue elif i + x >= n and i - x >= 0: if w[i - x] == -1: w[i - x] = s[i] elif w[i - x] != s[i]: pos = False print(-1) break else: continue elif i + x < n and i - x < 0: if w[i + x] == -1: w[i + x] = s[i] elif w[i + x] != s[i]: pos = False print(-1) break else: continue elif s[i] == "0": if w[i + x] == "1" or w[i - x] == "1": pos = False print(-1) break else: w[i + x] = "0" w[i - x] = "0" else: later.append(i) if pos: for idx in later: if w[idx + x] == "0" and w[idx - x] == "0": pos = False print(-1) break else: if w[idx + x] == -1: w[idx + x] = "1" if w[idx - x] == -1: w[idx - x] = "1" if pos: for i in range(len(w)): if w[i] == -1: w[i] = "0" print("".join(w))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR VAR STRING VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR FOR VAR VAR IF VAR BIN_OP VAR VAR STRING VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING IF VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
for t in range(int(input())): s = list(input()) n = len(s) d = ["1"] * n x = int(input()) flag = False for i, j in enumerate(s): if j == "0": if i + x < n: d[i + x] = "0" if i - x >= 0: d[i - x] = "0" for i, j in enumerate(s): if j == "1": b1 = int(d[i - x]) if i - x >= 0 else 0 b2 = int(d[i + x]) if i + x < n else 0 if b1 + b2 == 0: flag = True break if flag: print(-1) else: print("".join(d))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
for _ in range(int(input())): s = input() x = int(input()) a = ["?"] * len(s) for i in range(len(s)): if s[i] == "0": if i + x < len(s): a[i + x] = "0" if i - x >= 0: a[i - x] = "0" temp = 0 for i in range(len(s)): if s[i] == "1": if i + x < len(s) and a[i + x] == "?": continue elif i - x >= 0 and a[i - x] == "?": continue else: temp = 1 if temp == 1: print(-1) else: ans = "" for i in range(len(s)): if a[i] == "?": ans += "1" else: ans += "0" print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
t = int(input()) for _ in range(t): s = input() x = int(input()) n = len(s) w = ["1"] * n bad = False ans = "" for i in range(n): if s[i] == "0": if i - x >= 0: w[i - x] = "0" if i + x < n: w[i + x] = "0" for i in range(n): one = True second = True if i - x >= 0: if w[i - x] == "1": one = True else: one = False else: one = False if i + x < n: if w[i + x] == "1": second = True else: second = False else: second = False if one or second: if s[i] != "1": print(-1) bad = True break if one is False and second is False: if s[i] != "0": print(-1) bad = True break if not bad: for ch in w: ans += ch print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
from sys import stdin for _ in range(int(stdin.readline())): string = stdin.readline().strip() x = int(stdin.readline()) build = "" flag = True array = [0] * len(string) if x + x > len(string): for i in range(len(string) - x): array[i + x] = string[i] for i in range(len(string) - x, x): if string[i] != "0": flag = False else: array[i] = "0" for i in range(x, len(string)): array[i - x] = string[i] if flag == False: print(-1) else: print("".join(array)) else: for i in range(x): array[i + x] = string[i] for i in range(x, len(string) - x): if array[i - x] == 0: array[i - x] = string[i] if string[i] == "0": array[i + x] = "0" elif string[i] == "0": if array[i - x] == "0": array[i + x] = "0" elif array[i - x] == 0: array[i - x] = "0" array[i + x] = "0" else: flag = False elif array[i - x] == "1": pass elif array[i - x] == "0": array[i + x] = "1" if i + x + x >= len(string): array[i + x] = string[i] for i in range(len(string) - x, len(string)): if array[i - x] == 0: array[i - x] = string[i] if string[i] != array[i - x]: flag = False if flag: print("".join(array)) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR STRING IF VAR VAR STRING IF VAR BIN_OP VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR STRING IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR STRING IF VAR BIN_OP VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
from sys import stdin for _ in range(int(input())): s = list(stdin.readline().rstrip()) x = int(input()) n = len(s) w = ["1"] * n test = ["0"] * n for i in range(n): if s[i] == "0": if i >= x: w[i - x] = "0" if i + x <= n - 1: w[i + x] = "0" for i in range(n): if i >= x and w[i - x] == "1": test[i] = "1" if i < n - x and w[i + x] == "1": test[i] = "1" if test != s: print(-1) else: print("".join(w))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR STRING ASSIGN VAR VAR STRING IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR STRING ASSIGN VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
for idn in range(int(input())): s = [int(symbol) for symbol in input()] n = len(s) x = int(input()) q = [(1) for i in range(n)] for i in range(n): if x <= i and s[i] == 0: q[i - x] = 0 if i + x < n and s[i] == 0: q[i + x] = 0 for i in range(min(x, n - x)): q[i + x] = s[i] q[n - i - 1 - x] = s[n - i - 1] w = [(0) for i in range(n)] for i in range(n): if x <= i: w[i] = w[i] | q[i - x] if i + x < n: w[i] = w[i] | q[i + x] if w == s: print(str(q)[1:-1].replace(", ", "")) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER NUMBER STRING STRING EXPR FUNC_CALL VAR NUMBER
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
for _ in range(int(input())): s = input() flag = 0 x = int(input()) a = ["-1" for i in range(len(s))] for i in range(len(s)): if s[i] == "1": if i - x >= 0 and a[i - x] != "0": a[i - x] = "1" elif i + x < len(s) and a[i + x] != "0": a[i + x] = "1" else: flag = 1 break else: if i - x >= 0: if a[i - x] != "1": a[i - x] = "0" else: flag = 1 break if i + x < len(s): if a[i + x] != "1": a[i + x] = "0" else: break for i in range(len(s)): if a[i] == "-1": a[i] = "1" if flag == 0: print("".join(a)) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
t = int(input()) for _ in range(t): s = input() n = len(s) w = [(-1) for i in range(n)] x = int(input()) possible = True for i in range(n): imx = i - x ipx = i + x if s[i] == "1": if imx >= 0 and w[imx] == -1: w[imx] = "1" if ipx < n: w[ipx] = "1" else: if imx >= 0: w[imx] = "0" if ipx < n: w[ipx] = "0" for i in range(n): if w[i] == -1: w[i] = "0" for i in range(n): ipx = i + x imx = i - x if s[i] == "1": if imx >= 0 and ipx < n and w[imx] != "1" and w[ipx] != "1": possible = not possible break elif imx >= 0 and ipx >= n and w[imx] != "1": possible = not possible break elif ipx < n and imx < 0 and w[ipx] != "1": possible = not possible break elif ipx >= n and imx < 0: possible = not possible break elif imx >= 0 and ipx < n and (w[imx] != "0" or w[ipx] != "0"): possible = not possible break elif imx >= 0 and ipx >= n and w[imx] != "0": possible = not possible break elif ipx < n and imx < 0 and w[ipx] != "0": possible = not possible break if not possible: print(-1) else: print("".join(w))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR ASSIGN VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR VAR VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER VAR VAR VAR VAR STRING ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR VAR STRING ASSIGN VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR VAR VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER VAR VAR VAR VAR STRING ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR VAR STRING ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
def main(): t = int(input()) for _ in range(t): s = input() x = int(input()) n = len(s) w = ["1"] * n for i in range(n): if i + x < n: if s[i + x] == "0": w[i] = "0" if i - x >= 0: if s[i - x] == "0": w[i] = "0" ok = True for i in range(n): if s[i] == "0": if i + x < n: if w[i + x] == "1": ok = False break if i - x >= 0: if w[i - x] == "1": ok = False break else: if i + x < n and i - x >= 0: if w[i + x] == "0" and w[i - x] == "0": ok = False break if i + x >= n and i - x >= 0: if w[i - x] == "0": ok = False break if i + x < n and i - x < 0: if w[i + x] == "0": ok = False break if i + x >= n and i - x < 0: ok = False break print("".join(w) if ok else -1) return main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR STRING ASSIGN VAR VAR STRING IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR STRING VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL STRING VAR NUMBER RETURN EXPR FUNC_CALL VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
def findStr(s, x, c): enc = list(["0" if c == "1" else "1"] * len(s)) for i in range(len(s)): if s[i] == c: if i - x >= 0: enc[i - x] = c if i + x < len(s): enc[i + x] = c return enc for _ in range(int(input())): s = list(input()) x = int(input()) orig = findStr(s, x, "0") enc = findStr(orig, x, "1") if enc == s: print("".join(orig)) else: print(-1)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP LIST VAR STRING STRING STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
t = int(input()) for i in range(t): ar = [str(i) for i in input()] n = len(ar) f = int(input()) no = 0 b = ["1"] * n for x in range(n): if ar[x] == "0": if x >= f: b[x - f] = "0" if x + f < n: b[x + f] = "0" for x in range(n): if ar[x] == "1": if (x >= f and b[x - f] == "0" or x < f) and ( x + f < n and b[x + f] == "0" or x + f >= n ): no = -1 break if no == -1: print(no) else: print("".join(b))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR VAR BIN_OP VAR VAR STRING VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR STRING BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
for _ in range(int(input())): s = input() x = int(input()) w = [0] * len(s) k = True for i in range(len(s)): if s[i] == "1": h = False if i - x >= 0: if i - x - x >= 0: if s[i - x - x] != "0": w[i - x] = 1 h = True else: w[i - x] = 1 h = True if i + x < len(s): if i + x + x < len(s): if s[i + x + x] != "0": w[i + x] = 1 h = True else: w[i + x] = 1 h = True if h == False: print(-1) k = False break w1 = "" for i in w: w1 += str(i) if k == True: print(w1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
import sys input = sys.stdin.buffer.readline T = int(input()) for testcase in range(T): s = list(input()) n = len(s) - 2 x = int(input()) w = ["1"] * n for i in range(n): if s[i] == 48: if 0 <= i + x < n: w[i + x] = "0" if 0 <= i - x < n: w[i - x] = "0" flag = True for i in range(n): q = True if 0 <= i - x < n and w[i - x] == "1": q = False if s[i] == 48: flag = False if 0 <= i + x < n and w[i + x] == "1": q = False if s[i] == 48: flag = False if q and s[i] == 49: flag = False if flag: print("".join(w)) else: print(-1)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING IF NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
T = int(input()) for t in range(T): s = input() x = int(input()) n = len(s) w = ["1"] * n flag = 0 for i in range(n): if s[i] == "0": if i - x >= 0: w[i - x] = "0" if i + x < n: w[i + x] = "0" for i in range(n): if s[i] == "1": if i - x >= 0 and w[i - x] == "1" or i + x < n and w[i + x] == "1": continue else: print(-1) flag = 1 break elif i - x >= 0 and w[i - x] == "1" or i + x < n and w[i + x] == "1": print(-1) flag = 1 break if flag: continue for i in range(n): print(w[i], end="") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR STRING BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR STRING BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
tc = int(input()) for t in range(tc): inp = [int(i) for i in input()] n = len(inp) x = int(input()) res = [1] * n ans = 0 for i in range(n): if inp[i] == 0: if i - x >= 0: res[i - x] = 0 if i + x < n: res[i + x] = 0 for i in range(n): if inp[i] == 1: f1 = False if i - x >= 0: if res[i - x] == 1: f1 = True f2 = False if i + x < n: if res[i + x] == 1: f2 = True if f1 or f2: continue else: ans = -1 break if ans == 0: ans = "" for i in res: ans += str(i) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
tc = int(input()) for _ in range(tc): s = input() x = int(input()) w = ["1"] * len(s) for i in range(len(s)): if s[i] == "0": if i - x > -1: w[i - x] = "0" if i + x < len(s): w[i + x] = "0" s2 = "" for i in range(len(s)): if i - x > -1 and w[i - x] == "1": if w[i - x] == "1": s2 += "1" elif i + x < len(s) and w[i + x] == "1": if w[i + x] == "1": s2 += "1" else: s2 += "0" ans = "".join(w) if s2 == s: print(ans) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR STRING IF VAR BIN_OP VAR VAR STRING VAR STRING IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR STRING IF VAR BIN_OP VAR VAR STRING VAR STRING VAR STRING ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
from sys import stdin tt = int(stdin.readline()) for loop in range(tt): s = stdin.readline() s = s[:-1] x = int(stdin.readline()) n = len(s) w = [None] * n flag = True for i in range(n): if s[i] == "0": if i - x >= 0: if w[i - x] == 1: flag = False break w[i - x] = 0 if i + x < n: if w[i + x] == 1: flag = False break w[i + x] = 0 elif i - x >= 0 and w[i - x] != 0: w[i - x] = 1 continue elif i + x < n: w[i + x] = 1 else: flag = False break if flag: for i in range(n): if w[i] == None: w[i] = 0 print("".join(map(str, w))) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NONE ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
def get_int(): return int(input()) def get_ints(): return [int(x) for x in input().split()] t = get_int() for i in range(0, t): s = input() x = get_int() w = "0" * x + "?" * len(s) + "0" * x s = list(s) w = list(w) for j in range(0, len(s)): if s[j] == "0": w[j] = "0" w[j + 2 * x] = "0" for j in range(0, len(w)): if w[j] == "?": w[j] = "1" impossible = False for j in range(0, len(s)): if s[j] == "1": if w[j] == "0" and w[j + 2 * x] == "0": print("-1") impossible = True break if not impossible: print("".join(w[x : x + len(s)]))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP STRING VAR BIN_OP STRING FUNC_CALL VAR VAR BIN_OP STRING VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING VAR BIN_OP VAR BIN_OP NUMBER VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR BIN_OP VAR FUNC_CALL VAR VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
for t in range(int(input())): s = input() x = int(input()) a = [] count = 0 for i in s: a.append(int(i)) count += 1 b = [1] * count for i in range(count): if a[i] == 0: if i - x >= 0: b[i - x] = 0 if i + x < count: b[i + x] = 0 flag = 0 for i in range(count): if i >= x and b[i - x] == 1 or i + x < count and b[i + x] == 1: if a[i] == 0: flag = -1 break elif a[i] == 1: flag = -1 break if flag == -1: print(-1) continue for i in b: print(i, end="") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
T = int(input()) for _ in range(T): s = input() x = int(input()) n = len(s) w = [(-1) for i in range(n)] flag = 0 for i in range(n): if s[i] == "0": if i - x >= 0: if w[i - x] == 1: flag = 1 break else: w[i - x] = 0 if i + x < n: if w[i + x] == 1: flag = 1 break else: w[i + x] = 0 for i in range(n): if s[i] == "1": if i - x >= 0 and i + x >= n: if w[i - x] == 0: flag = 1 break elif i + x < n and i - x < 0: if w[i + x] == 0: flag = 1 break elif i + x >= n and i - x < 0: flag = 1 elif w[i + x] == 0 and w[i - x] == 0: flag = 1 break if flag == 1: print(-1) else: ans = "" for i in w: if i == -1: ans = ans + "1" else: ans = ans + "0" print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
t = int(input()) Ans = [] for k in range(t): s = input() s = [int(j) for j in s] x = int(input()) w = [1] * len(s) for i in range(len(s)): if s[i] == 0: if i + 1 > x: w[i - x] = 0 if i + x + 1 <= len(s): w[i + x] = 0 for i in range(len(s)): if s[i] == 1: if ( i + 1 > x and w[i - x] == 0 and i + x + 1 > len(s) or i + x + 1 <= len(s) and w[i + x] == 0 and i + 1 <= x or i + 1 > x and w[i - x] == 0 and i + x + 1 <= len(s) and w[i + x] == 0 or i + x + 1 > len(s) and i + 1 <= x ): Ans.append(-1) break if len(Ans) == k: Ans.append("".join(str(j) for j in w)) for i in Ans: print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
for _ in range(int(input())): s = list(input()) n = len(s) x = int(input()) w = ["0"] * n c = 1 for i in range(n): if s[i] == "1": if i - x >= 0: w[i - x] = "1" if i + x < n: w[i + x] = "1" for i in range(n): if s[i] == "0": if i - x >= 0: w[i - x] = "0" if i + x < n: w[i + x] = "0" for i in range(n): if s[i] == "1": if i - x >= 0 and w[i - x] == "1" or i + x < n and w[i + x] == "1": continue else: c = 0 break if c: print("".join(w)) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR STRING BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
for _ in range(int(input())): st = input() x = int(input()) le = len(st) li = [-1] * le flag = True for i in range(le): if st[i] == "1": if i - x >= 0 and li[i - x] == 1: continue elif i + x < le and li[i + x] == 1: continue elif i - x >= 0 and li[i - x] == -1: li[i - x] = 1 elif i + x < le and li[i + x] == -1: li[i + x] = 1 else: flag = False break else: if i - x >= 0 and li[i - x] == 1: flag = False break elif i + x < le and li[i + x] == 1: flag = False break if i + x < le: li[i + x] = 0 if i - x >= 0: li[i - x] = 0 if flag: for i in li: if i == -1: print(0, end="") else: print(i, end="") else: print(-1, end="") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
for _ in range(int(input())): s = input() x = int(input()) l = len(s) ans = ["1"] * l for i in range(l): a = i - x >= 0 b = i + x < l if s[i] == "0": if a and b: ans[i + x] = "0" ans[i - x] = "0" elif a: ans[i - x] = "0" elif b: ans[i + x] = "0" for i in range(l): if s[i] == "0": if i - x >= 0 and ans[i - x] != "0": ans = ["-1"] break if i + x < l and ans[i + x] != "0": ans = ["-1"] break else: flag = False if i - x >= 0 and ans[i - x] != "0": flag = True if i + x < l and ans[i + x] != "0": flag = True if flag == False: ans = [-1] break print(*ans, sep="")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR STRING IF VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR STRING IF VAR ASSIGN VAR BIN_OP VAR VAR STRING IF VAR ASSIGN VAR BIN_OP VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR STRING ASSIGN VAR LIST STRING IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR STRING ASSIGN VAR LIST STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR STRING
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
for i in range(0, int(input())): s = list(input()) n = int(input()) w = [] for j in range(0, len(s)): w.append(1) for j in range(0, len(s)): if s[j] == "0": if j - n >= 0: w[j - n] = 0 if j + n < len(s): w[j + n] = 0 q = [0] * len(w) for j in range(0, len(w)): if j - n >= 0 and w[j - n] == 1: q[j] = 1 elif j + n < len(s) and w[j + n] == 1: q[j] = 1 else: q[j] = 0 k = 0 for j in range(0, len(s)): if int(s[j]) == q[j]: k += 1 if k == len(s): print("".join(map(str, w))) else: print("-1")
FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
def read_int(): return int(input().strip()) def read_ints(): return list(map(int, input().strip().split())) def read_floats(): return list(map(float, input().strip().split())) def read_str(): return input().strip() def solve_t(): s = read_str() x = read_int() n = len(s) w = list("1" * n) for i in range(n): if s[i] == "0": if i - x >= 0: w[i - x] = "0" if i + x < n: w[i + x] = "0" for i in range(n): if s[i] == "1": invalid = True if i - x >= 0 and w[i - x] == "1": invalid = False if i + x < n and w[i + x] == "1": invalid = False if invalid: print(-1) return print("".join(w)) def solve(): t = read_int() for _ in range(t): solve_t() solve()
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
import sys def rl(proc=None): if proc is not None: return proc(sys.stdin.readline()) else: return sys.stdin.readline().rstrip() def srl(proc=None): if proc is not None: return list(map(proc, rl().split())) else: return rl().split() def solve(s, x): n = len(s) w = [1] * n for i, v in enumerate(s): if v == 0: if i - x >= 0: w[i - x] = 0 if i + x < n: w[i + x] = 0 for i, v in enumerate(s): r = 0 if i - x >= 0: r |= w[i - x] if i + x < n: r |= w[i + x] if r != v: return [-1] return w def main(): T = rl(int) for t in range(1, T + 1): s = list(map(int, rl())) x = rl(int) print("".join(map(str, solve(s, x)))) main()
IMPORT FUNC_DEF NONE IF VAR NONE RETURN FUNC_CALL VAR FUNC_CALL VAR RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE IF VAR NONE RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN LIST NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
import sys lines = sys.stdin.read().splitlines() lincnt = -1 def input(): global lincnt lincnt += 1 return lines[lincnt] for _ in range(int(input())): s = input() x = int(input()) r = ["1"] * len(s) for i in range(len(s)): if s[i] == "0": if i >= x: r[i - x] = "0" if i <= len(s) - 1 - x: r[i + x] = "0" t = [] for i in range(len(s)): if i >= x and r[i - x] == "1": t.append("1") elif i <= len(s) - 1 - x and r[i + x] == "1": t.append("1") else: t.append("0") if s == "".join(t): print("".join(r)) else: print(-1)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR STRING IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
for _ in range(int(input())): s = input() n = int(input()) y = len(s) l = [1] * y f = 0 for i in range(y): if s[i] == "0": if i + n < y: l[i + n] = 0 if i - n >= 0: l[i - n] = 0 x = 0 for i in range(y): f = 0 if i - n >= 0 and l[i - n] == 1: f += 1 if i + n < y and l[i + n] == 1: f += 1 if f == 0 and s[i] == "1" or f > 1 and s[i] == "0": print("-1") x = 1 break if x == 0: for j in l: print(j, end="") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR STRING VAR NUMBER VAR VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
import sys I = lambda: int(input()) RL = readline = lambda: sys.stdin.readline().strip("\n") RM = readmap = lambda x=int: map(x, readline().split(" ")) for _ in range(I()): s, x = RL(), I() n = len(s) w = ["1"] * n for ind, i in enumerate(s): if i == "0": i1, i2 = ind - x, ind + x if i1 >= 0: w[i1] = "0" if i2 < n: w[i2] = "0" imp = False for ind, i in enumerate(s): i1, i2 = ind - x, ind + x flag = i1 >= 0 and w[i1] == "1" or i2 < n and w[i2] == "1" if "01"[flag] != i: imp = True break print("".join(w) if not imp else -1)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER VAR VAR STRING VAR VAR VAR VAR STRING IF STRING VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL STRING VAR NUMBER
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
for _ in range(int(input())): s = input() n = len(s) x = int(input()) a = ["1" for x in range(len(s))] for i, j in enumerate(s): if s[i] == "0": if i >= x: a[i - x] = "0" if i + x <= n - 1: a[i + x] = "0" for a_, b_, c_, d_ in zip(["0"] * x + a, a, a[x:] + ["0"] * x, s): if d_ == "1": if a_ == "0" and c_ == "0": a = ["-1"] break print(*a, sep="")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP LIST STRING VAR VAR VAR BIN_OP VAR VAR BIN_OP LIST STRING VAR VAR IF VAR STRING IF VAR STRING VAR STRING ASSIGN VAR LIST STRING EXPR FUNC_CALL VAR VAR STRING
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
def controller(s, x): n = len(s) w = list("1" * n) a = -x b = x for si in s: if a >= 0 and si == "0": w[a] = "0" if b < n and si == "0": w[b] = "0" if a < 0 and b >= n and si == "1": return "-1" a += 1 b += 1 a = -x b = x for si in s: if si == "1": if a < 0: if b < n and w[b] == "0": return "-1" elif b < n: if w[a] == "0" == w[b]: return "-1" elif w[a] == "0": return "-1" a += 1 b += 1 return "".join(w) def c_in(): n_test_case = int(input()) for i in range(n_test_case): s = input().rstrip() x = int(input()) print(controller(s, x)) c_in()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP STRING VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR NUMBER VAR STRING ASSIGN VAR VAR STRING IF VAR VAR VAR STRING ASSIGN VAR VAR STRING IF VAR NUMBER VAR VAR VAR STRING RETURN STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR STRING IF VAR NUMBER IF VAR VAR VAR VAR STRING RETURN STRING IF VAR VAR IF VAR VAR STRING VAR VAR RETURN STRING IF VAR VAR STRING RETURN STRING VAR NUMBER VAR NUMBER RETURN FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
for i in range(int(input())): s = input() n = len(s) k = int(input()) w = [-1] * n f = 0 for i in range(k): if i + k < n: w[i + k] = int(s[i]) elif i + k >= n and s[i] == "1": print(-1) f = 1 break for i in range(k, n): if s[i] == "1": if w[i - k] == -1: w[i - k] = 1 elif w[i - k] == 1: continue elif i + k < n: w[i + k] = 1 else: print(-1) f = 1 break else: if w[i - k] == -1: w[i - k] = 0 if w[i - k] != 0: print(-1) f = 1 break if i + k < n: w[i + k] = 0 if f == 1: continue for i in range(n): if w[i] == -1: w[i] = 0 print(*w, sep="")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
tests = int(input()) for _ in range(tests): string = list(input()) n = len(string) x = int(input()) ans = list(n * "1") for i in range(n): if string[i] == "0": if i + x < n: ans[i + x] = "0" if i - x >= 0: ans[i - x] = "0" check = [] for i in range(n): number = "0" if i - x >= 0 and ans[i - x] == "1": number = "1" if i + x < n and ans[i + x] == "1": number = "1" check.append(number) if check == string: print("".join(ans)) else: print("-1")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR STRING ASSIGN VAR STRING IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
import sys INP = lambda: sys.stdin.readline().strip() INT = lambda: int(INP()) MAP = lambda: map(int, INP().split()) ARR = lambda: [int(i) for i in INP().split()] def JOIN(arr, x=" "): return x.join([str(i) for i in arr]) def EXIT(x="NO"): print(x) exit() def good(s2, s, n, x): new = [0] * n for i in range(n): if i - x >= 0 and s2[i - x] or i + x < n and s2[i + x]: new[i] = 1 return new == s for _ in range(INT()): s = [int(i) for i in list(INP())] n = len(s) s2 = [1] * n x = INT() for i in range(n): if s[i] == 0: if i - x >= 0: s2[i - x] = 0 if i + x < n: s2[i + x] = 0 if good(s2, s, n, x): print(JOIN(s2, "")) else: print(-1)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF STRING RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
t = int(input()) def construct(A, x): n = len(A) B = [0] * n for i in range(len(A)): B.append(int(A[i])) for i in range(n): B.append(0) C = [0] * n for i in range(n): C[i] = B[n + i - x] or B[n + i + x] return C for _ in range(t): A = list(str(input())) A = [int(i) for i in A] x = int(input()) n = len(A) B = [1] * (3 * n) for i in range(n): if A[i] == 0: B[n + i - x] = 0 B[n + i + x] = 0 C = B[n : 2 * n] D = construct(C, x) if D == A: for i in range(n): print(C[i], end="") print() else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
for _ in range(int(input())): s = "x" + input() n = len(s) x = int(input()) w = ["1"] * n for i in range(1, n): if s[i] == "0": if i > x and i - x > 0 and i - x < n: w[i - x] = "0" if i + x < n: w[i + x] = "0" ans = "".join(w[1:]) ss = "" for i in range(1, n): flag1 = flag2 = False if i > x and i - x > 0 and i - x < n: if w[i - x] == "1": flag1 = True if i + x < n: if w[i + x] == "1": flag2 = True if not flag1 | flag2: ss += "0" else: ss += "1" if s[1:] == ss: print(ans) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR FUNC_CALL STRING VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR STRING VAR STRING IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): s = input().strip() n = len(s) x = int(input()) outl = ["1"] * n for i in range(n): if s[i] == "0": if i >= x: outl[i - x] = "0" if i + x < n: outl[i + x] = "0" out = "".join(outl) works = True for i in range(n): if s[i] == "1": if (i < x or out[i - x] == "0") and (i + x >= n or out[i + x] == "0"): works = False break if works: print(out) else: print(-1)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR VAR BIN_OP VAR VAR STRING BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
for t in range(int(input())): s = input() x = int(input()) l = ["1"] * len(s) flag = 0 for i in range(len(s)): if s[i] == "0": if i + x < len(s): l[i + x] = "0" if i - x >= 0: l[i - x] = "0" l1 = [] a = "" for i in range(len(l)): if i + x < len(l) and l[i + x] == "1" or i - x >= 0 and l[i - x] == "1": l1.append("1") else: l1.append("0") a = "".join(l1) if a != s: print(-1) continue for i in range(len(l)): if i == len(s) - 1: print(l[i]) else: print(l[i], end="")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR STRING BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ def gift(): for _ in range(t): string = input() x = int(input()) n = len(string) markpoint = [] Failed = False for i in range(n): if string[i] == "0": markpoint.append(i) dic = {} for ele in markpoint: if ele - x >= 0: dic[ele - x] = True if ele + x <= n - 1: dic[ele + x] = True for ele in dic: if ele - x >= 0: if ele - 2 * x >= 0: if dic.get(ele - 2 * x, False): if string[ele - x] != "0": Failed = True break elif string[ele - x] != "1": Failed = True break elif string[ele - x] != "0": Failed = True break if ele + x <= n - 1: if ele + 2 * x <= n - 1: if dic.get(ele + 2 * x, False): if string[ele + x] != "0": Failed = True break elif string[ele + x] != "1": Failed = True break elif string[ele + x] != "0": Failed = True break if Failed: yield -1 else: ans = [] for i in range(n): if dic.get(i, False): ans.append("0") else: ans.append("1") kap = "".join(ans) right = True for i in range(n): curr = False if i - x >= 0: if kap[i - x] == "1": curr = True if i + x <= n - 1: if kap[i + x] == "1": curr = True if curr: if string[i] != "1": right = False break elif string[i] != "0": right = False break if right: yield kap else: yield -1 t = int(input()) ans = gift() print(*ans, sep="\n")
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF VAR EXPR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR EXPR VAR EXPR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows: if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0. You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$). The total length of all strings $s$ in the input does not exceed $10^5$. -----Output----- For each test case, print the answer on a separate line as follows: if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them. -----Example----- Input 3 101110 2 01 1 110 1 Output 111011 10 -1
t = int(input()) for i in range(t): s = input().strip() x = int(input()) n = len(s) a = [-1] * n for j in range(n): if s[j] == "0": q = 0 if j - x >= 0: a[j - x] = 0 else: q = q + 1 if j + x < n: a[j + x] = 0 else: q = q + 1 if q == 2: a[j] = 0 f = 0 for j in range(n): if s[j] == "1": c = 0 if j - x >= 0 and a[j - x] != 0: c = c + 1 a[j - x] = 1 if j + x < n and a[j + x] != 0: c = c + 1 a[j + x] = 1 if c == 0: f = 1 break if f == 1: print(-1) elif -1 in a: print(-1) else: print("".join(map(str, a)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
"Duel!" Betting on the lovely princess Claris, the duel between Tokitsukaze and Quailty has started. There are $n$ cards in a row. Each card has two sides, one of which has color. At first, some of these cards are with color sides facing up and others are with color sides facing down. Then they take turns flipping cards, in which Tokitsukaze moves first. In each move, one should choose exactly $k$ consecutive cards and flip them to the same side, which means to make their color sides all face up or all face down. If all the color sides of these $n$ cards face the same direction after one's move, the one who takes this move will win. Princess Claris wants to know who will win the game if Tokitsukaze and Quailty are so clever that they won't make mistakes. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^5$). The second line contains a single string of length $n$ that only consists of $0$ and $1$, representing the situation of these $n$ cards, where the color side of the $i$-th card faces up if the $i$-th character is $1$, or otherwise, it faces down and the $i$-th character is $0$. -----Output----- Print "once again" (without quotes) if the total number of their moves can exceed $10^9$, which is considered a draw. In other cases, print "tokitsukaze" (without quotes) if Tokitsukaze will win, or "quailty" (without quotes) if Quailty will win. Note that the output characters are case-sensitive, and any wrong spelling would be rejected. -----Examples----- Input 4 2 0101 Output quailty Input 6 1 010101 Output once again Input 6 5 010101 Output tokitsukaze Input 4 1 0011 Output once again -----Note----- In the first example, no matter how Tokitsukaze moves, there would be three cards with color sides facing the same direction after her move, and Quailty can flip the last card to this direction and win. In the second example, no matter how Tokitsukaze moves, Quailty can choose the same card and flip back to the initial situation, which can allow the game to end in a draw. In the third example, Tokitsukaze can win by flipping the leftmost five cards up or flipping the rightmost five cards down. The fourth example can be explained in the same way as the second example does.
import sys n, k = map(int, input().split()) i = 0 cc = input() while i < n and cc[i] == cc[0]: i += 1 prefix = i i = 0 while i < n - 1 and cc[-i - 1] == cc[-1]: i += 1 suffix = i if k + prefix >= n or k + suffix >= n or cc[0] == cc[-1] and k + prefix + suffix >= n: print("tokitsukaze") sys.exit(0) cc0 = cc[0:i] + "0" * k + cc[i + k : n] if k <= prefix or k <= suffix: print("once again") sys.exit(0) if cc[0] == cc[-1]: print("once again") sys.exit(0) assert cc[0] != cc[-1] and prefix < k and suffix < k if prefix + k + 2 <= n or suffix + k + 2 <= n: print("once again") sys.exit(0) i = k while cc[i] == cc[0]: i += 1 if i + k >= n: break if i + k < n: print("once again") sys.exit(0) i = k while cc[-i - 1] == cc[-1]: i += 1 if i + k >= n: break if i + k < n: print("once again") sys.exit(0) print("quailty")
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP STRING VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING
"Duel!" Betting on the lovely princess Claris, the duel between Tokitsukaze and Quailty has started. There are $n$ cards in a row. Each card has two sides, one of which has color. At first, some of these cards are with color sides facing up and others are with color sides facing down. Then they take turns flipping cards, in which Tokitsukaze moves first. In each move, one should choose exactly $k$ consecutive cards and flip them to the same side, which means to make their color sides all face up or all face down. If all the color sides of these $n$ cards face the same direction after one's move, the one who takes this move will win. Princess Claris wants to know who will win the game if Tokitsukaze and Quailty are so clever that they won't make mistakes. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^5$). The second line contains a single string of length $n$ that only consists of $0$ and $1$, representing the situation of these $n$ cards, where the color side of the $i$-th card faces up if the $i$-th character is $1$, or otherwise, it faces down and the $i$-th character is $0$. -----Output----- Print "once again" (without quotes) if the total number of their moves can exceed $10^9$, which is considered a draw. In other cases, print "tokitsukaze" (without quotes) if Tokitsukaze will win, or "quailty" (without quotes) if Quailty will win. Note that the output characters are case-sensitive, and any wrong spelling would be rejected. -----Examples----- Input 4 2 0101 Output quailty Input 6 1 010101 Output once again Input 6 5 010101 Output tokitsukaze Input 4 1 0011 Output once again -----Note----- In the first example, no matter how Tokitsukaze moves, there would be three cards with color sides facing the same direction after her move, and Quailty can flip the last card to this direction and win. In the second example, no matter how Tokitsukaze moves, Quailty can choose the same card and flip back to the initial situation, which can allow the game to end in a draw. In the third example, Tokitsukaze can win by flipping the leftmost five cards up or flipping the rightmost five cards down. The fourth example can be explained in the same way as the second example does.
n, k = map(int, input().split()) a = input() A = 0 for i in range(n): A += int(a[i]) s = 0 c = 0 W = False L = False D = False for i in range(n): s += int(a[i]) if i >= k: s -= int(a[i - k]) c += int(a[i - k]) if i >= k - 1: if s == A: W = True elif A - s + k == n: W = True elif c > 0 and A - s - c > 0: D = True elif c == 0 and i >= k and i < n - 1 and c == 0 and A - s - c == 0: D = True if W: print("tokitsukaze") elif D or k * 2 < n: print("once again") else: print("quailty")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
"Duel!" Betting on the lovely princess Claris, the duel between Tokitsukaze and Quailty has started. There are $n$ cards in a row. Each card has two sides, one of which has color. At first, some of these cards are with color sides facing up and others are with color sides facing down. Then they take turns flipping cards, in which Tokitsukaze moves first. In each move, one should choose exactly $k$ consecutive cards and flip them to the same side, which means to make their color sides all face up or all face down. If all the color sides of these $n$ cards face the same direction after one's move, the one who takes this move will win. Princess Claris wants to know who will win the game if Tokitsukaze and Quailty are so clever that they won't make mistakes. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^5$). The second line contains a single string of length $n$ that only consists of $0$ and $1$, representing the situation of these $n$ cards, where the color side of the $i$-th card faces up if the $i$-th character is $1$, or otherwise, it faces down and the $i$-th character is $0$. -----Output----- Print "once again" (without quotes) if the total number of their moves can exceed $10^9$, which is considered a draw. In other cases, print "tokitsukaze" (without quotes) if Tokitsukaze will win, or "quailty" (without quotes) if Quailty will win. Note that the output characters are case-sensitive, and any wrong spelling would be rejected. -----Examples----- Input 4 2 0101 Output quailty Input 6 1 010101 Output once again Input 6 5 010101 Output tokitsukaze Input 4 1 0011 Output once again -----Note----- In the first example, no matter how Tokitsukaze moves, there would be three cards with color sides facing the same direction after her move, and Quailty can flip the last card to this direction and win. In the second example, no matter how Tokitsukaze moves, Quailty can choose the same card and flip back to the initial situation, which can allow the game to end in a draw. In the third example, Tokitsukaze can win by flipping the leftmost five cards up or flipping the rightmost five cards down. The fourth example can be explained in the same way as the second example does.
from sys import exit, stdin, stdout n, k = map(int, stdin.readline().split()) s = stdin.readline().strip() def z(winner): return "tokitsukaze" if winner == 0 else "quailty" ones = [i for i in range(n) if s[i] == "1"] zeros = [i for i in range(n) if s[i] == "0"] if not ones or not zeros: print(z(0)) exit() if max(ones) - min(ones) + 1 <= k or max(zeros) - min(zeros) + 1 <= k: print(z(0)) exit() if max(ones) - min(ones) + 1 >= k + 2 or max(zeros) - min(zeros) + 1 >= k + 2: print("once again") exit() if n == 2 * (k + 1): print("once again") exit() print(z(1))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN VAR NUMBER STRING STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER
"Duel!" Betting on the lovely princess Claris, the duel between Tokitsukaze and Quailty has started. There are $n$ cards in a row. Each card has two sides, one of which has color. At first, some of these cards are with color sides facing up and others are with color sides facing down. Then they take turns flipping cards, in which Tokitsukaze moves first. In each move, one should choose exactly $k$ consecutive cards and flip them to the same side, which means to make their color sides all face up or all face down. If all the color sides of these $n$ cards face the same direction after one's move, the one who takes this move will win. Princess Claris wants to know who will win the game if Tokitsukaze and Quailty are so clever that they won't make mistakes. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^5$). The second line contains a single string of length $n$ that only consists of $0$ and $1$, representing the situation of these $n$ cards, where the color side of the $i$-th card faces up if the $i$-th character is $1$, or otherwise, it faces down and the $i$-th character is $0$. -----Output----- Print "once again" (without quotes) if the total number of their moves can exceed $10^9$, which is considered a draw. In other cases, print "tokitsukaze" (without quotes) if Tokitsukaze will win, or "quailty" (without quotes) if Quailty will win. Note that the output characters are case-sensitive, and any wrong spelling would be rejected. -----Examples----- Input 4 2 0101 Output quailty Input 6 1 010101 Output once again Input 6 5 010101 Output tokitsukaze Input 4 1 0011 Output once again -----Note----- In the first example, no matter how Tokitsukaze moves, there would be three cards with color sides facing the same direction after her move, and Quailty can flip the last card to this direction and win. In the second example, no matter how Tokitsukaze moves, Quailty can choose the same card and flip back to the initial situation, which can allow the game to end in a draw. In the third example, Tokitsukaze can win by flipping the leftmost five cards up or flipping the rightmost five cards down. The fourth example can be explained in the same way as the second example does.
n, k = map(int, input().split()) s = input() def func(s): num = 1 num2 = 1 for i in range(1, n): if s[i] != s[i - 1]: break num += 1 for i in range(n - 1, 0, -1): if s[i] != s[i - 1]: break num2 += 1 if k + num >= n or k + num2 >= n: return 0 elif s[-1] == s[0] and k + num + num2 >= n: return 0 else: return 1 if func(s) == 0: print("tokitsukaze") else: s2 = list(s) nums = [0] * 8 s3 = list(s) for i in range(k): s3[i] = "1" s4 = list(s) for i in range(k): s4[i] = "0" s5 = list(s) for i in range(n - 1, n - k - 1, -1): s5[i] = "1" s6 = list(s) for i in range(n - 1, n - k - 1, -1): s6[i] = "0" s7 = list(s) for i in range(1, k + 1): s7[i] = "1" s8 = list(s) for i in range(1, k + 1): s8[i] = "0" s9 = list(s) for i in range(n - 2, n - k - 2, -1): s9[i] = "1" s10 = list(s) for i in range(n - 2, n - k - 2, -1): s10[i] = "0" nums[0] = func(s3) nums[1] = func(s4) nums[2] = func(s5) nums[3] = func(s6) nums[4] = func(s7) nums[5] = func(s8) nums[6] = func(s9) nums[7] = func(s10) if sum(nums) != 0: print("once again") else: print("quailty")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR STRING ASSIGN VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
"Duel!" Betting on the lovely princess Claris, the duel between Tokitsukaze and Quailty has started. There are $n$ cards in a row. Each card has two sides, one of which has color. At first, some of these cards are with color sides facing up and others are with color sides facing down. Then they take turns flipping cards, in which Tokitsukaze moves first. In each move, one should choose exactly $k$ consecutive cards and flip them to the same side, which means to make their color sides all face up or all face down. If all the color sides of these $n$ cards face the same direction after one's move, the one who takes this move will win. Princess Claris wants to know who will win the game if Tokitsukaze and Quailty are so clever that they won't make mistakes. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^5$). The second line contains a single string of length $n$ that only consists of $0$ and $1$, representing the situation of these $n$ cards, where the color side of the $i$-th card faces up if the $i$-th character is $1$, or otherwise, it faces down and the $i$-th character is $0$. -----Output----- Print "once again" (without quotes) if the total number of their moves can exceed $10^9$, which is considered a draw. In other cases, print "tokitsukaze" (without quotes) if Tokitsukaze will win, or "quailty" (without quotes) if Quailty will win. Note that the output characters are case-sensitive, and any wrong spelling would be rejected. -----Examples----- Input 4 2 0101 Output quailty Input 6 1 010101 Output once again Input 6 5 010101 Output tokitsukaze Input 4 1 0011 Output once again -----Note----- In the first example, no matter how Tokitsukaze moves, there would be three cards with color sides facing the same direction after her move, and Quailty can flip the last card to this direction and win. In the second example, no matter how Tokitsukaze moves, Quailty can choose the same card and flip back to the initial situation, which can allow the game to end in a draw. In the third example, Tokitsukaze can win by flipping the leftmost five cards up or flipping the rightmost five cards down. The fourth example can be explained in the same way as the second example does.
n, k = map(int, input().strip().split()) t = input().strip() l1 = t.rfind("1") - t.find("1") + 1 l2 = t.rfind("0") - t.find("0") + 1 if l1 <= k or l2 <= k: print("tokitsukaze") elif k < n // 2 or l1 > k + 1 or l2 > k + 1: print("once again") else: print("quailty")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
"Duel!" Betting on the lovely princess Claris, the duel between Tokitsukaze and Quailty has started. There are $n$ cards in a row. Each card has two sides, one of which has color. At first, some of these cards are with color sides facing up and others are with color sides facing down. Then they take turns flipping cards, in which Tokitsukaze moves first. In each move, one should choose exactly $k$ consecutive cards and flip them to the same side, which means to make their color sides all face up or all face down. If all the color sides of these $n$ cards face the same direction after one's move, the one who takes this move will win. Princess Claris wants to know who will win the game if Tokitsukaze and Quailty are so clever that they won't make mistakes. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^5$). The second line contains a single string of length $n$ that only consists of $0$ and $1$, representing the situation of these $n$ cards, where the color side of the $i$-th card faces up if the $i$-th character is $1$, or otherwise, it faces down and the $i$-th character is $0$. -----Output----- Print "once again" (without quotes) if the total number of their moves can exceed $10^9$, which is considered a draw. In other cases, print "tokitsukaze" (without quotes) if Tokitsukaze will win, or "quailty" (without quotes) if Quailty will win. Note that the output characters are case-sensitive, and any wrong spelling would be rejected. -----Examples----- Input 4 2 0101 Output quailty Input 6 1 010101 Output once again Input 6 5 010101 Output tokitsukaze Input 4 1 0011 Output once again -----Note----- In the first example, no matter how Tokitsukaze moves, there would be three cards with color sides facing the same direction after her move, and Quailty can flip the last card to this direction and win. In the second example, no matter how Tokitsukaze moves, Quailty can choose the same card and flip back to the initial situation, which can allow the game to end in a draw. In the third example, Tokitsukaze can win by flipping the leftmost five cards up or flipping the rightmost five cards down. The fourth example can be explained in the same way as the second example does.
import sys def Check(): for j in [0, 1]: if R[j][1] + L[j][n] + k >= n: return 1 return 0 n, k = [int(i) for i in input().split()] S = [int(i) for i in input()] R = [[0] * (n + 2), [0] * (n + 2)] L = [[0] * (n + 2), [0] * (n + 2)] for j in [0, 1]: L[S[0]][1] = 1 for i in range(1, n): if S[i] == j: L[j][i + 1] = L[j][i] + 1 for j in [0, 1]: R[S[n - 1]][n] = 1 for i in range(n - 2, -1, -1): if S[i] == j: R[j][i + 1] = R[j][i + 2] + 1 if Check(): print("tokitsukaze") sys.exit(0) for j in [0, 1]: for r in range(k, n + 1): l = r - k + 1 t = [R[j][1], L[j][n], R[j ^ 1][1], L[j ^ 1][n]] if R[j][1] >= l - 1: R[j][1] = r + R[j][r + 1] if L[j][n] >= n - r: L[j][n] = n - l + 1 + L[j][l - 1] if R[j ^ 1][1] > l - 1: R[j ^ 1][1] = l - 1 if L[j ^ 1][n] > n - r: L[j ^ 1][n] = n - r if not Check(): print("once again") sys.exit(0) R[j][1], L[j][n], R[j ^ 1][1], L[j ^ 1][n] = t print("quailty")
IMPORT FUNC_DEF FOR VAR LIST NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER FOR VAR LIST NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER FOR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING
"Duel!" Betting on the lovely princess Claris, the duel between Tokitsukaze and Quailty has started. There are $n$ cards in a row. Each card has two sides, one of which has color. At first, some of these cards are with color sides facing up and others are with color sides facing down. Then they take turns flipping cards, in which Tokitsukaze moves first. In each move, one should choose exactly $k$ consecutive cards and flip them to the same side, which means to make their color sides all face up or all face down. If all the color sides of these $n$ cards face the same direction after one's move, the one who takes this move will win. Princess Claris wants to know who will win the game if Tokitsukaze and Quailty are so clever that they won't make mistakes. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^5$). The second line contains a single string of length $n$ that only consists of $0$ and $1$, representing the situation of these $n$ cards, where the color side of the $i$-th card faces up if the $i$-th character is $1$, or otherwise, it faces down and the $i$-th character is $0$. -----Output----- Print "once again" (without quotes) if the total number of their moves can exceed $10^9$, which is considered a draw. In other cases, print "tokitsukaze" (without quotes) if Tokitsukaze will win, or "quailty" (without quotes) if Quailty will win. Note that the output characters are case-sensitive, and any wrong spelling would be rejected. -----Examples----- Input 4 2 0101 Output quailty Input 6 1 010101 Output once again Input 6 5 010101 Output tokitsukaze Input 4 1 0011 Output once again -----Note----- In the first example, no matter how Tokitsukaze moves, there would be three cards with color sides facing the same direction after her move, and Quailty can flip the last card to this direction and win. In the second example, no matter how Tokitsukaze moves, Quailty can choose the same card and flip back to the initial situation, which can allow the game to end in a draw. In the third example, Tokitsukaze can win by flipping the leftmost five cards up or flipping the rightmost five cards down. The fourth example can be explained in the same way as the second example does.
n = list(map(int, input().split())) st = input() i = 0 mas = [] while i < len(st): mas.insert(i, int(st[i])) i = i + 1 i = 0 cnt = 0 while i < n[0]: if mas[i] == 1: cnt = cnt + 1 i = i + 1 i = 0 cnt2 = 0 while i < n[1]: if mas[i] == 1: cnt2 = cnt2 + 1 i = i + 1 ans = 0 mn = cnt2 id = 0 izm = 0 if cnt - cnt2 == n[0] - n[1] or n[0] - cnt - (n[1] - cnt2) == n[0] - n[1]: ans = 1 while i < n[0]: if mas[i] == 1: cnt2 = cnt2 + 1 if mas[i - n[1]] == 1: cnt2 = cnt2 - 1 if cnt - cnt2 == n[0] - n[1] or n[0] - cnt - (n[1] - cnt2) == n[0] - n[1]: ans = 1 if cnt2 <= mn: mn = cnt2 id = i - n[1] izm = 0 if n[1] - cnt2 <= mn: mn = n[1] - cnt2 id = i - n[1] izm = 1 i = i + 1 if ans == 1: print("tokitsukaze") exit(0) i = 0 zer = [1000000, -1] one = [1000000, -1] 10001 while i < n[0]: if mas[i] == 1: one[0] = min(one[0], i) one[1] = max(one[1], i) if mas[i] == 0: zer[0] = min(zer[0], i) zer[1] = max(zer[1], i) i = i + 1 if one[1] - one[0] - 1 >= n[1] or zer[1] - zer[0] - 1 >= n[1]: print("once again") exit(0) id2 = id while id < id2 + n[1]: mas[id] = izm id = id + 1 i = 0 cnt2 = 0 cnt = 0 while i < n[0]: if mas[i] == 1: cnt = cnt + 1 i = i + 1 i = 0 while i < n[1]: if mas[i] == 1: cnt2 = cnt2 + 1 i = i + 1 ans = 0 if cnt - cnt2 == n[0] - n[1] or n[0] - cnt - (n[1] - cnt2) == n[0] - n[1]: ans = 1 while i < n[0]: if mas[i] == 1: cnt2 = cnt2 + 1 if mas[i - n[1]] == 1: cnt2 = cnt2 - 1 if cnt - cnt2 == n[0] - n[1] or n[0] - cnt - (n[1] - cnt2) == n[0] - n[1]: ans = 1 i = i + 1 if ans == 1: print("quailty") else: print("once again")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER EXPR NUMBER WHILE VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
"Duel!" Betting on the lovely princess Claris, the duel between Tokitsukaze and Quailty has started. There are $n$ cards in a row. Each card has two sides, one of which has color. At first, some of these cards are with color sides facing up and others are with color sides facing down. Then they take turns flipping cards, in which Tokitsukaze moves first. In each move, one should choose exactly $k$ consecutive cards and flip them to the same side, which means to make their color sides all face up or all face down. If all the color sides of these $n$ cards face the same direction after one's move, the one who takes this move will win. Princess Claris wants to know who will win the game if Tokitsukaze and Quailty are so clever that they won't make mistakes. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^5$). The second line contains a single string of length $n$ that only consists of $0$ and $1$, representing the situation of these $n$ cards, where the color side of the $i$-th card faces up if the $i$-th character is $1$, or otherwise, it faces down and the $i$-th character is $0$. -----Output----- Print "once again" (without quotes) if the total number of their moves can exceed $10^9$, which is considered a draw. In other cases, print "tokitsukaze" (without quotes) if Tokitsukaze will win, or "quailty" (without quotes) if Quailty will win. Note that the output characters are case-sensitive, and any wrong spelling would be rejected. -----Examples----- Input 4 2 0101 Output quailty Input 6 1 010101 Output once again Input 6 5 010101 Output tokitsukaze Input 4 1 0011 Output once again -----Note----- In the first example, no matter how Tokitsukaze moves, there would be three cards with color sides facing the same direction after her move, and Quailty can flip the last card to this direction and win. In the second example, no matter how Tokitsukaze moves, Quailty can choose the same card and flip back to the initial situation, which can allow the game to end in a draw. In the third example, Tokitsukaze can win by flipping the leftmost five cards up or flipping the rightmost five cards down. The fourth example can be explained in the same way as the second example does.
def win1(): if n == k or r[k + 1] == n or l[n - k] == 1: return True for i in range(2, n - k + 1): if l[i - 1] == 1 and r[i + k] == n and a[i - 1] == a[i + k]: return True return False def win2(): if 2 * k < n: return False for i in range(2, n - k + 1): if l[i - 1] != 1 or r[i + k] != n: return False return True s = input().split() n, k = int(s[0]), int(s[1]) s = input().split() a = [0] l = [(0) for i in range(n + 1)] r = [(0) for i in range(n + 1)] for c in s[0]: a.append(int(c)) l[1], r[n] = 1, n for i in range(2, n + 1): if a[i - 1] == a[i]: l[i] = l[i - 1] else: l[i] = i if a[n - i + 1] == a[n - i + 2]: r[n - i + 1] = r[n - i + 2] else: r[n - i + 1] = n - i + 1 if win1(): print("tokitsukaze") elif win2(): print("quailty") else: print("once again")
FUNC_DEF IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF BIN_OP NUMBER VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
"Duel!" Betting on the lovely princess Claris, the duel between Tokitsukaze and Quailty has started. There are $n$ cards in a row. Each card has two sides, one of which has color. At first, some of these cards are with color sides facing up and others are with color sides facing down. Then they take turns flipping cards, in which Tokitsukaze moves first. In each move, one should choose exactly $k$ consecutive cards and flip them to the same side, which means to make their color sides all face up or all face down. If all the color sides of these $n$ cards face the same direction after one's move, the one who takes this move will win. Princess Claris wants to know who will win the game if Tokitsukaze and Quailty are so clever that they won't make mistakes. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^5$). The second line contains a single string of length $n$ that only consists of $0$ and $1$, representing the situation of these $n$ cards, where the color side of the $i$-th card faces up if the $i$-th character is $1$, or otherwise, it faces down and the $i$-th character is $0$. -----Output----- Print "once again" (without quotes) if the total number of their moves can exceed $10^9$, which is considered a draw. In other cases, print "tokitsukaze" (without quotes) if Tokitsukaze will win, or "quailty" (without quotes) if Quailty will win. Note that the output characters are case-sensitive, and any wrong spelling would be rejected. -----Examples----- Input 4 2 0101 Output quailty Input 6 1 010101 Output once again Input 6 5 010101 Output tokitsukaze Input 4 1 0011 Output once again -----Note----- In the first example, no matter how Tokitsukaze moves, there would be three cards with color sides facing the same direction after her move, and Quailty can flip the last card to this direction and win. In the second example, no matter how Tokitsukaze moves, Quailty can choose the same card and flip back to the initial situation, which can allow the game to end in a draw. In the third example, Tokitsukaze can win by flipping the leftmost five cards up or flipping the rightmost five cards down. The fourth example can be explained in the same way as the second example does.
n, k = map(int, input().split()) s = input() fir0 = -1 last0 = -1 fir1 = -1 last1 = -1 for i in range(n): if s[i] == "0": if fir0 == -1: fir0 = i last0 = i else: if fir1 == -1: fir1 = i last1 = i d0 = last0 - fir0 d1 = last1 - fir1 if min(d0, d1) < k: print("tokitsukaze") elif d0 > k or d1 > k: print("once again") else: cnt1 = 0 cnt0 = 0 for i in range(n): if s[i] == "0": cnt0 += 1 cnt1 = 0 else: cnt1 += 1 cnt0 = 0 if cnt1 >= k or cnt0 >= k: print("once again") exit() print("quailty")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
"Duel!" Betting on the lovely princess Claris, the duel between Tokitsukaze and Quailty has started. There are $n$ cards in a row. Each card has two sides, one of which has color. At first, some of these cards are with color sides facing up and others are with color sides facing down. Then they take turns flipping cards, in which Tokitsukaze moves first. In each move, one should choose exactly $k$ consecutive cards and flip them to the same side, which means to make their color sides all face up or all face down. If all the color sides of these $n$ cards face the same direction after one's move, the one who takes this move will win. Princess Claris wants to know who will win the game if Tokitsukaze and Quailty are so clever that they won't make mistakes. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^5$). The second line contains a single string of length $n$ that only consists of $0$ and $1$, representing the situation of these $n$ cards, where the color side of the $i$-th card faces up if the $i$-th character is $1$, or otherwise, it faces down and the $i$-th character is $0$. -----Output----- Print "once again" (without quotes) if the total number of their moves can exceed $10^9$, which is considered a draw. In other cases, print "tokitsukaze" (without quotes) if Tokitsukaze will win, or "quailty" (without quotes) if Quailty will win. Note that the output characters are case-sensitive, and any wrong spelling would be rejected. -----Examples----- Input 4 2 0101 Output quailty Input 6 1 010101 Output once again Input 6 5 010101 Output tokitsukaze Input 4 1 0011 Output once again -----Note----- In the first example, no matter how Tokitsukaze moves, there would be three cards with color sides facing the same direction after her move, and Quailty can flip the last card to this direction and win. In the second example, no matter how Tokitsukaze moves, Quailty can choose the same card and flip back to the initial situation, which can allow the game to end in a draw. In the third example, Tokitsukaze can win by flipping the leftmost five cards up or flipping the rightmost five cards down. The fourth example can be explained in the same way as the second example does.
import sys def sum_range(l, r): if r < l: return 0 if l == 0: return sum[r] return sum[r] - sum[l - 1] n, k = map(int, input().split()) cards = input() sum = [0] * n sum[0] = 1 if cards[0] == "1" else 0 for i in range(1, n): sum[i] += sum[i - 1] if cards[i] == "1": sum[i] += 1 min0 = min1 = n max0 = max1 = -1 for i in range(0, n): if cards[i] == "1": min1 = min(min1, i) max1 = i else: min0 = min(min0, i) max0 = i toki = False qual = True for i in range(0, n - k + 1): if sum_range(0, i - 1) + sum_range(i + k, n - 1) + k == n: toki = True if sum_range(0, i - 1) + sum_range(i + k, n - 1) + 0 == 0: toki = True prefix = sum_range(0, i - 1) == 0 suffix = sum_range(i + k, n - 1) == 0 if i > 0 and i + k < n and prefix ^ suffix == 0: qual = False if i - min0 > k or i - min1 > k or max0 - (i + k - 1) > k or max1 - (i + k - 1) > k: qual = False if toki == True: print("tokitsukaze") elif qual == True: print("quailty") else: print("once again")
IMPORT FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN VAR VAR RETURN BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
def solve(i): a = i * u b = i * v if k <= -a: return 0 if k <= b: return a + k return a + b + 2 * (k - b) for _ in range(int(input())): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) u = max(l1, l2) - min(r1, r2) v = max(r1, r2) - min(l1, l2) if u <= 0: print(solve(n)) else: print(min(solve(i) for i in range(1, n + 1)))
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR RETURN BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
for i in " " * int(input()): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) if l1 > l2: l1, r1, l2, r2 = l2, r2, l1, r1 meeting = 0 if r1 > l2: meeting = (min(r1, r2) - l2) * n if meeting >= k: print(0) continue k -= meeting tot = max(r1, r2) - min(l1, l2) - meeting // n mxtot = tot * n distformeet = 0 if r1 < l2: distformeet = l2 - r1 if mxtot >= k: q = (k - distformeet) // tot if (k - distformeet) % tot: q += 1 q = min(n, max(q, 1)) if q * tot >= k: print(q * distformeet + k) else: print(2 * k - q * tot + q * distformeet) else: print(n * distformeet + mxtot + (k - mxtot) * 2)
FOR VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) intersect_length = min(r1, r2) - max(l1, l2) length = max(r1, r2) - min(l1, l2) ans = 0 cost = 0 if intersect_length > 0: ans += intersect_length * n if ans >= k: print(0) continue nokori = length - intersect_length for _ in range(n): if nokori + ans <= k: cost += nokori ans += nokori else: diff = k - ans cost += diff ans += diff print(cost + (k - ans) * 2) continue dist = -intersect_length cost += dist nokori = length if nokori + ans <= k: ans += nokori cost += nokori else: print(cost + k) continue to_same_val = max(r1, r2) - min(l1, l2) to_same_cost = 2 * to_same_val - (r1 - l1) - (r2 - l2) if to_same_val * 2 <= to_same_cost: print(cost + (k - ans) * 2) continue for _ in range(1, n): if to_same_val + ans <= k: ans += to_same_val cost += to_same_cost else: need_val = k - ans need_cost = dist + need_val if need_val * 2 >= need_cost: ans += need_val cost += need_cost print(cost + (k - ans) * 2)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
import sys input = sys.stdin.readline def intersection(a, b): if a[0] >= b[0] and a[1] <= b[1]: return -(a[1] - a[0]) if b[0] > a[1]: return b[0] - a[1] elif b[1] < a[0]: return a[0] - b[1] if a[1] < b[1]: return b[0] - a[1] return a[0] - b[1] def check(a, b): diff = intersection(a, b) if diff < 0: if abs(diff) * n >= k: return True return False for nt in range(int(input())): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) if r2 - l2 < r1 - l1: minn = l2, r2 maxx = l1, r1 else: minn = l1, r1 maxx = l2, r2 if check(minn, maxx): print(0) continue diff = intersection(minn, maxx) one = abs(minn[0] - maxx[0]) + abs(minn[1] - maxx[1]) r = min(minn[0], maxx[0]), max(minn[1], maxx[1]) if diff < 0: k -= abs(diff) * n done = r[1] - r[0] + diff else: done = r[1] - r[0] if one >= 2 * done and (done != 0 and k // done != 0): print(max(0, one + 2 * (k - done))) continue if done * n <= k: print(max(0, one * n + 2 * (k - done * n))) continue left = k % done ans = k // done * one if diff < 0: print(ans + left) elif ans != 0: print(min(ans + 2 * left, ans + diff + left)) else: print(ans + diff + left)
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
import sys input = sys.stdin.buffer.readline def print(val): sys.stdout.write(str(val) + "\n") def prog(): for _ in range(int(input())): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) if l1 > l2: p1 = l1 p2 = r1 l1 = l2 r1 = r2 l2 = p1 r2 = p2 total = 0 moves = 0 if r1 >= l2: intersection = min(r1, r2) - max(l1, l2) l3 = min(l1, l2) r3 = max(r1, r2) leftover = r3 - l3 - intersection k -= intersection * n if k <= 0: print(0) elif k - leftover * n <= 0: print(k) else: k -= leftover * n moves += leftover * n moves += k * 2 k = 0 print(moves) else: l3 = l1 r3 = r2 a = l2 - r1 if (r3 - l3) * n >= k: if k <= r3 - l3: print(a + k) continue else: k -= r3 - l3 moves += a + r3 - l3 if r3 - l3 + a >= 2 * (r3 - l3): moves += 2 * k else: moves += (r3 - l3 + a) * (k // (r3 - l3)) moves += min(k % (r3 - l3) + a, 2 * (k % (r3 - l3))) print(moves) else: k -= (r3 - l3) * n moves += (r3 - l3 + a) * n moves += k * 2 print(moves) prog()
IMPORT ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP NUMBER BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
T = int(input()) for _ in range(T): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) ans = 0 if k <= 0: print(0) elif l1 >= l2 and r1 <= r2: k -= (r1 - l1) * n if k <= 0: print(0) else: trans = n * (l1 - l2 + r2 - r1) ans += min(k, trans) k -= trans print(ans if k <= 0 else ans + 2 * k) elif l2 >= l1 and r2 <= r1: k -= (r2 - l2) * n if k <= 0: print(0) else: trans = n * (l2 - l1 + r1 - r2) ans += min(k, trans) k -= trans print(ans if k <= 0 else ans + 2 * k) elif r2 <= l1: ans += l1 - r2 trans = r1 - l2 ans += min(k, trans) k -= trans if k <= 0: print(ans) else: for i in range(2, n + 1): t1 = l1 - r2 + min(k, r1 - l2) ans += min(t1, 2 * min(k, r1 - l2)) k -= min(k, r1 - l2) if k <= 0: break print(ans if k <= 0 else ans + 2 * k) elif l2 >= r1: ans += l2 - r1 trans = r2 - l1 ans += min(k, trans) k -= trans if k <= 0: print(ans) else: for i in range(2, n + 1): t1 = l2 - r1 + min(k, r2 - l1) ans += min(t1, 2 * min(k, r2 - l1)) k -= min(k, r2 - l1) if k <= 0: break print(ans if k <= 0 else ans + 2 * k) else: if l1 > l2 or l1 == l2 and r1 > r2: l1, l2 = l2, l1 r1, r2 = r2, r1 k -= n * (r1 - l2) if k <= 0: print(0) else: trans = n * (abs(r2 - r1) + abs(l2 - l1)) ans += min(k, trans) k -= trans print(ans if k <= 0 else ans + 2 * k)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
for _ in range(int(input())): ans = 1e18 n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) if min(r1, r2) >= max(l1, l2): rem = max(0, k - n * (min(r1, r2) - max(l1, l2))) poss = n * (abs(l1 - l2) + abs(r1 - r2)) ans = min(rem, poss) + 2 * max(0, rem - poss) else: invest = max(l1, l2) - min(r1, r2) for i in range(1, n + 1): curans = invest * i poss = i * (max(r1, r2) - min(l1, l2)) curans += min(k, poss) + 2 * max(0, k - poss) ans = min(curans, ans) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
for _ in range(int(input())): n, k = map(int, input().split()) a, b = map(int, input().split()) c, d = map(int, input().split()) if d < b: a, b, c, d = c, d, a, b if a <= c <= b: cur = (b - c) * n ( print(max(k - cur, 0)) if (d - a) * n > k else print((d - a) * n - cur + (k - (d - a) * n) * 2) ) elif c < a: cur = (b - a) * n ( print(max(k - cur, 0)) if (d - c) * n > k else print((d - c) * n - cur + (k - (d - c) * n) * 2) ) else: ans, cur = 10**18, 0 for i in range(n): cur += c - b if k > d - a: k -= d - a cur += d - a else: cur += k k -= k ans = min(ans, cur + k * 2) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
t = int(input()) for _ in range(0, t): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) if max(l1, l2) <= min(r1, r2): cur_inter = n * (min(r1, r2) - max(l1, l2)) can_inter = n * (max(r1, r2) - min(l1, l2)) if k <= cur_inter: print(0) elif k <= can_inter: print(k - cur_inter) else: print(can_inter - cur_inter + 2 * (k - can_inter)) else: ans = 10**10 + 7 for i in range(1, n + 1): to_inter = i * (max(l1, l2) - min(r1, r2)) can_inter = i * (max(r1, r2) - min(l1, l2)) if k <= can_inter: to_inter += k else: to_inter += can_inter + 2 * (k - can_inter) ans = min(ans, to_inter) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
def anss1(stp, numneed, stc): ans1 = stp if stc >= numneed: ans1 += numneed else: ans1 += stc numneed -= stc ans1 += 2 * numneed return ans1 def anss2(stp, numneed, stc): ans2 = 0 for i in range(n): if i > 0 and numneed < stp: ans2 += 2 * numneed numneed = 0 else: ans2 += stp if stc >= numneed: ans2 += numneed numneed = 0 break ans2 += stc numneed -= stc if numneed > 0: ans2 += 2 * numneed return ans2 for t in range(int(input())): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) stp = 0 stc = 0 curr = 0 if r1 < l2 or r2 < l1: a1 = min(l1, l2) b1 = min(r1, r2) a2 = max(l1, l2) b2 = max(r1, r2) stp = a2 - b1 stc = b2 - a1 else: points = list(sorted([l1, r1, l2, r2])) curr = points[2] - points[1] stc = points[3] - points[0] - curr numneed = k - n * curr if numneed <= 0: print(0) else: print(min(anss1(stp, numneed, stc), anss2(stp, numneed, stc)))
FUNC_DEF ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
T = int(input()) for _ in range(T): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) if l2 < l1: l1, l2, r1, r2 = l2, l1, r2, r1 d = max(r1, r2) - min(l1, l2) res = 0 if r1 < l2: sd = l2 - r1 if k <= d * n: if k % d >= sd: res = (d + sd) * (k // d) + k % d + sd elif k < sd: res = sd + k else: res = (d + sd) * (k // d) + k % d * 2 else: res = (d + sd) * n + (k - d * n) * 2 else: c1 = d - (r1 - l1) + d - (r2 - l2) c0 = d - c1 if k <= c0 * n: res = 0 elif k <= d * n: res = k - c0 * n else: res = d * n - c0 * n + (k - d * n) * 2 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
def solve(): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) if l1 > l2: l1, r1, l2, r2 = l2, r2, l1, r1 gap = l2 - r1 if gap >= 0: u = r1 - l1 v = r2 - l2 if u > v: u, v = v, u if k <= 0: print(0) return step = gap cur = 0 mx = max(r2, r1) - min(l1, l2) ans = gap + 2 * k for i in range(n): if cur + mx >= k: ans = min(ans, step + max(0, k - cur)) step += mx cur += mx ans = min(ans, step + max(k - cur, 0) * 2) step += gap ans = min(ans, step + max(k - cur, 0) * 2) print(ans) else: mx = (max(r2, r1) - min(l1, l2)) * n mn = (min(r1, r2) - max(l1, l2)) * n if k <= mx: print(max(0, k - mn)) return else: print((k - mx) * 2 + (mx - mn)) return t = int(input()) for _ in range(t): solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
def get_value(l, r, x, y): return abs(l - x) + abs(r - y) t = int(input()) for _ in range(t): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) lmin, rmax = min(l1, l2), max(r1, r2) ma = rmax - lmin if ma * n <= k: res = ( n * (get_value(l1, r1, lmin, rmax) + get_value(l2, r2, lmin, rmax)) + (k - ma * n) * 2 ) else: lmax, rmin = max(l1, l2), min(r1, r2) if lmax <= rmin: tmp = rmin - lmax res = k - tmp * n if k > tmp * n else 0 else: distance = lmax - rmin val = distance + ma res = val + 2 * (k - ma) if k > ma else distance + k num = (k + ma - 1) // ma res = min(res, distance * num + k) num = k // ma if num > 0: res = min(res, distance * num + k + (k - ma * num)) print(res)
FUNC_DEF RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
from sys import stdin for _ in range(int(stdin.readline())): n, k = map(int, stdin.readline().split()) l1, r1 = map(int, stdin.readline().split()) l2, r2 = map(int, stdin.readline().split()) curr = max(0, min(r1, r2) - max(l1, l2)) * n if curr > k: print(0) else: sin_inc = min(abs(l2 - l1) + abs(r2 - r1), max(r2, r1) - min(l2, l1)) waste = max(0, max(l2, l1) - min(r2, r1)) if k - curr <= sin_inc: print(waste + k - curr) elif sin_inc < k - curr <= sin_inc * n: moves = waste + sin_inc req = k - curr - sin_inc if sin_inc > waste: moves += req // sin_inc * (waste + sin_inc) moves += min(waste + req % sin_inc, req % sin_inc * 2) else: moves += req * 2 print(moves) else: moves = waste + sin_inc req = k - curr - sin_inc if sin_inc > waste: moves += min(req // sin_inc, n - 1) * (waste + sin_inc) if req // sin_inc >= n - 1: req -= sin_inc * (n - 1) moves += req * 2 else: moves += min(waste + req % sin_inc, req % sin_inc * 2) else: moves += req * 2 print(moves)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
for tc in range(int(input())): n, k = input().split(" ") al, ar = input().split(" ") bl, br = input().split(" ") n, k, al, ar, bl, br = int(n), int(k), int(al), int(ar), int(bl), int(br) if al > bl: al, ar, bl, br = bl, br, al, ar if ar < bl: blank = bl - ar get = br - al need = blank + get if k < get: print(blank + k) elif k <= n * get: print(int(k / get) * need + min(k % get, blank) + k % get) else: print(n * need + (k - n * get) * 2) else: get = max(br, ar) - al init = n * (min(br, ar) - bl) if k <= init: print(0) elif k <= n * get: print(k - init) else: print(n * get - init + 2 * (k - n * get))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP NUMBER BIN_OP VAR BIN_OP VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) if l2 >= r1 or l1 >= r2: if l1 >= r2: l1, r1, l2, r2 = l2, r2, l1, r1 d = l2 - r1 ans = d while n > 0: n -= 1 if r2 - l1 < k: ans += r2 - l1 k -= r2 - l1 if k < d: ans += 2 * k k = 0 break elif n > 0: ans += d else: ans += k k = 0 break if k > 0: ans += k * 2 print(ans) elif l2 <= r1 and r2 >= r1 and l2 >= l1: d = r1 - l2 k -= n * (r1 - l2) if k > 0: ans = 0 s = l2 - l1 + r2 - r1 if n * s < k: k -= n * s ans += n * s ans += 2 * k else: ans += k k = 0 else: ans = 0 print(ans) elif l1 <= r2 and l2 <= l1 and r2 <= r1: l1, r1, l2, r2 = l2, r2, l1, r1 d = r1 - l2 k -= n * (r1 - l2) if k > 0: ans = 0 s = l2 - l1 + r2 - r1 if n * s < k: k -= n * s ans += n * s ans += 2 * k else: ans += k k = 0 else: ans = 0 print(ans) else: if l2 <= l1 and r1 <= r2: l1, r1, l2, r2 = l2, r2, l1, r1 k -= (r2 - l2) * n if k > 0: s = l2 - l1 + r1 - r2 ans = 0 if n * s < k: k -= n * s ans += n * s ans += 2 * k else: ans += k k = 0 else: ans = 0 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
import sys input = sys.stdin.readline def solve(): n, k = map(int, input().split()) avail = 0 l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) if l2 < l1: l1, l2 = l2, l1 r1, r2 = r2, r1 avail = max(r2, r1) - min(l1, l2) if l2 <= r1: overlap = min(r1, r2) - max(l1, l2) k -= overlap * n if k <= 0: print(0) return dist = 0 avail -= overlap else: dist = l2 - r1 best = dist + 2 * k steps = 0 for i in range(n): steps += dist best = min(best, steps + max(0, k - avail) * 2 + min(k, avail)) steps += avail k -= avail print(best) for _ in range(int(input())): solve()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
from sys import stdin tt = int(stdin.readline()) for loop in range(tt): n, k = map(int, stdin.readline().split()) l1, r1 = map(int, stdin.readline().split()) l2, r2 = map(int, stdin.readline().split()) if l1 >= r2 or l2 >= r1: ans = float("inf") for usenum in range(1, n + 1): if max(abs(l1 - r2), abs(l2 - r1)) * usenum >= k: ans = min(ans, min(abs(l1 - r2), abs(l2 - r1)) * usenum + k) else: rem = k - max(abs(l1 - r2), abs(l2 - r1)) * usenum ans = min( ans, min(abs(l1 - r2), abs(l2 - r1)) * usenum + max(abs(l1 - r2), abs(l2 - r1)) * usenum + rem * 2, ) print(ans) else: ans = float("inf") over = min(r1 - l1, r2 - l2, r1 - l2, r2 - l1) usenum = n if over * usenum >= k: print(0) continue k -= over * usenum useable = max(r1 - l1, r2 - l2, r1 - l2, r2 - l1) - over if useable * usenum >= k: print(k) continue else: rem = k - useable * usenum print(useable * usenum + rem * 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
t = int(input()) for _ in range(t): n, k = list(map(int, input().split())) l1, r1 = list(map(int, input().split())) l2, r2 = list(map(int, input().split())) if r1 > r2: l1, r1, l2, r2 = l2, r2, l1, r1 overlap = max(0, r1 - max(l1, l2)) k -= overlap * n if k <= 0: print(0) continue touch_cost = max(0, max(l1, l2) - min(r1, r2)) overlap_cost = max(r2, r1) - min(l1, l2) - overlap overlap_gain = max(r1, r2) - min(l1, l2) ans = 2 * k + touch_cost for i in range(1, n + 1): poss = touch_cost * i if i * overlap_cost >= k: poss += k else: poss += 2 * k - overlap_cost * i ans = min(poss, ans) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
for _ in range(int(input())): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) ans = 0 if max(l1, l2) <= min(r1, r2): k -= (min(r1, r2) - max(l1, l2)) * n if k <= 0: print(0) else: add = max(r1, r2) - min(l1, l2) - (min(r1, r2) - max(l1, l2)) if k - add * n <= 0: print(k) else: print(add * n + (k - add * n) * 2) else: ni = max(l1, l2) - min(r1, r2) bg = max(r1, r2) - min(l1, l2) for i in range(n): ans += ni ans += min(k, bg) k -= bg if k <= 0: break if k <= ni: ans += k * 2 k = -1 break if k <= 0: print(ans) else: print(ans + k * 2)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
import sys readline = sys.stdin.buffer.readline def even(n): return 1 if n % 2 == 0 else 0 def main(): n, k = map(int, readline().split()) a = list(map(int, readline().split())) b = list(map(int, readline().split())) if a[0] > b[0]: a, b = b, a score = 0 cost = 0 score_init = max(0, min(a[1], b[1]) - b[0]) * n if score_init >= k: print(0) return if a[0] <= b[0] and b[1] <= a[1]: res = b[0] - a[0] + a[1] - b[1] score += res * n cost += res * n if k <= score + score_init: print(k - score_init) return elif a[1] > b[0]: res = b[0] - a[0] + b[1] - a[1] score += res * n cost += res * n if k <= score + score_init: print(k - score_init) return else: one_term_score = b[1] - a[0] one_term_cost = a[1] - a[0] + b[1] - b[0] + (b[0] - a[1]) * 2 if k < one_term_score * n: let = k // one_term_score if let == 0: print(b[0] - a[1] + k) return score += one_term_score * let cost += one_term_cost * let if k - score < b[0] - a[1]: pass else: cost += b[0] - a[1] + (k - score) score = k else: score += one_term_score * n cost += one_term_cost * n if score + score_init < k: cost += 2 * (k - score - score_init) print(cost) n = int(readline()) for i in range(n): main()
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP VAR NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR RETURN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
import sys input = sys.stdin.readline t = int(input()) for ii in range(t): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) inter, diff = 0, 0 left, right = max(l1, l2), min(r1, r2) left1, right1 = min(l1, l2), max(r1, r2) maxpos = right1 - left1 inter = max(0, right - left) intertotal = max(0, right - left) * n maxpostotal = (right1 - left1) * n pos, ans = intertotal, 0 if k > maxpostotal: ans += (k - maxpostotal) * 2 pos = k - maxpostotal if intertotal >= k: print(0) else: for i in range(n): if inter != 0: if pos + (maxpos - inter) <= k: ans += maxpos - inter pos += maxpos - inter else: ans += k - pos break elif pos + maxpos <= k: ans += maxpos + (left - right) pos += maxpos else: op1 = 100000000000000 if i > 0: op1 = (k - pos) * 2 op2 = k - pos + left - right ans += min(op1, op2) break print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
import sys sys.setrecursionlimit(10**5) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] for _ in range(II()): inf = 10**16 n, k = MI() al, ar = MI() bl, br = MI() if al > bl: al, ar, bl, br = bl, br, al, ar d = max(0, min(ar, br) - max(al, bl)) k -= d * n if k <= 0: print(0) continue if (al, ar) == (bl, br): print(k * 2) continue cost1 = max(0, bl - ar) score = 0 ans1 = cost1 ar = max(ar, bl) cost2 = abs(al - bl) + abs(ar - br) score += min(cost2, k) ans1 += min(cost2, k) + (k - score) * 2 score = 0 ans2 = ans3 = inf t = min(k // cost2, n) if t: score += t * cost2 ans2 = ans3 = t * (cost1 + cost2) ans2 += (k - score) * 2 if t < n: ans3 += cost1 + (k - score) else: ans3 = inf print(min(ans1, ans2, ans3))
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) mr = max(r1, r2) - min(l1, l2) step = 2 * mr - (r1 - l1) - (r2 - l2) over = max(0, min(r1, r2) - max(l1, l2)) dis = max(l1, l2) - min(r1, r2) if over == 0 else 0 k = k - over * n if k <= 0: print(0) continue mr -= over res = 0 if k <= mr: res = dis + k elif mr * n >= k: cnt = k // mr r = k % mr res += cnt * step if r > dis: res += dis + r else: res += 2 * r else: res += n * step + (k - mr * n) * 2 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) a, b = map(int, input().split()) c, d = map(int, input().split()) if c < a: a, b, c, d = c, d, a, b elif c == a and b < d: a, b, c, d = c, d, a, b count = 0 if b > c: if d > b: k -= (b - c) * n else: k -= (d - c) * n if k <= 0: print(0) continue if b > c: if d > b: cal = (c + d - a - b) * n if cal >= k: print(k) else: print(cal + (k - cal) * 2) else: cal = (c + b - a - d) * n if cal >= k: print(k) else: print(cal + (k - cal) * 2) else: cost = c - a + d - b get = d - a if k <= get: print(c - b + k) else: count = cost k -= get if get * 2 <= cost: print(count + k * 2) else: ma = k // get if ma >= n - 1: print(cost * n + (k - get * (n - 1)) * 2) else: count += ma * cost k -= ma * get print(count + min(k * 2, c - b + k))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
import sys sys.setrecursionlimit(10**5) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] for _ in range(II()): n, k = MI() al, ar = MI() bl, br = MI() if al > bl: al, ar, bl, br = bl, br, al, ar p1 = max(0, bl - ar) p2 = abs(al - bl) + abs(ar - br) d = max(0, min(ar, br) - max(al, bl)) k -= d * n if k <= 0: print(0) continue if p2 == 0: print(k * 2) continue if k // (p2 - p1) == 0 or p1 * 2 - p2 >= 0: ans = p1 cost = min(p2 - p1, k) ans += cost k -= cost if k: ans += 2 * k else: t = min(k // (p2 - p1), n) n -= t k -= (p2 - p1) * t ans = p2 * t if n and k > p1: ans += k + p1 else: ans += 2 * k print(ans)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR IF VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
import sys readline = sys.stdin.readline def solve(): N, K = map(int, readline().split()) L1, R1 = map(int, readline().split()) L2, R2 = map(int, readline().split()) if L1 > L2: L1, L2 = L2, L1 R1, R2 = R2, R1 if R1 < L2: gap = L2 - R1 union = R2 - L1 ans = gap if K <= union: ans += K print(ans) return ans += union rem = K - union if N == 1: print(ans + 2 * rem) return t = min(rem // union, N - 1) ans += t * (gap + union) rem -= t * union if t == N - 1: print(ans + 2 * rem) return if 2 * rem < gap + rem: print(ans + 2 * rem) return else: print(ans + gap + rem) return else: ovl = min(R1, R2) - L2 if ovl * N >= K: print(0) return union = max(R1, R2) - L1 if union * N >= K: print(K - ovl * N) return else: ans = union * N - ovl * N ans += 2 * (K - union * N) print(ans) return T = int(readline()) for i in range(T): solve()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN IF BIN_OP NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR RETURN ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP NUMBER BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR