description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
def main(): s = input() l = "" ns = "" nb1 = 2 inc = 0 for x in range(len(s)): if s[x] != l: inc = 0 if inc == 0: nb1 += 1 ns += s[x] elif inc == 1 and nb1 >= 2: nb1 = 0 ns += s[x] inc += 1 l = s[x] print(ns) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
MOD = 10**9 + 7 I = lambda: list(map(int, input().split())) s = input() res = "" d = [] n = len(s) if n < 3: print(s) else: res += s[:3] i = 3 j = 2 if res[j] == res[j - 1] and res[j - 1] == res[j - 2]: res = res[:j] j -= 1 while i < n: res += s[i] i += 1 j += 1 if j >= 2 and res[j] == res[j - 1] and res[j - 1] == res[j - 2]: res = res[:j] j -= 1 if j >= 3 and res[j] == res[j - 1] and res[j - 2] == res[j - 3]: res = res[:j] j -= 1 print("".join(res))
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
s = list(input()) n = len(s) a = [1] * n prev = -1 cnt = 1 prev_cnt = 0 for i in range(n): if s[i] == prev: cnt += 1 else: prev_cnt = cnt cnt = 1 if prev_cnt < 2: if cnt >= 3: a[i] = 0 cnt -= 1 elif cnt >= 2: a[i] = 0 cnt -= 1 prev = s[i] for i in range(n): if a[i]: print(s[i], end="")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
s = list(input()) l = len(s) ln = [] if l < 2: ln = s else: ln = s[0:2] j = 2 for i in range(2, l): if s[i] != ln[j - 1] or s[i] != ln[j - 2]: if ln[j - 1] != ln[j - 2] or i == l - 1 or s[i] != s[i + 1]: ln.append(s[i]) j += 1 print("".join(ln))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
a = str(input()) l = list(a) l1 = [] l1.append(l[0]) if len(l) == 1: print(l[0]) else: l1.append(l[1]) for i in range(2, len(l)): if ( l1[-1] == l1[-2] == l[i] or len(l1) > 2 and l1[-2] == l1[-3] and l1[-1] == l[i] ): continue else: l1.append(l[i]) a = "".join(l1) print(a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
s = input() stack = [] i = 0 n = len(s) while i < n: if len(stack) < 2: stack.append(s[i]) i += 1 elif s[i] == stack[-1] == stack[-2]: i += 1 elif len(stack) >= 3 and s[i] == stack[-1] and stack[-2] == stack[-3]: i += 1 else: stack.append(s[i]) i += 1 print("".join(stack))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
def fun1(st): le = len(st) stack = [0] * le i = 0 x = 0 sh = 0 h = 0 ans = list(st) while i < len(ans): if ans[i] == "#": stack[i] = stack[i - 1] i += 1 continue if i > 0 and ans[i] == ans[i - 1]: stack[i] = stack[i - 1] + 1 else: stack[i] = 0 if i < len(ans) - 1 and ans[i] != ans[i + 1] or i == len(ans) - 1: p = 0 if i < le and stack[i] == 0: ans[i] = ans[i] elif i > 2 and stack[i] == 1 and stack[i - 2] == 1: ans[i] = "#" h = 1 sh = 0 i -= 2 elif stack[i] != 1: h = i x = i while stack[x - 1] != 0: ans[x] = "#" x -= 1 i = x - 3 i += 1 return "".join(c for c in ans if c != "#") st = input() print(fun1(st))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN FUNC_CALL STRING VAR VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
a = input() n = len(a) if n <= 2: print(a) else: res = "" a += "_" n = len(a) i = 0 mode = 0 while i < n - 1: c = 1 while i < n - 1 and a[i] == a[i + 1]: c += 1 i += 1 if c == 1: res += a[i] mode = 0 elif mode == 1: mode = 0 res += a[i] else: res += 2 * a[i] mode = 1 i += 1 print(res)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
from sys import stdin, stdout def main(): from sys import stdin, stdout ip = stdin.readline().strip() t = ip[0] for ch in ip[1:]: if len(t) > 2: if ch == t[-1] and t[-2] == t[-3] or ch == t[-1] and t[-1] == t[-2]: continue else: t += ch elif len(t) == 2: if ch == t[-1] and ch == t[-2]: continue else: t += ch else: t += ch stdout.write(t) main()
FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
s, t = input(), 1 k = list(s) for i in range(1, len(s)): t = t + 1 if s[i] == s[i - 1] else 1 if t > 2: k[i] = "|" k = "".join(k).replace("|", "") + "||" t, k = 0, list(k) for i in range(1, len(k)): if k[i] == k[i - 1] and t: t = 0 k[i] = "|" if k[i] == k[i - 1]: t = 1 elif k[i] != k[i + 1]: t = 0 k = "".join(k).replace("|", "") print(k)
ASSIGN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP FUNC_CALL FUNC_CALL STRING VAR STRING STRING STRING ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL STRING VAR STRING STRING EXPR FUNC_CALL VAR VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
s = input() t = s[0] j = 0 b = [] for i in range(0, len(s)): if s[i] == t and j == 0: j = 1 b.append(s[i]) elif s[i] == t and j == 1: j = 2 b.append(s[i]) elif s[i] != t and j == 1: b.append(s[i]) elif s[i] != t and j == 2: j = 3 b.append(s[i]) elif s[i] != t and j == 3: j = 1 b.append(s[i]) t = s[i] print(*b, sep="")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
s = input() ans = [] p_count = c_count = 0 p_char = c_char = "" d_used = False for c in s: if c == c_char: c_count += 1 if c_count <= 2: if c_count == 2 and p_count >= 2: continue if c_count == 2: d_used = True ans.append(c) else: if d_used: p_count = min(2, c_count) d_used = False else: p_count = 1 c_count = 1 c_char = c ans.append(c) print("".join(ans))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
l = input() ans = "" ct = [] i = 0 while i < len(l): t = l[i] c = 0 while t == l[i]: i = i + 1 c = c + 1 if i == len(l): break if c == 1: ans = ans + t ct.append(c) else: if c >= 3: c = 2 if len(ct) > 0: if c == 2 and ct[-1] == 2: c = 1 ct.append(c) ans = ans + t * c else: ct.append(c) ans = ans + t * c else: ans = ans + t * c ct.append(c) print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
s = list(input()) n = len(s) count = 0 i = 0 answer = [1] * n while i < n - 2: if s[i] != s[i + 1] or answer[i] != 1: i += 1 else: j = i + 2 while j < n and s[j] == s[i]: answer[j] = -1 j += 1 while j < n - 1 and s[j] == s[j + 1]: answer[j + 1] = -1 j += 1 i = j ans = "" for i in range(n): if answer[i] != -1: ans += s[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
s = input().strip() arr = [] i = 0 while i < len(s): j = i cc = 0 while j < len(s) and s[i] == s[j]: j += 1 cc += 1 arr.append([s[i], cc]) i = j pre = -1 ans = "" for i in range(len(arr)): if pre == -1: pre = arr[i][1] ans += arr[i][0] * min(2, arr[i][1]) elif pre > 1: ans += arr[i][0] pre = 1 else: ans += arr[i][0] * min(2, arr[i][1]) pre = arr[i][1] print(ans)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
s = input() n = len(s) if n == 1 or n == 2: print(s) else: ans = [s[0], s[1]] for i in range(2, n): k = len(ans) - 1 if s[i] != ans[k]: ans.append(s[i]) elif ans[k] != ans[k - 1]: if k >= 2: if ans[k - 1] != ans[k - 2]: ans.append(s[i]) else: ans.append(s[i]) print("".join(ans))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
s = input() i = 0 n = len(s) l = [] while i < n: cnt = s[i] while i + 1 < n and s[i] == s[i + 1]: cnt += s[i + 1] i += 1 l.append(cnt) i += 1 for i in range(len(l)): if len(l[i]) > 2: l[i] = l[i][:2] ans1, t1 = "", 0 for i in l: if len(i) == 2: if t1: ans1 += i[0] t1 = 0 else: ans1 += i t1 = 1 else: ans1 += i t1 = 0 print(ans1)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR VAR VAR WHILE BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR STRING NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
def fixing_typos(s): s = list(s) ans = [s[0]] i = 1 double = False triple = False while i < len(s): if s[i] == ans[-1]: if triple: i += 1 continue if double: i += 1 continue triple = True else: double = triple triple = False ans.append(s[i]) i += 1 return "".join(ans) s = input().strip() print(fixing_typos(s))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
s = input() s1 = "" for i in range(len(s)): if ( len(s1) > 1 and s[i] == s1[-1] and s1[-1] == s1[-2] or len(s1) > 2 and s[i] == s1[-1] and s1[-2] == s1[-3] ): continue else: s1 += s[i] print(s1)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
s = input() C = [0] * len(s) ind = -1 x = True for i in range(len(s)): if x: C[i] = 1 y = True x = False ind += 1 continue if y: if s[i] == s[ind]: C[i] = 1 z = True y = False ind += 1 continue else: C[i] = 1 ind += 1 continue if z: if s[i] == s[ind]: C[i] = 0 z = True continue else: C[i] = 1 z = False a = True ind2 = i continue if a: if s[i] == s[ind2]: C[i] = 0 continue else: ind = i C[i] = 1 a = False y = True for i in range(len(s)): if C[i]: print(s[i], end="")
ASSIGN VAR FUNC_CALL 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 IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
t = list(input().strip()) d = [] for i in range(len(t) - 1): if t[i] == t[i + 1]: d.append(i) for i in range(len(d) - 1): if d[i + 1] - d[i] == 1: t[d[i]] = "" a = "".join(t) if len(a) < 3: print(a) else: ans = a[:3] for i in range(3, len(a)): if not (a[i] == ans[-1] and ans[-2] == ans[-3]): ans += a[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR STRING ASSIGN VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
import sys t = 1 for __ in range(t): s1 = input() ans = [] ch = 4 for i in range(len(s1)): if i <= len(s1) - 3: if s1[i] == s1[i + 1] == s1[i + 2]: continue if i <= len(s1) - 2: if s1[i] == s1[i + 1]: if ch != -1: ans.append(s1[i]) ch = 4 else: continue ch = -2 else: ans.append(s1[i]) if ch == -2: ch += 1 else: ch = 4 ans.append(s1[-1]) print("".join(ans))
IMPORT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
s = list(input()) pre = s[0] ns = [] prec = 0 ns.append(pre) posc = 1 for i in s[1:]: if i == pre: if prec != 2 and posc < 2: ns.append(i) posc += 1 pre = i else: prec = posc posc = 1 ns.append(i) pre = i print("".join(ns))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
s = list(input()) n = len(s) cnt = 0 prev = s[0] ans = [] chk = False for x in range(n): if s[x] == prev: cnt += 1 else: if cnt == 1: ans.append(s[x - 1]) chk = False elif cnt >= 2 and chk: ans.append(s[x - 1]) chk = False elif cnt >= 2 and chk == False: chk = True ans.append(s[x - 1]) ans.append(s[x - 1]) cnt = 1 prev = s[x] x = n if cnt == 1: ans.append(s[x - 1]) chk = False elif cnt >= 2 and chk: ans.append(s[x - 1]) elif cnt >= 2 and chk == False: chk = True ans.append(s[x - 1]) ans.append(s[x - 1]) print(*ans, sep="")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
n = input() ar = [""] pair = 0 for i in n: if ar[-1] == i and pair == 0: ar.append(i) pair = 2 elif ar[-1] != i: ar.append(i) if pair > 0: pair -= 1 s = "" s += "".join(i for i in ar if i != "") print(s)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING VAR FUNC_CALL STRING VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
w = list(input()) a = ["@"] x = 0 y = 0 for s in w: if a[len(a) - 1] == s: if x < 2: if y <= 1: a.append(s) y += 1 elif y == 0: a.append(s) y += 1 else: x = y y = 0 a.append(s) y += 1 a.pop(0) print("".join(a))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
import sys input = lambda: sys.stdin.readline().strip("\r\n") s = list(input()) n = len(s) ans = [] for i in range(n): if ( len(ans) >= 2 and ans[-1] == ans[-2] == s[i] or len(ans) > 2 and ans[-3] == ans[-2] and ans[-1] == s[i] ): continue ans.append(s[i]) print("".join(ans))
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
s = input() ans = [] index_shift = 0 for i, si in enumerate(s): ans.append(si) if i >= 2: if s[i - 2] == s[i - 1] == si: ans.pop(i - index_shift) index_shift += 1 elif i >= 3: if ( ans[i - 3 - index_shift] == ans[i - 2 - index_shift] and ans[i - 1 - index_shift] == si ): ans.pop(i - index_shift) index_shift += 1 print("".join(ans))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
s = input() pre, ls, ans, last = [s[0], 1], len(s), [s[0]], 0 for i in range(1, ls): if s[i] == pre[0]: if last == 2 or pre[1] >= 2: continue else: ans.append(s[i]) pre[1] += 1 else: last = pre[1] pre = [s[i], 1] ans.append(s[i]) print("".join(ans))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR LIST VAR NUMBER NUMBER FUNC_CALL VAR VAR LIST VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
s = input() l = "" if len(s) <= 2: print(s) elif len(s) <= 3: if s[0] == s[1] == s[2]: print(s[:2]) else: print(s) else: for i in range(len(s)): if i < 2: l += s[i] elif i == 2 and s[2] == s[1] and s[1] != s[0]: l += s[i] elif l[-1] != s[i]: l += s[i] elif len(l) >= 3 and l[-1] == s[i] and l[-2] != l[-3] and l[-2] != s[i]: l += s[i] print(l)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
s = input() ans = "" l = 0 for i in s: tr = True if l > 1: if i == ans[l - 1] and ans[l - 1] == ans[l - 2]: tr = False if l > 2: if i == ans[l - 1] and ans[l - 2] == ans[l - 3]: tr = False if tr: ans += i l += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
s = input() last = s[0] final = [] i = 1 curr = 1 prev = 0 while i < len(s): if s[i] == last: curr += 1 elif curr >= 2: if prev == 2: final.append(s[i - 1]) prev = 1 curr = 1 last = s[i] else: final.append(s[i - 1]) final.append(s[i - 1]) prev = 2 curr = 1 last = s[i] else: final.append(s[i - 1]) prev = 1 curr = 1 last = s[i] i += 1 if curr >= 2: if prev == 2: final.append(s[i - 1]) prev = 1 curr = 1 else: final.append(s[i - 1]) final.append(s[i - 1]) prev = 2 curr = 1 else: final.append(s[i - 1]) prev = 1 curr = 1 print("".join(final))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
import sys s = sys.stdin.readline()[:-1] ans = "" if len(s) < 3: ans = s else: last = s[0] lastCount = 1 count = 1 i = 1 ans = last fixed = False while i < len(s): if s[i] == last: count = count + 1 if count == 2 and (lastCount < 2 or fixed): ans = ans + s[i] fixed = False elif count == 2: fixed = True else: lastCount = count count = 1 last = s[i] ans = ans + s[i] i = i + 1 print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
s = input() s1 = list(s[:2]) for i in range(2, len(s)): if not s[i] == s[i - 1] == s[i - 2]: s1.append(s[i]) ans = s1[:3] if len(ans) >= 3: for i in range(3, len(s1)): if not (s1[i] == s1[i - 1] and s1[i - 2] == s1[i - 3]): ans += s1[i] else: s1[i] = "!" for i in range(len(ans)): print(ans[i], end="")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
from sys import stdin input = stdin.readline def typo(s): ans = "" for i in s: try: if ans[-1] == ans[-2] == i: continue if ans[-1] == i and ans[-2] == ans[-3] and ans[-2] != i: continue except: pass ans += i return ans print(typo(input().strip()))
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR STRING FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR IF VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. -----Input----- The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters. -----Output----- Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them. -----Examples----- Input helloo Output hello Input woooooow Output woow -----Note----- The second valid answer to the test from the statement is "heloo".
s = input() def encode(s): enc = [] i = 0 n = len(s) curChar = s[i] count = 0 while i < n: if s[i] == curChar: count += 1 else: enc.append(curChar) enc.append(count) curChar = s[i] count = 1 i += 1 enc.append(curChar) enc.append(count) return enc def decode(s): a = "" for i in range(0, len(s), 2): p = s[i] * s[i + 1] a += p return a e = encode(s) i = 0 n = len(e) while i < n - 1: if e[i + 1] > 2: e[i + 1] = 2 i += 2 i = 2 while i < n: if e[i + 1] > 1 and e[i - 1] > 1: e[i + 1] = 1 i += 2 else: i += 2 ans = decode(e) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n × m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 × 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≤ t ≤ 5000) — the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≤ n, m ≤ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≤ k ≤ nm) — the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, x_2, x_3 ≤ n, 1 ≤ y_1, y_2, y_3 ≤ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 × 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
import sys sys.setrecursionlimit(10**5) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.buffer.readline()) def MI(): return map(int, sys.stdin.buffer.readline().split()) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def BI(): return sys.stdin.buffer.readline().rstrip() def SI(): return sys.stdin.buffer.readline().rstrip().decode() def flip(i, j): res = [] if j == w - 1: j -= 1 cnt = 3 if aa[i][j]: aa[i][j] ^= 1 cnt -= 1 res += [i + 1, j + 1] if aa[i][j + 1]: aa[i][j + 1] ^= 1 cnt -= 1 res += [i + 1, j + 2] if cnt == 2 or aa[i + 1][j]: aa[i + 1][j] ^= 1 cnt -= 1 res += [i + 2, j + 1] if cnt: aa[i + 1][j + 1] ^= 1 cnt -= 1 res += [i + 2, j + 2] return res def right(i, j): res = [] cnt = 3 if aa[i][j]: aa[i][j] ^= 1 cnt -= 1 res += [i + 1, j + 1] if aa[i + 1][j]: aa[i + 1][j] ^= 1 cnt -= 1 res += [i + 2, j + 1] if cnt == 2 or aa[i][j + 1]: aa[i][j + 1] ^= 1 cnt -= 1 res += [i + 1, j + 2] if cnt: aa[i + 1][j + 1] ^= 1 res += [i + 2, j + 2] return res def zero(sj): ij1 = [] ij0 = [] res = [] for i in range(h - 2, h): for j in range(sj, sj + 2): if aa[i][j]: ij1.append((i, j)) else: ij0.append((i, j)) if len(ij1) == 3: for i, j in ij1: aa[i][j] ^= 1 res += [i + 1, j + 1] elif len(ij1) == 2: i, j = ij1[0] aa[i][j] ^= 1 res += [i + 1, j + 1] for i, j in ij0: aa[i][j] ^= 1 res += [i + 1, j + 1] else: i, j = ij1[0] aa[i][j] ^= 1 res += [i + 1, j + 1] for i, j in ij0[:2]: aa[i][j] ^= 1 res += [i + 1, j + 1] return res for _ in range(II()): h, w = MI() aa = [[int(c) for c in SI()] for _ in range(h)] ans = [] for i in range(h - 2): for j in range(w): if aa[i][j]: ans.append(flip(i, j)) for j in range(w - 1): if aa[h - 2][j] + aa[h - 1][j]: ans.append(right(h - 2, j)) while aa[h - 2][w - 2] + aa[h - 2][w - 1] + aa[h - 1][w - 2] + aa[h - 1][w - 1]: ans.append(zero(w - 2)) print(len(ans)) for row in ans: print(*row)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR VAR VAR NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR VAR VAR NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR WHILE BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n × m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 × 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≤ t ≤ 5000) — the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≤ n, m ≤ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≤ k ≤ nm) — the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, x_2, x_3 ≤ n, 1 ≤ y_1, y_2, y_3 ≤ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 × 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
from sys import stdin, stdout def solve(x, y): c = 0 for i in range(x, x + 2): for j in range(y, y + 2): if s[i][j] == "1": c += 1 if c == 0: return 1 if c == 4 or c == 3: aux = [] z = 0 for i in range(x, x + 2): for j in range(y, y + 2): if s[i][j] == "1" and z <= 2: aux.append(i + 1) aux.append(j + 1) s[i][j] = "0" z += 1 ans.append(aux.copy()) return 0 if c == 1 or c == 2: y1 = 0 z = 0 aux = [] for i in range(x, x + 2): for j in range(y, y + 2): if s[i][j] == "1" and y1 == 0: aux.append(i + 1) aux.append(j + 1) s[i][j] = "0" y1 += 1 continue if s[i][j] == "0" and z <= 1: aux.append(i + 1) aux.append(j + 1) s[i][j] = "1" z += 1 continue ans.append(aux.copy()) return 0 def clean1(x, y): if x + 1 == n: return 0 c = 0 aux = [] if s[x][y + 1] == "1": aux.append(x + 1) aux.append(y + 2) s[x][y + 1] = "0" c += 1 if s[x + 1][y + 1] == "1": aux.append(x + 2) aux.append(y + 2) s[x + 1][y + 1] = "0" c += 1 if c == 0: return if c <= 2: aux.append(x + 2) aux.append(y + 1) if s[x + 1][y] == "1": s[x + 1][y] = "0" else: s[x + 1][y] = "1" if c <= 1: aux.append(x + 1) aux.append(y + 1) if s[x][y] == "1": s[x][y] = "0" else: s[x][y] = "1" ans.append(aux.copy()) def clean2(x, y): if y + 1 == m: return 0 c = 0 aux = [] if s[x + 1][y] == "1": aux.append(x + 2) aux.append(y + 1) s[x + 1][y] = "0" c += 1 if s[x + 1][y + 1] == "1": aux.append(x + 2) aux.append(y + 2) s[x + 1][y + 1] = "0" c += 1 if c == 0: return if c <= 2: aux.append(x + 1) aux.append(y + 1) if s[x][y] == "1": s[x][y] = "0" else: s[x][y] = "1" if c <= 1: aux.append(x + 1) aux.append(y + 2) if s[x][y + 1] == "1": s[x][y + 1] = "0" else: s[x][y + 1] = "1" ans.append(aux.copy()) T = int(stdin.readline().strip()) for caso in range(T): n, m = map(int, stdin.readline().strip().split()) s = [list(stdin.readline().strip()) for i in range(n)] ans = [] x = n - 2 y = m - 2 if n % 2 != 0 and m % 2 != 0: q = solve(x, y) while q != 1: q = solve(x, y) x = n - 2 y = m - 2 if m % 2 != 0: for i in range(0, n, 2): clean1(i, m - 2) if n % 2 != 0: for i in range(0, m, 2): clean2(n - 2, i) for i in range(0, n - 1, 2): for j in range(0, m - 1, 2): x = solve(i, j) while x != 1: x = solve(i, j) stdout.write("%d\n" % len(ans)) for i in ans: stdout.write("%d %d %d %d %d %d\n" % (i[0], i[1], i[2], i[3], i[4], i[5]))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN NUMBER FUNC_DEF IF BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER RETURN IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR BIN_OP VAR NUMBER VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER RETURN IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER
This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n × m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 × 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≤ t ≤ 5000) — the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≤ n, m ≤ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≤ k ≤ nm) — the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, x_2, x_3 ≤ n, 1 ≤ y_1, y_2, y_3 ≤ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 × 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
import sys input = sys.stdin.readline t = int(input()) for tests in range(t): n, m = map(int, input().split()) A = [list(map(int, list(input().strip()))) for i in range(n)] ANS = [] for i in range(n - 2): for j in range(m - 1): if A[i][j] == 1 and A[i][j + 1] == 1: ANS.append((i + 1, j + 1, i + 1, j + 2, i + 2, j + 2)) A[i][j] ^= 1 A[i][j + 1] ^= 1 A[i + 1][j + 1] ^= 1 elif A[i][j] == 1 and A[i][j + 1] == 0: ANS.append((i + 1, j + 1, i + 2, j + 1, i + 2, j + 2)) A[i][j] ^= 1 A[i + 1][j] ^= 1 A[i + 1][j + 1] ^= 1 elif j == m - 2 and A[i][j] == 0 and A[i][j + 1] == 1: ANS.append((i + 1, j + 2, i + 2, j + 1, i + 2, j + 2)) A[i][j + 1] ^= 1 A[i + 1][j] ^= 1 A[i + 1][j + 1] ^= 1 for j in range(m - 2): if A[n - 2][j] == 1 and A[n - 1][j] == 1: ANS.append((n - 1, j + 1, n, j + 1, n - 1, j + 2)) A[n - 2][j] ^= 1 A[n - 1][j] ^= 1 A[n - 2][j + 1] ^= 1 elif A[n - 2][j] == 1 and A[n - 1][j] == 0: ANS.append((n - 1, j + 1, n - 1, j + 2, n, j + 2)) A[n - 2][j] ^= 1 A[n - 2][j + 1] ^= 1 A[n - 1][j + 1] ^= 1 elif A[n - 2][j] == 0 and A[n - 1][j] == 1: ANS.append((n, j + 1, n - 1, j + 2, n, j + 2)) A[n - 1][j] ^= 1 A[n - 2][j + 1] ^= 1 A[n - 1][j + 1] ^= 1 count = 0 C = [] if A[n - 2][m - 2] == 1: count += 1 C.append(n - 1) C.append(m - 1) if A[n - 2][m - 1] == 1: count += 1 C.append(n - 1) C.append(m) if A[n - 1][m - 2] == 1: count += 1 C.append(n) C.append(m - 1) if A[n - 1][m - 1] == 1: count += 1 C.append(n) C.append(m) if count == 3: ANS.append(tuple(C)) elif count == 0: True else: ANSX = [] if count == 2: for x, y in [ (n - 2, m - 2), (n - 2, m - 1), (n - 1, m - 2), (n - 1, m - 1), ]: if A[x][y] == 0: C.append(x + 1) C.append(y + 1) ANSX = tuple(C) break elif count == 4: ANSX = n - 1, m - 1, n - 1, m, n, m if ANSX != []: ANS.append(ANSX) a, b, c, d, e, f = ANSX A[a - 1][b - 1] ^= 1 A[c - 1][d - 1] ^= 1 A[e - 1][f - 1] ^= 1 if A[n - 2][m - 2] == 1: ANS.append((n - 1, m - 1, n - 1, m, n, m - 1)) ANS.append((n - 1, m - 1, n - 1, m, n, m)) ANS.append((n - 1, m - 1, n, m - 1, n, m)) elif A[n - 1][m - 1] == 1: ANS.append((n, m, n - 1, m, n, m - 1)) ANS.append((n, m, n - 1, m, n - 1, m - 1)) ANS.append((n, m, n, m - 1, n - 1, m - 1)) elif A[n - 2][m - 1] == 1: ANS.append((n - 1, m, n, m, n - 1, m - 1)) ANS.append((n - 1, m, n, m, n, m - 1)) ANS.append((n - 1, m, n - 1, m - 1, n, m - 1)) elif A[n - 1][m - 2] == 1: ANS.append((n, m - 1, n - 1, m - 1, n, m)) ANS.append((n, m - 1, n - 1, m - 1, n - 1, m)) ANS.append((n, m - 1, n, m, n - 1, m)) print(len(ANS)) for a, b, c, d, e, f in ANS: print(a, b, c, d, e, f)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR NUMBER ASSIGN VAR LIST IF VAR NUMBER FOR VAR VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR
This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n × m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 × 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≤ t ≤ 5000) — the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≤ n, m ≤ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≤ k ≤ nm) — the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, x_2, x_3 ≤ n, 1 ≤ y_1, y_2, y_3 ≤ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 × 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
def flip(x, y): global mat if mat[x][y]: mat[x][y] = 0 else: mat[x][y] = 1 def func(x1, y1, x2, y2): global mat, k ones, z = [], [] if mat[x1][y1] == 1: ones.append((x1 + 1, y1 + 1)) else: z.append((x1 + 1, y1 + 1)) if mat[x2][y1] == 1: ones.append((x2 + 1, y1 + 1)) else: z.append((x2 + 1, y1 + 1)) if mat[x1][y2] == 1: ones.append((x1 + 1, y2 + 1)) else: z.append((x1 + 1, y2 + 1)) if mat[x2][y2] == 1: ones.append((x2 + 1, y2 + 1)) else: z.append((x2 + 1, y2 + 1)) while len(ones) != 0: if len(ones) == 4: k += 1 t = [] for x, y in ones[:-1]: mat[x - 1][y - 1] = 0 t.append(x) t.append(y) out.append(t) elif len(ones) == 3: k += 1 t = [] for x, y in ones: mat[x - 1][y - 1] = 0 t.append(x) t.append(y) out.append(t) elif len(ones) == 2: k += 1 t = [] for x, y in ones[:-1]: mat[x - 1][y - 1] = 0 t.append(x) t.append(y) for x, y in z: mat[x - 1][y - 1] = 1 t.append(x) t.append(y) out.append(t) else: k += 1 t = [] for x, y in ones: mat[x - 1][y - 1] = 0 t.append(x) t.append(y) for x, y in z[:-1]: mat[x - 1][y - 1] = 1 t.append(x) t.append(y) out.append(t) ones, z = [], [] if mat[x1][y1] == 1: ones.append((x1 + 1, y1 + 1)) else: z.append((x1 + 1, y1 + 1)) if mat[x2][y1] == 1: ones.append((x2 + 1, y1 + 1)) else: z.append((x2 + 1, y1 + 1)) if mat[x1][y2] == 1: ones.append((x1 + 1, y2 + 1)) else: z.append((x1 + 1, y2 + 1)) if mat[x2][y2] == 1: ones.append((x2 + 1, y2 + 1)) else: z.append((x2 + 1, y2 + 1)) for _ in range(int(input())): n, m = map(int, input().split()) mat = [] for i in range(n): mat.append(list(map(int, list(input())))) k = 0 out = [] i = 0 if n % 2: j = 0 if m % 2: func(n - 2, m - 2, n - 1, m - 1) while j < m - 1: if mat[n - 1][j] == 1: flip(n - 1, j) flip(n - 2, j) flip(n - 2, j + 1) k += 1 out.append([n, j + 1, n - 1, j + 1, n - 1, j + 2]) if mat[n - 1][j + 1] == 1: flip(n - 1, j + 1) flip(n - 2, j) flip(n - 2, j + 1) k += 1 out.append([n, j + 2, n - 1, j + 1, n - 1, j + 2]) j += 2 while i < n - 1: j = 0 if m % 2: if mat[i][m - 1]: flip(i, m - 1) flip(i, m - 2) flip(i + 1, m - 2) k += 1 out.append([i + 1, m, i + 1, m - 1, i + 2, m - 1]) if mat[i + 1][m - 1]: flip(i + 1, m - 1) flip(i, m - 2) flip(i + 1, m - 2) k += 1 out.append([i + 2, m, i + 1, m - 1, i + 2, m - 1]) while j < m - 1: func(i, j, i + 1, j + 1) j += 2 i += 2 print(k) for i in out: print(*i)
FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR LIST LIST IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST LIST IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n × m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 × 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≤ t ≤ 5000) — the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≤ n, m ≤ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≤ k ≤ nm) — the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, x_2, x_3 ≤ n, 1 ≤ y_1, y_2, y_3 ≤ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 × 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
import sys readline = sys.stdin.readline T = int(readline()) Ans = [] for qu in range(T): N, M = map(int, readline().split()) ans = [] G = [list(map(int, readline().strip())) for _ in range(N)] for i in range(N - 2): for j in range(M - 1): if G[i][j]: ans.append(f"{i + 1} {j + 1} {i + 1} {j + 2} {i + 2} {j + 1}") G[i][j] ^= 1 G[i][j + 1] ^= 1 G[i + 1][j] ^= 1 j = M - 1 if G[i][M - 1]: ans.append(f"{i + 1} {j + 1} {i + 2} {j} {i + 2} {j + 1}") G[i][j] ^= 1 G[i + 1][j - 1] ^= 1 G[i + 1][j] ^= 1 i = N - 2 for j in range(M - 2): if G[i][j]: ans.append(f"{i + 1} {j + 1} {i + 1} {j + 2} {i + 2} {j + 1}") G[i][j] ^= 1 G[i][j + 1] ^= 1 G[i + 1][j] ^= 1 a = N - 2 b = N - 1 ai = a + 1 bi = b + 1 for j in range(M - 2): if G[a][j] and G[b][j]: ans.append(f"{ai} {j + 1} {bi} {j + 1} {ai} {j + 2}") G[a][j] ^= 1 G[b][j] ^= 1 G[a][j + 1] ^= 1 continue if G[a][j] or G[b][j]: if G[a][j]: c = a else: c = b ans.append(f"{c + 1} {j + 1} {bi} {j + 2} {ai} {j + 2}") G[c][j] ^= 1 G[b][j + 1] ^= 1 G[a][j + 1] ^= 1 H = [G[N - 2][M - 2], G[N - 1][M - 2], G[N - 2][M - 1], G[N - 1][M - 1]] ax, ay = N - 2 + 1, M - 2 + 1 bx, by = N - 1 + 1, M - 2 + 1 cx, cy = N - 2 + 1, M - 1 + 1 dx, dy = N - 1 + 1, M - 1 + 1 abcd = [(ax, ay), (bx, by), (cx, cy), (dx, dy)] if H.count(1) == 4: H[0] ^= 1 H[1] ^= 1 H[2] ^= 1 ans.append(f"{ax} {ay} {bx} {by} {cx} {cy}") if H.count(1) == 1: res = [0, 1, 2, 3] for idx in range(4): if H[idx]: break res.remove(res[idx - 1]) ans.append( f"{abcd[res[0]][0]} {abcd[res[0]][1]} {abcd[res[1]][0]} {abcd[res[1]][1]} {abcd[res[2]][0]} {abcd[res[2]][1]}" ) for r in res: H[r] ^= 1 if H.count(1) == 2: res = [] for idx in range(4): if not H[idx]: res.append(idx) for idx in range(4): if H[idx]: res.append(idx) break ans.append( f"{abcd[res[0]][0]} {abcd[res[0]][1]} {abcd[res[1]][0]} {abcd[res[1]][1]} {abcd[res[2]][0]} {abcd[res[2]][1]}" ) for r in res: H[r] ^= 1 if H.count(1) == 3: res = [] for idx in range(4): if H[idx]: res.append(idx) ans.append( f"{abcd[res[0]][0]} {abcd[res[0]][1]} {abcd[res[1]][0]} {abcd[res[1]][1]} {abcd[res[2]][0]} {abcd[res[2]][1]}" ) for r in res: H[r] ^= 1 Ans.append(str(len(ans))) Ans.extend(ans) print("\n".join(Ans))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER STRING VAR STRING BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING BIN_OP VAR NUMBER STRING VAR STRING BIN_OP VAR NUMBER STRING VAR STRING BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER STRING VAR STRING BIN_OP VAR NUMBER STRING VAR STRING BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR IF FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER STRING VAR VAR NUMBER NUMBER STRING VAR VAR NUMBER NUMBER STRING VAR VAR NUMBER NUMBER STRING VAR VAR NUMBER NUMBER STRING VAR VAR NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER STRING VAR VAR NUMBER NUMBER STRING VAR VAR NUMBER NUMBER STRING VAR VAR NUMBER NUMBER STRING VAR VAR NUMBER NUMBER STRING VAR VAR NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER STRING VAR VAR NUMBER NUMBER STRING VAR VAR NUMBER NUMBER STRING VAR VAR NUMBER NUMBER STRING VAR VAR NUMBER NUMBER STRING VAR VAR NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n × m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 × 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≤ t ≤ 5000) — the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≤ n, m ≤ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≤ k ≤ nm) — the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, x_2, x_3 ≤ n, 1 ≤ y_1, y_2, y_3 ≤ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 × 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
t = int(input()) for _ in range(t): n, m = map(int, input().split()) a = [list(map(int, input())) for i in range(n)] def add(x, y, z): a[x[0]][x[1]] ^= 1 a[y[0]][y[1]] ^= 1 a[z[0]][z[1]] ^= 1 ans.append(" ".join(map(lambda x: str(x + 1), (*x, *y, *z)))) ans = [] for i in range(n - 1, 1, -1): for j in range(m): if a[i][j]: add((i, j), (i - 1, j), (i - 1, j + (1 if j < m - 1 else -1))) for j in range(m - 1, 1, -1): for i in (0, 1): if a[i][j]: add((i, j), (0, j - 1), (1, j - 1)) for i in (0, 1): for j in (0, 1): if (a[0][0] + a[0][1] + a[1][0] + a[1][1] + a[i][j]) % 2: add((i ^ 1, j), (i, j ^ 1), (i ^ 1, j ^ 1)) print(len(ans)) print("\n".join(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 VAR VAR FUNC_CALL VAR VAR FUNC_DEF VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR NUMBER NUMBER FOR VAR NUMBER NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n × m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 × 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≤ t ≤ 5000) — the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≤ n, m ≤ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≤ k ≤ nm) — the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, x_2, x_3 ≤ n, 1 ≤ y_1, y_2, y_3 ≤ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 × 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
import sys input = sys.stdin.readline testcase = int(input()) def op(s): if s == "1": return "0" else: return "1" for _ in range(testcase): h, w = map(int, input().split()) grid = [list(input().rstrip()) for i in range(h)] ans = [] for i in range(h - 2): for j in range(w): if grid[i][j] == "1": if j < w - 1: grid[i][j] = op(grid[i][j]) grid[i][j + 1] = op(grid[i][j + 1]) grid[i + 1][j] = op(grid[i + 1][j]) ans.append((i + 1, j + 1, i + 1, j + 2, i + 2, j + 1)) else: grid[i][j] = op(grid[i][j]) grid[i + 1][j] = op(grid[i + 1][j]) grid[i + 1][j - 1] = op(grid[i + 1][j - 1]) ans.append((i + 1, j + 1, i + 2, j + 1, i + 2, j)) for j in range(w - 2): if grid[h - 2][j] == "1": i = h - 2 grid[i][j] = op(grid[i][j]) grid[i][j + 1] = op(grid[i][j + 1]) grid[i + 1][j] = op(grid[i + 1][j]) ans.append((i + 1, j + 1, i + 1, j + 2, i + 2, j + 1)) if grid[h - 1][j] == "1": i = h - 1 grid[i][j] = op(grid[i][j]) grid[i][j + 1] = op(grid[i][j + 1]) grid[i - 1][j + 1] = op(grid[i - 1][j + 1]) ans.append((i + 1, j + 1, i + 1, j + 2, i, j + 2)) ls = [ grid[h - 2][w - 2], grid[h - 2][w - 1], grid[h - 1][w - 2], grid[h - 1][w - 1], ] for i in range(16): lsc = ls[:] ope = [] for j in range(4): if i & 1 << j: ope.append(j) for x in ope: for k in range(4): if x != k: lsc[k] = op(lsc[k]) if lsc.count("0") == 4: for x in ope: if x == 0: ans.append((h, w - 1, h - 1, w, h, w)) if x == 1: ans.append((h - 1, w - 1, h, w - 1, h, w)) if x == 2: ans.append((h - 1, w - 1, h - 1, w, h, w)) if x == 3: ans.append((h - 1, w - 1, h, w - 1, h - 1, w)) break print(len(ans)) for row in ans: print(*row)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR STRING RETURN STRING RETURN STRING 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 FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR STRING NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n × m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 × 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≤ t ≤ 5000) — the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≤ n, m ≤ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≤ k ≤ nm) — the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, x_2, x_3 ≤ n, 1 ≤ y_1, y_2, y_3 ≤ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 × 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, m = map(int, input().split()) table = [list(map(int, list(input().strip()))) for _ in range(n)] def add(a, b, c): table[a[0]][a[1]] = 1 - table[a[0]][a[1]] table[b[0]][b[1]] = 1 - table[b[0]][b[1]] table[c[0]][c[1]] = 1 - table[c[0]][c[1]] out.append( " ".join(map(lambda x: str(x + 1), (a[0], a[1], b[0], b[1], c[0], c[1]))) ) out = [] for i in range(n - 1, 1, -1): for j in range(m): if table[i][j]: if j + 1 < m: add((i, j), (i - 1, j), (i - 1, j + 1)) else: add((i, j), (i - 1, j), (i - 1, j - 1)) for j in range(m - 1, 1, -1): for i in range(2): if table[i][j]: if table[i][j]: add((i, j), (0, j - 1), (1, j - 1)) for i in range(2): for j in range(2): tot = table[0][0] + table[0][1] + table[1][0] + table[1][1] if (tot + table[i][j]) % 2: add((1 - i, j), (1 - i, 1 - j), (i, 1 - j)) print(len(out)) print("\n".join(out))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n × m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 × 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≤ t ≤ 5000) — the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≤ n, m ≤ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≤ k ≤ nm) — the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, x_2, x_3 ≤ n, 1 ≤ y_1, y_2, y_3 ≤ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 × 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
import sys from itertools import permutations input = lambda: sys.stdin.readline().rstrip("\r\n") def countSet(arr): n = len(arr) m = len(arr[0]) cnt = 0 for i in range(n): for j in range(m): if arr[i][j] == 1: cnt += 1 return cnt def applyInPlace(grid, igrid, jgrid, sq2x2): if sq2x2[0][0] == 1: grid[igrid][jgrid] = 1 - grid[igrid][jgrid] if sq2x2[0][1] == 1: grid[igrid][jgrid + 1] = 1 - grid[igrid][jgrid + 1] if sq2x2[1][0] == 1: grid[igrid + 1][jgrid] = 1 - grid[igrid + 1][jgrid] if sq2x2[1][1] == 1: grid[igrid + 1][jgrid + 1] = 1 - grid[igrid + 1][jgrid + 1] def solveAllExceptLast2Rows(grid): n = len(grid) m = len(grid[0]) ans = [] for igrid in range(n - 2): for jgrid in range(m): if grid[igrid][jgrid] == 1: if jgrid < m - 1: apply = 2 else: jgrid -= 1 apply = 3 sq2x2 = all2x2with3set[apply] applyInPlace(grid, igrid, jgrid, sq2x2) ans.append(convertToSixIntegers(igrid, jgrid, sq2x2)) return ans def solveLast2Rows(grid): n = len(grid) m = len(grid[0]) igrid = n - 2 ans = [] for jgrid in range(m - 2): if grid[igrid][jgrid] == 1 and grid[igrid + 1][jgrid] == 1: apply = 0 elif grid[igrid][jgrid] == 1: apply = 1 elif grid[igrid + 1][jgrid] == 1: apply = 3 else: continue sq2x2 = all2x2with3set[apply] applyInPlace(grid, igrid, jgrid, sq2x2) ans.append(convertToSixIntegers(igrid, jgrid, sq2x2)) for x in solveLastCorner(grid): ans.append(x) return ans def solveLastCorner(grid): n = len(grid) m = len(grid[0]) arr = [ [grid[n - 2][m - 2], grid[n - 2][m - 1]], [grid[n - 1][m - 2], grid[n - 1][m - 1]], ] if countSet(arr) == 0: return [] ans = [-1, -1, -1, -1, -1] for p in permutations((0, 1, 2, 3)): arr = [ [grid[n - 2][m - 2], grid[n - 2][m - 1]], [grid[n - 1][m - 2], grid[n - 1][m - 1]], ] for i in range(4): applyInPlace(arr, 0, 0, all2x2with3set[p[i]]) if countSet(arr) == 0: if i + 1 < len(ans): ans = p[: i + 1] break igrid = n - 2 jgrid = m - 2 ans2 = [] for i in ans: sq2x2 = all2x2with3set[i] ans2.append(convertToSixIntegers(igrid, jgrid, sq2x2)) return ans2 def convertToSixIntegers(igrid, jgrid, sq2x2): ans = [] if sq2x2[0][0] == 1: ans.append(igrid + 1) ans.append(jgrid + 1) if sq2x2[0][1] == 1: ans.append(igrid + 1) ans.append(jgrid + 2) if sq2x2[1][0] == 1: ans.append(igrid + 2) ans.append(jgrid + 1) if sq2x2[1][1] == 1: ans.append(igrid + 2) ans.append(jgrid + 2) return ans def multiLineArrayOfArraysPrint(arr): print("\n".join([" ".join([str(x) for x in y]) for y in arr])) all2x2sqs = [] for b in range(16): temp = [[0, 0], [0, 0]] if b & 1 << 0 > 0: temp[0][0] = 1 if b & 1 << 1 > 0: temp[0][1] = 1 if b & 1 << 2 > 0: temp[1][0] = 1 if b & 1 << 3 > 0: temp[1][1] = 1 all2x2sqs.append(temp) all2x2with3set = [] for arr in all2x2sqs: if countSet(arr) == 3: all2x2with3set.append(arr) t = int(input()) for _ in range(t): n, m = [int(x) for x in input().split()] grid = [] for __ in range(n): grid.append([int(x) for x in input()]) ans = [] ans = ans + solveAllExceptLast2Rows(grid) + solveLast2Rows(grid) print(len(ans)) multiLineArrayOfArraysPrint(ans)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST LIST VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN LIST ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST LIST VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n × m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 × 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≤ t ≤ 5000) — the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≤ n, m ≤ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≤ k ≤ nm) — the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, x_2, x_3 ≤ n, 1 ≤ y_1, y_2, y_3 ≤ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 × 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
import sys def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) def solve(): n, m = mints() a = [list(map(int, minp())) for i in range(n)] ans = [] for i in range(n - 2): l = a[i] ll = a[i + 1] for j in range(m - 1): if l[j] == 1: if l[j + 1] == 1: ans.append((i, j, i, j + 1, i + 1, j)) l[j + 1] ^= 1 ll[j] ^= 1 else: ans.append((i, j, i + 1, j, i + 1, j + 1)) ll[j] ^= 1 ll[j + 1] ^= 1 elif l[j + 1] == 1: ans.append((i, j + 1, i + 1, j, i + 1, j + 1)) l[j + 1] ^= 1 ll[j] ^= 1 ll[j + 1] ^= 1 aa = a[n - 2] bb = a[n - 1] for j in range(m - 2): if aa[j] == 1: if bb[j] == 1: ans.append((n - 2, j, n - 1, j, n - 2, j + 1)) aa[j + 1] ^= 1 else: ans.append((n - 2, j, n - 2, j + 1, n - 1, j + 1)) aa[j + 1] ^= 1 bb[j + 1] ^= 1 elif bb[j] == 1: ans.append((n - 1, j, n - 2, j + 1, n - 1, j + 1)) aa[j + 1] ^= 1 bb[j + 1] ^= 1 c = [(n - 2, m - 2), (n - 2, m - 1), (n - 1, m - 2), (n - 1, m - 1)] while True: ss = 0 for x, y in c: ss += a[x][y] if ss == 0: break if ss == 1 or ss == 3: for i in range(4): x, y = c[i] if a[x][y] == 0: break if ss == 2: for i in range(4): x, y = c[i] if a[x][y] == 1: break if ss == 4: i = 0 d = c.copy() d.pop(i) tmp = [] for x, y in d: a[x][y] ^= 1 tmp.append(x) tmp.append(y) ans.append(tmp) print(len(ans)) for l in ans: print(" ".join([str(i + 1) for i in l])) for i in range(mint()): solve()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n × m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 × 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≤ t ≤ 5000) — the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≤ n, m ≤ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≤ k ≤ nm) — the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, x_2, x_3 ≤ n, 1 ≤ y_1, y_2, y_3 ≤ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 × 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
t = int(input()) def countone(i, j): count = 0 ans = [] if arr[i][j] == "1": count += 1 ans.append(i + 1) ans.append(j + 1) if arr[i + 1][j] == "1": count += 1 ans.append(i + 2) ans.append(j + 1) if arr[i][j + 1] == "1": count += 1 ans.append(i + 1) ans.append(j + 2) if arr[i + 1][j + 1] == "1": count += 1 ans.append(i + 2), ans.append(j + 2) return ans, count def switch(i, j): if arr[i][j] == "1": arr[i][j] = "0" else: arr[i][j] = "1" def change1(i, j, a): a1 = a.copy() x1, y1 = a c1 = [x1 - 1, y1 - 1] ans1 = [] matrix = [[i, j], [i, j + 1], [i + 1, j], [i + 1, j + 1]] m = [] for item in matrix: if item != c1: m.append(item) for i in range(3): for j in range(i + 1, 3): k = [x1, y1, m[i][0] + 1, m[i][1] + 1, m[j][0] + 1, m[j][1] + 1] switch(x1 - 1, y1 - 1) switch(m[i][0], m[i][1]) switch(m[j][0], m[j][1]) ans1.append(k) return ans1 def change2(i, j, a1): a = a1.copy() x1, y1, x2, y2 = a c1 = [x1 - 1, y1 - 1] c2 = [x2 - 1, y2 - 1] matrix = [[i, j], [i, j + 1], [i + 1, j], [i + 1, j + 1]] for item in matrix: if item != c1 and item != c2: a.append(item[0] + 1) a.append(item[1] + 1) switch(item[0], item[1]) break switch(x1 - 1, y1 - 1) switch(x2 - 1, y2 - 1) return a while t > 0: t -= 1 n, m = [int(x) for x in input().split()] arr = [] for i in range(n): s = input() a = [] for j in range(m): a.append(s[j]) arr.append(a) ans = [] result = 0 if n % 2 != 0: for i in range(m - 1): if arr[n - 1][i] == "1": switch(n - 1, i) switch(n - 2, i) switch(n - 2, i + 1) ans.append([n, i + 1, n - 1, i + 1, n - 1, i + 2]) result += 1 if arr[n - 1][m - 1] == "1": switch(n - 1, m - 1) switch(n - 2, m - 1) switch(n - 2, m - 2) ans.append([n, m, n - 1, m, n - 1, m - 1]) result += 1 if m % 2 != 0: if arr[0][m - 1] == "1": switch(0, m - 1) switch(0, m - 2) switch(1, m - 2) ans.append([1, m, 1, m - 1, 2, m - 1]) result += 1 end = n - n % 2 for i in range(1, end): if arr[i][m - 1] == "1": switch(i, m - 1) switch(i, m - 2) switch(i - 1, m - 2) ans.append([i + 1, m, i + 1, m - 1, i, m - 1]) result += 1 i = 0 j = 0 while i < n - 1: while j < m - 1: a, count = countone(i, j) if count == 4: result += 4 ans.append([i + 1, j + 2, i + 2, j + 1, i + 2, j + 2]) switch(i, j + 1) switch(i + 1, j) switch(i + 1, j + 1) ans += change1(i, j, [i + 1, j + 1]) elif count == 3: result += 1 switch(a[0] - 1, a[1] - 1) switch(a[2] - 1, a[3] - 1) switch(a[4] - 1, a[5] - 1) ans.append(a) elif count == 2: ans.append(change2(i, j, a)) col = ans[-1][-1] row = ans[-1][-2] ans += change1(i, j, [row, col]) result += 4 elif count == 1: ans += change1(i, j, a) result += 3 j += 2 j = 0 i += 2 while i < n - 1: while j < m - 1: a, count = countone(i, j) j += 1 i += 1 print(result) for i in range(result): print(*ans[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST LIST VAR VAR LIST VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST VAR VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST LIST VAR VAR LIST VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR LIST VAR VAR VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n × m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 × 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≤ t ≤ 5000) — the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≤ n, m ≤ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≤ k ≤ nm) — the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, x_2, x_3 ≤ n, 1 ≤ y_1, y_2, y_3 ≤ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 × 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
import sys Z = sys.stdin.readline O = [] def S(q, x, y): s = sum(sum(i) for i in q) v = [ (x, y, x + 1, y, x, y + 1), (x, y, x + 1, y + 1, x, y + 1), (x, y, x + 1, y, x + 1, y + 1), (x + 1, y + 1, x + 1, y, x, y + 1), ] if s > 3: return v if s > 2: if q[0][0] < 1: return [v[3]] if q[0][1] < 1: return [v[2]] if q[1][0] < 1: return [v[1]] return [v[0]] if s > 1: if q[0][0] < 1 and q[0][1] < 1: return v[:2] if q[0][0] < 1 and q[1][1] < 1: return v[1:3] if q[0][0] < 1 and q[1][0] < 1: return [v[0], v[2]] if q[0][1] < 1 and q[1][1] < 1: return [v[1], v[3]] if q[0][1] < 1 and q[1][0] < 1: return [v[0], v[3]] if q[1][0] < 1 and q[1][1] < 1: return v[2:] if s: if q[0][0]: k = 3 elif q[0][1]: k = 2 elif q[1][0]: k = 1 else: k = 0 return v[:k] + v[k + 1 :] return [] for _ in range(int(Z())): n, m = map(int, Z().split()) a = [] o = [] for i in range(n): a.append(list(map(int, Z().strip()))) if n % 2: for i in range(0, m - m % 2, 2): if a[-1][i]: if a[-1][i + 1]: o += [(n, i + 1, n, i + 2, n - 1, i + 1)] a[-2][i] = 1 - a[-2][i] else: o += [(n, i + 1, n - 1, i + 2, n - 1, i + 1)] a[-2][i] = 1 - a[-2][i] a[-2][i + 1] = 1 - a[-2][i + 1] elif a[-1][i + 1]: o += [(n, i + 2, n - 1, i + 2, n - 1, i + 1)] a[-2][i] = 1 - a[-2][i] a[-2][i + 1] = 1 - a[-2][i + 1] if m % 2 and a[-1][-1]: o += [(n, m, n - 1, m, n - 1, m - 1)] a[-2][-1] = 1 - a[-2][-1] a[-2][-2] = 1 - a[-2][-2] if m % 2: for i in range(0, n - n % 2, 2): if a[i][-1]: if a[i + 1][-1]: o += [(i + 1, m, i + 2, m, i + 1, m - 1)] a[i][-2] = 1 - a[i][-2] else: o += [(i + 1, m, i + 2, m - 1, i + 1, m - 1)] a[i][-2] = 1 - a[i][-2] a[i + 1][-2] = 1 - a[i + 1][-2] elif a[i + 1][-1]: o += [(i + 2, m - 1, i + 2, m, i + 1, m - 1)] a[i][-2] = 1 - a[i][-2] a[i + 1][-2] = 1 - a[i + 1][-2] for i in range(n // 2): for j in range(m // 2): x, y = 2 * i, 2 * j o += S( ((a[x][y], a[x][y + 1]), (a[x + 1][y], a[x + 1][y + 1])), x + 1, y + 1 ) O.append(str(len(o))) O.append("\n".join(" ".join(map(str, i)) for i in o)) print("\n".join(O))
IMPORT ASSIGN VAR VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR IF VAR NUMBER IF VAR NUMBER NUMBER NUMBER RETURN LIST VAR NUMBER IF VAR NUMBER NUMBER NUMBER RETURN LIST VAR NUMBER IF VAR NUMBER NUMBER NUMBER RETURN LIST VAR NUMBER RETURN LIST VAR NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER RETURN VAR NUMBER IF VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER RETURN VAR NUMBER NUMBER IF VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER RETURN LIST VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER RETURN LIST VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER RETURN LIST VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER RETURN VAR NUMBER IF VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER VAR VAR LIST VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR LIST VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR LIST VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP NUMBER VAR NUMBER NUMBER IF BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n × m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 × 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≤ t ≤ 5000) — the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≤ n, m ≤ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≤ k ≤ nm) — the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, x_2, x_3 ≤ n, 1 ≤ y_1, y_2, y_3 ≤ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 × 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
s, res = [], [] def solve1(x, y, c1, c2, c3, c4): if c1 == "1": res.append([x, y, x, y + 1, x + 1, y]) solve2(x, y, "0", "1", "1", "0") elif c2 == "1": res.append([x, y, x, y + 1, x + 1, y]) solve2(x, y, "1", "0", "1", "0") elif c3 == "1": res.append([x, y, x, y + 1, x + 1, y]) solve2(x, y, "1", "1", "0", "0") elif c4 == "1": res.append([x, y, x, y + 1, x + 1, y + 1]) solve2(x, y, "1", "1", "0", "0") def solve2(x, y, c1, c2, c3, c4): if c1 == "0" and c2 == "0": res.append([x, y, x, y + 1, x + 1, y]) solve3(x, y, "1", "1", "0", "1") elif c1 == "0" and c3 == "0": res.append([x, y, x + 1, y, x, y + 1]) solve3(x, y, "1", "0", "1", "1") elif c1 == "0" and c4 == "0": res.append([x, y, x + 1, y, x + 1, y + 1]) solve3(x, y, "1", "1", "0", "1") elif c2 == "0" and c3 == "0": res.append([x, y, x + 1, y, x, y + 1]) solve3(x, y, "0", "1", "1", "1") elif c2 == "0" and c4 == "0": res.append([x, y, x, y + 1, x + 1, y + 1]) solve3(x, y, "0", "1", "1", "1") elif c3 == "0" and c4 == "0": res.append([x, y, x + 1, y, x + 1, y + 1]) solve3(x, y, "0", "1", "1", "1") def solve3(x, y, c1, c2, c3, c4): if c1 == "0": res.append([x + 1, y, x, y + 1, x + 1, y + 1]) elif c2 == "0": res.append([x + 1, y, x, y, x + 1, y + 1]) elif c3 == "0": res.append([x, y, x, y + 1, x + 1, y + 1]) elif c4 == "0": res.append([x + 1, y, x, y + 1, x, y]) def solve4(x, y): res.append([x, y, x, y + 1, x + 1, y]) solve1(x, y, "0", "0", "0", "1") def trans(x1, y1, x2, y2, x3, y3): s[x1][y1] = "1" if s[x1][y1] == "0" else "0" s[x2][y2] = "1" if s[x2][y2] == "0" else "0" s[x3][y3] = "1" if s[x3][y3] == "0" else "0" res.append([x1 + 1, y1 + 1, x2 + 1, y2 + 1, x3 + 1, y3 + 1]) T = int(input()) for _ in range(T): n, m = map(int, input().split()) s.clear() res.clear() for i in range(n): s.append(list(input())) if m % 2: for i in range(n - 1): if s[i][m - 1] == "1" and s[i + 1][m - 1] == "0": trans(i, m - 1, i, m - 2, i + 1, m - 2) elif s[i][m - 1] == "0" and s[i + 1][m - 1] == "1": trans(i + 1, m - 1, i, m - 2, i + 1, m - 2) elif s[i][m - 1] == "1" and s[i + 1][m - 1] == "1": trans(i + 1, m - 1, i, m - 1, i + 1, m - 2) for j in range(0, m - 1, 2): if n % 2: if s[n - 1][j] == "1" and s[n - 1][j + 1] == "0": trans(n - 1, j, n - 2, j, n - 2, j + 1) elif s[n - 1][j] == "0" and s[n - 1][j + 1] == "1": trans(n - 1, j + 1, n - 2, j, n - 2, j + 1) elif s[n - 1][j] == "1" and s[n - 1][j + 1] == "1": trans(n - 1, j, n - 2, j, n - 1, j + 1) for i in range(0, n - 1, 2): num = 0 if s[i][j] == "1": num += 1 if s[i][j + 1] == "1": num += 1 if s[i + 1][j] == "1": num += 1 if s[i + 1][j + 1] == "1": num += 1 if num == 1: solve1(i + 1, j + 1, s[i][j], s[i][j + 1], s[i + 1][j], s[i + 1][j + 1]) if num == 2: solve2(i + 1, j + 1, s[i][j], s[i][j + 1], s[i + 1][j], s[i + 1][j + 1]) if num == 3: solve3(i + 1, j + 1, s[i][j], s[i][j + 1], s[i + 1][j], s[i + 1][j + 1]) if num == 4: solve4(i + 1, j + 1) print(len(res)) for arr in res: print(" ".join("%s" % d for d in arr))
ASSIGN VAR VAR LIST LIST FUNC_DEF IF VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR STRING STRING STRING STRING IF VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR STRING STRING STRING STRING IF VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR STRING STRING STRING STRING IF VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING STRING STRING STRING FUNC_DEF IF VAR STRING VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR STRING STRING STRING STRING IF VAR STRING VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING STRING STRING STRING IF VAR STRING VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING STRING STRING STRING IF VAR STRING VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING STRING STRING STRING IF VAR STRING VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING STRING STRING STRING IF VAR STRING VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING STRING STRING STRING FUNC_DEF IF VAR STRING EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR STRING STRING STRING STRING FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR STRING STRING STRING ASSIGN VAR VAR VAR VAR VAR VAR STRING STRING STRING ASSIGN VAR VAR VAR VAR VAR VAR STRING STRING STRING EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP STRING VAR VAR VAR
This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n × m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 × 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≤ t ≤ 5000) — the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≤ n, m ≤ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≤ k ≤ nm) — the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, x_2, x_3 ≤ n, 1 ≤ y_1, y_2, y_3 ≤ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 × 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
for _ in range(0, int(input())): n, m = map(int, input().split()) mat = [[char for char in input()] for _ in range(0, n)] answer = [] def update(changes): answer.append((changes[0], changes[1], changes[2])) for x, y in changes: if mat[x][y] == "0": mat[x][y] = "1" else: mat[x][y] = "0" def change(a, b, c, vec0, vec1): answer.append((a, b, c)) for x, y in [a, b, c]: if mat[x][y] == "0": vec0.remove((x, y)) vec1.append((x, y)) mat[x][y] = "1" else: vec0.append((x, y)) vec1.remove((x, y)) mat[x][y] = "0" return vec0, vec1 if n > 2 or m > 2: for x in range(0, n, 2): for j in range(0, m - 1, 1): i = min(n - 2, x) if mat[i][j] == "0" and mat[i + 1][j] == "0": continue changes = [] if mat[i][j] == "1": changes.append((i, j)) if mat[i + 1][j] == "1": changes.append((i + 1, j)) if len(changes) < 3: changes.append((i, j + 1)) if len(changes) < 3: changes.append((i + 1, j + 1)) update(changes) for x in range(0, n - 2): i = x j = m - 2 if mat[i][j] == "0" and mat[i][j + 1] == "0": continue changes = [] if mat[i][j] == "1": changes.append((i, j)) if mat[i][j + 1] == "1": changes.append((i, j + 1)) if len(changes) < 3: changes.append((i + 1, j)) if len(changes) < 3: changes.append((i + 1, j + 1)) update(changes) a = n - 2 b = m - 2 vec0 = [ (x, y) for x in range(a, a + 2) for y in range(b, b + 2) if mat[x][y] == "0" ] vec1 = [ (x, y) for x in range(a, a + 2) for y in range(b, b + 2) if mat[x][y] == "1" ] cnt = 0 while len(vec1) > 0: if len(vec1) == 4: vec0, vec1 = change(vec1[0], vec1[1], vec1[2], vec0, vec1) elif len(vec1) == 3: vec0, vec1 = change(vec1[0], vec1[1], vec1[2], vec0, vec1) else: vec0, vec1 = change(vec0[0], vec0[1], vec1[0], vec0, vec1) print(len(answer)) for x, y, z in answer: print(x[0] + 1, x[1] + 1, y[0] + 1, y[1] + 1, z[0] + 1, z[1] + 1)
FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR LIST VAR VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR STRING RETURN VAR VAR IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR STRING VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR LIST IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR LIST IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR STRING ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER
This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n × m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 × 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≤ t ≤ 5000) — the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≤ n, m ≤ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≤ k ≤ nm) — the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, x_2, x_3 ≤ n, 1 ≤ y_1, y_2, y_3 ≤ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 × 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
import sys input = sys.stdin.readline def main(): n, m = map(int, input().split()) alst = [list(input().strip()) for _ in range(n)] rev = {"1": "0", "0": "1"} ans = [] for i in range(n - 2): for j in range(m - 2): if alst[i][j] == "1": alst[i][j] = "0" alst[i + 1][j] = rev[alst[i + 1][j]] alst[i][j + 1] = rev[alst[i][j + 1]] ans.append((i + 1, j + 1, i + 2, j + 1, i + 1, j + 2)) i = n - 1 for j in range(m - 2): if alst[i][j] == "1" and alst[i - 1][j] == "1": alst[i][j] = "0" alst[i - 1][j] = "0" alst[i][j + 1] = rev[alst[i][j + 1]] ans.append((i + 1, j + 1, i, j + 1, i + 1, j + 2)) elif alst[i][j] == "1": alst[i][j] = "0" alst[i - 1][j + 1] = rev[alst[i - 1][j + 1]] alst[i][j + 1] = rev[alst[i][j + 1]] ans.append((i + 1, j + 1, i, j + 2, i + 1, j + 2)) elif alst[i - 1][j] == "1": alst[i - 1][j] = "0" alst[i - 1][j + 1] = rev[alst[i - 1][j + 1]] alst[i][j + 1] = rev[alst[i][j + 1]] ans.append((i, j + 1, i, j + 2, i + 1, j + 2)) j = m - 1 for i in range(n - 2): if alst[i][j] == "1" and alst[i][j - 1] == "1": alst[i][j] = "0" alst[i][j - 1] = "0" alst[i + 1][j] = rev[alst[i + 1][j]] ans.append((i + 1, j + 1, i + 1, j, i + 2, j + 1)) elif alst[i][j] == "1": alst[i][j] = "0" alst[i + 1][j - 1] = rev[alst[i + 1][j - 1]] alst[i + 1][j] = rev[alst[i + 1][j]] ans.append((i + 1, j + 1, i + 2, j, i + 2, j + 1)) elif alst[i][j - 1] == "1": alst[i][j - 1] = "0" alst[i + 1][j - 1] = rev[alst[i + 1][j - 1]] alst[i + 1][j] = rev[alst[i + 1][j]] ans.append((i + 1, j, i + 2, j, i + 2, j + 1)) blst = [alst[-2][-2:], alst[-1][-2:]] cnt = 0 one = [] for i in range(2): for j in range(2): if blst[i][j] == "1": one.append((i, j)) cnt += 1 lst = [(0, 0), (0, 1), (1, 0), (1, 1)] if cnt == 0: pass if cnt == 4: one = [(1, 1)] tmp = [] tmp.append(n - 1) tmp.append(m - 1) tmp.append(n) tmp.append(m - 1) tmp.append(n - 1) tmp.append(m) cnt = 1 ans.append(tmp) if cnt == 1: tmp = [n - 1 + one[0][0], m - 1 + one[0][1]] sub = 1 for l in lst: if l == one[0]: continue tmp.append(n - 1 + l[0]) tmp.append(m - 1 + l[1]) one.append(l) sub += 1 if sub == 3: break ans.append(tmp) one = one[1:] cnt = 2 if cnt == 2: tmp = [n - 1 + one[0][0], m - 1 + one[0][1]] for l in lst: if l in one: continue tmp.append(n - 1 + l[0]) tmp.append(m - 1 + l[1]) one.append(l) ans.append(tmp) one = one[1:] cnt = 3 if cnt == 3: tmp = [] for ii, jj in one: tmp.append(n - 1 + ii) tmp.append(m - 1 + jj) ans.append(tmp) cnt = 0 print(len(ans)) for row in ans: print(*row) for _ in range(int(input())): main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT STRING STRING STRING STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR LIST BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n × m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 × 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≤ t ≤ 5000) — the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≤ n, m ≤ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≤ k ≤ nm) — the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, x_2, x_3 ≤ n, 1 ≤ y_1, y_2, y_3 ≤ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 × 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
from sys import stdin def c(x1, y1, x2, y2, x3, y3): ans.append((x1 + 1, y1 + 1, x2 + 1, y2 + 1, x3 + 1, y3 + 1)) a[x1][y1] ^= 1 a[x2][y2] ^= 1 a[x3][y3] ^= 1 return tt = int(stdin.readline()) for loop in range(tt): n, m = map(int, stdin.readline().split()) a = [] ans = [] for i in range(n): tmp = list(map(int, list(stdin.readline()[:-1]))) a.append(tmp) for i in range(n - 2): for j in range(m): if j != m - 1: if a[i][j] == 1 and a[i][j + 1] == 1: if a[i + 1][j] == 1: c(i, j, i, j + 1, i + 1, j) else: c(i, j, i, j + 1, i + 1, j + 1) elif a[i][j] == 1: c(i, j, i + 1, j, i + 1, j + 1) else: continue elif a[i][j] == 1: c(i, j, i + 1, j, i + 1, j - 1) for i in range(n - 2, n - 1): for j in range(m - 1): if a[i][j] == 1 and a[i + 1][j] == 1: if a[i][j + 1] == 1: c(i, j, i + 1, j, i, j + 1) else: c(i, j, i + 1, j, i + 1, j + 1) elif a[i][j] == 1: c(i, j, i, j + 1, i + 1, j + 1) elif a[i + 1][j] == 1: c(i + 1, j, i, j + 1, i + 1, j + 1) else: continue if a[n - 1][m - 1] == 0 and a[n - 2][m - 1] == 0: pass elif a[n - 1][m - 1] == 1 and a[n - 2][m - 1] == 0: c(n - 1, m - 1, n - 2, m - 1, n - 2, m - 2) c(n - 1, m - 1, n - 1, m - 2, n - 2, m - 2) c(n - 1, m - 1, n - 1, m - 2, n - 2, m - 1) elif a[n - 1][m - 1] == 0 and a[n - 2][m - 1] == 1: c(n - 1, m - 1, n - 1, m - 2, n - 2, m - 1) c(n - 1, m - 1, n - 2, m - 1, n - 2, m - 2) c(n - 2, m - 2, n - 1, m - 2, n - 2, m - 1) else: c(n - 1, m - 1, n - 1, m - 2, n - 2, m - 2) c(n - 2, m - 2, n - 1, m - 2, n - 2, m - 1) print(len(ans)) for i in ans: print(*i)
FUNC_DEF EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation. For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a_1, a_2, ..., a_{n}. He also wrote a square matrix b of size n × n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as b_{ij}) equals: the "bitwise AND" of numbers a_{i} and a_{j} (that is, b_{ij} = a_{i} & a_{j}), if i ≠ j; -1, if i = j. Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly. Help Polycarpus, given matrix b, restore the sequence of numbers a_1, a_2, ..., a_{n}, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 10^9. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 100) — the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix b_{ij}. It is guaranteed, that for all i (1 ≤ i ≤ n) the following condition fulfills: b_{ii} = -1. It is guaranteed that for all i, j (1 ≤ i, j ≤ n; i ≠ j) the following condition fulfills: 0 ≤ b_{ij} ≤ 10^9, b_{ij} = b_{ji}. -----Output----- Print n non-negative integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) — the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces. It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them. -----Examples----- Input 1 -1 Output 0 Input 3 -1 18 0 18 -1 0 0 0 -1 Output 18 18 0 Input 4 -1 128 128 128 128 -1 148 160 128 148 -1 128 128 160 128 -1 Output 128 180 148 160 -----Note----- If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation.
n = int(input()) p = [0] * n for i in range(n): t = list(map(int, input().split())) t.pop(i) s = 0 for j in t: s |= j p[i] = s print(" ".join(map(str, p)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation. For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a_1, a_2, ..., a_{n}. He also wrote a square matrix b of size n × n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as b_{ij}) equals: the "bitwise AND" of numbers a_{i} and a_{j} (that is, b_{ij} = a_{i} & a_{j}), if i ≠ j; -1, if i = j. Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly. Help Polycarpus, given matrix b, restore the sequence of numbers a_1, a_2, ..., a_{n}, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 10^9. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 100) — the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix b_{ij}. It is guaranteed, that for all i (1 ≤ i ≤ n) the following condition fulfills: b_{ii} = -1. It is guaranteed that for all i, j (1 ≤ i, j ≤ n; i ≠ j) the following condition fulfills: 0 ≤ b_{ij} ≤ 10^9, b_{ij} = b_{ji}. -----Output----- Print n non-negative integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) — the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces. It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them. -----Examples----- Input 1 -1 Output 0 Input 3 -1 18 0 18 -1 0 0 0 -1 Output 18 18 0 Input 4 -1 128 128 128 128 -1 148 160 128 148 -1 128 128 160 128 -1 Output 128 180 148 160 -----Note----- If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation.
n = int(input()) a = [] for i in range(n): a.append(list(map(int, input().split()))) ans = [0] * n for i in range(n): for j in range(n): if j != i: ans[i] |= a[i][j] print(ans[i], end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation. For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a_1, a_2, ..., a_{n}. He also wrote a square matrix b of size n × n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as b_{ij}) equals: the "bitwise AND" of numbers a_{i} and a_{j} (that is, b_{ij} = a_{i} & a_{j}), if i ≠ j; -1, if i = j. Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly. Help Polycarpus, given matrix b, restore the sequence of numbers a_1, a_2, ..., a_{n}, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 10^9. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 100) — the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix b_{ij}. It is guaranteed, that for all i (1 ≤ i ≤ n) the following condition fulfills: b_{ii} = -1. It is guaranteed that for all i, j (1 ≤ i, j ≤ n; i ≠ j) the following condition fulfills: 0 ≤ b_{ij} ≤ 10^9, b_{ij} = b_{ji}. -----Output----- Print n non-negative integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) — the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces. It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them. -----Examples----- Input 1 -1 Output 0 Input 3 -1 18 0 18 -1 0 0 0 -1 Output 18 18 0 Input 4 -1 128 128 128 128 -1 148 160 128 148 -1 128 128 160 128 -1 Output 128 180 148 160 -----Note----- If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation.
n = int(input()) a = [0] * n for i in range(n): for x in map(int, input().split()): if x != -1: a[i] |= x print(*a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation. For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a_1, a_2, ..., a_{n}. He also wrote a square matrix b of size n × n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as b_{ij}) equals: the "bitwise AND" of numbers a_{i} and a_{j} (that is, b_{ij} = a_{i} & a_{j}), if i ≠ j; -1, if i = j. Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly. Help Polycarpus, given matrix b, restore the sequence of numbers a_1, a_2, ..., a_{n}, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 10^9. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 100) — the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix b_{ij}. It is guaranteed, that for all i (1 ≤ i ≤ n) the following condition fulfills: b_{ii} = -1. It is guaranteed that for all i, j (1 ≤ i, j ≤ n; i ≠ j) the following condition fulfills: 0 ≤ b_{ij} ≤ 10^9, b_{ij} = b_{ji}. -----Output----- Print n non-negative integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) — the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces. It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them. -----Examples----- Input 1 -1 Output 0 Input 3 -1 18 0 18 -1 0 0 0 -1 Output 18 18 0 Input 4 -1 128 128 128 128 -1 148 160 128 148 -1 128 128 160 128 -1 Output 128 180 148 160 -----Note----- If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation.
n = int(input()) A = [0] * n ans = [0] * n for i in range(n): A[i] = list(map(int, input().split())) for j in range(n): if j == i: continue ans[i] |= A[i][j] for i in range(n): print(ans[i], " ", end="")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING STRING
Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation. For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a_1, a_2, ..., a_{n}. He also wrote a square matrix b of size n × n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as b_{ij}) equals: the "bitwise AND" of numbers a_{i} and a_{j} (that is, b_{ij} = a_{i} & a_{j}), if i ≠ j; -1, if i = j. Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly. Help Polycarpus, given matrix b, restore the sequence of numbers a_1, a_2, ..., a_{n}, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 10^9. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 100) — the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix b_{ij}. It is guaranteed, that for all i (1 ≤ i ≤ n) the following condition fulfills: b_{ii} = -1. It is guaranteed that for all i, j (1 ≤ i, j ≤ n; i ≠ j) the following condition fulfills: 0 ≤ b_{ij} ≤ 10^9, b_{ij} = b_{ji}. -----Output----- Print n non-negative integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) — the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces. It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them. -----Examples----- Input 1 -1 Output 0 Input 3 -1 18 0 18 -1 0 0 0 -1 Output 18 18 0 Input 4 -1 128 128 128 128 -1 148 160 128 148 -1 128 128 160 128 -1 Output 128 180 148 160 -----Note----- If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation.
n = int(input()) if n == 1: print(0) else: m = [[0] * n] * n a = [int(0)] * n for i in range(0, n): m[i] = input().split() a[i] = int(m[i][(i + 1) % n]) for j in range(0, n): if j != i: a[i] = a[i] | int(m[i][j]) for i in range(0, n): print(a[i], end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP LIST NUMBER VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR STRING
Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation. For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a_1, a_2, ..., a_{n}. He also wrote a square matrix b of size n × n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as b_{ij}) equals: the "bitwise AND" of numbers a_{i} and a_{j} (that is, b_{ij} = a_{i} & a_{j}), if i ≠ j; -1, if i = j. Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly. Help Polycarpus, given matrix b, restore the sequence of numbers a_1, a_2, ..., a_{n}, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 10^9. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 100) — the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix b_{ij}. It is guaranteed, that for all i (1 ≤ i ≤ n) the following condition fulfills: b_{ii} = -1. It is guaranteed that for all i, j (1 ≤ i, j ≤ n; i ≠ j) the following condition fulfills: 0 ≤ b_{ij} ≤ 10^9, b_{ij} = b_{ji}. -----Output----- Print n non-negative integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) — the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces. It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them. -----Examples----- Input 1 -1 Output 0 Input 3 -1 18 0 18 -1 0 0 0 -1 Output 18 18 0 Input 4 -1 128 128 128 128 -1 148 160 128 148 -1 128 128 160 128 -1 Output 128 180 148 160 -----Note----- If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation.
n = int(input()) if n > 1: p = [0] * n r = format(n - 1, "b")[::-1] l = len(r) - 1 for i in range(n): t = list(map(int, input().split())) t.pop(i) s = 0 for j in range(l): if r[j] == "1": s |= t.pop() t = [(t[k] | t[k + 1]) for k in range(0, len(t), 2)] p[i] = s | t[0] print(" ".join(map(str, p))) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER STRING NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
def convert(arr, m): res = [] for val in arr: num = 1 while val % m == 0: val //= m num *= m if len(res) > 0 and res[-1][0] == val: res[-1][1] += num else: res.append([val, num]) return res for i in range(int(input())): n, m = map(int, input().split()) a = list(map(int, input().split())) k = int(input()) b = list(map(int, input().split())) p = convert(a, m) l = convert(b, m) if p == l: print("Yes") else: print("No")
FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
def solve(): n, m = map(int, input().split()) arr = list(map(int, input().split())) k = int(input()) brr = list(map(int, input().split())) crr = [] for num in arr: temp = num while num % m == 0: num //= m ext = 0 if len(crr) > 0 and crr[-1][0] == num: ext += crr[-1][1] crr.pop() crr.append([num, ext + temp // num]) drr = [] for num in brr: temp = num while num % m == 0: num //= m ext = 0 if len(drr) > 0 and drr[-1][0] == num: ext += drr[-1][1] drr.pop() drr.append([num, ext + temp // num]) if crr == drr: print("Yes") else: print("No") for i in range(int(input())): solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
for _ in range(int(input())): n, m = map(int, input().split()) a = list(map(int, input().split())) k = int(input()) b = list(map(int, input().split())) cpt = 0 crem = a[0] ok = True mstep = a.copy() ac = [None] * n bc = [None] * k for i in range(n): q = divmod(mstep[i], m) cnt = 1 while not q[1]: mstep[i] //= m cnt *= m q = divmod(mstep[i], m) ac[i] = [mstep[i], cnt] mstep = b.copy() for i in range(k): q = divmod(mstep[i], m) cnt = 1 while not q[1]: mstep[i] //= m cnt *= m q = divmod(mstep[i], m) bc[i] = [mstep[i], cnt] for i in range(len(ac) - 2, -1, -1): if ac[i][0] == ac[i + 1][0]: ac[i][1] += ac[i + 1][1] ac[i + 1][0] = 0 ac = [x for x in ac if x[0]] for i in range(len(bc) - 2, -1, -1): if bc[i][0] == bc[i + 1][0]: bc[i][1] += bc[i + 1][1] bc[i + 1][0] = 0 bc = [x for x in bc if x[0]] print("Yes" if ac == bc else "No")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING STRING
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
def main(): for _ in range(int(input())): n, m = map(int, input().split()) a = list(map(int, input().split())) k = int(input()) b = list(map(int, input().split())) aa, bb = [], [] for i in range(n): c = 1 while a[i] % m == 0: a[i] //= m c *= m if len(aa) != 0 and aa[-1][0] == a[i]: aa[-1][1] += c else: aa.append([a[i], c]) for i in range(k): c = 1 while b[i] % m == 0: b[i] //= m c *= m if len(bb) != 0 and bb[-1][0] == b[i]: bb[-1][1] += c else: bb.append([b[i], c]) if aa == bb: print("Yes") else: print("No") main()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
t = int(input()) for jkjk in range(t): n, m = tuple(map(int, input().split())) arr = list(map(int, input().split())) k = int(input()) b = list(map(int, input().split())) a_mod = [] b_mod = [] for i in range(len(arr)): p = 1 while arr[i] % m == 0: arr[i] = arr[i] // m p = p * m a_mod.append(p) for i in range(len(b)): p = 1 while b[i] % m == 0: b[i] = b[i] // m p = p * m b_mod.append(p) i, j = 0, 0 while 1: if i >= n and j >= k: print("Yes") break if i >= n or j >= k: print("No") break if arr[i] != b[j]: print("No") break else: jin = min(a_mod[i], b_mod[j]) a_mod[i] -= jin b_mod[j] -= jin if a_mod[i] == 0: i += 1 if b_mod[j] == 0: j += 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
def f(arr, n, m): ar = [] for i in range(n): p = 1 x = arr[i] while x % m == 0: x = x // m p *= m tm = [x, p] ar.append(tm) li = [] i = 0 while i < len(ar): li.append(ar[i]) j = i + 1 while j < len(ar) and ar[i][0] == ar[j][0]: li[len(li) - 1][1] += ar[j][1] j += 1 i = j return li t = int(input()) for i in range(t): n, g = map(int, input().split()) arr = list(map(int, input().split())) m = int(input()) ar = list(map(int, input().split())) smarr = sum(arr) smar = sum(ar) if smarr != smar: print("No") else: a = f(arr, n, g) b = f(ar, m, g) if a == b: print("Yes") else: print("No")
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
import sys input = sys.stdin.readline def solve(): n, m = map(int, input().split()) A = list(map(int, input().split())) k = int(input()) B = list(map(int, input().split())) if abs(n - k) % (m - 1) > 0: return "No" arr1 = [] arr2 = [] for i in range(n): v1 = A[i] cnt = 0 while v1 > 0 and v1 % m == 0: v1 //= m cnt += 1 if not arr1: arr1.append([v1, m**cnt]) elif arr1[-1][0] == v1: arr1[-1][1] += m**cnt else: arr1.append([v1, m**cnt]) for j in range(k): v2 = B[j] cnt = 0 while v2 > 0 and v2 % m == 0: v2 //= m cnt += 1 if not arr2: arr2.append([v2, m**cnt]) elif arr2[-1][0] == v2: arr2[-1][1] += m**cnt else: arr2.append([v2, m**cnt]) u, v = len(arr1), len(arr2) if u != v: return "No" for i in range(u): if arr1[i] != arr2[i]: return "No" return "Yes" for _ in range(int(input())): print(solve())
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER RETURN STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR IF VAR NUMBER NUMBER VAR VAR NUMBER NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR IF VAR NUMBER NUMBER VAR VAR NUMBER NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
def cal(n, a, m): c, d = [], [] for i in range(n): e = 1 while a[i] % m == 0: a[i] //= m e *= m if len(c) == len(d) and len(d) == 0 or c[len(c) - 1] != a[i]: c.append(a[i]) d.append(e) else: d[len(d) - 1] += e return c, d for p in range(int(input())): n, m = map(int, input().split()) a = list(map(int, input().split())) k = int(input()) b = list(map(int, input().split())) c, d = cal(n, a, m) f, g = cal(k, b, m) if c == f and d == g: print("Yes") else: print("No")
FUNC_DEF ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
def reduce(l, m): d = [] last = l[0] c = 1 for i in l[1:]: if i != last: d.append([last, c]) last = i c = 1 else: c += 1 d.append([last, c]) for i in range(len(d)): while d[i][0] % m == 0: d[i] = [d[i][0] // m, d[i][1] * m] r = [d[0]] for i in range(1, len(d)): if d[i][0] == r[-1][0]: r[-1][1] += d[i][1] else: r.append(d[i]) return r for case in range(int(input())): _, m = [int(j) for j in input().split()] a = [int(j) for j in input().split()] input() b = [int(j) for j in input().split()] print(["No", "Yes"][reduce(a, m) == reduce(b, m)])
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR LIST BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST STRING STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
def listbreak(x, m): nt = 1 while x % m == 0: x //= m nt *= m return [x, nt] t = int(input()) for _ in range(t): n1, m = map(int, input().split()) a = list(map(int, input().split())) n2 = int(input()) b = list(map(int, input().split())) na, nb = [listbreak(a[0], m)], [listbreak(b[0], m)] for i in range(1, n1): ret = listbreak(a[i], m) if ret[0] == na[-1][0]: na[-1][1] += ret[1] else: na.append(ret) for i in range(1, n2): ret = listbreak(b[i], m) if ret[0] == nb[-1][0]: nb[-1][1] += ret[1] else: nb.append(ret) if na == nb: print("Yes") else: print("No")
FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR VAR RETURN LIST VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST FUNC_CALL VAR VAR NUMBER VAR LIST FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
for _ in range(int(input())): na, x = map(int, input().split()) a = (*map(int, input().split()),) nb = int(input()) b = (*map(int, input().split()),) x_to_the = [1] mx = max(max(a), max(b)) while x_to_the[-1] <= mx: x_to_the.append(x_to_the[-1] * x) m = len(x_to_the) A = [[1, 0]] for num in a: for i in range(m): if num % x_to_the[i]: break count = x_to_the[i - 1] new = num // count if new == A[-1][0]: A[-1][-1] += count else: A.append([new, count]) B = [[1, 0]] for num in b: for i in range(m): if num % x_to_the[i]: break count = x_to_the[i - 1] new = num // count if new == B[-1][0]: B[-1][-1] += count else: B.append([new, count]) if A == B: print("Yes") else: print("No")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
t = int(input()) def my_reduce(s, m): extras = [] for k in s: time = 1 while k % m == 0: time *= m k /= m time = max(time, 1) extras += [[int(k), time]] i = 1 typer = extras[0][0] prev = 0 while i < len(extras): if extras[i][0] == typer: extras[prev][1] += extras[i][1] extras[i] = 0 else: typer = extras[i][0] prev = i i += 1 return [x for x in extras if x] for i in range(t): n, m = map(int, input().split()) a = list(map(int, input().split())) k = int(input()) b = list(map(int, input().split())) a = my_reduce(a, m) b = my_reduce(b, m) if a == b: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR LIST LIST FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR 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 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 VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
for _ in range(int(input())): n, m = map(int, input().split()) a = [[1, int(x)] for x in input().split()] k = int(input()) b = [[1, int(x)] for x in input().split()] while True: while a and b and a[-1][1] == b[-1][1]: t = min(a[-1][0], b[-1][0]) a[-1][0] -= t b[-1][0] -= t if not a[-1][0]: a.pop() if not b[-1][0]: b.pop() if not a or not b: break while (t := a[-1][1]) % m == 0: if a[-1][0] > 1: a[-1][0] -= 1 else: a.pop() a.append([m, t // m]) while (t := b[-1][1]) % m == 0: if b[-1][0] > 1: b[-1][0] -= 1 else: b.pop() b.append([m, t // m]) if a[-1][1] != b[-1][1]: break print("No" if a or b else "Yes")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR WHILE NUMBER WHILE VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF VAR VAR WHILE BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR STRING STRING
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
T = int(input()) for i in range(T): n, m = list(map(int, input().split())) arr = list(map(int, input().split())) arr_2 = arr.copy() k = int(input()) brr = list(map(int, input().split())) brr_2 = brr.copy() for i in range(n): while arr_2[i] % m == 0: arr_2[i] //= m for i in range(k): while brr_2[i] % m == 0: brr_2[i] //= m pos_a = 0 stats_a = [] last = -1 for i in range(n): if arr_2[i] != last: last = arr_2[i] stats_a.append([last, arr[i]]) else: stats_a[-1][1] += arr[i] stats_b = [] last = -1 for i in range(k): if brr_2[i] != last: last = brr_2[i] stats_b.append([last, brr[i]]) else: stats_b[-1][1] += brr[i] if stats_b == stats_a: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR 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 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 FOR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR NUMBER NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
for _ in range(int(input())): n, m = map(int, input().split()) a = list(map(int, input().split())) k = int(input()) b = list(map(int, input().split())) i = 0 c = [1] * n while i < n: if a[i] % m == 0: temp = a[i] // m a[i] = temp c[i] = c[i] * m else: i += 1 i = 0 d = [1] * k while i < k: if b[i] % m == 0: temp = b[i] // m b[i] = temp d[i] = d[i] * m else: i += 1 i = 1 while i < n: if a[i] == a[i - 1]: c[i] = c[i] + c[i - 1] a.pop(i - 1) c.pop(i - 1) n -= 1 else: i += 1 i = 1 while i < k: if b[i] == b[i - 1]: d[i] = d[i] + d[i - 1] b.pop(i - 1) d.pop(i - 1) k -= 1 else: i += 1 if n != k: print("No") else: flag = True for i in range(n): if a[i] != b[i] or c[i] != d[i]: flag = False break if flag: print("Yes") else: print("No")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
def mi(): return map(int, input().split()) def li(): return list(mi()) def si(): return str(input()) def ni(): return int(input()) def find(x, m): count = 0 while x % m == 0: count += 1 x //= m return x, m**count def check(a, m): new = [[0, 0]] for i in a: temp = find(i, m) if new[-1][0] != temp[0]: new.append([temp[0], 0]) new[-1][1] += temp[1] return new for t in range(int(input())): n, m = mi() a = li() k = ni() b = li() if check(a, m) == check(b, m): print("Yes") else: print("No")
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR RETURN VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
def spread(num): times = 0 while num % m == 0: times += 1 num = int(num / m) return num, m**times def append(arr, x, times=1): if not arr or arr[-1][0] != x: arr.append([x, times]) else: arr[-1][1] += times def flatten(a): new_a = [] for x in a: if x % m == 0: num, times = spread(x) append(new_a, num, times) else: append(new_a, x, 1) return new_a tests = int(input()) for _ in range(tests): n, m = list(map(int, input().split())) a = list(map(int, input().split())) k = int(input()) b = list(map(int, input().split())) a = flatten(a) b = flatten(b) print("Yes" if a == b else "No")
FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR BIN_OP VAR VAR FUNC_DEF NUMBER IF VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER NUMBER VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING STRING
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
t = int(input()) for _ in range(t): n, m = [int(x) for x in input().split()] a0 = [int(x) for x in input().split()] k = int(input()) b0 = [int(x) for x in input().split()] a = [] b = [] for i in range(n): q = a0[i] j = 1 while q % m == 0: j *= m q //= m if i > 0 and q == a[-1][0]: a[-1][1] += j else: a.append([q, j]) for i in range(k): q = b0[i] j = 1 while q % m == 0: j *= m q //= m if i > 0 and q == b[-1][0]: b[-1][1] += j else: b.append([q, j]) if len(a) == len(b): s = True for i in range(len(a)): if (a[i][0] == b[i][0] and a[i][1] == b[i][1]) == False: s = False break if s: print("Yes") else: print("No") else: print("No")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
t = int(input()) for _ in range(t): n, m = map(int, input().split()) arra = list(map(int, input().split())) k = map(int, input().split()) arrb = list(map(int, input().split())) v = [] for q in [arra, arrb]: r = [] for i in q: tms = 1 vl = i while vl % m == 0: tms *= m vl //= m if len(r) > 0 and r[-1][0] == vl: r[-1][1] += tms else: r.append([vl, tms]) v.append(r) print("Yes" if v[0] == v[1] else "No")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN 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 FOR VAR LIST VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING STRING
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
def int_input(): a = int(input()) return a def int_list_input(): a = list(map(int, input().split())) return a def expand(a, m): b = [] for i in a: x = i while x % m == 0: x = x // m if b and b[-1][0] == x: b[-1][1] += i // x else: b.append([x, i // x]) return b def solve(): n, m = int_list_input() a = int_list_input() k = int_input() b = int_list_input() print("Yes" if expand(a, m) == expand(b, m) else "No") for _ in range(int(input())): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
t = int(input()) for _ in range(t): n, m = list(map(int, input().split())) a = list(map(int, input().split())) k = int(input()) b = list(map(int, input().split())) fl = True pointer_n, pointer_k = 0, 0 while pointer_n < n and pointer_k < k: if a[pointer_n] == b[pointer_k]: pointer_n += 1 pointer_k += 1 else: sum_n, sum_k = 0, 0 curr = a[pointer_n] while curr % m == 0: curr //= m rem = curr while pointer_n < n: curr = a[pointer_n] while curr % m == 0: curr //= m if curr == rem: sum_n += a[pointer_n] else: break pointer_n += 1 while pointer_k < k: curr = b[pointer_k] while curr % m == 0: curr //= m if curr == rem: sum_k += b[pointer_k] else: break pointer_k += 1 if sum_n != sum_k: fl = False print("No") break if fl: if pointer_n == n and pointer_k == k: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
t = int(input()) for _ in range(t): n, m = map(int, input().split()) lst = list(map(int, input().split())) nls = [] top = -1 for i in lst: cnt = 1 while i % m == 0: i //= m cnt *= m if nls and nls[top][0] == i: nls[top][1] += cnt else: top += 1 nls.append([i, cnt]) k = int(input()) lt = list(map(int, input().split())) nl = [] top = -1 for i in lt: cnt = 1 while i % m == 0: i //= m cnt *= m if nl and nl[top][0] == i: nl[top][1] += cnt else: top += 1 nl.append([i, cnt]) if nls == nl: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
def expand(a, m): b = [] for x in a: t = x while t % m == 0: t //= m if b and b[-1][0] == t: b[-1][1] += x // t else: b.append([t, x // t]) return b for _ in range(int(input())): _, m = map(int, input().split()) a = map(int, input().split()) input() b = map(int, input().split()) print("Yes" if expand(a, m) == expand(b, m) else "No")
FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR STRING STRING
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
def newList(nums, m, newnums): for num in nums: x = num while x % m == 0: x //= m if not newnums or newnums[-1][0] != x: newnums.append([x, num // x]) else: newnums[-1][1] += num // x return newnums for _ in range(int(input())): n, m = map(int, input().split()) nums = list(map(int, input().split())) k = int(input()) nums1 = list(map(int, input().split())) if newList(nums, m, []) == newList(nums1, m, []): print("Yes") else: print("No")
FUNC_DEF FOR VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR VAR VAR LIST EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
def decom(a, m): c = 1 while a % m == 0: a = a // m c = c * m return a, c for time in range(int(input())): t, m = map(int, input().split()) a = [int(k) for k in input().split()] t = input() b = [int(k) for k in input().split()] arep = [], [] brep = [], [] for j in a: d, c = decom(j, m) if len(arep[0]) > 0 and arep[0][-1] == d: arep[1][-1] += c else: arep[0].append(d) arep[1].append(c) for j in b: d, c = decom(j, m) if len(brep[0]) > 0 and brep[0][-1] == d: brep[1][-1] += c else: brep[0].append(d) brep[1].append(c) if brep == arep: print("Yes") else: print("No")
FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST ASSIGN VAR LIST LIST FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
t = int(input()) for tt in range(t): n, m = map(int, input().split()) a = list(map(int, input().split())) k = int(input()) b = list(map(int, input().split())) a1 = [] for i in range(n): q = a[i] x = 1 while q % m == 0: x *= m q //= m a1 += [[q, x]] b1 = [] for j in range(k): p = b[j] y = 1 while p % m == 0: y *= m p //= m b1 += [[p, y]] e = 0 f = 0 while e < n and f < k: if a1[e] == b1[f]: e += 1 f += 1 elif a1[e][0] == b1[f][0]: if a1[e][1] > b1[f][1]: a1[e][1] -= b1[f][1] f += 1 else: b1[f][1] -= a1[e][1] e += 1 else: print("No") break else: if e == n and f == k: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL 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 ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR LIST LIST VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR LIST LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
import sys def solve1(a, m): r = [] for x in a: z = 1 while x % m == 0: z *= m x //= m if r and r[-1][0] == x: r[-1][1] += z else: r.append([x, z]) return r def solve(): inp = sys.stdin.readline n, m = map(int, inp().split()) a = list(map(int, inp().split())) k = int(inp()) b = list(map(int, inp().split())) if solve1(a, m) == solve1(b, m): print("Yes") else: print("No") def main(): for i in range(int(sys.stdin.readline())): solve() main()
IMPORT FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR FUNC_DEF ASSIGN 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 FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Fishingprince is playing with an array $[a_1,a_2,\dots,a_n]$. He also has a magic number $m$. He can do the following two operations on it: Select $1\le i\le n$ such that $a_i$ is divisible by $m$ (that is, there exists an integer $t$ such that $m \cdot t = a_i$). Replace $a_i$ with $m$ copies of $\frac{a_i}{m}$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[2,3]$ and $i=1$, $a$ changes into $[1,1,3]$. Select $1\le i\le n-m+1$ such that $a_i=a_{i+1}=\dots=a_{i+m-1}$. Replace these $m$ elements with a single $m \cdot a_i$. The order of the other elements doesn't change. For example, when $m=2$ and $a=[3,2,2,3]$ and $i=2$, $a$ changes into $[3,4,3]$. Note that the array length might change during the process. The value of $n$ above is defined as the current length of the array (might differ from the $n$ in the input). Fishingprince has another array $[b_1,b_2,\dots,b_k]$. Please determine if he can turn $a$ into $b$ using any number (possibly zero) of operations. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\le n\le 5\cdot 10^4$, $2\le m\le 10^9$). The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le 10^9$). The third line of each test case contains one integer $k$ ($1\le k\le 5\cdot 10^4$). The fourth line of each test case contains $k$ integers $b_1,b_2,\ldots,b_k$ ($1\le b_i\le 10^9$). It is guaranteed that the sum of $n+k$ over all test cases does not exceed $2\cdot 10^5$. -----Output----- For each testcase, print Yes if it is possible to turn $a$ into $b$, and No otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Output Yes Yes No Yes No -----Note----- In the first test case of the sample, we can do the second operation with $i=2$: $[1,{2,2},4,2]\to [1,{4},4,2]$. In the second testcase of the sample, we can: do the second operation with $i=2$: $[1,{2,2},8,2,2]\to [1,{4},8,2,2]$. do the second operation with $i=4$: $[1,4,8,{2,2}]\to [1,4,8,{4}]$. do the first operation with $i=3$: $[1,4,{8},4]\to [1,4,{4,4},4]$. do the second operation with $i=2$: $[1,{4,4},4,4]\to [1,{8},4,4]$. do the second operation with $i=3$: $[1,8,{4,4}]\to [1,8,{8}]$. do the second operation with $i=2$: $[1,{8,8}]\to [1,{16}]$.
for _ in range(int(input())): n, m = map(int, input().split()) a = list(map(int, input().split())) k = int(input()) b = list(map(int, input().split())) def expand(arr, z): e = [] for x in arr: c = 1 while x % z == 0: c *= z x = x // z if len(e) != 0 and e[-1][0] == x: e[-1][1] += c else: e.append([x, c]) return e if expand(a, m) == expand(b, m): print("Yes") else: print("No")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Email address in Berland is a string of the form A@B, where A and B are arbitrary strings consisting of small Latin letters. Bob is a system administrator in «Bersoft» company. He keeps a list of email addresses of the company's staff. This list is as a large string, where all addresses are written in arbitrary order, separated by commas. The same address can be written more than once. Suddenly, because of unknown reasons, all commas in Bob's list disappeared. Now Bob has a string, where all addresses are written one after another without any separators, and there is impossible to determine, where the boundaries between addresses are. Unfortunately, on the same day his chief asked him to bring the initial list of addresses. Now Bob wants to disjoin addresses in some valid way. Help him to do that. Input The first line contains the list of addresses without separators. The length of this string is between 1 and 200, inclusive. The string consists only from small Latin letters and characters «@». Output If there is no list of the valid (according to the Berland rules) email addresses such that after removing all commas it coincides with the given string, output No solution. In the other case, output the list. The same address can be written in this list more than once. If there are several solutions, output any of them. Examples Input a@aa@a Output a@a,a@a Input a@a@a Output No solution Input @aa@a Output No solution
a = input() b = a.split("@") ans = [] flag = True for i in b[1:-1]: if len(i) < 2: flag = False break if len(b) < 2: flag = False if b[0] and b[-1] and flag: ind = 0 index_list = [] str_len = len(a) temp = "" j = 0 ans = [] while j != str_len: temp += a[j] if a[j] == "@": temp += a[j + 1] ans.append(temp) temp = "" j += 1 j += 1 ans[-1] = ans[-1] + temp print(",".join(ans)) else: print("No solution")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR VAR IF VAR VAR STRING VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING
Email address in Berland is a string of the form A@B, where A and B are arbitrary strings consisting of small Latin letters. Bob is a system administrator in «Bersoft» company. He keeps a list of email addresses of the company's staff. This list is as a large string, where all addresses are written in arbitrary order, separated by commas. The same address can be written more than once. Suddenly, because of unknown reasons, all commas in Bob's list disappeared. Now Bob has a string, where all addresses are written one after another without any separators, and there is impossible to determine, where the boundaries between addresses are. Unfortunately, on the same day his chief asked him to bring the initial list of addresses. Now Bob wants to disjoin addresses in some valid way. Help him to do that. Input The first line contains the list of addresses without separators. The length of this string is between 1 and 200, inclusive. The string consists only from small Latin letters and characters «@». Output If there is no list of the valid (according to the Berland rules) email addresses such that after removing all commas it coincides with the given string, output No solution. In the other case, output the list. The same address can be written in this list more than once. If there are several solutions, output any of them. Examples Input a@aa@a Output a@a,a@a Input a@a@a Output No solution Input @aa@a Output No solution
entrada = input() salida = "" lista = [] pos = -1 ultimapos = -1 rip = 0 for x in entrada: pos += 1 if x == "@": lista.append(pos) if ( len(entrada) < 3 or len(lista) == 0 or lista[0] == 0 or lista[-1] == len(entrada) - 1 ): print("No solution") else: for i in range(len(lista)): try: if lista[i + 1] - lista[i] > 2: salida += entrada[ultimapos + 1 : lista[i] + 2] + "," ultimapos = lista[i] + 1 else: rip = 1 break except IndexError: if lista[i] - lista[i - 1] > 2 or len(lista) == 1: salida += entrada[ultimapos + 1 :] + "," print(salida[: len(salida) - 1]) rip -= 2 if rip == 1: print("No solution")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER STRING ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING
Email address in Berland is a string of the form A@B, where A and B are arbitrary strings consisting of small Latin letters. Bob is a system administrator in «Bersoft» company. He keeps a list of email addresses of the company's staff. This list is as a large string, where all addresses are written in arbitrary order, separated by commas. The same address can be written more than once. Suddenly, because of unknown reasons, all commas in Bob's list disappeared. Now Bob has a string, where all addresses are written one after another without any separators, and there is impossible to determine, where the boundaries between addresses are. Unfortunately, on the same day his chief asked him to bring the initial list of addresses. Now Bob wants to disjoin addresses in some valid way. Help him to do that. Input The first line contains the list of addresses without separators. The length of this string is between 1 and 200, inclusive. The string consists only from small Latin letters and characters «@». Output If there is no list of the valid (according to the Berland rules) email addresses such that after removing all commas it coincides with the given string, output No solution. In the other case, output the list. The same address can be written in this list more than once. If there are several solutions, output any of them. Examples Input a@aa@a Output a@a,a@a Input a@a@a Output No solution Input @aa@a Output No solution
ans, capt = [], "" for x in input(): if x == "@": if capt and capt[-1] != "@": capt += x else: ans = [] break elif capt and capt[-1] == "@": ans.append(capt + x) capt = "" else: capt += x if ans: ans[-1] += capt print(",".join(ans)) else: print("No solution")
ASSIGN VAR VAR LIST STRING FOR VAR FUNC_CALL VAR IF VAR STRING IF VAR VAR NUMBER STRING VAR VAR ASSIGN VAR LIST IF VAR VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR STRING VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING
Email address in Berland is a string of the form A@B, where A and B are arbitrary strings consisting of small Latin letters. Bob is a system administrator in «Bersoft» company. He keeps a list of email addresses of the company's staff. This list is as a large string, where all addresses are written in arbitrary order, separated by commas. The same address can be written more than once. Suddenly, because of unknown reasons, all commas in Bob's list disappeared. Now Bob has a string, where all addresses are written one after another without any separators, and there is impossible to determine, where the boundaries between addresses are. Unfortunately, on the same day his chief asked him to bring the initial list of addresses. Now Bob wants to disjoin addresses in some valid way. Help him to do that. Input The first line contains the list of addresses without separators. The length of this string is between 1 and 200, inclusive. The string consists only from small Latin letters and characters «@». Output If there is no list of the valid (according to the Berland rules) email addresses such that after removing all commas it coincides with the given string, output No solution. In the other case, output the list. The same address can be written in this list more than once. If there are several solutions, output any of them. Examples Input a@aa@a Output a@a,a@a Input a@a@a Output No solution Input @aa@a Output No solution
import sys email_string = sys.stdin.readline().strip() n_characters_before_at, cur_string, all_emails = 0, "", [] found_at = False i = 0 if email_string[-1] == "@": print("No solution") sys.exit(0) while i < len(email_string): if email_string[i] == "@": found_at = True if n_characters_before_at == 0: print("No solution") sys.exit(0) else: cur_string += email_string[i] i += 1 if email_string[i] == "@": print("No solution") sys.exit(0) cur_string += email_string[i] all_emails.append(cur_string) n_characters_before_at = 0 cur_string = "" else: n_characters_before_at += 1 cur_string += email_string[i] i += 1 if len(all_emails) == 0: print("No solution") sys.exit(0) all_emails[-1] += cur_string for index, email in enumerate(all_emails): if index != 0: sys.stdout.write(",") sys.stdout.write(email) print("")
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER STRING LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING VAR NUMBER VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Email address in Berland is a string of the form A@B, where A and B are arbitrary strings consisting of small Latin letters. Bob is a system administrator in «Bersoft» company. He keeps a list of email addresses of the company's staff. This list is as a large string, where all addresses are written in arbitrary order, separated by commas. The same address can be written more than once. Suddenly, because of unknown reasons, all commas in Bob's list disappeared. Now Bob has a string, where all addresses are written one after another without any separators, and there is impossible to determine, where the boundaries between addresses are. Unfortunately, on the same day his chief asked him to bring the initial list of addresses. Now Bob wants to disjoin addresses in some valid way. Help him to do that. Input The first line contains the list of addresses without separators. The length of this string is between 1 and 200, inclusive. The string consists only from small Latin letters and characters «@». Output If there is no list of the valid (according to the Berland rules) email addresses such that after removing all commas it coincides with the given string, output No solution. In the other case, output the list. The same address can be written in this list more than once. If there are several solutions, output any of them. Examples Input a@aa@a Output a@a,a@a Input a@a@a Output No solution Input @aa@a Output No solution
s = input() au = [pos for pos, char in enumerate(s) if char == "@"] dis = [(au[i + 1] - au[i]) for i in range(len(au) - 1)] dis = [(1) for i in dis if i <= 2] if len(au) == 0 or au[0] == 0 or au[-1] == len(s) - 1 or sum(dis) > 0: print("No solution") elif len(au) == 1: print(s) else: res = [] i = 0 j = 0 m = au[j] + 2 while j < len(au): res.append(s[i:m]) i = m if i > len(s): break j += 1 if j == len(au) - 1: m = len(s) + 1 else: m = au[j] + 2 print(*res, sep=",")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
Email address in Berland is a string of the form A@B, where A and B are arbitrary strings consisting of small Latin letters. Bob is a system administrator in «Bersoft» company. He keeps a list of email addresses of the company's staff. This list is as a large string, where all addresses are written in arbitrary order, separated by commas. The same address can be written more than once. Suddenly, because of unknown reasons, all commas in Bob's list disappeared. Now Bob has a string, where all addresses are written one after another without any separators, and there is impossible to determine, where the boundaries between addresses are. Unfortunately, on the same day his chief asked him to bring the initial list of addresses. Now Bob wants to disjoin addresses in some valid way. Help him to do that. Input The first line contains the list of addresses without separators. The length of this string is between 1 and 200, inclusive. The string consists only from small Latin letters and characters «@». Output If there is no list of the valid (according to the Berland rules) email addresses such that after removing all commas it coincides with the given string, output No solution. In the other case, output the list. The same address can be written in this list more than once. If there are several solutions, output any of them. Examples Input a@aa@a Output a@a,a@a Input a@a@a Output No solution Input @aa@a Output No solution
A = input().split("@") if len(A[0]) == 0 or len(A[-1]) == 0 or any([(len(x) < 2) for x in A[1:-1]]): print("No solution") elif len(A) == 1: print("No solution") elif len(A) == 2: print("@".join(A)) else: result = [A[0] + "@" + A[1][0]] for i in range(1, len(A) - 2): result += [A[i][1:] + "@" + A[i + 1][0]] result += [A[-2][1:] + "@" + A[-1]] print(",".join(result))
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR LIST BIN_OP BIN_OP VAR NUMBER STRING VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR LIST BIN_OP BIN_OP VAR VAR NUMBER STRING VAR BIN_OP VAR NUMBER NUMBER VAR LIST BIN_OP BIN_OP VAR NUMBER NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Email address in Berland is a string of the form A@B, where A and B are arbitrary strings consisting of small Latin letters. Bob is a system administrator in «Bersoft» company. He keeps a list of email addresses of the company's staff. This list is as a large string, where all addresses are written in arbitrary order, separated by commas. The same address can be written more than once. Suddenly, because of unknown reasons, all commas in Bob's list disappeared. Now Bob has a string, where all addresses are written one after another without any separators, and there is impossible to determine, where the boundaries between addresses are. Unfortunately, on the same day his chief asked him to bring the initial list of addresses. Now Bob wants to disjoin addresses in some valid way. Help him to do that. Input The first line contains the list of addresses without separators. The length of this string is between 1 and 200, inclusive. The string consists only from small Latin letters and characters «@». Output If there is no list of the valid (according to the Berland rules) email addresses such that after removing all commas it coincides with the given string, output No solution. In the other case, output the list. The same address can be written in this list more than once. If there are several solutions, output any of them. Examples Input a@aa@a Output a@a,a@a Input a@a@a Output No solution Input @aa@a Output No solution
import sys def main(): str = input() dogs = [] cnt = 0 n = len(str) for i in range(n): if str[i] == "@": cnt += 1 dogs.append(i) ok = 1 k = len(dogs) for i in range(k - 1): if dogs[i + 1] - dogs[i] < 3: ok = 0 break if not ok or str[0] == "@" or str[-1] == "@" or cnt == 0: print("No solution") return 0 if cnt == 1: print(str) return 0 ans = [] adr = "" j = 0 for i in range(n - 2): adr += str[i] if str[i + 2] == "@": ans.append(adr) j = i adr = "" if j > 0: j += 1 ans.append(str[j:]) for i in range(len(ans) - 1): if ans[i].find("@") == -1: ans[i + 1] = ans[i] + ans[i + 1] ans.pop(i) print(",".join(ans)) main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
Email address in Berland is a string of the form A@B, where A and B are arbitrary strings consisting of small Latin letters. Bob is a system administrator in «Bersoft» company. He keeps a list of email addresses of the company's staff. This list is as a large string, where all addresses are written in arbitrary order, separated by commas. The same address can be written more than once. Suddenly, because of unknown reasons, all commas in Bob's list disappeared. Now Bob has a string, where all addresses are written one after another without any separators, and there is impossible to determine, where the boundaries between addresses are. Unfortunately, on the same day his chief asked him to bring the initial list of addresses. Now Bob wants to disjoin addresses in some valid way. Help him to do that. Input The first line contains the list of addresses without separators. The length of this string is between 1 and 200, inclusive. The string consists only from small Latin letters and characters «@». Output If there is no list of the valid (according to the Berland rules) email addresses such that after removing all commas it coincides with the given string, output No solution. In the other case, output the list. The same address can be written in this list more than once. If there are several solutions, output any of them. Examples Input a@aa@a Output a@a,a@a Input a@a@a Output No solution Input @aa@a Output No solution
string = input() lst = string.split("@") if len(lst) == 1: print("No solution") exit() middle = lst[1:-1] if not (lst[0].isalpha() and lst[-1].isalpha()): print("No solution") exit() if len(lst) == 2: print(string) exit() if not all(list(map(lambda x: len(x) > 1 and x.isalpha(), middle))): print("No solution") exit() first = lst[0] emails = [] for i in middle: last = i[0] emails.append(first + "@" + last) first = i[1:] emails.append(first + "@" + lst[-1]) print(",".join(emails))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER IF FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR STRING VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Email address in Berland is a string of the form A@B, where A and B are arbitrary strings consisting of small Latin letters. Bob is a system administrator in «Bersoft» company. He keeps a list of email addresses of the company's staff. This list is as a large string, where all addresses are written in arbitrary order, separated by commas. The same address can be written more than once. Suddenly, because of unknown reasons, all commas in Bob's list disappeared. Now Bob has a string, where all addresses are written one after another without any separators, and there is impossible to determine, where the boundaries between addresses are. Unfortunately, on the same day his chief asked him to bring the initial list of addresses. Now Bob wants to disjoin addresses in some valid way. Help him to do that. Input The first line contains the list of addresses without separators. The length of this string is between 1 and 200, inclusive. The string consists only from small Latin letters and characters «@». Output If there is no list of the valid (according to the Berland rules) email addresses such that after removing all commas it coincides with the given string, output No solution. In the other case, output the list. The same address can be written in this list more than once. If there are several solutions, output any of them. Examples Input a@aa@a Output a@a,a@a Input a@a@a Output No solution Input @aa@a Output No solution
p = input() s = p.split("@") ans = "" ans2 = [] if p.count("@") == 0: print("No solution") else: for j in range(1, len(s) - 1): if len(s[j]) <= 1: ans = "No solution" if s[0] == "" or s[-1] == "": ans = "No solution" if ans != "": print(ans) else: email = "" for j in range(len(p)): email += p[j] if len(email) >= 2: if email[-2] == "@": ans2.append(email) email = "" if email != "": ans2[-1] = ans2[-1] + email for j in range(len(ans2) - 1): print(ans2[j], end=",") print(ans2[-1])
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR LIST IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR STRING IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR STRING ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
Email address in Berland is a string of the form A@B, where A and B are arbitrary strings consisting of small Latin letters. Bob is a system administrator in «Bersoft» company. He keeps a list of email addresses of the company's staff. This list is as a large string, where all addresses are written in arbitrary order, separated by commas. The same address can be written more than once. Suddenly, because of unknown reasons, all commas in Bob's list disappeared. Now Bob has a string, where all addresses are written one after another without any separators, and there is impossible to determine, where the boundaries between addresses are. Unfortunately, on the same day his chief asked him to bring the initial list of addresses. Now Bob wants to disjoin addresses in some valid way. Help him to do that. Input The first line contains the list of addresses without separators. The length of this string is between 1 and 200, inclusive. The string consists only from small Latin letters and characters «@». Output If there is no list of the valid (according to the Berland rules) email addresses such that after removing all commas it coincides with the given string, output No solution. In the other case, output the list. The same address can be written in this list more than once. If there are several solutions, output any of them. Examples Input a@aa@a Output a@a,a@a Input a@a@a Output No solution Input @aa@a Output No solution
lista = input().split("@") ver = True if len(lista) < 2: ver = False for i in range(1, len(lista) - 1): if len(lista[i]) < 2: ver = False break lista[i] = lista[i][0] + "," + lista[i][1:] if not lista[0] or not lista[-1] or not ver: print("No solution") else: print("@".join(lista))
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Email address in Berland is a string of the form A@B, where A and B are arbitrary strings consisting of small Latin letters. Bob is a system administrator in «Bersoft» company. He keeps a list of email addresses of the company's staff. This list is as a large string, where all addresses are written in arbitrary order, separated by commas. The same address can be written more than once. Suddenly, because of unknown reasons, all commas in Bob's list disappeared. Now Bob has a string, where all addresses are written one after another without any separators, and there is impossible to determine, where the boundaries between addresses are. Unfortunately, on the same day his chief asked him to bring the initial list of addresses. Now Bob wants to disjoin addresses in some valid way. Help him to do that. Input The first line contains the list of addresses without separators. The length of this string is between 1 and 200, inclusive. The string consists only from small Latin letters and characters «@». Output If there is no list of the valid (according to the Berland rules) email addresses such that after removing all commas it coincides with the given string, output No solution. In the other case, output the list. The same address can be written in this list more than once. If there are several solutions, output any of them. Examples Input a@aa@a Output a@a,a@a Input a@a@a Output No solution Input @aa@a Output No solution
s = input() l = [] n = len(s) if s[n - 1] == "@" or s[0] == "@": print("No solution") else: flag = 0 d = 0 e = 0 for i in range(n): l.append(0) for i in range(n): if s[i] == "@": l[i] = l[i] + 1 e = 1 for i in range(n): if flag == 0 and l[i] > 0: p = i flag = 1 elif flag == 1 and l[i] > 0: q = i - p if q < 3: print("No solution") d = 1 break else: p = i if d == 0 and e == 1: c = 0 for i in range(n): if l[i] == 0 and c == 0: print(s[i], end="") elif l[i] > 0: print(s[i], end="") c = 1 elif l[i] == 0 and c == 1: print(s[i], end="") if i + 2 < n and s[i + 2] == "@": print(",", end="") c = 0 elif e == 0: print("No solution")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR STRING STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING
Email address in Berland is a string of the form A@B, where A and B are arbitrary strings consisting of small Latin letters. Bob is a system administrator in «Bersoft» company. He keeps a list of email addresses of the company's staff. This list is as a large string, where all addresses are written in arbitrary order, separated by commas. The same address can be written more than once. Suddenly, because of unknown reasons, all commas in Bob's list disappeared. Now Bob has a string, where all addresses are written one after another without any separators, and there is impossible to determine, where the boundaries between addresses are. Unfortunately, on the same day his chief asked him to bring the initial list of addresses. Now Bob wants to disjoin addresses in some valid way. Help him to do that. Input The first line contains the list of addresses without separators. The length of this string is between 1 and 200, inclusive. The string consists only from small Latin letters and characters «@». Output If there is no list of the valid (according to the Berland rules) email addresses such that after removing all commas it coincides with the given string, output No solution. In the other case, output the list. The same address can be written in this list more than once. If there are several solutions, output any of them. Examples Input a@aa@a Output a@a,a@a Input a@a@a Output No solution Input @aa@a Output No solution
s = input() a = s.split("@") n = len(a) valid = True if n == 1: valid = False username = [] host = [] for i in range(n): if len(a[i]) == 0: valid = False break if i == 0: username.append(a[i]) elif i == n - 1: host.append(a[i]) else: if len(a[i]) < 2: valid = False break host.append(a[i][0]) username.append(a[i][1:]) if not valid: print("No solution") else: emails = [(username[i] + "@" + host[i]) for i in range(n - 1)] print(",".join(emails))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Email address in Berland is a string of the form A@B, where A and B are arbitrary strings consisting of small Latin letters. Bob is a system administrator in «Bersoft» company. He keeps a list of email addresses of the company's staff. This list is as a large string, where all addresses are written in arbitrary order, separated by commas. The same address can be written more than once. Suddenly, because of unknown reasons, all commas in Bob's list disappeared. Now Bob has a string, where all addresses are written one after another without any separators, and there is impossible to determine, where the boundaries between addresses are. Unfortunately, on the same day his chief asked him to bring the initial list of addresses. Now Bob wants to disjoin addresses in some valid way. Help him to do that. Input The first line contains the list of addresses without separators. The length of this string is between 1 and 200, inclusive. The string consists only from small Latin letters and characters «@». Output If there is no list of the valid (according to the Berland rules) email addresses such that after removing all commas it coincides with the given string, output No solution. In the other case, output the list. The same address can be written in this list more than once. If there are several solutions, output any of them. Examples Input a@aa@a Output a@a,a@a Input a@a@a Output No solution Input @aa@a Output No solution
s = input().split("@") if len(s) > 1 and s[0] and s[-1] and min(map(len, s[1:-1] + ["__"])) > 1: for i in range(1, len(s) - 1): s[i] = s[i][0] + "," + s[i][1:] print("@".join(s)) else: print("No solution")
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER LIST STRING NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING
Email address in Berland is a string of the form A@B, where A and B are arbitrary strings consisting of small Latin letters. Bob is a system administrator in «Bersoft» company. He keeps a list of email addresses of the company's staff. This list is as a large string, where all addresses are written in arbitrary order, separated by commas. The same address can be written more than once. Suddenly, because of unknown reasons, all commas in Bob's list disappeared. Now Bob has a string, where all addresses are written one after another without any separators, and there is impossible to determine, where the boundaries between addresses are. Unfortunately, on the same day his chief asked him to bring the initial list of addresses. Now Bob wants to disjoin addresses in some valid way. Help him to do that. Input The first line contains the list of addresses without separators. The length of this string is between 1 and 200, inclusive. The string consists only from small Latin letters and characters «@». Output If there is no list of the valid (according to the Berland rules) email addresses such that after removing all commas it coincides with the given string, output No solution. In the other case, output the list. The same address can be written in this list more than once. If there are several solutions, output any of them. Examples Input a@aa@a Output a@a,a@a Input a@a@a Output No solution Input @aa@a Output No solution
n = input() n = n.strip() naslovi = [] try: pos = True A = "" i = 0 while i < len(n): if n[i] == "@": if len(A) != 0: A += n[i] if n[i + 1] != "@": A += n[i + 1] naslovi.append(A) A = "" i = i + 2 else: pos = False break else: pos = False break else: A += n[i] i = i + 1 naslovi[-1] += A if pos: print(",".join(naslovi)) else: print("No solution") except: print("No solution")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING