inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
|---|---|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
t=int(input())
for i in range(t):
n,m=map(int,input().split())
if n == 1 or m==1:print("YES")
elif n==2 and m==2:print("YES")
else:print("NO")
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
for f in range(int(input())):
n,m=map(int,input().split())
if n==1 or m==1 or (n==2 and m==2):
print("YES")
else:
print("NO")
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
for _ in range(int(input())):
n, m = list(map(int, input().split()))
print( "YES" if min(n, m) == 1 or max(n, m) <= 2 else "NO" )
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
t = int(input())
for i in range(t):
n, m = map(int, input().split())
if n != 1 and m != 1 and n*m != 4:
print("NO")
else:
print("YES")
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
t = int(input())
for case in range(t):
n, m = list(map(int, input().split()))
if (min(n, m) == 1):
print('YES')
elif n == m and n == 2:
print('YES')
else:
print('NO')
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
import sys
readline = sys.stdin.readline
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: list(map(int, readline().split()))
nl = lambda: list(map(int, readline().split()))
def solve():
n, m = nm()
if min(n, m) == 1 or max(n, m) == 2:
print('YES')
else:
print('NO')
# solve()
T = ni()
for _ in range(T):
solve()
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
t = int(input())
for i in range(t):
n, m = list(map(int, input().split()))
if n == 1 or m == 1 or (n == 2 and m == 2):
print("YES")
else:
print("NO")
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
import sys
T = int(sys.stdin.readline().strip())
def getT(line):
return map(int, line.strip().split(" "))
for t in range(T):
(m,n) = getT(sys.stdin.readline())
if min(m, n) == 1: print("YES")
elif min(m, n) == 2 and max(m, n) == 2: print("YES")
else: print("NO")
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
for _ in range(int(input())):
a,b=map(int,input().split())
if min(a,b)==1:
print('YES')
elif a==2 and b==2:
print('YES')
else:
print('NO')
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
#from sys import stdin, stdout, setrecursionlimit
#input = stdin.readline
#print = stdout.write
for _ in range(int(input())):
n, m = list(map(int, input().split()))
ans = 'NO'
if n == 1 or m == 1 or (n == 2 and m == 2):
ans = 'YES'
print(ans)
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
for _ in range(int(input())):
a, b = list(map(int, input().split()))
if a == 1 or b == 1:
print('YES')
elif a == b == 2:
print('YES')
else:
print('NO')
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
def solve():
N,M = list(map(int,input().split()))
if N==1 or M==1:
print("YES")
elif N==2 and M==2:
print("YES")
else:
print("NO")
for _ in range(int(input())):
solve()
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
def main():
n, m = map(int, input().split())
if n == 2 and m == 2:
print("YES")
else:
if n == 1 or m == 1:
print("YES")
else:
print("NO")
def __starting_point():
t = int(input())
for i in range(t):
main()
__starting_point()
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
t = int(input())
for i10 in range(t):
n, m = list(map(int, input().split()))
if n == 1 or m == 1 or n + m == 4:
print("YES")
else:
print("NO")
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
for _ in range(int(input())):
n, m = list(map(int, input().split()))
if n == 2 and m == 2:
print("YES")
continue
if n == 1 or m == 1:
print("YES")
continue
print("NO")
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
t = int(input())
for fk in range(t):
n, m = [int(x) for x in input().split()]
if n == 1 or m == 1:
print('YES')
elif n==2 and m == 2:
print('YES')
else : print('NO')
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
q = int(input())
for _ in range(q):
n, m = list(map(int, input().split()))
if n == 2 and m == 2 or n == 1 or m == 1:
print("YES")
else:
print("NO")
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
q = int(input())
for i in range(q):
n, m = list(map(int, input().split()))
if (n == 1 or m == 1):
print("YES")
elif (n == m == 2):
print("YES")
else:
print("NO")
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
n=int(input())
for i in range(n):
a,b=[int(i) for i in input().split()]
if (a==b==2) or a==1 or b==1:
print('YES')
else:
print('NO')
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
t = int(input())
for u in range(t):
n,m=list(map(int,input().split()))
x = 2*n+2*m
y = 3*n*m
z = n*m
if x+z >= y:
print("YES")
else:
print("NO")
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
for _ in range(int(input())):
n, m = tuple(map(int, input().split()))
a = (n - 1) * m + (m - 1) * n
b = n * m
if a <= b:
print('YES')
else:
print('NO')
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
t = int(input())
for case in range(t):
n, m = map(int, input().split())
ans = 'NO'
if (n == m == 2):
ans = 'YES'
elif (n == 1 or m == 1):
ans = 'YES'
print (ans)
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
t = int(input())
for case in range(t):
n, m = list(map(int, input().split()))
perimeter = 2*n + 2*m
inside = m*(n-1) + n*(m-1)
nobs = 2*n*m
if (nobs > perimeter):
print ("NO")
else:
print ("YES")
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
for _ in range(int(input())):
n, m = list(map(int, input().split()))
if n * m <= n + m:
print("YES")
else:
print("NO")
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
t = int(input())
for i in range(t):
n, m = list(map(int, input().split()))
if min(n, m) == 1 or m==2 and n==2:
print("YES")
else:
print("NO")
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
# n = int(input())
# l = list(map(int, input().split()))
for tt in range(int(input())):
n, m = map(int, input().split())
if(n==1 or m==1 or (n==2 and m==2)):
print("YES")
continue
print("NO")
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
for _ in range(int(input())):
n, m = list(map(int, input().split()))
if n == m and n == 2:
print('YES')
elif n >= 2 and m >= 2:
print('NO')
else:
print('YES')
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
for _ in range(int(input())):
n, m = map(int, input().split())
print('YES' if n == 1 or m == 1 or (n == 2 and m == 2) else 'NO')
|
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$
The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
-----Input-----
The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β the number of test cases. Next $t$ lines contain descriptions of test cases.
Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$).
-----Output-----
For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
3
1 3
100000 100000
2 2
Output
YES
NO
YES
-----Note-----
For the first test case, this is an example solution: [Image]
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
|
import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
ORDA = 97 #a
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return [int(i) for i in input().split()]
def lcm(a, b): return abs(a * b) // math.gcd(a, b)
def revn(n): return str(n)[::-1]
def dd(): return collections.defaultdict(int)
def ddl(): return collections.defaultdict(list)
def sieve(n):
if n < 2: return list()
prime = [True for _ in range(n + 1)]
p = 3
while p * p <= n:
if prime[p]:
for i in range(p * 2, n + 1, p):
prime[i] = False
p += 2
r = [2]
for p in range(3, n + 1, 2):
if prime[p]:
r.append(p)
return r
def divs(n, start=1):
r = []
for i in range(start, int(math.sqrt(n) + 1)):
if (n % i == 0):
if (n / i == i):
r.append(i)
else:
r.extend([i, n // i])
return r
def divn(n, primes):
divs_number = 1
for i in primes:
if n == 1:
return divs_number
t = 1
while n % i == 0:
t += 1
n //= i
divs_number *= t
def prime(n):
if n == 2: return True
if n % 2 == 0 or n <= 1: return False
sqr = int(math.sqrt(n)) + 1
for d in range(3, sqr, 2):
if n % d == 0: return False
return True
def convn(number, base):
newnumber = 0
while number > 0:
newnumber += number % base
number //= base
return newnumber
def cdiv(n, k): return n // k + (n % k != 0)
for _ in range(ii()):
n, m = mi()
if n == 1 or m == 1 or m == 2 and n == 2:
print('YES')
else:
print('NO')
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
tests = int(input())
for test in range(tests):
n = int(input())
a = [int(i) for i in input().split()]
d = {}
for i in range(n):
s = 0
while a[i] % 2 == 0:
a[i] //= 2
s += 1
if a[i] in list(d.keys()):
d[a[i]] = max(s, d[a[i]])
else:
d[a[i]] = s
s = 0
for i in list(d.keys()):
s += d[i]
print(s)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
t=int(input())
for g in range(t):
n=int(input())
a=list(map(int,input().split()))
b=list()
for i in range(n):
while a[i]%2==0:
b.append(a[i])
a[i]=a[i]//2
b.sort()
count=1
for i in range(len(b)-1):
if b[i]!=b[i+1]:
count+=1
if len(b)==0:
print(0)
else:
print(count)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
t=int(input())
def power(n):
res=0
while n%2==0:
res+=1
n//=2
if n not in d:
d[n]=0
d[n]=max(d[n],res)
for i in range(t):
n=int(input())
a=list(map(int,input().split()))
maxx=0
d={}
for num in a:
power(num)
print(sum(list(d.values())))
# print(maxx)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
for _ in range(int(input())):
d = dict()
N = int(input())
a = list(map(int, input().split()))
for i in range(N):
c = 0
tmp = a[i]
while tmp % 2 != 1:
tmp = tmp // 2
c += 1
if tmp in d:
d[tmp] = max(d[tmp], c)
else:
d[tmp] = c
res = 0
for i in list(d.keys()):
res += d[i]
print(res)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
from collections import defaultdict
def f(n):
st = 0
while n % 2 == 0:
n //= 2
st += 1
return n, st
t = int(input())
for _ in range(t):
n = int(input())
d = defaultdict(int)
for i in input().split():
el = int(i)
os, st = f(el)
d[os] = max(d[os], st)
s = 0
for el in list(d.values()):
s += el
print(s)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
t = int(input())
for j in range(t):
n = int(input())
a = list(map(int, input().split()))
s = set()
ans = 0
for i in range(n):
k = a[i]
while k % 2 == 0 and k not in s:
s.add(k)
k = k // 2
ans += 1
print(ans)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
t = int(input())
for g in range(t):
n = int(input())
st = set()
a = [int(i) for i in input().split()]
for i in range(n):
q = a[i]
while q % 2 == 0:
st.add(q)
q //= 2
print(len(st))
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
def f(x):
tmp = x
z = 0
while tmp % 2 == 0:
tmp //= 2
z += 1
return [tmp, z]
for i in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
sl = dict()
for x in a:
y, z = f(x)
if sl.get(y) == None:
sl[y] = z
else:
sl[y] = max(sl[y], z)
ans = 0
for x in sl.keys():
ans += sl[x]
print(ans)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
for q in range(int(input())):
n = int(input())
line = list(map(int, input().split()))
Q = dict()
for i in range(n):
l = 0
r = 100
while r - l > 1:
m = (l + r) // 2
if line[i] % (1 << m) == 0:
l = m
else:
r = m
f = line[i] // (1 << l)
if f in Q:
Q[f] = max(Q[f], l)
else:
Q[f] = l
Q = list(Q.items())
ans = 0
for a, b in Q:
ans += b
print(ans)
#print(Q)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
import heapq
import sys
input = lambda : sys.stdin.readline()
for i in range(int(input())):
n = int(input())
s = set()
h = []
for i in map(int,input().split()):
if i%2==0:
if i in s:
continue
s.add(i)
heapq.heappush(h,-i)
ans = 0
while h:
i = -heapq.heappop(h)//2
ans+=1
if i % 2 == 0:
if i in s:
continue
s.add(i)
heapq.heappush(h, -i)
print(ans)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
t = int(input())
for _ in range(t):
used_q = set()
n = int(input())
nums = list(map(int,input().split(' ')))
for i in range(len(nums)):
q = nums[i]
while q % 2 == 0:
if q in used_q:
q = q // 2
else:
used_q.add(q)
q = q // 2
print(len(used_q))
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
for _ in range(int(input())):
n = int(input())
A = list(map(int, input().split()))
dell = []
for i in range(n):
new = 0
while A[i] % 2 != 1:
A[i] //= 2
new += 1
dell.append([A[i], new])
dicter = {}
for el in dell:
if el[1] > dicter.get(el[0], -1):
dicter[el[0]] = el[1]
ans = 0
for el in dicter:
ans += dicter[el]
print(ans)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
b = dict()
for j in range(n):
if a[j] % 2 == 0:
b[a[j]] = b.get(a[j], 0) + 1
k = 0
for key in b:
c = key
while c % 2 == 0:
k += 1
c = c // 2
if c in b.keys():
break
print(k)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
t=int(input())
for r in range(t):
q=input()
a=list(map(int,input().split()))
d=dict()
for w in a:
s=0
while w%2==0:
w//=2
s+=1
if w in list(d.keys()):
d[w]=max([d[w],s])
else:
d[w]=s
e=0
for w in list(d.keys()):
e+=d[w]
print(e)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
import sys
import math
import heapq
def input():
return sys.stdin.readline().strip()
def iinput():
return int(input())
def tinput():
return input().split()
def rinput():
return list(map(int, tinput()))
def rlinput():
return list(rinput())
def main():
n, w, q, res = iinput(), set(), [], 0
for i in rinput():
if i % 2 == 0:
if i not in w:
w.add(i)
heapq.heappush(q, -i)
while q:
i = -heapq.heappop(q) // 2
res += 1
if i % 2 == 0:
if i not in w:
w.add(i)
heapq.heappush(q, -i)
print(res)
for i in range(iinput()):
main()
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
for __ in range(int(input())):
n = int(input())
ar = list(map(int, input().split()))
ar1 = []
ar2 = []
for elem in ar:
num = 0
while elem % 2 == 0:
elem //= 2
num += 1
ar1.append(num)
ar2.append(elem)
ar3 = []
for i in range(n):
ar3.append([ar2[i], ar1[i]])
ar3.sort()
i = 1
j = 1
num = 1
ans = sum(ar1)
while i < n:
while j < n and ar3[j][0] == ar3[j - 1][0]:
j += 1
times = j - i
prev_val = 0
for h in range(i - 1, min(j, n)):
ans -= times * (ar3[h][1] - prev_val)
times -= 1
prev_val = ar3[h][1]
i = j + 1
j = i
print(ans)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
a = int(input())
for i in range(a):
s1 = set()
ans = 0
l = input()
now = input().split()
for i in now:
k =int(i)
while k%2==0 and k not in s1:
s1.add(k)
k=k//2
print(len(s1))
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
def main():
m = int(input())
for i in range(m):
n = int(input())
nums = map(int, input().split())
arr = {}
for j in nums:
base = j
step = 0
while not base & 1:
base >>= 1
step += 1
if not base in arr:
arr[base] = step
else:
arr[base] = max(arr[base], step)
print(sum(arr.values()))
def __starting_point():
main()
__starting_point()
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
t = int(input())
ans = []
for _ in range(t):
n = int(input())
m = list(map(int, input().split()))
d = {}
for el1 in m:
el = el1
c = 0
while (el%2==0):
el//=2
c+=1
if (el in list(d.keys())):
d[el] = max(d[el], c)
else:
d[el] = c
s = 0
for el in d:
s+=d[el]
ans.append(s)
for el in ans:
print(el)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
t=int(input())
for j in range(t):
n=int(input())
a=(list(map(int,input().split())))
a.sort()
s=set()
s1=set(a)
ans=0
l=n
while l>0:
now=a.pop()
l-=1
if now not in s and now%2==0:
s.add(now)
ans+=1
if now//2 not in s1:
s1.add(now//2)
a.append(now//2)
l+=1
print(ans)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
t = int(input())
for i in range(0, t):
n = int(input())
a = list(map(int, input().split()))
b = []
for j in range(0, n):
if a[j] % 2 == 0:
num = 0
k = a[j]
while k % 2 == 0:
k //= 2
num += 1
b.append([k, num])
b.sort()
ans = 0
length = len(b)
for q in range(0, length - 1):
if b[q][0] != b[q + 1][0]:
ans += b[q][1]
if length != 0:
print(ans + b[length - 1][1])
else:
print(ans)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
k = int(input())
def absolute() :
c = dict()
m = 0
for i in [int(x) for x in input().split()] :
q = 0
if i % 2 != 0 : continue
while i % 2 == 0 :
i //= 2
q += 1
if c.get(i, 0) < q :
m += q - c.get(i, 0)
c[i] = q
#print(c)
return m
for j in range(k) :
input()
print(absolute())
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
def ck(a):
ans=0
while a%2==0:
a=a//2
ans+=1
return([a,ans])
t=int(input())
for _ in range(t):
n=int(input())
a=list(map(int,input().split()))
c={}
for i in range(n):
x,y=ck(a[i])
if c.get(x)==None:
c[x]=y
elif c.get(x)<y:
c[x]=y
ans=sum(c.values())
print(ans)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
def res(e):
ans = 0
e1 = int(e)
while e1 % 2 == 0:
e1 //= 2
ans += 1
return 2 ** ans
for i in range(int(input())):
n = int(input())
s = list([x for x in list(map(int, input().split())) if x % 2 == 0])
if len(s) == 0:
print(0)
else:
temp = list([x // res(x) for x in s])
ans = 0
s1 = set()
while temp != s:
for i1 in range(len(s)):
if temp[i1] == s[i1]:
continue
elif temp[i1] not in s1:
s1.add(temp[i1])
ans += 1
temp[i1] *= 2
elif temp[i1] in s1:
temp[i1] *= 2
print(ans)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
t = int(input())
for i in range(0, t):
n = int(input())
data = list(map(int, input().split()))
d = dict()
for j in range(0, n):
a = data[j]
count = 0
while a % 2 == 0:
a = a // 2
count += 1
d[a] = max(d.get(a, 0), count)
print(sum(d.values()))
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
t = int(input())
for i in range(0, t):
n = int(input())
data = list(map(int, input().split()))
d = dict()
for j in range(0, n):
a = data[j]
count = 0
while a % 2 == 0:
a = a // 2
count += 1
d[a] = max(d.get(a, 0), count)
print(sum(d.values()))
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
t = int(input())
for i in range(t):
n = int(input())
l = list(map(int, input().split()))
s = set()
d = {}
for a in l:
j = 0
while (a % 2) == 0:
a = a // 2
j += 1
s.add(a)
if a in d:
if d[a] < j:
d[a] = j
else:
d[a] = j
p = 0
for q in d:
p += d[q]
print(p)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
from sys import stdin as s
for i in range(int(s.readline())):
n=int(s.readline())
l=sorted([i for i in set(map(int,s.readline().split())) if i%2==0],reverse=True)
t=set()
c=0
for i in l:
if i not in t:
t.add(i)
while i%2==0:
i//=2
t.add(i)
c+=1
print(c)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
from collections import Counter
def primfacs(n):
if n % 2 == 0:
primfac = [0,0]
else:
primfac = [0,0]
while n % 2 == 0:
n = n / 2
primfac[0] += 1
primfac[1] = n
return primfac
t = int(input())
for i in range(t):
n = int(input())
A = list(map(int, input().split()))
Ost = []
for j in range(n):
Ost.append(primfacs(A[j]))
Ost.sort()
d = {}
for j in range(len(Ost)):
d[Ost[j][1]] = Ost[j][0]
print(sum(list(d.values())))
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
t = int(input())
for i in range(t):
n = int(input())
a = set(map(int, input().split()))
#print(a)
even_numbers = {x for x in a if x % 2 == 0}
used_numbers = set()
k = 0
for x in even_numbers:
while x % 2 == 0 and x not in used_numbers:
used_numbers.add(x)
x //= 2
k += 1
print(k)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
from collections import Counter
import heapq
t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
rep = Counter()
ans = 0
heap = []
for i in range(len(a)):
rep[a[i]] += 1
if rep[a[i]] == 1:
heapq.heappush(heap, -a[i])
while heap:
x = -heapq.heappop(heap)
if x % 2 == 0:
dx = x // 2
if rep[dx] == 0:
heapq.heappush(heap, -dx)
rep[dx] = 1
else:
rep[dx] += rep[x]
ans += 1
print(ans)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
t=int(input())
for i in range(t):
n=int(input())
a=list([bin(int(x))[2:] for x in input().split()])
d=dict()
for i in a:
ir=i.rfind("1")
c=len(i)-ir-1
raw=int(i[:ir+1],base=2)
d[raw]=max(d.get(raw,c),c)
print(sum(d.values()))
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
def factorize(x):
tmp = x
cnt = 0
while (tmp % 2 == 0):
tmp //= 2
cnt += 1
return tmp, cnt
n = int(input())
for i in range(n):
k = int(input())
x = dict()
cnt = 0
tmp = list(map(int, input().split()))
for j in tmp:
g, v = factorize(j)
try:
x[g] = max(x[g], v)
except:
x[g] = v
for c in list(x.keys()):
cnt += x[c]
print(cnt)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
t = int(input())
for i in range(t):
n = int(input())
a = [int(j) for j in input().split()]
used = set()
for j in a:
if j%2==1:
continue
while j%2==0 and j not in used:
used.add(j)
j /= 2
print(len(used))
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
t=int(input())
for _ in range(t):
n=int(input())
a=[int (i) for i in input().split()]
d=dict()
su=0
for i in a:
k=0
while i%2==0:
i=i//2
k+=1
if i not in d:
d[i]=k
else:
d[i]=max(d[i],k)
for i in list(d.values()):
su+=i
print(su)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
t = int(input())
for i in range(t):
ans = 0
n = int(input())
a = list(map(int, input().split()))
for j in range(n):
count = 0
while a[j] % 2 == 0:
a[j] = a[j] // 2
count += 1
a[j] = [a[j], count]
a.sort()
j = 0
while j != n:
m = a[j][1]
while j + 1 < n and a[j][0] == a[j + 1][0]:
m = max([a[j][1], a[j + 1][1]])
j+=1
j+=1
ans += m
print(ans)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
n = int(input())
for i in range(n):
answer = 0
d = set()
m = int(input())
arr = [int(x) for x in input().split()]
for j in arr:
if j % 2 == 0:
if j not in d:
d.add(j)
s = list(d)
s.sort(reverse=True)
for j in s:
ch = j // 2
answer += 1
while ch % 2 == 0:
if ch not in d:
ch //= 2
answer += 1
else:
break
print(answer)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
t = int(input())
ans_l = []
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
ar = set()
for i in a:
if i % 2 == 0:
x = i
ar.add(x)
while x % 2 == 0:
ar.add(x)
x //= 2
ans_l.append(len(ar))
print(*ans_l, sep='\n')
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
a = int(input())
for i in range(a):
f = int(input())
k = list(map(int, input().split()))
l = set()
ch = 0
lol = 0
for i in range(len(k)):
lol = k[i]
while lol % 2 == 0:
l.add(lol)
lol /= 2
print(len(l))
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
def razl(a):
if a % 2 == 0:
r = [0, 0]
else:
r = [0, 0]
while a % 2 == 0:
a = a / 2
r[0] += 1
r[1] = a
return r
ans = []
for i in range(int(input())):
a = int(input())
b = list(map(int, input().split()))
c = []
for j in range(a):
c.append(razl(b[j]))
c.sort()
d = {}
for j in range(len(c)):
d[c[j][1]] = c[j][0]
ans.append(sum(list(d.values())))
for i in ans:
print(i)
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
def f(n):
minn = 0
maxx = 30
mid = 10
while mid != minn:
if n // (2 ** mid) == n / (2 ** mid):
minn = mid
mid = (minn + maxx) // 2
else:
maxx = mid
mid = (minn + maxx) // 2
return mid
t = int(input())
for i in range(t):
d = dict()
n = int(input())
a = set(map(int, input().split()))
for j in a:
p = f(j)
if j // (2 ** p) in d:
if p > d[j // (2 ** p)]:
d[j // (2 ** p)] = p
else:
d[j // (2 ** p)] = p
print(sum(d.values()))
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
def ans():
nonlocal lst
d = dict()
for i in lst:
s2, delit = st2(i)
if delit not in d:
d[delit] = s2
continue
if d[delit] < s2:
d[delit] = s2
return sum(d.values())
def st2(num):
c = 0
while (num%2==0) and num != 0:
num = num >> 1
c += 1
return [c, num]
lst = []
for i in range(int(input())):
t = int(input())
lst = list(map(int, input().split()))
print(ans())
|
There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$.
For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move.
You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$).
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) β the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$.
-----Output-----
For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$).
-----Example-----
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
-----Note-----
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ β all numbers are odd.
Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
|
t = int(input())
answers = [0] * t
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
arr = [[] for _ in range(n)]
ans = 0
for j in range(n):
pow1 = 0
cur = a[j]
while cur % 2 == 0:
cur //= 2
pow1 += 1
arr[j] = [cur, pow1]
arr.sort(reverse=True)
cur_nech = -1
for j in range(n):
if arr[j][0] != cur_nech:
ans += arr[j][1]
cur_nech = arr[j][0]
answers[i] = ans
print(*answers, sep='\n')
|
Acacius is studying strings theory. Today he came with the following problem.
You are given a string $s$ of length $n$ consisting of lowercase English letters and question marks. It is possible to replace question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once?
Each question mark should be replaced with exactly one lowercase English letter. For example, string "a?b?c" can be transformed into strings "aabbc" and "azbzc", but can't be transformed into strings "aabc", "a?bbc" and "babbc".
Occurrence of a string $t$ of length $m$ in the string $s$ of length $n$ as a substring is a index $i$ ($1 \leq i \leq n - m + 1$) such that string $s[i..i+m-1]$ consisting of $m$ consecutive symbols of $s$ starting from $i$-th equals to string $t$. For example string "ababa" has two occurrences of a string "aba" as a substring with $i = 1$ and $i = 3$, but there are no occurrences of a string "aba" in the string "acba" as a substring.
Please help Acacius to check if it is possible to replace all question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once.
-----Input-----
First line of input contains an integer $T$ ($1 \leq T \leq 5000$), number of test cases. $T$ pairs of lines with test case descriptions follow.
The first line of a test case description contains a single integer $n$ ($7 \leq n \leq 50$), length of a string $s$.
The second line of a test case description contains string $s$ of length $n$ consisting of lowercase English letters and question marks.
-----Output-----
For each test case output an answer for it.
In case if there is no way to replace question marks in string $s$ with a lowercase English letters in such a way that there is exactly one occurrence of a string "abacaba" in the resulting string as a substring output "No".
Otherwise output "Yes" and in the next line output a resulting string consisting of $n$ lowercase English letters. If there are multiple possible strings, output any.
You may print every letter in "Yes" and "No" in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answer).
-----Example-----
Input
6
7
abacaba
7
???????
11
aba?abacaba
11
abacaba?aba
15
asdf???f???qwer
11
abacabacaba
Output
Yes
abacaba
Yes
abacaba
Yes
abadabacaba
Yes
abacabadaba
No
No
-----Note-----
In first example there is exactly one occurrence of a string "abacaba" in the string "abacaba" as a substring.
In second example seven question marks can be replaced with any seven lowercase English letters and with "abacaba" in particular.
In sixth example there are two occurrences of a string "abacaba" as a substring.
|
import sys
INF = 10**20
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
from math import gcd
from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br
"""
Facts and Data representation
Constructive? Top bottom up down
"""
def check(s):
t = 'abacaba'
ans = 0
for i in range(len(s)):
if s[i: i + 7] == t:
ans += 1
return ans
def solve():
n, = I()
s = input()
t = 'abacaba'
cnt = check(s)
if cnt > 1:
print('No')
return
elif cnt == 1:
s = list(s)
for i in range(n):
if s[i] == '?':
s[i] = 'z'
print('Yes')
print(''.join(s))
else:
s = list(s)
ok = s[::]
for i in range(n - 6):
ok = s[::]
for j in range(7):
if s[i + j] == t[j]:
continue
elif s[i + j] == '?':
ok[i + j] = t[j]
else:
break
else:
for i in range(n):
if ok[i] == '?':
ok[i] = 'z'
ok = ''.join(ok)
if check(ok) != 1:
continue
print('Yes')
print(ok)
return
print('No')
t, = I()
while t:
t -= 1
solve()
|
Acacius is studying strings theory. Today he came with the following problem.
You are given a string $s$ of length $n$ consisting of lowercase English letters and question marks. It is possible to replace question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once?
Each question mark should be replaced with exactly one lowercase English letter. For example, string "a?b?c" can be transformed into strings "aabbc" and "azbzc", but can't be transformed into strings "aabc", "a?bbc" and "babbc".
Occurrence of a string $t$ of length $m$ in the string $s$ of length $n$ as a substring is a index $i$ ($1 \leq i \leq n - m + 1$) such that string $s[i..i+m-1]$ consisting of $m$ consecutive symbols of $s$ starting from $i$-th equals to string $t$. For example string "ababa" has two occurrences of a string "aba" as a substring with $i = 1$ and $i = 3$, but there are no occurrences of a string "aba" in the string "acba" as a substring.
Please help Acacius to check if it is possible to replace all question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once.
-----Input-----
First line of input contains an integer $T$ ($1 \leq T \leq 5000$), number of test cases. $T$ pairs of lines with test case descriptions follow.
The first line of a test case description contains a single integer $n$ ($7 \leq n \leq 50$), length of a string $s$.
The second line of a test case description contains string $s$ of length $n$ consisting of lowercase English letters and question marks.
-----Output-----
For each test case output an answer for it.
In case if there is no way to replace question marks in string $s$ with a lowercase English letters in such a way that there is exactly one occurrence of a string "abacaba" in the resulting string as a substring output "No".
Otherwise output "Yes" and in the next line output a resulting string consisting of $n$ lowercase English letters. If there are multiple possible strings, output any.
You may print every letter in "Yes" and "No" in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answer).
-----Example-----
Input
6
7
abacaba
7
???????
11
aba?abacaba
11
abacaba?aba
15
asdf???f???qwer
11
abacabacaba
Output
Yes
abacaba
Yes
abacaba
Yes
abadabacaba
Yes
abacabadaba
No
No
-----Note-----
In first example there is exactly one occurrence of a string "abacaba" in the string "abacaba" as a substring.
In second example seven question marks can be replaced with any seven lowercase English letters and with "abacaba" in particular.
In sixth example there are two occurrences of a string "abacaba" as a substring.
|
import sys
t = int(input())
req = 'abacaba'
for _ in range(t):
n = int(sys.stdin.readline())
s = sys.stdin.readline().rstrip()
cnt = 0
for i in range(n-6):
if s[i:i+7] == req:
cnt += 1
if cnt == 1:
print('Yes')
print(s.replace('?', 'z'))
continue
if cnt > 1:
print('No')
continue
for i in range(n-6):
if all(c1 == c2 or c1 == '?' for c1, c2 in zip(s[i:i+7], req)):
if s[i+7:i+11] == 'caba' or i >= 4 and s[i-4:i] == 'abac':
continue
s = s[:i] + req + s[i+7:]
print('Yes')
print(s.replace('?', 'z'))
break
else:
print('No')
|
Acacius is studying strings theory. Today he came with the following problem.
You are given a string $s$ of length $n$ consisting of lowercase English letters and question marks. It is possible to replace question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once?
Each question mark should be replaced with exactly one lowercase English letter. For example, string "a?b?c" can be transformed into strings "aabbc" and "azbzc", but can't be transformed into strings "aabc", "a?bbc" and "babbc".
Occurrence of a string $t$ of length $m$ in the string $s$ of length $n$ as a substring is a index $i$ ($1 \leq i \leq n - m + 1$) such that string $s[i..i+m-1]$ consisting of $m$ consecutive symbols of $s$ starting from $i$-th equals to string $t$. For example string "ababa" has two occurrences of a string "aba" as a substring with $i = 1$ and $i = 3$, but there are no occurrences of a string "aba" in the string "acba" as a substring.
Please help Acacius to check if it is possible to replace all question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once.
-----Input-----
First line of input contains an integer $T$ ($1 \leq T \leq 5000$), number of test cases. $T$ pairs of lines with test case descriptions follow.
The first line of a test case description contains a single integer $n$ ($7 \leq n \leq 50$), length of a string $s$.
The second line of a test case description contains string $s$ of length $n$ consisting of lowercase English letters and question marks.
-----Output-----
For each test case output an answer for it.
In case if there is no way to replace question marks in string $s$ with a lowercase English letters in such a way that there is exactly one occurrence of a string "abacaba" in the resulting string as a substring output "No".
Otherwise output "Yes" and in the next line output a resulting string consisting of $n$ lowercase English letters. If there are multiple possible strings, output any.
You may print every letter in "Yes" and "No" in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answer).
-----Example-----
Input
6
7
abacaba
7
???????
11
aba?abacaba
11
abacaba?aba
15
asdf???f???qwer
11
abacabacaba
Output
Yes
abacaba
Yes
abacaba
Yes
abadabacaba
Yes
abacabadaba
No
No
-----Note-----
In first example there is exactly one occurrence of a string "abacaba" in the string "abacaba" as a substring.
In second example seven question marks can be replaced with any seven lowercase English letters and with "abacaba" in particular.
In sixth example there are two occurrences of a string "abacaba" as a substring.
|
#
# ------------------------------------------------
# ____ _ Generatered using
# / ___| | |
# | | __ _ __| | ___ _ __ ______ _
# | | / _` |/ _` |/ _ \ '_ \|_ / _` |
# | |__| (_| | (_| | __/ | | |/ / (_| |
# \____\____|\____|\___|_| |_/___\____|
#
# GNU Affero General Public License v3.0
# ------------------------------------------------
# Author : prophet
# Created : 2020-07-19 05:12:32.701664
# UUID : fZpWYlRPKqbpTDmt
# ------------------------------------------------
#
production = True
import sys, math, collections
def input(input_format = 0, multi = 0):
if multi > 0: return [input(input_format) for i in range(multi)]
else:
next_line = sys.stdin.readline()[:-1]
if input_format >= 10:
use_list = False
input_format = int(str(input_format)[-1])
else: use_list = True
if input_format == 0: formatted_input = [next_line]
elif input_format == 1: formatted_input = list(map(int, next_line.split()))
elif input_format == 2: formatted_input = list(map(float, next_line.split()))
elif input_format == 3: formatted_input = list(next_line)
elif input_format == 4: formatted_input = list(map(int, list(next_line)))
elif input_format == 5: formatted_input = next_line.split()
else: formatted_input = [next_line]
return formatted_input if use_list else formatted_input[0]
def out(output_line, output_format = 0, newline = True):
formatted_output = ""
if output_format == 0: formatted_output = str(output_line)
elif output_format == 1: formatted_output = " ".join(map(str, output_line))
elif output_format == 2: formatted_output = "\n".join(map(str, output_line))
print(formatted_output, end = "\n" if newline else "")
def log(*args):
if not production:
print("$$$", end = "")
print(*args)
enu = enumerate
ter = lambda a, b, c: b if a else c
ceil = lambda a, b: -(-a // b)
def mapl(iterable, format = 0):
if format == 0: return list(map(int, iterable))
elif format == 1: return list(map(str, iterable))
elif format == 2: return list(map(list, iterable))
#
# >>>>>>>>>>>>>>> START OF SOLUTION <<<<<<<<<<<<<<
#
def ch(a, r, n):
c = 0
for i in range(n - 6):
y = a[i:i + 7]
if y == r:
c += 1
return c == 1
def solve():
n = input(11)
a = input(3)
r = list("abacaba")
for i in range(n - 6):
y = a[i:i + 7]
for x, z in zip(y, r):
if not (x == "?" or x == z):
break
else:
s = a[:i] + r + a[i + 7:]
if ch(s, r, n):
u = ""
for j in s:
if j == "?":
u += "z"
else:
u += j
out("Yes")
out(u)
return
out("No")
return
for i in range(input(11)): solve()
# solve()
#
# >>>>>>>>>>>>>>>> END OF SOLUTION <<<<<<<<<<<<<<<
#
|
Acacius is studying strings theory. Today he came with the following problem.
You are given a string $s$ of length $n$ consisting of lowercase English letters and question marks. It is possible to replace question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once?
Each question mark should be replaced with exactly one lowercase English letter. For example, string "a?b?c" can be transformed into strings "aabbc" and "azbzc", but can't be transformed into strings "aabc", "a?bbc" and "babbc".
Occurrence of a string $t$ of length $m$ in the string $s$ of length $n$ as a substring is a index $i$ ($1 \leq i \leq n - m + 1$) such that string $s[i..i+m-1]$ consisting of $m$ consecutive symbols of $s$ starting from $i$-th equals to string $t$. For example string "ababa" has two occurrences of a string "aba" as a substring with $i = 1$ and $i = 3$, but there are no occurrences of a string "aba" in the string "acba" as a substring.
Please help Acacius to check if it is possible to replace all question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once.
-----Input-----
First line of input contains an integer $T$ ($1 \leq T \leq 5000$), number of test cases. $T$ pairs of lines with test case descriptions follow.
The first line of a test case description contains a single integer $n$ ($7 \leq n \leq 50$), length of a string $s$.
The second line of a test case description contains string $s$ of length $n$ consisting of lowercase English letters and question marks.
-----Output-----
For each test case output an answer for it.
In case if there is no way to replace question marks in string $s$ with a lowercase English letters in such a way that there is exactly one occurrence of a string "abacaba" in the resulting string as a substring output "No".
Otherwise output "Yes" and in the next line output a resulting string consisting of $n$ lowercase English letters. If there are multiple possible strings, output any.
You may print every letter in "Yes" and "No" in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answer).
-----Example-----
Input
6
7
abacaba
7
???????
11
aba?abacaba
11
abacaba?aba
15
asdf???f???qwer
11
abacabacaba
Output
Yes
abacaba
Yes
abacaba
Yes
abadabacaba
Yes
abacabadaba
No
No
-----Note-----
In first example there is exactly one occurrence of a string "abacaba" in the string "abacaba" as a substring.
In second example seven question marks can be replaced with any seven lowercase English letters and with "abacaba" in particular.
In sixth example there are two occurrences of a string "abacaba" as a substring.
|
def f(s):
t="abacaba"
for i in range(7):
if s[i]!="?" and t[i]!=s[i]:return False
return True
def g(s):
c=0
for i in range(7,len(s)+1):
if s[i-7:i]=="abacaba":c+=1
return c
for _ in range(int(input())):
n=int(input())
s=input()
if g(s)>1:
print("No")
continue
if "abacaba" in s:
print("Yes")
print(s.replace("?","z"))
continue
flag=False
for i in range(7,len(s)+1):
if f(s[i-7:i]):
t=(s[:i-7]+"abacaba"+s[i:]).replace("?","z")
if g(t)>1:continue
print("Yes")
print(t)
flag=True
break
if not(flag):print("No")
|
Acacius is studying strings theory. Today he came with the following problem.
You are given a string $s$ of length $n$ consisting of lowercase English letters and question marks. It is possible to replace question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once?
Each question mark should be replaced with exactly one lowercase English letter. For example, string "a?b?c" can be transformed into strings "aabbc" and "azbzc", but can't be transformed into strings "aabc", "a?bbc" and "babbc".
Occurrence of a string $t$ of length $m$ in the string $s$ of length $n$ as a substring is a index $i$ ($1 \leq i \leq n - m + 1$) such that string $s[i..i+m-1]$ consisting of $m$ consecutive symbols of $s$ starting from $i$-th equals to string $t$. For example string "ababa" has two occurrences of a string "aba" as a substring with $i = 1$ and $i = 3$, but there are no occurrences of a string "aba" in the string "acba" as a substring.
Please help Acacius to check if it is possible to replace all question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once.
-----Input-----
First line of input contains an integer $T$ ($1 \leq T \leq 5000$), number of test cases. $T$ pairs of lines with test case descriptions follow.
The first line of a test case description contains a single integer $n$ ($7 \leq n \leq 50$), length of a string $s$.
The second line of a test case description contains string $s$ of length $n$ consisting of lowercase English letters and question marks.
-----Output-----
For each test case output an answer for it.
In case if there is no way to replace question marks in string $s$ with a lowercase English letters in such a way that there is exactly one occurrence of a string "abacaba" in the resulting string as a substring output "No".
Otherwise output "Yes" and in the next line output a resulting string consisting of $n$ lowercase English letters. If there are multiple possible strings, output any.
You may print every letter in "Yes" and "No" in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answer).
-----Example-----
Input
6
7
abacaba
7
???????
11
aba?abacaba
11
abacaba?aba
15
asdf???f???qwer
11
abacabacaba
Output
Yes
abacaba
Yes
abacaba
Yes
abadabacaba
Yes
abacabadaba
No
No
-----Note-----
In first example there is exactly one occurrence of a string "abacaba" in the string "abacaba" as a substring.
In second example seven question marks can be replaced with any seven lowercase English letters and with "abacaba" in particular.
In sixth example there are two occurrences of a string "abacaba" as a substring.
|
check="abacaba"
def compare(s,t):
res=True
for i in range(len(s)):
res&=(s[i]==t[i] or s[i]=="?" or t[i]=="?")
return res
for _ in range(int(input())):
n=int(input())
s=input()
ans="No"
res=""
for i in range(n-6):
t=s
test=t[i:i+7]
if compare(test,check):
t=s[:i]+check+s[i+7:]
t=t.replace("?","z")
count=0
for j in range(n-6):
if t[j:j+7]==check:
count+=1
if count==1:
ans="Yes"
res=t
print(ans)
if ans=="Yes":
print(res)
|
Acacius is studying strings theory. Today he came with the following problem.
You are given a string $s$ of length $n$ consisting of lowercase English letters and question marks. It is possible to replace question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once?
Each question mark should be replaced with exactly one lowercase English letter. For example, string "a?b?c" can be transformed into strings "aabbc" and "azbzc", but can't be transformed into strings "aabc", "a?bbc" and "babbc".
Occurrence of a string $t$ of length $m$ in the string $s$ of length $n$ as a substring is a index $i$ ($1 \leq i \leq n - m + 1$) such that string $s[i..i+m-1]$ consisting of $m$ consecutive symbols of $s$ starting from $i$-th equals to string $t$. For example string "ababa" has two occurrences of a string "aba" as a substring with $i = 1$ and $i = 3$, but there are no occurrences of a string "aba" in the string "acba" as a substring.
Please help Acacius to check if it is possible to replace all question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once.
-----Input-----
First line of input contains an integer $T$ ($1 \leq T \leq 5000$), number of test cases. $T$ pairs of lines with test case descriptions follow.
The first line of a test case description contains a single integer $n$ ($7 \leq n \leq 50$), length of a string $s$.
The second line of a test case description contains string $s$ of length $n$ consisting of lowercase English letters and question marks.
-----Output-----
For each test case output an answer for it.
In case if there is no way to replace question marks in string $s$ with a lowercase English letters in such a way that there is exactly one occurrence of a string "abacaba" in the resulting string as a substring output "No".
Otherwise output "Yes" and in the next line output a resulting string consisting of $n$ lowercase English letters. If there are multiple possible strings, output any.
You may print every letter in "Yes" and "No" in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answer).
-----Example-----
Input
6
7
abacaba
7
???????
11
aba?abacaba
11
abacaba?aba
15
asdf???f???qwer
11
abacabacaba
Output
Yes
abacaba
Yes
abacaba
Yes
abadabacaba
Yes
abacabadaba
No
No
-----Note-----
In first example there is exactly one occurrence of a string "abacaba" in the string "abacaba" as a substring.
In second example seven question marks can be replaced with any seven lowercase English letters and with "abacaba" in particular.
In sixth example there are two occurrences of a string "abacaba" as a substring.
|
def count(string, substring):
count = 0
start = 0
while start < len(string):
pos = string.find(substring, start)
if pos != -1:
start = pos + 1
count += 1
else:
break
return count
for _ in range(int(input())):
n = int(input())
os = input()
good = False
for i in range(n):
if (os[i] == "a" or os[i] == "?") and i <= n-7:
s = list(os)
bad = False
for j in range(i, i+7):
if s[j] != "?" and s[j] != "abacaba"[j-i]:
bad = True
break
s[j] = "abacaba"[j-i]
if bad:
continue
ans = "".join(s).replace("?", "z")
if count(ans, "abacaba") == 1:
good = True
break
if good:
print("Yes")
print(ans)
else:
print("No")
|
You are given an array $a$ consisting of $n$ integers numbered from $1$ to $n$.
Let's define the $k$-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length $k$ (recall that a subsegment of $a$ of length $k$ is a contiguous part of $a$ containing exactly $k$ elements). If there is no integer occuring in all subsegments of length $k$ for some value of $k$, then the $k$-amazing number is $-1$.
For each $k$ from $1$ to $n$ calculate the $k$-amazing number of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of elements in the array. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) β the elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case print $n$ integers, where the $i$-th integer is equal to the $i$-amazing number of the array.
-----Example-----
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1
|
input=__import__('sys').stdin.readline
for _ in range(int(input())):
n=int(input())
s=list(map(int,input().split()))
g=[[-1]for _ in range(n+1)]
for i in range(n):
g[s[i]].append(i)
inf=10**10
ans=[-1]*n
lstunused=n
for i in range(1,n+1):
g[i].append(n)
mx=0
for j in range(1,len(g[i])):
mx=max(mx,g[i][j]-g[i][j-1]-1)
for j in range(mx,lstunused):
ans[j]=i
lstunused=min(lstunused,mx)
print(*ans)
|
You are given an array $a$ consisting of $n$ integers numbered from $1$ to $n$.
Let's define the $k$-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length $k$ (recall that a subsegment of $a$ of length $k$ is a contiguous part of $a$ containing exactly $k$ elements). If there is no integer occuring in all subsegments of length $k$ for some value of $k$, then the $k$-amazing number is $-1$.
For each $k$ from $1$ to $n$ calculate the $k$-amazing number of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of elements in the array. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) β the elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case print $n$ integers, where the $i$-th integer is equal to the $i$-amazing number of the array.
-----Example-----
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1
|
import sys
def main():
#n = iinput()
#k = iinput()
#m = iinput()
n = int(sys.stdin.readline().strip())
#n, k = rinput()
#n, m = rinput()
#m, k = rinput()
#n, k, m = rinput()
#n, m, k = rinput()
#k, n, m = rinput()
#k, m, n = rinput()
#m, k, n = rinput()
#m, n, k = rinput()
q = list(map(int, sys.stdin.readline().split()))
#q = linput()
clovar, p, x = {}, [], 1e9
for i in range(n):
if q[i] in clovar:
clovar[q[i]].append(i)
else:
clovar[q[i]] = [i]
for o in clovar:
t = clovar[o]
ma = max(t[0] + 1, n - t[-1])
dlinat = len(t) - 1
for i in range(dlinat):
ma = max(t[i + 1] - t[i], ma)
p.append([ma, o])
p.sort()
ans = [p[0]]
dlinap = len(p)
for i in range(1, dlinap):
if ans[-1][0] != p[i][0]:
ans.append(p[i])
ans.append([n + 1, 1e9])
dlina_1 = ans[0][0] - 1
print(*[-1 for i in range(dlina_1)], end=" ")
dlinaans = len(ans) - 1
for i in range(dlinaans):
x = min(x, ans[i][1])
dlinax = ans[i + 1][0] - ans[i][0]
print(*[x for o in range(dlinax)], end=" ")
print()
for i in range(int(sys.stdin.readline().strip()) ):
main()
|
You are given an array $a$ consisting of $n$ integers numbered from $1$ to $n$.
Let's define the $k$-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length $k$ (recall that a subsegment of $a$ of length $k$ is a contiguous part of $a$ containing exactly $k$ elements). If there is no integer occuring in all subsegments of length $k$ for some value of $k$, then the $k$-amazing number is $-1$.
For each $k$ from $1$ to $n$ calculate the $k$-amazing number of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of elements in the array. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) β the elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case print $n$ integers, where the $i$-th integer is equal to the $i$-amazing number of the array.
-----Example-----
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1
|
import sys
import math
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
S = {}
for el in arr:
S[el] = [0]
for i in range(len(arr)):
S[arr[i]].append(i+1)
G = {}
for key in S:
S[key].append(n+1)
best = 0
for i in range(len(S[key]) - 1):
gap = abs(S[key][i] - S[key][i+1])
best = max(gap, best)
G[key] = best
#print(G)
B = {}
for key in G:
l = G[key]
if l not in B:
B[l] = key
else:
B[l] = min(B[l], key)
ans = []
for key in B:
ans.append((key, B[key]))
ans.sort()
pp = []
low = 9999999999999999
j = 0
for i in range(1, n+1):
if j<len(ans) and i==ans[j][0]:
if ans[j][1] < low:
low = ans[j][1]
j += 1
if low > 10**10:
pp.append(-1)
else:
pp.append(low)
print(*pp)
|
You are given an array $a$ consisting of $n$ integers numbered from $1$ to $n$.
Let's define the $k$-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length $k$ (recall that a subsegment of $a$ of length $k$ is a contiguous part of $a$ containing exactly $k$ elements). If there is no integer occuring in all subsegments of length $k$ for some value of $k$, then the $k$-amazing number is $-1$.
For each $k$ from $1$ to $n$ calculate the $k$-amazing number of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of elements in the array. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) β the elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case print $n$ integers, where the $i$-th integer is equal to the $i$-amazing number of the array.
-----Example-----
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1
|
from collections import defaultdict
T = int(input())
for t in range(T):
N = int(input())
A = [int(_) for _ in input().split()]
els = sorted(set(A))
pos = defaultdict(list)
for i, el in enumerate(A):
pos[el].append(i)
DMAX = {}
for el in list(pos.keys()):
dmax = -1
arr = [-1] + sorted(pos[el]) + [N]
for i in range(1, len(arr)):
dmax = max(dmax, arr[i] - arr[i-1])
DMAX[el] = dmax
ci = 0
answer = []
for i in range(N-1, -1, -1):
while ci < len(els) and DMAX[els[ci]] > i+1:
ci += 1
if ci >= len(els):
answer.append(-1)
else:
answer.append(els[ci])
print(' '.join(map(str, answer[::-1])))
|
You are given an array $a$ consisting of $n$ integers numbered from $1$ to $n$.
Let's define the $k$-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length $k$ (recall that a subsegment of $a$ of length $k$ is a contiguous part of $a$ containing exactly $k$ elements). If there is no integer occuring in all subsegments of length $k$ for some value of $k$, then the $k$-amazing number is $-1$.
For each $k$ from $1$ to $n$ calculate the $k$-amazing number of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of elements in the array. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) β the elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case print $n$ integers, where the $i$-th integer is equal to the $i$-amazing number of the array.
-----Example-----
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1
|
t = int(input())
for case in range(t):
n = int(input())
a = [int(x) - 1 for x in input().split()]
last_occ = [-1 for _ in range(n)]
max_dist = [float('-inf') for _ in range(n)]
for i, x in enumerate(a):
max_dist[x] = max(max_dist[x], i - last_occ[x])
last_occ[x] = i
for x in a:
max_dist[x] = max(max_dist[x], n - last_occ[x])
inverted = [float('inf') for _ in range(n)]
for x in a:
inverted[max_dist[x] - 1] = min(inverted[max_dist[x] - 1], x)
best = float('inf')
for x in inverted:
if x != float('inf'):
best = min(x, best)
if best == float('inf'):
print(-1, end=' ')
else:
print(best + 1, end=' ')
print()
|
You are given an array $a$ consisting of $n$ integers numbered from $1$ to $n$.
Let's define the $k$-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length $k$ (recall that a subsegment of $a$ of length $k$ is a contiguous part of $a$ containing exactly $k$ elements). If there is no integer occuring in all subsegments of length $k$ for some value of $k$, then the $k$-amazing number is $-1$.
For each $k$ from $1$ to $n$ calculate the $k$-amazing number of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of elements in the array. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) β the elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case print $n$ integers, where the $i$-th integer is equal to the $i$-amazing number of the array.
-----Example-----
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1
|
import sys
sys.setrecursionlimit(1000000)
T = int(input())
for _ in range(T):
n = int(input())
a = [int(x) - 1 for x in input().split()]
prev = [-1 for _ in range(n)]
val = [1 for _ in range(n)]
for i, x in enumerate(a):
delta = i - prev[x]
val[x] = max(val[x], delta)
prev[x] = i
for i in range(n):
val[i] = max(val[i], n - prev[i])
ans = [-1 for _ in range(n + 1)]
r = n + 1
for i in range(n):
if val[i] < r:
for j in range(val[i], r):
ans[j] = i + 1
r = val[i]
print(' '.join([str(x) for x in ans[1:]]))
|
You are given an array $a$ consisting of $n$ integers numbered from $1$ to $n$.
Let's define the $k$-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length $k$ (recall that a subsegment of $a$ of length $k$ is a contiguous part of $a$ containing exactly $k$ elements). If there is no integer occuring in all subsegments of length $k$ for some value of $k$, then the $k$-amazing number is $-1$.
For each $k$ from $1$ to $n$ calculate the $k$-amazing number of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of elements in the array. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) β the elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case print $n$ integers, where the $i$-th integer is equal to the $i$-amazing number of the array.
-----Example-----
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1
|
for qq in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
last = [-1] * (n+1)
dura = [-1] * (n+1)
for i in range(n):
dura[a[i]] = max(dura[a[i]], i-last[a[i]]-1)
last[a[i]] = i
for i in range(n+1):
dura[i] = max(dura[i], n-last[i]-1)
ans = [n+1] * n
for i in range(n+1):
if dura[i]==n: continue
ans[dura[i]] = min(ans[dura[i]], i)
for i in range(n-1):
ans[i+1] = min(ans[i+1], ans[i])
for i in range(n):
if ans[i]==n+1: ans[i] = -1
print(*ans)
|
You are given an array $a$ consisting of $n$ integers numbered from $1$ to $n$.
Let's define the $k$-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length $k$ (recall that a subsegment of $a$ of length $k$ is a contiguous part of $a$ containing exactly $k$ elements). If there is no integer occuring in all subsegments of length $k$ for some value of $k$, then the $k$-amazing number is $-1$.
For each $k$ from $1$ to $n$ calculate the $k$-amazing number of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of elements in the array. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) β the elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case print $n$ integers, where the $i$-th integer is equal to the $i$-amazing number of the array.
-----Example-----
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1
|
INF = 10 ** 15
for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
d = {i: 0 for i in arr}
last = {i: -1 for i in arr}
for i in range(n):
if last[arr[i]] == -1:
d[arr[i]] = max(d[arr[i]], i + 1)
else:
d[arr[i]] = max(d[arr[i]], i - last[arr[i]])
last[arr[i]] = i
for i in list(last.keys()):
d[i] = max(d[i], n - last[i])
# print(d)
d2 = {}
for k, v in list(d.items()):
if v not in d2:
d2[v] = INF
d2[v] = min(d2[v], k)
# print(d2)
ans = [INF] * n
for i in range(1, n + 1):
can = INF
if i != 1:
can = ans[i - 2]
if i in list(d2.keys()):
can = min(can, d2[i])
ans[i - 1] = can
for i in range(n):
if ans[i] == INF:
ans[i] = -1
print(*ans)
|
You are given an array $a$ consisting of $n$ integers numbered from $1$ to $n$.
Let's define the $k$-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length $k$ (recall that a subsegment of $a$ of length $k$ is a contiguous part of $a$ containing exactly $k$ elements). If there is no integer occuring in all subsegments of length $k$ for some value of $k$, then the $k$-amazing number is $-1$.
For each $k$ from $1$ to $n$ calculate the $k$-amazing number of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of elements in the array. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) β the elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case print $n$ integers, where the $i$-th integer is equal to the $i$-amazing number of the array.
-----Example-----
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1
|
import sys
import math
def II():
return int(sys.stdin.readline())
def LI():
return list(map(int, sys.stdin.readline().split()))
def MI():
return map(int, sys.stdin.readline().split())
def SI():
return sys.stdin.readline().strip()
t = II()
for q in range(t):
n = II()
a = LI()
d = [[] for i in range(n+1)]
d2 = [0]*(n+1)
dp = [0]*(n+1)
for i in range(n):
if d2[a[i]] == 0:
d2[a[i]] = 1
d[a[i]].append(i)
dp[a[i]] = i+1
else:
d2[a[i]]+=1
dp[a[i]] = max(dp[a[i]], i-d[a[i]][-1])
d[a[i]].append(i)
for i in range(n):
dp[a[i]] = max(dp[a[i]], n-d[a[i]][-1])
ans = [-1]*(n+1)
temp = -1
for i in range(n+1):
if ans[dp[i]] == -1:
ans[dp[i]] = i
temp = -1
for i in range(1,n+1):
if ans[i]!=-1:
if temp == -1:
temp = ans[i]
elif ans[i]<temp:
temp = ans[i]
else:
ans[i] = temp
else:
ans[i] = temp
print(*ans[1:])
|
You are given an array $a$ consisting of $n$ integers numbered from $1$ to $n$.
Let's define the $k$-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length $k$ (recall that a subsegment of $a$ of length $k$ is a contiguous part of $a$ containing exactly $k$ elements). If there is no integer occuring in all subsegments of length $k$ for some value of $k$, then the $k$-amazing number is $-1$.
For each $k$ from $1$ to $n$ calculate the $k$-amazing number of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of elements in the array. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) β the elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case print $n$ integers, where the $i$-th integer is equal to the $i$-amazing number of the array.
-----Example-----
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1
|
for anynumber in range(int(input())):
n = int(input())
l = list(map(int,input().split()))
d = {}
for (index, i) in enumerate(l):
if i not in d.keys():
d[i] = [index+1,index]
else:
d[i] = [max(index-d[i][1], d[i][0]),index]
for i in d.keys():
d[i] = max(d[i][0], n-d[i][1])
ans = [-1 for i in range(n)]
for i in sorted(d.keys(), reverse=True):
ans[d[i]-1] = i
for i in range(1,n):
if ans[i] == -1:
ans[i] = ans[i-1]
elif ans[i-1] != -1:
if ans[i-1]<ans[i]:
ans[i] = ans[i-1]
for i in range(n-1):
print(ans[i],end=" ")
print(ans[n-1])
|
You are given an array $a$ consisting of $n$ integers numbered from $1$ to $n$.
Let's define the $k$-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length $k$ (recall that a subsegment of $a$ of length $k$ is a contiguous part of $a$ containing exactly $k$ elements). If there is no integer occuring in all subsegments of length $k$ for some value of $k$, then the $k$-amazing number is $-1$.
For each $k$ from $1$ to $n$ calculate the $k$-amazing number of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of elements in the array. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) β the elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case print $n$ integers, where the $i$-th integer is equal to the $i$-amazing number of the array.
-----Example-----
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1
|
t = int(input())
for i in range(t):
n = int(input())
a = [int(x) for x in input().split()]
dct = {}
for i in a:
dct[i] = (-1, 0)
now = 0
for i in a:
dct[i] = [now, max(dct[i][1], now - dct[i][0])]
now += 1
for i in dct:
dct[i] = max(dct[i][1], (n - dct[i][0]))
a = [(dct[i], i) for i in dct]
a.sort()
mini = 1000000000000000
now = 0
q = len(a)
for i in range(1, n + 1):
while now < q and a[now][0] == i:
mini = min(mini, a[now][1])
now += 1
if mini == 1000000000000000:
print(-1,end=' ')
else:
print(mini,end=' ')
print()
|
You are given an array $a$ consisting of $n$ integers numbered from $1$ to $n$.
Let's define the $k$-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length $k$ (recall that a subsegment of $a$ of length $k$ is a contiguous part of $a$ containing exactly $k$ elements). If there is no integer occuring in all subsegments of length $k$ for some value of $k$, then the $k$-amazing number is $-1$.
For each $k$ from $1$ to $n$ calculate the $k$-amazing number of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of elements in the array. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) β the elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case print $n$ integers, where the $i$-th integer is equal to the $i$-amazing number of the array.
-----Example-----
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1
|
t = int(input())
for w in range(t):
n = int(input())
a = tuple(map(int, input().split()))
d = {}
for i, x in enumerate(a):
if x not in d:
d[x] = [i + 1, i + 1]
else:
d[x] = [i + 1, max(d[x][1], i + 1 - d[x][0])]
l = len(a) + 1
for i in d:
d[i] = max(d[i][1], l - d[i][0])
z = {}
for i, x in list(d.items()):
if x in z:
if z[x] > i:
z[x] = i
else:
z[x] = i
q = [-1 for x in range(n)]
for i, x in list(z.items()):
q[i - 1] = x
q1 = []
m = -1
for x in q:
if x == -1:
q1.append(m)
else:
if m != -1:
m = min(m, x)
else:
m = x
q1.append(m)
print(' '.join(str(x) for x in q1))
|
You are given an array $a$ consisting of $n$ integers numbered from $1$ to $n$.
Let's define the $k$-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length $k$ (recall that a subsegment of $a$ of length $k$ is a contiguous part of $a$ containing exactly $k$ elements). If there is no integer occuring in all subsegments of length $k$ for some value of $k$, then the $k$-amazing number is $-1$.
For each $k$ from $1$ to $n$ calculate the $k$-amazing number of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of elements in the array. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) β the elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case print $n$ integers, where the $i$-th integer is equal to the $i$-amazing number of the array.
-----Example-----
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1
|
t = int(input())
for _ in range(t):
n = int(input())
a = [0] + list(map(int, input().split()))
period = [0 for i in range(n+1)]
first = [-1 for i in range(n+1)]
last = [-1 for i in range(n+1)]
for i in range(1, len(a)):
b = a[i]
if first[b] == -1:
first[b] = i
last[b] = i
else:
period[b] = max(period[b], i - last[b])
last[b] = i
for i in range(1, len(period)):
period[i] = max(period[i], n-last[i]+1)
period = period[1:]
l = sorted(list(e if e[0] > first[e[1]] else (first[e[1]], e[1]) for e in zip(period, list(range(1, n+1))) if e[0] > 0))
ans = []
AA = n+5
ind = 0
for i in range(1, n+1):
if ind < len(l) and l[ind][0] == i:
AA = min(AA, l[ind][1])
ans.append(-1 if AA == n+5 else AA)
while ind < len(l) and l[ind][0] == i:
ind += 1
print(*ans)
|
You are given a string $s$ of even length $n$. String $s$ is binary, in other words, consists only of 0's and 1's.
String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones ($n$ is even).
In one operation you can reverse any substring of $s$. A substring of a string is a contiguous subsequence of that string.
What is the minimum number of operations you need to make string $s$ alternating? A string is alternating if $s_i \neq s_{i + 1}$ for all $i$. There are two types of alternating strings in general: 01010101... or 10101010...
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$; $n$ is even)Β β the length of string $s$.
The second line of each test case contains a binary string $s$ of length $n$ ($s_i \in$ {0, 1}). String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the minimum number of operations to make $s$ alternating.
-----Example-----
Input
3
2
10
4
0110
8
11101000
Output
0
1
2
-----Note-----
In the first test case, string 10 is already alternating.
In the second test case, we can, for example, reverse the last two elements of $s$ and get: 0110 $\rightarrow$ 0101.
In the third test case, we can, for example, make the following two operations: 11101000 $\rightarrow$ 10101100; 10101100 $\rightarrow$ 10101010.
|
t = int(input())
for i in range(t):
n = int(input())
s = input()
ans = 0
for y in range(1, n):
if s[y] == s[y-1]:
ans += 1
print((ans + ans % 2) // 2)
|
You are given a string $s$ of even length $n$. String $s$ is binary, in other words, consists only of 0's and 1's.
String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones ($n$ is even).
In one operation you can reverse any substring of $s$. A substring of a string is a contiguous subsequence of that string.
What is the minimum number of operations you need to make string $s$ alternating? A string is alternating if $s_i \neq s_{i + 1}$ for all $i$. There are two types of alternating strings in general: 01010101... or 10101010...
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$; $n$ is even)Β β the length of string $s$.
The second line of each test case contains a binary string $s$ of length $n$ ($s_i \in$ {0, 1}). String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the minimum number of operations to make $s$ alternating.
-----Example-----
Input
3
2
10
4
0110
8
11101000
Output
0
1
2
-----Note-----
In the first test case, string 10 is already alternating.
In the second test case, we can, for example, reverse the last two elements of $s$ and get: 0110 $\rightarrow$ 0101.
In the third test case, we can, for example, make the following two operations: 11101000 $\rightarrow$ 10101100; 10101100 $\rightarrow$ 10101010.
|
import sys
input=sys.stdin.readline
for _ in range(int(input())):
n = int(input())
s = input().strip()
c = 0
for i in range(n-1):
if s[i] == s[i+1]:
c += 1
print((c+1)//2)
|
You are given a string $s$ of even length $n$. String $s$ is binary, in other words, consists only of 0's and 1's.
String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones ($n$ is even).
In one operation you can reverse any substring of $s$. A substring of a string is a contiguous subsequence of that string.
What is the minimum number of operations you need to make string $s$ alternating? A string is alternating if $s_i \neq s_{i + 1}$ for all $i$. There are two types of alternating strings in general: 01010101... or 10101010...
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$; $n$ is even)Β β the length of string $s$.
The second line of each test case contains a binary string $s$ of length $n$ ($s_i \in$ {0, 1}). String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the minimum number of operations to make $s$ alternating.
-----Example-----
Input
3
2
10
4
0110
8
11101000
Output
0
1
2
-----Note-----
In the first test case, string 10 is already alternating.
In the second test case, we can, for example, reverse the last two elements of $s$ and get: 0110 $\rightarrow$ 0101.
In the third test case, we can, for example, make the following two operations: 11101000 $\rightarrow$ 10101100; 10101100 $\rightarrow$ 10101010.
|
t = int(input())
for q in range(t):
n = int(input())
s = input()
a, b = 0, 0
for i in range(n - 1):
if s[i] == s[i + 1]:
if s[i] == '0':
a += 1
else:
b += 1
print(max(a, b))
|
You are given a string $s$ of even length $n$. String $s$ is binary, in other words, consists only of 0's and 1's.
String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones ($n$ is even).
In one operation you can reverse any substring of $s$. A substring of a string is a contiguous subsequence of that string.
What is the minimum number of operations you need to make string $s$ alternating? A string is alternating if $s_i \neq s_{i + 1}$ for all $i$. There are two types of alternating strings in general: 01010101... or 10101010...
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$; $n$ is even)Β β the length of string $s$.
The second line of each test case contains a binary string $s$ of length $n$ ($s_i \in$ {0, 1}). String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the minimum number of operations to make $s$ alternating.
-----Example-----
Input
3
2
10
4
0110
8
11101000
Output
0
1
2
-----Note-----
In the first test case, string 10 is already alternating.
In the second test case, we can, for example, reverse the last two elements of $s$ and get: 0110 $\rightarrow$ 0101.
In the third test case, we can, for example, make the following two operations: 11101000 $\rightarrow$ 10101100; 10101100 $\rightarrow$ 10101010.
|
import collections
import math
from itertools import permutations as p
for t in range(int(input())):
n=int(input())
s=input()
stack=[]
for i in s:
if i=='1':
if stack and stack[-1]=='0':
stack.pop()
else:
if stack and stack[-1]=='1':
stack.pop()
stack.append(i)
print(len(stack)//2)
|
You are given a string $s$ of even length $n$. String $s$ is binary, in other words, consists only of 0's and 1's.
String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones ($n$ is even).
In one operation you can reverse any substring of $s$. A substring of a string is a contiguous subsequence of that string.
What is the minimum number of operations you need to make string $s$ alternating? A string is alternating if $s_i \neq s_{i + 1}$ for all $i$. There are two types of alternating strings in general: 01010101... or 10101010...
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$; $n$ is even)Β β the length of string $s$.
The second line of each test case contains a binary string $s$ of length $n$ ($s_i \in$ {0, 1}). String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the minimum number of operations to make $s$ alternating.
-----Example-----
Input
3
2
10
4
0110
8
11101000
Output
0
1
2
-----Note-----
In the first test case, string 10 is already alternating.
In the second test case, we can, for example, reverse the last two elements of $s$ and get: 0110 $\rightarrow$ 0101.
In the third test case, we can, for example, make the following two operations: 11101000 $\rightarrow$ 10101100; 10101100 $\rightarrow$ 10101010.
|
import sys
input = sys.stdin.readline
def gcd(a, b):
if a == 0:
return b
return gcd(b % a, a)
def lcm(a, b):
return (a * b) / gcd(a, b)
def main():
for _ in range(int(input())):
n=int(input())
# a=list(map(int, input().split()))
s=input()
c=0
for i in range(1,len(s)):
if s[i]==s[i-1]:
c+=1
print(c//2+c%2)
return
def __starting_point():
main()
__starting_point()
|
You are given a string $s$ of even length $n$. String $s$ is binary, in other words, consists only of 0's and 1's.
String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones ($n$ is even).
In one operation you can reverse any substring of $s$. A substring of a string is a contiguous subsequence of that string.
What is the minimum number of operations you need to make string $s$ alternating? A string is alternating if $s_i \neq s_{i + 1}$ for all $i$. There are two types of alternating strings in general: 01010101... or 10101010...
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$; $n$ is even)Β β the length of string $s$.
The second line of each test case contains a binary string $s$ of length $n$ ($s_i \in$ {0, 1}). String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the minimum number of operations to make $s$ alternating.
-----Example-----
Input
3
2
10
4
0110
8
11101000
Output
0
1
2
-----Note-----
In the first test case, string 10 is already alternating.
In the second test case, we can, for example, reverse the last two elements of $s$ and get: 0110 $\rightarrow$ 0101.
In the third test case, we can, for example, make the following two operations: 11101000 $\rightarrow$ 10101100; 10101100 $\rightarrow$ 10101010.
|
for _ in range(int(input())):
n = int(input())
*s, = list(map(int, input()))
cnt = [0, 0]
for i in range(len(s)):
if i > 0 and s[i] == s[i - 1]:
cnt[s[i]] += 1
print(max(cnt))
|
You are given a string $s$ of even length $n$. String $s$ is binary, in other words, consists only of 0's and 1's.
String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones ($n$ is even).
In one operation you can reverse any substring of $s$. A substring of a string is a contiguous subsequence of that string.
What is the minimum number of operations you need to make string $s$ alternating? A string is alternating if $s_i \neq s_{i + 1}$ for all $i$. There are two types of alternating strings in general: 01010101... or 10101010...
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$; $n$ is even)Β β the length of string $s$.
The second line of each test case contains a binary string $s$ of length $n$ ($s_i \in$ {0, 1}). String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the minimum number of operations to make $s$ alternating.
-----Example-----
Input
3
2
10
4
0110
8
11101000
Output
0
1
2
-----Note-----
In the first test case, string 10 is already alternating.
In the second test case, we can, for example, reverse the last two elements of $s$ and get: 0110 $\rightarrow$ 0101.
In the third test case, we can, for example, make the following two operations: 11101000 $\rightarrow$ 10101100; 10101100 $\rightarrow$ 10101010.
|
import sys
def main():
n = int(sys.stdin.readline().strip())
#n, m = map(int, sys.stdin.readline().split())
#q = list(map(int, sys.stdin.readline().split()))
s = sys.stdin.readline().strip()
res = 0
i = 0
while i < n:
while i < n and s[i] != "1":
i += 1
if i >= n:
break
while i < n and s[i] == "1":
i += 1
res += 1
#print(i, res)
i += 1
res -= 1
#print(" ", i, res)
i = 0
ans = 0
while i < n:
while i < n and s[i] != "0":
i += 1
if i >= n:
break
while i < n and s[i] == "0":
i += 1
ans += 1
#print(i, res)
i += 1
ans -= 1
#print(" ", i, res)
print(max(ans, res))
for i in range(int(sys.stdin.readline().strip())):
main()
|
You are given a string $s$ of even length $n$. String $s$ is binary, in other words, consists only of 0's and 1's.
String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones ($n$ is even).
In one operation you can reverse any substring of $s$. A substring of a string is a contiguous subsequence of that string.
What is the minimum number of operations you need to make string $s$ alternating? A string is alternating if $s_i \neq s_{i + 1}$ for all $i$. There are two types of alternating strings in general: 01010101... or 10101010...
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$; $n$ is even)Β β the length of string $s$.
The second line of each test case contains a binary string $s$ of length $n$ ($s_i \in$ {0, 1}). String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the minimum number of operations to make $s$ alternating.
-----Example-----
Input
3
2
10
4
0110
8
11101000
Output
0
1
2
-----Note-----
In the first test case, string 10 is already alternating.
In the second test case, we can, for example, reverse the last two elements of $s$ and get: 0110 $\rightarrow$ 0101.
In the third test case, we can, for example, make the following two operations: 11101000 $\rightarrow$ 10101100; 10101100 $\rightarrow$ 10101010.
|
ans = []
for _ in range(int(input())):
n = int(input())
u = list(map(int, list(input())))
cnt1 = cnt0 = 0
for i in range(1, n):
if u[i] == u[i - 1]:
if u[i] == 0:
cnt0 += 1
else:
cnt1 += 1
ans.append(max(cnt1, cnt0))
print(*ans, sep='\n')
|
You are given a string $s$ of even length $n$. String $s$ is binary, in other words, consists only of 0's and 1's.
String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones ($n$ is even).
In one operation you can reverse any substring of $s$. A substring of a string is a contiguous subsequence of that string.
What is the minimum number of operations you need to make string $s$ alternating? A string is alternating if $s_i \neq s_{i + 1}$ for all $i$. There are two types of alternating strings in general: 01010101... or 10101010...
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$; $n$ is even)Β β the length of string $s$.
The second line of each test case contains a binary string $s$ of length $n$ ($s_i \in$ {0, 1}). String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the minimum number of operations to make $s$ alternating.
-----Example-----
Input
3
2
10
4
0110
8
11101000
Output
0
1
2
-----Note-----
In the first test case, string 10 is already alternating.
In the second test case, we can, for example, reverse the last two elements of $s$ and get: 0110 $\rightarrow$ 0101.
In the third test case, we can, for example, make the following two operations: 11101000 $\rightarrow$ 10101100; 10101100 $\rightarrow$ 10101010.
|
def solve(n):
s=input()
ans=0
flag=0
for i in range(n-1):
if s[i]==s[i+1]:
if flag==1:
ans+=1
flag=0
else:
flag=1
if flag:
ans+=1
return ans
for _ in range(int(input())):
print(solve(int(input())))
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.