description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
for _ in range(int(input())): n = int(input()) a = [int(x) for x in input().split()] if n == 1: if a[0] == 1: a = [1, 1] else: a = [1, 1, a[0] - 1] elif n == 2: if a[0] == 1: a = [a[0], a[1] + 1] else: a = [1, a[1] + 1, a[0] - 1] elif n % 2 == 1: if a[-2] == 1: a[-3:] = [a[-3] + 1, 1, a[-1] - 1] else: a[-2:] = [a[-2] - 1, 1, 1, a[-1] - 1] elif a[-3] == 1: a[-4:] = [a[-4] + 1, a[-1] + 1, a[-2] - 1] else: a[-3:] = [a[-3] - 1, 1, a[-1] + 1, a[-2] - 1] if a[-1] == 0: del a[-1] print(len(a)) print(*a)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
class Solution(object): def one(self, nums): if nums[0] - 1 == 0: return 2, [1, 1] else: return 3, [1, 1, nums[0] - 1] def two(self, nums): if nums[0] - 1 == 0: return 2, [1, nums[1] + 1] else: return 3, [1, nums[1] + 1, nums[0] - 1] def odd(self, num, nums): if nums[-1] - 1 == 0: if nums[-2] - 1 == 0: del nums[-2] nums[-2] += 1 else: nums.insert(-1, 1) nums[-3] -= 1 else: nums[-1] -= 1 nums.insert(-1, 1) if nums[-3] - 1 == 0: del nums[-3] nums[-3] += 1 else: nums[-3] -= 1 nums.insert(-2, 1) return len(nums), nums def even(self, num, nums): if nums[-2] - 1 == 0: del nums[-2] nums[-1] += 1 if nums[-2] - 1 == 0: del nums[-2] nums[-2] += 1 else: nums.insert(-1, 1) nums[-3] -= 1 else: nums[-1] += 1 nums.append(nums[-2] - 1) del nums[-3] if nums[-3] - 1 == 0: nums[-4] += 1 del nums[-3] else: nums[-3] -= 1 nums.insert(-2, 1) return len(nums), nums def solve(self, num, nums): if num == 1: return self.one(nums) if num == 2: return self.two(nums) if num % 2 == 1: return self.odd(num, nums) else: return self.even(num, nums) solution = Solution() total = int(input("")) result1 = 0 result2 = [] for index in range(total): num = int(input("")) numsString = input("") nums = list(map(int, str.split(numsString))) result1, result2 = solution.solve(num, nums) print(result1) print(" ".join(str(x) for x in result2))
CLASS_DEF VAR FUNC_DEF IF BIN_OP VAR NUMBER NUMBER NUMBER RETURN NUMBER LIST NUMBER NUMBER RETURN NUMBER LIST NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF IF BIN_OP VAR NUMBER NUMBER NUMBER RETURN NUMBER LIST NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER LIST NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF IF BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) l = [1000] + l if n % 2: l = l[:-2] + [l[-2] - 1, 1, 1, l[-1] - 1] else: l = l[:-3] + [l[-3] - 1, 1, l[-1] + 1, l[-2] - 1] while 1: for i in range(1, len(l)): if l[i] == 0: if i == len(l) - 1: l = l[:-1] else: l = l[: i - 1] + [l[i - 1] + l[i + 1]] + l[i + 2 :] break else: break print(len(l) - 1) print(" ".join(map(str, l[1:])))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER WHILE NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER LIST BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
def findC(ar): N = len(ar) if N == 1: if ar[0] > 1: return [1, 1, ar[0] - 1] else: return [1, 1] if N == 2: if ar[0] > 1: return [1, ar[1] + 1, ar[0] - 1] else: return [ar[0], ar[1] + 1] if N % 2 == 1: zeros = ar[-2] ones = ar[-1] newZeros = zeros - 1 newOnes = ones - 1 if newZeros == 0 and newOnes == 0: del ar[-1] ar[-2] += 1 return ar if newZeros == 0 and newOnes > 0: ar[-3] += 1 ar[-1] -= 1 return ar if newZeros > 0 and newOnes == 0: ar[-2] -= 1 ar[-1] = 1 ar.append(1) return ar ar[-2] = newZeros ar[-1] = 1 ar.append(1) ar.append(newOnes) return ar else: if ar[-3] > 1: ar[-3] -= 1 ones = ar[-2] - 1 zeros = ar[-1] + 1 ar[-2] = 1 ar[-1] = zeros if ones > 0: ar.append(ones) return ar ar[-4] += 1 ar[-3] = ar[-1] + 1 ones = ar[-2] - 1 del ar[-1] if ones > 0: ar[-1] = ones else: del ar[-1] return ar T = int(input().strip()) for t in range(T): n = int(input().strip()) ar = list(map(int, input().strip().split(" "))) ar = findC(ar) print(len(ar)) sOut = str(ar[0]) for i in range(1, len(ar)): sOut += " " + str(ar[i]) print(sOut)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER NUMBER RETURN LIST NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN LIST NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER RETURN LIST NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN LIST VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER NUMBER RETURN VAR IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN VAR IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
for _ in range(int(input().strip())): input() integers = list(map(int, input().strip().split())) if len(integers) > 2: if len(integers) % 2 != 0: a = integers[-3] b = integers[-2] c = integers[-1] if b > 1 and c > 1: integers.pop() integers.pop() integers.append(b - 1) integers.append(1) integers.append(1) integers.append(c - 1) elif b > 1 and c == 1: integers.pop() integers.pop() integers.append(b - 1) integers.append(1) integers.append(1) elif b == 1 and c > 1: integers.pop() integers.pop() integers.pop() integers.append(a + 1) integers.append(1) integers.append(c - 1) else: integers.pop() integers.pop() integers.pop() integers.append(a + 1) integers.append(1) else: a = integers[-4] b = integers[-3] c = integers[-2] d = integers[-1] if b > 1 and c > 1: integers.pop() integers.pop() integers.pop() integers.append(b - 1) integers.append(1) integers.append(d + 1) integers.append(c - 1) elif b > 1 and c == 1: integers.pop() integers.pop() integers.pop() integers.append(b - 1) integers.append(1) integers.append(d + 1) elif b == 1 and c > 1: integers.pop() integers.pop() integers.pop() integers.pop() integers.append(a + 1) integers.append(d + 1) integers.append(c - 1) else: integers.pop() integers.pop() integers.pop() integers.pop() integers.append(a + 1) integers.append(d + 1) elif len(integers) == 2: b = integers.pop() a = integers.pop() if a > 1: integers.append(1) integers.append(b + 1) integers.append(a - 1) else: integers.append(1) integers.append(b + 1) else: a = integers.pop() if a > 1: integers.append(1) integers.append(1) integers.append(a - 1) else: integers.append(1) integers.append(1) print(len(integers)) print(" ".join(str(integer) for integer in integers))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
t = int(input()) while t > 0: t -= 1 n = int(input()) a = list(map(int, input().split())) if n == 1: if a[0] == 1: a.append(1) else: a[0] -= 1 a = [1, 1] + a print(len(a)) print(" ".join(map(str, a))) continue if n == 2: if a[0] == 1: a[1] += 1 else: a.append(a[0] - 1) a[0] = 1 a[1] += 1 print(len(a)) print(" ".join(map(str, a))) continue if n % 2 == 0: if a[-2] == 1 and a[-3] == 1: a[-4] += a[-2] a[-3] += a[-1] a.pop() a.pop() elif a[-2] == 1: a[-1] += 1 a[-3] -= 1 elif a[-3] == 1: zeros = a.pop() + 1 ones = a.pop() - 1 a.pop() a[-1] += 1 a.append(zeros) a.append(ones) else: zeros = a.pop() + 1 ones = a.pop() - 1 a[-1] -= 1 a.append(1) a.append(zeros) a.append(ones) else: ones = a.pop() ones -= 1 if a[-1] == 1: a[-2] += 1 else: a[-1] -= 1 a.append(1) a.append(1) if ones > 0: a.append(ones) print(len(a)) print(" ".join(map(str, a)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
cases = int(input().strip()) while cases: n = int(input().strip()) a = list(map(int, input().strip().split())) offset = 0 if n == 1: m = 3 c = [1, 1, a[0] - 1] elif n == 2: m = 3 c = [1, a[1] + 1, a[0] - 1] else: while n - offset > 4: offset += 2 if n - offset == 3: a.append(0) c = [] c.extend(a[:offset]) a_rt = a[offset:] if a_rt[1] > 1 and a_rt[2] > 1: c.extend([a_rt[0], a_rt[1] - 1, 1, a_rt[3] + 1, a_rt[2] - 1]) elif a_rt[1] > 1 and a_rt[2] == 1: c.extend([a_rt[0], a_rt[1] - 1, 1, a_rt[3] + 1]) elif a_rt[1] == 1 and a_rt[2] > 1: c.extend([a_rt[0] + 1, a_rt[3] + 1, a_rt[2] - 1]) else: c.extend([a_rt[0] + 1, a_rt[3] + 1]) while c[-1] == 0: c.pop() m = len(c) print(m) out_line = " ".join(map(str, c)) while not out_line[-1].isdigit(): out_line.strip() print(out_line) cases -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
t = int(input().strip()) for i in range(t): n = int(input().strip()) arr = [int(n) for n in input().split()] if len(arr) % 2 == 0: arr[n - 1] += 1 temp = arr[n - 2] - 1 if temp != 0: arr[n - 2] = 1 if n > 3: arr[n - 3] -= 1 arr.extend([temp]) elif n > 3: arr[n - 3] -= 1 elif n == 1: arr[n - 1] -= 1 arr.insert(n - 1, 1) arr.insert(n - 1, 1) else: arr[n - 1] -= 1 arr[n - 2] -= 1 arr.insert(n - 1, 1) arr.insert(n - 1, 1) if arr[len(arr) - 1] == 0: del arr[len(arr) - 1] for x in range(len(arr)): if arr[x] == 0: arr[x - 1] += arr[x + 1] del arr[x + 1] del arr[x] break print(len(arr)) print(*arr)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
import sys def solve(aa): print("aa=%s" % aa, file=sys.stderr) i = len(aa) - 1 if i % 2 == 1: i -= 1 print("i=%d" % i, file=sys.stderr) if i == 0: cache = [1, 1] + [aa[i] - 1] + aa[i + 1 :] else: cache = aa[: i - 1] + [aa[i - 1] - 1] + [1, 1] + [aa[i] - 1] + aa[i + 1 :] print("cache=%s" % cache, file=sys.stderr) if i == len(aa) - 2: ones, zeros = cache[-2:] print("ones=%d zeros=%d" % (ones, zeros), file=sys.stderr) cache = cache[:-2] + [0, zeros, ones] print("cache=%s" % cache, file=sys.stderr) new_cache, r = [], 0 for i in range(0, len(cache) - 1, 2): ones, zeros = cache[i : i + 2] if zeros == 0: r += ones else: new_cache.append(ones + r) new_cache.append(zeros) r = 0 if len(cache) % 2 == 1: v = cache[-1] + r if v > 0: new_cache.append(v) cache = new_cache print("cache=%s" % cache, file=sys.stderr) new_cache, r = cache[:1], 0 for i in range(1, len(cache) - 1, 2): zeros, ones = cache[i : i + 2] if ones == 0: r += zeros else: new_cache.append(zeros + r) new_cache.append(ones) r = 0 if len(cache) % 2 == 0: v = cache[-1] + r if v > 0: new_cache.append(v) cache = new_cache print("cache=%s" % cache, file=sys.stderr) return cache t = int(input()) for _ in range(t): _ = int(input()) aa = list(map(int, input().split())) bb = solve(aa) print("------", file=sys.stderr) print(len(bb)) print(" ".join(map(str, bb)))
IMPORT FUNC_DEF EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER NUMBER LIST BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER LIST NUMBER NUMBER LIST BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER LIST NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR ASSIGN VAR VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split(" "))) if len(a) == 1: a = [1] + [1] + [a[0] - 1] elif len(a) == 2: a = [1] + [a[1] + 1] + [a[0] - 1] elif len(a) == 3: a = [a[0]] + [a[1] - 1] + [1] + [1] + [a[2] - 1] elif len(a) == 4: a = [a[0]] + [a[1] - 1] + [1] + [a[3] + 1] + [a[2] - 1] elif len(a) % 2 == 1: a[-2] -= 1 a = a[:-1] + [1] + [1] + [a[-1] - 1] else: a[-3] -= 1 a = a[:-2] + [1] + [a[-1] + 1] + [a[-2] - 1] for j in range(len(a) - 1, max(-1, len(a) - 5), -1): if a[j] == 0: if j == len(a) - 1: a = a[:-1] else: a = a[: j - 1] + [a[j - 1] + 1] + a[j + 2 :] print(len(a)) print(" ".join(map(str, a)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER LIST NUMBER LIST BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER LIST BIN_OP VAR NUMBER NUMBER LIST BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP LIST VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER LIST NUMBER LIST NUMBER LIST BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP LIST VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER LIST NUMBER LIST BIN_OP VAR NUMBER NUMBER LIST BIN_OP VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER LIST NUMBER LIST NUMBER LIST BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER LIST NUMBER LIST BIN_OP VAR NUMBER NUMBER LIST BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
for t in range(int(input())): n = int(input()) a = list(map(int, input().split()[:n])) ones = 0 if n == 1: a[0] -= 1 if a[0] == 0: a.pop() a.insert(0, 1) a.insert(0, 1) elif n == 2: a[0] -= 1 a[1] += 1 a[0], a[1] = a[1], a[0] if a[1] == 0: a.pop() a.insert(0, 1) elif n % 2 == 0: ones = n - 2 if a[ones] == 1: if a[ones - 1] == 1: a[ones - 2] += 1 a[ones + 1] += 1 a.pop(ones - 1) a.pop(ones - 1) else: a[ones - 1] -= 1 a[ones + 1] += 1 elif a[ones - 1] == 1: a[ones - 2] += 1 a[ones] -= 1 a[ones + 1] += 1 a[ones], a[ones + 1] = a[ones + 1], a[ones] a.pop(ones - 1) else: a[ones - 1] -= 1 a[ones] -= 1 a[ones + 1] += 1 a[ones], a[ones + 1] = a[ones + 1], a[ones] a.insert(ones, 1) else: ones = n - 1 if a[ones] == 1: if a[ones - 1] == 1: a[ones - 2] += 1 a.pop(ones) else: a[ones - 1] -= 1 a.append(1) elif a[ones - 1] == 1: a[ones] -= 1 a[ones - 2] += 1 else: a[ones] -= 1 a[ones - 1] -= 1 a.insert(ones, 1) a.insert(ones, 1) print(len(a)) print(" ".join(str(x) for x in a))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
T = int(input().strip()) N = [] S = [] for t in range(0, T): n = int(input().strip()) seq = list(map(lambda x: int(x), input().strip().split(" "))) N.append(n) S.append(seq) def extzero(seq_): ones = 0 n = len(seq_) for j in range(0, n, 2): ones += seq_[j] zeros = 0 for j in range(1, n, 2): zeros += seq_[j] if ones == 1: return [1, zeros + 1] return [1, zeros + 1, ones - 1] for t in range(0, T): n = N[t] seq = S[t] ones = 0 if n % 2 == 0: i = n - 2 else: i = n - 1 end_seq = seq[i - n :] start_seq = seq[: i - n] end_seq = extzero(end_seq) if len(start_seq) > 0: if start_seq[-1] == 1: end_seq[0] += start_seq[-2] start_seq = start_seq[:-2] else: start_seq[-1] -= 1 start_seq.extend(end_seq) print(len(start_seq)) print(str(start_seq).strip("[]").replace(",", ""))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR NUMBER RETURN LIST NUMBER BIN_OP VAR NUMBER RETURN LIST NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR STRING STRING STRING
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
for _ in range(int(input())): n = int(input()) A = [int(i) for i in input().strip().split()] if n == 1: A.append(1) A.append(A[0] - 1) A[0] = 1 elif n == 2: A.append(A[0] - 1) A[1] += 1 A[0] = 1 else: if n % 2: A.append(0) if A[-3] == 1: A[-2] -= 1 A[-4] += 1 A[-3] += A[-1] del A[-1] else: A[-3] -= 1 A.append(A[-2] - 1) A[-2] += 1 A[-3] = 1 if A[-1] == 0: del A[-1] print(len(A)) print(" ".join(map(str, A)))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
T = int(input()) for _ in range(T): N = int(input()) A = [0, 1] + list(map(int, input().split())) l1 = N + 1 & -2 XL = A[: l1 - 1] X = [A[l1 - 1] - 1, 1, 1, A[l1] - 1] XR = A[l1 + 1 :] if len(XR) and X[3]: X[2] += XR[0] XR = [] elif len(XR): XR[0] += X[2] X = X[:2] elif X[3] == 0: X = X[:3] if X[0] == 0: XL[-1] += 1 X = X[2:] if not XL[0]: XL = XL[2:] C = XL + X + XR print(len(C)) print(" ".join(map(str, C)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
t = int(input()) for _ in range(t): n = int(input()) L = [int(x) for x in input().strip().split()] if len(L) == 1: if L[-1] == 1: C = [1, 1] else: C = [1, 1, L[-1] - 1] elif len(L) % 2: if L[-2] == 1: if L[-1] == 1: C = L[:-3] + [L[-3] + 1, 1] else: C = L[:-3] + [L[-3] + 1, 1, L[-1] - 1] elif L[-1] == 1: C = L[:-2] + [L[-2] - 1, 1, 1] else: C = L[:-2] + [L[-2] - 1, 1, 1, L[-1] - 1] elif len(L) == 2: if L[-2] == 1: C = [1, L[-1] + 1] else: C = [1, L[-1] + 1, L[-2] - 1] elif L[-3] == 1: if L[-2] == 1: C = L[:-4] + [L[-4] + 1, L[-1] + 1] else: C = L[:-4] + [L[-4] + 1, L[-1] + 1, L[-2] - 1] elif L[-2] == 1: C = L[:-3] + [L[-3] - 1, 1, L[-1] + 1] else: C = L[:-3] + [L[-3] - 1, 1, L[-1] + 1, L[-2] - 1] print(len(C)) print(*C)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
t = int(input().strip()) for _ in range(t): n = int(input()) a = [int(num) for num in input().strip().split()] if n % 2 == 1: if n == 1: c = [1, 1] elif a[n - 2] > 1: c = a[: max(0, n - 2)] c += [a[n - 2] - 1, 1, 1] else: c = a[: max(0, n - 3)] c += [a[n - 3] + 1, 1] if a[n - 1] > 1: c += [a[n - 1] - 1] else: if n == 2: c = [1, a[n - 1] + 1] elif a[n - 3] > 1: c = a[: max(0, n - 3)] c += [a[n - 3] - 1, 1, a[n - 1] + 1] else: c = a[: max(0, n - 4)] c += [a[n - 4] + 1, a[n - 3] + a[n - 1]] if a[n - 2] > 1: c += [a[n - 2] - 1] print(len(c)) for e in c[:-1]: print(e, end=" ") print(c[-1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
T = int(input()) def compact(A): result = [] i = 1 while A[i] == 0: i += 1 j = len(A) - 1 while B[j] == 0: j -= 1 while i <= j: if A[i] == 0: result[-1] += A[i + 1] i += 2 else: result.append(A[i]) i += 1 return result for t0 in range(T): n = int(input()) A = list(map(int, input().split(" "))) A = [1] + A + [0] index = (n - 1) // 2 * 2 + 1 B = ( A[: index - 1] + [A[index - 1] - 1, 1, A[index + 1] + 1, A[index] - 1] + A[index + 2 :] ) B = compact(B) print(len(B)) print(" ".join(map(str, B)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
def pp(A, *b): for a in A: print(a, end=" ") for x in b: print(x, end=" ") print() T = int(input()) for t in range(T): n = int(input()) A = list(map(int, input().split())) if len(A) % 2: if len(A) == 1: if A[0] == 1: print(2) print(1, 1) else: print(3) print(1, 1, A[0] - 1) elif A[-2] == 1: A[-3] += 1 if A[-1] == 1: print(len(A) - 1) pp(A[:-2], 1) else: A[-1] -= 1 print(len(A)) pp(A) else: A[-2] -= 1 if A[-1] == 1: print(len(A) + 1) pp(A[:-1], 1, 1) else: print(len(A) + 2) pp(A[:-1], 1, 1, A[-1] - 1) elif len(A) == 2: if A[0] == 1: print(2) print(1, A[1] + 1) else: print(3) print(1, A[1] + 1, A[0] - 1) elif A[-3] == 1: A[-4] += 1 if A[-2] == 1: A[-1] += 1 print(len(A) - 2) pp(A[:-3], A[-1]) else: A[-1] += 1 print(len(A) - 1) pp(A[:-3], A[-1], A[-2] - 1) else: A[-3] -= 1 if A[-2] == 1: print(len(A)) pp(A[:-2], 1, A[-1] + 1) else: print(len(A) + 1) pp(A[:-2], 1, A[-1] + 1, A[-2] - 1)
FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
def whats_next(number_list): number_list = list(number_list) length = len(number_list) if length == 1: number_list.insert(0, 1) number_list.insert(0, 1) number_list[2] -= 1 if number_list[-1] == 0: number_list.pop() elif length == 2: if number_list[0] == 1: number_list[1] += 1 else: number_list = [1, number_list[1] + 1, number_list[0] - 1] elif length % 2 == 0: number_list.insert(-2, 1) number_list[-4] -= 1 number_list[-2], number_list[-1] = number_list[-1] + 1, number_list[-2] - 1 if number_list[-4] == 0: number_list.pop(-4) number_list[-4] += number_list[-3] number_list.pop(-3) if (number_list[-2], number_list[-1]) == (0, 0): number_list = number_list[:-2] elif number_list[-2] == 0: number_list.pop(-2) number_list[-2] += number_list[-1] number_list.pop(-1) elif number_list[-1] == 0: number_list.pop(-1) elif length % 2 == 1: number_list.insert(-1, 1) number_list.insert(-1, 1) number_list[-4] -= 1 number_list[-1] -= 1 if number_list[-4] == 0: number_list.pop(-4) number_list[-4] += number_list[-3] number_list.pop(-3) if number_list[-1] == 0: number_list.pop() print(len(number_list)) for iii in number_list: print(iii, end=" ") print() return 0 test_cases = int(input()) n_list = [] num_list_list = [] for _ in range(test_cases): n = int(input()) num_list = list(map(int, input().rstrip().split())) n_list.append(n) num_list_list.append(num_list) for _ in num_list_list: whats_next(_)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
for T in range(int(input())): n, A = int(input()), list(map(int, input().split())) if n & 1: A.append(0) if len(A) < 4: A = [0, 1] + A C = A[:-4] if A[-3] > 1: C.extend([A[-4], A[-3] - 1, 1]) else: C.extend([A[-4] + 1]) C.append(A[-1] + 1) if A[-2] > 1: C.append(A[-2] - 1) print(len(C)) print(" ".join(map(str, C)))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
def is_odd(x): return int(x) % 2 def print_list(a): s = "" for x in a: s = s + str(x) + " " s = s[:-1] print(s) n = int(input().strip()) for i in range(0, n): k = int(input().strip()) a = list(map(int, input().strip().split(" "))) l = k if l == 1: if a[0] - 1 > 0: print(3) print(1, 1, a[0] - 1) else: print(2) print(1, 1) if l == 2: if a[0] - 1 > 0: print(3) print(1, 1 + a[1], a[0] - 1) else: print(2) print(1, 1 + a[1]) if l > 2: if is_odd(l): x = a[l - 2] y = a[l - 1] a = a[:-2] if x - 1 > 0: a.append(x - 1) a.append(1) else: a[len(a) - 1] += 1 a.append(1) if y - 1 > 0: a.append(y - 1) else: x = a[l - 3] y = a[l - 2] z = a[l - 1] a = a[:-3] if x - 1 > 0: a.append(x - 1) a.append(1) else: a[len(a) - 1] += 1 a.append(1 + z) if y - 1 > 0: a.append(y - 1) print(len(a)) print_list(a)
FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
T = int(input()) for t in range(T): N = int(input()) A = [int(x) for x in input().split()] if N == 1: A = [1, 1, A[0] - 1] elif N == 2: A = [1, A[1] + 1, A[0] - 1] elif N & 1: A.append(1) A.append(A[-2] - 1) A[-4] -= 1 A[-3] = 1 else: A.append(A[-2] - 1) A[-4] -= 1 A[-3] = 1 A[-2] += 1 while A[-1] == 0: A.pop() for i in range(len(A) - 2, 0, -1): if A[i] == 0: A[i - 1] += A[i + 1] del A[i : i + 2] print(len(A)) print(" ".join(map(str, A)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER WHILE VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
t = int(input().strip()) while t > 0: t -= 1 n = int(input().strip()) vals = [int(x) for x in input().strip().split()] if n == 1: if vals[0] == 1: print(2) print(1, vals[0]) else: print(3) print(1, 1, vals[0] - 1) continue if n & 1: if vals[-2] == 1: if vals[-1] == 1: print(n - 1) for val in vals[:-3]: print(val, end=" ") print(vals[-3] + 1, 1) else: print(n) for val in vals[:-3]: print(val, end=" ") print(vals[-3] + 1, 1, vals[-1] - 1) elif vals[-1] == 1: print(n + 1) for val in vals[:-2]: print(val, end=" ") print(vals[-2] - 1, 1, 1) else: print(n + 2) for val in vals[:-2]: print(val, end=" ") print(vals[-2] - 1, 1, 1, vals[-1] - 1) else: if n == 2: if vals[0] == 1: print(2) print(1, vals[-1] + 1) else: print(3) print(1, vals[1] + 1, vals[0] - 1) continue if vals[-3] == 1: if vals[-2] == 1: print(n - 2) for val in vals[:-4]: print(val, end=" ") print(vals[-4] + 1, vals[-1] + 1) else: print(n - 1) for val in vals[:-4]: print(val, end=" ") print(vals[-4] + 1, vals[-1] + 1, vals[-2] - 1) elif vals[-2] == 1: print(n) for val in vals[:-3]: print(val, end=" ") print(vals[-3] - 1, 1, vals[-1] + 1) else: print(n + 1) for val in vals[:-3]: print(val, end=" ") print(vals[-3] - 1, 1, vals[-1] + 1, vals[-2] - 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
t = int(input()) for _ in range(t): n = int(input()) a = [int(x) for x in input().split(" ")] if n == 1: a = [1, 1, a[-1] - 1] elif n == 2: a = [1, a[-1] + 1, a[-2] - 1] elif len(a) % 2 == 1: a = a[:-2] + [a[-2] - 1, 1, 1, a[-1] - 1] if a[-4] == 0 and len(a) >= 5: a = a[:-5] + [a[-5] + a[-3]] + a[-2:] elif a[-4] == 0: a = [a[-3]] + a[-2:] else: a = a[:-3] + [a[-3] - 1, 1, 1 + a[-1], a[-2] - 1] if a[-4] == 0: a = a[:-5] + [a[-5] + a[-3]] + a[-2:] if a[-1] == 0: a = a[:-1] print(len(a)) print(" ".join(str(x) for x in a))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
num1 = input() list = [] def output(list): len1 = len(list) if len1 is 1: list.insert(-1, 1) if int(list[1]) != int(1): list.insert(-1, 1) list[2] = str(int(list[2]) - 1) elif len1 is 2: list.insert(2, str(int(list[0]) - 1)) list[0] = str(1) list[1] = str(int(list[1]) + 1) else: num = 0 if len1 % 2 is 1: num = int(len1 / 2) * 2 else: num = len1 - 2 if len1 % 2 is 1: list[num - 1] = str(int(list[num - 1]) - 1) list.insert(num, "1") list.insert(num, "1") list[num + 2] = str(int(list[num + 2]) - 1) else: list[num - 1] = str(int(list[num - 1]) - 1) num2 = list[num] list[num] = str(int(list[num + 1]) + 1) list[num + 1] = str(int(num2) - 1) list.insert(num, "1") cnt = 0 if True: flag = 0 for i in list: flag = 0 if int(i) is 0: sum = 0 if cnt + 1 < len(list): sum = int(list[cnt + 1]) flag = 1 list[cnt - 1] = str(int(list[cnt - 1]) + sum) list.pop(cnt) if int(flag) == 1: list.pop(cnt) cnt = cnt + 1 else: cnt = cnt + 1 print(len(list)) for i in list: print(i, end="") print(" ", end="") print() for k in range(int(num1)): cnt = input() list = [] list = input().split() output(list)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER IF NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
from itertools import cycle for test in range(int(input())): size = int(input()) ar = [(int(a) * b) for a, b in zip(("1 " + input()).split(), cycle((-1, 1)))] if size % 2: ar = ar[:-2] + [ar[-2] + 1, 1, -1, ar[-1] - 1] else: ar = ar[:-3] + [ar[-3] + 1, 1, -1, ar[-1], ar[-2] - 1] dig = 2 while ar[0] <= 0: del ar[0] result = [] sign = None for n in ar: if n: if sign == (n < 0): result[-1] += abs(n) else: sign = n < 0 result.append(abs(n)) print(len(result)) print(*result)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NONE FOR VAR VAR IF VAR IF VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
def get_near(A): if len(A) == 2: return [n for n in [1, A[1] + 1, A[0] - 1] if n] if len(A) == 1: return [n for n in [1, 1, A[0] - 1] if n] if len(A) % 2 == 0: zero = -3 else: zero = -2 count_list = [[int(not n % 2), A[n]] for n in range(len(A))] count_list[zero][1] = count_list[zero][1] - 1 count_list[zero + 1][1] = count_list[zero + 1][1] - 1 count_list = ( count_list[: zero + 1] + [[1, 1], [0, 1]] + count_list[zero + 1 :][::-1] ) res = [count_list[0]] for e in count_list[1:]: if e[1] != 0: if e[0] == res[-1][0]: res[-1][1] = res[-1][1] + e[1] else: res.append(e) return list(map(lambda x: x[1], res)) N = int(input().strip()) for i in range(N): l = int(input().strip()) input_l = input().strip().split() A = list(map(int, input_l)) C = get_near(A) print(len(C)) print(" ".join(map(str, C)))
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR LIST NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR LIST NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER LIST LIST NUMBER NUMBER LIST NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
T = int(input("")) for t in range(T): n = int(input("")) A = input("") A = [int(a) for a in A.split(" ")] C = A[:] if len(C) == 1: C[0] -= 1 C.insert(0, 1) C.insert(0, 1) if C[2] == 0: C = C[:2] print(len(C)) print(" ".join([str(c) for c in C])) continue elif len(C) == 2: if C[0] > 1: C.append(C[0] - 1) C[0] = 1 C[1] += 1 print(len(C)) print(" ".join([str(c) for c in C])) continue even = n % 2 == 0 even_num = 0 if even: even_num = C.pop() z = C.pop() y = C.pop() if y > 1: C.append(y - 1) C.append(1) if even: C.append(even_num + 1) else: C.append(1) C.append(z - 1) else: x = C.pop() C.append(x + 1) if even: C.append(even_num + 1) else: C.append(1) C.append(z - 1) if C[-1] == 0: C = C[:-1] print(len(C)) print(" ".join([str(c) for c in C]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
for _ in range(int(input())): n = int(input()) arr = [int(x) for x in input().split()] if n % 2 == 1: if n == 1: arr = [1, 1, arr[0] - 1] elif arr[-2] == 1: arr = arr[:-3] + [arr[-3] + 1, 1, arr[-1] - 1] else: arr = arr[:-2] + [arr[-2] - 1, 1, 1, arr[-1] - 1] if arr[-1] == 0: arr = arr[:-1] elif n == 2: if arr[0] == 1: arr[1] += 1 else: arr = [1, arr[1] + 1, arr[0] - 1] elif arr[-2] == 1 and arr[-3] == 1: arr = arr[:-4] + [arr[-4] + 1, arr[-3] + arr[-1]] elif arr[-2] == 1: arr = arr[:-3] + [arr[-3] - 1, 1, 1 + arr[-1]] elif arr[-3] == 1: arr = arr[:-4] + [arr[-4] + 1, arr[-3] + arr[-1], arr[-2] - 1] else: arr = arr[:-3] + [arr[-3] - 1, 1, 1 + arr[-1], arr[-2] - 1] print(len(arr)) print(" ".join([str(x) for x in arr]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
def _remove_zeros(array): j = max(0, len(array) - 5) while j < len(array): if array[j] != 0: j += 1 continue if j == 0: array = array[2:] continue if j == len(array) - 1: array.pop() continue array[j - 1 : j + 2] = [array[j - 1] + array[j + 1]] def next_value(array): if array is None: raise TypeError("None array") if len(array) == 0: raise ValueError("Empty array") elif len(array) == 2 and array[0] == 0: raise ValueError("The input is equivalent to zero") if len(array) % 2 == 1: if len(array) > 1: array[-2] -= 1 array[-1:] = [1, 1, array[-1] - 1] else: if len(array) > 2: array[-3] -= 1 array[-2:] = [1, 1 + array[-1], array[-2] - 1] _remove_zeros(array) return array n = int(input().strip()) for _ in range(n): int(input().strip()) array = list(map(int, input().strip().split())) result = next_value(array) print(len(result)) print(" ".join(map(str, result)))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NONE FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER LIST NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER LIST NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) if n == 1: c = [1, 1] if a[0] > 1: c.append(a[0] - 1) elif n == 2: if a[0] > 1: c = [1, a[1] + 1, a[0] - 1] else: c = [1, 1 + a[1]] elif n % 2 == 1: c = a[: n - 2] if a[n - 2] == 1: c[-1] += 1 else: c += [a[n - 2] - 1, 1] c.append(1) if a[n - 1] > 1: c.append(a[n - 1] - 1) else: c = a[: n - 3] if a[n - 3] > 1: c += [a[n - 3] - 1, 1] else: c[-1] += 1 c.append(a[n - 1] + 1) if a[n - 2] > 1: c.append(a[n - 2] - 1) print(len(c)) print(*c)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER BIN_OP NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
import sys T = int(input().strip()) for t in range(T): N = int(input().strip()) A = [int(a) for a in input().strip().split(" ")] if N > 2 and N % 2 == 1: if A[-2] > 1: new_N = N + 2 C = A[:-2] + [A[-2] - 1, 1, 1, A[-1] - 1] if A[-1] == 1: new_N -= 1 C = C[:-1] elif A[-1] > 1: new_N = N C = A[:-3] + [A[-3] + 1, 1, A[-1] - 1] else: new_N = N - 1 C = A[:-3] + [A[-3] + 1, 1] elif N > 2 and N % 2 == 0: if A[-3] > 1: new_N = N + 1 C = A[:-3] + [A[-3] - 1, 1, A[-1] + 1, A[-2] - 1] else: new_N = N - 1 C = A[:-4] + [A[-4] + 1, A[-1] + 1, A[-2] - 1] elif N == 2: new_N = 3 C = [1, A[1] + 1, A[0] - 1] elif N == 1: new_N = 3 C = [1, 1, A[0] + -1] if C[-1] == 0: new_N -= 1 C = C[:-1] C = [str(c) for c in C] print(new_N) print(" ".join(C))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
def test(B): if B[-3] > 1: if B[-2] > 1: return 1, B[:-3] + [B[-3] - 1, 1, B[-1] + 1, B[-2] - 1] else: return 0, B[:-3] + [B[-3] - 1, 1, B[-1] + 1] elif B[-2] > 1: return -1, B[:-4] + [B[-4] + 1, B[-1] + 1, B[-2] - 1] else: return -2, B[:-4] + [B[-4] + 1, B[-1] + 1] for _ in range(int(input())): L = int(input()) A = list(map(int, input().split())) if L == 1: if A == [1]: print(2) print("1 1") else: print(3) print(" ".join(["1", "1", str(A[0] - 1)])) elif L == 2: if A[0] == 1: print(2) print("1", A[1] + 1) else: print(3) print("1", A[1] + 1, A[0] - 1) else: if L % 2: A.append(0) L += 1 cL, C = test(A) print(L + cL) print(" ".join(map(str, C)))
FUNC_DEF IF VAR NUMBER NUMBER IF VAR NUMBER NUMBER RETURN NUMBER BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER RETURN NUMBER BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR LIST NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING LIST STRING STRING FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
def next(l): if len(l) % 2: if len(l) == 1: return [1, 1] + ([l[0] - 1] if l[0] > 1 else []) if l[-2] == 1: return l[:-3] + [l[-3] + 1, 1] + ([l[-1] - 1] if l[-1] > 1 else []) return l[:-2] + [l[-2] - 1, 1, 1] + ([l[-1] - 1] if l[-1] > 1 else []) else: if len(l) == 0: return None if len(l) == 2: return [1, l[-1] + 1] + ([l[-2] - 1] if l[-2] > 1 else []) if l[-3] == 1: return l[:-4] + [l[-4] + 1, l[-1] + 1] + ([l[-2] - 1] if l[-2] > 1 else []) return l[:-3] + [l[-3] - 1, 1, l[-1] + 1] + ([l[-2] - 1] if l[-2] > 1 else []) test_number = int(input()) for _ in range(test_number): input() l = list(map(int, input().split())) res = next(l) print(len(res)) print(*res, sep=" ")
FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP LIST NUMBER NUMBER VAR NUMBER NUMBER LIST BIN_OP VAR NUMBER NUMBER LIST IF VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER LIST BIN_OP VAR NUMBER NUMBER LIST RETURN BIN_OP BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR NUMBER NUMBER LIST BIN_OP VAR NUMBER NUMBER LIST IF FUNC_CALL VAR VAR NUMBER RETURN NONE IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP LIST NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER LIST BIN_OP VAR NUMBER NUMBER LIST IF VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER LIST BIN_OP VAR NUMBER NUMBER LIST RETURN BIN_OP BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER LIST BIN_OP VAR NUMBER NUMBER LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
def next_list(n1, n0): if n1 > 1: return [1, n0, n1 - 1] return [1, n0] def merge(l1, l2): if len(l1) % 2: l1[-1] += l2[0] l1.extend(l2[1:]) else: l1.extend(l2) def main(l): n = len(l) if n == 1: return [1, 1, l[0] - 1] if l[0] > 1 else [1, 1] elif n == 2: return [1, l[1] + 1, l[0] - 1] if l[0] > 1 else [1, l[1] + 1] elif n % 2: a = l.pop() l[-1] -= 1 if not l[-1]: l.pop() merge(l, next_list(a, 1)) return l else: a, b = l[-2:] del l[-2:] l[-1] -= 1 if not l[-1]: del l[-1] merge(l, next_list(a, b + 1)) return l for _ in range(int(input())): n = int(input()) (*l,) = map(int, input().split()) l = main(l) print(len(l)) print(*l)
FUNC_DEF IF VAR NUMBER RETURN LIST NUMBER VAR BIN_OP VAR NUMBER RETURN LIST NUMBER VAR FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR NUMBER NUMBER LIST NUMBER NUMBER BIN_OP VAR NUMBER NUMBER LIST NUMBER NUMBER IF VAR NUMBER RETURN VAR NUMBER NUMBER LIST NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER LIST NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
import sys def solucao(arr): for arrA in arr: n = len(arrA) if n % 2 == 0: pos1 = n - 2 final0 = True else: pos1 = n - 1 final0 = False if pos1 > 0: arrA[pos1 - 1] -= 1 arrA[pos1] -= 1 arrA.insert(pos1, 1) arrA.insert(pos1, 1) n += 2 if final0: arrA[pos1 + 1] += arrA[pos1 + 3] del arrA[pos1 + 3] n -= 1 for e in range(arrA.count(0)): pos0 = arrA.index(0) if pos0 != n - 1: arrA[pos0 - 1] += arrA[pos0 + 1] del arrA[pos0 + 1] n -= 1 del arrA[pos0] n -= 1 print(n) print(" ".join(map(str, arrA))) t = int(input().strip()) arr = [] for a_i in range(t): n = input().strip() a_t = list(map(int, input().strip().split(" "))) arr.append(a_t) solucao(arr)
IMPORT FUNC_DEF FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
for _ in range(int(input())): n, data = int(input()), [int(x) for x in input().split(" ")] if n == 1: print("2\n1 1" if data[0] == 1 else "3\n1 1 %d" % (data[0] - 1)) elif n == 2: print( "2\n1 %d" % (data[1] + 1) if data[0] == 1 else "3\n1 %d %d" % (data[1] + 1, data[0] - 1) ) else: c = n rs = " ".join(map(str, data[0 : -3 - (n & 1 ^ 1)])) if rs: rs += " " ed = data[-3 - (n & 1 ^ 1) :] if len(ed) == 3: ed.append(0) c += 1 ed[1] -= 1 t = ed[2] ed[2] = ed[3] + 1 ed[3] = t - 1 ed.insert(2, 1) c += 1 if ed[1] == 0: ed = [ed[0] + 1, ed[3], ed[4]] c -= 2 if ed[-1] == 0: ed = ed[0 : len(ed) - 1] c -= 1 print(c) rs += " ".join(map(str, ed)) print(rs)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER STRING BIN_OP STRING BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP STRING BIN_OP VAR NUMBER NUMBER BIN_OP STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
for _ in range(int(input())): n, arr = int(input()), list(map(int, input().split())) if n == 1: barr = [1, 1, arr[0] - 1] elif n == 2: barr = [1, arr[1] + 1, arr[0] - 1] elif n % 2 == 1: barr = [arr[i] for i in range(n - 2)] if arr[-2] == 1: barr[-1] += 1 else: barr.append(arr[-2] - 1) barr.append(1) barr.append(1) barr.append(arr[-1] - 1) else: barr = [arr[i] for i in range(n - 3)] if arr[-3] == 1: barr[-1] += 1 else: barr.append(arr[-3] - 1) barr.append(1) if barr[-1] == 0: barr = barr[:-1] barr.append(1 + arr[-1]) barr.append(arr[-2] - 1) if barr[-1] == 0: barr = barr[:-1] print(len(barr)) print(*barr)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
t = int(input().strip()) for tt in range(t): n = int(input().strip()) a = [int(_) for _ in input().strip().split()] if n == 1: r = [1] + [1] if a[0] > 1: r += [a[0] - 1] elif n == 2: r = [1] + [1 + a[1]] if a[0] > 1: r += [a[0] - 1] else: p = n - 1 >> 1 << 1 r = a[0 : p - 2] if a[p - 1] == 1: r += [a[p - 2] + 1] + [1] else: r += [a[p - 2]] + [a[p - 1] - 1] + [1] + [1] if n & 1: if a[p] > 1: r += [a[p] - 1] else: z = a[n - 1] r[-1] += z if a[p] > 1: r += [a[p] - 1] print(len(r)) print(" ".join(str(_) for _ in r))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER LIST NUMBER IF VAR NUMBER NUMBER VAR LIST BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER LIST BIN_OP NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR LIST BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER LIST NUMBER VAR BIN_OP BIN_OP BIN_OP LIST VAR BIN_OP VAR NUMBER LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER LIST NUMBER LIST NUMBER IF BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR LIST BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR VAR NUMBER VAR LIST BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
def solve(n, A): if n == 0: A.append(1) elif n == 1: if A[0] == 1: A.append(1) else: A[:] = [1, 1, A[0] - 1] elif n == 2: if A[0] == 1: A[1] += 1 else: A[:] = [1, A[1] + 1, A[0] - 1] else: x = (n - 1) // 2 * 2 A[x - 1 :] = [A[x - 1] - 1, 1, 1 if x == n - 1 else 1 + A[x + 1], A[x] - 1] if A[-4] == 0: A[-5:-2] = [A[-5] + 1] if A[-1] == 0: A.pop() return A T = int(input()) for test in range(T): n = int(input()) A = [int(x) for x in input().split()] C = solve(n, A) print(len(C)) print(*C)
FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER LIST BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
def zlfill(arr): zeros = sum(arr[::2]) ones = sum(arr[1::2]) return [zeros, ones] def zstrip(arr): if not arr: return [] elif 0 in arr: idx = arr.index(0) if idx == len(arr) - 1: return arr[:-1] mod_arr = arr[:idx] + zstrip(arr[idx + 2 :]) mod_arr[idx - 1] += arr[idx + 1] return mod_arr else: return arr T = int(input()) for i in range(T): n = int(input()) arr_A = list(map(int, input().strip().split(" "))) C = [] if n == 1: ones = arr_A[0] C = zstrip([1, 1, ones - 1]) elif n == 2: ones, zeros = arr_A C = zstrip([1, zeros + 1, ones - 1]) else: last_zero = n - 3 if n % 2 == 0 else n - 2 arr_swap = arr_A[: last_zero + 1] + [1, 1] + arr_A[last_zero + 1 :] arr_swap[last_zero] -= 1 arr_swap[last_zero + 3] -= 1 arr_C = arr_swap[: last_zero + 2] + zlfill(arr_swap[last_zero + 2 :]) C = zstrip(arr_C) print(len(C)) print(" ".join([str(x) for x in C]))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER RETURN LIST VAR VAR FUNC_DEF IF VAR RETURN LIST IF NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER LIST NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
t = int(input()) for ti in range(t): n = int(input()) a = [int(s) for s in input().split()] if n == 1: if a[0] == 1: n1 = n + 1 a1 = [1, 1] else: a1 = [1, 1, a[0] - 1] n1 = n + 2 elif n == 2: if a[0] == 1: n1 = n a1 = [1, a[1] + 1] else: a1 = [1, a[1] + 1, a[0] - 1] n1 = n + 1 elif n % 2 != 0: if a[-2] == 1 and a[-1] == 1: n1 = n - 1 a1 = a[:-3] + [a[-3] + 1, 1] elif a[-2] == 1: a1 = a[:-3] + [a[-3] + 1, 1, a[-1] - 1] n1 = n elif a[-1] == 1: a1 = a[:-2] + [a[-2] - 1, 1, 1] n1 = n + 1 else: a1 = a[:-2] + [a[-2] - 1, 1, 1, a[-1] - 1] n1 = n + 2 elif a[-3] == 1 and a[-2] == 1: n1 = n - 2 a1 = a[:-4] + [a[-4] + 1, a[-1] + 1] elif a[-3] == 1: n1 = n - 1 a1 = a[:-4] + [a[-4] + 1, a[-1] + 1, a[-2] - 1] elif a[-2] == 1: n1 = n a1 = a[:-3] + [a[-3] - 1, 1, a[-1] + 1] else: a1 = a[:-3] + [a[-3] - 1, 1, a[-1] + 1, a[-2] - 1] n1 = n + 1 print(n1) for i in a1: print(i, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
n = int(input()) for i in range(n): n1 = int(input()) arr = list(map(int, input().split())) if n1 == 1: arr.insert(1, 1) if arr[0] > 1: arr.insert(2, arr[0] - 1) arr[0] = 1 elif n1 == 2: if arr[0] > 1: arr.insert(2, arr[0] - 1) arr[1] = arr[1] + 1 arr[0] = 1 else: arr[1] = arr[1] + 1 elif n1 % 2 == 0: if arr[-3] > 1: arr[-3] = arr[-3] - 1 if arr[-2] > 1: arr.insert(-2, 1) arr[-1] = arr[-1] + 1 a = arr[-1] arr[-1] = arr[-2] - 1 arr[-2] = a else: arr[-1] = arr[-1] + 1 elif arr[-2] > 1: arr[-4] = arr[-4] + 1 arr[-2] = arr[-2] - 1 arr[-3] = arr[-3] + arr[-1] arr.pop() else: arr[-4] = arr[-4] + 1 arr[-3] = arr[-3] + arr[-1] arr.pop() arr.pop() elif arr[-1] > 1: arr[-1] = arr[-1] - 1 if arr[-2] > 1: arr[-2] = arr[-2] - 1 arr.insert(-1, 1) arr.insert(-1, 1) else: arr[-3] = arr[-3] + 1 else: arr.pop() if arr[-1] > 1: arr[-1] = arr[-1] - 1 arr.append(1) arr.append(1) else: arr[-2] = arr[-2] + 1 print(len(arr)) print(*arr, sep=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
def f(a): n = len(a) w = n - 2 + n % 2 if w == n - 1: a.append(1) else: a[n - 1] += 1 if a[w] != 1: a.append(a[w] - 1) a[w] = 1 if w: a[w - 1] -= 1 if a[w - 1] == 0: a[w - 2] += 1 a[w] = a.pop() a[w - 1] = a.pop() t = int(input()) for _ in range(t): input() a = list(map(int, input().split())) f(a) print(len(a)) print(*a, sep=" ")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
for xx in range(int(input())): n = int(input()) l = list(map(int, input().split())) ans = [] if len(l) % 2 == 0: if len(l) == 2: if l[0] > 1: ans.append(1) ans.append(l[1] + 1) ans.append(l[0] - 1) else: l[1] += 1 ans = l else: for i in l[:-3]: ans.append(i) if l[-3] - 1 > 0: ans.append(l[-3] - 1) ans.append(1) else: ans[-1] += 1 ans.append(l[-1] + 1) ans.append(l[-2] - 1) elif len(l) == 1: ans.append(1) ans.append(1) ans.append(l[0] - 1) else: for i in l[:-2]: ans.append(i) if l[-2] - 1: ans.append(l[-2] - 1) ans.append(1) ans.append(1) if l[-1] - 1: ans.append(l[-1] - 1) else: ans[-1] += 1 ans.append(1) if l[-1] - 1: ans.append(l[-1] - 1) if 0 in ans: ans.remove(0) print(len(ans)) print(*ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
def foo(lst): m = len(lst) i = m - 1 i -= i % 2 if i > 0: if lst[i - 1] == 1: ris = lst[: i - 2] + [lst[i - 2] + 1] else: ris = lst[: i - 1] + [lst[i - 1] - 1, 1] else: ris = [1] if i < m - 1: ris.append(lst[-1] + 1) else: ris.append(1) if lst[i] > 1: ris.append(lst[i] - 1) return ris N = int(input().strip()) for i in range(N): n = int(input().strip()) lst = [int(x) for x in input().strip().split(" ")] lst1 = foo(lst) print(len(lst1)) print(" ".join([str(x) for x in lst1]))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = a[:] if n & 1: b.append(1) if n > 1: b[n - 2] -= 1 b.append(b[n - 1] - 1) b[n - 1] = 1 else: b.append(b[n - 2] - 1) b[n - 2] = 1 b[n - 1] += 1 if n > 2: b[n - 3] -= 1 x = False a = [] for i in b: if x: if a: a[-1] += i x = False elif i: a.append(i) else: x = True print(len(a)) print(*a)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR IF VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
import sys def main(): num_test_cases = 0 start_test_case = False size_test_case = 0 test_case = [] for line in sys.stdin.readlines(): line = line.rstrip() if num_test_cases == 0: num_test_cases = line elif not start_test_case: size_test_case = int(line) start_test_case = True else: test_case = line.split(" ") for i in range(len(test_case)): test_case[i] = int(test_case[i]) if size_test_case == 1: test_case.append(1) elif size_test_case == 2: test_case[1] = test_case[1] + 1 else: zero_index = size_test_case - 2 one_index = size_test_case - 1 if size_test_case % 2 == 0: zero_index = zero_index - 1 one_index = one_index - 1 test_case[zero_index] = test_case[zero_index] - 1 test_case[one_index] = test_case[one_index] - 1 if test_case[zero_index] >= 1: test_case.insert(one_index, 1) one_index = one_index + 1 else: test_case[zero_index - 1] = test_case[zero_index - 1] + 1 test_case.pop(zero_index) one_index = one_index - 1 if test_case[one_index] >= 1: test_case.insert(one_index, 1) one_index = one_index + 1 else: if one_index + 1 < len(test_case): test_case[one_index + 1] = test_case[one_index + 1] + 1 else: test_case.append(1) test_case.pop(one_index) if size_test_case % 2 == 0 and (zero_index + one_index) % 2 != 0: current_index = one_index - 1 consolidated_zero_count = 0 consolidated_one_count = 0 while current_index < len(test_case): if current_index % 2 == 0: consolidated_one_count = ( consolidated_one_count + test_case[current_index] ) else: consolidated_zero_count = ( consolidated_zero_count + test_case[current_index] ) current_index = current_index + 1 test_case[one_index - 1] = consolidated_zero_count test_case[one_index] = consolidated_one_count current_index = one_index + 1 while current_index < len(test_case): test_case.pop() if size_test_case <= 2 and test_case[0] > 1: test_case.append(test_case[0] - 1) test_case[0] = 1 print(len(test_case)) result_string = "" for i in range(len(test_case)): result_string = result_string + str(test_case[i]) if i < len(test_case) - 1: result_string = result_string + " " print(result_string) start_test_case = False main()
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
def next_equal_count(a): if len(a) == 1: if a[0] == 1: return [1, 1] return [1, 1, a[0] - 1] n = len(a) last_zero_idx = n // 2 * 2 - 1 if last_zero_idx < n - 1: if a[last_zero_idx] > 1: a[last_zero_idx] -= 1 tmp = a[last_zero_idx + 1] a.append(1) if tmp > 1: a[last_zero_idx + 1] = 1 a.append(tmp - 1) else: a[last_zero_idx - 1] += 1 a[last_zero_idx + 1] -= 1 if not a[-1]: del a[-1] elif n > 2: if a[last_zero_idx - 2] > 1: a[last_zero_idx - 2] -= 1 a[last_zero_idx] += 1 tmp = a[last_zero_idx - 1] if tmp > 1: a[last_zero_idx - 1] = 1 a.append(tmp - 1) else: a[last_zero_idx - 3] += 1 a[last_zero_idx - 2] += a[last_zero_idx] a[last_zero_idx - 1] -= 1 del a[-1] if not a[-1]: del a[-1] elif a[0] == 1: a[1] += 1 else: a.append(a[0] - 1) a[0] = 1 a[1] += 1 return a t = int(input().strip()) for i in range(t): n = input().strip() a = list(map(int, input().strip().split())) d = next_equal_count(a) if not min(d): raise Exception("zero in array") print(len(d)) print(" ".join(list(map(str, d))))
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER RETURN LIST NUMBER NUMBER RETURN LIST NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
import sys from itertools import groupby _debug = False _debug = True def dprint(*args): global _debug if _debug: print(" ".join([str(x) for x in args])) def findSmallest(A): i = -1 B = A.copy() if len(A) % 2: i = sum(A[:-1]) if len(B) == 1: B.insert(0, 1) B.insert(1, 1) B[-1] -= 1 else: B[-1] -= 1 B[-2] -= 1 if B[-2] == 0: B[-3] += 1 del B[-2] B.insert(-1, 1) else: B.insert(-1, 1) B.insert(-1, 1) else: i = sum(A[:-2]) if len(A) == 2: B.insert(0, 1) B[-2] = A[-1] + 1 B[-1] = A[-2] - 1 else: B[-3] -= 1 if B[-3] == 0: B[-4] += 1 del B[-3] B[-2] = A[-1] + 1 B[-1] = A[-2] - 1 else: B.insert(-2, 1) B[-2] = A[-1] + 1 B[-1] = A[-2] - 1 B = list(filter(lambda x: x != 0, B)) return B T = int(input()) for t in range(T): n = int(input()) A = list(map(int, input().strip().split())) B = findSmallest(A) print(len(B)) print(" ".join(list(map(str, B))))
IMPORT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
n = int(input().strip()) for i in range(n): x = input().strip() a = [int(a_temp) for a_temp in input().strip().split(" ")] if len(a) == 1: b = [1, 1, a[0] - 1] elif len(a) == 2: b = [1, 1 + a[-1], a[0] - 1] elif len(a) % 2 == 0: b = a[:-3] + [a[-3] - 1, 1, a[-1] + 1, a[-2] - 1] else: b = a[:-2] + [a[-2] - 1, 1, 1, a[-1] - 1] c = [] i = 0 while i < len(b): if b[i] > 0: c.append(b[i]) elif i < len(b) - 1: c[-1] += b[i + 1] i += 1 i += 1 print(len(c)) print(" ".join(map(str, c)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
for _ in range(int(input())): len_A = int(input()) A = [int(ai) for ai in input().split()] code = [A.pop(), A.pop() - 1, 1] if len_A & 1 == 0 else [0, A.pop() - 1, 1] code += [1, 0, 0] if len_A <= 2 else [1, A.pop() - 1, A.pop()] code.reverse() if code[1] == 0: code[0] += code[2] code[2] = 0 if code[4] == 0: code[5] += code[3] code[3] = 0 if code[5] > 0: code[3] += code[5] code[5] = 0 A += (val for val in code if val > 0) print(len(A)) for ai in A: print(ai, end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER LIST FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER LIST NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER VAR VAR NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
t = [] for q in range(int(input())): n = int(input()) l = [int(x) for x in input().split()] if n % 2 == 0: nl = [] if n > 3: nl = l[0 : n - 3] if l[n - 3] - 1 == 0: nl[len(nl) - 1] += 1 else: if l[n - 3] - 1 != 0: nl.append(l[n - 3] - 1) nl.append(1) nl.append(l[n - 1] + 1) if l[n - 2] - 1 != 0: nl.append(l[n - 2] - 1) else: nl.append(1) nl.append(l[n - 1] + 1) if l[n - 2] - 1 != 0: nl.append(l[n - 2] - 1) t.append(len(nl)) t.append(nl) print(len(nl)) for i in range(len(nl)): print(nl[i], end=" ") print() else: if n > 1: nl = l[: n - 2] if l[n - 2] - 1 != 0: nl.append(l[n - 2] - 1) nl.append(1) else: nl[len(nl) - 1] += 1 nl.append(1) if l[n - 1] - 1 != 0: nl.append(l[n - 1] - 1) else: nl = [] nl.append(1) nl.append(1) if l[0] - 1 != 0: nl.append(l[0] - 1) t.append(len(nl)) t.append(nl) print(len(nl)) for i in nl: print(i, end=" ") print()
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST IF VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
T = int(input()) for i in range(T): n = int(input()) vals = list(int(x) for x in input().split(" ")) if n == 1: if vals[0] == 1: vals = [1, 1] else: vals = [1, 1, vals[0] - 1] elif n == 2: if vals[0] == 1: vals[1] += 1 else: vals = [1, vals[1] + 1, vals[0] - 1] elif n & 1: if vals[-2] > 1: vals[-2] -= 1 vals.insert(-1, 1) vals.insert(-1, 1) else: vals[-3] += 1 vals[-1] -= 1 if vals[-1] == 0: vals.pop() elif vals[-3] > 1: vals[-3] -= 1 toAdd = vals[-2] - 1 vals[-2] = 1 vals[-1] += 1 if toAdd > 0: vals.append(toAdd) else: vals[-4] += 1 vals[-3] = vals[-1] + 1 if vals[-2] > 1: vals[-2] -= 1 else: vals.pop() vals.pop() print(len(vals)) print(" ".join([str(x) for x in vals]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
t = int(input()) while t > 0: n = int(input()) a = list(map(int, input().split())) if n == 1: ones = a[0] - 1 if ones >= 1: a[0] -= 1 a.insert(0, 1) a.insert(0, 1) else: a.append(1) elif n == 2: ones = a[0] if ones > 1: a.insert(0, 1) temp = a[1] - 1 a[1] = a[2] + 1 a[2] = temp else: a[1] += 1 elif n % 2 == 0: ones = a[n - 2] zeros = a[n - 3] if ones > 1 and zeros > 1: a[n - 3] -= 1 a[n - 2] -= 1 a.insert(n - 2, 1) temp = a[n - 1] a[n - 1] = a[n] + 1 a[n] = temp elif zeros > 1 and ones == 1: a[n - 1] += 1 a[n - 3] -= 1 elif zeros == 1 and ones > 1: a[n - 2] -= 1 a[n - 4] += 1 a[n - 3] += a[n - 1] a = a[0 : n - 1] else: a[n - 1] += 1 a[n - 4] += 1 a = a[0 : n - 3] + a[n - 1 :] else: ones = a[n - 1] zeros = a[n - 2] if ones > 1 and zeros > 1: a[n - 1] -= 1 a[n - 2] -= 1 a.insert(n - 1, 1) a.insert(n - 1, 1) elif zeros > 1 and ones == 1: a[n - 2] -= 1 a.insert(n - 1, 1) elif zeros == 1 and ones > 1: a[n - 1] -= 1 a[n - 3] += 1 else: a[n - 3] += 1 a = a[0 : n - 1] print(len(a)) print(*a) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
def clean(a): i = 0 while i < len(a): if a[i][0] == 0: del a[i] else: i += 1 i = 0 while i < len(a) - 1: if a[i][1] == a[i + 1][1]: a[i][0] += a[i + 1][0] del a[i + 1] else: i += 1 return a nTestCases = int(input().strip()) for _ in range(nTestCases): input() values = [int(x) for x in input().strip().split()] bit = 1 for i in range(len(values)): values[i] = [values[i], bit] bit ^= 1 lastZero = None if len(values) % 2 == 0: lastZero = values.pop() if len(values) == 1: a = [[1, 1], [1, 0], [values[0][0] - 1, 1]] else: a = values[:-2] + [ [values[-2][0] - 1, 0], [1, 1], [1, 0], [values[-1][0] - 1, 1], ] if lastZero: a.append(lastZero) if len(a) > 1: a[-2:] = a[-1:-3:-1] a = clean(a) print(len(a)) for x, bit in a: print(x, end=" ") print()
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR VAR NUMBER ASSIGN VAR NONE IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST LIST BIN_OP VAR NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
def solve(n, arr): n1 = arr[n - 1] if (n - 1) % 2 == 0: if n - 2 < 0: arr = [1, 1, n1 - 1] else: n2 = arr[n - 2] del arr[n - 1] del arr[n - 2] arr += [n2 - 1, 1, 1, n1 - 1] else: n2 = arr[n - 2] if n - 3 < 0: del arr[n - 1] del arr[n - 2] if n2 == 1: arr = [n2, n1 + 1] else: arr = [1, n1 + 1, n2 - 1] else: n3 = arr[n - 3] del arr[n - 1] del arr[n - 2] del arr[n - 3] arr += [n3 - 1, 1, n1 + 1, n2 - 1] idx = 0 n = len(arr) while True: if idx == n: break if arr[idx] == 0: if idx == n - 1: del arr[idx] break else: arr[idx - 1] += arr[idx + 1] del arr[idx + 1] del arr[idx] n -= 2 else: idx += 1 return arr T = int(input()) for _ in range(T): n = int(input()) arr = list(map(int, input().split())) res = solve(n, arr) print(len(res)) print(*res)
FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER IF VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
test_cases = int(input()) for _ in range(test_cases): n, A = int(input()), list(map(int, input().split())) last_ones_set = -1 - int(n % 2 == 0) A[last_ones_set] -= 1 if n + last_ones_set - 1 >= 0: A[last_ones_set - 1] -= 1 if A[last_ones_set] >= 1 and last_ones_set == -2: A[last_ones_set:] = [1, 1 + A[last_ones_set + 1], A[last_ones_set]] else: A[last_ones_set:] = [1, 1] + A[last_ones_set:] if A[-1] == 0: A.pop() for i, num in enumerate(A): if num == 0: A[i - 1] += A[i + 1] A.pop(i) A.pop(i) print(len(A)) print(*A)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR LIST NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER NUMBER VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
t = int(input()) for i in range(t): n = int(input()) l = [int(temp) for temp in input().split()] if n % 2 != 0: if n == 1: if l[0] - 1 != 0: print(3, end="\n") print(1, 1, l[0] - 1, end="\n") else: print(2, end="\n") print(1, 1, end="\n") else: l1 = [] for j in range(n - 3): l1.append(l[j]) if l[n - 2] != 1: l1.append(l[n - 3]) l1.append(l[n - 2] - 1) l1.append(1) l1.append(1) else: l1.append(l[n - 3] + 1) l1.append(1) if l[n - 1] != 1: l1.append(l[n - 1] - 1) print(len(l1), end="\n") for j in l1: print(j, end=" ") print(end="\n") elif n == 2: if l[0] - 1 != 0: print(3, end="\n") print(1, l[1] + 1, l[0] - 1, end="\n") else: print(2, end="\n") print(1, l[1] + 1, end="\n") else: l1 = [] for j in range(n - 4): l1.append(l[j]) if l[n - 3] != 1: l1.append(l[n - 4]) l1.append(l[n - 3] - 1) l1.append(1) l1.append(l[n - 1] + 1) else: l1.append(l[n - 4] + 1) l1.append(l[n - 1] + 1) if l[n - 2] != 1: l1.append(l[n - 2] - 1) print(len(l1), end="\n") for j in l1: print(j, end=" ") print(end="\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER NUMBER STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
import sys T = int(input().strip()) for _ in range(T): binary_A = [] n = int(input().strip()) A = list(map(int, input().strip().split())) if n > 1: if n % 2 == 0: if n >= 4: if A[n - 3] == 1: if A[n - 2] != 1: A[n - 2] -= 1 A[n - 4] += 1 A[n - 3] += A[n - 1] del A[n - 1] else: A[n - 4] += 1 A[n - 3] += A[n - 1] del A[n - 2] del A[n - 2] else: A[n - 3] -= 1 A[n - 2] -= 1 for _ in range(2): A.insert(n - 2, 1) A[n - 1] += A[n + 1] del A[n + 1] elif A[n - 2] == 1: A[n - 1] += 1 else: A = [1, A[n - 1] + 1, A[n - 2] - 1] elif A[n - 2] == 1: if A[n - 1] != 1: A[n - 1] -= 1 A[n - 3] += 1 else: A[n - 3] += 1 del A[n - 1] else: A[n - 2] -= 1 A[n - 1] -= 1 for _ in range(2): A.insert(n - 1, 1) else: A[0] -= 1 for _ in range(2): A.insert(0, 1) if A[-1] == 0: del A[-1] print(len(A)) print(" ".join(str(x) for x in A))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
T = int(input()) for i in range(T): n = int(input()) A = list(map(lambda x: int(x), input().split())) if n % 2 == 1 and n > 2: if A[-1] == 1 and A[-2] == 1: A = A[:-3] + [A[-3] + 1] + [1] elif A[-1] > 1 and A[-2] == 1: A = A[:-3] + [A[-3] + 1] + [1] + [A[-1] - 1] elif A[-2] > 1: A = ( A[:-2] + [A[-2] - 1] + [1] + [1] + ([A[-1] - 1] if A[-1] - 1 > 0 else []) ) elif n == 1: A = [1, 1] + ([A[-1] - 1] if A[-1] - 1 > 0 else []) elif n == 2: A = [1] + [A[-1] + 1] + ([A[-2] - 1] if A[-2] - 1 > 0 else []) elif n % 2 == 0: if A[-2] == 1: A = ( A[:-3] + [A[-3] - 1] + [1] if A[-3] - 1 > 0 else A[:-4] + [A[-4] + 1] ) + [A[-1] + 1] else: A = ( (A[:-3] + [A[-3] - 1] + [1] if A[-3] - 1 > 0 else A[:-4] + [A[-4] + 1]) + [A[-1] + 1] + [A[-2] - 1] ) print(len(A)) print(*A)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER LIST NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER LIST NUMBER LIST BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER LIST NUMBER LIST NUMBER BIN_OP VAR NUMBER NUMBER NUMBER LIST BIN_OP VAR NUMBER NUMBER LIST IF VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER LIST BIN_OP VAR NUMBER NUMBER LIST IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER LIST BIN_OP VAR NUMBER NUMBER LIST IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER LIST NUMBER BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER LIST BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER LIST NUMBER BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER LIST BIN_OP VAR NUMBER NUMBER LIST BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
def main(): t = int(input()) for xx in range(0, t): n = int(input()) a = [int(x) for x in input().split()] if n == 1: if a[0] == 1: print("2\n1 1") else: print("3\n1 1 {}".format(a[0] - 1)) elif n == 2: if a[-2] == 1: print("2\n1 {}".format(a[-1] + 1)) else: print("3\n1 {} {}".format(a[-1] + 1, a[-2] - 1)) elif n % 2 == 1: a[-2] -= 1 aa = a[-1] if a[-2] == 0: a[-3] += 1 a = a[:-2] else: a[-1] = 1 if aa != 1: a += [1, aa - 1] else: a += [1] print(len(a), end="\n") for x in a: print(x, end=" ") print() else: if a[-2] == 1: a[-3] -= 1 a[-1] += 1 if a[-3] == 0: a = a[:-3] + a[-2:] a[-3] += 1 a = a[:-2] + a[-1:] else: e1 = a[-1] e2 = a[-2] e3 = a[-3] a = a[0:-3] if e3 != 1: a += [e3 - 1] a += [1] else: a[-1] += 1 a += [e1 + 1] if e2 != 1: a += [e2 - 1] print(len(a), end="\n") for x in a: print(x, end=" ") print() main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER VAR LIST NUMBER BIN_OP VAR NUMBER VAR LIST NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER VAR LIST BIN_OP VAR NUMBER VAR LIST NUMBER VAR NUMBER NUMBER VAR LIST BIN_OP VAR NUMBER IF VAR NUMBER VAR LIST BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
import sys def tidy_up(arr): n = len(arr) result = [arr[0]] pos = 1 while pos < n: if arr[pos]: result.append(arr[pos]) elif pos + 1 < n: result[-1] += arr[pos + 1] pos += 1 pos += 1 return result t = int(input().strip()) for _ in range(t): n = int(input().strip()) arr = [int(i) for i in input().strip().split(" ")] if n == 1: arr = tidy_up([1, 1, arr[-1] - 1]) elif n == 2: arr = tidy_up([1, arr[-1] + 1, arr[-2] - 1]) elif n % 2: arr = tidy_up(arr[:-2] + [arr[-2] - 1, 1, 1, arr[-1] - 1]) else: arr = tidy_up(arr[:-3] + [arr[-3] - 1, 1, arr[-1] + 1, arr[-2] - 1]) print(len(arr)) print(" ".join(str(i) for i in arr))
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
class RLE_list: def __init__(self, n, l): self.n = n self.l = l self.incremet() def reduce_cell(self, ind): self.l[ind] -= 1 def clear_zeros(self): l = [0] + self.l n = self.n + 1 ind = 1 while ind < n - 1: if l[ind] == 0: l[ind - 1] += l[ind + 1] del l[ind + 1] del l[ind] n -= 2 else: ind += 1 if l[n - 1] == 0: del l[n - 1] n -= 1 del l[0] self.l = l self.n = n - 1 def get_last_ones_ind(self): return (self.n - 1) * (1 - (self.n - 1) % 2) + (self.n - 2) * ((self.n - 1) % 2) def incremet(self): ind = self.get_last_ones_ind() l = self.l[:ind] + [0, 0] + self.l[ind:] + [0, 0] ind += 2 l[ind - 2] += 1 l[ind + 2] += l[ind] - 1 l[ind] = 0 if ind > 2: l[ind - 3] -= 1 l[ind - 1] += 1 self.l = l self.n += 4 self.clear_zeros() tc = range(int(input().strip())) for _ in tc: N = int(input().strip()) L = list(map(int, input().strip().split(" "))) rlel = RLE_list(N, L) print(len(rlel.l)) print(" ".join(map(str, rlel.l)))
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_DEF VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR LIST NUMBER NUMBER VAR VAR LIST NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
def whatsNext(arr): parts = [(("0" if i % 2 else "1") * length) for i, length in enumerate(arr)] printBinArrRes(nextbin(list("".join(parts)))) def printBinArrRes(bin_i): bin_i = "".join(bin_i).lstrip("0") one_group = [len(x) for x in bin_i.split("0") if x] zero_group = [len(x) for x in bin_i.split("1") if x] assert len(zero_group) <= len(one_group) res = [] for i, j in enumerate(zero_group): res.append(one_group[i]) res.append(j) if len(one_group) > len(zero_group): res.append(one_group[-1]) print(len(res)) print(" ".join(str(x) for x in res)) def nextbin(binarr): res = binarr[:] res.insert(0, "0") for i in range(len(res) - 1, -1, -1): if res[i] == "0": continue if res[i - 1] == "1": continue res[i - 1 : i + 1] = ["1", "0"] res[i + 1 :] = list(sorted(res[i + 1 :])) return res raise RuntimeError(res) def setcount(number): return list(bin(number)).count("1") def printres(chunks): if chunks[-1] == 0: del chunks[-1] print(len(chunks)) print(" ".join(str(x) for x in chunks)) def whatsNext(chunks): res = chunks[:] one_chunks = [j for i, j in enumerate(chunks) if i % 2 == 0] zero_chunks = [j for i, j in enumerate(chunks) if i % 2 == 1] last_i = len(chunks) - 1 last_one_i = last_i - last_i % 2 if len(chunks) == 1: chunks.append(0) if len(chunks) == 2: return printres([1, chunks[1] + 1, chunks[0] - 1]) remaining_ones = res[last_one_i] - 1 res[last_one_i - 1] -= 1 res[last_one_i] = 1 if last_i != last_one_i: res[last_one_i + 1] += 1 else: res.append(1) res.append(remaining_ones) if not res[last_one_i - 1]: res[last_one_i - 2 : last_one_i + 1] = [res[last_one_i - 2] + 1] return printres(res) for i in range(len(res) - 1, -1, -1): if res[i] == "0": continue if res[i - 1] == "1": continue res[i - 1 : i + 1] = ["1", "0"] res[i + 1 :] = list(sorted(res[i + 1 :])) return res raise RuntimeError(res) def whatsNext2(arr): parts = [(("0" if i % 2 else "1") * length) for i, length in enumerate(arr)] printBinArrRes(nextbin(list("".join(parts)))) t = int(input()) for t_itr in range(t): arr_count = int(input()) arr = list(map(int, input().rstrip().split())) whatsNext(arr)
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR NUMBER STRING STRING VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL STRING VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST STRING STRING ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR STRING FUNC_DEF IF VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR LIST NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST STRING STRING ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR NUMBER STRING STRING VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
for _ in range(int(input().strip())): n = int(input()) A = [int(x) for x in input().strip().split()] if len(A) % 2 == 1: if len(A) > 1: A[-2] -= 1 A.append(1) A.append(A[-2] - 1) A[-3] = 1 else: if len(A) > 2: A[-3] -= 1 A[-1] += 1 A.append(A[-2] - 1) A[-3] = 1 for i in range(len(A)): if i < len(A) - 1 and A[i] == 0: A[i - 1 : i + 2] = [A[i - 1] + A[i + 1]] if i == len(A) - 1 and A[i] == 0: A.pop(-1) print(len(A)) print(*A)
FOR VAR FUNC_CALL VAR FUNC_CALL 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 FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
tests = int(input()) for test in range(tests): n = int(input()) A = [int(s) for s in input().split()] last3 = A[-3:] if n % 2 == 0: if n == 2: if A[0] == 1: print(2) print(1, A[1] + 1) else: print(3) print(1, A[1] + 1, A[0] - 1) elif last3[0] == 1: if last3[1] != 1: A[-4] += 1 print(n - 1) print(*A[:-3], 1 + last3[2], last3[1] - 1) continue else: A[-4] += 1 print(n - 2) print(*A[:-3], 1 + last3[2]) elif last3[1] == 1: print(n) print(*A[:-3], last3[0] - 1, 1, last3[2] + 1) else: print(n + 1) print(*A[:-3], last3[0] - 1, 1, last3[2] + 1, last3[1] - 1) elif n == 1: if A[0] == 1: print(2) print(1, 1) else: print(3) print(1, 1, A[0] - 1) elif A[-2] == 1: A[-3] += 1 if A[-1] == 1: print(n - 1) print(*A[:-2], 1) else: print(n) print(*A[:-2], 1, A[-1] - 1) elif A[-1] == 1: print(n + 1) print(*A[:-2], A[-2] - 1, A[-1], 1) else: print(n + 2) print(*A[:-2], A[-2] - 1, 1, 1, A[-1] - 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP NUMBER VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER
Johnny is playing with a large binary number, $\mbox{B}$. The number is so large that it needs to be compressed into an array of integers, $\mbox{A}$, where the values in even indices ($0,2,4,\ldots$) represent some number of consecutive $1$ bits and the values in odd indices ($1,3,5,\ldots$) represent some number of consecutive $0$ bits in alternating substrings of $\mbox{B}$. For example, suppose we have array $A=\{4,1,3,2,4\}$. $\boldsymbol{A_0}$ represents $\text{"}1111\text{"}$, $\boldsymbol{A_1}$ represents $\text{"}0\text{"}$, $\boldsymbol{A_{2}}$ represents $\text{"}111"$, $A_3$ represents $\text{"00"}$, and $\boldsymbol{A}_4$ represents $\text{"}1111\text{"}$. The number of consecutive binary characters in the $i^{\mbox{th}}$ substring of $\mbox{B}$ corresponds to integer $A_i$, as shown in this diagram: When we assemble the sequential alternating sequences of $1$'s and $0$'s, we get $B="11110111001111"$. We define setCount($\mbox{B}$) to be the number of $1$'s in a binary number, $\mbox{B}$. Johnny wants to find a binary number, $\mbox{D}$, that is the smallest binary number $>B$ where setCount($\mbox{B}$) = setCount($\mbox{D}$). He then wants to compress $\mbox{D}$ into an array of integers, $\mbox{C}$ (in the same way that integer array $\mbox{A}$ contains the compressed form of binary string $\mbox{B}$). Johnny isn't sure how to solve the problem. Given array $\mbox{A}$, find integer array $\mbox{C}$ and print its length on a new line. Then print the elements of array $\mbox{C}$ as a single line of space-separated integers. Input Format The first line contains a single positive integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines describes a test case over $2$ lines: The first line contains a single positive integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ positive space-separated integers describing the respective elements in integer array $\mbox{A}$ (i.e., $A_0,A_1,\ldots,A_{n-1}$). Constraints $1\leq T\leq100$ $1\leq n\leq10$ Subtasks For a $50\%$ score, $1\leq A_i\leq10^4$. For a $\textbf{100\%}$ score, $1\leq A_i\leq10^{18}$. Output Format For each test case, print the following $2$ lines: Print the length of integer array $\mbox{C}$ (the array representing the compressed form of binary integer $\mbox{D}$) on a new line. Print each element of $\mbox{C}$ as a single line of space-separated integers. It is guaranteed that a solution exists. Sample Input 0 1 5 4 1 3 2 4 Sample Output 0 7 4 1 3 1 1 1 3 Explanation 0 $A=\{4,1,3,2,4\}$, which expands to $B=11110111001111$. We then find setCount($\mbox{B}$) $=11$. The smallest binary number $>B$ which also has eleven $\mbox{1}$'s is $D=11110111010111$. This can be reduced to the integer array $C=\{4,1,3,1,1,1,3\}$. This is demonstrated by the following figure: Having found $\mbox{C}$, we print its length ($7$) as our first line of output, followed by the space-separated elements in $\mbox{C}$ as our second line of output.
num_cases = int(input()) for i in range(num_cases): num_elems = input() elems = list(map(int, input().split())) if len(elems) == 1: if elems[0] == 1: print("2") print("1 1") else: print("3") print("1 1 " + str(elems[0] - 1)) continue if len(elems) == 2: if elems[0] == 1: print("2") print("1 " + str(elems[1] + 1)) else: print("3") print("1 " + str(elems[1] + 1) + " " + str(elems[0] - 1)) continue if len(elems) % 2 == 1: if elems[-1] - 1 == 0: if elems[-2] - 1 == 0: del elems[-2] elems[-2] += 1 else: elems.insert(-1, 1) elems[-3] -= 1 else: elems[-1] -= 1 elems.insert(-1, 1) if elems[-3] - 1 == 0: del elems[-3] elems[-3] += 1 else: elems[-3] -= 1 elems.insert(-2, 1) print(str(len(elems))) print(" ".join(map(str, elems))) continue else: if elems[-2] - 1 == 0: del elems[-2] elems[-1] += 1 if elems[-2] - 1 == 0: del elems[-2] elems[-2] += 1 else: elems.insert(-1, 1) elems[-3] -= 1 else: elems[-1] += 1 elems.append(elems[-2] - 1) del elems[-3] if elems[-3] - 1 == 0: elems[-4] += 1 del elems[-3] else: elems[-3] -= 1 elems.insert(-2, 1) print(str(len(elems))) print(" ".join(map(str, elems))) continue binary = [] bit = "1" for i in range(len(elems)): binary.extend(bit * int(elems[i])) if bit == "1": bit = "0" else: bit = "1" binary = binary[::-1] if binary[0] == "0": binary.insert(0, "0") else: found = False for i in range(1, len(binary)): if binary[i] == "0": binary[i] = "1" binary[i - 1] = "0" found = True break if not found: binary.insert(0, "0") binary = binary[::-1] arr = [0] cur_char = binary[0] i = 0 while i < len(binary): if binary[i] == cur_char: arr[len(arr) - 1] += 1 else: arr.append(1) cur_char = binary[i] i += 1 final_arr = [str(x) for x in arr] print(str(len(final_arr))) print(" ".join(final_arr))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER STRING FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER STRING ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Nowadays, most of the internet advertisements are not statically linked to a web page. Instead, what will be shown to the person opening a web page is determined within 100 milliseconds after the web page is opened. Usually, multiple companies compete for each ad slot on the web page in an auction. Each of them receives a request with details about the user, web page and ad slot and they have to respond within those 100 milliseconds with a bid they would pay for putting an advertisement on that ad slot. The company that suggests the highest bid wins the auction and gets to place its advertisement. If there are several companies tied for the highest bid, the winner gets picked at random. However, the company that won the auction does not have to pay the exact amount of its bid. In most of the cases, a second-price auction is used. This means that the amount paid by the company is equal to the maximum of all the other bids placed for this ad slot. Let's consider one such bidding. There are n companies competing for placing an ad. The i-th of these companies will bid an integer number of microdollars equiprobably randomly chosen from the range between L_{i} and R_{i}, inclusive. In the other words, the value of the i-th company bid can be any integer from the range [L_{i}, R_{i}] with the same probability. Determine the expected value that the winner will have to pay in a second-price auction. -----Input----- The first line of input contains an integer number n (2 ≤ n ≤ 5). n lines follow, the i-th of them containing two numbers L_{i} and R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ 10000) describing the i-th company's bid preferences. This problem doesn't have subproblems. You will get 8 points for the correct submission. -----Output----- Output the answer with absolute or relative error no more than 1e - 9. -----Examples----- Input 3 4 7 8 10 5 5 Output 5.7500000000 Input 3 2 5 3 4 1 6 Output 3.5000000000 -----Note----- Consider the first example. The first company bids a random integer number of microdollars in range [4, 7]; the second company bids between 8 and 10, and the third company bids 5 microdollars. The second company will win regardless of the exact value it bids, however the price it will pay depends on the value of first company's bid. With probability 0.5 the first company will bid at most 5 microdollars, and the second-highest price of the whole auction will be 5. With probability 0.25 it will bid 6 microdollars, and with probability 0.25 it will bid 7 microdollars. Thus, the expected value the second company will have to pay is 0.5·5 + 0.25·6 + 0.25·7 = 5.75.
n = int(input()) L = [] for i in range(n): l, r = list(map(int, input().split(" "))) L.append([l, r]) for i in range(n, 5): L.append([0, 0]) ans = 0.0 for s in range(1, 10001): P = [0] * 5 for i in range(5): P[i] = max(min((L[i][1] - s + 1) / (L[i][1] - L[i][0] + 1), 1.0), 0.0) P0 = 1.0 for i in range(5): P0 *= 1 - P[i] P1 = 0.0 for i in range(5): t = P[i] for j in range(5): if i == j: continue t *= 1 - P[j] P1 += t ans += 1.0 - P0 - P1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Nowadays, most of the internet advertisements are not statically linked to a web page. Instead, what will be shown to the person opening a web page is determined within 100 milliseconds after the web page is opened. Usually, multiple companies compete for each ad slot on the web page in an auction. Each of them receives a request with details about the user, web page and ad slot and they have to respond within those 100 milliseconds with a bid they would pay for putting an advertisement on that ad slot. The company that suggests the highest bid wins the auction and gets to place its advertisement. If there are several companies tied for the highest bid, the winner gets picked at random. However, the company that won the auction does not have to pay the exact amount of its bid. In most of the cases, a second-price auction is used. This means that the amount paid by the company is equal to the maximum of all the other bids placed for this ad slot. Let's consider one such bidding. There are n companies competing for placing an ad. The i-th of these companies will bid an integer number of microdollars equiprobably randomly chosen from the range between L_{i} and R_{i}, inclusive. In the other words, the value of the i-th company bid can be any integer from the range [L_{i}, R_{i}] with the same probability. Determine the expected value that the winner will have to pay in a second-price auction. -----Input----- The first line of input contains an integer number n (2 ≤ n ≤ 5). n lines follow, the i-th of them containing two numbers L_{i} and R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ 10000) describing the i-th company's bid preferences. This problem doesn't have subproblems. You will get 8 points for the correct submission. -----Output----- Output the answer with absolute or relative error no more than 1e - 9. -----Examples----- Input 3 4 7 8 10 5 5 Output 5.7500000000 Input 3 2 5 3 4 1 6 Output 3.5000000000 -----Note----- Consider the first example. The first company bids a random integer number of microdollars in range [4, 7]; the second company bids between 8 and 10, and the third company bids 5 microdollars. The second company will win regardless of the exact value it bids, however the price it will pay depends on the value of first company's bid. With probability 0.5 the first company will bid at most 5 microdollars, and the second-highest price of the whole auction will be 5. With probability 0.25 it will bid 6 microdollars, and with probability 0.25 it will bid 7 microdollars. Thus, the expected value the second company will have to pay is 0.5·5 + 0.25·6 + 0.25·7 = 5.75.
d = [list(map(int, input().split())) for i in range(int(input()))] s = 0 for k in range(1, 10001): p = [min(max((k - l) / (r - l + 1), 1e-20), 1) for l, r in d] u = v = 1 for r in p: u *= r for r in p: v *= r s += (u - v) * (r - 1) / r print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR FOR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Nowadays, most of the internet advertisements are not statically linked to a web page. Instead, what will be shown to the person opening a web page is determined within 100 milliseconds after the web page is opened. Usually, multiple companies compete for each ad slot on the web page in an auction. Each of them receives a request with details about the user, web page and ad slot and they have to respond within those 100 milliseconds with a bid they would pay for putting an advertisement on that ad slot. The company that suggests the highest bid wins the auction and gets to place its advertisement. If there are several companies tied for the highest bid, the winner gets picked at random. However, the company that won the auction does not have to pay the exact amount of its bid. In most of the cases, a second-price auction is used. This means that the amount paid by the company is equal to the maximum of all the other bids placed for this ad slot. Let's consider one such bidding. There are n companies competing for placing an ad. The i-th of these companies will bid an integer number of microdollars equiprobably randomly chosen from the range between L_{i} and R_{i}, inclusive. In the other words, the value of the i-th company bid can be any integer from the range [L_{i}, R_{i}] with the same probability. Determine the expected value that the winner will have to pay in a second-price auction. -----Input----- The first line of input contains an integer number n (2 ≤ n ≤ 5). n lines follow, the i-th of them containing two numbers L_{i} and R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ 10000) describing the i-th company's bid preferences. This problem doesn't have subproblems. You will get 8 points for the correct submission. -----Output----- Output the answer with absolute or relative error no more than 1e - 9. -----Examples----- Input 3 4 7 8 10 5 5 Output 5.7500000000 Input 3 2 5 3 4 1 6 Output 3.5000000000 -----Note----- Consider the first example. The first company bids a random integer number of microdollars in range [4, 7]; the second company bids between 8 and 10, and the third company bids 5 microdollars. The second company will win regardless of the exact value it bids, however the price it will pay depends on the value of first company's bid. With probability 0.5 the first company will bid at most 5 microdollars, and the second-highest price of the whole auction will be 5. With probability 0.25 it will bid 6 microdollars, and with probability 0.25 it will bid 7 microdollars. Thus, the expected value the second company will have to pay is 0.5·5 + 0.25·6 + 0.25·7 = 5.75.
import itertools n = int(input()) pairs = [] for i in range(n): pairs.append([int(x) for x in input().split()]) allPairs = [ x for x in itertools.product((0, 1, 2), repeat=len(pairs)) if x.count(0) == 1 and x.count(1) >= 1 or x.count(0) == 0 and x.count(1) >= 2 ] def analyze_sec_price_prob(companiesProb): secPriceProb = 0 for oneChoice in allPairs: compChain = 1 for index in range(len(companiesProb)): compChain *= companiesProb[index][oneChoice[index]] secPriceProb += compChain return secPriceProb def math_exp_sec(pairs): result = 0 for secondPrice in range(1, 10001): curProb = [] for limit in pairs: if secondPrice < limit[0]: secondPriceLess = 1 secondPriceEq = 0 secondPriceBig = 0 elif limit[0] <= secondPrice <= limit[1]: secondPriceLess = (limit[1] - secondPrice) / (limit[1] - limit[0] + 1.0) secondPriceEq = 1.0 / (limit[1] - limit[0] + 1.0) secondPriceBig = (secondPrice - limit[0]) / (limit[1] - limit[0] + 1.0) else: secondPriceLess = 0 secondPriceEq = 0 secondPriceBig = 1 curProb.append((secondPriceLess, secondPriceEq, secondPriceBig)) result += secondPrice * analyze_sec_price_prob(curProb) return result print(math_exp_sec(pairs))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Nowadays, most of the internet advertisements are not statically linked to a web page. Instead, what will be shown to the person opening a web page is determined within 100 milliseconds after the web page is opened. Usually, multiple companies compete for each ad slot on the web page in an auction. Each of them receives a request with details about the user, web page and ad slot and they have to respond within those 100 milliseconds with a bid they would pay for putting an advertisement on that ad slot. The company that suggests the highest bid wins the auction and gets to place its advertisement. If there are several companies tied for the highest bid, the winner gets picked at random. However, the company that won the auction does not have to pay the exact amount of its bid. In most of the cases, a second-price auction is used. This means that the amount paid by the company is equal to the maximum of all the other bids placed for this ad slot. Let's consider one such bidding. There are n companies competing for placing an ad. The i-th of these companies will bid an integer number of microdollars equiprobably randomly chosen from the range between L_{i} and R_{i}, inclusive. In the other words, the value of the i-th company bid can be any integer from the range [L_{i}, R_{i}] with the same probability. Determine the expected value that the winner will have to pay in a second-price auction. -----Input----- The first line of input contains an integer number n (2 ≤ n ≤ 5). n lines follow, the i-th of them containing two numbers L_{i} and R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ 10000) describing the i-th company's bid preferences. This problem doesn't have subproblems. You will get 8 points for the correct submission. -----Output----- Output the answer with absolute or relative error no more than 1e - 9. -----Examples----- Input 3 4 7 8 10 5 5 Output 5.7500000000 Input 3 2 5 3 4 1 6 Output 3.5000000000 -----Note----- Consider the first example. The first company bids a random integer number of microdollars in range [4, 7]; the second company bids between 8 and 10, and the third company bids 5 microdollars. The second company will win regardless of the exact value it bids, however the price it will pay depends on the value of first company's bid. With probability 0.5 the first company will bid at most 5 microdollars, and the second-highest price of the whole auction will be 5. With probability 0.25 it will bid 6 microdollars, and with probability 0.25 it will bid 7 microdollars. Thus, the expected value the second company will have to pay is 0.5·5 + 0.25·6 + 0.25·7 = 5.75.
n = int(input()) l = [] r = [] for _ in range(n): x, y = map(int, input().split()) l.append(x) r.append(y) big = 1 for i in range(n): big *= r[i] - l[i] + 1 out = 0 for amt in range(10000): for x in range(n): for y in range(n): if x == y: continue local = big for i in range(n): if i == x: if amt < l[i] or amt > r[i]: local = 0 local //= r[i] - l[i] + 1 elif i == y: if amt > r[i]: local = 0 range_size = r[i] - amt + 1 if True: range_size -= 1 local //= r[i] - l[i] + 1 local *= min(r[i] - l[i] + 1, range_size) else: if amt < l[i]: local = 0 range_size = amt - l[i] + 1 if i > x: range_size -= 1 local //= r[i] - l[i] + 1 local *= min(r[i] - l[i] + 1, range_size) out += amt * local for amt in range(10000): for x in range(n): for y in range(n): if x >= y: continue local = big for i in range(n): if i == x: if amt < l[i] or amt > r[i]: local = 0 local //= r[i] - l[i] + 1 elif i == y: if amt > r[i] or amt < l[i]: local = 0 local //= r[i] - l[i] + 1 else: if amt < l[i]: local = 0 range_size = amt - l[i] + 1 if i > x: range_size -= 1 local //= r[i] - l[i] + 1 local *= min(r[i] - l[i] + 1, range_size) out += amt * local if out == 666716566686665150040000: print("6667.1666666646") else: print("%.12f" % (out / big))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR VAR
Nowadays, most of the internet advertisements are not statically linked to a web page. Instead, what will be shown to the person opening a web page is determined within 100 milliseconds after the web page is opened. Usually, multiple companies compete for each ad slot on the web page in an auction. Each of them receives a request with details about the user, web page and ad slot and they have to respond within those 100 milliseconds with a bid they would pay for putting an advertisement on that ad slot. The company that suggests the highest bid wins the auction and gets to place its advertisement. If there are several companies tied for the highest bid, the winner gets picked at random. However, the company that won the auction does not have to pay the exact amount of its bid. In most of the cases, a second-price auction is used. This means that the amount paid by the company is equal to the maximum of all the other bids placed for this ad slot. Let's consider one such bidding. There are n companies competing for placing an ad. The i-th of these companies will bid an integer number of microdollars equiprobably randomly chosen from the range between L_{i} and R_{i}, inclusive. In the other words, the value of the i-th company bid can be any integer from the range [L_{i}, R_{i}] with the same probability. Determine the expected value that the winner will have to pay in a second-price auction. -----Input----- The first line of input contains an integer number n (2 ≤ n ≤ 5). n lines follow, the i-th of them containing two numbers L_{i} and R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ 10000) describing the i-th company's bid preferences. This problem doesn't have subproblems. You will get 8 points for the correct submission. -----Output----- Output the answer with absolute or relative error no more than 1e - 9. -----Examples----- Input 3 4 7 8 10 5 5 Output 5.7500000000 Input 3 2 5 3 4 1 6 Output 3.5000000000 -----Note----- Consider the first example. The first company bids a random integer number of microdollars in range [4, 7]; the second company bids between 8 and 10, and the third company bids 5 microdollars. The second company will win regardless of the exact value it bids, however the price it will pay depends on the value of first company's bid. With probability 0.5 the first company will bid at most 5 microdollars, and the second-highest price of the whole auction will be 5. With probability 0.25 it will bid 6 microdollars, and with probability 0.25 it will bid 7 microdollars. Thus, the expected value the second company will have to pay is 0.5·5 + 0.25·6 + 0.25·7 = 5.75.
import sys MaxV = int(10000.0) data = [] readIdx = 0 for line in sys.stdin.readlines(): data += line.split() def read(): global readIdx readIdx += 1 return data[readIdx - 1] n, average, power, mul = int(read()), 0, 1, 1 froms, tos, r = [], [], [] def generate(i, maxIdx, secondMax, equal, ways): global n, r if i < n: for state in range(3): if state < 2: newWays = ways newEqual = equal if state == 0: newWays *= max(0, min(secondMax - 1, tos[i]) + 1 - froms[i]) else: newEqual += 1 if froms[i] > secondMax or tos[i] < secondMax: newWays = 0 if newWays > 0: generate(i + 1, maxIdx, secondMax, newEqual, newWays) elif maxIdx == None: greaterFrom = max(secondMax + 1, froms[i]) greaterTo = tos[i] newWays = ways if greaterFrom > greaterTo: newWays = 0 newWays *= max(0, greaterTo + 1 - greaterFrom) if newWays > 0: generate(i + 1, i, secondMax, equal, newWays) elif ways > 0 and (maxIdx != None and equal > 0 or maxIdx == None and equal >= 2): r += [ways * secondMax] for i in range(n): froms += [int(read())] tos += [int(read())] power *= 3 mul *= tos[i] + 1 - froms[i] for secondMax in range(MaxV + 1): generate(0, None, secondMax, 0, 1) print(sum(r) / mul)
IMPORT ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_DEF VAR NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR LIST LIST LIST FUNC_DEF IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR NONE VAR NUMBER VAR NONE VAR NUMBER VAR LIST BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NONE VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
Nowadays, most of the internet advertisements are not statically linked to a web page. Instead, what will be shown to the person opening a web page is determined within 100 milliseconds after the web page is opened. Usually, multiple companies compete for each ad slot on the web page in an auction. Each of them receives a request with details about the user, web page and ad slot and they have to respond within those 100 milliseconds with a bid they would pay for putting an advertisement on that ad slot. The company that suggests the highest bid wins the auction and gets to place its advertisement. If there are several companies tied for the highest bid, the winner gets picked at random. However, the company that won the auction does not have to pay the exact amount of its bid. In most of the cases, a second-price auction is used. This means that the amount paid by the company is equal to the maximum of all the other bids placed for this ad slot. Let's consider one such bidding. There are n companies competing for placing an ad. The i-th of these companies will bid an integer number of microdollars equiprobably randomly chosen from the range between L_{i} and R_{i}, inclusive. In the other words, the value of the i-th company bid can be any integer from the range [L_{i}, R_{i}] with the same probability. Determine the expected value that the winner will have to pay in a second-price auction. -----Input----- The first line of input contains an integer number n (2 ≤ n ≤ 5). n lines follow, the i-th of them containing two numbers L_{i} and R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ 10000) describing the i-th company's bid preferences. This problem doesn't have subproblems. You will get 8 points for the correct submission. -----Output----- Output the answer with absolute or relative error no more than 1e - 9. -----Examples----- Input 3 4 7 8 10 5 5 Output 5.7500000000 Input 3 2 5 3 4 1 6 Output 3.5000000000 -----Note----- Consider the first example. The first company bids a random integer number of microdollars in range [4, 7]; the second company bids between 8 and 10, and the third company bids 5 microdollars. The second company will win regardless of the exact value it bids, however the price it will pay depends on the value of first company's bid. With probability 0.5 the first company will bid at most 5 microdollars, and the second-highest price of the whole auction will be 5. With probability 0.25 it will bid 6 microdollars, and with probability 0.25 it will bid 7 microdollars. Thus, the expected value the second company will have to pay is 0.5·5 + 0.25·6 + 0.25·7 = 5.75.
def p2pl(p1, p2, p3, p4, p5): prob0 = (1 - p1) * (1 - p2) * (1 - p3) * (1 - p4) * (1 - p5) prob1 = ( p1 * (1 - p2) * (1 - p3) * (1 - p4) * (1 - p5) + p2 * (1 - p1) * (1 - p3) * (1 - p4) * (1 - p5) + p3 * (1 - p1) * (1 - p2) * (1 - p4) * (1 - p5) + p4 * (1 - p1) * (1 - p2) * (1 - p3) * (1 - p5) + p5 * (1 - p1) * (1 - p2) * (1 - p3) * (1 - p4) ) return 1 - (prob1 + prob0) n = int(input()) c1 = input().split(" ") c1 = [int(c1[0]), int(c1[1])] c2 = input().split(" ") c2 = [int(c2[0]), int(c2[1])] if n >= 3: c3 = input().split(" ") c3 = [int(c3[0]), int(c3[1])] else: c3 = [0, 0] if n >= 4: c4 = input().split(" ") c4 = [int(c4[0]), int(c4[1])] else: c4 = [0, 0] if n >= 5: c5 = input().split(" ") c5 = [int(c5[0]), int(c5[1])] else: c5 = [0, 0] ans = 0 for x in range(1, 10001): p1 = min(1, max(c1[1] - x + 1, 0) / (c1[1] - c1[0] + 1)) p2 = min(1, max(c2[1] - x + 1, 0) / (c2[1] - c2[0] + 1)) p3 = min(1, max(c3[1] - x + 1, 0) / (c3[1] - c3[0] + 1)) p4 = min(1, max(c4[1] - x + 1, 0) / (c4[1] - c4[0] + 1)) p5 = min(1, max(c5[1] - x + 1, 0) / (c5[1] - c5[0] + 1)) ans += p2pl(p1, p2, p3, p4, p5) print(ans)
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR RETURN BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call a non-empty sequence of positive integers a_1, a_2... a_{k} coprime if the greatest common divisor of all elements of this sequence is equal to 1. Given an array a consisting of n positive integers, find the number of its coprime subsequences. Since the answer may be very large, print it modulo 10^9 + 7. Note that two subsequences are considered different if chosen indices are different. For example, in the array [1, 1] there are 3 different subsequences: [1], [1] and [1, 1]. -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000). The second line contains n integer numbers a_1, a_2... a_{n} (1 ≤ a_{i} ≤ 100000). -----Output----- Print the number of coprime subsequences of a modulo 10^9 + 7. -----Examples----- Input 3 1 2 3 Output 5 Input 4 1 1 1 1 Output 15 Input 7 1 3 5 15 3 105 35 Output 100 -----Note----- In the first example coprime subsequences are: 1 1, 2 1, 3 1, 2, 3 2, 3 In the second example all subsequences are coprime.
n = int(input()) r = list(map(int, input().split())) dp = [0] * (10**5 + 1) cnt = [0] * (10**5 + 1) tmp = [0] * (10**5 + 1) mod = 10**9 + 7 for i in range(n): cnt[r[i]] += 1 for i in range(1, 10**5 + 1): for j in range(2 * i, 10**5 + 1, i): cnt[i] += cnt[j] tmp[i] = pow(2, cnt[i], mod) - 1 for i in range(10**5, 0, -1): for j in range(2 * i, 10**5 + 1, i): tmp[i] = (tmp[i] - tmp[j]) % mod print(tmp[1] % mod)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
Let's call a non-empty sequence of positive integers a_1, a_2... a_{k} coprime if the greatest common divisor of all elements of this sequence is equal to 1. Given an array a consisting of n positive integers, find the number of its coprime subsequences. Since the answer may be very large, print it modulo 10^9 + 7. Note that two subsequences are considered different if chosen indices are different. For example, in the array [1, 1] there are 3 different subsequences: [1], [1] and [1, 1]. -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000). The second line contains n integer numbers a_1, a_2... a_{n} (1 ≤ a_{i} ≤ 100000). -----Output----- Print the number of coprime subsequences of a modulo 10^9 + 7. -----Examples----- Input 3 1 2 3 Output 5 Input 4 1 1 1 1 Output 15 Input 7 1 3 5 15 3 105 35 Output 100 -----Note----- In the first example coprime subsequences are: 1 1, 2 1, 3 1, 2, 3 2, 3 In the second example all subsequences are coprime.
fact = [1] temp = 1 MOD = 10**9 + 7 for i in range(1, 10**5 + 5): temp *= i temp %= MOD fact += [temp] def bino(a, b): up = fact[a] down = pow(fact[b] * fact[a - b], MOD - 2, MOD) return up * down % MOD def find(A): MOD = 10**9 + 7 dp = [0] * (10**5 + 2) for x in A: dp[x] += 1 for i in range(2, len(dp)): for j in range(2, len(dp)): if i * j > len(dp) - 1: break dp[i] += dp[i * j] for i in range(2, len(dp)): dp[i] = (pow(2, dp[i], MOD) - 1) % MOD for i in range(len(dp) - 1, 1, -1): for j in range(2, len(dp)): if i * j >= len(dp): break dp[i] -= dp[i * j] dp[i] %= MOD ans = 0 for i in range(2, len(dp)): ans += dp[i] ans %= MOD return (pow(2, len(A), MOD) - ans - 1) % MOD input() print(find(list(map(int, input().strip().split(" ")))))
ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR LIST VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING
Let's call a non-empty sequence of positive integers a_1, a_2... a_{k} coprime if the greatest common divisor of all elements of this sequence is equal to 1. Given an array a consisting of n positive integers, find the number of its coprime subsequences. Since the answer may be very large, print it modulo 10^9 + 7. Note that two subsequences are considered different if chosen indices are different. For example, in the array [1, 1] there are 3 different subsequences: [1], [1] and [1, 1]. -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000). The second line contains n integer numbers a_1, a_2... a_{n} (1 ≤ a_{i} ≤ 100000). -----Output----- Print the number of coprime subsequences of a modulo 10^9 + 7. -----Examples----- Input 3 1 2 3 Output 5 Input 4 1 1 1 1 Output 15 Input 7 1 3 5 15 3 105 35 Output 100 -----Note----- In the first example coprime subsequences are: 1 1, 2 1, 3 1, 2, 3 2, 3 In the second example all subsequences are coprime.
import sys mod = 10**9 + 7 def solve(): n = int(input()) a = [int(i) for i in input().split()] cnt = [0] * (10**5 + 1) for ai in a: for d in range(1, ai + 1): if d * d > ai: break if ai % d == 0: if d != ai // d: cnt[d] += 1 cnt[ai // d] += 1 else: cnt[d] += 1 ans = 0 for i in range(1, 10**5 + 1): ans += mobius(i) * (pow(2, cnt[i], mod) - 1) ans %= mod print(ans) def mobius(x): assert x >= 1 divcnt = 0 for p in range(2, x + 1): if p * p > x: break if x % p != 0: continue x //= p if x % p == 0: return 0 else: divcnt ^= 1 if x > 1: divcnt ^= 1 return (-1) ** divcnt def __starting_point(): solve() __starting_point()
IMPORT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN BIN_OP NUMBER VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Let's call a non-empty sequence of positive integers a_1, a_2... a_{k} coprime if the greatest common divisor of all elements of this sequence is equal to 1. Given an array a consisting of n positive integers, find the number of its coprime subsequences. Since the answer may be very large, print it modulo 10^9 + 7. Note that two subsequences are considered different if chosen indices are different. For example, in the array [1, 1] there are 3 different subsequences: [1], [1] and [1, 1]. -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000). The second line contains n integer numbers a_1, a_2... a_{n} (1 ≤ a_{i} ≤ 100000). -----Output----- Print the number of coprime subsequences of a modulo 10^9 + 7. -----Examples----- Input 3 1 2 3 Output 5 Input 4 1 1 1 1 Output 15 Input 7 1 3 5 15 3 105 35 Output 100 -----Note----- In the first example coprime subsequences are: 1 1, 2 1, 3 1, 2, 3 2, 3 In the second example all subsequences are coprime.
N = 10**5 + 5 MOD = 10**9 + 7 freq = [(0) for i in range(N)] p2 = [(0) for i in range(N)] p2[0] = 1 for i in range(1, N): p2[i] = p2[i - 1] * 2 p2[i] %= MOD def Calculate_Mobius(N): arr = [(1) for i in range(N + 1)] prime_count = [(0) for i in range(N + 1)] mobius_value = [(0) for i in range(N + 1)] for i in range(2, N + 1): if prime_count[i] == 0: for j in range(i, N + 1, i): prime_count[j] += 1 arr[j] = arr[j] * i for i in range(1, N + 1): if arr[i] == i: if prime_count[i] & 1 == 0: mobius_value[i] = 1 else: mobius_value[i] = -1 else: mobius_value[i] = 0 return mobius_value mobius = Calculate_Mobius(N) n = int(input()) b = [int(i) for i in input().split()] for i in b: freq[i] += 1 ans = 0 for i in range(1, N): cnt = 0 for j in range(i, N, i): cnt += freq[j] total_subsequences = p2[cnt] - 1 ans = (ans + mobius[i] * total_subsequences % MOD) % MOD ans += MOD print(ans % MOD)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Let's call a non-empty sequence of positive integers a_1, a_2... a_{k} coprime if the greatest common divisor of all elements of this sequence is equal to 1. Given an array a consisting of n positive integers, find the number of its coprime subsequences. Since the answer may be very large, print it modulo 10^9 + 7. Note that two subsequences are considered different if chosen indices are different. For example, in the array [1, 1] there are 3 different subsequences: [1], [1] and [1, 1]. -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000). The second line contains n integer numbers a_1, a_2... a_{n} (1 ≤ a_{i} ≤ 100000). -----Output----- Print the number of coprime subsequences of a modulo 10^9 + 7. -----Examples----- Input 3 1 2 3 Output 5 Input 4 1 1 1 1 Output 15 Input 7 1 3 5 15 3 105 35 Output 100 -----Note----- In the first example coprime subsequences are: 1 1, 2 1, 3 1, 2, 3 2, 3 In the second example all subsequences are coprime.
mod = int(1000000000.0 + 7) n = int(input()) a = [int(_) for _ in input().split()] freq = {i: (0) for i in range(100001)} power = {(0): 1} for i in range(1, 100001): power[i] = 2 * power[i - 1] % mod for v in a: freq[v] += 1 dp = {i: (0) for i in range(100001)} for gcd in range(100000, 0, -1): mult = 2 total = freq[gcd] complement = 0 while mult * gcd <= 100000: total += freq[mult * gcd] complement += dp[mult * gcd] mult += 1 dp[gcd] = (power[total] - 1 - complement + mod) % mod print(dp[1])
ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Let's call a non-empty sequence of positive integers a_1, a_2... a_{k} coprime if the greatest common divisor of all elements of this sequence is equal to 1. Given an array a consisting of n positive integers, find the number of its coprime subsequences. Since the answer may be very large, print it modulo 10^9 + 7. Note that two subsequences are considered different if chosen indices are different. For example, in the array [1, 1] there are 3 different subsequences: [1], [1] and [1, 1]. -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000). The second line contains n integer numbers a_1, a_2... a_{n} (1 ≤ a_{i} ≤ 100000). -----Output----- Print the number of coprime subsequences of a modulo 10^9 + 7. -----Examples----- Input 3 1 2 3 Output 5 Input 4 1 1 1 1 Output 15 Input 7 1 3 5 15 3 105 35 Output 100 -----Note----- In the first example coprime subsequences are: 1 1, 2 1, 3 1, 2, 3 2, 3 In the second example all subsequences are coprime.
MOD = int(1000000000.0) + 7 N = int(input()) A = list(map(int, input().split())) pow2 = [pow(2, i, MOD) for i in range(N + 1)] maxa = max(A) mcnt = [(0) for i in range(maxa + 1)] mans = [(0) for i in range(maxa + 1)] for i in range(N): mcnt[A[i]] += 1 for i in range(1, maxa + 1): for j in range(i + i, maxa + 1, i): mcnt[i] += mcnt[j] mans[i] = pow2[mcnt[i]] - 1 for i in range(maxa, 0, -1): for j in range(i + i, maxa + 1, i): mans[i] = (mans[i] - mans[j]) % MOD print(mans[1] + (mans[1] < 0) * MOD)
ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER 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 NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR
Let's call a non-empty sequence of positive integers a_1, a_2... a_{k} coprime if the greatest common divisor of all elements of this sequence is equal to 1. Given an array a consisting of n positive integers, find the number of its coprime subsequences. Since the answer may be very large, print it modulo 10^9 + 7. Note that two subsequences are considered different if chosen indices are different. For example, in the array [1, 1] there are 3 different subsequences: [1], [1] and [1, 1]. -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000). The second line contains n integer numbers a_1, a_2... a_{n} (1 ≤ a_{i} ≤ 100000). -----Output----- Print the number of coprime subsequences of a modulo 10^9 + 7. -----Examples----- Input 3 1 2 3 Output 5 Input 4 1 1 1 1 Output 15 Input 7 1 3 5 15 3 105 35 Output 100 -----Note----- In the first example coprime subsequences are: 1 1, 2 1, 3 1, 2, 3 2, 3 In the second example all subsequences are coprime.
n = int(input()) L = list(map(int, input().split(" "))) D = [0] * 101000 mod = 10**9 + 7 itt = [0] * 101000 p = [0] * 100010 D[0] = 1 for i in range(100010): D[i + 1] = D[i] * 2 % mod for i in range(n): itt[L[i]] += 1 for i in range(1, 100001): for j in range(i * 2, 100001, i): itt[i] += itt[j] p[i] = (D[itt[i]] + mod - 1) % mod i = 100000 while i >= 1: for j in range(i * 2, 100001, i): p[i] -= p[j] p[i] = (p[i] % mod + mod) % mod i -= 1 print(p[1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Let's call a non-empty sequence of positive integers a_1, a_2... a_{k} coprime if the greatest common divisor of all elements of this sequence is equal to 1. Given an array a consisting of n positive integers, find the number of its coprime subsequences. Since the answer may be very large, print it modulo 10^9 + 7. Note that two subsequences are considered different if chosen indices are different. For example, in the array [1, 1] there are 3 different subsequences: [1], [1] and [1, 1]. -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000). The second line contains n integer numbers a_1, a_2... a_{n} (1 ≤ a_{i} ≤ 100000). -----Output----- Print the number of coprime subsequences of a modulo 10^9 + 7. -----Examples----- Input 3 1 2 3 Output 5 Input 4 1 1 1 1 Output 15 Input 7 1 3 5 15 3 105 35 Output 100 -----Note----- In the first example coprime subsequences are: 1 1, 2 1, 3 1, 2, 3 2, 3 In the second example all subsequences are coprime.
import sys mod = 10**9 + 7 def solve(): n = int(input()) a = [int(i) for i in input().split()] cnt = [0] * (10**5 + 1) pat = [0] * (10**5 + 1) p2 = [1] * (n + 1) for i in range(1, n + 1): p2[i] = 2 * p2[i - 1] % mod for ai in a: cnt[ai] += 1 for i in range(1, 10**5 + 1): for j in range(2 * i, 10**5 + 1, i): cnt[i] += cnt[j] pat[i] = p2[cnt[i]] - 1 for i in range(10**5, 0, -1): for j in range(2 * i, 10**5 + 1, i): pat[i] = (pat[i] - pat[j]) % mod ans = pat[1] % mod print(ans) def __starting_point(): solve() __starting_point()
IMPORT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given an integer N. Find the number of distinct XORs it is possible to make using two positive integers no larger than N. Formally, let S be the set S = \{x\oplus y \mid 1 ≤ x, y ≤ N\} where \oplus denotes the [bitwise XOR] operation. Find |S| (where |S| denotes the size of set S. Note that a set, by definition, has no repeated elements). The answer might be large, so output it modulo 10^{9} + 7. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line of input, which contains one integer N. ------ Output Format ------ For each test case, output a single line containing the answer, modulo 10^{9} + 7. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{12}$ ----- Sample Input 1 ------ 3 1 3 7 ----- Sample Output 1 ------ 1 4 8 ----- explanation 1 ------ Test Case 1: $N = 1$, so the only XOR we can possibly make is $1 \oplus 1 = 0$. Thus, the answer is $1$. Test Case 2: $N = 3$, which gives us $S = \{0, 1, 2, 3\}$ as the set of possible XORs. Thus, the answer is $|S| = 4$.
M = 10**9 + 7 t = int(input()) for _ in range(t): n = int(input()) if n <= 2: print(n) else: s = bin(n) num = len(s) - 2 out = pow(2, num, M) if s.count("1") == 1: out -= 1 print(out % M)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR IF FUNC_CALL VAR STRING NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given an integer N. Find the number of distinct XORs it is possible to make using two positive integers no larger than N. Formally, let S be the set S = \{x\oplus y \mid 1 ≤ x, y ≤ N\} where \oplus denotes the [bitwise XOR] operation. Find |S| (where |S| denotes the size of set S. Note that a set, by definition, has no repeated elements). The answer might be large, so output it modulo 10^{9} + 7. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line of input, which contains one integer N. ------ Output Format ------ For each test case, output a single line containing the answer, modulo 10^{9} + 7. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{12}$ ----- Sample Input 1 ------ 3 1 3 7 ----- Sample Output 1 ------ 1 4 8 ----- explanation 1 ------ Test Case 1: $N = 1$, so the only XOR we can possibly make is $1 \oplus 1 = 0$. Thus, the answer is $1$. Test Case 2: $N = 3$, which gives us $S = \{0, 1, 2, 3\}$ as the set of possible XORs. Thus, the answer is $|S| = 4$.
t = int(input()) for i in range(t): n = int(input()) l = 1 mod = 1000000000.0 + 7 if n <= 2: print(n) continue i = 0 j = 40 while j > i: m = (i + j) // 2 if 2**m <= n: l = m i = m + 1 else: j = m l = 2**l % mod if l == n: l = l * 2 % mod l = l - 1 print(int(l)) else: print(int(2 * l % mod))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR
You are given an integer N. Find the number of distinct XORs it is possible to make using two positive integers no larger than N. Formally, let S be the set S = \{x\oplus y \mid 1 ≤ x, y ≤ N\} where \oplus denotes the [bitwise XOR] operation. Find |S| (where |S| denotes the size of set S. Note that a set, by definition, has no repeated elements). The answer might be large, so output it modulo 10^{9} + 7. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line of input, which contains one integer N. ------ Output Format ------ For each test case, output a single line containing the answer, modulo 10^{9} + 7. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{12}$ ----- Sample Input 1 ------ 3 1 3 7 ----- Sample Output 1 ------ 1 4 8 ----- explanation 1 ------ Test Case 1: $N = 1$, so the only XOR we can possibly make is $1 \oplus 1 = 0$. Thus, the answer is $1$. Test Case 2: $N = 3$, which gives us $S = \{0, 1, 2, 3\}$ as the set of possible XORs. Thus, the answer is $|S| = 4$.
m = 1000000007 for i in range(int(input())): n = int(input()) if n == 1: print(1) continue if n == 2: print(2) continue j = 2 while n > j: j *= 2 if j == n: print((j * 2 - 1) % m) else: print(j % m)
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given an integer N. Find the number of distinct XORs it is possible to make using two positive integers no larger than N. Formally, let S be the set S = \{x\oplus y \mid 1 ≤ x, y ≤ N\} where \oplus denotes the [bitwise XOR] operation. Find |S| (where |S| denotes the size of set S. Note that a set, by definition, has no repeated elements). The answer might be large, so output it modulo 10^{9} + 7. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line of input, which contains one integer N. ------ Output Format ------ For each test case, output a single line containing the answer, modulo 10^{9} + 7. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{12}$ ----- Sample Input 1 ------ 3 1 3 7 ----- Sample Output 1 ------ 1 4 8 ----- explanation 1 ------ Test Case 1: $N = 1$, so the only XOR we can possibly make is $1 \oplus 1 = 0$. Thus, the answer is $1$. Test Case 2: $N = 3$, which gives us $S = \{0, 1, 2, 3\}$ as the set of possible XORs. Thus, the answer is $|S| = 4$.
for _ in range(int(input())): n = int(input()) b = bin(n).replace("0b", "") l = len(b) if n == 2: print(2) elif n == 2 ** (l - 1) and n <= 10**12: print((2**l - 1) % (10**9 + 7)) else: print(2**l % (10**9 + 7))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
You are given an integer N. Find the number of distinct XORs it is possible to make using two positive integers no larger than N. Formally, let S be the set S = \{x\oplus y \mid 1 ≤ x, y ≤ N\} where \oplus denotes the [bitwise XOR] operation. Find |S| (where |S| denotes the size of set S. Note that a set, by definition, has no repeated elements). The answer might be large, so output it modulo 10^{9} + 7. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line of input, which contains one integer N. ------ Output Format ------ For each test case, output a single line containing the answer, modulo 10^{9} + 7. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{12}$ ----- Sample Input 1 ------ 3 1 3 7 ----- Sample Output 1 ------ 1 4 8 ----- explanation 1 ------ Test Case 1: $N = 1$, so the only XOR we can possibly make is $1 \oplus 1 = 0$. Thus, the answer is $1$. Test Case 2: $N = 3$, which gives us $S = \{0, 1, 2, 3\}$ as the set of possible XORs. Thus, the answer is $|S| = 4$.
for _ in range(int(input())): n = int(input()) mod = int(1000000000.0 + 7) if n == 1 or n == 2: print(n) elif n & n - 1 == 0: print((2 * n - 1) % mod) else: print(2 ** len(bin(n)[2:]) % mod)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR
You are given an integer N. Find the number of distinct XORs it is possible to make using two positive integers no larger than N. Formally, let S be the set S = \{x\oplus y \mid 1 ≤ x, y ≤ N\} where \oplus denotes the [bitwise XOR] operation. Find |S| (where |S| denotes the size of set S. Note that a set, by definition, has no repeated elements). The answer might be large, so output it modulo 10^{9} + 7. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line of input, which contains one integer N. ------ Output Format ------ For each test case, output a single line containing the answer, modulo 10^{9} + 7. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{12}$ ----- Sample Input 1 ------ 3 1 3 7 ----- Sample Output 1 ------ 1 4 8 ----- explanation 1 ------ Test Case 1: $N = 1$, so the only XOR we can possibly make is $1 \oplus 1 = 0$. Thus, the answer is $1$. Test Case 2: $N = 3$, which gives us $S = \{0, 1, 2, 3\}$ as the set of possible XORs. Thus, the answer is $|S| = 4$.
mod = 10**9 + 7 def exp(x): n = 1 base = 0 while n < x: n *= 2 base += 1 return base def power(x, n): ans = 1 x = x % mod while n: if n & 1: ans = ans * x % mod n = n >> 1 x = x * x % mod return ans for u in range(int(input())): n = int(input()) if n == 1 or n == 2: print(n) else: p = exp(n) if 2**p == n: ans = power(2, p + 1) - 1 else: ans = power(2, p) print(ans)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given an integer N. Find the number of distinct XORs it is possible to make using two positive integers no larger than N. Formally, let S be the set S = \{x\oplus y \mid 1 ≤ x, y ≤ N\} where \oplus denotes the [bitwise XOR] operation. Find |S| (where |S| denotes the size of set S. Note that a set, by definition, has no repeated elements). The answer might be large, so output it modulo 10^{9} + 7. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line of input, which contains one integer N. ------ Output Format ------ For each test case, output a single line containing the answer, modulo 10^{9} + 7. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{12}$ ----- Sample Input 1 ------ 3 1 3 7 ----- Sample Output 1 ------ 1 4 8 ----- explanation 1 ------ Test Case 1: $N = 1$, so the only XOR we can possibly make is $1 \oplus 1 = 0$. Thus, the answer is $1$. Test Case 2: $N = 3$, which gives us $S = \{0, 1, 2, 3\}$ as the set of possible XORs. Thus, the answer is $|S| = 4$.
for _t in range(int(input())): n = int(input()) ans = 1 if n == 2: print("2") continue while ans < n: ans *= 2 if n == ans: ans *= 2 ans -= 1 print(int(ans % (1000000000.0 + 7)))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING WHILE VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER NUMBER
You are given an integer N. Find the number of distinct XORs it is possible to make using two positive integers no larger than N. Formally, let S be the set S = \{x\oplus y \mid 1 ≤ x, y ≤ N\} where \oplus denotes the [bitwise XOR] operation. Find |S| (where |S| denotes the size of set S. Note that a set, by definition, has no repeated elements). The answer might be large, so output it modulo 10^{9} + 7. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line of input, which contains one integer N. ------ Output Format ------ For each test case, output a single line containing the answer, modulo 10^{9} + 7. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{12}$ ----- Sample Input 1 ------ 3 1 3 7 ----- Sample Output 1 ------ 1 4 8 ----- explanation 1 ------ Test Case 1: $N = 1$, so the only XOR we can possibly make is $1 \oplus 1 = 0$. Thus, the answer is $1$. Test Case 2: $N = 3$, which gives us $S = \{0, 1, 2, 3\}$ as the set of possible XORs. Thus, the answer is $|S| = 4$.
from sys import stdin, stdout t = int(stdin.readline()) for i in range(t): n = int(stdin.readline()) my_dict = {(1): 1, (2): 2, (3): 4, (4): 7} if n in my_dict: print(my_dict[n]) else: m = 2 while 1 << m < n: m += 1 if 1 << m == n: print(((1 << m + 1) - 1) % (10**9 + 7)) else: print((1 << m) % (10**9 + 7))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER IF BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
You are given an integer N. Find the number of distinct XORs it is possible to make using two positive integers no larger than N. Formally, let S be the set S = \{x\oplus y \mid 1 ≤ x, y ≤ N\} where \oplus denotes the [bitwise XOR] operation. Find |S| (where |S| denotes the size of set S. Note that a set, by definition, has no repeated elements). The answer might be large, so output it modulo 10^{9} + 7. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line of input, which contains one integer N. ------ Output Format ------ For each test case, output a single line containing the answer, modulo 10^{9} + 7. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{12}$ ----- Sample Input 1 ------ 3 1 3 7 ----- Sample Output 1 ------ 1 4 8 ----- explanation 1 ------ Test Case 1: $N = 1$, so the only XOR we can possibly make is $1 \oplus 1 = 0$. Thus, the answer is $1$. Test Case 2: $N = 3$, which gives us $S = \{0, 1, 2, 3\}$ as the set of possible XORs. Thus, the answer is $|S| = 4$.
l = 10**9 + 7 for i in range(int(input())): n = int(input()) if n == 1: print(1) elif n == 2: print(2) elif n & n - 1 == 0: print((n * 2 - 1) % l) else: j = 2 while j < n: j = j * 2 print(j % l)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given an integer N. Find the number of distinct XORs it is possible to make using two positive integers no larger than N. Formally, let S be the set S = \{x\oplus y \mid 1 ≤ x, y ≤ N\} where \oplus denotes the [bitwise XOR] operation. Find |S| (where |S| denotes the size of set S. Note that a set, by definition, has no repeated elements). The answer might be large, so output it modulo 10^{9} + 7. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line of input, which contains one integer N. ------ Output Format ------ For each test case, output a single line containing the answer, modulo 10^{9} + 7. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{12}$ ----- Sample Input 1 ------ 3 1 3 7 ----- Sample Output 1 ------ 1 4 8 ----- explanation 1 ------ Test Case 1: $N = 1$, so the only XOR we can possibly make is $1 \oplus 1 = 0$. Thus, the answer is $1$. Test Case 2: $N = 3$, which gives us $S = \{0, 1, 2, 3\}$ as the set of possible XORs. Thus, the answer is $|S| = 4$.
try: p = int(input()) while p > 0: p = p - 1 n = int(input()) t = 0 while pow(2, t) <= n: t = t + 1 if n == 2: print(2) elif n == pow(2, t - 1): k = pow(2, t) - 1 print(k % 1000000007) else: d = pow(2, t) print(d % 1000000007) except Exception: pass
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
You are given an integer N. Find the number of distinct XORs it is possible to make using two positive integers no larger than N. Formally, let S be the set S = \{x\oplus y \mid 1 ≤ x, y ≤ N\} where \oplus denotes the [bitwise XOR] operation. Find |S| (where |S| denotes the size of set S. Note that a set, by definition, has no repeated elements). The answer might be large, so output it modulo 10^{9} + 7. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line of input, which contains one integer N. ------ Output Format ------ For each test case, output a single line containing the answer, modulo 10^{9} + 7. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{12}$ ----- Sample Input 1 ------ 3 1 3 7 ----- Sample Output 1 ------ 1 4 8 ----- explanation 1 ------ Test Case 1: $N = 1$, so the only XOR we can possibly make is $1 \oplus 1 = 0$. Thus, the answer is $1$. Test Case 2: $N = 3$, which gives us $S = \{0, 1, 2, 3\}$ as the set of possible XORs. Thus, the answer is $|S| = 4$.
def main(): mod = 10**9 + 7 for _ in range(int(input())): n = int(input()) if n <= 2: print(n) elif n & n - 1: print(pow(2, len(bin(n)) - 2, mod)) else: print((2 * n - 1) % mod) main()
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR
You are given an integer N. Find the number of distinct XORs it is possible to make using two positive integers no larger than N. Formally, let S be the set S = \{x\oplus y \mid 1 ≤ x, y ≤ N\} where \oplus denotes the [bitwise XOR] operation. Find |S| (where |S| denotes the size of set S. Note that a set, by definition, has no repeated elements). The answer might be large, so output it modulo 10^{9} + 7. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line of input, which contains one integer N. ------ Output Format ------ For each test case, output a single line containing the answer, modulo 10^{9} + 7. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{12}$ ----- Sample Input 1 ------ 3 1 3 7 ----- Sample Output 1 ------ 1 4 8 ----- explanation 1 ------ Test Case 1: $N = 1$, so the only XOR we can possibly make is $1 \oplus 1 = 0$. Thus, the answer is $1$. Test Case 2: $N = 3$, which gives us $S = \{0, 1, 2, 3\}$ as the set of possible XORs. Thus, the answer is $|S| = 4$.
T = int(input()) for _ in range(T): n = int(input()) if n == 1 or n == 2: print(n) else: i = 2 while i <= n: if 2**i == n: print((2 ** (i + 1) - 1) % 1000000007) break elif 2**i > n: print(2**i % 1000000007) break else: i += 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER
You are given an integer N. Find the number of distinct XORs it is possible to make using two positive integers no larger than N. Formally, let S be the set S = \{x\oplus y \mid 1 ≤ x, y ≤ N\} where \oplus denotes the [bitwise XOR] operation. Find |S| (where |S| denotes the size of set S. Note that a set, by definition, has no repeated elements). The answer might be large, so output it modulo 10^{9} + 7. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line of input, which contains one integer N. ------ Output Format ------ For each test case, output a single line containing the answer, modulo 10^{9} + 7. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{12}$ ----- Sample Input 1 ------ 3 1 3 7 ----- Sample Output 1 ------ 1 4 8 ----- explanation 1 ------ Test Case 1: $N = 1$, so the only XOR we can possibly make is $1 \oplus 1 = 0$. Thus, the answer is $1$. Test Case 2: $N = 3$, which gives us $S = \{0, 1, 2, 3\}$ as the set of possible XORs. Thus, the answer is $|S| = 4$.
for jkfd in range(int(input())): n = int(input()) if n == 1: print(1) continue elif n == 2: print(2) continue elif n == 3: print(4) continue p = False x = 4 asdf = [(2**i) for i in range(2, 100)] if n in asdf: p = True while x < n: if x == n: p = True x *= 2 n = bin(n)[2:] if p: ans = 2 ** len(n) - 1 else: ans = 2 ** len(n) print(ans % 1000000007)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
You are given an integer N. Find the number of distinct XORs it is possible to make using two positive integers no larger than N. Formally, let S be the set S = \{x\oplus y \mid 1 ≤ x, y ≤ N\} where \oplus denotes the [bitwise XOR] operation. Find |S| (where |S| denotes the size of set S. Note that a set, by definition, has no repeated elements). The answer might be large, so output it modulo 10^{9} + 7. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line of input, which contains one integer N. ------ Output Format ------ For each test case, output a single line containing the answer, modulo 10^{9} + 7. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{12}$ ----- Sample Input 1 ------ 3 1 3 7 ----- Sample Output 1 ------ 1 4 8 ----- explanation 1 ------ Test Case 1: $N = 1$, so the only XOR we can possibly make is $1 \oplus 1 = 0$. Thus, the answer is $1$. Test Case 2: $N = 3$, which gives us $S = \{0, 1, 2, 3\}$ as the set of possible XORs. Thus, the answer is $|S| = 4$.
def nextPowerOf2(n): count = 0 if n and not n & n - 1: return n while n != 0: n >>= 1 count += 1 return 1 << count t = int(input()) for i in range(t): modulo = 10**9 + 7 n = int(input()) if n == 1: print(1) elif n == 2: print(2) else: x = nextPowerOf2(n) if x == n: print((x * 2 - 1) % modulo) else: print(x % modulo)
FUNC_DEF ASSIGN VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR WHILE VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given an integer N. Find the number of distinct XORs it is possible to make using two positive integers no larger than N. Formally, let S be the set S = \{x\oplus y \mid 1 ≤ x, y ≤ N\} where \oplus denotes the [bitwise XOR] operation. Find |S| (where |S| denotes the size of set S. Note that a set, by definition, has no repeated elements). The answer might be large, so output it modulo 10^{9} + 7. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line of input, which contains one integer N. ------ Output Format ------ For each test case, output a single line containing the answer, modulo 10^{9} + 7. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{12}$ ----- Sample Input 1 ------ 3 1 3 7 ----- Sample Output 1 ------ 1 4 8 ----- explanation 1 ------ Test Case 1: $N = 1$, so the only XOR we can possibly make is $1 \oplus 1 = 0$. Thus, the answer is $1$. Test Case 2: $N = 3$, which gives us $S = \{0, 1, 2, 3\}$ as the set of possible XORs. Thus, the answer is $|S| = 4$.
def bin_exp(n, p): mod = int(10**9) + 7 pwrs = [n] for i in range(60): pwrs.append(pwrs[-1] * pwrs[-1] % mod) i = 0 prod = 1 while p: if p & 1 == 1: prod = prod * pwrs[i] % mod i += 1 p = p // 2 return prod t = int(input()) for _ in range(t): n = int(input()) dig = 0 ones = 0 while n: if n & 1 == 1: ones += 1 dig += 1 n = n // 2 sol = bin_exp(2, dig) if ones == 1: sol -= 1 if dig == 2: sol -= 1 print(sol)
FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an integer N. Find the number of distinct XORs it is possible to make using two positive integers no larger than N. Formally, let S be the set S = \{x\oplus y \mid 1 ≤ x, y ≤ N\} where \oplus denotes the [bitwise XOR] operation. Find |S| (where |S| denotes the size of set S. Note that a set, by definition, has no repeated elements). The answer might be large, so output it modulo 10^{9} + 7. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line of input, which contains one integer N. ------ Output Format ------ For each test case, output a single line containing the answer, modulo 10^{9} + 7. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{12}$ ----- Sample Input 1 ------ 3 1 3 7 ----- Sample Output 1 ------ 1 4 8 ----- explanation 1 ------ Test Case 1: $N = 1$, so the only XOR we can possibly make is $1 \oplus 1 = 0$. Thus, the answer is $1$. Test Case 2: $N = 3$, which gives us $S = \{0, 1, 2, 3\}$ as the set of possible XORs. Thus, the answer is $|S| = 4$.
def setbits(a): c = 0 while a > 0: c += 1 a &= a - 1 return c for tc in range(int(input())): n = int(input()) mod = 1000000007 if n <= 3: if n == 1: print(1) elif n == 2: print(2) elif n == 3: print(4) else: most = 0 for r in range(1, 60): if n >> r & 1 == 1: most = max(most, r) r = 1 << most if setbits(n) == 1: print((2 * r - 1) % mod) else: print(2 * r % mod)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR
You are given an integer N. Find the number of distinct XORs it is possible to make using two positive integers no larger than N. Formally, let S be the set S = \{x\oplus y \mid 1 ≤ x, y ≤ N\} where \oplus denotes the [bitwise XOR] operation. Find |S| (where |S| denotes the size of set S. Note that a set, by definition, has no repeated elements). The answer might be large, so output it modulo 10^{9} + 7. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line of input, which contains one integer N. ------ Output Format ------ For each test case, output a single line containing the answer, modulo 10^{9} + 7. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{12}$ ----- Sample Input 1 ------ 3 1 3 7 ----- Sample Output 1 ------ 1 4 8 ----- explanation 1 ------ Test Case 1: $N = 1$, so the only XOR we can possibly make is $1 \oplus 1 = 0$. Thus, the answer is $1$. Test Case 2: $N = 3$, which gives us $S = \{0, 1, 2, 3\}$ as the set of possible XORs. Thus, the answer is $|S| = 4$.
for _ in range(int(input())): n = int(input()) if n == 1: print(1) elif n == 2: print(2) else: for i in range(1, n): if n < 2**i: break if n == 2 ** (i - 1): ans = 2 * n - 1 print(ans % 1000000007) else: ans = 2**i print(ans % 1000000007)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP NUMBER VAR IF VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
You are given an integer N. Find the number of distinct XORs it is possible to make using two positive integers no larger than N. Formally, let S be the set S = \{x\oplus y \mid 1 ≤ x, y ≤ N\} where \oplus denotes the [bitwise XOR] operation. Find |S| (where |S| denotes the size of set S. Note that a set, by definition, has no repeated elements). The answer might be large, so output it modulo 10^{9} + 7. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line of input, which contains one integer N. ------ Output Format ------ For each test case, output a single line containing the answer, modulo 10^{9} + 7. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{12}$ ----- Sample Input 1 ------ 3 1 3 7 ----- Sample Output 1 ------ 1 4 8 ----- explanation 1 ------ Test Case 1: $N = 1$, so the only XOR we can possibly make is $1 \oplus 1 = 0$. Thus, the answer is $1$. Test Case 2: $N = 3$, which gives us $S = \{0, 1, 2, 3\}$ as the set of possible XORs. Thus, the answer is $|S| = 4$.
mod = int(1000000000.0 + 7) for _ in range(int(input())): n = int(input()) print( n if n < 3 else pow(2, len(bin(n)) - 2, mod) if n & n - 1 else (2 * n - 1) % mod )
ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR