description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
It is a complicated version of problem F1. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 10$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 6 2021 3 177890 2 34512 3 724533 4 998244353 1 12345678 10 Output 2021 181111 34533 724542 999999999 12345678 -----Note----- None
def brute(n, k): for i in range(int(n), 10 ** len(n)): if len(set(str(i))) <= k: return i def strange(n, k): if len(set(n)) <= k: return int(n) str_n = n n = list(map(int, list(n))) minn = float("inf") for i in range(len(n) - 1, -1, -1): rp = len(n) - 1 - i if len(set(n[0:i])) > k: continue if len(set(n[0:i])) == k: ll = list(filter(lambda x: x > n[i], n[0:i])) if len(ll): v1 = int( str_n[0:i] + str(min(ll)) + str(min(n[0:i])) * (len(n) - 1 - i) ) if v1 >= int(str_n) and v1 < minn: minn = v1 elif len(set(n[0:i])) == k - 1: v1 = int(str_n[0:i] + str(n[i]) + str(min(n[0 : i + 1])) * (len(n) - 1 - i)) ll = list(filter(lambda x: x > n[i], n[0:i])) v2 = None if len(ll): v2 = int(str_n[0:i] + str(min(ll)) + "0" * (len(n) - 1 - i)) v3 = int( str_n[0:i] + str(n[i] + 1) + str(min(n[0:i] + [n[i] + 1])) * (len(n) - 1 - i) ) for v in [v1, v2, v3]: if not v: continue if v >= int(str_n) and v < minn: minn = v else: v1 = int(str_n[0:i] + str(n[i]) + str(min(n[0 : i + 1])) * (len(n) - 1 - i)) ll = list(filter(lambda x: x > n[i], n[0:i])) v2 = None if len(ll): v2 = int(str_n[0:i] + str(min(ll)) + "0" * (len(n) - 1 - i)) v3 = int( str_n[0:i] + str(n[i] + 1) + str(min(n[0:i] + [n[i] + 1])) * (len(n) - 1 - i) ) v4 = int(str_n[0:i] + str(n[i]) + "0" * (len(n) - 1 - i)) v5 = int(str_n[0:i] + str(n[i] + 1) + "0" * (len(n) - 1 - i)) for v in [v1, v2, v3, v4, v5]: if not v: continue if v >= int(str_n) and v < minn: minn = v return minn t = int(input()) for _ in range(t): n, k = input().split(" ") print(strange(n, int(k)))
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR ASSIGN VAR NONE IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP STRING BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR LIST BIN_OP VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR FOR VAR LIST VAR VAR VAR IF VAR IF VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR ASSIGN VAR NONE IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP STRING BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR LIST BIN_OP VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP STRING BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP STRING BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR FOR VAR LIST VAR VAR VAR VAR VAR IF VAR IF VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
It is a complicated version of problem F1. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 10$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 6 2021 3 177890 2 34512 3 724533 4 998244353 1 12345678 10 Output 2021 181111 34533 724542 999999999 12345678 -----Note----- None
l = len _, *t = open(0) for p in t: x, k = p.split() k = int(k) n = x while l(set(x)) > k: x = str(int(x) + 1).strip("0") print(x + (l(n) - l(x)) * min(x + "0" * (l(set(x)) < k)))
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
It is a complicated version of problem F1. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 10$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 6 2021 3 177890 2 34512 3 724533 4 998244353 1 12345678 10 Output 2021 181111 34533 724542 999999999 12345678 -----Note----- None
def one(n: str, lili: dict): sel = n[0] def have_greater_in_lili_or_sel(digit: str) -> bool: for li, exists in lili.items(): if exists and li > digit: return True return sel > digit for digit in n[1:]: if have_greater_in_lili_or_sel(digit): break if digit in [d for d in lili if lili[d]]: continue if digit > sel: sel = str(int(sel) + 1) if lili[sel]: lili["0"] = True break lili[sel] = True def run(n: str, k: int, lili: dict): if not n or not k: return elif lili[n[0]]: run(n[1:], k, lili) elif k == 1: one(n, lili) else: lili[n[0]] = True run(n[1:], k - 1, lili) def clean(n: str, lili: dict) -> str: def least_greater(n_dig: str, res_dig: str) -> str: best = res_dig for digit, exists in lili.items(): if exists and digit > n_dig: best = min(best, digit) return best maybe_a_better_resut = "" min_dig = min(digit for digit in "0123456789" if lili[digit]) max_dig = max(digit for digit in "0123456789" if lili[digit]) for curr_index, n_dig in enumerate(n): best = least_greater(str(int(n_dig) - 1), max_dig) if best > n_dig: maybe_a_better_resut += best for _ in range(len(n) - curr_index - 1): maybe_a_better_resut += min_dig break elif best < n_dig: parent = maybe_a_better_resut[curr_index - 1] maybe_a_better_resut = maybe_a_better_resut[ : curr_index - 1 ] + least_greater(maybe_a_better_resut[curr_index - 1], max_dig) if parent == maybe_a_better_resut[curr_index - 1]: n = n[: curr_index - 1] + str(int(parent) + 1) + n[curr_index:] return clean(n, lili) for _ in range(len(n) - curr_index): maybe_a_better_resut += min_dig break else: maybe_a_better_resut += best return maybe_a_better_resut t = int(input()) for i in range(1, t + 1): n, k = input().split() lili = {digit: (False) for digit in "0123456789"} if len(set(n)) == int(k): print(n) continue run(n, int(k), lili) result = clean(n, lili) print(result)
FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER FUNC_DEF VAR FOR VAR VAR FUNC_CALL VAR IF VAR VAR VAR RETURN NUMBER RETURN VAR VAR VAR FOR VAR VAR NUMBER IF FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR STRING NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF VAR VAR VAR IF VAR VAR RETURN IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF VAR VAR FUNC_DEF VAR VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR STRING VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR STRING IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
It is a complicated version of problem F1. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 10$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 6 2021 3 177890 2 34512 3 724533 4 998244353 1 12345678 10 Output 2021 181111 34533 724542 999999999 12345678 -----Note----- None
def int_(l): return int("".join(str(kk) for kk in l)) def f(n, k): if k == 1: fdig = n[0] ff = int_([fdig] * len(n)) nn = int_(n) if ff >= nn: return ff else: return int_([fdig + 1] * len(n)) digs = set() ii = -1 for i in range(len(n)): if len(digs) == k and n[i] not in digs: ii = i break digs.add(n[i]) if ii == -1: return int_(n) dg = n[ii] for ddd in range(dg + 1, 10): if ddd in digs: ans = n[:ii] + [ddd] + [min(digs)] * (len(n) - ii - 1) return int_(ans) nw = n[: ii - 1] + [n[ii - 1] + 1] + [0] * (len(n) - ii) return f(nw, k) for _ in range(int(input())): n_, b = input().split() k = int(b) n = [int(ii) for ii in n_] print(f(n, k))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP LIST VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR RETURN FUNC_CALL VAR BIN_OP LIST BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR LIST VAR BIN_OP LIST FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
It is a complicated version of problem F1. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 10$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 6 2021 3 177890 2 34512 3 724533 4 998244353 1 12345678 10 Output 2021 181111 34533 724542 999999999 12345678 -----Note----- None
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, k = input()[:-1].split() k = int(k) ni = int(n) t = "" ans = 10**18 for i in range(len(n)): for j in range(1, 10): c = len(set(t + str(int(n[i]) + j))) if c > k: continue elif c == k: d = ord(min(t + str(int(n[i]) + j))) - ord("0") x = int(t + str(int(n[i]) + j)) while ni > x: x *= 10 x += d if ans > x: ans = x else: x = int(t + str(int(n[i]) + j)) while ni > x: x *= 10 if ans > x: ans = x t += n[i] if len(set(n)) <= k: ans = ni 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 FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
It is a complicated version of problem F1. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 10$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 6 2021 3 177890 2 34512 3 724533 4 998244353 1 12345678 10 Output 2021 181111 34533 724542 999999999 12345678 -----Note----- None
def _measure_k(x): return len(set(str(x))) tests_n = int(input()) for i_test in range(tests_n): n, k = map(int, input().split()) prefix = n while _measure_k(prefix) > k: if prefix % 10 == 0: prefix //= 10 else: prefix += 1 prefix = str(prefix) tail_digit = "0" if _measure_k(prefix) < k else min(prefix) tail = tail_digit * (len(str(n)) - len(prefix)) print(prefix + tail)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 WHILE FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
It is a complicated version of problem F1. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 10$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 6 2021 3 177890 2 34512 3 724533 4 998244353 1 12345678 10 Output 2021 181111 34533 724542 999999999 12345678 -----Note----- None
for i in range(int(input())): n, k, x, tt = map(int, input().split() * 2) while len(set(str(x))) > k: x = x // 10 if not x % 10 else x + 1 print( str(x) + ("0" if len(set(str(x))) < k else min(str(x))) * (len(str(n)) - len(str(x))) )
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL FUNC_CALL VAR NUMBER WHILE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR
It is a complicated version of problem F1. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 10$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 6 2021 3 177890 2 34512 3 724533 4 998244353 1 12345678 10 Output 2021 181111 34533 724542 999999999 12345678 -----Note----- None
T = int(input()) for t in range(T): n, k = map(int, input().split()) s = str(n) if k == 1: t = s[0] * len(s) if t >= s: print(t) else: t = chr(ord(s[0]) + 1) * len(s) print(t) else: cur, cnt = s[0], 0 ans = "" ans += s[0] isUsed = [(False) for _ in range(10)] isUsed[ord(s[0]) - ord("0")] = True mx = ord(s[0]) - ord("0") for i in range(1, len(s)): x = ord(s[i]) - ord("0") if s[i] == s[i - 1]: ans += s[i] else: if not isUsed[x]: k -= 1 if k == 1: break else: ans += s[i] isUsed[x] = True mx = max(mx, x) cnt = len(ans) for i in range(len(s) - cnt): for d in range(10): t = ( ans + chr(d + ord("0")) + chr(max(d, mx) + ord("0")) * (len(s) - cnt - i - 1) ) if not isUsed[d] and k == 1 and t >= s: ans += chr(d + ord("0")) isUsed[d] = True mx = max(mx, d) k -= 1 break elif isUsed[d] and t >= s: ans += chr(d + ord("0")) break 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 FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR STRING VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
It is a complicated version of problem F1. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 10$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 6 2021 3 177890 2 34512 3 724533 4 998244353 1 12345678 10 Output 2021 181111 34533 724542 999999999 12345678 -----Note----- None
import sys input = sys.stdin.readline t = int(input()) for tests in range(t): n, k = map(int, input().split()) S = str(n) x = "" ANS = int(str(min(9, int(S[0]) + 1)) * len(S)) for i in range(len(S)): x += S[i] if len(set(x)) > k: break if i == len(S) - 1: ANS = n if i != len(S) - 1: j = S[i + 1] for t in sorted(x): if t > j: break if t > j: y = x + t if len(set(y)) == k: u = min(y) else: u = "0" ANS = min(ANS, int(y + u * (len(S) - len(y)))) if i != len(S) - 1: j = S[i + 1] if j == "9": continue y = x + str(int(j) + 1) if len(set(y)) > k: continue if len(set(y)) == k: u = min(y) else: u = "0" ANS = min(ANS, int(y + u * (len(S) - len(y)))) 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 FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
It is a complicated version of problem F1. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 10$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 6 2021 3 177890 2 34512 3 724533 4 998244353 1 12345678 10 Output 2021 181111 34533 724542 999999999 12345678 -----Note----- None
def upList(s, i): left = s[:i] right = s[i:] left = list(str(int("".join(left)) + 1)) return left + right def jg(x, val): y = set(i for i in x if i >= val) if len(y) == 0: return None return min(y) for _ in range(int(input())): n, k = map(int, input().split()) s = list(str(n)) i = len(s) - 1 while i >= 0: cMax = set() j = 0 while len(cMax) < k and j < len(s): cMax.add(s[j]) j += 1 if j == len(s): break if s[i] not in cMax: z = jg(cMax, s[i]) if z == None: s = upList(s, i) for var in range(i, len(s)): s[var] = "0" i = len(s) - 1 else: s[i] = z for var in range(i + 1, len(s)): s[var] = min(cMax) i -= 1 else: i -= 1 print("".join(s))
FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NONE RETURN FUNC_CALL 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 FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
This is an easier version of the next problem. The difference is only in constraints. You are given a rectangular $n \times m$ matrix $a$. In one move you can choose any column and cyclically shift elements in this column. You can perform this operation as many times as you want (possibly zero). You can perform this operation to a column multiple times. After you are done with cyclical shifts, you compute for every row the maximal value in it. Suppose that for $i$-th row it is equal $r_i$. What is the maximal possible value of $r_1+r_2+\ldots+r_n$? -----Input----- The first line contains an integer $t$ ($1 \le t \le 40$), the number of test cases in the input. The first line of each test case contains integers $n$ and $m$ ($1 \le n \le 4$, $1 \le m \le 100$) β€” the number of rows and the number of columns in the given matrix $a$. Each of the following $n$ lines contains $m$ integers, the elements of $a$ ($1 \le a_{i, j} \le 10^5$). -----Output----- Print $t$ integers: answers for all test cases in the order they are given in the input. -----Example----- Input 2 2 3 2 5 7 4 2 4 3 6 4 1 5 2 10 4 8 6 6 4 9 10 5 4 9 5 8 7 Output 12 29 -----Note----- In the first test case, you can shift the third column down by one, this way there will be $r_1 = 5$ and $r_2 = 7$. In the second case you can don't rotate anything at all, this way there will be $r_1 = r_2 = 10$ and $r_3 = 9$.
t = int(input()) def maxsa(A): ans = 0 for i in range(n): cur_maxx = 0 for j in range(4): cur_maxx = max(cur_maxx, A[j][i]) ans += cur_maxx return ans def fu(A): answer = 0 for j in range(n): A[0] = A[0][1:] + A[0][:1] for i in range(n): A[1] = A[1][1:] + A[1][:1] for k in range(n): A[2] = A[2][1:] + A[2][:1] for l in range(n): A[3] = A[3][1:] + A[3][:1] cur_ans = maxsa(A) answer = max(answer, cur_ans) return answer for j in range(t): n, m = map(int, input().split()) A = [0] * n inds = [-1, -1, -1, -1] maxs = [0, 0, 0, 0] for j in range(n): A[j] = list(map(int, input().split())) for j in range(m): cur_maxs = 0 for i in range(n): cur_maxs = max(cur_maxs, A[i][j]) maxs.append(cur_maxs) inds.append(j) ind = 4 while ind != 0 and maxs[ind] > maxs[ind - 1]: inds[ind], inds[ind - 1] = inds[ind - 1], inds[ind] maxs[ind], maxs[ind - 1] = maxs[ind - 1], maxs[ind] ind -= 1 maxs.pop() inds.pop() S = [0] * 4 for j in range(4): if inds[j] != -1: S[j] = [s[inds[j]] for s in A] else: S[j] = [0] * n print(fu(S))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
This is an easier version of the next problem. The difference is only in constraints. You are given a rectangular $n \times m$ matrix $a$. In one move you can choose any column and cyclically shift elements in this column. You can perform this operation as many times as you want (possibly zero). You can perform this operation to a column multiple times. After you are done with cyclical shifts, you compute for every row the maximal value in it. Suppose that for $i$-th row it is equal $r_i$. What is the maximal possible value of $r_1+r_2+\ldots+r_n$? -----Input----- The first line contains an integer $t$ ($1 \le t \le 40$), the number of test cases in the input. The first line of each test case contains integers $n$ and $m$ ($1 \le n \le 4$, $1 \le m \le 100$) β€” the number of rows and the number of columns in the given matrix $a$. Each of the following $n$ lines contains $m$ integers, the elements of $a$ ($1 \le a_{i, j} \le 10^5$). -----Output----- Print $t$ integers: answers for all test cases in the order they are given in the input. -----Example----- Input 2 2 3 2 5 7 4 2 4 3 6 4 1 5 2 10 4 8 6 6 4 9 10 5 4 9 5 8 7 Output 12 29 -----Note----- In the first test case, you can shift the third column down by one, this way there will be $r_1 = 5$ and $r_2 = 7$. In the second case you can don't rotate anything at all, this way there will be $r_1 = r_2 = 10$ and $r_3 = 9$.
import sys input = sys.stdin.readline t = int(input()) for testcases in range(t): n, m = list(map(int, input().split())) A = [list(map(int, input().split())) for i in range(n)] B = [] for j in range(m): B.append([A[i][j] for i in range(n)]) B.sort(key=lambda x: max(x), reverse=True) B = B[:n] LEN = len(B) if LEN == 1: print(sum(B[0])) elif LEN == 2: ANS = 0 for i in range(n): A = 0 for k in range(n): A += max(B[0][k], B[1][(i + k) % n]) ANS = max(ANS, A) print(ANS) elif LEN == 3: ANS = 0 for i in range(n): for j in range(n): A = 0 for k in range(n): A += max(B[0][k], B[1][(i + k) % n], B[2][(j + k) % n]) ANS = max(ANS, A) print(ANS) elif LEN == 4: ANS = 0 for i in range(n): for j in range(n): for l in range(n): A = 0 for k in range(n): A += max( B[0][k], B[1][(i + k) % n], B[2][(j + k) % n], B[3][(l + k) % n], ) ANS = max(ANS, A) 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is an easier version of the next problem. The difference is only in constraints. You are given a rectangular $n \times m$ matrix $a$. In one move you can choose any column and cyclically shift elements in this column. You can perform this operation as many times as you want (possibly zero). You can perform this operation to a column multiple times. After you are done with cyclical shifts, you compute for every row the maximal value in it. Suppose that for $i$-th row it is equal $r_i$. What is the maximal possible value of $r_1+r_2+\ldots+r_n$? -----Input----- The first line contains an integer $t$ ($1 \le t \le 40$), the number of test cases in the input. The first line of each test case contains integers $n$ and $m$ ($1 \le n \le 4$, $1 \le m \le 100$) β€” the number of rows and the number of columns in the given matrix $a$. Each of the following $n$ lines contains $m$ integers, the elements of $a$ ($1 \le a_{i, j} \le 10^5$). -----Output----- Print $t$ integers: answers for all test cases in the order they are given in the input. -----Example----- Input 2 2 3 2 5 7 4 2 4 3 6 4 1 5 2 10 4 8 6 6 4 9 10 5 4 9 5 8 7 Output 12 29 -----Note----- In the first test case, you can shift the third column down by one, this way there will be $r_1 = 5$ and $r_2 = 7$. In the second case you can don't rotate anything at all, this way there will be $r_1 = r_2 = 10$ and $r_3 = 9$.
from sys import stdin def f(lst, num): new = lst[num:] + lst[:num] return new t = int(stdin.readline()) for i in range(t): row, col = tuple(int(x) for x in stdin.readline().split()) lst = list([int(x)] for x in stdin.readline().split()) for j in range(row - 1): line = tuple(int(x) for x in stdin.readline().split()) for k in range(len(line)): lst[k].append(line[k]) lst.sort(key=lambda x: max(x), reverse=True) ans = float("-inf") for a in range(4): for b in range(4): for c in range(4): for d in range(4): if col >= 1: aa = f(lst[0], a) else: aa = (0,) * row if col >= 2: bb = f(lst[1], b) else: bb = (0,) * row if col >= 3: cc = f(lst[2], c) else: cc = (0,) * row if col >= 4: dd = f(lst[3], d) else: dd = (0,) * row ans = max( ans, sum(max(x[j] for x in (aa, bb, cc, dd)) for j in range(row)), ) print(ans)
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is an easier version of the next problem. The difference is only in constraints. You are given a rectangular $n \times m$ matrix $a$. In one move you can choose any column and cyclically shift elements in this column. You can perform this operation as many times as you want (possibly zero). You can perform this operation to a column multiple times. After you are done with cyclical shifts, you compute for every row the maximal value in it. Suppose that for $i$-th row it is equal $r_i$. What is the maximal possible value of $r_1+r_2+\ldots+r_n$? -----Input----- The first line contains an integer $t$ ($1 \le t \le 40$), the number of test cases in the input. The first line of each test case contains integers $n$ and $m$ ($1 \le n \le 4$, $1 \le m \le 100$) β€” the number of rows and the number of columns in the given matrix $a$. Each of the following $n$ lines contains $m$ integers, the elements of $a$ ($1 \le a_{i, j} \le 10^5$). -----Output----- Print $t$ integers: answers for all test cases in the order they are given in the input. -----Example----- Input 2 2 3 2 5 7 4 2 4 3 6 4 1 5 2 10 4 8 6 6 4 9 10 5 4 9 5 8 7 Output 12 29 -----Note----- In the first test case, you can shift the third column down by one, this way there will be $r_1 = 5$ and $r_2 = 7$. In the second case you can don't rotate anything at all, this way there will be $r_1 = r_2 = 10$ and $r_3 = 9$.
q = int(input()) for rquer in range(q): c, r = list(map(int, input().split())) matt = [list(map(int, input().split())) for i in range(c)] mat = [[matt[i][j] for i in range(c)] for j in range(r)] for i in range(r): mat[i].append(max(mat[i])) mat[i].reverse() mat.sort() mat.reverse() work = mat[: min(4, r)] for t in work: t.pop(0) r = min(4, r) wyn = 0 for num in range(c**r): shif = [(num // c**i % c) for i in range(r)] new = 0 for i in range(c): kol = [work[j][(i + shif[j]) % c] for j in range(r)] new += max(kol) wyn = max(wyn, new) print(wyn)
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is an easier version of the next problem. The difference is only in constraints. You are given a rectangular $n \times m$ matrix $a$. In one move you can choose any column and cyclically shift elements in this column. You can perform this operation as many times as you want (possibly zero). You can perform this operation to a column multiple times. After you are done with cyclical shifts, you compute for every row the maximal value in it. Suppose that for $i$-th row it is equal $r_i$. What is the maximal possible value of $r_1+r_2+\ldots+r_n$? -----Input----- The first line contains an integer $t$ ($1 \le t \le 40$), the number of test cases in the input. The first line of each test case contains integers $n$ and $m$ ($1 \le n \le 4$, $1 \le m \le 100$) β€” the number of rows and the number of columns in the given matrix $a$. Each of the following $n$ lines contains $m$ integers, the elements of $a$ ($1 \le a_{i, j} \le 10^5$). -----Output----- Print $t$ integers: answers for all test cases in the order they are given in the input. -----Example----- Input 2 2 3 2 5 7 4 2 4 3 6 4 1 5 2 10 4 8 6 6 4 9 10 5 4 9 5 8 7 Output 12 29 -----Note----- In the first test case, you can shift the third column down by one, this way there will be $r_1 = 5$ and $r_2 = 7$. In the second case you can don't rotate anything at all, this way there will be $r_1 = r_2 = 10$ and $r_3 = 9$.
t = int(input()) for i in range(t): n, m = [int(item) for item in input().split()] mat = [] col = [[] for _ in range(m)] for j in range(n): line = [int(item) for item in input().split()] for k, item in enumerate(line): col[k].append(item) mat.append(line) colmax = [] for line in col: colmax.append([max(line), line]) colmax.sort(reverse=True) colmax = colmax[:n] ans = 0 for j in range(n ** (n - 1)): index = j rot = [0] for k in range(n - 1): rot.append(index % n) index //= n ret = 0 for l in range(n): val = 0 for k in range(len(colmax)): val = max(val, colmax[k][1][(l + rot[k]) % n]) ret += val ans = max(ans, ret) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is an easier version of the next problem. The difference is only in constraints. You are given a rectangular $n \times m$ matrix $a$. In one move you can choose any column and cyclically shift elements in this column. You can perform this operation as many times as you want (possibly zero). You can perform this operation to a column multiple times. After you are done with cyclical shifts, you compute for every row the maximal value in it. Suppose that for $i$-th row it is equal $r_i$. What is the maximal possible value of $r_1+r_2+\ldots+r_n$? -----Input----- The first line contains an integer $t$ ($1 \le t \le 40$), the number of test cases in the input. The first line of each test case contains integers $n$ and $m$ ($1 \le n \le 4$, $1 \le m \le 100$) β€” the number of rows and the number of columns in the given matrix $a$. Each of the following $n$ lines contains $m$ integers, the elements of $a$ ($1 \le a_{i, j} \le 10^5$). -----Output----- Print $t$ integers: answers for all test cases in the order they are given in the input. -----Example----- Input 2 2 3 2 5 7 4 2 4 3 6 4 1 5 2 10 4 8 6 6 4 9 10 5 4 9 5 8 7 Output 12 29 -----Note----- In the first test case, you can shift the third column down by one, this way there will be $r_1 = 5$ and $r_2 = 7$. In the second case you can don't rotate anything at all, this way there will be $r_1 = r_2 = 10$ and $r_3 = 9$.
rnd_mod = 1234567890133 rnd_x = 987654321098 def rnd(): nonlocal rnd_x rnd_x = rnd_x**2 % rnd_mod return (rnd_x >> 5) % (1 << 20) def randrange(a): return rnd() % a T = int(input()) for _ in range(T): N, M = list(map(int, input().split())) X = [] for __ in range(N): X.append([int(a) for a in input().split()]) Y = [[X[i][j] for i in range(N)] for j in range(M)] ma = 0 for t in range(577): for i in range(M): a = randrange(N) Y[i] = [Y[i][j - a] for j in range(N)] ma = max(ma, sum([max([Y[i][j] for i in range(M)]) for j in range(N)])) print(ma)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR 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 LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is an easier version of the next problem. The difference is only in constraints. You are given a rectangular $n \times m$ matrix $a$. In one move you can choose any column and cyclically shift elements in this column. You can perform this operation as many times as you want (possibly zero). You can perform this operation to a column multiple times. After you are done with cyclical shifts, you compute for every row the maximal value in it. Suppose that for $i$-th row it is equal $r_i$. What is the maximal possible value of $r_1+r_2+\ldots+r_n$? -----Input----- The first line contains an integer $t$ ($1 \le t \le 40$), the number of test cases in the input. The first line of each test case contains integers $n$ and $m$ ($1 \le n \le 4$, $1 \le m \le 100$) β€” the number of rows and the number of columns in the given matrix $a$. Each of the following $n$ lines contains $m$ integers, the elements of $a$ ($1 \le a_{i, j} \le 10^5$). -----Output----- Print $t$ integers: answers for all test cases in the order they are given in the input. -----Example----- Input 2 2 3 2 5 7 4 2 4 3 6 4 1 5 2 10 4 8 6 6 4 9 10 5 4 9 5 8 7 Output 12 29 -----Note----- In the first test case, you can shift the third column down by one, this way there will be $r_1 = 5$ and $r_2 = 7$. In the second case you can don't rotate anything at all, this way there will be $r_1 = r_2 = 10$ and $r_3 = 9$.
for _ in range(int(input())): N, M = map(int, input().split()) X = [[int(a) for a in input().split()] for _ in range(N)] Y = [[X[i][j] for i in range(N)] for j in range(M)] ma = 0 dp = [([0] * (1 << N)) for _ in range(M + 1)] for j in range(M): for mask in range(1 << N): maskpre = mask while maskpre >= 0: maskpre &= mask ma = 0 for k in range(N): s = 0 for i in range(N): if maskpre >> i & 1 == 0 and mask >> i & 1: s += X[i - k][j] ma = max(ma, s) dp[j + 1][mask] = max(dp[j + 1][mask], dp[j][maskpre] + ma) maskpre -= 1 print(dp[-1][-1])
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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER
This is an easier version of the next problem. The difference is only in constraints. You are given a rectangular $n \times m$ matrix $a$. In one move you can choose any column and cyclically shift elements in this column. You can perform this operation as many times as you want (possibly zero). You can perform this operation to a column multiple times. After you are done with cyclical shifts, you compute for every row the maximal value in it. Suppose that for $i$-th row it is equal $r_i$. What is the maximal possible value of $r_1+r_2+\ldots+r_n$? -----Input----- The first line contains an integer $t$ ($1 \le t \le 40$), the number of test cases in the input. The first line of each test case contains integers $n$ and $m$ ($1 \le n \le 4$, $1 \le m \le 100$) β€” the number of rows and the number of columns in the given matrix $a$. Each of the following $n$ lines contains $m$ integers, the elements of $a$ ($1 \le a_{i, j} \le 10^5$). -----Output----- Print $t$ integers: answers for all test cases in the order they are given in the input. -----Example----- Input 2 2 3 2 5 7 4 2 4 3 6 4 1 5 2 10 4 8 6 6 4 9 10 5 4 9 5 8 7 Output 12 29 -----Note----- In the first test case, you can shift the third column down by one, this way there will be $r_1 = 5$ and $r_2 = 7$. In the second case you can don't rotate anything at all, this way there will be $r_1 = r_2 = 10$ and $r_3 = 9$.
def solve(matrix, col, N, M): if col == M: ans = 0 for row in matrix: if len(row) == 1: ans += row[0] else: ans += max(*row) return ans if N == 1: return solve(matrix, col + 1, N, M) ans = solve(matrix, col + 1, N, M) for _ in range(N - 1): tmp = matrix[0][col] for n in range(1, N): matrix[n - 1][col] = matrix[n][col] matrix[N - 1][col] = tmp local_ans = solve(matrix, col + 1, N, M) if local_ans > ans: ans = local_ans return ans def main(): T = int(input()) for t in range(T): N, M = list([int(x) for x in input().split()]) matrix = [] for n in range(N): matrix.append(list([int(x) for x in input().split()])) elements = [] for n in range(N): for m in range(M): elements.append((matrix[n][m], m)) elements.sort(reverse=True) candidates = [] for t in elements: if t[1] not in candidates: candidates.append(t[1]) if len(candidates) == N: break simplified = [] for n in range(N): row = [] for m in candidates: row.append(matrix[n][m]) simplified.append(row) ans = solve(simplified, 0, N, min(N, M)) print(ans) main()
FUNC_DEF IF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
This is an easier version of the next problem. The difference is only in constraints. You are given a rectangular $n \times m$ matrix $a$. In one move you can choose any column and cyclically shift elements in this column. You can perform this operation as many times as you want (possibly zero). You can perform this operation to a column multiple times. After you are done with cyclical shifts, you compute for every row the maximal value in it. Suppose that for $i$-th row it is equal $r_i$. What is the maximal possible value of $r_1+r_2+\ldots+r_n$? -----Input----- The first line contains an integer $t$ ($1 \le t \le 40$), the number of test cases in the input. The first line of each test case contains integers $n$ and $m$ ($1 \le n \le 4$, $1 \le m \le 100$) β€” the number of rows and the number of columns in the given matrix $a$. Each of the following $n$ lines contains $m$ integers, the elements of $a$ ($1 \le a_{i, j} \le 10^5$). -----Output----- Print $t$ integers: answers for all test cases in the order they are given in the input. -----Example----- Input 2 2 3 2 5 7 4 2 4 3 6 4 1 5 2 10 4 8 6 6 4 9 10 5 4 9 5 8 7 Output 12 29 -----Note----- In the first test case, you can shift the third column down by one, this way there will be $r_1 = 5$ and $r_2 = 7$. In the second case you can don't rotate anything at all, this way there will be $r_1 = r_2 = 10$ and $r_3 = 9$.
def main(): t = int(input()) for _ in range(t): n, m = map(int, input().split()) l = [] board = [] for i in range(n): li = list(map(int, input().split())) board.append(li) for j in range(m): l.append((li[j], j)) l.sort(key=lambda x: x[0], reverse=True) idxs = set() z = 0 while len(idxs) < min(n, m): curr = l[z] idxs.add(curr[1]) z += 1 idxs = list(idxs) total = 0 for i in range(n**n): rotations = [] num = i for j in range(n - 1, -1, -1): nj = n**j q = num // nj num -= q * nj rotations.append(q) subtotal = 0 for k in range(n): subtotal += max( board[(k + rotations[col]) % n][idxs[col]] for col in range(min(n, m)) ) total = max(total, subtotal) print(total) main()
FUNC_DEF 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 LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
This is an easier version of the next problem. The difference is only in constraints. You are given a rectangular $n \times m$ matrix $a$. In one move you can choose any column and cyclically shift elements in this column. You can perform this operation as many times as you want (possibly zero). You can perform this operation to a column multiple times. After you are done with cyclical shifts, you compute for every row the maximal value in it. Suppose that for $i$-th row it is equal $r_i$. What is the maximal possible value of $r_1+r_2+\ldots+r_n$? -----Input----- The first line contains an integer $t$ ($1 \le t \le 40$), the number of test cases in the input. The first line of each test case contains integers $n$ and $m$ ($1 \le n \le 4$, $1 \le m \le 100$) β€” the number of rows and the number of columns in the given matrix $a$. Each of the following $n$ lines contains $m$ integers, the elements of $a$ ($1 \le a_{i, j} \le 10^5$). -----Output----- Print $t$ integers: answers for all test cases in the order they are given in the input. -----Example----- Input 2 2 3 2 5 7 4 2 4 3 6 4 1 5 2 10 4 8 6 6 4 9 10 5 4 9 5 8 7 Output 12 29 -----Note----- In the first test case, you can shift the third column down by one, this way there will be $r_1 = 5$ and $r_2 = 7$. In the second case you can don't rotate anything at all, this way there will be $r_1 = r_2 = 10$ and $r_3 = 9$.
for _ in range(int(input())): n, m = map(int, input().split()) a = [[int(x) for x in input().split()] for j in range(n)] x = [[a[i][j] for i in range(n)] for j in range(m)] x.sort(key=lambda xx: -max(xx)) dp = [[(0) for i in range(1 << n)] for j in range(m + 1)] an = 0 for i in range(m): for prev in range(1 << n): for pres in range(1 << n): for j in range(n): ma = 0 if prev ^ pres != prev + pres: continue for st in range(n): if pres & 1 << st: ma += x[i][(st + j) % n] dp[i + 1][pres ^ prev] = max( dp[i + 1][pres ^ prev], dp[i][prev] + ma ) print(dp[m][(1 << n) - 1])
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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
for _ in range(int(input())): x, y = map(int, input().split()) c, d = x, y a = 0 b = 0 while x != y: if x | y == y: a += 1 break a += 1 x += 1 while c != d: if c | d == d: b += 1 break b += 1 d += 1 print(min(a, b))
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 VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
def solve(): a, b = map(int, input().split()) if abs(a - b) == 1 or a | b == b: print(1) return store_a = a ans = min(abs(a - b), abs((a | b) - b) + 1) count = 0 while a <= b: count += 1 temp = abs((a | b) - b) + count if temp < ans: ans = temp a += 1 a = store_a count = 0 temp_or = a | b while b <= temp_or: count += 1 temp = abs((a | b) - b) + count if temp < ans: ans = temp b += 1 print(ans) return for _ in range(int(input())): solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
def s(a, b): c, d = a, b ans1 = ans2 = 1 ans3 = b - a while a | b != b: a += 1 ans1 += 1 while c | d != d: d += 1 ans2 += 1 print(min(ans1, ans2, ans3)) for _ in range(int(input())): a, b = map(int, input().split()) s(a, b)
FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR 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 EXPR FUNC_CALL VAR VAR VAR
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
import sys def f(a, b): if a == b: return 0 if a | b == b: return 1 for i in range(29, -1, -1): if b >> i & 1 == 0 and a >> i & 1 == 1: for j in range(i + 1, 30): if b >> j & 1 == 1 and a >> j & 1 == 0: x = (a | (1 << j) - 1) + 1 if x == b: return x - a else: return x - a + 1 raise Exception("wut1" + str(a) + " " + str(b)) raise Exception("wut" + str(a) + " " + str(b)) def solve(): inp = sys.stdin.readline a, b = map(int, inp().split()) r = f(a, b) x = b while True: r = min(r, f(a, x) + x - b) r = min(r, (a | x) - b + 1) if x & x - 1 == 0: break x += x ^ x & x - 1 print(r) def main(): for i in range(int(sys.stdin.readline())): solve() main()
IMPORT FUNC_DEF IF VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER IF VAR VAR RETURN BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
def main2(): def gen(): r = 1 while True: r += 1 l = 1 while l < r: yield l, r l += 1 for i, x in enumerate(gen()): if i > 100: break print(i + 1, x) quit() for _ in range(int(input())): a, b = map(int, input().split()) result = b - a for i in range(b - a): na = a + i na = na | b cur = i + 1 + (na - b) result = min(result, cur) bit = 1 << 32 while bit: if a & bit and not b & bit: break bit = bit >> 1 mask = (bit << 1) - 1 a = a & mask b = b & mask result = min(result, abs(a - b) + 1) print(result)
FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR EXPR VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL 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 BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
for _ in range(int(input())): a, b = map(int, input().split()) if a | b == b: print(1) else: n1 = b - a c = 1 b1 = b while a | b != b: b = b + 1 c = c + 1 c1 = 1 while a | b1 != b1: a = a + 1 c1 = c1 + 1 print(min(n1, c, c1))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
for _ in range(int(input())): a, b = map(int, input().split(" ")) ans = b - a for i in range(b): if b == b | i: ans = min(ans, 1 + abs(i - a)) 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 STRING ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
t = int(input()) for i in range(t): q = input().split(" ") a = int(q[0]) b = int(q[1]) k1 = 0 while a <= b: if b == a: break if b == a | b: k1 += 1 break else: k1 += 1 a += 1 a = int(q[0]) b = int(q[1]) k2 = 0 while True: if b == a | b: k2 += 1 break else: k2 += 1 b += 1 print(min(k1, k2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR IF VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
import sys input = sys.stdin.readline t = int(input().strip()) for _ in range(t): a, b = map(int, input().split()) ret = b - a tmp = a while tmp | b != b: tmp += 1 ret = min(ret, 1 + tmp - a) tmp = b while a | tmp != tmp: tmp += 1 ret = min(ret, 1 + tmp - b) print(ret)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
try: for _ in range(int(input())): a, b = map(int, input().split()) minimum = [] if a == b: print(0) elif a | b == a or a | b == b: print(1) else: diff = abs(a - b) minimum.append(diff) steps = 0 mini = min(a, b) maxi = max(a, b) for i in range(0, maxi): steps += 1 mini += 1 if mini | maxi == mini or mini | maxi == maxi: steps += 1 break minimum.append(steps) steps = 0 mini = min(a, b) maxi = max(a, b) for i in range(0, maxi): steps += 1 maxi += 1 if mini | maxi == mini or mini | maxi == maxi: steps += 1 break minimum.append(steps) print(min(minimum)) except: pass
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 LIST IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
a = "001101110101100110" b = "101000001001000110" c = "101101111101100110" ans = "101101100100001" def main(): a, b = map(int, input().split()) k1 = 0 a1, b1 = a, b while a1 | b1 != b1: a1 += 1 k1 += 1 if a1 != b1: k1 += 1 k2 = 0 a2, b2 = a, b while a2 | b2 != b2: b2 += 1 k2 += 1 if a2 != b2: k2 += 1 print(min(k1, k2)) t = int(input()) for _ in range(t): main()
ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
for i in range(int(input())): a, b = map(int, input().split()) q, w = a, b k = 1 r = 1 o = min(b - a, (b | a) - b + 1) while a | b > b: b += 1 k += 1 while q | w > w: q += 1 r += 1 print(min(o, k, r))
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 VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
t = int(input()) for T in range(t): a, b = [int(x) for x in input().split()] x = a y = b n = b ans = b - a while n > 0: n -= 1 if x | y == y: ans = min(ans, b - n) break x += 1 n = b x = a y = b while n > 0: n -= 1 if x | y == y: ans = min(ans, b - n) break y += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
for _ in range(int(input())): a, b = map(int, input().split()) c = int(a) d = int(b) c1 = 0 c2 = 0 while a != b: if a | b == b: c1 += 1 a = a | b else: a += 1 c1 += 1 while c != d: if c | d == d: c2 += 1 c = c | d else: d += 1 c2 += 1 print(min(c1, c2))
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 FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
t = int(input()) def steps(a, b): if a == b: print(0) return if b == b | a: print(1) return ans1 = b - a ans2 = 0 for i in range(a, b): ans2 = ans2 + 1 if i | b == b: break ans3 = (a | b) - b + 1 ans4 = 0 while True: b = b + 1 ans4 += 1 if a | b == b: break ans4 += 1 print(min(ans1, ans2, ans3, ans4)) for _ in range(t): inp = list(map(int, input().split())) a, b = inp steps(a, b)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
t = int(input()) for _ in range(t): a, b = map(int, input().split()) for i in range(a + b): if a + i | b == b or a | b + i == b + i: break print(min(i + 1, b - a))
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 FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
def check(a, b): mn = abs(a - b) tmp = a while tmp | b != b: tmp += 1 mn = min(mn, 1 + tmp - a) tmp = b while a | tmp != tmp: tmp += 1 mn = min(mn, 1 + tmp - b) return mn t = int(input()) for _ in range(t): a, b = map(int, input().split()) print(check(a, b))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR VAR RETURN 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
t = int(input()) for _ in range(t): a, b = map(int, input().split()) cnt = 1 ta, tb = a, b while cnt < b - a: if ta | tb == tb: break tb += 1 cnt += 1 cnt2 = 1 ta, tb = a, b while cnt2 < b - a: if ta | tb == tb: break ta += 1 cnt2 += 1 print(min({b - a, cnt, cnt2}))
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 NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
import sys input = sys.stdin.readline for _ in range(int(input())): a, b = map(int, input().split()) best = b - a for i in range(b - a): a1 = a + i | b best = min(best, i + 1 + a1 - b) b1 = b + i a1 = a | b1 best = min(best, i + 1 + a1 - b1) if i > best - 1: break print(best)
IMPORT ASSIGN 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 BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
t = int(input()) while t != 0: t = t - 1 c = 0 a, b = map(int, input().split()) test = b d = 0 ans1 = (a | b) - b + 1 if a | b == b: print("1") continue ans2 = b - a for i in range(a, b): c = c + 1 if i | b == b: break ans3 = c while True: test = test + 1 d = d + 1 if a | test == test: break ans4 = d + 1 total = min(ans1, ans2, ans3, ans4) print(total)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
def answer(a, b): c = min(b - a, (b | a) - b + 1) k = 1 m = a n = b k1 = 1 while a | b > b: b += 1 k += 1 while m | n > n: m += 1 k1 += 1 return min(c, k1, k) t = int(input()) for k in range(t): inp = input().split() a = int(inp[0]) b = int(inp[1]) print(answer(a, b))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR 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 NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
n = int(input()) for _ in range(n): a, b = map(int, input().split()) ans = b - a ans1 = 1 ans2 = 1 c, d = a, b while a | b != b: a += 1 ans1 += 1 while c | d != d: d += 1 ans2 += 1 print(min(ans2, ans1, 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 BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
for _ in range(int(input())): a, b = map(int, input().split()) ans = b - a for i in range(0, a + 1): if a + i | b == b: ans = min(ans, i + 1) break for i in range(b + 1): if b + i | a == b + i: ans = min(ans, i + 1) break 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 BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $a$ and $b$ ($a < b$). After that, the student can apply any of the following operations any number of times: $a := a + 1$ (increase $a$ by $1$), $b := b + 1$ (increase $b$ by $1$), $a := a \ | \ b$ (replace $a$ with the bitwise OR of $a$ and $b$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $a$ and $b$ equal. Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $a$ equal to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The only line for each test case contains two integers $a$ and $b$ ($1 \le a < b \le 10^6$). It is guaranteed that the sum of $b$ over all test cases does not exceed $10^6$. -----Output----- For each test case print one integer β€” the minimum required number of operations to make $a$ and $b$ equal. -----Examples----- Input 5 1 3 5 8 2 5 3 19 56678 164422 Output 1 3 2 1 23329 -----Note----- In the first test case, it is optimal to apply the third operation. In the second test case, it is optimal to apply the first operation three times. In the third test case, it is optimal to apply the second operation and then the third operation.
t = int(input()) def solve(a, b): res = min(b - a, (b | a) - b + 1) u, v = a, b tmp1, tmp2 = 0, 0 while b | a > b: b += 1 tmp1 += 1 while u | v > v: u += 1 tmp2 += 1 return min(res, tmp1 + 1, tmp2 + 1) for _ in range(t): a, b = map(int, input().split()) print(solve(a, b))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problem statements in [Russian] and [Mandarin Chinese]. Find the minimum integer K such that sum of bits present on odd positions in binary representation of all integers from 1 to K is greater than or equal to N. The bits are enumerated from left to right starting from the leftmost set bit i.e. the most significant bit. For example, binary representation of 77 is 1001101. Bits present on 1^{st}, 3^{rd}, 5^{th} and 7^{th} positions in the binary representation are 1, 0, 1, 1 respectively. So sum of bits present on odd positions in binary representation of 77 is 1 + 0 + 1 + 1 = 3. ------ Input Format ------ - First line will contain T, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, an integer N. ------ Output Format ------ For each testcase, output in a single line the minimum possible value of K. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N ≀ 10^{9}$ ----- Sample Input 1 ------ 3 2 6 100 ----- Sample Output 1 ------ 2 5 57 ----- explanation 1 ------ - In the first test case, binary representations of $1$ and $2$ are 1, 10. Sum of bits present on odd positions in the binary representation of $1$ and $2$ = $1 + 1$ = $2$. - In the second test case, the binary representations of integers from $1$ to $5$ are 1, 10, 11, 100, 101 respectively. So the sum of bits present on odd positions in binary representation of integers from $1$ to $5$ are $1, 1, 1, 1, 2$ respectively which makes a total of $6$.
def check(x): ans = 0 for i in range(31): a = 1 << i if x < a: break if x == a: ans += 1 break if x < a * 2 - 1: ans += x - a + 1 f = 0 s = 0 if i & 1: s = 1 e = i - 1 while 1 << e & x == 0: e -= 1 for j in range(s, e + 1, 2): y = (x - a + 1) // (1 << j) ans += y // 2 * (1 << j) + (x - a + 1) % (1 << j) * (y % 2) else: ans += a for j in range(i - 2, -1, -2): ans += a // 2 return ans for _ in range(int(input())): n = int(input()) lo = 0 hi = n while lo < hi: mid = (lo + hi) // 2 if check(mid) < n: lo = mid + 1 else: hi = mid print(lo)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR IF VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef is very happy today because he got an offer from Gordon Ramsay himself to work for him in London. Chef wants to prepare a delicious cake for Gordon to impress him, but he does not know that Gordon Ramsay is not easy to impress. Gordon cut Chef's cake into $N$ small slices (numbered $1$ through $N$), placed in a row in such a way that for each valid $i$, slices $i$ and $i+1$ are adjacent. Note that slices $N$ and $1$ are not adjacent. For each valid $i$, the $i$-th slice has *taste* $A_{i}$. Gordon wants to put special toppings on some slices and then ask Chef to group all slices into $K$ clusters in such a way that the slices in each cluster form a contiguous sequence and the *sweetness* of the cake is maximum possible. For each valid $i$, let's denote the topping on the $i$-th slice by $t_{i}$ ($t_{i} = 0$ if there is no topping on this slice); the *sweetness* of this slice is $t_{i} \cdot A_{i}$, so the sweetness of a slice without any special topping is $0$. The sweetness of a cluster is the total (summed up) sweetness of all slices in this cluster. Let's denote the sweetnesses of all clusters by $S_{1}, S_{2}, \ldots, S_{K}$. The sweetness of the whole cake is computed as $S_{1} * S_{2} * \ldots * S_{K}$, where the operation $*$ is defined in the following way: for any non-negative integers $x$ and $y$ such that $x β‰₯ y$, $$x * y = y * x = \sum_{n=0}^{\lfloor\log_{2}(x)\rfloor} 2^{n}(\lfloor\frac{x}{2^{n}}\rfloor \bmod 2)(\lfloor\frac{y}{2^{n}}\rfloor \bmod 2) \,.$$ It can be proven that this operation is associative, i.e. $(x * y) * z = x * (y * z)$. You should answer $Q$ queries. In each query, you are given the toppings on all slices and the number of clusters $K$. Tell Chef the maximum possible sweetness of the cake! ------ Input ------ The first line of the input contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The next line contains a single integer $Q$. For each query, two lines follow. The first of these lines contains a single integer $K$. The second line contains $N$ space-separated integers $t_{1}, t_{2}, \ldots, t_{N}$. ------ Output ------ Print a single line containing one integer β€” the maximum sweetness. ------ Constraints ------ $1 ≀ Q ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ K ≀ 10^{5}$ $1 ≀ A_{i} ≀ 10^{15}$ for each valid $i$ $0 ≀ t_{i} ≀ 10$ for each valid $i$ in each query, the number of slices with toppings does not exceed $50$ ------ Subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 10 15 5 26 4 3 13 5 5 27 11 2 3 2 3 1 4 7 1 5 1 1 1 1 0 0 0 0 0 0 0 1 0 0 ----- Sample Output 1 ------ 33 5 ----- explanation 1 ------ For the first query, Chef can choose clusters in the following way: cluster $1$ contains slices $1$ and $2$, cluster $2$ contains slices $3$ through $7$ and cluster $3$ contains slices $8$ through $10$. The sweetness of the cake is $(15 \cdot 2 + 5 \cdot 3) * (26 \cdot 1 + 4 \cdot 4 + 3 \cdot 7 + 13 \cdot 1 + 5 \cdot 5) * (5 \cdot 1 + 27 \cdot 1 + 11 \cdot 1) = 33$.
def group(S, k): if k == 1: return sum(S) firstPiece = 0 answer = 0 dynamic = {} for i in range(len(S) - k + 1): firstPiece += S[i] answer = max(groupWithPattern(S[i + 1 :], k - 1, firstPiece, dynamic), answer) return answer def groupWithPattern(S, k, p, dynamic): if k == 1: return sum(S) & p try: return dynamic[len(S), k, p] except KeyError: pass firstPiece = 0 answer = 0 for i in range(len(S) - k + 1): firstPiece += S[i] answer = max( groupWithPattern(S[i + 1 :], k - 1, p & firstPiece, dynamic), answer ) dynamic[len(S), k, p] = answer return answer n = int(input()) A = [int(a) for a in input().split()] q = int(input()) for _ in range(q): k = int(input()) T = [int(t) for t in input().split()] S = [(a * t) for a, t in zip(A, T) if a * t > 0] print(group(S, k) if k <= len(S) else 0)
FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL 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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef is very happy today because he got an offer from Gordon Ramsay himself to work for him in London. Chef wants to prepare a delicious cake for Gordon to impress him, but he does not know that Gordon Ramsay is not easy to impress. Gordon cut Chef's cake into $N$ small slices (numbered $1$ through $N$), placed in a row in such a way that for each valid $i$, slices $i$ and $i+1$ are adjacent. Note that slices $N$ and $1$ are not adjacent. For each valid $i$, the $i$-th slice has *taste* $A_{i}$. Gordon wants to put special toppings on some slices and then ask Chef to group all slices into $K$ clusters in such a way that the slices in each cluster form a contiguous sequence and the *sweetness* of the cake is maximum possible. For each valid $i$, let's denote the topping on the $i$-th slice by $t_{i}$ ($t_{i} = 0$ if there is no topping on this slice); the *sweetness* of this slice is $t_{i} \cdot A_{i}$, so the sweetness of a slice without any special topping is $0$. The sweetness of a cluster is the total (summed up) sweetness of all slices in this cluster. Let's denote the sweetnesses of all clusters by $S_{1}, S_{2}, \ldots, S_{K}$. The sweetness of the whole cake is computed as $S_{1} * S_{2} * \ldots * S_{K}$, where the operation $*$ is defined in the following way: for any non-negative integers $x$ and $y$ such that $x β‰₯ y$, $$x * y = y * x = \sum_{n=0}^{\lfloor\log_{2}(x)\rfloor} 2^{n}(\lfloor\frac{x}{2^{n}}\rfloor \bmod 2)(\lfloor\frac{y}{2^{n}}\rfloor \bmod 2) \,.$$ It can be proven that this operation is associative, i.e. $(x * y) * z = x * (y * z)$. You should answer $Q$ queries. In each query, you are given the toppings on all slices and the number of clusters $K$. Tell Chef the maximum possible sweetness of the cake! ------ Input ------ The first line of the input contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The next line contains a single integer $Q$. For each query, two lines follow. The first of these lines contains a single integer $K$. The second line contains $N$ space-separated integers $t_{1}, t_{2}, \ldots, t_{N}$. ------ Output ------ Print a single line containing one integer β€” the maximum sweetness. ------ Constraints ------ $1 ≀ Q ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ K ≀ 10^{5}$ $1 ≀ A_{i} ≀ 10^{15}$ for each valid $i$ $0 ≀ t_{i} ≀ 10$ for each valid $i$ in each query, the number of slices with toppings does not exceed $50$ ------ Subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 10 15 5 26 4 3 13 5 5 27 11 2 3 2 3 1 4 7 1 5 1 1 1 1 0 0 0 0 0 0 0 1 0 0 ----- Sample Output 1 ------ 33 5 ----- explanation 1 ------ For the first query, Chef can choose clusters in the following way: cluster $1$ contains slices $1$ and $2$, cluster $2$ contains slices $3$ through $7$ and cluster $3$ contains slices $8$ through $10$. The sweetness of the cake is $(15 \cdot 2 + 5 \cdot 3) * (26 \cdot 1 + 4 \cdot 4 + 3 \cdot 7 + 13 \cdot 1 + 5 \cdot 5) * (5 \cdot 1 + 27 \cdot 1 + 11 \cdot 1) = 33$.
n = int(input()) a = list(map(int, input().split())) q = int(input()) for _ in range(q): k = int(input()) slices = list( filter( lambda x: x, map(lambda x: x[0] * x[1], zip(a, map(int, input().split()))) ) ) nn = len(slices) if k > nn: print(0) continue b = 64 bm = 0 p_sum = [slices[0]] for i in range(1, nn): p_sum.append(slices[i] + p_sum[-1]) dp = [([False] * k) for __ in range(nn)] while b > -1: bm = bm | 1 << b for cn in range(nn): dp[cn][0] = p_sum[cn] & bm == bm for ck in range(1, k): dp[cn][ck] = False for cn in range(nn): for ck in range(1, k): for pn in range(cn): dp[cn][ck] = ( dp[cn][ck] or dp[pn][ck - 1] and p_sum[cn] - p_sum[pn] & bm == bm ) if not dp[-1][-1]: bm -= 1 << b b -= 1 print(bm)
ASSIGN VAR FUNC_CALL VAR 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
for _ in range(int(input())): n, k = map(int, input().split()) s = list(input()) key = 0 ct0 = 0 flag = 0 ct1 = s.count("1") solo = 0 if n < k: print("NO") elif k == 1: print("YES") elif k == 2: if ct1 % 2 == 1: print("NO") else: print("YES") elif ct1 % 2 == 1: if k % 2 == 1 and k <= ct1: print("YES") else: print("NO") else: for i in s: if key == 0 and i == "0": ct0 += 1 elif key == 1 and i == "1": key = 0 ct0 += 1 elif key == 0 and i == "1": key = 1 if k <= ct0: print("YES") elif k <= ct1 and k % 2 == 0: print("YES") else: print("NO")
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 FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR VAR IF VAR NUMBER VAR STRING VAR NUMBER IF VAR NUMBER VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR STRING ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
__author__ = "nograhol" def even_substr_max(S): st = 0 ans = 0 for i in range(len(S)): if st == 0 and S[i] == "0": ans += 1 elif st == 0 and S[i] == "1": st = 1 elif st == 1 and S[i] == "1": st = 0 ans += 1 return ans T = int(input()) for ta in range(T): N, K = map(int, input().split(" ")) S = input() ones = S.count("1") even = even_substr_max(S) if K <= even and ones % 2 == 0: print("YES") elif K <= ones and K % 2 == ones % 2: print("YES") else: print("NO")
ASSIGN VAR STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER RETURN 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 STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
for _ in range(int(input())): n, k = map(int, input().split()) k1 = k s = input() o = s.count("1") o1 = o p1 = 0 while k1 > 0 and p1 < n: if s[p1] == "1": k1 -= 1 o1 -= 1 p1 += 1 if k1 == 0 and o1 % 2 == 0: print("YES") else: p1 = 0 co = 0 while p1 < n and k > 0: if s[p1] == "0": if co == 0: k -= 1 else: co += 1 if co == 2: k -= 1 o -= 2 co = 0 p1 += 1 if k == 0 and o % 2 == 0: print("YES") else: print("NO")
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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
for _ in range(int(input())): n, k = map(int, input().split()) s = input() ones = s.count("1") zeroes = s.count("0") t0 = 0 i = 0 summ = 0 flag0 = 1 while i < len(s): if (summ + int(s[i])) % 2 == 0: t0 += 1 summ = 0 else: summ += int(s[i]) i += 1 if summ % 2 != 0: flag0 = 0 i = 0 t1 = 0 summ = 0 flag1 = 1 while i < len(s): if (summ + int(s[i])) % 2 == 1: t1 += 1 summ = 0 else: summ += int(s[i]) i += 1 if flag0 and t0 == k or flag1 and t1 == k: print("YES") elif t0 < k and t1 < k: print("NO") elif flag0 and t0 > k or flag1 and t1 > k and (t1 - k) % 2 == 0: print("YES") else: print("NO")
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
from sys import stdin input = stdin.readline def solve(N, K, S): if K == 1: return "YES" ones = sum(1 for s in S if s == "1") if ones % 2 == 0: k = 0 count = 0 for i in range(N): if S[i] == "0" and count == 0: k += 1 elif S[i] == "0" and count == 1: pass elif S[i] == "1" and count == 0: count = 1 elif S[i] == "1" and count == 1: k += 1 count = 0 if k >= K: return "YES" if ones >= K and (ones - K) % 2 == 0: return "YES" return "NO" T = int(input().strip()) for problem in range(1, T + 1): N, K = [int(x) for x in input().strip().split()] S = input().strip() print(solve(N, K, S))
ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER RETURN STRING ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR RETURN STRING IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
t = int(input()) for _ in range(t): n, x = map(int, input().split()) a = input() fl = 0 now = 0 tobe = 0 k = x ind = -1 for i in range(n): now ^= int(a[i]) if tobe == now: k -= 1 if k == 0: ind = i break try: now = 0 except: pass if ind != -1: if ind == n: if k == 0: fl = 1 else: now = ind + 1 one = 0 for i in range(ind + 1, n): if a[i] == "1": one += 1 if one % 2 == 0: fl = 1 now = 0 tobe = 1 k = x ind = -1 for i in range(n): now ^= int(a[i]) if tobe == now: k -= 1 if k == 0: ind = i break try: now = 0 except: pass if ind != -1: if ind == n: if k == 0: fl = 1 else: now = ind + 1 one = 0 for i in range(ind + 1, n): if a[i] == "1": one += 1 if one % 2 == 0: fl = 1 if fl == 1: print("YES") else: print("NO")
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 FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
t = int(input()) for i in range(t): p = list(map(int, input().split())) n = p[0] k = p[1] s = input() count0 = 0 count1 = 0 f = 0 res = 0 for x in s: res ^= ord(x) - 48 if res == 0: count0 += 1 if count0 >= k and res != 1: f = 1 print("YES") res = 0 for x in s: res ^= ord(x) - 48 if res == 1: count1 += 1 res = 0 if count1 >= k and (count1 - k) % 2 == 0: if f != 1: f = 1 print("YES") if f == 0: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
for i in range(int(input())): n, k = map(int, input().split()) a = input() l = [] x = 0 for i in range(n): x ^= int(a[i]) l.append(x) c = 0 ci = 0 ll = 0 for i in range(n): if l[i] == 0: c += 1 if l[i] != ll: ll = l[i] ci += 1 if l[n - 1] == 0 and k <= c: print("YES") elif ci % 2 == k % 2 and k <= ci: print("YES") else: print("NO")
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 FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
for _ in range(int(input())): n, k = map(int, input().split()) s = input() one = s.count("1") if one >= k: count = 0 for i in range(n): if s[i] == "1": count += 1 if count == k: break if count == k and one - k & 1 == 0: print("YES") continue count = 0 i = 0 while i < n: if s[i] == "0": count += 1 elif s[i] == "1": i += 1 while i < n and s[i] != "1": i += 1 if i < n and s[i] == "1": count += 1 i += 1 if count == k: break if count == k and one & 1 == 0: print("YES") continue print("NO")
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
t = int(input()) for _ in range(t): n, k = [int(s) for s in input().split(" ")] s = input() a = [int(b) for b in s] val = 0 cnt = 0 for i in range(n): val ^= a[i] if val == 0: cnt += 1 val = 0 if val != 1 and cnt >= k: print("YES") continue cnt = s.count("1") if cnt % 2 != k % 2: print("NO") continue if cnt >= k: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
t = input() for i in range(int(t)): n, k = map(int, input().split()) s = input() x = 0 j = 0 zeros = 0 ones = 0 while j < n and zeros < k - 1: x ^= int(s[j]) j += 1 if x == 0: zeros += 1 x = 0 x = 0 z = 0 while z < n and ones < k - 1: x ^= int(s[z]) z += 1 if x == 1: ones += 1 x = 0 flag = False xor = 0 if zeros == k - 1 and j < n: for temp in s[j:]: xor ^= int(temp) if xor == 0: flag = True xor = 0 if ones == k - 1 and z < n: for temp in s[z:]: xor ^= int(temp) if xor == 1: flag = True if flag: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR 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 WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = input() o = 0 z = 0 for i in range(n): if s[i] == "1": o += 1 else: z += 1 if o % 2 == 0 and o // 2 + z >= k: c = 0 cur = 0 flag = 0 for i in range(n): if s[i] == "0" and cur == 0: c += 1 elif s[i] == "1" and cur == 0: cur = 1 elif s[i] == "1" and cur == 1: cur = 0 c += 1 if c >= k and cur == 0: print("YES") continue if o % 2 == 0: x = o - k if x >= 0 and x % 2 == 0: print("YES") continue else: print("NO") continue if o % 2 != 0: x = o - k if x >= 0 and x % 2 == 0: print("YES") continue else: print("NO") continue
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 FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
def ispos(s, k): ss = "" xor = 0 for i in s: xor ^= int(i) ss += str(xor) s = ss if s[-1] == "0" and s.count("0") >= k or k == 1: return "YES" else: if k % 2 == 0 and 1 == int(s[-1]): return "NO" elif k % 2 == 1 and 0 == int(s[-1]): return "NO" st = 1 i = k - 1 for j in s[:-1]: if int(j) == st: i -= 1 st ^= 1 if i == 0: return "YES" if i == 0: return "YES" return "NO" for _ in range(int(input())): n, x = map(int, input().split()) print(ispos(input(), x))
FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NUMBER STRING FUNC_CALL VAR STRING VAR VAR NUMBER RETURN STRING IF BIN_OP VAR NUMBER NUMBER NUMBER FUNC_CALL VAR VAR NUMBER RETURN STRING IF BIN_OP VAR NUMBER NUMBER NUMBER FUNC_CALL VAR VAR NUMBER RETURN STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
T = int(input()) for _ in range(T): N, K = [int(x) for x in input().split()] S = input() s = sum(c == "1" for c in S) if not K & 1 and s & 1: print("NO") elif K & 1: s = s & 1 cnt = 0 curr = 0 works = False for c in S: curr += c == "1" if curr & 1 == s: curr = 0 cnt += 1 if cnt == K: works = True break if works: print("YES") else: print("NO") else: for s in [0, 1]: cnt = 0 curr = 0 works = False for c in S: curr += c == "1" if curr & 1 == s: curr = 0 cnt += 1 if cnt == K: works = True break if works: print("YES") break if not works: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR STRING VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR STRING IF BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR STRING IF BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING IF VAR EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
t = int(input()) for _ in range(0, t): inputs = [int(num) for num in input().split()] n = inputs[0] k = inputs[1] s = input() ones = 0 zeros = 0 for i in range(0, len(s)): if s[i] == "0": zeros += 1 else: ones += 1 if k % 2 == 1: min1 = 1 max1 = 0 c = 0 onec = 0 if ones % 2 == 0: i = 0 while i < len(s): if s[i] == "0": if onec == 0: c += 1 else: onec += 1 if onec == 2: c += 1 onec = 0 i += 1 max1 = c if k >= min1 and k <= max1: print("YES") else: print("NO") else: i = 0 max1 = ones min1 = 1 if k <= max1: print("YES") else: print("NO") elif ones % 2 == 1: print("NO") else: maxo = ones min1 = 1 maxe = 0 onec = 0 c = 0 i = 0 while i < len(s): if s[i] == "0": if onec == 0: c += 1 else: onec += 1 if onec == 2: c += 1 onec = 0 i += 1 maxe = c if k <= maxe or k <= maxo: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
ans = {(True): "YES", (False): "NO"} def can_split(S, K): N = len(S) ones = S.count("1") if ones == 0: return ans[K <= N] elif 1 == ones % 2: return ans[1 == K % 2 and ones >= K] elif 0 == K % 2 and ones >= K: return "YES" else: inpair = False for c in S: if c == "1": if inpair: K -= 1 inpair = not inpair elif not inpair: K -= 1 if K == 0: return "YES" return "NO" T = int(input()) for tx in range(T): N, K = map(int, input().split()) S = input().strip() print(can_split(S, K))
ASSIGN VAR DICT NUMBER NUMBER STRING STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER RETURN VAR VAR VAR IF NUMBER BIN_OP VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF NUMBER BIN_OP VAR NUMBER VAR VAR RETURN STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER IF VAR NUMBER RETURN STRING RETURN STRING 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 FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
import sys f = sys.stdin next(f) for k, s in zip(f, f): k = int(k.split()[1]) i = j = 0 for x in s[:-1]: if x == "1": i += 1 if i & 1 ^ 1: j += 1 print("YNEOS"[max((i ^ k) & 1 ^ 1 and i, i & 1 ^ 1 and j) < k :: 2])
IMPORT ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR STRING VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
def main(): n, k = map(int, input().split()) b = list(map(int, list(input()))) if k == 1: return "YES" one = 0 for z in b: if z: one += 1 if one % 2 and k % 2 == 0: return "NO" if one % 2 and k % 2: if one >= k: return "YES" return "NO" if one % 2 == 0 and k % 2 == 0: if one >= k: return "YES" mx = 0 flag = False for z in b: if not z: if not flag: mx += 1 elif not flag: mx += 1 flag = True else: flag = False if mx >= k: return "YES" return "NO" if one % 2 == 0 and k % 2: mx = 0 flag = False for z in b: if not z: if not flag: mx += 1 elif not flag: mx += 1 flag = True else: flag = False if mx >= k: return "YES" return "NO" for _ in range(int(input())): print(main())
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 VAR FUNC_CALL VAR IF VAR NUMBER RETURN STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN STRING IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR RETURN STRING RETURN STRING IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR RETURN STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR RETURN STRING RETURN STRING IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
for _ in range(int(input())): n, k = map(int, input().split()) s = input() ones = 0 zeros = 0 xor = 0 for i in s: if i == "1": xor ^= 1 if xor == 0: zeros += 1 if xor != ones % 2: ones += 1 if xor == 0 and zeros >= k or ones >= k and ones % 2 == k % 2: print("YES") else: print("NO")
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 FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
T = int(input()) for t in range(1, T + 1): inp = input().split() N = int(inp[0]) K = int(inp[1]) S = input() Slist = [] Slist[:0] = S Slist = list(map(int, Slist)) num_0 = 0 num_1 = 0 xor_0 = xor_1 = 0 for i, s in enumerate(Slist): xor_0 = xor_0 ^ s xor_1 = xor_1 ^ s if xor_0 == 0: num_0 += 1 if xor_1 == 1: num_1 += 1 xor_1 = 0 if K > num_0 and K > num_1: print("NO") elif K <= num_0 and xor_0 == 0: print("YES") elif K <= num_1 and (num_1 - K) % 2 == 0: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
from itertools import * for _ in range(int(input())): n, k = map(int, input().split()) s = input() z = s.count("0") o = n - z s1 = "NO" if k == 1 or k == n and o in [0, n]: s1 = "YES" elif k <= o and (o - k) % 2 == 0: s1 = "YES" else: x = 0 co = 0 for i in s: x = x ^ int(i) if x == 0: co += 1 if x == 0 and co >= k: s1 = "YES" print(s1)
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR STRING IF VAR NUMBER VAR VAR VAR LIST NUMBER VAR ASSIGN VAR STRING IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
import sys def f(l, n, f): a = 0 x = n - 1 while a <= x: m = a + (x - a) // 2 if l[m] == f: return m elif l[m] > f: x = m - 1 else: a = m + 1 return None for _ in range(int(sys.stdin.readline())): n, x = map(int, sys.stdin.readline().split()) s = input() c = s.count("1") j = 0 k = 0 t = 0 for i in range(n): t = t ^ int(s[i]) if t == 0: j = j + 1 t = 0 for i in range(n): t = t ^ int(s[i]) if t == 1: k = k + 1 t = 0 if x % 2 == 0: if c % 2 == 1: print("NO") elif j >= x: print("YES") elif k >= x and (k - x) % 2 == 0: print("YES") else: print("NO") elif c % 2 == 1: if c >= x: print("YES") else: print("NO") elif j >= x: print("YES") elif k >= x and (k - x) % 2 == 0: print("YES") else: print("NO")
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NONE 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
def count0(s, n): c = 0 x = 0 for i in s: if i == "0": if x == 0: c += 1 elif x == 1: c += 1 x = 0 else: x = 1 if x == 1: return 0 return c def count1(s): c = 0 for i in s: if i == "1": c += 1 if c == 0: return 0 return c t = int(input()) for _ in range(t): n, k = [int(i) for i in input().split()] s = input() ones = count1(s) zeros = count0(s, n) if zeros >= k: print("YES") continue d = ones - k if d >= 0 and d % 2 == 0: print("YES") continue print("NO")
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
for _ in range(int(input())): n, k = map(int, input().split()) l = list(map(int, input())) part = 0 flag = 0 i = 0 while i < n: if l[i] == 1: part += 1 if part == k - 1: i += 1 break i += 1 c = 0 while i < n: if l[i] == 1: c += 1 i += 1 if c % 2: flag = 1 i = 0 part = 0 f = 0 while i < n: if l[i] == 1: f += 1 if f % 2 == 0: part += 1 if part == k - 1: i += 1 break i += 1 c = 0 fl = 0 while i < n: fl = 1 if l[i] == 1: c += 1 i += 1 if c % 2 == 0 and fl: flag = 1 if flag or k == 1: print("YES") else: print("NO")
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
def solve(): n = l[0] k = l[1] xor = 0 for i in range(n): if k == 1: if i == n - 1: if s[i] == "1": print("YES") return elif x(s[i:]) == 1: print("YES") return xor ^= int(s[i]) if xor == 1: xor = 0 k -= 1 xor = 0 k = l[1] for i in range(n): if k == 1: if i == n - 1: if s[i] == "0": print("YES") return elif x(s[i:]) == 0: print("YES") return else: xor ^= int(s[i]) if xor == 0: xor = 0 k -= 1 print("NO") def x(s): r = 0 for i in s: r ^= int(i) return r t = int(input()) while t > 0: l = list(map(int, input().split())) s = input() solve() t -= 1
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR STRING RETURN IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR STRING RETURN IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
tc = int(input()) for _ in range(tc): n, k = map(int, input().split()) s = input() ones = s.count("1") zeros = n - ones yes, no = "YES", "NO" if ones % 2 == 1: if k % 2 == 1 and ones >= k: print(yes) else: print(no) elif k % 2 == 0 and ones % 2 == 0 and ones >= k: print(yes) else: pairs, zs = 0, 0 start = False for i in range(n): if s[i] == "1": if start: pairs += 1 start = False else: start = True elif not start: zs += 1 if pairs + zs >= k: print(yes) else: print(no)
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR STRING STRING IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
for T in range(int(input())): N, K = map(int, input().split()) S = input() ZeroCount = 0 ZP = False OneCount = 0 prev = " " for ch in S: if prev == " " and ch == "0": ZeroCount += 1 prev = "0" elif prev == " " and ch == "1": prev = "1" elif prev == "0" and ch == "0": ZeroCount += 1 elif prev == "0" and ch == "1": prev = "1" elif prev == "1" and ch == "0": pass elif prev == "1" and ch == "1": ZeroCount += 1 prev = "0" if prev == "0": ZP = True for ch in S: if ch == "1": OneCount += 1 if ZP and ZeroCount >= K: print("YES") elif OneCount >= K and (OneCount - K) % 2 == 0: print("YES") else: print("NO")
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 FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR STRING VAR NUMBER ASSIGN VAR STRING IF VAR STRING VAR STRING ASSIGN VAR STRING IF VAR STRING VAR STRING VAR NUMBER IF VAR STRING VAR STRING ASSIGN VAR STRING IF VAR STRING VAR STRING IF VAR STRING VAR STRING VAR NUMBER ASSIGN VAR STRING IF VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
def cal_xor(c: int): z = K flag = xor = 0 for i in range(N): xor ^= int(s[i]) if z == 1: flag = 1 z -= 1 if not flag and xor == c: z -= 1 xor = 0 if xor == c and z == 0: return 1 return 0 def calculate(): a = cal_xor(0) b = cal_xor(1) return "YES" if a or b else "NO" T = int(input()) for _ in range(T): N, K = map(int, input().split()) s = input() print(calculate())
FUNC_DEF VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER RETURN VAR VAR STRING STRING 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 FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
ans = "" for _ in range(int(input())): n, k = list(map(int, input().split())) s = input() evenSub = 0 oddSub = 0 cnt1 = 0 cnt2 = 0 oneCount = 0 for i in range(n): if s[i] == "1": oneCount += 1 cnt1 += 1 cnt2 += 1 if cnt1 % 2 == 0: evenSub += 1 cnt1 = 0 if cnt2 % 2 == 1: oddSub += 1 cnt2 = 0 if oneCount % 2 == 0 and evenSub >= k: ans += "YES\n" elif oddSub >= k and oddSub % 2 == k % 2: ans += "YES\n" else: ans += "NO\n" print(ans)
ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING IF VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
for _ in range(int(input())): n, k = map(int, input().split()) s = input() x, c1 = 0, 0 for i in s: x ^= int(i) if x == 0: c1 += 1 if c1 >= k and x != 1: print("YES") else: y, c2 = 0, 0 for i in s: y ^= int(i) if y == 1: c2 += 1 y = 0 if c2 >= k and (c2 - k) % 2 == 0: print("YES") else: print("NO")
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 FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
def f(): x, c = 0, 0 for i in range(n): x ^= l[i] == "1" if x == 1 and c < k - 1 and i != n - 1: c += 1 x = 0 if c == k - 1 and x == 1: return "YES" x, c = 0, 0 for i in range(n): x ^= l[i] == "1" if x == 0 and c < k - 1 and i != n - 1: c += 1 x = 0 if c == k - 1 and x == 0: return "YES" return "NO" for _ in range(int(input())): n, k = map(int, input().split()) l = input() print(f())
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR STRING IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER RETURN STRING ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR STRING IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER RETURN STRING RETURN STRING 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 FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
for _ in range(int(input())): n, k = map(int, input().split()) s = list(input()) if n == k: if s.count("1") == n or s.count("1") == 0: print("YES") else: print("NO") else: f = True if s.count("1") % 2: f = False else: c = 0 t = 0 for ch in s: if ch == "1": t += 1 if t % 2 == 0: c += 1 t = 0 if c < k: f = False if f: print("YES") else: f = True if abs(s.count("1") - k) % 2: f = False else: c = 0 t = 0 for ch in s: if ch == "1": t += 1 if t % 2: c += 1 t = 0 if c < k: f = False if f: print("YES") else: print("NO")
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 FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR IF FUNC_CALL VAR STRING VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
def count(s): n = len(s) i = 0 ans = 0 while i < n: if s[i] == "0": ans += 1 i += 1 else: i += 1 while i < n and s[i] != "1": i += 1 ans += 1 i += 1 return ans for _ in range(int(input())): n, k = list(map(int, input().split())) s = input() c = s.count("1") if c % 2 != 0: if k % 2 == 0: print("NO") elif k <= c: print("YES") else: print("NO") else: c = s.count("1") if k % 2 == 0: if k <= c: print("YES") elif k <= count(s): print("YES") else: print("NO") elif k <= count(s): print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
t = int(input()) for i in range(t): n, k = map(int, input().split()) s = input() xor = [(0) for i in range(n)] xor[0] = int(s[0]) cnt = 0 if s[0] == "0": cnt += 1 for i in range(1, n): xor[i] = xor[i - 1] ^ int(s[i]) if xor[i] == 0: cnt += 1 if xor[-1] == 0: if cnt >= k: print("YES") else: cnt = 0 b = 0 for i in range(n - 1, -1, -1): if b == 0: if xor[i] == 0: cnt += 1 b = 1 elif xor[i] == 1: cnt += 1 b = 0 if k % 2 == 0 and cnt >= k: print("YES") else: print("NO") else: cnt = 0 b = 1 for i in range(n - 1, -1, -1): if b == 0: if xor[i] == 0: cnt += 1 b = 1 elif xor[i] == 1: cnt += 1 b = 0 if k % 2 != 0 and cnt >= k: print("YES") else: print("NO")
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 FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
for _ in range(int(input())): n, k = map(int, input().split()) s = input() sum = 0 x = True m = 0 for i in s: if i == "1": sum += 1 if sum % 2 == 1: x = False else: x = True if x: if i == "0": m += 1 if k == 1: print("YES") continue if "1" not in s: print("YES") continue if sum % 2 == 0: if m + sum // 2 >= k: print("YES") elif k % 2 == 0: if k <= sum: print("YES") else: print("NO") else: print("NO") elif k <= sum: if k % 2 == 1: print("YES") else: print("NO") else: print("NO")
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 FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF STRING VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
t = int(input()) for i in range(t): n, k = map(int, input().split()) s = input() e = 0 ind = 0 c = 0 h = 0 lst1 = [] while True: if ind > n - 1 or h == k - 1: break else: if s[ind] == "1": c += 1 if c % 2 == 0: c = 0 h += 1 ind += 1 co = s[ind:].count("1") if co % 2 == 0 and h == k - 1 and s[ind:] != "": print("YES") else: e = 0 ind = 0 c = 0 h = 0 lst1 = [] while True: if ind > n - 1 or h == k - 1: break else: if s[ind] == "1": c += 1 if c % 2 != 0: c = 0 h += 1 ind += 1 co = s[ind:].count("1") if co % 2 != 0 and h == k - 1 and s[ind:] != "": print("YES") else: print("NO")
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 FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = input() zc = s.count("0") oc = s.count("1") ttl_xor = 0 for i in s: ttl_xor ^= int(i) xor = 0 count = 0 for i in s: u = int(i) xor ^= u if xor == 0: count += 1 if count == k: break if count < k: ans1 = False elif xor == ttl_xor: ans1 = True else: ans1 = False xorb = 0 xor = 0 count = 0 for i in s: u = int(i) xor ^= u xorb ^= u if xor == 1: count += 1 xor = 0 if count == k: break if count < k: ans2 = False elif xorb == ttl_xor: ans2 = True else: ans2 = False if ans1 or ans2: print("YES") else: print("NO")
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
T = int(input()) for _ in range(T): N, K = map(int, input().split()) binary_str = input() int_str = [] arr = [] cur = 0 count = {(0): 0, (1): 0} int_count = {(0): 0, (1): 0} for c in binary_str: cur = int(c) ^ cur arr.append(cur) count[cur] += 1 int_count[int(c)] += 1 if arr[-1] == 0 and count[0] >= K: print("YES") elif (int_count[1] - K) % 2 == 0 and int_count[1] >= K: print("YES") else: print("NO")
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 FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR STRING IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
for _ in range(int(input())): n, k = map(int, input().split()) s = input() if k == 1: print("YES") else: ones = s.count("1") zeroes = len(s) - ones if ones % 2 == 1: if k % 2 == 1 and k <= ones: print("YES") continue elif k % 2 == 0 and k <= ones: print("YES") continue else: count = 0 ans = 0 for i in range(len(s)): if s[i] == "1": count += 1 if count % 2 == 0: ans += 1 if k <= ans: print("YES") continue print("NO")
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 FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
def zxor(k): c = 0 pos = 0 while k > 0 and pos < n: if s[pos] == "1": c += 1 elif c == 0: k -= 1 if c == 2: k -= 1 c = 0 pos += 1 if k == 0: break c = 0 if k > 0: return False for j in range(pos, n): if s[j] == "1": c += 1 return c % 2 == 0 for i in range(int(input())): n, k = map(int, input().split()) s = input() c1 = s.count("1") if zxor(k) or c1 - k >= 0 and (c1 - k) % 2 == 0: print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR NUMBER RETURN BIN_OP VAR NUMBER 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
t = int(input()) for test in range(t): n, k = map(int, input().split()) s = input() n1 = s.count("1") c1 = n1 + 0 g = k + 0 if k <= n1: c1 = c1 - k + 1 if c1 % 2 == 1: print("YES") continue if n1 % 2 == 1: pass else: twis = 0 cont = 0 for i in range(n): if s[i] == "1": if twis == 1: cont += 1 twis = 0 continue if twis == 0: twis = 1 elif twis == 0: cont += 1 if cont >= k: print("YES") continue print("NO")
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
for _ in range(int(input())): n, k = map(int, input().split()) s = list(input()) count = 0 u = 0 for i in range(n): u = u ^ int(s[i]) if u == 0: count += 1 v = 0 count2 = 0 for i in range(n): if s[i] == "1" and count2 != k: count2 += 1 elif count2 == k: v ^= int(s[i]) if count >= k and u == 0: print("YES") elif v == 0 and count2 == k: print("YES") else: print("NO")
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 FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
for _ in range(int(input())): n, k = map(int, input().split()) s = input() cnt = [0, 0] a = 0 b = 0 for i in range(n): if s[i] == "1": a += 1 b += 1 if a == 1: cnt[0] += 1 a = 0 if s[i] == "0" and b % 2 == 0 or b == 2: cnt[1] += 1 b = 0 if k <= cnt[0] and (cnt[0] - k) % 2 == 0 or k <= cnt[1] and b == 0: print("YES") else: print("NO")
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 FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
T = int(input()) for _ in range(T): N, X = map(int, input().split()) s = input() x = 0 for i in s: if i == "1": x += 1 y = 0 a = 0 for i in s: if a == 0: if i == "0": y += 1 else: a = 1 - a elif i != "0": a = 1 - a y += 1 if X % 2 == 0: if x % 2 == 0: if x >= X: print("YES") continue elif y >= X: print("YES") continue else: if x % 2 == 1: if x >= X: print("YES") continue if x % 2 == 0: if y >= X: print("YES") continue print("NO")
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 FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR STRING ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Chef has a binary string S of length N. He wonders if it is possible to divide S into exactly K non-empty [substrings] such that each S_{i} belongs to exactly one substring and the \texttt{XOR} of each substring is the same. Can you help Chef to determine if it is possible to do so? Note: \texttt{XOR} of substring S_{L \cdots R} is defined as: \texttt{XOR} (S_{L \cdots R}) = S_{L} \oplus S_{L+1} \oplus \cdots \oplus S_{R}. Here, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the number of substrings in which S has to be divided. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output YES if S can be divided into K substrings such that \texttt{XOR} of each substring is the same. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ K ≀N ≀ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 4 3 2 111 5 3 01100 8 3 01001111 6 2 000100 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Test case 1: It can be proven that there is no way to divide $S = 111$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same. Test case 2: One of the possible ways of dividing $S$ is: $\underline{0}$ $\underline{11}$ $\underline{00}$. Here $\texttt{XOR}$ of each substring is $0$. Test case 3: One of the possible ways of dividing $S$ is: $\underline{01}$ $\underline{001}$ $\underline{111}$. Here $\texttt{XOR}$ of each substring is $1$. Test case 4: It can be proven that there is no way to divide $S = 000100$ into $K = 2$ substrings such that $\texttt{XOR}$ of each substring is the same.
def solve(N, K, S): count0 = 0 count1 = 0 if len(S) < K: print("NO") return if K == 1: print("YES") return found0 = 0 found1 = 0 count1 = 0 i = 0 while i < len(S): if S[i] == "0": found0 += 1 i += 1 else: i += 1 count1 += 1 while i < len(S) and S[i] != "1": i += 1 if i < len(S): found1 += 1 count1 += 1 i += 1 if found0 + found1 >= K: if count1 == 0: print("YES") return elif count1 % 2 == 0: print("YES") return found0 = 0 found1 = 0 i = 0 while i < len(S): if S[i] == "0": i += 1 else: found1 += 1 i += 1 if found1 >= K and (found1 - K) % 2 == 0: print("YES") return print("NO") return T = int(input()) res = [] for i in range(T): s = input().split() N = int(s[0]) K = int(s[1]) S = input() solve(N, K, S)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
import sys input = sys.stdin.buffer.readline def solve_tc(): n, k = list(map(int, input().split())) a = list(map(int, input().split())) ans = float("-inf") for i in range(max(0, n - 110), n): for j in range(i + 1, n): ans = max(ans, (i + 1) * (j + 1) - k * (a[i] | a[j])) return ans t = int(input()) for _ in range(t): sys.stdout.write(str(solve_tc()) + "\n")
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR 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 ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR STRING
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
for _ in range(int(input())): n, k = list(map(int, input().split())) l = list(map(int, input().split())) c = float("-inf") m = max(n - 2 * k, 0) for i in range(m, n): for j in range(i + 1, n): c = max(c, (i + 1) * (j + 1) - k * (l[i] | l[j])) print(c)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
for test in range(int(input())): def solve(): n, k = map(int, input().split()) arr = list(map(int, input().split())) ar = arr[-101:] k1 = len(ar) ans = -9999999999999 add = n - k1 + 1 for i in range(k1): for j in range(i + 1, k1): ans = max(ans, (add + i) * (add + j) - k * (ar[i] | ar[j])) return ans print(solve())
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR 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 VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) r = n l = max(0, r - 2 * k - 5) ans = -1145141919810 for i in range(l, r): ai = a[i] for j in range(i + 1, r): aj = a[j] ans = max(ans, (i + 1) * (j + 1) - k * (ai | aj)) 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
import sys input = sys.stdin.buffer.readline for _ in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) ans = -1000000000.0 l = 1 while l * l < k * n: l += 1 l += 5 for i in range(max(0, n - l), n): for j in range(i + 1, n): ans = max(ans, (i + 1) * (j + 1) - k * (a[i] | a[j])) print(ans)
IMPORT ASSIGN 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
def solve(n, k, a): x = n * (n - 1) - k * (a[-1] | a[-2]) r = list(reversed(range(n))) for i in r: for j in r: if (i + 1) * (j + 1) < x: break if j >= i: continue y = (i + 1) * (j + 1) - k * (a[i] | a[j]) x = max(x, y) return x t = int(input()) for i in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) print(solve(n, k, a))
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR IF BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = list(map(int, input().split()))[::-1] ans = -float("inf") b = a[0 : min(n, 200)] l = len(b) for i in range(l): for j in range(i + 1, l): ans = max(ans, (n - i) * (n - j) - k * (b[i] | b[j])) 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
def main(): N, K = map(int, input().split()) A = list(map(int, input().split())) Ans = -1001001001 L = max(1, N - 2 * K - 1) for i in range(L, N + 1): for j in range(i + 1, N + 1): Value = i * j - K * (A[i - 1] | A[j - 1]) if Ans < Value: Ans = Value print(Ans) T = int(input()) for _ in range(T): main()
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 NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN 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