description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | t = int(input())
for _ in range(t):
s = list(str(input()))
n = len(s)
nxt = [-1] * n
for i in range(n):
if s[i] == "1":
nxt[i] = i
elif i != 0:
nxt[i] = nxt[i - 1]
res = 0
for r in range(n):
sum_ = 0
for l in range(r, -1, -1):
if r - l > 20:
break
if s[l] == "0":
continue
sum_ += 1 << r - l
if l == 0:
if sum_ <= r - -1:
res += 1
elif sum_ <= r - nxt[l - 1]:
res += 1
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER IF VAR VAR STRING VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | t = int(input())
for _ in range(t):
ln = [int(x) for x in input()]
sz, zc, ans = len(ln), 0, 0
for i in range(sz):
if ln[i] == 1:
num, rl = 0, min(i + 20, sz)
for j in range(i, rl):
num = num * 2 + ln[j]
if j - i + 1 + zc >= num:
ans += 1
zc = 0
else:
zc += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | for _ in range(int(input())):
s = [int(x) for x in list(input().strip())]
n = len(s)
an = 0
c = 0
for i in range(n):
if s[i] == 1:
p = 0
for j in range(18):
if i + j == n:
break
else:
p = p << 1
p += s[i + j]
if p >= j + 1 and p <= j + 1 + c:
an += 1
c = 0
else:
c += 1
print(an) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | R = lambda: map(int, input().split())
for _ in range(int(input())):
s = [i for i in input()]
l = len(s)
L = [(0) for i in range(l)]
c = 0
for i in range(l):
if s[i] == "1":
L[i] = c
c = 0
else:
c += 1
i = l - 1
res = 0
while i >= 0:
if s[i] == "1":
k = min(l, i + 20)
t = "0b" + "".join(s[i:k])
v = int(t, 2)
p = k - i
while p > 0:
if p + L[i] >= v:
res += 1
v >>= 1
p -= 1
i -= 1
print(res) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP STRING FUNC_CALL STRING VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
s = stdin.readline()
s = s[:-1]
n = len(s)
precomp = []
for i in range(len(s)):
if i == 0 and s[i] == "0":
precomp.append(-1)
elif s[i] == "1":
precomp.append(i)
else:
precomp.append(precomp[-1])
l = 0
r = 0
count = 0
while r < n:
l = r
sum1 = 0
while l >= 0 and r - l < 20:
if s[l] != "0":
sum1 += 1 << r - l
if sum1 == r - l + 1:
count += 1
if sum1 > r - l + 1:
dif = sum1 - r + l - 1
ls = l - dif
rs = ls + dif - 1
if (
ls >= 0
and rs <= n - 1
and s[ls] == "0"
and s[rs] == "0"
and precomp[ls] == precomp[rs]
):
count += 1
l -= 1
r += 1
stdout.write(str(count) + "\n") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR STRING VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR STRING VAR VAR STRING VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | import sys
inf = 2 * 10**5
inf_bin = len(bin(inf)[2:])
def pre_calculate_amount_of_zero_left(s):
prefix = [0] * len(s)
cur_amount = 0
for i in range(0, len(s)):
if s[i] == "0":
cur_amount += 1
elif s[i] == "1":
prefix[i] = cur_amount
cur_amount = 0
return prefix
def solve_for_bit_len(string, pref, bit_len):
ans = 0
for i in range(0, len(string) - bit_len + 1):
if string[i] == "1":
num = int(string[i : i + bit_len], 2)
rem = num - bit_len
if num <= inf and rem <= pref[i]:
ans += 1
return ans
for q in range(0, int(sys.stdin.readline().strip())):
s = str(sys.stdin.readline().strip())
amount_of_zero_left = pre_calculate_amount_of_zero_left(s)
ans = 0
for bit_len in range(1, inf_bin + 1):
ans += int(solve_for_bit_len(s, amount_of_zero_left, bit_len))
print(ans) | IMPORT ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | for i in range(int(input())):
s = input()
n = len(s)
prev = 0
ans = 0
dp = [-1] * (n + 1)
se = set()
for i in range(n):
if s[i] == "1":
dp[i + 1] = i
else:
dp[i + 1] = dp[i]
for i in range(n):
for j in range(i + 1, min(n + 1, i + 19)):
e = s[i:j]
y = int(e, 2)
if len(e) == y:
if (i, j) not in se:
ans += 1
se.add((i, j))
elif y > 18:
if y <= len(e) + i - dp[i] - 1:
ser = j - y + 1
if (ser, j) not in se:
ans += 1
se.add((ser, j))
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | from sys import stdin, stdout
t = int(stdin.readline().strip())
for caso in range(t):
s = stdin.readline().strip()
n = len(s)
ans = 0
z = [(0) for i in range(n)]
for i in range(n):
if s[i] == "0":
z[i] += 1
if i > 0:
z[i] += z[i - 1]
if s[i] == "1":
z[i] = 0
for i in range(n):
x = 1
aux = ""
if s[i] == "1":
for j in range(i, min(n, i + 21)):
aux += s[j]
aux1 = 0
if i > 0:
aux1 += z[i - 1]
if int(aux, 2) <= aux1 + x:
ans += 1
x += 1
stdout.write("%d\n" % ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING IF VAR VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | ll = lambda: list(map(int, input().split()))
testcases = 1
[testcases] = ll()
for _ in range(testcases):
s = input()
n = len(s)
dp = [0] * (n + 1)
dp[0] = int(s[0] == "0")
for i in range(1, n):
if s[i] == "1":
dp[i] = 0
else:
dp[i] = dp[i - 1] + 1
ans = 0
for i in range(n):
sub = ""
num = 0
l = 0
if s[i] == "1":
for j in range(0, min(n - i + 1, 30)):
try:
num *= 2
if num == 0 and s[i + j] == "1":
msd = i + j
num += int(s[i + j] == "1")
sub += s[i + j]
except:
continue
if i == 0:
ans += num == j + 1
elif num - dp[i - 1] <= j + 1 and num >= j + 1:
ans += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN LIST VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | t = int(input())
for _ in range(t):
s = [int(i) for i in input().strip()]
n = len(s)
left_distance = [0] * n
left_distance[0] = int(not s[0])
for i in range(1, n):
if not s[i]:
left_distance[i] = left_distance[i - 1] + 1
ct = 0
for i in range(n):
if s[i]:
cmp = 0
if i:
cmp = left_distance[i - 1]
b = ""
j = i
while j < min(i + 20, n):
b += str(s[j])
if int(b, 2) <= 1 + j - i + cmp:
ct += 1
j += 1
print(ct) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | for _ in range(int(input())):
s = input()
block = min(20, len(s))
zeros = [0] * len(s)
count = 0
ans = 0
for i in range(len(s)):
zeros[i] = count
if s[i] == "1":
count = -1
count += 1
for r in range(len(s) - 1, -1, -1):
for l in range(r, r - block, -1):
if l < 0:
break
if s[l] == "1":
if r - l + 1 + zeros[l] >= int(s[l : r + 1], 2):
ans += 1
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER IF VAR VAR STRING IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | def decimal(ss):
ans = 0
c = 0
for i in range(len(ss) - 1, -1, -1):
if ss[i] == "1":
ans += 2**c
c += 1
return ans
for _ in range(int(input())):
s = input()
ll = len(s)
zer = [0] * (ll + 1)
s += "#"
for i in range(ll):
if s[i] == "0":
zer[i + 1] = zer[i] + 1
else:
zer[i + 1] = 0
ans = 0
for i in range(ll):
if s[i] == "1":
for j in range(min(20, ll - i)):
no = decimal(s[i : i + j + 1])
if zer[i] + j + 1 >= no:
ans += 1
print(ans) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | for _ in range(int(input())):
s = input()
num_zero = 0
ans = 0
length = len(s)
for i in range(length):
if s[i] == "0":
num_zero += 1
else:
act_num = 1
j = i
is_right = True
while j < length and is_right:
if act_num - (j - i + 1) <= num_zero:
ans += 1
j += 1
if j < length:
act_num = act_num * 2 + int(s[j])
else:
is_right = False
num_zero = 0
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | t = int(input())
for _ in [0] * t:
s = input()
stack = []
zero_count = 0
ans = 0
for c in map(int, s):
new_stack = []
append = new_stack.append
if c:
append((c, zero_count))
ans += 1
zero_count = 0
else:
zero_count += 1
for v, zeros in stack:
v = (v << 1) + c
need_zeros = v - v.bit_length()
if need_zeros <= zeros:
ans += 1
append((v, zeros))
stack = new_stack
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | t = int(input())
for i in range(t):
a = [int(x) for x in list(input())]
n = len(a)
zero = 0
arr = 0
for i in range(n):
if a[i] == 1:
size = 2
num = 1
arr += 1
if i != n - 1:
j = i + 1
if a[j] == 1:
num *= 2 + 1
else:
num *= 2
while num <= size + zero and num >= size:
arr += 1
if j == n - 1:
break
j += 1
if a[j] == 1:
num = num * 2 + 1
else:
num *= 2
size += 1
zero = 0
else:
zero += 1
print(arr) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP NUMBER NUMBER VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | def decimal(s, e, k):
a = 0
m = 1
for j in range(e, s - 1, -1):
a += k[j] * m
m *= 2
return a
for _ in range(int(input())):
k = [int(i) for i in input()]
n = len(k)
zer = [0] * n
for i in range(1, n):
if k[i - 1] == 0:
zer[i] = zer[i - 1] + 1
else:
zer[i] = 0
ans = 0
for i in range(n):
if k[i] == 0:
continue
else:
for j in range(i, min(n, 21 + i), 1):
le = j - i + 1
an = decimal(i, j, k)
if an < le:
p = 0
elif an <= le + zer[i]:
ans += 1
print(ans) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | import sys
def main():
t = int(sys.stdin.readline().strip())
for _ in range(t):
s = sys.stdin.readline().strip()
c0 = ord("0")
s = [(ord(c) - c0) for c in s]
res = 0
n = len(s)
zeros = 0
for i in range(n):
if s[i] == 0:
zeros += 1
continue
j = 0
cur = 0
while i + j < n:
cur = cur * 2 + s[i + j]
need_zeros = cur - j - 1
if need_zeros <= zeros:
res += 1
else:
break
j += 1
zeros = 0
print(res)
main() | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | a = int(input())
for i in range(a):
s = input()
cnt = 1
ans = []
index = 0
for i in range(1, len(s)):
if s[i] == s[i - 1]:
cnt += 1
else:
ans.append([cnt, s[i - 1], index])
cnt = 1
index = i
ans.append([cnt, s[-1], index])
total = s.count("1") + s.count("10")
if ans[0][1] == "1":
ans.pop(0)
if len(ans) == 0:
print(total)
continue
cnt = ans[0][0]
for i in range(1, len(ans)):
if i % 2 == 0:
cnt = ans[i][0]
continue
t = ans[i][2]
g1 = 0
rem = 0
for i in range(t, len(s)):
rem = 2 * rem
rem += int(s[i] == "1")
if rem == 2 or rem == 1:
continue
cars = i - t + 1
req = rem - cars
if cnt < req:
break
total += 1
print(total) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING IF VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | t = int(input())
ans = []
for _ in range(t):
s = input()
n = len(s)
count = 0
la = []
prev = -1
for i in range(n):
st = []
for j in range(i, min(i + 18, n)):
st.append(s[j])
z = int("".join(st), 2)
if z >= len(st) and st[0] == "1":
k = z - len(st)
start = i - k
end = i - 1
if z == len(st):
count += 1
continue
if prev < i - k:
count += 1
if s[i] == "1":
prev = i
ans.append(count)
print(*ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER STRING ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | def f(l, r):
a = u[l : r + 1]
m = len(a)
m1 = 0
for i in range(m):
m1 += a[i] * 2 ** (m - i - 1)
return m1 == m
for _ in range(int(input())):
s = input()
u = list(map(int, list(s)))
n = len(u)
ans = 0
for a in range(1, min(19, n) + 1):
p = 0
for i in range(n - a + 1):
if u[i] == 0:
p += 1
continue
q = int(s[i : i + a], 2)
if q - a <= p and q >= a:
ans += 1
p = 0
print(ans) | FUNC_DEF ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | import sys
input = sys.stdin.readline
def getInt():
return int(input())
def getVars():
return map(int, input().split())
def getList():
return list(map(int, input().split()))
def getStr():
return input().strip()
def addDictList(d, key, val):
if key not in d:
d[key] = []
d[key].append(val)
def addDictInt(d, key, val):
if key not in d:
d[key] = 0
d[key] = val
def addDictCount(d, key):
if key not in d:
d[key] = 0
d[key] += 1
def addDictSum(d, key, val):
if key not in d:
d[key] = 0
d[key] += val
def max0(s):
m1 = 1
m2 = len(s)
while m2 - m1 > 5:
m3 = (m1 + m2) // 2
p = s.find("0" * m3)
if p > -1:
m1 = m3
else:
m2 = m3
res = 0
for i in range(m1, m2):
p = s.find("0" * i)
if p > -1:
res = i
else:
break
return res
t = getInt()
for _ in range(t):
s = getStr()
res = 0
d = {}
d1 = []
for i in range(len(s)):
if s[i] == "1":
d1.append(i)
d["1"] = d1
res += len(d1)
while len(d) > 0:
key, val = d.popitem()
d0, d1 = [], []
p1 = len(key) - 1
p2 = p1 + 1
for p in val:
if p + len(key) >= len(s):
continue
if s[p + len(key)] == "0":
if p >= p1 and s[p - p1 : p] == "0" * p1:
d0.append(p - p1)
elif p >= p2 and s[p - p2 : p] == "0" * p2:
d1.append(p - p2)
res += len(d0)
res += len(d1)
if len(d0) > 0:
d["0" * p1 + key + "0"] = d0
if len(d1) > 0:
d["0" * p2 + key + "1"] = d1
print(res) | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP STRING VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP STRING VAR IF VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR FUNC_CALL VAR VAR STRING IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP STRING VAR VAR STRING VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP STRING VAR VAR STRING VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | import sys
D = {}
m = 18
for i in range(1, 1 << m):
D[bin(i)[2:]] = i
for _ in range(int(input())):
S = sys.stdin.readline().rstrip()
s = 0
N = len(S)
ans = 0
for i in range(N):
if S[i] == "1":
for j in range(1, min(m, N - i) + 1):
k = D[S[i : i + j]]
if s + j >= k:
ans += 1
s = 0
else:
s += 1
print(ans) | IMPORT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
si = lambda: input()
msi = lambda: map(int, stdin.readline().split())
lsi = lambda: list(msi())
t = ii()
for _ in range(t):
s = si()
n = len(s)
c = 0
ans = 0
for i in range(n):
if s[i] == "0":
c += 1
else:
sum = 0
y = 0
for j in range(i, n):
sum = sum * 2 + (s[j] == "1")
y += 1
if c + y >= sum:
ans += 1
else:
break
c = 0
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | t = int(input())
nm = {}
for i in range(1, 200001):
nm[format(i, "b")] = i
while t > 0:
s = input()
sm = [0]
for c in s:
sm.append(sm[-1] + (1 if c == "1" else 0))
ans = 0
for sz in range(1, 20):
for i in range(0, len(s) - sz + 1):
if s[i : i + sz] in nm:
num = nm[s[i : i + sz]]
if num == sz:
ans += 1
else:
li = i + sz - num
if li >= 0 == sm[i] - sm[li]:
ans += 1
print(ans)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR STRING NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$).
Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$.
The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$.
For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$.
Your task is to calculate the number of good substrings of string $s$.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of queries.
The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$.
It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$.
-----Output-----
For each query print one integer β the number of good substrings of string $s$.
-----Example-----
Input
4
0110
0101
00001000
0001000
Output
4
3
4
3 | for _ in range(int(input())):
s = input()
a = [-1] + [i for i, j in enumerate(s) if j == "1"]
ans = 0
for i, v in enumerate(a[1:], 1):
ans += 1
val = 1
for j in range(v + 1, len(s)):
val = (val << 1) + (s[j] == "1")
if val - (j - v + 1) <= v - a[i - 1] - 1:
ans += 1
else:
break
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR STRING IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef was impressed by an array A of N non-negative integers. But, he was asked to solve the following problem in order to get this array.
Given a non-negative integer k, find the number of pairs (i, j) (1 β€ i < j β€ N) such that the following condition holds true:
(A_{i} | A_{j}) + (A_{i} \oplus A_{j}) + (A_{i} \& A_{j}) = k + A_{j}, where
(A_{i} | A_{j}) denotes [Bitwise OR] operation,
(A_{i} \oplus A_{j}) denotes [Bitwise XOR] operation,
(A_{i} \& A_{j}) denotes [Bitwise AND] operation.
You, being Chef's friend, help him get this array by solving the above problem.
------ Input Format ------
- The first line contains an integer T, the number of test cases.
- The first line of each test case contains two space-separated integers N and k, the number of integers in the array A and the non-negative integer k respectively.
- The next line contains N space-separated non-negative integers A_{1},A_{2}, \ldots, A_{N}, the elements of the array A.
------ Output Format ------
For each test case, print a single line containing the number of pairs satisfying the mentioned condition.
------ Constraints ------
$1 β€ T β€ 200$
$1 β€ N β€ 10^{5}$
$0 β€ k β€ 10^{18}$
$0 β€ A_{i} β€ 10^{18}$
- Sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$.
----- Sample Input 1 ------
2
1 8
7
3 8
1 6 4
----- Sample Output 1 ------
0
2
----- explanation 1 ------
Test case $1$: The input array contains only a single integer. Thus, no pairs are possible.
Test case $2$: There are two pairs satisfying the condition -
- $(i,j) = (1, 2)$: $A_{1} = 1$ and $A_{2} = 6$. Thus, $(1|6) + (1\oplus 6) + (1$ $\&$ $6) = 7 + 7 + 0 = 14$. Also, $k+A_{2} = 8 + 6 = 14$.
- $(i,j) = (2, 3)$: $A_{2} = 6$ and $A_{3} = 4$. Thus, $(6|4) + (6\oplus 4) + (6$ $\&$ $4) = 6 + 2 + 4 = 12$. Also, $k+A_{3} = 8 + 4 = 12$. | for _ in range(int(input())):
n, k = map(int, input().split())
s = [int(i) for i in input().split()]
ans = 0
n1 = max(s)
d = dict()
for i in range(n):
t1 = k - s[i] ^ s[i]
if s[i] in d:
ans += d[s[i]]
if t1 in d:
d[t1] += 1
else:
d[t1] = 1
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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef was impressed by an array A of N non-negative integers. But, he was asked to solve the following problem in order to get this array.
Given a non-negative integer k, find the number of pairs (i, j) (1 β€ i < j β€ N) such that the following condition holds true:
(A_{i} | A_{j}) + (A_{i} \oplus A_{j}) + (A_{i} \& A_{j}) = k + A_{j}, where
(A_{i} | A_{j}) denotes [Bitwise OR] operation,
(A_{i} \oplus A_{j}) denotes [Bitwise XOR] operation,
(A_{i} \& A_{j}) denotes [Bitwise AND] operation.
You, being Chef's friend, help him get this array by solving the above problem.
------ Input Format ------
- The first line contains an integer T, the number of test cases.
- The first line of each test case contains two space-separated integers N and k, the number of integers in the array A and the non-negative integer k respectively.
- The next line contains N space-separated non-negative integers A_{1},A_{2}, \ldots, A_{N}, the elements of the array A.
------ Output Format ------
For each test case, print a single line containing the number of pairs satisfying the mentioned condition.
------ Constraints ------
$1 β€ T β€ 200$
$1 β€ N β€ 10^{5}$
$0 β€ k β€ 10^{18}$
$0 β€ A_{i} β€ 10^{18}$
- Sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$.
----- Sample Input 1 ------
2
1 8
7
3 8
1 6 4
----- Sample Output 1 ------
0
2
----- explanation 1 ------
Test case $1$: The input array contains only a single integer. Thus, no pairs are possible.
Test case $2$: There are two pairs satisfying the condition -
- $(i,j) = (1, 2)$: $A_{1} = 1$ and $A_{2} = 6$. Thus, $(1|6) + (1\oplus 6) + (1$ $\&$ $6) = 7 + 7 + 0 = 14$. Also, $k+A_{2} = 8 + 6 = 14$.
- $(i,j) = (2, 3)$: $A_{2} = 6$ and $A_{3} = 4$. Thus, $(6|4) + (6\oplus 4) + (6$ $\&$ $4) = 6 + 2 + 4 = 12$. Also, $k+A_{3} = 8 + 4 = 12$. | t = int(input())
while t > 0:
t -= 1
n, k = input().split()
n = int(n)
k = int(k)
a = [int(x) for x in input().split()]
d = {}
c = 0
for i in range(n):
x = a[i]
x = x ^ k - x
if a[i] in d.keys():
c += d[a[i]]
if x in d.keys():
d[x] += 1
else:
d[x] = 1
print(c) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef was impressed by an array A of N non-negative integers. But, he was asked to solve the following problem in order to get this array.
Given a non-negative integer k, find the number of pairs (i, j) (1 β€ i < j β€ N) such that the following condition holds true:
(A_{i} | A_{j}) + (A_{i} \oplus A_{j}) + (A_{i} \& A_{j}) = k + A_{j}, where
(A_{i} | A_{j}) denotes [Bitwise OR] operation,
(A_{i} \oplus A_{j}) denotes [Bitwise XOR] operation,
(A_{i} \& A_{j}) denotes [Bitwise AND] operation.
You, being Chef's friend, help him get this array by solving the above problem.
------ Input Format ------
- The first line contains an integer T, the number of test cases.
- The first line of each test case contains two space-separated integers N and k, the number of integers in the array A and the non-negative integer k respectively.
- The next line contains N space-separated non-negative integers A_{1},A_{2}, \ldots, A_{N}, the elements of the array A.
------ Output Format ------
For each test case, print a single line containing the number of pairs satisfying the mentioned condition.
------ Constraints ------
$1 β€ T β€ 200$
$1 β€ N β€ 10^{5}$
$0 β€ k β€ 10^{18}$
$0 β€ A_{i} β€ 10^{18}$
- Sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$.
----- Sample Input 1 ------
2
1 8
7
3 8
1 6 4
----- Sample Output 1 ------
0
2
----- explanation 1 ------
Test case $1$: The input array contains only a single integer. Thus, no pairs are possible.
Test case $2$: There are two pairs satisfying the condition -
- $(i,j) = (1, 2)$: $A_{1} = 1$ and $A_{2} = 6$. Thus, $(1|6) + (1\oplus 6) + (1$ $\&$ $6) = 7 + 7 + 0 = 14$. Also, $k+A_{2} = 8 + 6 = 14$.
- $(i,j) = (2, 3)$: $A_{2} = 6$ and $A_{3} = 4$. Thus, $(6|4) + (6\oplus 4) + (6$ $\&$ $4) = 6 + 2 + 4 = 12$. Also, $k+A_{3} = 8 + 4 = 12$. | t = int(input())
for _ in range(t):
a, b = map(int, input().split())
c = list(map(int, input().split()))
dict_ = {}
ans = 0
for i in range(a):
if c[i] in dict_.keys():
ans += dict_[c[i]]
if b >= c[i]:
dict_1 = b - c[i] ^ c[i]
if dict_1 in dict_.keys():
dict_[dict_1] += 1
else:
dict_[dict_1] = 1
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef was impressed by an array A of N non-negative integers. But, he was asked to solve the following problem in order to get this array.
Given a non-negative integer k, find the number of pairs (i, j) (1 β€ i < j β€ N) such that the following condition holds true:
(A_{i} | A_{j}) + (A_{i} \oplus A_{j}) + (A_{i} \& A_{j}) = k + A_{j}, where
(A_{i} | A_{j}) denotes [Bitwise OR] operation,
(A_{i} \oplus A_{j}) denotes [Bitwise XOR] operation,
(A_{i} \& A_{j}) denotes [Bitwise AND] operation.
You, being Chef's friend, help him get this array by solving the above problem.
------ Input Format ------
- The first line contains an integer T, the number of test cases.
- The first line of each test case contains two space-separated integers N and k, the number of integers in the array A and the non-negative integer k respectively.
- The next line contains N space-separated non-negative integers A_{1},A_{2}, \ldots, A_{N}, the elements of the array A.
------ Output Format ------
For each test case, print a single line containing the number of pairs satisfying the mentioned condition.
------ Constraints ------
$1 β€ T β€ 200$
$1 β€ N β€ 10^{5}$
$0 β€ k β€ 10^{18}$
$0 β€ A_{i} β€ 10^{18}$
- Sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$.
----- Sample Input 1 ------
2
1 8
7
3 8
1 6 4
----- Sample Output 1 ------
0
2
----- explanation 1 ------
Test case $1$: The input array contains only a single integer. Thus, no pairs are possible.
Test case $2$: There are two pairs satisfying the condition -
- $(i,j) = (1, 2)$: $A_{1} = 1$ and $A_{2} = 6$. Thus, $(1|6) + (1\oplus 6) + (1$ $\&$ $6) = 7 + 7 + 0 = 14$. Also, $k+A_{2} = 8 + 6 = 14$.
- $(i,j) = (2, 3)$: $A_{2} = 6$ and $A_{3} = 4$. Thus, $(6|4) + (6\oplus 4) + (6$ $\&$ $4) = 6 + 2 + 4 = 12$. Also, $k+A_{3} = 8 + 4 = 12$. | t = int(input())
for i in range(t):
n, k = list(map(int, input().split()))
d = dict()
a = list(map(int, input().split()))
b = []
for i in range(n):
b.append(k - a[i])
if a[i] in d:
d[a[i]] += 1
else:
d[a[i]] = 1
ans = 0
for i in range(n):
if b[i] < 0:
pass
else:
num = a[i] ^ b[i]
if num == a[i]:
ans += d[num] - 1
elif num in d:
ans += d[num]
d[a[i]] -= 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN 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 BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef was impressed by an array A of N non-negative integers. But, he was asked to solve the following problem in order to get this array.
Given a non-negative integer k, find the number of pairs (i, j) (1 β€ i < j β€ N) such that the following condition holds true:
(A_{i} | A_{j}) + (A_{i} \oplus A_{j}) + (A_{i} \& A_{j}) = k + A_{j}, where
(A_{i} | A_{j}) denotes [Bitwise OR] operation,
(A_{i} \oplus A_{j}) denotes [Bitwise XOR] operation,
(A_{i} \& A_{j}) denotes [Bitwise AND] operation.
You, being Chef's friend, help him get this array by solving the above problem.
------ Input Format ------
- The first line contains an integer T, the number of test cases.
- The first line of each test case contains two space-separated integers N and k, the number of integers in the array A and the non-negative integer k respectively.
- The next line contains N space-separated non-negative integers A_{1},A_{2}, \ldots, A_{N}, the elements of the array A.
------ Output Format ------
For each test case, print a single line containing the number of pairs satisfying the mentioned condition.
------ Constraints ------
$1 β€ T β€ 200$
$1 β€ N β€ 10^{5}$
$0 β€ k β€ 10^{18}$
$0 β€ A_{i} β€ 10^{18}$
- Sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$.
----- Sample Input 1 ------
2
1 8
7
3 8
1 6 4
----- Sample Output 1 ------
0
2
----- explanation 1 ------
Test case $1$: The input array contains only a single integer. Thus, no pairs are possible.
Test case $2$: There are two pairs satisfying the condition -
- $(i,j) = (1, 2)$: $A_{1} = 1$ and $A_{2} = 6$. Thus, $(1|6) + (1\oplus 6) + (1$ $\&$ $6) = 7 + 7 + 0 = 14$. Also, $k+A_{2} = 8 + 6 = 14$.
- $(i,j) = (2, 3)$: $A_{2} = 6$ and $A_{3} = 4$. Thus, $(6|4) + (6\oplus 4) + (6$ $\&$ $4) = 6 + 2 + 4 = 12$. Also, $k+A_{3} = 8 + 4 = 12$. | t = int(input())
for _ in range(t):
n, k = map(int, input().split())
a = list(map(int, input().split()))
memo, count = {}, 0
for i in a:
count += memo.get(i, 0)
memo[k - i ^ i] = memo.get(k - i ^ i, 0) + 1
print(count) | 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 DICT NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Chef was impressed by an array A of N non-negative integers. But, he was asked to solve the following problem in order to get this array.
Given a non-negative integer k, find the number of pairs (i, j) (1 β€ i < j β€ N) such that the following condition holds true:
(A_{i} | A_{j}) + (A_{i} \oplus A_{j}) + (A_{i} \& A_{j}) = k + A_{j}, where
(A_{i} | A_{j}) denotes [Bitwise OR] operation,
(A_{i} \oplus A_{j}) denotes [Bitwise XOR] operation,
(A_{i} \& A_{j}) denotes [Bitwise AND] operation.
You, being Chef's friend, help him get this array by solving the above problem.
------ Input Format ------
- The first line contains an integer T, the number of test cases.
- The first line of each test case contains two space-separated integers N and k, the number of integers in the array A and the non-negative integer k respectively.
- The next line contains N space-separated non-negative integers A_{1},A_{2}, \ldots, A_{N}, the elements of the array A.
------ Output Format ------
For each test case, print a single line containing the number of pairs satisfying the mentioned condition.
------ Constraints ------
$1 β€ T β€ 200$
$1 β€ N β€ 10^{5}$
$0 β€ k β€ 10^{18}$
$0 β€ A_{i} β€ 10^{18}$
- Sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$.
----- Sample Input 1 ------
2
1 8
7
3 8
1 6 4
----- Sample Output 1 ------
0
2
----- explanation 1 ------
Test case $1$: The input array contains only a single integer. Thus, no pairs are possible.
Test case $2$: There are two pairs satisfying the condition -
- $(i,j) = (1, 2)$: $A_{1} = 1$ and $A_{2} = 6$. Thus, $(1|6) + (1\oplus 6) + (1$ $\&$ $6) = 7 + 7 + 0 = 14$. Also, $k+A_{2} = 8 + 6 = 14$.
- $(i,j) = (2, 3)$: $A_{2} = 6$ and $A_{3} = 4$. Thus, $(6|4) + (6\oplus 4) + (6$ $\&$ $4) = 6 + 2 + 4 = 12$. Also, $k+A_{3} = 8 + 4 = 12$. | for _ in range(int(input())):
n, x = map(int, input().split())
a = list(map(int, input().split()))
d = dict()
ans = 0
for i in range(n - 1, -1, -1):
re = x - a[i] ^ a[i]
if re in d.keys():
ans += d[re]
if a[i] in d.keys():
d[a[i]] += 1
else:
d[a[i]] = 1
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef was impressed by an array A of N non-negative integers. But, he was asked to solve the following problem in order to get this array.
Given a non-negative integer k, find the number of pairs (i, j) (1 β€ i < j β€ N) such that the following condition holds true:
(A_{i} | A_{j}) + (A_{i} \oplus A_{j}) + (A_{i} \& A_{j}) = k + A_{j}, where
(A_{i} | A_{j}) denotes [Bitwise OR] operation,
(A_{i} \oplus A_{j}) denotes [Bitwise XOR] operation,
(A_{i} \& A_{j}) denotes [Bitwise AND] operation.
You, being Chef's friend, help him get this array by solving the above problem.
------ Input Format ------
- The first line contains an integer T, the number of test cases.
- The first line of each test case contains two space-separated integers N and k, the number of integers in the array A and the non-negative integer k respectively.
- The next line contains N space-separated non-negative integers A_{1},A_{2}, \ldots, A_{N}, the elements of the array A.
------ Output Format ------
For each test case, print a single line containing the number of pairs satisfying the mentioned condition.
------ Constraints ------
$1 β€ T β€ 200$
$1 β€ N β€ 10^{5}$
$0 β€ k β€ 10^{18}$
$0 β€ A_{i} β€ 10^{18}$
- Sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$.
----- Sample Input 1 ------
2
1 8
7
3 8
1 6 4
----- Sample Output 1 ------
0
2
----- explanation 1 ------
Test case $1$: The input array contains only a single integer. Thus, no pairs are possible.
Test case $2$: There are two pairs satisfying the condition -
- $(i,j) = (1, 2)$: $A_{1} = 1$ and $A_{2} = 6$. Thus, $(1|6) + (1\oplus 6) + (1$ $\&$ $6) = 7 + 7 + 0 = 14$. Also, $k+A_{2} = 8 + 6 = 14$.
- $(i,j) = (2, 3)$: $A_{2} = 6$ and $A_{3} = 4$. Thus, $(6|4) + (6\oplus 4) + (6$ $\&$ $4) = 6 + 2 + 4 = 12$. Also, $k+A_{3} = 8 + 4 = 12$. | t = int(input())
for _ in range(t):
n, k = map(int, input().split())
arr = list(map(int, input().split()))
d = {}
ans = 0
for x in arr:
try:
ans += d[x]
except:
pass
if x <= k:
ele = x ^ k - x
try:
d[ele] += 1
except:
d[ele] = 1
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef was impressed by an array A of N non-negative integers. But, he was asked to solve the following problem in order to get this array.
Given a non-negative integer k, find the number of pairs (i, j) (1 β€ i < j β€ N) such that the following condition holds true:
(A_{i} | A_{j}) + (A_{i} \oplus A_{j}) + (A_{i} \& A_{j}) = k + A_{j}, where
(A_{i} | A_{j}) denotes [Bitwise OR] operation,
(A_{i} \oplus A_{j}) denotes [Bitwise XOR] operation,
(A_{i} \& A_{j}) denotes [Bitwise AND] operation.
You, being Chef's friend, help him get this array by solving the above problem.
------ Input Format ------
- The first line contains an integer T, the number of test cases.
- The first line of each test case contains two space-separated integers N and k, the number of integers in the array A and the non-negative integer k respectively.
- The next line contains N space-separated non-negative integers A_{1},A_{2}, \ldots, A_{N}, the elements of the array A.
------ Output Format ------
For each test case, print a single line containing the number of pairs satisfying the mentioned condition.
------ Constraints ------
$1 β€ T β€ 200$
$1 β€ N β€ 10^{5}$
$0 β€ k β€ 10^{18}$
$0 β€ A_{i} β€ 10^{18}$
- Sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$.
----- Sample Input 1 ------
2
1 8
7
3 8
1 6 4
----- Sample Output 1 ------
0
2
----- explanation 1 ------
Test case $1$: The input array contains only a single integer. Thus, no pairs are possible.
Test case $2$: There are two pairs satisfying the condition -
- $(i,j) = (1, 2)$: $A_{1} = 1$ and $A_{2} = 6$. Thus, $(1|6) + (1\oplus 6) + (1$ $\&$ $6) = 7 + 7 + 0 = 14$. Also, $k+A_{2} = 8 + 6 = 14$.
- $(i,j) = (2, 3)$: $A_{2} = 6$ and $A_{3} = 4$. Thus, $(6|4) + (6\oplus 4) + (6$ $\&$ $4) = 6 + 2 + 4 = 12$. Also, $k+A_{3} = 8 + 4 = 12$. | t = int(input())
for _ in range(t):
lis = list(map(int, input().split()))
n, k = lis[0], lis[1]
a = list(map(int, input().split()))
count = 0
dic = {}
if n == 1:
print(0)
else:
for i in range(n):
aux = a[i] ^ k - a[i]
if a[i] in dic:
count += dic[a[i]]
if aux not in dic:
dic[aux] = 1
else:
dic[aux] += 1
print(count) | 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 VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef was impressed by an array A of N non-negative integers. But, he was asked to solve the following problem in order to get this array.
Given a non-negative integer k, find the number of pairs (i, j) (1 β€ i < j β€ N) such that the following condition holds true:
(A_{i} | A_{j}) + (A_{i} \oplus A_{j}) + (A_{i} \& A_{j}) = k + A_{j}, where
(A_{i} | A_{j}) denotes [Bitwise OR] operation,
(A_{i} \oplus A_{j}) denotes [Bitwise XOR] operation,
(A_{i} \& A_{j}) denotes [Bitwise AND] operation.
You, being Chef's friend, help him get this array by solving the above problem.
------ Input Format ------
- The first line contains an integer T, the number of test cases.
- The first line of each test case contains two space-separated integers N and k, the number of integers in the array A and the non-negative integer k respectively.
- The next line contains N space-separated non-negative integers A_{1},A_{2}, \ldots, A_{N}, the elements of the array A.
------ Output Format ------
For each test case, print a single line containing the number of pairs satisfying the mentioned condition.
------ Constraints ------
$1 β€ T β€ 200$
$1 β€ N β€ 10^{5}$
$0 β€ k β€ 10^{18}$
$0 β€ A_{i} β€ 10^{18}$
- Sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$.
----- Sample Input 1 ------
2
1 8
7
3 8
1 6 4
----- Sample Output 1 ------
0
2
----- explanation 1 ------
Test case $1$: The input array contains only a single integer. Thus, no pairs are possible.
Test case $2$: There are two pairs satisfying the condition -
- $(i,j) = (1, 2)$: $A_{1} = 1$ and $A_{2} = 6$. Thus, $(1|6) + (1\oplus 6) + (1$ $\&$ $6) = 7 + 7 + 0 = 14$. Also, $k+A_{2} = 8 + 6 = 14$.
- $(i,j) = (2, 3)$: $A_{2} = 6$ and $A_{3} = 4$. Thus, $(6|4) + (6\oplus 4) + (6$ $\&$ $4) = 6 + 2 + 4 = 12$. Also, $k+A_{3} = 8 + 4 = 12$. | for _ in range(int(input())):
n, k = map(int, input().rstrip().split())
l = list(map(int, input().rstrip().split()))
d = dict()
ans = 0
for ele in l:
ans += d.get(ele, 0)
if k >= ele:
x = k - ele ^ ele
d[x] = d.get(x, 0) + 1
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Chef was impressed by an array A of N non-negative integers. But, he was asked to solve the following problem in order to get this array.
Given a non-negative integer k, find the number of pairs (i, j) (1 β€ i < j β€ N) such that the following condition holds true:
(A_{i} | A_{j}) + (A_{i} \oplus A_{j}) + (A_{i} \& A_{j}) = k + A_{j}, where
(A_{i} | A_{j}) denotes [Bitwise OR] operation,
(A_{i} \oplus A_{j}) denotes [Bitwise XOR] operation,
(A_{i} \& A_{j}) denotes [Bitwise AND] operation.
You, being Chef's friend, help him get this array by solving the above problem.
------ Input Format ------
- The first line contains an integer T, the number of test cases.
- The first line of each test case contains two space-separated integers N and k, the number of integers in the array A and the non-negative integer k respectively.
- The next line contains N space-separated non-negative integers A_{1},A_{2}, \ldots, A_{N}, the elements of the array A.
------ Output Format ------
For each test case, print a single line containing the number of pairs satisfying the mentioned condition.
------ Constraints ------
$1 β€ T β€ 200$
$1 β€ N β€ 10^{5}$
$0 β€ k β€ 10^{18}$
$0 β€ A_{i} β€ 10^{18}$
- Sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$.
----- Sample Input 1 ------
2
1 8
7
3 8
1 6 4
----- Sample Output 1 ------
0
2
----- explanation 1 ------
Test case $1$: The input array contains only a single integer. Thus, no pairs are possible.
Test case $2$: There are two pairs satisfying the condition -
- $(i,j) = (1, 2)$: $A_{1} = 1$ and $A_{2} = 6$. Thus, $(1|6) + (1\oplus 6) + (1$ $\&$ $6) = 7 + 7 + 0 = 14$. Also, $k+A_{2} = 8 + 6 = 14$.
- $(i,j) = (2, 3)$: $A_{2} = 6$ and $A_{3} = 4$. Thus, $(6|4) + (6\oplus 4) + (6$ $\&$ $4) = 6 + 2 + 4 = 12$. Also, $k+A_{3} = 8 + 4 = 12$. | t = int(input())
for _ in range(t):
n, k = map(int, input().split(" "))
arr = list(map(int, input().split(" ")))
dict_count = {}
for idx in range(n):
if arr[idx] in dict_count:
dict_count[arr[idx]] += 1
else:
dict_count[arr[idx]] = 1
res = 0
for idx in range(n):
temp = k - arr[idx] ^ arr[idx]
if temp in dict_count:
res += dict_count[temp]
if arr[idx] == temp:
res = res - 1
dict_count[arr[idx]] -= 1
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef was impressed by an array A of N non-negative integers. But, he was asked to solve the following problem in order to get this array.
Given a non-negative integer k, find the number of pairs (i, j) (1 β€ i < j β€ N) such that the following condition holds true:
(A_{i} | A_{j}) + (A_{i} \oplus A_{j}) + (A_{i} \& A_{j}) = k + A_{j}, where
(A_{i} | A_{j}) denotes [Bitwise OR] operation,
(A_{i} \oplus A_{j}) denotes [Bitwise XOR] operation,
(A_{i} \& A_{j}) denotes [Bitwise AND] operation.
You, being Chef's friend, help him get this array by solving the above problem.
------ Input Format ------
- The first line contains an integer T, the number of test cases.
- The first line of each test case contains two space-separated integers N and k, the number of integers in the array A and the non-negative integer k respectively.
- The next line contains N space-separated non-negative integers A_{1},A_{2}, \ldots, A_{N}, the elements of the array A.
------ Output Format ------
For each test case, print a single line containing the number of pairs satisfying the mentioned condition.
------ Constraints ------
$1 β€ T β€ 200$
$1 β€ N β€ 10^{5}$
$0 β€ k β€ 10^{18}$
$0 β€ A_{i} β€ 10^{18}$
- Sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$.
----- Sample Input 1 ------
2
1 8
7
3 8
1 6 4
----- Sample Output 1 ------
0
2
----- explanation 1 ------
Test case $1$: The input array contains only a single integer. Thus, no pairs are possible.
Test case $2$: There are two pairs satisfying the condition -
- $(i,j) = (1, 2)$: $A_{1} = 1$ and $A_{2} = 6$. Thus, $(1|6) + (1\oplus 6) + (1$ $\&$ $6) = 7 + 7 + 0 = 14$. Also, $k+A_{2} = 8 + 6 = 14$.
- $(i,j) = (2, 3)$: $A_{2} = 6$ and $A_{3} = 4$. Thus, $(6|4) + (6\oplus 4) + (6$ $\&$ $4) = 6 + 2 + 4 = 12$. Also, $k+A_{3} = 8 + 4 = 12$. | q = int(input())
for _ in range(q):
inp = list(map(int, input().split()))
n = inp[0]
k = inp[1]
a = list(map(int, input().split()))
f = {}
count = 0
for i in range(n):
b_i = k - a[i] ^ a[i]
count += f.get(a[i], 0)
if b_i in f:
f[b_i] += 1
else:
f[b_i] = 1
print(count) | 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef was impressed by an array A of N non-negative integers. But, he was asked to solve the following problem in order to get this array.
Given a non-negative integer k, find the number of pairs (i, j) (1 β€ i < j β€ N) such that the following condition holds true:
(A_{i} | A_{j}) + (A_{i} \oplus A_{j}) + (A_{i} \& A_{j}) = k + A_{j}, where
(A_{i} | A_{j}) denotes [Bitwise OR] operation,
(A_{i} \oplus A_{j}) denotes [Bitwise XOR] operation,
(A_{i} \& A_{j}) denotes [Bitwise AND] operation.
You, being Chef's friend, help him get this array by solving the above problem.
------ Input Format ------
- The first line contains an integer T, the number of test cases.
- The first line of each test case contains two space-separated integers N and k, the number of integers in the array A and the non-negative integer k respectively.
- The next line contains N space-separated non-negative integers A_{1},A_{2}, \ldots, A_{N}, the elements of the array A.
------ Output Format ------
For each test case, print a single line containing the number of pairs satisfying the mentioned condition.
------ Constraints ------
$1 β€ T β€ 200$
$1 β€ N β€ 10^{5}$
$0 β€ k β€ 10^{18}$
$0 β€ A_{i} β€ 10^{18}$
- Sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$.
----- Sample Input 1 ------
2
1 8
7
3 8
1 6 4
----- Sample Output 1 ------
0
2
----- explanation 1 ------
Test case $1$: The input array contains only a single integer. Thus, no pairs are possible.
Test case $2$: There are two pairs satisfying the condition -
- $(i,j) = (1, 2)$: $A_{1} = 1$ and $A_{2} = 6$. Thus, $(1|6) + (1\oplus 6) + (1$ $\&$ $6) = 7 + 7 + 0 = 14$. Also, $k+A_{2} = 8 + 6 = 14$.
- $(i,j) = (2, 3)$: $A_{2} = 6$ and $A_{3} = 4$. Thus, $(6|4) + (6\oplus 4) + (6$ $\&$ $4) = 6 + 2 + 4 = 12$. Also, $k+A_{3} = 8 + 4 = 12$. | T = int(input())
for t in range(1, T + 1):
inp = input().split()
N = int(inp[0])
K = int(inp[1])
A = list(map(int, input().split()))
cnt = 0
freq = {}
for a in A:
if a in freq.keys():
cnt += freq[a]
val = K - a ^ a
if val in freq.keys():
freq[val] += 1
else:
freq[val] = 1
print(cnt) | 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef was impressed by an array A of N non-negative integers. But, he was asked to solve the following problem in order to get this array.
Given a non-negative integer k, find the number of pairs (i, j) (1 β€ i < j β€ N) such that the following condition holds true:
(A_{i} | A_{j}) + (A_{i} \oplus A_{j}) + (A_{i} \& A_{j}) = k + A_{j}, where
(A_{i} | A_{j}) denotes [Bitwise OR] operation,
(A_{i} \oplus A_{j}) denotes [Bitwise XOR] operation,
(A_{i} \& A_{j}) denotes [Bitwise AND] operation.
You, being Chef's friend, help him get this array by solving the above problem.
------ Input Format ------
- The first line contains an integer T, the number of test cases.
- The first line of each test case contains two space-separated integers N and k, the number of integers in the array A and the non-negative integer k respectively.
- The next line contains N space-separated non-negative integers A_{1},A_{2}, \ldots, A_{N}, the elements of the array A.
------ Output Format ------
For each test case, print a single line containing the number of pairs satisfying the mentioned condition.
------ Constraints ------
$1 β€ T β€ 200$
$1 β€ N β€ 10^{5}$
$0 β€ k β€ 10^{18}$
$0 β€ A_{i} β€ 10^{18}$
- Sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$.
----- Sample Input 1 ------
2
1 8
7
3 8
1 6 4
----- Sample Output 1 ------
0
2
----- explanation 1 ------
Test case $1$: The input array contains only a single integer. Thus, no pairs are possible.
Test case $2$: There are two pairs satisfying the condition -
- $(i,j) = (1, 2)$: $A_{1} = 1$ and $A_{2} = 6$. Thus, $(1|6) + (1\oplus 6) + (1$ $\&$ $6) = 7 + 7 + 0 = 14$. Also, $k+A_{2} = 8 + 6 = 14$.
- $(i,j) = (2, 3)$: $A_{2} = 6$ and $A_{3} = 4$. Thus, $(6|4) + (6\oplus 4) + (6$ $\&$ $4) = 6 + 2 + 4 = 12$. Also, $k+A_{3} = 8 + 4 = 12$. | def xor(a, k):
return k - a ^ a
def ans(arr, k):
d = {}
answer = 0
for y in arr:
if y in d.keys():
answer += d[y]
t = xor(y, k)
if t in d.keys():
d[t] += 1
else:
d[t] = 1
return answer
test_cases = int(input())
while test_cases != 0:
d = list(map(int, input().split()))
d2 = list(map(int, input().split()))
print(ans(d2, d[1]))
test_cases -= 1 | FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER |
Chef was impressed by an array A of N non-negative integers. But, he was asked to solve the following problem in order to get this array.
Given a non-negative integer k, find the number of pairs (i, j) (1 β€ i < j β€ N) such that the following condition holds true:
(A_{i} | A_{j}) + (A_{i} \oplus A_{j}) + (A_{i} \& A_{j}) = k + A_{j}, where
(A_{i} | A_{j}) denotes [Bitwise OR] operation,
(A_{i} \oplus A_{j}) denotes [Bitwise XOR] operation,
(A_{i} \& A_{j}) denotes [Bitwise AND] operation.
You, being Chef's friend, help him get this array by solving the above problem.
------ Input Format ------
- The first line contains an integer T, the number of test cases.
- The first line of each test case contains two space-separated integers N and k, the number of integers in the array A and the non-negative integer k respectively.
- The next line contains N space-separated non-negative integers A_{1},A_{2}, \ldots, A_{N}, the elements of the array A.
------ Output Format ------
For each test case, print a single line containing the number of pairs satisfying the mentioned condition.
------ Constraints ------
$1 β€ T β€ 200$
$1 β€ N β€ 10^{5}$
$0 β€ k β€ 10^{18}$
$0 β€ A_{i} β€ 10^{18}$
- Sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$.
----- Sample Input 1 ------
2
1 8
7
3 8
1 6 4
----- Sample Output 1 ------
0
2
----- explanation 1 ------
Test case $1$: The input array contains only a single integer. Thus, no pairs are possible.
Test case $2$: There are two pairs satisfying the condition -
- $(i,j) = (1, 2)$: $A_{1} = 1$ and $A_{2} = 6$. Thus, $(1|6) + (1\oplus 6) + (1$ $\&$ $6) = 7 + 7 + 0 = 14$. Also, $k+A_{2} = 8 + 6 = 14$.
- $(i,j) = (2, 3)$: $A_{2} = 6$ and $A_{3} = 4$. Thus, $(6|4) + (6\oplus 4) + (6$ $\&$ $4) = 6 + 2 + 4 = 12$. Also, $k+A_{3} = 8 + 4 = 12$. | T = int(input())
for i in range(T):
Nk = str(input()).split(" ")
N = int(Nk[0])
k = int(Nk[1])
A = str(input()).split(" ")
dicto = {}
total = 0
for j in range(N):
total = total + dicto.get(int(A[j]), 0)
dicto[k - int(A[j]) ^ int(A[j])] = dicto.get(k - int(A[j]) ^ int(A[j]), 0) + 1
print(total) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Chef was impressed by an array A of N non-negative integers. But, he was asked to solve the following problem in order to get this array.
Given a non-negative integer k, find the number of pairs (i, j) (1 β€ i < j β€ N) such that the following condition holds true:
(A_{i} | A_{j}) + (A_{i} \oplus A_{j}) + (A_{i} \& A_{j}) = k + A_{j}, where
(A_{i} | A_{j}) denotes [Bitwise OR] operation,
(A_{i} \oplus A_{j}) denotes [Bitwise XOR] operation,
(A_{i} \& A_{j}) denotes [Bitwise AND] operation.
You, being Chef's friend, help him get this array by solving the above problem.
------ Input Format ------
- The first line contains an integer T, the number of test cases.
- The first line of each test case contains two space-separated integers N and k, the number of integers in the array A and the non-negative integer k respectively.
- The next line contains N space-separated non-negative integers A_{1},A_{2}, \ldots, A_{N}, the elements of the array A.
------ Output Format ------
For each test case, print a single line containing the number of pairs satisfying the mentioned condition.
------ Constraints ------
$1 β€ T β€ 200$
$1 β€ N β€ 10^{5}$
$0 β€ k β€ 10^{18}$
$0 β€ A_{i} β€ 10^{18}$
- Sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$.
----- Sample Input 1 ------
2
1 8
7
3 8
1 6 4
----- Sample Output 1 ------
0
2
----- explanation 1 ------
Test case $1$: The input array contains only a single integer. Thus, no pairs are possible.
Test case $2$: There are two pairs satisfying the condition -
- $(i,j) = (1, 2)$: $A_{1} = 1$ and $A_{2} = 6$. Thus, $(1|6) + (1\oplus 6) + (1$ $\&$ $6) = 7 + 7 + 0 = 14$. Also, $k+A_{2} = 8 + 6 = 14$.
- $(i,j) = (2, 3)$: $A_{2} = 6$ and $A_{3} = 4$. Thus, $(6|4) + (6\oplus 4) + (6$ $\&$ $4) = 6 + 2 + 4 = 12$. Also, $k+A_{3} = 8 + 4 = 12$. | for t in range(int(input())):
N, k = map(int, input().split())
A = list(map(int, input().split()))
A.reverse()
count = 0
d = dict()
for i in A:
count += d.get(k - i ^ i, 0)
if i not in d:
d[i] = 1
else:
d[i] += 1
print(count) | 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 EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef and his friends would like to attend a large New Year's party in Goa. However, their teacher has given them an assignment to complete. Chef asked for your help to complete the assignment quickly so that he can attend the party with complete freedom.
Chef was given an array A = [A_{1}, A_{2}, \ldots, A_{N}] of length N, an array B = [B_{1}, B_{2}, \ldots, B_{M}] of length M, and a prime number P. His task is to find the number of good pairs. An ordered pair of integers (i, j) is said to be a good pair if it satisfies the following conditions:
1 β€ i β€ N
1 β€ j β€ M
(A_{i} \oplus B_{j}) < P
P divides ((A_{i} \cdot (A_{i} \oplus B_{j})) - 1)
where \oplus denotes the [bitwise XOR] operation.
Please help Chef and his friends, and find the number of good pairs.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains three space-separated integers N, M, and P, respectively.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
- The third line of each test case contains M space-separated integers B_{1}, B_{2}, \ldots, B_{M}.
------ Output Format ------
For each test case, output one line containing a single integer β the number of good pairs.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N, M β€ 2 \cdot 10^{5}$
$2 β€ A_{i}, B_{j} β€ 10^{9}$ for every $1β€ i β€ N$ and $1β€ j β€ M$
$2 β€ P β€ 10^{9}$
$P$ is a prime number
- Sum of $N$ across all test cases will not exceed $2 \cdot 10^{5}$
- Sum of $M$ across all test cases will not exceed $2 \cdot 10^{5}$
----- Sample Input 1 ------
3
2 2 17
7 14
2 13
1 1 11
2
2
4 3 101
10 91 99 200
81 182 149
----- Sample Output 1 ------
1
0
3
----- explanation 1 ------
Test Case $1$: There are $4$ ordered pairs of indices. Looking at them individually,
- $(1,1)$ is a good pair because $(7\oplus 2) = 5 < 17$, and $((7 \cdot (7 \oplus 2)) - 1) = 34$ is divisible by $17$.
- $(1, 2)$ is not a good pair because $((7 \cdot (7 \oplus 13)) - 1) = 69$ is not divisible by $17$.
- $(2, 1)$ is not a good pair because $((14 \cdot (14 \oplus 2)) - 1) = 167$ is not divisible by $17$.
- $(2, 2)$ is not a good pair because $((14 \cdot (14 \oplus 13)) - 1) = 41$ is not divisible by $17$.
Test Case $2$: The only ordered pair of indices is $(1, 1)$. This is not a good pair because $((2 \cdot (2 \oplus 2)) - 1) = -1$ is not divisible by $11$, since it leaves a remainder of $10$. | def func():
n, m, p = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
dicti = {}
for i in B:
dicti[i] = dicti.get(i, 0) + 1
ans = 0
for ai in A:
if ai % p != 0:
modinv = pow(ai, p - 2, p)
b = ai ^ modinv
ans += dicti.get(b, 0)
return ans
for _ in range(int(input())):
print(func()) | FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
Chef and his friends would like to attend a large New Year's party in Goa. However, their teacher has given them an assignment to complete. Chef asked for your help to complete the assignment quickly so that he can attend the party with complete freedom.
Chef was given an array A = [A_{1}, A_{2}, \ldots, A_{N}] of length N, an array B = [B_{1}, B_{2}, \ldots, B_{M}] of length M, and a prime number P. His task is to find the number of good pairs. An ordered pair of integers (i, j) is said to be a good pair if it satisfies the following conditions:
1 β€ i β€ N
1 β€ j β€ M
(A_{i} \oplus B_{j}) < P
P divides ((A_{i} \cdot (A_{i} \oplus B_{j})) - 1)
where \oplus denotes the [bitwise XOR] operation.
Please help Chef and his friends, and find the number of good pairs.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains three space-separated integers N, M, and P, respectively.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
- The third line of each test case contains M space-separated integers B_{1}, B_{2}, \ldots, B_{M}.
------ Output Format ------
For each test case, output one line containing a single integer β the number of good pairs.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N, M β€ 2 \cdot 10^{5}$
$2 β€ A_{i}, B_{j} β€ 10^{9}$ for every $1β€ i β€ N$ and $1β€ j β€ M$
$2 β€ P β€ 10^{9}$
$P$ is a prime number
- Sum of $N$ across all test cases will not exceed $2 \cdot 10^{5}$
- Sum of $M$ across all test cases will not exceed $2 \cdot 10^{5}$
----- Sample Input 1 ------
3
2 2 17
7 14
2 13
1 1 11
2
2
4 3 101
10 91 99 200
81 182 149
----- Sample Output 1 ------
1
0
3
----- explanation 1 ------
Test Case $1$: There are $4$ ordered pairs of indices. Looking at them individually,
- $(1,1)$ is a good pair because $(7\oplus 2) = 5 < 17$, and $((7 \cdot (7 \oplus 2)) - 1) = 34$ is divisible by $17$.
- $(1, 2)$ is not a good pair because $((7 \cdot (7 \oplus 13)) - 1) = 69$ is not divisible by $17$.
- $(2, 1)$ is not a good pair because $((14 \cdot (14 \oplus 2)) - 1) = 167$ is not divisible by $17$.
- $(2, 2)$ is not a good pair because $((14 \cdot (14 \oplus 13)) - 1) = 41$ is not divisible by $17$.
Test Case $2$: The only ordered pair of indices is $(1, 1)$. This is not a good pair because $((2 \cdot (2 \oplus 2)) - 1) = -1$ is not divisible by $11$, since it leaves a remainder of $10$. | for tc in range(int(input())):
ls = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
p = ls[-1]
ct = 0
dis = dict()
for x in b:
dis[x] = 1 if dis.get(x) is None else dis[x] + 1
for x in a:
if x % p:
minv = pow(x, p - 2, p)
if dis.get(minv ^ x) is not None:
ct += dis[minv ^ x]
print(ct) | FOR VAR FUNC_CALL 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 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 NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NONE NUMBER BIN_OP VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR VAR NONE VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Chef and his friends would like to attend a large New Year's party in Goa. However, their teacher has given them an assignment to complete. Chef asked for your help to complete the assignment quickly so that he can attend the party with complete freedom.
Chef was given an array A = [A_{1}, A_{2}, \ldots, A_{N}] of length N, an array B = [B_{1}, B_{2}, \ldots, B_{M}] of length M, and a prime number P. His task is to find the number of good pairs. An ordered pair of integers (i, j) is said to be a good pair if it satisfies the following conditions:
1 β€ i β€ N
1 β€ j β€ M
(A_{i} \oplus B_{j}) < P
P divides ((A_{i} \cdot (A_{i} \oplus B_{j})) - 1)
where \oplus denotes the [bitwise XOR] operation.
Please help Chef and his friends, and find the number of good pairs.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains three space-separated integers N, M, and P, respectively.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
- The third line of each test case contains M space-separated integers B_{1}, B_{2}, \ldots, B_{M}.
------ Output Format ------
For each test case, output one line containing a single integer β the number of good pairs.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N, M β€ 2 \cdot 10^{5}$
$2 β€ A_{i}, B_{j} β€ 10^{9}$ for every $1β€ i β€ N$ and $1β€ j β€ M$
$2 β€ P β€ 10^{9}$
$P$ is a prime number
- Sum of $N$ across all test cases will not exceed $2 \cdot 10^{5}$
- Sum of $M$ across all test cases will not exceed $2 \cdot 10^{5}$
----- Sample Input 1 ------
3
2 2 17
7 14
2 13
1 1 11
2
2
4 3 101
10 91 99 200
81 182 149
----- Sample Output 1 ------
1
0
3
----- explanation 1 ------
Test Case $1$: There are $4$ ordered pairs of indices. Looking at them individually,
- $(1,1)$ is a good pair because $(7\oplus 2) = 5 < 17$, and $((7 \cdot (7 \oplus 2)) - 1) = 34$ is divisible by $17$.
- $(1, 2)$ is not a good pair because $((7 \cdot (7 \oplus 13)) - 1) = 69$ is not divisible by $17$.
- $(2, 1)$ is not a good pair because $((14 \cdot (14 \oplus 2)) - 1) = 167$ is not divisible by $17$.
- $(2, 2)$ is not a good pair because $((14 \cdot (14 \oplus 13)) - 1) = 41$ is not divisible by $17$.
Test Case $2$: The only ordered pair of indices is $(1, 1)$. This is not a good pair because $((2 \cdot (2 \oplus 2)) - 1) = -1$ is not divisible by $11$, since it leaves a remainder of $10$. | from sys import stdin
input = stdin.readline
def answer():
d = dict()
for i in range(m):
d[b[i]] = d.get(b[i], 0) + 1
ans = 0
for i in range(n):
if a[i] % p == 0:
continue
x = pow(a[i], p - 2, p)
ans += d.get(a[i] ^ x, 0)
return ans
for T in range(int(input())):
n, m, p = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
print(answer()) | ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
Chef and his friends would like to attend a large New Year's party in Goa. However, their teacher has given them an assignment to complete. Chef asked for your help to complete the assignment quickly so that he can attend the party with complete freedom.
Chef was given an array A = [A_{1}, A_{2}, \ldots, A_{N}] of length N, an array B = [B_{1}, B_{2}, \ldots, B_{M}] of length M, and a prime number P. His task is to find the number of good pairs. An ordered pair of integers (i, j) is said to be a good pair if it satisfies the following conditions:
1 β€ i β€ N
1 β€ j β€ M
(A_{i} \oplus B_{j}) < P
P divides ((A_{i} \cdot (A_{i} \oplus B_{j})) - 1)
where \oplus denotes the [bitwise XOR] operation.
Please help Chef and his friends, and find the number of good pairs.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains three space-separated integers N, M, and P, respectively.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
- The third line of each test case contains M space-separated integers B_{1}, B_{2}, \ldots, B_{M}.
------ Output Format ------
For each test case, output one line containing a single integer β the number of good pairs.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N, M β€ 2 \cdot 10^{5}$
$2 β€ A_{i}, B_{j} β€ 10^{9}$ for every $1β€ i β€ N$ and $1β€ j β€ M$
$2 β€ P β€ 10^{9}$
$P$ is a prime number
- Sum of $N$ across all test cases will not exceed $2 \cdot 10^{5}$
- Sum of $M$ across all test cases will not exceed $2 \cdot 10^{5}$
----- Sample Input 1 ------
3
2 2 17
7 14
2 13
1 1 11
2
2
4 3 101
10 91 99 200
81 182 149
----- Sample Output 1 ------
1
0
3
----- explanation 1 ------
Test Case $1$: There are $4$ ordered pairs of indices. Looking at them individually,
- $(1,1)$ is a good pair because $(7\oplus 2) = 5 < 17$, and $((7 \cdot (7 \oplus 2)) - 1) = 34$ is divisible by $17$.
- $(1, 2)$ is not a good pair because $((7 \cdot (7 \oplus 13)) - 1) = 69$ is not divisible by $17$.
- $(2, 1)$ is not a good pair because $((14 \cdot (14 \oplus 2)) - 1) = 167$ is not divisible by $17$.
- $(2, 2)$ is not a good pair because $((14 \cdot (14 \oplus 13)) - 1) = 41$ is not divisible by $17$.
Test Case $2$: The only ordered pair of indices is $(1, 1)$. This is not a good pair because $((2 \cdot (2 \oplus 2)) - 1) = -1$ is not divisible by $11$, since it leaves a remainder of $10$. | for _ in range(int(input())):
n, m, p = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
xb = [[b[i], i] for i in range(m)]
xb = sorted(xb)
b = [xb[0][0], 1]
for i in range(1, m):
if xb[i][0] == xb[i - 1][0]:
b[-1] += 1
else:
b += [xb[i][0], 1]
b = {b[i]: b[i + 1] for i in range(0, len(b), 2)}
ans = 0
for i in range(n):
if a[i] % p > 0:
inv = pow(a[i], p - 2, p)
temp = a[i] ^ inv
ans += b.setdefault(temp, 0)
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR LIST VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | t = int(input())
for i in range(t):
a = input()
b = input()
a_ = []
b_ = []
if len(a) > len(b):
c = 0
while len(b) + c != len(a):
b_.append("0")
c += 1
elif len(a) < len(b):
d = 0
while len(a) + d != len(b):
a_.append("0")
d += 1
for i in a:
a_.append(i)
for i in b:
b_.append(i)
pair = []
for i in range(len(a_)):
d = a_[i], b_[i]
pair.append(d)
pair.reverse()
pair_rev = pair
if int(b) == 0:
print(0)
else:
l = 0
for i in range(len(pair_rev)):
if pair_rev[i][0] == "1" and pair_rev[i][1] == "1":
c = 1
j = i + 1
while j < len(pair_rev) and (
pair_rev[j][0] == "1"
and pair_rev[j][1] == "0"
or pair_rev[j][0] == "0"
and pair_rev[j][1] == "1"
):
c += 1
j += 1
if l < c:
l = c
print(l + 1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING VAR VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER STRING VAR VAR NUMBER STRING VAR VAR NUMBER STRING VAR VAR NUMBER STRING VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | from sys import stdin
t = int(stdin.readline())
while t:
a = stdin.readline().strip()
b = stdin.readline().strip()
if b == "0":
print(0)
else:
la, lb = len(a), len(b)
l = 0
if la > lb:
b = "0" * (la - lb) + b
l = la
else:
a = "0" * (lb - la) + a
l = lb
res, i = 1, l - 1
while i >= 0:
if a[i] == "1" and b[i] == "1":
j = i - 1
while j >= 0 and a[j] != b[j]:
j -= 1
res = max(res, i - j + 1)
i = j
else:
i -= 1
print(res)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | t = int(input())
while t > 0:
t -= 1
a = input()
b = input()
k = int(b, 2)
if k == 0:
print(0)
continue
c = ""
if len(a) > len(b):
b = c.zfill(len(a) - len(b)) + b
else:
a = c.zfill(len(b) - len(a)) + a
mx = 1
i = len(a) - 1
while i >= 0:
s = -1
if a[i] == "1" and b[i] == "1":
s = i
j = i - 1
e = -1
while j >= 0:
if a[j] == "1" and b[j] == "1" or a[j] == "0" and b[j] == "0":
e = j
break
j -= 1
i = j
if s - e + 1 > mx:
mx = s - e + 1
else:
i -= 1
j = 0
if s > -1 and e == -1:
e = j - 1
if s - e + 1 > mx:
mx = s - e + 1
print(mx) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | def solve(X, Y):
if Y == "0":
return 0
n = max(len(X), len(Y))
c = ""
for i in range(abs(len(X) - len(Y))):
c += "0"
if len(X) < len(Y):
X = c + X
else:
Y = c + Y
ans = 1
for i in range(n - 1, -1, -1):
if X[i] == "1" and Y[i] == "1":
cur = 2
j = i - 1
while j >= 0 and X[j] != Y[j]:
j -= 1
cur += 1
ans = max(ans, cur)
return ans
for _ in range(int(input())):
X = input()
Y = input()
ans = solve(X, Y)
print(ans) | FUNC_DEF IF VAR STRING RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | t = int(input())
while t > 0:
s1 = str(input())
s2 = str(input())
t -= 1
if "1" not in s2:
print(0)
else:
if len(s2) < len(s1):
for i in range(abs(len(s2) - len(s1))):
s2 = "0" + s2
else:
for i in range(abs(len(s2) - len(s1))):
s1 = "0" + s1
s1 = s1[::-1]
s2 = s2[::-1]
ans = 0
tre = 0
for i in range(len(s1)):
k1 = s1[i]
k2 = s2[i]
if k1 == k2 == "1":
x = i + 1
while x < len(s1):
if s1[x] == s2[x]:
break
x += 1
val = abs(i - x)
if val > tre:
ans += abs(val - tre)
tre = val
print(ans + 1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | def alt_add(A, B):
C = int(A, 2)
D = int(B, 2)
if D == 0:
return 0
x = C & D
y = C ^ D
p = len(A)
q = len(B)
maxi = q
if p > q:
maxi = p
x = bin(x)[2:].zfill(maxi)
y = bin(y)[2:].zfill(maxi)
maxi = 0
max_till = 0
for i in range(len(y)):
if y[i] == "1" and x[i] == "0":
maxi = maxi + 1
elif y[i] == "0" and x[i] == "0":
maxi = 0
elif y[i] == "0" and x[i] == "1":
max_till = max(max_till, maxi)
maxi = 0
if max_till == 0 and x.count("1") > 0:
return 2
elif max_till == 0 and x.count("1") == 0:
return 1
else:
return max_till + 2
T = int(input())
for t in range(T):
A = input().strip()
B = input().strip()
print(alt_add(A, B)) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FUNC_CALL VAR STRING NUMBER RETURN NUMBER IF VAR NUMBER FUNC_CALL VAR STRING NUMBER RETURN NUMBER RETURN BIN_OP VAR NUMBER 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 FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | T = int(input())
for k in range(T):
a = input()
b = input()
if b == "0":
print("0")
elif a == "0":
print("1")
else:
if len(a) > len(b):
b = "0" * (len(a) - len(b)) + b
else:
a = "0" * (len(b) - len(a)) + a
mx = 1
flag = -1
count = 0
for i in range(len(a) - 1, -1, -1):
if flag == 0:
if a[i] == "1" and b[i] == "0" or b[i] == "1" and a[i] == "0":
count = 1 + count
else:
flag = -1
count = 0
if a[i] == b[i] and a[i] == "1":
flag = 0
count = 2
if count > mx:
mx = count
print(mx) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR STRING IF VAR STRING EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | def samelength(n, p, ln, lp):
if lp == ln:
return n, p, ln, lp
elif lp > ln:
for _ in range(lp - ln):
n = "0" + n
ln = lp
else:
for _ in range(ln - lp):
p = "0" + p
lp = ln
return n, p, ln, lp
def addb(a, b, c):
if a == b and b == c and c == "0":
return "0", "0"
elif a == b and b == c and c == "1":
return "1", "1"
elif a == "0" and b == c and c == "1":
return "0", "1"
elif a == "0":
return "1", "0"
elif a == "1" and b == c and b == "0":
return "1", "0"
return "0", "1"
def coans(n, p):
ln = len(n)
lp = len(p)
n, p, ln, lp = samelength(n, p, ln, lp)
carry = ["0"]
ans = 0
c0 = 0
for i in range(1, ln + 1):
d, f = addb(carry[-1], n[-i], p[-i])
carry.append(f)
n = "0" + n
p = "0" + p
for i in range(1, ln + 2):
if carry[i - 1] == "1":
c0 += 1
if carry[i - 1] == "1" and n[-i] == "1" and p[-i] == "1":
ans = max(c0, ans)
c0 = 0
if carry[i - 1] == "1" and n[-i] == "0" and p[-i] == "0":
ans = max(c0, ans)
c0 = 0
else:
ans = max(c0, ans)
c0 = 0
ans = max(c0, ans)
return ans + 1
for _ in range(int(input())):
n = input()
p = input()
dp = int(p, base=2)
if dp == 0:
print("0")
else:
print(coans(n, p)) | FUNC_DEF IF VAR VAR RETURN VAR VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR VAR RETURN VAR VAR VAR VAR FUNC_DEF IF VAR VAR VAR VAR VAR STRING RETURN STRING STRING IF VAR VAR VAR VAR VAR STRING RETURN STRING STRING IF VAR STRING VAR VAR VAR STRING RETURN STRING STRING IF VAR STRING RETURN STRING STRING IF VAR STRING VAR VAR VAR STRING RETURN STRING STRING RETURN STRING STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | try:
t = int(input())
for _ in range(t):
A = input()
B = input()
fcount = 0
flag = 1
DecimalA = int(A, 2)
DecimalB = int(B, 2)
if DecimalB == 0:
print(0)
continue
if DecimalA == 0 and DecimalB != 0:
print(1)
continue
var = abs(len(A) - len(B))
if len(A) > len(B):
B = "0" * var + B
else:
A = "0" * var + A
i = len(A) - 1
while i >= 0:
count = 1
if A[i] == B[i] and A[i] == "1":
flag = 0
j = i - 1
while A[j] != B[j] and j >= 0:
count += 1
j -= 1
if fcount < count:
fcount = count
i -= 1
if flag == 0:
print(fcount + 1)
else:
print(1)
except:
pass | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | t = int(input())
for _ in range(t):
a = input()
b = input()
if "1" not in b:
print(0)
elif "1" not in a:
print(1)
else:
n = max(len(a), len(b))
a = a.zfill(n)
b = b.zfill(n)
count = 1
for i in range(n):
if a[i] == "1" and b[i] == "1":
c = 2
for j in range(i - 1, -1, -1):
if a[j] != b[j]:
c += 1
else:
break
count = max(c, count)
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF STRING VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | for _ in range(int(input())):
a = input()
b = input()
if b == "0":
print(0)
continue
la = len(a)
lb = len(b)
l = la
if la > lb:
b = "0" * (la - lb) + b
elif la < lb:
a = "0" * (lb - la) + a
l = lb
a = "0" + a
b = "0" + b
ans = 0
count = 0
for i in range(l, -1, -1):
if a[i] == "1" and b[i] == "1":
ans = max(ans, count)
count = 1
elif a[i] == "0" and b[i] == "0":
ans = max(ans, count)
count = 0
elif count:
count += 1
print(ans + 1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | t = int(input())
for j in range(t):
a = int(input(), 2)
b = int(input(), 2)
i = 0
while b > 0:
u = a ^ b
v = a & b
a = u
b = 2 * v
i += 1
print(i) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | t = int(input())
for _ in range(t):
A = input()
B = input()
a = int(A, 2)
b = int(B, 2)
A = list(A)
B = list(B)
ans = 0
if len(A) > len(B):
B = B[::-1]
while len(B) != len(A):
B.append("0")
B = B[::-1]
if len(A) < len(B):
A = A[::-1]
while len(A) != len(B):
A.append("0")
A = A[::-1]
B = B[::-1]
C = []
A = A[::-1]
maxx = 0
if a > 1000000 and b > 10000000:
for i in range(len(A)):
C.append(int(A[i]) + int(B[i]))
for i in range(len(A)):
if C[i] == 2:
i += 1
temp = 0
while i < len(A) and C[i] == 1:
temp += 1
i += 1
if temp > maxx:
maxx = temp
else:
i += 1
if b == 0:
print(0)
elif a == 0:
print(1)
else:
print(maxx + 2)
else:
ans = 0
while b > 0:
U = a ^ b
V = a & b
b = 2 * V
a = U
ans += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | T = int(input())
for i in range(T):
A = input()
B = input()
if B == "0":
print("0")
else:
if len(A) > len(B):
B = B.zfill(len(A))
else:
A = A.zfill(len(B))
ans = 1
for i in range(len(A) - 1, -1, -1):
if A[i] == "1" and B[i] == "1":
temp = 2
i -= 1
while i >= 0:
if A[i] != B[i]:
i -= 1
temp += 1
if A[i] == B[i]:
if A[i] == "1":
i += 1
break
ans = max(temp, ans)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | from sys import stdin, stdout
__author__ = "Ronald Kaiser"
__email__ = "raios dot catodicos at gmail dot com"
rline = lambda: stdin.readline().strip()
def solve():
global A, B
if not "1" in B:
return 0
diff = len(A) - len(B)
if diff > 0:
B = "0" * diff + B
else:
A = "0" * -diff + A
s = 1
for i in range(len(A) - 1, -1, -1):
if A[i] == B[i] == "1":
j = i - 1
while j >= 0 and A[j] != B[j]:
j -= 1
s = max(s, i - j + 1)
return s
def read_input():
global A, B
A, B = "0" + rline(), "0" + rline()
def write_output(s):
stdout.write("\n".join(map(str, s)))
def main():
s = []
for _ in range(int(input())):
read_input()
answer = solve()
s.append(answer)
write_output(s)
main() | ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF STRING VAR RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR BIN_OP STRING FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | t = int(input())
for i in range(t):
first_string = input()
second_string = input()
if int(second_string, 2) == 0:
print(0)
elif int(first_string, 2) == 0:
print(1)
else:
n1 = len(first_string)
n2 = len(second_string)
if n1 > n2:
temp_string = ""
for j in range(n1 - n2):
temp_string = temp_string + "0"
temp_string = temp_string + second_string
second_string = temp_string
elif n2 > n1:
temp_string = ""
for j in range(n2 - n1):
temp_string = temp_string + "0"
temp_string = temp_string + first_string
first_string = temp_string
x = len(first_string) - 1
compare_variable = -1
while x >= 0:
temp_count = 0
if first_string[x] == "1" and second_string[x] == "1":
x -= 1
while first_string[x] != second_string[x] and x >= 0:
temp_count += 1
x -= 1
if temp_count > compare_variable:
compare_variable = temp_count
else:
x -= 1
if compare_variable == -1:
print(1)
else:
print(compare_variable + 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | for _ in range(int(input())):
a = "0" + input()
b = "0" + input()
n = len(a)
m = len(b)
if b == "00":
print(0)
continue
elif a == "00":
print(1)
continue
if n < m:
a = "0" * (m - n) + a
elif n > m:
b = "0" * (n - m) + b
m = 0
for i in range(len(a) - 1, 0, -1):
cnt = 1
if a[i] == "1" and b[i] == "1":
j = -1
while True:
if a[j + i] != b[j + i]:
cnt += 1
else:
break
j -= 1
if m < cnt:
m = cnt
print(m + 1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER WHILE NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | t = int(input())
while t > 0:
a = input()
b = input()
l1 = len(a)
l2 = len(b)
l = 0
if l1 > l2:
l += l1
else:
l += l2
a = a.zfill(l)
b = b.zfill(l)
cnt = 0
k = 0
for i in range(0, l):
if a[i] == "1" and b[i] == "1":
cnt += 1
k = max(cnt, k)
cnt = 0
elif a[i] == "1" or b[i] == "1":
cnt += 1
else:
cnt = 0
b = int(b, 2)
if b == 0:
print(0)
else:
print(k + 1)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | def test(a, b):
if int(b) == 0:
return 0
l = max(len(a), len(b))
st_a = len(a)
st_b = len(b)
s = "0" * 10**5
if st_a > st_b:
b = s[: st_a - st_b] + b
else:
a = s[: st_b - st_a] + a
a = a[::-1]
b = b[::-1]
i = 0
ans = 1
while i < l:
if a[i] == "1" and b[i] == "1":
j = i + 1
while j < l and a[j] != b[j]:
j += 1
ans = max(ans, j - i + 1)
i = j - 1
i += 1
return ans
t = int(input())
while t:
t -= 1
a = input()
b = input()
print(test(a, b)) | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING BIN_OP NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | t = int(input())
for _ in range(t):
a = input()
b = input()
cnt = 0
a = a[::-1]
b = b[::-1]
if b == "0":
print("0\n")
elif a == "0":
print("1\n")
else:
while len(a) < len(b):
a = a + "0"
while len(b) < len(a):
b = b + "0"
a = a[::-1]
b = b[::-1]
cnt, curr, maxi = 1, -1, 0
for j in reversed(range(len(a))):
if a[j] == b[j] == "1":
curr = j
cnt = 1
maxi = max(maxi, cnt)
elif curr > -1 and (
a[j] == "0" and b[j] == "1" or a[j] == "1" and b[j] == "0"
):
cnt += 1
maxi = max(maxi, cnt)
elif a[j] == b[j] == "0":
curr = -1
print(maxi + 1, "\n") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR STRING IF VAR STRING EXPR FUNC_CALL VAR STRING WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR STRING WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | def add(A, B):
cnt = 0
while B > 0:
cnt += 1
U = A ^ B
B = (A & B) << 1
A = U
print(cnt)
def newfun(A, B):
if len(A) > len(B):
diff = len(A) - len(B)
B = "0" * diff + B
elif len(B) > len(A):
diff = len(B) - len(A)
A = "0" * diff + A
l = len(A)
A = A[::-1] + "0"
B = B[::-1] + "0"
ans = 0
for i in range(l):
temp = 1
if A[i] == "1" and B[i] == "1":
indx = 1
while A[i + indx] != B[i + indx]:
indx += 1
temp += indx - 1
if temp > ans:
ans = temp
print(ans + 1)
for t in range(int(input())):
A = input()
B = input()
if B == "0":
print("0")
elif len(B) < 501 or len(A) < 501:
A = int(A, 2)
B = int(B, 2)
add(A, B)
else:
newfun(A, B) | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | t = int(input())
ans = []
for _ in range(t):
temp1 = input()
temp2 = input()
if len(temp1) <= 50000 and len(temp2) <= 50000:
count = 0
a = int(temp1, 2)
b = int(temp2, 2)
while b > 0:
u = a ^ b
v = a & b
a = u
b = v << 1
count = count + 1
ans.append(count)
else:
maximum = [0]
c = list(temp1)
d = list(temp2)
c = c[::-1]
d = d[::-1]
if len(c) - len(d) > 0:
temp = len(c) - len(d)
for k in range(temp):
d.append("0")
if len(d) - len(c) > 0:
temp = len(d) - len(c)
for k in range(temp):
c.append("0")
flag = 0
i = 0
z = len(c)
while i < z:
count = 0
if int(c[i]) == 1 and int(d[i]) == 1 and i < z - 1:
flag = 1
i = i + 1
if c[i] == d[i]:
continue
while 1:
if c[i] != d[i]:
i = i + 1
count = count + 1
if i >= z:
break
else:
break
maximum.append(count)
i = i + 1
if flag == 0:
ans.append(1)
else:
ans.append(max(maximum) + 2)
for i in ans:
print(i) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR WHILE NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | t = int(input())
while t != 0:
A = input()
B = input()
C = int(A, 2)
D = int(B, 2)
x = C & D
y = C ^ D
p = len(A)
q = len(B)
maxi = 0
if p > q:
maxi = p
else:
maxi = q
x = bin(x)[2:].zfill(maxi)
y = bin(y)[2:].zfill(maxi)
maxi = 0
max_till = 0
if D == 0:
print("0")
else:
for i in range(len(y)):
if y[i] == "1" and x[i] == "0":
maxi = maxi + 1
continue
elif y[i] == "0" and x[i] == "0":
maxi = 0
elif y[i] == "0" and x[i] == "1":
max_till = max(max_till, maxi)
maxi = 0
if max_till == 0 and x.count("1") > 0:
print("2")
elif max_till == 0 and x.count("1") == 0:
print("1")
else:
print(max_till + 2)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | def binaryToDecimal(binary):
binary1 = binary
decimal, i, n = 0, 0, 0
while binary != 0:
dec = binary % 10
decimal = decimal + dec * pow(2, i)
binary = binary // 10
i += 1
return decimal
def add(a, b):
count = 0
u = 0
v = 0
while b > 0:
u = a ^ b
v = a & b
a = u
b = v * 2
count = count + 1
return count
t = int(input())
while t != 0:
a = int(input())
b = int(input())
a = int(binaryToDecimal(a))
b = int(binaryToDecimal(b))
print(add(a, b))
t = t - 1 | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | t = int(input())
while t > 0:
t -= 1
a = input().strip()
b = input().strip()
if len(b) == 1 and b[0] == "0":
print(0)
continue
if len(a) > len(b):
b = "0" * (len(a) - len(b)) + b
else:
a = "0" * (len(b) - len(a)) + a
max_l = 0
for i in range(len(a)):
if b[i] == "1" and a[i] == "1":
tmp = i - 1
count = 1
while tmp >= 0:
if b[tmp] == "0" and a[tmp] == "1" or b[tmp] == "1" and a[tmp] == "0":
count += 1
else:
break
tmp -= 1
max_l = max(max_l, count + 1)
print(max(max_l, 1)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | def fn2(a, b):
print("-" * 30)
ct = 0
while b > 0:
ct += 1
lenMax = max(len(bin(a)[2:]), len(bin(b)[2:]))
lena = len(bin(a)[2:])
lenb = len(bin(b)[2:])
print(
"0" * (lenMax - lena) + bin(a)[2:],
"\n" + "0" * (lenMax - lenb) + bin(b)[2:],
"\n",
)
u = a ^ b
b = (a & b) << 1
a = u
lenMax = max(len(bin(a)[2:]), len(bin(b)[2:]))
lena = len(bin(a)[2:])
lenb = len(bin(b)[2:])
print(
"0" * (lenMax - lena) + bin(a)[2:],
"\n" + "0" * (lenMax - lenb) + bin(b)[2:],
"\n",
)
return ct
def go2():
for tt in range(int(input())):
a = int(input(), 2)
b = int(input(), 2)
x = fn3(a, b)
print(x)
def fn3(a, b):
if b == 0:
return 0
elif a == 0:
return 1
lenMax = max(len(bin(a)[2:]), len(bin(b)[2:]))
lena = len(bin(a)[2:])
lenb = len(bin(b)[2:])
a = "0" * (lenMax - lena) + bin(a)[2:]
b = "0" * (lenMax - lenb) + bin(b)[2:]
a = list(a)
b = list(b)
x = []
for i in range(lenMax):
x.append(int(a[i]) + int(b[i]))
maxc = 1
u = lenMax
while u > 0:
u -= 1
if x[u] == 2:
c = 2
while u > 0:
u -= 1
if x[u] == 1:
c += 1
else:
u += 1
break
if c > maxc:
maxc = c
return maxc
go2() | FUNC_DEF EXPR FUNC_CALL VAR BIN_OP STRING NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP STRING BIN_OP STRING BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP STRING BIN_OP STRING BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER STRING RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR EXPR FUNC_CALL VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | for i in range(int(input())):
C = "0"
A = input()
B = input()
if len(A) < len(B):
B = C + B
C *= len(B) - len(A)
A = C + A
elif len(B) < len(A):
A = C + A
C *= len(A) - len(B)
B = C + B
else:
A = C + A
B = C + B
a = 0
dat = []
b = 0
if "1" not in B:
print(0)
elif "1" not in A:
print(1)
else:
for i in range(1, len(A) + 1):
if A[-i] != B[-i] and a != 0:
b += 1
elif A[-i] == B[-i] and a != 0:
if A[-i] == "0":
a = 0
else:
a = 1
dat.append(b + 1)
b = 1
elif A[-i] == B[-i] and A[-i] == "1" and a == 0:
a = 1
b = 1
if len(dat) > 0:
print(max(dat))
else:
print(1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | for t in range(int(input())):
a, b = input(), input()
if int(b, 2) == 0:
print(0)
continue
l = max(len(a), len(b)) + 1
a, b = a.rjust(l, "0"), b.rjust(l, "0")
p, ans, c = "0", 0, 0
for i in range(1, l + 1):
if p == "1":
c += 1
ans = max(ans, c)
if a[-i] == b[-i]:
c = 0
else:
c = 0
p = "1" if (a[-i] + b[-i] + p).count("1") > 1 else "0"
print(ans + 1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING ASSIGN VAR VAR VAR STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL BIN_OP BIN_OP VAR VAR VAR VAR VAR STRING NUMBER STRING STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | def add(A, B):
cnt = 0
while B > 0:
cnt += 1
U = A ^ B
V = A & B
A = U
B = V * 2
return cnt
for _ in range(int(input().strip())):
print(add(int(input().strip(), 2), int(input().strip(), 2))) | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | tc = int(input())
for i in range(tc):
a = input()
b = input()
l = 0
l1 = len(a)
l2 = len(b)
if l1 > l2:
l = l1
else:
l = l2
a = a.zfill(l)
b = b.zfill(l)
temp = int(b, 2)
if temp == 0:
print(0)
else:
maxc = 0
count = 0
for i in range(l):
if a[i] == "1" and b[i] == "1":
count += 1
maxc = max(count + 1, maxc)
count = 0
elif a[i] == "1" or b[i] == "1":
count += 1
else:
count = 0
if maxc == 0:
print(1)
else:
print(maxc) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | for _ in range(int(input())):
A, B, count, res, flag = str(input()), str(input()), 0, 1, 0
if int(B) == 0:
print(0)
else:
A, B = A.zfill(max(len(A), len(B))), B.zfill(max(len(A), len(B)))
for i, j in zip(A[::-1], B[::-1]):
if i == j:
if i == "1":
flag, count = 1, 1
elif i == "0" and j == "0":
count, flag = 0, 0
elif flag == 1:
count += 1
res = max(res, count + 1)
print(res) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR IF VAR STRING ASSIGN VAR VAR NUMBER NUMBER IF VAR STRING VAR STRING ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | TESTCASES = int(input())
for i in range(0, TESTCASES):
A = input().strip()[::-1]
B = input().strip()[::-1]
if "1" not in B:
print("0")
continue
while len(A) < len(B):
A += "0"
while len(B) < len(A):
B += "0"
LL = 1
CC = False
JJ = 0
for i in range(0, len(A)):
ACC = A[i] == "1"
BCC = B[i] == "1"
if ACC and BCC:
CC = True
JJ = 2
LL = max(LL, JJ)
elif ACC ^ BCC and CC:
JJ += 1
LL = max(LL, JJ)
else:
CC = False
print(LL) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR STRING WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR STRING WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | turn = int(input())
for _ in range(turn):
flag = 0
c = 0
max = -1
a = input()
b = input()
l = len(a) if len(a) > len(b) else len(b)
a = a.zfill(l)
b = b.zfill(l)
if int(b, 2) == 0:
print(0)
continue
m = len(a) - len(b)
for i in range(len(a), 0, -1):
if a[i - 1] == "1" and b[i - 1] == "1":
flag = 1
if max < c:
max = c
c = 0
elif (
a[i - 1] == "1" and b[i - 1] == "0" or a[i - 1] == "0" and b[i - 1] == "1"
) and flag != 0:
c += 1
if max < c:
max = c
elif a[i - 1] == "0" and b[i - 1] == "0":
flag = 0
if max == -1:
print(1)
else:
print(max + 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | def main(debug=False):
for _ in range(int(input())):
a = input().strip()
b = input().strip()
a = a[::-1]
b = b[::-1]
while len(a) < len(b):
a += "0"
while len(b) < len(a):
b += "0"
if debug:
print(a, b)
carry, ans, cur = 0, 0, 0
for i in range(len(a)):
carry = carry + int(a[i]) + int(b[i])
if debug:
print("For i=", i, "a[i] b[i]", a[i], b[i])
if carry == 2:
cur += 1
else:
cur = carry // 2
if debug:
print("carry", carry)
print("Curr", cur)
carry = int(carry / 2)
ans = max(ans, cur + 1)
if debug:
print("Ans", ans)
if b.count("1") > 0:
print(ans)
else:
print(0)
while True:
try:
main(debug=False)
except (ValueError, EOFError) as e:
break | FUNC_DEF NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR STRING WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR STRING IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING VAR STRING VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING VAR IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER WHILE NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | for _ in range(int(input())):
a = "0b" + input()
b = "0b" + input()
a = int(a, 2)
b = int(b, 2)
cnt = 0
while b:
U = a ^ b
V = a & b
a = U
b = V * 2
cnt += 1
print(cnt) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | from sys import stdin, stdout
for i in range(int(stdin.readline())):
stra = int(stdin.readline(), 2)
strb = int(stdin.readline(), 2)
count = 0
while strb != 0:
stra, strb = [stra ^ strb, (stra & strb) * 2]
count += 1
print(count) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR LIST BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | t = int(input())
for i in range(t):
A = input()
B = input()
arr = []
lenA = len(A)
lenB = len(B)
if lenA < lenB:
A = (lenB - lenA) * "0" + A
else:
B = (lenA - lenB) * "0" + B
for j in range(max(lenA, lenB)):
arr.append(int(A[j]) + int(B[j]))
j = len(arr) - 1
count_max = 0
count = 0
while j >= 0:
if arr[j] == 2:
if count > count_max:
count_max = count
count = 1
elif arr[j] == 1 and count != 0:
count += 1
elif arr[j] == 0:
if count > count_max:
count_max = count
count = 0
j -= 1
if count > count_max:
count_max = count
if int(B) == 0:
print(0)
else:
print(count_max + 1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR STRING VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | t = int(input())
while t > 0:
t -= 1
a = input().strip()
b = input().strip()
a = a[::-1]
b = b[::-1]
if b == "0":
value = 0
else:
while len(a) < len(b):
a += "0"
while len(b) < len(a):
b += "0"
carry = 0
cnt = 0
value = 0
for i in range(len(a)):
cmp = carry + int(a[i]) + int(b[i])
if cmp == 2:
cnt += 1
carry = 1
elif cmp == 3:
cnt = 1
carry = 1
else:
cnt = 0
carry = 0
if cnt > value:
value = cnt
value += 1
print(int(value)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR STRING WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | for _ in range(int(input())):
a = input()
b = input()
x = int(a, 2)
y = int(b, 2)
if y == 0:
print(0)
elif x == 0 and y != 0:
print(1)
else:
ans = [0]
count = 0
f = 0
if len(a) + len(b) > 100000:
if len(a) < len(b):
d = len(b) - len(a)
a = a.zfill(d + 1 + len(a))
b = b.zfill(1 + len(b))
for i in range(len(a) - 1, -1, -1):
if a[i] == "1" and b[i] == "1":
if f == 1:
ans.append(count)
count = 0
f = 0
else:
f = 1
if a[i] == "0" and b[i] == "0":
if f == 1:
ans.append(count)
count = 0
f = 0
if f == 1:
count += 1
if len(a) > len(b):
d = len(a) - len(b)
a = a.zfill(1 + len(a))
b = b.zfill(d + 1 + len(b))
for i in range(len(a) - 1, -1, -1):
if a[i] == "1" and b[i] == "1":
if f == 1:
ans.append(count)
count = 0
f = 0
else:
f = 1
if a[i] == "0" and b[i] == "0":
if f == 1:
ans.append(count)
count = 0
f = 0
if f == 1:
count += 1
if len(a) == len(b):
a = a.zfill(1 + len(a))
b = b.zfill(1 + len(b))
for i in range(len(a) - 1, -1, -1):
if a[i] == "1" and b[i] == "1":
if f == 1:
ans.append(count)
count = 0
f = 0
else:
f = 1
if a[i] == "0" and b[i] == "0":
if f == 1:
ans.append(count)
count = 0
f = 0
if f == 1:
count += 1
print(max(ans) + 1)
else:
c = 0
sum = x + y
while y > 0:
U = x ^ y
x = U
y = sum - x
c = c + 1
print(c) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | def addition(a, b, a1, b1):
c = 0
while b > 0:
u = a ^ b
v = a & b
a = u
b = v * 2
c += 1
if c > 100:
sol(a1, b1)
return
print(c)
def binaryToDecimal(n):
return int(n, 2)
def sol(a, b):
a = list(a)
b = list(b)
if len(a) < l:
for j in range(l - len(a)):
a = ["0"] + a
elif len(b) < l:
for j in range(l - len(b)):
b = ["0"] + b
a.reverse()
b.reverse()
pa = []
j = 0
pa.append(0)
while j < l:
if a[j] == "1" and b[j] == "1":
co = 1
j += 1
if j < l:
while a[j] != b[j]:
co += 1
j += 1
if j >= l:
break
pa.append(co)
else:
j += 1
print(max(pa) + 1)
t = int(input())
for i in range(t):
a = input()
b = input()
l = max(len(a), len(b))
if b == "0":
print(0)
elif a == "0":
print(1)
else:
a1 = binaryToDecimal(a)
b1 = binaryToDecimal(b)
addition(a1, b1, a, b) | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR IF FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | t = int(input())
for j in range(t):
a = input()
b = input()
n = len(a)
n1 = len(b)
A = int(a, 2)
B = int(b, 2)
c = 0
max = 0
if n - 1 > 500 or n1 - 1 > 500:
s = "0" * abs(n - n1)
if n > n1:
b = s + b
else:
a = s + a
a1 = a[::-1]
b1 = b[::-1]
flag = 0
for i in range(len(a1)):
if a1[i] == "0" and b1[i] == "0":
flag = 0
c = 0
elif a1[i] == "1" and b1[i] == "1":
flag = 1
c = 0
elif flag == 1:
c = c + 1
if c > max:
max = c
print(max + 2)
else:
while B > 0:
U = A ^ B
V = A & B
A = U
B = V * 2
c = c + 1
print(c) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | t = int(input())
for i in range(t):
a1 = input()
b1 = input()
val1 = 0
val2 = 0
u = 0
v = 0
def gen(m):
val1 = int(a1, 2)
val2 = int(b1, 2)
c = 0
while m > 0:
c += 1
u = val1 ^ m
v = val1 & m
val1 = u
m = 2 * v
yield c
val2 = int(b1, 2)
g = gen(val2)
for i in g:
print(i) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER VAR EXPR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | def ones(n):
c = 0
while n:
n = n & n - 1
c += 1
if c == 1:
return True
return False
def cc(a, b):
a = bin(a)[2:]
b = bin(b)[2:]
sa = len(a)
sb = len(b)
ind = -1
qq = abs(sa - sb)
y = 0
if qq == 0:
ind = 0
qq += 1
y += 1
if sa - sb > 0:
for i in range(qq, sa):
if a[i] == "1" and b[y] == "1":
ind = i
break
y += 1
else:
for i in range(qq, sb):
if b[i] == "1" and a[y] == "1":
ind = i
break
y += 1
return ind
for _ in range(int(input())):
a = int(input(), 2)
b = int(input(), 2)
if b == 0:
print("0")
continue
if a == 0:
print("1")
continue
c = 0
f = 0
while True:
c += 1
u = a ^ b
v = a & b
if v == 0:
break
a = u
b = v << 1
if ones(b):
f = 1
break
if f == 1:
k = 0
z = a | b
z = bin(z)[2:]
index = cc(a, b)
for i in range(index, -1, -1):
if z[i] == "1":
k += 1
else:
break
c += max(k + 1, 1)
print(c) | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | def make_eqlength(A, B):
if len(A) < len(B):
while len(A) < len(B):
A = "0" + A
else:
while len(B) < len(A):
B = "0" + B
return A, B
t = int(input())
while t > 0:
A = input()
B = input()
count = 0
res = 1
if int(B) == 0:
print(0)
t -= 1
continue
else:
if len(A) != len(B):
A, B = make_eqlength(A, B)
flag = 0
for i, j in zip(A[::-1], B[::-1]):
if i == j:
if i == "1":
flag = 1
count = 1
elif i == "0" and j == "0":
count = 0
flag = 0
elif flag == 1:
count += 1
c = count + 1
res = max(res, c)
print(res)
t -= 1 | FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR IF VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | t = input()
for _ in range(int(t)):
a = input()
b = input()
carry = 0
car_range = 0
car_start = -1
if int(b) == 0:
print(0)
continue
c = a if len(a) > len(b) else b
o = b if len(a) > len(b) else a
c = c[::-1]
o = o[::-1]
for i, x in enumerate(c):
y = 0
if len(o) > i:
y = o[i]
if int(x) + int(y) == 1 and carry == 1:
carry = 1
elif int(x) + int(y) == 2 and carry == 0:
carry = 1
elif int(x) + int(y) == 2 and carry == 1:
car_range = max(car_range, int(i) - int(car_start) + 1)
car_start = i
carry = 1
else:
carry = 0
if carry > 0:
if car_start == -1:
car_start = i
elif car_start != -1:
car_range = max(car_range, int(i) - int(car_start) + 1)
car_start = -1
if carry > 0:
car_range = max(car_range, int(len(c) - 1 + carry) - int(car_start) + 1)
if car_range == 0:
car_range = 1
print(car_range) | ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | T = int(input())
while T:
A = input()
B = input()
if B == "0":
print(0)
else:
if len(A) < len(B):
add = ""
for i in range(len(B) - len(A)):
add += "0"
A = add + A
elif len(B) < len(A):
add = ""
for i in range(len(A) - len(B)):
add += "0"
B = add + B
maxval = 0
curval = 0
for i in range(len(A) - 1, -1, -1):
if A[i] == "0" and B[i] == "0":
curval = 0
elif A[i] == "1" and B[i] == "1":
curval = 1
elif curval > 0:
curval += 1
maxval = max(curval, maxval)
print(maxval + 1)
T -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | T = int(input())
for i in range(T):
a = input()
b = input()
al = len(a)
bl = len(b)
if al > 450 and bl > 450:
x = max(al, bl)
if al > bl:
for i in range(al - bl):
b = "0" + b[:]
elif al < bl:
for i in range(bl - al):
a = "0" + a[:]
else:
pass
res = 1
req = 1
for j in range(x - 1, -1, -1):
t = 1
if b[j] == "1":
req = 0
if a[j] == "1" and b[j] == "1":
t = t + 1
for k in range(j - 1, -1, -1):
if a[k] == "0" and b[k] == "1" or a[k] == "1" and b[k] == "0":
t = t + 1
else:
break
j = k + 1
if t > res:
res = t
if req == 1:
print("0")
else:
print(res)
else:
A = int(a, 2)
B = int(b, 2)
count = 0
if B == 0:
print("0")
elif A == 0:
print("1")
else:
while B > 0:
count = count + 1
temp = A & B
A = A ^ B
B = temp * 2
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP STRING VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | def add(a, b):
count = 0
u = 0
v = 0
while b > 0:
u = a ^ b
v = a & b
a = u
b = v * 2
count = count + 1
return count
def bruteforce(a, b):
anum = int(a, 2)
bnum = int(b, 2)
print(add(anum, bnum))
t = int(input())
while t > 0:
t = t - 1
a = input()
b = input()
if int(b, 2) == 0:
print("0")
continue
if int(a, 2) == 0:
print("1")
continue
ashort = True
if len(a) < len(b):
ashort = True
else:
ashort = False
if ashort == True:
a = "0" * abs(len(a) - len(b)) + a
else:
b = "0" * abs(len(a) - len(b)) + b
if len(a) < 90000:
bruteforce(a, b)
continue
oneone = False
maxi = 0
ans = 0
for i in range(len(a) - 1, -1, -1):
if a[i] == "1" and b[i] == "1" or a[i] == "0" and b[i] == "0":
oneone = True
ans = max(ans, maxi)
maxi = 0
if oneone == True:
if a[i] == "0" and b[i] == "1" or a[i] == "1" and b[i] == "0":
maxi = maxi + 1
print(ans + 2) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | for r in range(int(input())):
x = input()
y = input()
m = 0
na = len(x)
nb = len(y)
if na - 1 > 500 or nb - 1 > 500:
if na > nb:
y = (na - nb) * "0" + y
elif nb > na:
x = (nb - na) * "0" + x
f = 0
s = 0
for i in range(len(x) - 1, -1, -1):
if f == 0:
if x[i] == "1" and y[i] == "1":
s = 0
elif x[i] == "0" and y[i] == "0":
s = 0
f = 1
else:
s += 1
if s > m:
m = s
elif x[i] == "1" and y[i] == "1" and f == 1:
s = 0
f = 0
else:
s = 0
print(m + 2)
else:
x = int(x, 2)
y = int(y, 2)
s = 0
while y > 0:
u = x ^ y
v = x & y
x = u
y = v * 2
s += 1
print(s) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR STRING VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | def loji_hogya(a, b):
count = 0
while b > 0:
u = a ^ b
v = a & b
a = u
b = v * 2
count = count + 1
return count
n = int(input())
while n != 0:
a = input()
b = input()
a = int(a, 2)
b = int(b, 2)
print(loji_hogya(a, b))
n = n - 1 | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR 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 ASSIGN VAR BIN_OP VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | t = int(input())
for i in range(t):
a = list(input())
b = list(input())
c = []
x, y = len(a), len(b)
if x > y:
c[: x - y + 1] = ["0"] * (x - y)
c[x - y + 1 :] = b[:]
b = c
z = x
else:
c[: y - x + 1] = ["0"] * (y - x)
c[y - x + 1 :] = a[:]
a = c
z = y
k = 1
r = 0
if b == ["0"] * z:
print(0)
else:
f = 0
for j in range(z - 1, -1, -1):
if a[j] == b[j] == "1" and f == 0:
f = 1
k += 1
elif (
a[j] == "1"
and b[j] == "0"
and f == 1
or a[j] == "0"
and b[j] == "1"
and f == 1
):
k += 1
else:
if k > r:
r = k
k = 1
f = 0
if a[j] == b[j] == "1":
f = 1
k += 1
if k > r:
r = k
print(r) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP LIST STRING BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP LIST STRING BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP LIST STRING VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER VAR VAR STRING VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | for _ in range(0, int(input())):
a = input()
b = input()
def f(a, b):
y = 0
while b > 0:
u = a ^ b
v = a & b
a = u
b = v * 2
y += 1
return y
print(f(int(a, 2), int(b, 2))) | FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | for _ in range(int(input())):
a = input()
b = input()
m = len(a)
n = len(b)
if m > n:
for i in range(m - n):
b = "0" + b
elif n > m:
for i in range(n - m):
a = "0" + a
m = n
l = [(-1) for i in range(m)]
for i in range(m - 1, -1, -1):
if a[i] == "1" and b[i] == "1":
l[i] = 0
j = -1
for i in range(m - 1, -1, -1):
if a[i] == "1" and b[i] == "1":
if j != -1:
l[j] = l[j] + 1
j = i
elif a[i] == "0":
if b[i] == "0":
if j != -1:
l[j] = l[j] + 1
j = -1
elif j != -1:
l[j] = l[j] + 1
elif b[i] == "0":
if j != -1:
l[j] = l[j] + 1
if a[0] == "1" and b[0] == "1":
l[j] = l[j] + 1
elif j != -1:
l[j] = l[j] + 1
if int(b) == 0:
print(0)
elif max(l) == -1:
print(1)
else:
print(max(l) + 1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP STRING VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR STRING IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | for i in range(0, int(input())):
m = input()
n = input()
if n == "0":
print(0)
elif n != "0":
if len(m) > len(n):
diff = len(m) - len(n)
for j in range(diff):
n = "0" + n
elif len(n) > len(m):
diff = len(n) - len(m)
for j in range(diff):
m = "0" + m
m = "0" + m
n = "0" + n
lenm = len(m)
L = []
L.append(0)
m = m[::-1]
n = n[::-1]
for k in range(lenm - 1):
A = 1
if m[k] == "1" and n[k] == "1":
chk = 1
brk = 1
while brk == 1:
if m[chk + k] == "1" and n[chk + k] == "0":
A += 1
elif m[chk + k] == "0" and n[chk + k] == "1":
A += 1
else:
brk = 0
chk += 1
L.append(A)
print(max(L) + 1) | FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR BIN_OP VAR VAR STRING VAR BIN_OP VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR VAR STRING VAR BIN_OP VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | t = int(input())
for i in range(t):
a1 = input()
b1 = input()
val1 = 0
val2 = 0
c = 0
u = 0
v = 0
val1 = int(a1, 2)
val2 = int(b1, 2)
while val2 > 0:
c += 1
u = val1 ^ val2
v = val1 & val2
val1 = u
val2 = 2 * v
print(c) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer β the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | t = int(input())
for _ in range(t):
a = input()
b = input()
al = []
suma = 0
bl = []
sumb = 0
for i in range(len(a)):
c = int(a[len(a) - i - 1])
al.append(c)
suma += c
for i in range(len(b)):
c = int(b[len(b) - i - 1])
bl.append(c)
sumb += c
if sumb == 0:
steps = 0
elif suma == 0:
steps = 1
else:
max = 1
if len(al) > len(bl):
for i in range(len(al) - len(bl)):
bl.append(0)
else:
for i in range(len(bl) - len(al)):
al.append(0)
for i in range(len(al)):
if al[i] == 1 and bl[i] == 1:
count = 2
i += 1
while i < len(al):
if al[i] ^ bl[i] == 1:
count += 1
i += 1
else:
break
if count > max:
max = count
steps = max
print(str(steps)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.