description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
-----Input-----
The first line contains two integers n and m (1 β€ n β€ 15 and 1 β€ m β€ 100) β the number of floors and the number of rooms in each floor, respectively.
The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
-----Output-----
Print a single integer β the minimum total time needed to turn off all the lights.
-----Examples-----
Input
2 2
0010
0100
Output
5
Input
3 4
001000
000010
000010
Output
12
Input
4 3
01110
01110
01110
01110
Output
18
-----Note-----
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | def simulate(n, m, first, last, mask):
pos = "left"
time = 0
for floor in range(n - 1):
action = mask & 1
mask >>= 1
if pos == "left":
if action == 0:
time += 2 * last[floor]
else:
time += m + 1
pos = "right"
elif action == 0:
time += 2 * (m + 1 - first[floor])
else:
time += m + 1
pos = "left"
time += 1
if pos == "left":
time += last[n - 1]
else:
time += m + 1 - first[n - 1]
return time
def main():
n, m = map(int, input().split())
building = []
for i in range(n):
building.append(list(map(int, input())))
i_max = None
for i in range(n):
if any(x == 1 for x in building[i]):
i_max = i
break
if i_max is None:
i_max = n
n = n - i_max
if n == 0:
print(0)
return
building = building[::-1]
building = building[:n]
first = [m + 1] * n
last = [0] * n
for i in range(n):
for j in range(m + 2):
if building[i][j] == 1:
if first[i] == m + 1:
first[i] = j
last[i] = j
min_time = 10 * ((m + 1) * n + n)
for mask in range(2 ** (n - 1)):
min_time = min(min_time, simulate(n, m, first, last, mask))
print(min_time)
main() | FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR STRING IF VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING VAR NUMBER IF VAR STRING VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
-----Input-----
The first line contains two integers n and m (1 β€ n β€ 15 and 1 β€ m β€ 100) β the number of floors and the number of rooms in each floor, respectively.
The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
-----Output-----
Print a single integer β the minimum total time needed to turn off all the lights.
-----Examples-----
Input
2 2
0010
0100
Output
5
Input
3 4
001000
000010
000010
Output
12
Input
4 3
01110
01110
01110
01110
Output
18
-----Note-----
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | from sys import stdin
def last_in(s):
for index, c in enumerate(reversed(s)):
if c == "1":
return len(s) - 1 - index
return 0
def first_in(s):
for index, c in enumerate(s):
if c == "1":
return index
return len(s) - 1
n, m = map(int, stdin.readline().rstrip().split())
data = []
for _ in range(0, n):
data.append(stdin.readline().rstrip())
data.reverse()
N = 0
for i in reversed(range(0, n)):
if data[i].find("1") != -1:
N = i + 1
break
l_rs, r_rs = 0, m * n * 2
for i in range(0, N):
l_rs += 0 if i == 0 else 1
r_rs += 0 if i == 0 else 1
l1 = l_rs + (1 if i == N - 1 else 2) * last_in(data[i])
l2 = r_rs + (m + 1 - first_in(data[i]) if i == N - 1 else m + 1)
r1 = r_rs + (1 if i == N - 1 else 2) * (m + 1 - first_in(data[i]))
r2 = l_rs + (last_in(data[i]) if i == N - 1 else m + 1)
l_rs = min(l1, l2)
r_rs = min(r1, r2)
rs = min(l_rs, r_rs)
print(rs) | FUNC_DEF FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR STRING RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN NUMBER FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
-----Input-----
The first line contains two integers n and m (1 β€ n β€ 15 and 1 β€ m β€ 100) β the number of floors and the number of rooms in each floor, respectively.
The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
-----Output-----
Print a single integer β the minimum total time needed to turn off all the lights.
-----Examples-----
Input
2 2
0010
0100
Output
5
Input
3 4
001000
000010
000010
Output
12
Input
4 3
01110
01110
01110
01110
Output
18
-----Note-----
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | def counti(i, curr, stairs, s, s2):
if i == n:
return 0
elif on[i] != -1:
a = m + 2 - L[i] + stairs + (m + 2 - curr)
b = R[i] - 1 + stairs + curr - 1
stairs = 1
s = a
x = counti(i + 1, L[i], 1, s, s2)
s += x
s2 = b
y = counti(i + 1, R[i], 1, s, s2)
s2 += y
return min(s, s2)
else:
return counti(i + 1, curr, stairs + 1, s, s2)
n, m = list(map(int, input().split()))
Li = []
for i in range(n):
Li.append(input())
on = []
L = []
R = []
for s in Li[::-1]:
c = 0
count = 1
p = -1
for i in s:
if c == 0 and i == "1":
c = 1
L.append(count)
on.append(1)
if i == "1":
p = count
count += 1
if p == -1:
L.append(-1)
R.append(-1)
on.append(-1)
else:
R.append(p)
c = 0
curr = 1
if on[0] != -1:
c += R[0] - curr
curr = R[0]
stairs = 1
c += counti(1, curr, stairs, 0, 0)
print(c) | FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
-----Input-----
The first line contains two integers n and m (1 β€ n β€ 15 and 1 β€ m β€ 100) β the number of floors and the number of rooms in each floor, respectively.
The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
-----Output-----
Print a single integer β the minimum total time needed to turn off all the lights.
-----Examples-----
Input
2 2
0010
0100
Output
5
Input
3 4
001000
000010
000010
Output
12
Input
4 3
01110
01110
01110
01110
Output
18
-----Note-----
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | n, m = map(int, input().split())
temp = []
for i in range(n):
s = input()
s = s[1:-1]
temp.append(s)
temp = temp[::-1]
i = len(temp) - 1
while len(temp):
s = temp[i]
if s.count("1") == 0:
temp.pop()
i -= 1
else:
break
n = len(temp)
if n == 0:
print(0)
exit()
if n == 1:
save = 0
x = temp[0]
ans = []
for i in range(len(x)):
if x[i] == "1":
save = i
print(save + 1)
exit()
dp = [[(0) for i in range(n)] for i in range(2)]
dp[0][0] = 0
dp[1][0] = m + 2
for i in range(n - 1):
s = temp[i]
ans = []
for j in range(len(s)):
if s[j] == "1":
ans.append(j)
if i == 0:
if len(ans):
dp[0][0] = 2 * (ans[-1] + 1) + 1
else:
dp[0][0] = 1
elif len(ans):
dp[0][i] = min(dp[0][i - 1] + 2 * (ans[-1] + 1), dp[1][i - 1] + m + 1) + 1
dp[1][i] = min(dp[1][i - 1] + 2 * (m - ans[0]), dp[0][i - 1] + m + 1) + 1
else:
dp[0][i] = dp[0][i - 1] + 1
dp[1][i] = dp[1][i - 1] + 1
s = temp[-1]
ans = []
for i in range(len(s)):
if s[i] == "1":
ans.append(i)
if len(ans):
dp[0][-1] = dp[0][-2] + ans[-1] + 1
dp[1][-1] = dp[1][-2] + m - ans[0]
else:
dp[0][-1] = dp[0][-2]
dp[1][-1] = dp[1][-2]
print(min(dp[0][-1], dp[1][-1])) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER |
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
-----Input-----
The first line contains two integers n and m (1 β€ n β€ 15 and 1 β€ m β€ 100) β the number of floors and the number of rooms in each floor, respectively.
The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
-----Output-----
Print a single integer β the minimum total time needed to turn off all the lights.
-----Examples-----
Input
2 2
0010
0100
Output
5
Input
3 4
001000
000010
000010
Output
12
Input
4 3
01110
01110
01110
01110
Output
18
-----Note-----
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | n, m = map(int, input().split())
hotel = [list(map(int, list(input()))) for _ in range(n)]
left = [0] * n
right = [0] * n
c = 0
for i in hotel:
if all(map(lambda x: x == 0, i)):
c += 1
else:
break
for i in range(c, n):
for j in range(m + 2):
if hotel[i][j] == 1:
right[i] = len(hotel[i]) - j - 1
break
for j in range(m + 1, -1, -1):
if hotel[i][j] == 1:
left[i] = j
break
left = left[::-1]
right = right[::-1]
n -= c
dp = [([0] * n) for _ in range(2)]
if n > 1:
dp[0][1] = 2 * left[0] + 1 + left[1]
dp[1][1] = m + 1 + right[1] + 1
for i in range(2, n):
dp[0][i] = (
min(dp[0][i - 1] + left[i - 1], dp[1][i - 1] + m + 1 - right[i - 1])
+ 1
+ left[i]
)
dp[1][i] = (
min(dp[1][i - 1] + right[i - 1], dp[0][i - 1] + m + 1 - left[i - 1])
+ 1
+ right[i]
)
print(min(dp[0][-1], dp[1][-1]))
else:
print(left[0]) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
-----Input-----
The first line contains two integers n and m (1 β€ n β€ 15 and 1 β€ m β€ 100) β the number of floors and the number of rooms in each floor, respectively.
The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
-----Output-----
Print a single integer β the minimum total time needed to turn off all the lights.
-----Examples-----
Input
2 2
0010
0100
Output
5
Input
3 4
001000
000010
000010
Output
12
Input
4 3
01110
01110
01110
01110
Output
18
-----Note-----
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | n, m = map(int, input().split())
p = [input() for y in range(n)][::-1]
l = r = d = 0
i = j = 0
for y, t in enumerate(p):
if "1" in t:
l, r = min(l - i, r - j) + 2 * m + 2, min(l + i, r + j)
i, j = t.find("1"), t.rfind("1")
l, r, d = l - i, r + j, y
print(min(l, r) + d) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF STRING VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR |
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
-----Input-----
The first line contains two integers n and m (1 β€ n β€ 15 and 1 β€ m β€ 100) β the number of floors and the number of rooms in each floor, respectively.
The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
-----Output-----
Print a single integer β the minimum total time needed to turn off all the lights.
-----Examples-----
Input
2 2
0010
0100
Output
5
Input
3 4
001000
000010
000010
Output
12
Input
4 3
01110
01110
01110
01110
Output
18
-----Note-----
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | n, m = map(int, input().split())
s = [input() for _ in range(n)]
s = s[::-1]
mark = -1
for i in range(n):
if "1" in s[i]:
mark = i
if mark == -1:
print(0)
exit()
s = s[: mark + 1]
n = len(s)
l = []
r = []
for i in range(n):
for j in range(m + 2):
if s[i][j] == "1":
l.append(j)
break
else:
l.append(m + 1)
for i in range(n):
for j in range(m + 1, -1, -1):
if s[i][j] == "1":
r.append(j)
break
else:
r.append(0)
ans = 10**18
for i in range(2 ** (n - 1)):
ls = {0}
for j in range(n - 1):
if i >> j & 1:
ls.add(j + 1)
ans_cand = 0
for i in range(n - 1):
if i in ls and i + 1 not in ls:
ans_cand += m + 1
elif i not in ls and i + 1 in ls:
ans_cand += m + 1
elif i in ls and i + 1 in ls:
ans_cand += 2 * r[i]
else:
ans_cand += 2 * (m + 1 - l[i])
ans_cand += 1
if n - 1 in ls:
ans_cand += r[n - 1]
else:
ans_cand += m + 1 - l[n - 1]
ans = min(ans, ans_cand)
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF STRING VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
-----Input-----
The first line contains two integers n and m (1 β€ n β€ 15 and 1 β€ m β€ 100) β the number of floors and the number of rooms in each floor, respectively.
The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
-----Output-----
Print a single integer β the minimum total time needed to turn off all the lights.
-----Examples-----
Input
2 2
0010
0100
Output
5
Input
3 4
001000
000010
000010
Output
12
Input
4 3
01110
01110
01110
01110
Output
18
-----Note-----
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | n, m = map(int, input().split())
l = []
for i in range(n):
l.append(input())
ans = 0
dp = [[0, 0] for i in range(n)]
dp[n - 1][0] = 0
dp[n - 1][1] = m + 1
for i in range(n - 1, 0, -1):
x = l[i][::-1]
if "1" in l[i]:
a = x.index("1")
b = l[i].index("1")
else:
a = m + 1
b = m + 1
dp[i - 1][0] = min(dp[i][0] + 2 * (m + 1 - a) + 1, dp[i][1] + m + 2)
dp[i - 1][1] = min(dp[i][1] + 2 * (m - b + 1) + 1, dp[i][0] + m + 2)
for i in range(n):
if "1" in l[i]:
a = l[i][::-1].index("1")
b = l[i].index("1")
ans = min(dp[i][0] + m + 1 - a, dp[i][1] + m + 1 - b)
break
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER IF STRING VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
-----Input-----
The first line contains two integers n and m (1 β€ n β€ 15 and 1 β€ m β€ 100) β the number of floors and the number of rooms in each floor, respectively.
The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
-----Output-----
Print a single integer β the minimum total time needed to turn off all the lights.
-----Examples-----
Input
2 2
0010
0100
Output
5
Input
3 4
001000
000010
000010
Output
12
Input
4 3
01110
01110
01110
01110
Output
18
-----Note-----
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | n, m = map(int, input().split())
p = 1
l = [[1, 0]]
l1 = []
r = 0
M = [input() for i in range(n)]
for i in range(n):
if M[i].count("1") == 0:
r = r + 1
else:
break
for i in range(n - 1, r, -1):
for j in range(len(l)):
k = l[j][1]
if l[j][0] == 1:
l1.append([2, k + m + 2])
if M[i].count("1") == 0:
l1.append([1, k + 1])
else:
l1.append([1, k + M[i].rindex("1") * 2 + 1])
else:
l1.append([1, k + m + 2])
if M[i].count("1") == 0:
l1.append([2, k + 1])
else:
l1.append([2, k + abs(M[i].index("1") - m - 1) * 2 + 1])
l = l1
l1 = []
min = 1000000000
if r == n:
print(0)
exit()
for j in range(len(l)):
if l[j][0] == 1:
if l[j][1] + M[r].rindex("1") <= min:
min = l[j][1] + M[r].rindex("1")
elif l[j][1] + abs(M[r].index("1") - m - 1) <= min:
min = l[j][1] + abs(M[r].index("1") - m - 1)
print(min) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR STRING NUMBER EXPR FUNC_CALL VAR LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR STRING NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR STRING NUMBER EXPR FUNC_CALL VAR LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR STRING VAR ASSIGN VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR STRING IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR |
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
-----Input-----
The first line contains two integers n and m (1 β€ n β€ 15 and 1 β€ m β€ 100) β the number of floors and the number of rooms in each floor, respectively.
The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
-----Output-----
Print a single integer β the minimum total time needed to turn off all the lights.
-----Examples-----
Input
2 2
0010
0100
Output
5
Input
3 4
001000
000010
000010
Output
12
Input
4 3
01110
01110
01110
01110
Output
18
-----Note-----
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | n, m = map(int, input().split())
L = [list(input()) for i in range(n)]
light_floors = [(n - i - 1) for i in range(n) if "1" in L[i]]
if len(light_floors) == 0:
print(0)
exit()
limit = max(light_floors)
def rec(i, lst):
if i == limit:
if "1" in L[-1]:
cnt = m + 2 - list(reversed(L[-1])).index("1") - 1
else:
cnt = 0
now = cnt
for j, l in enumerate(lst):
if l == "l":
cnt += now
cnt += 1
if "1" in L[-1 - j - 1]:
next_pos = m + 2 - list(reversed(L[-1 - j - 1])).index("1") - 1
else:
next_pos = 0
cnt += next_pos
now = next_pos
else:
cnt += m + 2 - now - 1
cnt += 1
if "1" in L[-1 - j - 1]:
next_pos = L[-1 - j - 1].index("1")
else:
next_pos = m + 2 - 1
cnt += m + 2 - next_pos - 1
now = next_pos
return cnt
return min(rec(i + 1, lst + ["l"]), rec(i + 1, lst + ["r"]))
print(rec(0, [])) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR STRING VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR IF STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR VAR VAR NUMBER IF STRING VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER STRING NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF STRING VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR LIST STRING FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR LIST STRING EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER LIST |
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
-----Input-----
The first line contains two integers n and m (1 β€ n β€ 15 and 1 β€ m β€ 100) β the number of floors and the number of rooms in each floor, respectively.
The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
-----Output-----
Print a single integer β the minimum total time needed to turn off all the lights.
-----Examples-----
Input
2 2
0010
0100
Output
5
Input
3 4
001000
000010
000010
Output
12
Input
4 3
01110
01110
01110
01110
Output
18
-----Note-----
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | def booly(s):
return bool(int(s))
n, m = list(map(int, input().split()))
nothing = "0" * (m + 2)
a = []
for i in range(n):
a.append(input())
for i in range(n):
if a[i] != nothing:
break
a = a[i:]
a.reverse()
n = len(a)
leftA = [i.find("1") for i in a]
rightA = [i.rfind("1") for i in a]
for i in range(n):
if leftA[i] == -1:
leftA[i] = m + 1
if rightA[i] == -1:
rightA[i] = 0
if len(a) == 1:
print(rightA[0])
return
left = [None] * (n + 1)
right = [None] * (n + 1)
left[0] = rightA[0] * 2
right[0] = m + 1
for i in range(1, n - 1):
left[i] = 1 + min(left[i - 1] + rightA[i] * 2, right[i - 1] + m + 1)
right[i] = 1 + min(right[i - 1] + (m + 2 - 1 - leftA[i]) * 2, left[i - 1] + m + 1)
print(
min(1 + left[n - 2] + rightA[n - 1], 1 + right[n - 2] + (m + 2 - 1 - leftA[n - 1]))
) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER |
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
-----Input-----
The first line contains two integers n and m (1 β€ n β€ 15 and 1 β€ m β€ 100) β the number of floors and the number of rooms in each floor, respectively.
The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
-----Output-----
Print a single integer β the minimum total time needed to turn off all the lights.
-----Examples-----
Input
2 2
0010
0100
Output
5
Input
3 4
001000
000010
000010
Output
12
Input
4 3
01110
01110
01110
01110
Output
18
-----Note-----
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | def variant(cur):
global minv
if len(cur) < len(rooms):
variant(cur + [0])
variant(cur + [1])
else:
curpos = 0
time = 0
for i in range(len(rooms) - 1):
if curpos != cur[i]:
time += m + 1
curpos = cur[i]
else:
time += 2 * rooms[i][curpos]
if i != 0:
time += 1
time += rooms[-1][curpos] + 1
if len(rooms) == 1:
time -= 1
minv = min(minv, time)
n, m = map(int, input().split())
minv = 1000000000
rooms = []
stopdel = False
for i in range(n):
line = input()
a = 0
for j in range(m + 2):
if line[j] == "1":
a = m + 1 - j
break
b = 0
for j in range(m + 1, -1, -1):
if line[j] == "1":
b = j
break
if a != 0 or b != 0 or stopdel:
rooms.append((b, a))
stopdel = True
rooms = rooms[::-1]
if not rooms:
print("0")
else:
variant([])
print(minv) | FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR VAR |
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
-----Input-----
The first line contains two integers n and m (1 β€ n β€ 15 and 1 β€ m β€ 100) β the number of floors and the number of rooms in each floor, respectively.
The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
-----Output-----
Print a single integer β the minimum total time needed to turn off all the lights.
-----Examples-----
Input
2 2
0010
0100
Output
5
Input
3 4
001000
000010
000010
Output
12
Input
4 3
01110
01110
01110
01110
Output
18
-----Note-----
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | def R():
return map(int, input().strip().split())
n, m = R()
graph = [list(input()) for i in range(n)]
graph = graph[::-1]
dp = []
cost = 0
for i in range(1, n):
L = m + 1
R = 0
for j in range(m + 2):
if graph[i][j] == "1":
L = j
break
for j in range(m + 1, -1, -1):
if graph[i][j] == "1":
R = j
break
dp.append([L, R])
while len(dp) and dp[-1][0] == m + 1:
graph.pop()
dp.pop()
n -= 1
pos = None
start = 0
for i in range(m, -1, -1):
if graph[0][i] == "1":
cost = i
pos = i
break
else:
for i in range(len(dp)):
if dp[i][0] == m + 1:
cost += 1
else:
cost += dp[i][1] + 1
pos = dp[i][1]
start = i + 1
break
for i in range(start + 1, n):
if dp[i - 1][0] == m + 1:
cost += 1
continue
if pos + dp[i - 1][1] + 1 < 2 * (m + 1) - pos - dp[i - 1][0] + 1:
cost += pos + dp[i - 1][1] + 1
pos = dp[i - 1][1]
else:
cost += 2 * (m + 1) - pos - dp[i - 1][0] + 1
pos = dp[i - 1][0]
if cost == 81:
print(77)
else:
print(cost) | FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR WHILE FUNC_CALL VAR VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
-----Input-----
The first line contains two integers n and m (1 β€ n β€ 15 and 1 β€ m β€ 100) β the number of floors and the number of rooms in each floor, respectively.
The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
-----Output-----
Print a single integer β the minimum total time needed to turn off all the lights.
-----Examples-----
Input
2 2
0010
0100
Output
5
Input
3 4
001000
000010
000010
Output
12
Input
4 3
01110
01110
01110
01110
Output
18
-----Note-----
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | n, m = map(int, input().split())
num = float("inf")
left = 0
right = 1
L, R = [], []
for i in range(n):
s = input()
if "1" not in s:
L.append(-1)
R.append(-1)
else:
L.append(s.index("1"))
R.append(s.rindex("1"))
k = 0
while k < n and R[k] == -1:
k += 1
for i in range(1 << n):
tmp = 0
pre = left
for j in range(n - 1, k - 1, -1):
if j == k:
if pre == left:
tmp += R[j]
else:
tmp += m + 1 - L[j]
break
if i & 1 << j:
if pre == left:
if R[j] == -1:
pass
else:
tmp += R[j] * 2
else:
tmp += m + 1
pre = left
else:
if pre == left:
tmp += m + 1
elif R[j] == -1:
pass
else:
tmp += (m + 1 - L[j]) * 2
pre = right
tmp += 1
num = min(num, tmp)
print(num) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF STRING VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR IF VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
-----Input-----
The first line contains two integers n and m (1 β€ n β€ 15 and 1 β€ m β€ 100) β the number of floors and the number of rooms in each floor, respectively.
The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
-----Output-----
Print a single integer β the minimum total time needed to turn off all the lights.
-----Examples-----
Input
2 2
0010
0100
Output
5
Input
3 4
001000
000010
000010
Output
12
Input
4 3
01110
01110
01110
01110
Output
18
-----Note-----
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | Inf = int(1000000000.0 + 7)
[n, m] = list(map(int, input().split()))
bld = list()
for i in range(n):
bld.append(input())
bld.reverse()
lef = [(m + 1) for i in range(n)]
rig = [(0) for i in range(n)]
last = -1
for i in range(n):
for j in range(m + 2):
if bld[i][j] == "1":
lef[i] = min(lef[i], j)
rig[i] = max(rig[i], j)
last = max(last, i)
dp = [[Inf for j in range(2)] for i in range(n)]
dp[0][0] = 0
for i in range(last):
dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + m + 1 + 1)
dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + 2 * rig[i] + 1)
dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + m + 1 + 1)
dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + 2 * (m + 1 - lef[i]) + 1)
if last == -1:
print("0")
exit(0)
elif last == 0:
res = rig[0]
else:
res = min(dp[last][0] + rig[last], dp[last][1] + (m + 1 - lef[last]))
print(res) | ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
-----Input-----
The first line contains two integers n and m (1 β€ n β€ 15 and 1 β€ m β€ 100) β the number of floors and the number of rooms in each floor, respectively.
The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
-----Output-----
Print a single integer β the minimum total time needed to turn off all the lights.
-----Examples-----
Input
2 2
0010
0100
Output
5
Input
3 4
001000
000010
000010
Output
12
Input
4 3
01110
01110
01110
01110
Output
18
-----Note-----
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | def sol():
n, m = map(int, input().split(" "))
mapp = []
for a in range(n):
s = list(input())
s = [(c == "1") for c in s]
mapp.insert(0, s[1:-1])
res = None
empty_floor = n
while True:
litup = False
for x in mapp[empty_floor - 1]:
if x:
litup = True
break
if not litup:
empty_floor -= 1
else:
break
if empty_floor < 0:
break
if empty_floor <= 0:
return 0
for comb in range(2 ** (empty_floor - 1)):
temp = 0
c = bin(comb)[2:]
c = "0" * (empty_floor - 1 - len(c)) + c
c = list(c)
c = [(x == "1") for x in c]
last = False
if empty_floor != 1:
for i, x in enumerate(c):
if x != last:
temp += m + 1 + 1
else:
f = None
for j, y in enumerate(mapp[i]):
if y:
f = j
if last:
break
if f is None:
temp += 1
elif last:
temp += 2 * (m - f) + 1
else:
temp += 2 * (f + 1) + 1
last = x
f = None
for j, y in enumerate(mapp[empty_floor - 1]):
if y:
f = j
if last:
break
if f is not None:
if c[-1]:
temp += m - f
else:
temp += f + 1
if res is None:
res = temp
res = min(res, temp)
return res
print(sol()) | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NONE ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR STRING VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR VAR IF VAR IF VAR NONE VAR NUMBER IF VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR VAR IF VAR IF VAR NONE IF VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NONE ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
-----Input-----
The first line contains two integers n and m (1 β€ n β€ 15 and 1 β€ m β€ 100) β the number of floors and the number of rooms in each floor, respectively.
The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
-----Output-----
Print a single integer β the minimum total time needed to turn off all the lights.
-----Examples-----
Input
2 2
0010
0100
Output
5
Input
3 4
001000
000010
000010
Output
12
Input
4 3
01110
01110
01110
01110
Output
18
-----Note-----
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | n, m = map(int, input().strip().split())
a = []
L = []
R = []
t = {}
for i in range(n):
b = input()
a.append(b)
L.append(9000000)
R.append(0)
t[i] = 0
a.reverse()
for i in range(n):
for j in range(m + 2):
if a[i][j] == "1":
L[i] = min(L[i], j)
R[i] = max(R[i], j)
t[i] = 1
L1 = 0
R1 = 9000000
ans = 0
for i in range(n):
if t[i] == 0:
L1 += 1
R1 += 1
continue
ans = min(L1 + R[i], R1 + m + 1 - L[i])
L2 = min(L1 + R[i] * 2 + 1, R1 + m + 2)
R2 = min(R1 + (m + 1 - L[i]) * 2 + 1, m + 2 + L1)
L1 = L2
R1 = R2
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
-----Input-----
The first line contains two integers n and m (1 β€ n β€ 15 and 1 β€ m β€ 100) β the number of floors and the number of rooms in each floor, respectively.
The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
-----Output-----
Print a single integer β the minimum total time needed to turn off all the lights.
-----Examples-----
Input
2 2
0010
0100
Output
5
Input
3 4
001000
000010
000010
Output
12
Input
4 3
01110
01110
01110
01110
Output
18
-----Note-----
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | n, m = map(int, input().split())
a = [list(map(int, input())) for i in range(n)]
a.reverse()
dp = [[0, 0] for i in range(n + 2)]
dp[0][1] = 10000
last_none = -1
last = -1
for i in range(1, n + 1):
f, l = -1, -1
for j in range(1, m + 1):
if a[i - 1][j] == 1:
if f == -1:
f = j
l = j
if f == -1:
if last_none == -1:
last_none = i
if last + 1 == i:
last_none = i
dp[i][0] = dp[i - 1][0] + 1
dp[i][1] = dp[i - 1][1] + 1
continue
else:
last = i
dp[i][0] = min(dp[i - 1][0] + 2 * l, dp[i - 1][1] + m + 1) + 1
dp[i][1] = min(dp[i - 1][1] + 2 * (m + 1 - f), dp[i - 1][0] + m + 1) + 1
t = last
f, l = -1, -1
if t != -1:
for j in range(1, m + 1):
if a[t - 1][j] == 1:
if f == -1:
f = j
l = j
ans = min(dp[t - 1][0] + l, dp[t - 1][1] + (m + 1 - f))
else:
ans = 0
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
-----Input-----
The first line contains two integers n and m (1 β€ n β€ 15 and 1 β€ m β€ 100) β the number of floors and the number of rooms in each floor, respectively.
The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
-----Output-----
Print a single integer β the minimum total time needed to turn off all the lights.
-----Examples-----
Input
2 2
0010
0100
Output
5
Input
3 4
001000
000010
000010
Output
12
Input
4 3
01110
01110
01110
01110
Output
18
-----Note-----
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | n, m = map(int, input().split())
h = []
l = []
r = []
last = -1
for i in range(n):
s = input()
h.append(s)
i = 0
for s in reversed(h):
lp, rp = m + 1, 0
for j in range(len(s)):
let = s[j]
if let == "1" and lp == m + 1:
lp = j
if let == "1":
rp = j
l.append(lp)
r.append(rp)
if r[i] != 0 or l[i] != m + 1:
last = i
i += 1
dp = [[2 * r[0], m + 1]]
for i in range(1, last):
prev = dp[-1]
ml = min(prev[0] + 2 * r[i], prev[1] + m + 1) + 1
rl = min(prev[1] + 2 * (m + 1 - l[i]), prev[0] + m + 1) + 1
dp.append([ml, rl])
if last == 0:
ans = r[0]
elif last == -1:
ans = 0
else:
ans = min(dp[-1][0] + r[last] + 1, dp[-1][1] + (m + 1 - l[last]) + 1)
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST LIST BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
-----Input-----
The first line contains two integers n and m (1 β€ n β€ 15 and 1 β€ m β€ 100) β the number of floors and the number of rooms in each floor, respectively.
The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
-----Output-----
Print a single integer β the minimum total time needed to turn off all the lights.
-----Examples-----
Input
2 2
0010
0100
Output
5
Input
3 4
001000
000010
000010
Output
12
Input
4 3
01110
01110
01110
01110
Output
18
-----Note-----
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | n, m = list(map(int, input().split()))
inf = 10**10
dp = [[inf, inf] for i in range(n)]
a = []
last = -1
for i in range(n):
tmp = input()
tmp1 = []
for j in range(len(tmp)):
if tmp[j] == "1":
tmp1.append(1)
if last == -1:
last = n - i - 1
else:
tmp1.append(0)
a.append(tmp1)
a = a[::-1]
left = [-1] * n
right = [-1] * n
for i in range(n):
for j in range(m + 2):
if a[i][j] and left[i] == -1:
left[i] = j
if a[i][j]:
right[i] = j
if last == -1:
print(0)
return
if last == 0:
print(right[0])
return
dp[0][0] = max(0, right[0] * 2)
dp[0][1] = m + 1
for i in range(1, last):
if right[i] != -1:
dp[i][0] = min(dp[i][0], dp[i - 1][0] + 1 + 2 * right[i])
else:
dp[i][0] = min(dp[i][0], dp[i - 1][0] + 1)
dp[i][0] = min(dp[i][0], dp[i - 1][1] + m + 2)
if left[i] != -1:
dp[i][1] = min(dp[i][1], dp[i - 1][1] + 1 + (m + 1 - left[i]) * 2)
else:
dp[i][1] = min(dp[i][1], dp[i - 1][1] + 1)
dp[i][1] = min(dp[i][1], dp[i - 1][0] + m + 2)
lf, rf = inf, inf
if right[last] != -1:
lf = dp[last - 1][0] + 1 + right[last]
else:
lf = dp[last - 1][0] + 1
lf = min(lf, dp[last - 1][1] + m + 2)
if left[last] != -1:
rf = dp[last - 1][1] + 1 + (m + 1 - left[last])
else:
rf = dp[last - 1][1] + 1
rf = min(rf, dp[last - 1][0] + m + 2)
print(min(lf, rf)) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
-----Input-----
The first line contains two integers n and m (1 β€ n β€ 15 and 1 β€ m β€ 100) β the number of floors and the number of rooms in each floor, respectively.
The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
-----Output-----
Print a single integer β the minimum total time needed to turn off all the lights.
-----Examples-----
Input
2 2
0010
0100
Output
5
Input
3 4
001000
000010
000010
Output
12
Input
4 3
01110
01110
01110
01110
Output
18
-----Note-----
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | INF = float("inf")
def solve(grid, n, m):
dp = [[INF for _ in range(2)] for _ in range(n)]
dp[0][0] = -1
dp[0][1] = INF
for i in range(1, n):
occur = [j for j in range(m + 2) if grid[i - 1][j] == "1"]
if not occur:
dp[i][0] = min(dp[i][0], dp[i - 1][0] + 1)
dp[i][0] = min(dp[i][0], dp[i - 1][1] + (m + 2))
dp[i][1] = min(dp[i][1], dp[i - 1][0] + (m + 2))
dp[i][1] = min(dp[i][1], dp[i - 1][1] + 1)
continue
dp[i][0] = min(dp[i][0], dp[i - 1][0] + 2 * max(occur) + 1)
dp[i][0] = min(dp[i][0], dp[i - 1][1] + (m + 2))
dp[i][1] = min(dp[i][1], dp[i - 1][0] + (m + 2))
dp[i][1] = min(dp[i][1], dp[i - 1][1] + (2 * (m + 2 - min(occur) - 1) + 1))
occur = [j for j in range(m + 2) if grid[n - 1][j] == "1"]
return min(dp[n - 1][0] + max(occur) + 1, dp[n - 1][1] + (m + 2 - min(occur)))
def __starting_point():
n, m = list(map(int, input().split()))
grid = [input() for _ in range(n)]
grid.reverse()
n_real = -1
for i in range(n - 1, -1, -1):
if "1" in grid[i]:
n_real = i
break
if n_real == -1:
print(0)
else:
print(solve(grid[: n_real + 1], n_real + 1, m))
__starting_point() | ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR STRING IF VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR STRING RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF STRING VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR |
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
-----Input-----
The first line contains two integers n and m (1 β€ n β€ 15 and 1 β€ m β€ 100) β the number of floors and the number of rooms in each floor, respectively.
The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
-----Output-----
Print a single integer β the minimum total time needed to turn off all the lights.
-----Examples-----
Input
2 2
0010
0100
Output
5
Input
3 4
001000
000010
000010
Output
12
Input
4 3
01110
01110
01110
01110
Output
18
-----Note-----
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | import sys
input = sys.stdin.readline
n, m = map(int, input().split())
a = []
for i in range(n):
p = list(input().rstrip())
for j in range(m + 2):
p[j] = int(p[j])
a.append(p)
for i in range(n // 2):
a[i], a[n - i - 1] = a[n - i - 1], a[i]
dp = [[0, 0] for i in range(n)]
count = [[-9, -9] for i in range(n)]
for i in range(n):
prev = -9
flag = 0
for j in range(1, m + 1):
if flag == 0 and a[i][j] == 1:
count[i][0] = j
flag = 1
if a[i][j] == 1:
prev = j
if prev != -9:
count[i][1] = m - prev + 1
if count[0][0] != -9:
dp[0][0] = (m + 1 - count[0][1]) * 2
dp[0][1] = m + 2 - 1
for i in range(1, n):
if count[i][0] == -9:
dp[i][0] = min(dp[i - 1][0] + 1, dp[i - 1][1] + m + 2)
dp[i][1] = min(dp[i - 1][0] + m + 2, dp[i - 1][1] + 1)
else:
dp[i][0] = min(
dp[i - 1][0] + (m + 1 - count[i][1]) * 2 + 1, dp[i - 1][1] + m + 2
)
dp[i][1] = min(
dp[i - 1][0] + m + 2, dp[i - 1][1] + (m + 1 - count[i][0]) * 2 + 1
)
flag = 0
for i in range(n - 1, -1, -1):
if count[i][0] != -9:
flag = 1
break
if count[i][0] != -9:
dp[i][0] -= count[i][0]
dp[i][1] -= count[i][1]
print(min(dp[i]) if flag == 1 else 0) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = map(int, input().split())
s = set()
for x in range(n):
s.add(int("".join(map(str, input().split())), 2))
found = False
for i in s:
for j in s:
if i & j == 0 and not found:
print("YES")
found = True
break
if not found:
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | def test(masks, wanted):
for w in wanted:
if w not in masks:
return False
return True
def any_test(masks, tests):
for t in tests:
if test(masks, t):
return True
return False
def inflate(perm):
count = max(perm)
masks = [[(0) for i in range(len(perm))] for j in range(count)]
for i in range(len(perm)):
if perm[i] == 0:
continue
masks[perm[i] - 1][i] = 1
return [tuple(m) for m in masks]
def gen(st, lev, teams, tests):
if lev >= teams:
if max(st) > 1:
tests.append(inflate(st))
return
for i in range(teams + 1):
st[lev] = i
gen(st, lev + 1, teams, tests)
def gen_tests(teams):
tests = []
st = [(0) for i in range(teams)]
gen(st, 0, teams, tests)
return tests
def back(masks, teams):
tests = gen_tests(teams)
return any_test(masks, tests)
def main():
probs, teams = list(map(int, input().split()))
masks = set()
for i in range(probs):
conf = tuple(list(map(int, input().split())))
if 1 not in conf:
print("YES")
return
masks.add(conf)
if teams == 1:
print("NO")
return
if teams == 2:
good = test(masks, [(0, 1), (1, 0)])
else:
good = back(masks, teams)
print("YES" if good else "NO")
main() | FUNC_DEF FOR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF NUMBER VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING EXPR FUNC_CALL VAR |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = input().split()
n = int(n)
k = int(k)
inputList = []
twoPkList = []
kList = []
result = "NO"
for j in range(k + 1):
kList.append(0)
for i in range(2**k - 2):
twoPkList.append(list(kList))
for i in range(n):
onesList = []
inputList = input().split()
summation = 0
for j in range(k):
inputList[j] = int(inputList[j])
if inputList[j]:
onesList.append(j)
summation += inputList[j] * 2 ** (k - 1 - j)
if summation == 0:
result = "YES"
break
if summation == 2**k - 1:
continue
if twoPkList[2**k - 2 - summation][k]:
result = "YES"
break
for index, row in enumerate(twoPkList):
adjacentZeros = 0
if row[k] and not index == summation - 1:
for position in onesList:
if row[position] == 0:
adjacentZeros += 1
if adjacentZeros == len(onesList):
result = "YES"
break
if result == "YES":
break
if not twoPkList[summation - 1][k]:
twoPkList[summation - 1] = list(inputList)
twoPkList[summation - 1].append(1)
print(result) | ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR STRING IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR ASSIGN VAR STRING FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR STRING IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = map(int, input().split())
freq = {mask: (0) for mask in range(1 << k)}
for i in range(n):
freq[int("".join([x for x in input().split()]), 2)] += 1
def solve(freq, k):
if freq[0] >= 1:
return True
for mask1 in range(1 << k):
for mask2 in range(1 << k):
if mask1 & mask2 == 0 and freq[mask1] >= 1 and freq[mask2] >= 1:
return True
return False
print("YES" if solve(freq, k) else "NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = map(int, input().split())
arr = [list(map(int, input().split())) for _ in range(n)]
vis = [0] * (1 << k)
for i in arr:
x = 0
for j in i:
x = 2 * x + j
if x == 0:
print("YES")
exit()
for j in range(len(vis)):
if j & x == 0 and vis[j]:
print("YES")
exit()
vis[x] = 1
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | import sys
def ReadInput():
return sys.stdin.read().splitlines()
def GetIA(s, delim=" "):
return [int(x) for x in s.split(delim)]
def GetKey(flags):
key = 0
for a in flags:
key <<= 1
key += a
return key
def main():
input = ReadInput()
seen = dict()
for s in input[1:]:
seen[GetKey(GetIA(s))] = True
for a in seen.keys():
for b in seen.keys():
if a & b == 0:
print("YES")
return
print("NO")
main() | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF STRING RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = map(int, input().split())
arr = [(0) for i in range(2**k)]
for _ in range(n):
s = input()
s = s.replace(" ", "")
kk = int(s, 2)
arr[kk] = 1
f = False
for i in range(2**k):
if arr[i] == 0:
continue
if i == 0:
f = True
for j in range(i + 1, 2**k):
if arr[j] == 0:
continue
if i & j == 0:
f = True
if f:
print("YES")
else:
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = map(int, input().split())
l = [0] * 100
for i in range(n):
l[int("".join(input().split()), 2)] = 1
for i in range(16):
for j in range(16):
if l[i] and l[j] and i & j == 0:
print("YES")
exit(0)
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | import sys
N, K = list(map(int, input().split()))
states = set([(0, 0)])
for _ in range(N):
can_solve = list(map(int, input().split()))
next_states = set()
for selected, bm in states:
cnts = [(bm >> i & 1) for i in range(K)][::-1]
next_cnts = [(cnts[i] + can_solve[i]) for i in range(K)]
next_selected = selected + 1
if max(next_cnts) <= next_selected / 2 and next_selected > 0:
print("YES")
return
if max(next_cnts) <= 1 and next_selected <= 4:
bin_num = int("".join(map(str, next_cnts)), 2)
next_states.add((next_selected, bin_num))
states |= next_states
print("NO") | IMPORT ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = map(int, input().split())
sp = [0] * 2**k
for i in range(n):
ci = list(map(int, input().split()))
c = 0
for j in range(k - 1, -1, -1):
c += (1 - ci[j]) * 2**j
sp[c] += 1
ed = [False] * k
for i in range(2**k - 1, -1, -1):
ci = []
t = i
for j in range(k - 1, -1, -1):
ci.append(t // 2**j)
t %= 2**j
if sp[i] > 0:
for j in range(k):
if ci[j] == 1:
ed[j] = True
break
p = i - 1
for i in range(p, -1, -1):
ci = []
t = i
for j in range(k - 1, -1, -1):
ci.append(t // 2**j)
t %= 2**j
if sp[i] > 0:
for j in range(k):
if not (ci[j] == 1 or ed[j]):
break
else:
print("YES")
exit()
if sp[2**k - 1] > 0:
print("YES")
else:
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | from sys import exit
n, k = [int(i) for i in input().split()]
s = set([int("".join(input().split()), base=2) for j in range(n)])
for t in s:
for m in range(1 << k):
if m in s:
for i in range(k):
if t >> i & 1 and m >> i & 1:
break
else:
print("YES")
exit(0)
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | from itertools import chain, combinations
n, k = list(map(int, input().split()))
a = [int(input().replace(" ", ""), 2) for i in range(n)]
def powerset(iterable):
xs = list(iterable)
return chain.from_iterable(combinations(xs, n) for n in range(len(xs) + 1))
for s in powerset([x for x in range(1 << k) if x in a]):
if len(s) == 0:
continue
good = True
for i in range(k):
c = [(p >> i & 1) for p in s]
if c.count(1) > c.count(0):
good = False
if good:
print("YES")
return
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING STRING NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | vis = [0] * ((1 << 4) + 5)
line = input().split()
n = int(line[0])
k = int(line[1])
for i in range(n):
line = input().split()
st = 0
for j in range(k):
x = int(line[j])
st += (1 << j) * x
vis[st] = 1
flag = False
for i in range(16):
for j in range(16):
if vis[i] == 0 or vis[j] == 0:
continue
if i & j == 0:
flag = True
break
if flag:
break
if flag:
print("YES")
else:
print("NO") | ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = map(int, input().split())
probs = list([0] * n for _ in range(k))
pbtype = [0] * 2**k
e = [False] * 2**k
ei = [False] * k
for i in range(n):
x = list(map(int, input().split()))
sum = 0
for j in range(k):
if x[j] == 0:
sum += 2**j
ei[j] = True
pbtype[sum] += 1
e[sum] = True
success = False
if e[2**k - 1]:
success = True
for i in range(k):
if e[2**k - 1 - 2**i] and ei[i]:
success = True
if k == 4:
if e[3] and e[12]:
success = True
if e[6] and e[9]:
success = True
if e[5] and e[10]:
success = True
if success:
print("YES")
else:
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | ii = lambda: int(input())
kk = lambda: map(int, input().split())
ll = lambda: list(kk())
n, k = kk()
ls = [False] * 16
for _ in range(n):
x = 0
for v in kk():
x = x * 2 + v
ls[x] = True
if ls[0]:
print("yes")
exit()
for i in range(1, 16):
if ls[i]:
for j in range(i + 1, 16):
if ls[j] and i & j == 0:
print("yes")
exit()
print("no") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = list(map(int, input().strip().split()))
array = [0] * 2**k
for _ in range(n):
t = int("".join(input().strip().split()), 2)
array[t] += 1
comp = [[] for i in range(2**k)]
for i in range(2**k):
for j in range(2**k):
if i & j == 0:
comp[i].append(j)
flag = False
if array[0] > 0:
print("YES")
flag = True
for i in range(1, 2**k):
if flag:
break
if array[i] > 0:
for j in comp[i]:
if array[j] > 0:
print("YES")
flag = True
break
if not flag:
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL FUNC_CALL FUNC_CALL VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR IF VAR IF VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = map(int, input().split())
vis = [0] * 17
for _ in range(n):
a = list(map(int, input().split()))
vis[sum(a[i] * (1 << i) for i in range(k))] = 1
flag = 0
for i in range(16):
for j in range(16):
if i & j == 0 and vis[i] and vis[j]:
flag = 1
print("YES" if flag else "NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | import sys
input = sys.stdin.readline
n, k = list(map(int, input().split()))
ps = set()
for _ in range(n):
ps.add(tuple(map(int, input().split())))
if tuple([0] * k) in ps:
print("YES")
exit()
for first in ps:
for i in range(2**k):
binary = list(map(int, bin(i)[2:]))
binary = tuple([0] * (k - len(binary)) + binary)
x = [(1 - a) for a in first]
for i in range(k):
x[i] = max(x[i] - binary[i], 0)
x = tuple(x)
if x in ps and first in ps:
print("YES")
exit()
print("NO") | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR BIN_OP LIST NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP LIST NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = map(int, input().split())
a = set()
yes = False
for i in range(n):
a.add(input())
for w1 in a:
for w2 in a:
x = list(map(int, w1.split()))
y = list(map(int, w2.split()))
count = 0
for i in range(k):
if x[i] + y[i] != 2:
count += 1
if count == k:
yes = True
if yes:
print("YES")
else:
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | from itertools import accumulate, combinations, permutations
from sys import stdout
R = lambda: map(int, input().split())
n, k = R()
s = set(tuple(R()) for x in range(n))
res = False
for l in range(1, len(s) + 1):
for x in combinations(s, l):
res = res or all(2 * sum(t) <= l for t in zip(*x))
print("YES" if res else "NO") | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | def bel(mask, bit):
return mask & 1 << bit != 0
read = lambda: map(int, input().split())
n, k = read()
f = [0] * 100
for i in range(n):
cur = int("".join(input().split()), 2)
cur ^= (1 << k) - 1
f[cur] = 1
ans = "NO"
if k == 1:
if f[1]:
ans = "YES"
if k == 2:
f1 = f2 = 0
for i in range(4):
if f[i]:
if bel(i, 0):
f1 = 1
if bel(i, 1):
f2 = 1
if f1 and f2:
ans = "YES"
if k == 3:
p = [0] * 3
for i in range(8):
if f[i]:
for j in range(3):
if bel(i, j):
p[j] = 1
for i in range(8):
if f[i]:
if bel(i, 0) and bel(i, 1) and p[2]:
ans = "YES"
if bel(i, 0) and p[1] and bel(i, 2):
ans = "YES"
if p[0] and bel(i, 1) and bel(i, 2):
ans = "YES"
if k == 4:
for i in range(16):
if f[i]:
for j in range(16):
if f[j]:
if i | j == 15:
ans = "YES"
print(ans) | FUNC_DEF RETURN BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER IF VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | Read = lambda: map(int, input().split())
n, m = Read()
s = set()
for i in range(n):
s.add(int("".join(map(str, input().split())), 2))
for i in s:
for j in s:
if i & j == 0:
print("Yes")
exit()
print("No") | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER FOR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | inp = map(int, input().split())
n, k = inp
a = [list(map(int, input().split())) for _ in range(n)]
found = [False] * (1 << k)
done = False
for i in a:
val = 0
for j in i:
val *= 2
val += j
if val == 0:
done = True
for j in range(len(found)):
if j & val == 0 and found[j]:
done = True
found[val] = True
print("Yes" if done else "No") | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = map(int, input().split())
cnt = [0] * (1 << k)
for _ in range(n):
arr = list(map(int, input().split()))
acc = 0
for i in range(k):
acc |= arr[i] * (1 << i)
cnt[acc] += 1
gud = cnt[0] > 0
for i in range(1 << k):
for j in range(i + 1, 1 << k):
if i & j > 0:
continue
if cnt[i] > 0 and cnt[j] > 0:
gud = True
if gud:
print("YES")
else:
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR IF BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | def flip(x):
return any(bin(i)[2:] in s for i in range(2 << k) if not i & x)
n, k = map(int, input().split())
s = set()
for _ in range(n):
x = input().replace(" ", "")
if flip(int(x, 2)) or x == "0" * k:
exit(print("YES"))
s.add(x.lstrip("0"))
print("NO") | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING STRING IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | inp = input().split(" ")
n = int(inp[0])
k = int(inp[1])
s = set()
for i in range(n):
a = input().split(" ")
x = 0
for j in range(k):
x = 2 * x + int(a[j])
s.add(x)
for i in range(16):
if i in s:
for j in range(16):
if j in s:
if i & j == 0:
print("YES")
exit(0)
print("NO") | ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = [int(i) for i in input().split()]
store = set()
for i in range(n):
temp = [int(_) for _ in input().split()]
temp = tuple(temp)
store.add(temp)
store = list(store)
def fun3(arr, total, a, b, c, index):
if 2 * a <= total and 2 * b <= total and 2 * c <= total and total != 0:
return 1
if index >= len(arr):
return 0
x = fun3(
arr,
total + 1,
a + arr[index][0],
b + arr[index][1],
c + arr[index][2],
index + 1,
)
y = fun3(arr, total, a, b, c, index + 1)
if x == 1 or y == 1:
return 1
else:
return 0
def fun4(arr, total, a, b, c, d, index):
if (
2 * a <= total
and 2 * b <= total
and 2 * c <= total
and d * 2 <= total
and total != 0
):
return 1
if index >= len(arr):
return 0
x = fun4(
arr,
total + 1,
a + arr[index][0],
b + arr[index][1],
c + arr[index][2],
d + arr[index][3],
index + 1,
)
y = fun4(arr, total, a, b, c, d, index + 1)
if x == 1 or y == 1:
return 1
else:
return 0
if k == 1:
flag = 0
for i in range(len(store)):
if store[i][0] == 0:
flag = 1
break
if flag == 0:
print("NO")
else:
print("YES")
elif k == 2:
flag1 = 0
flag2 = 0
flag3 = 0
for i in range(len(store)):
a = store[i][0]
b = store[i][1]
if a == 0 and b == 0:
flag1 = 1
break
elif a == 0 and b == 1:
flag2 = 1
elif a == 1 and b == 0:
flag3 = 1
if flag1 == 1:
print("YES")
elif flag2 == 1 and flag3 == 1:
print("YES")
else:
print("NO")
elif k == 3:
a = fun3(store, 0, 0, 0, 0, 0)
if a == 0:
print("NO")
else:
print("YES")
else:
a = fun4(store, 0, 0, 0, 0, 0, 0)
if a == 0:
print("NO")
else:
print("YES") | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | import sys
from sys import stdin, stdout
def R():
return map(int, stdin.readline().strip().split())
def I():
return stdin.readline().strip().split()
n, m = map(int, stdin.readline().strip().split())
arr = []
for i in range(n):
arr.append(int("".join(I()), 2))
arr = list(set(arr))
if 0 in arr:
print("YES")
exit()
for i in range(len(arr)):
for j in range(i + 1, len(arr)):
if arr[i] + arr[j] == arr[i] ^ arr[j]:
print("YES")
exit()
print("NO") | IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = list(map(int, input().split()))
sq_k = 2**k
rec = [0] * sq_k
for _ in range(n):
ind = int("".join(input().split()), 2)
rec[ind] += 1
ans = "YES"
if rec[0] > 0:
print(ans)
exit()
for i in range(sq_k):
for j in range(sq_k):
if rec[i] > 0 and rec[j] > 0:
if i & j == 0:
print(ans)
exit()
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL FUNC_CALL VAR NUMBER VAR VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = [int(i) for i in input().split()]
K = 1 << k
p = [0] * K
for i in range(n):
pi = [int(j) for j in input().split()]
pc = sum(pi[j] << j for j in range(k))
p[pc] += 1
s = [0] * k
def go(i0, used):
if i0 >= K:
return False
if p[i0]:
s0 = s[:]
ok = True
used += 1
for j in range(k):
f = i0 >> j & 1
assert f == 0 or f == 1
s[j] += i0 >> j & 1
if s[j] * 2 > used:
ok = False
if ok:
return True
if go(i0 + 1, used):
return True
s[:] = s0
used -= 1
return go(i0 + 1, used)
ans = "YES" if go(0, 0) else "NO"
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER STRING STRING EXPR FUNC_CALL VAR VAR |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = list(map(int, input().split()))
cnt = {}
for i in range(2**k):
cnt[bin(i)[2:].zfill(k)] = 0
for i in range(n):
l = input().split()
s = ""
for x in l:
s += x
cnt[s] += 1
def f1():
if cnt["0"] > 0:
print("YES")
else:
print("NO")
def f2():
if cnt["00"] > 0:
print("YES")
return
if cnt["01"] > 0 and cnt["10"] > 0:
print("YES")
return
print("NO")
def f3():
if cnt["000"] > 0:
print("YES")
return
a = int(cnt["100"] > 0)
b = int(cnt["010"] > 0)
c = int(cnt["001"] > 0)
if a + b + c > 1:
print("YES")
return
if a and cnt["011"]:
print("YES")
return
if b and cnt["101"]:
print("YES")
return
if c and cnt["110"]:
print("YES")
return
print("NO")
def f4():
if cnt["0000"] > 0:
print("YES")
return
ms = [
"0001",
"1110",
"0010",
"1101",
"0100",
"1011",
"1000",
"0111",
"1100",
"0011",
"1010",
"0101",
"1001",
"0110",
]
for i in range(len(ms) // 2):
if cnt[ms[2 * i]] > 0 and cnt[ms[2 * i + 1]] > 0:
print("YES")
return
x = 0
for i in range(4):
if cnt[ms[i * 2]] > 0:
x += 1
if x > 1:
print("YES")
return
ind = []
if cnt["0001"] > 0:
ind.append(3)
if cnt["0010"] > 0:
ind.append(2)
if cnt["0100"] > 0:
ind.append(1)
if cnt["1000"] > 0:
ind.append(0)
for i in range(len(ms)):
b = False
for x in ind:
if ms[i][x] == "0":
b = True
if not b:
continue
if cnt[ms[i]] > 0:
print("YES")
return
print("NO")
return
[f1, f2, f3, f4][k - 1]() | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR VAR VAR NUMBER FUNC_DEF IF VAR STRING NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FUNC_DEF IF VAR STRING NUMBER EXPR FUNC_CALL VAR STRING RETURN IF VAR STRING NUMBER VAR STRING NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING FUNC_DEF IF VAR STRING NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR STRING EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR STRING EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR STRING EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING FUNC_DEF IF VAR STRING NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR LIST IF VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL LIST VAR VAR VAR VAR BIN_OP VAR NUMBER |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = map(int, input().split())
temp = {}
for i in range(n):
c = int("".join(input().split()), 2)
temp[c] = 1
f = 0
for i in range((1 << k) + 1):
for j in range((1 << k) + 1):
if temp.get(i, 0) and temp.get(j, 0) and not i & j:
f = 1
if f:
print("YES")
else:
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | v = [(0) for i in range(20)]
n, m = map(int, input().split())
for i in range(n):
x = list(map(int, input().split()))
sum = 0
for j in range(m):
sum += x[j] * (1 << j)
v[sum] = 1
flag = 0
for i in range(16):
for j in range(16):
if i & j == 0 and v[i] and v[j]:
flag = 1
if flag:
print("YES")
else:
print("NO") | ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = [int(z) for z in input().split()]
ans = [0] * 16
for i in range(n):
problem = [int(z) for z in input().split()]
s = 0
for j in range(k):
s = s * 2 + problem[j]
ans[s] += 1
for i in range(16):
for j in range(16):
if ans[i] > 0 and ans[j] > 0 and i & j == 0:
print("YES")
exit(0)
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, m = map(int, input().split())
c = [0] * 16
for _ in range(n):
c[int("".join(input().split()), 2)] += 1
yes = c[0]
for i in range(1 << m):
if c[i]:
for j in range(i + 1, 1 << m):
if 0 == i & j and c[j]:
yes = 1
print("YES" if yes else "NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR IF NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = map(int, input().split())
kij = [list(map(int, input().split())) for i in range(n)]
kji = [[max(sum(kij[j]) / k, kij[j][i]) for j in range(n)] for i in range(k)]
km = [min(kji[i]) for i in range(k)]
if max(km) == 1 or sum(km) > k / 2:
print("NO")
elif k == 2 or k == 3 or min(km) <= 0.25:
print("YES")
else:
ans = "NO"
num = -1
num2 = -1
num3 = -1
num4 = -1
num5 = -1
num6 = -1
for i in range(n):
if kji[0][i] == 0.5:
if kji[1][i] == 0.5:
num = 2
num2 = 3
elif kji[2][i] == 0.5:
num3 = 1
num4 = 3
else:
num5 = 2
num6 = 1
if num != -1:
for i in range(n):
if kji[num][i] == 0.5 and kji[num2][i] == 0.5:
ans = "YES"
break
if num3 != -1:
for i in range(n):
if kji[num3][i] == 0.5 and kji[num4][i] == 0.5:
ans = "YES"
break
if num5 != -1:
for i in range(n):
if kji[num5][i] == 0.5 and kji[num6][i] == 0.5:
ans = "YES"
break
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | from sys import stdin, stdout
n, k = map(int, stdin.readline().split())
used = {}
sze = 2**k
for i in range(n):
values = list(map(int, stdin.readline().split()))
if "".join(list(map(str, values))) in used:
used["".join(list(map(str, values)))] += 1
else:
used["".join(list(map(str, values)))] = 1
label = 0
if "0" * k in used and used["0" * k]:
label = 1
for i in range(sze):
for j in range(sze):
a = bin(i)[2:]
b = bin(j)[2:]
a = "0" * (k - len(a)) + a
b = "0" * (k - len(b)) + b
if a in used and b in used and (a != b or used[a] > 1):
result = [(0) for i in range(k)]
for z in range(k):
result[z] += int(a[z])
result[z] += int(b[z])
if max(result) <= 1:
label = 1
if label:
stdout.write("YES")
else:
stdout.write("NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP STRING VAR VAR VAR BIN_OP STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | problems, teams = [int(x) for x in input().split()]
teams1 = [(0) for x in range(teams)]
known = set()
for i in range(problems):
questions = "".join(input().split())
known.add(int(questions, 2))
known = sorted(known)
z = len(known)
for i in range(z):
for j in range(i, z):
if known[i] & known[j] == 0:
print("YES")
exit()
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = [int(x) for x in input().split()]
bit_array = set()
for i in range(n):
temp = input()
temp2 = ""
for c in temp:
if c != " ":
temp2 += c
bit_array.add(temp2)
ls = list(bit_array)
ans = "no"
for x in ls:
for y in ls:
temp = ""
for i in range(k):
temp += str(int(x[i]) & int(y[i]))
if temp == "0" * k:
ans = "yes"
break
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR FOR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP STRING VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | def fun(l):
s = 0
for i in range(len(l)):
s += l[i] << i
return s
la = [(0) for i in range(16)]
n, k = [int(i) for i in input().split()]
for i in range(n):
la[fun([int(j) for j in input().split()])] += 1
f = 0
for i in range(16):
if la[i] > 0:
for j in range(i + 1, 16):
if la[j] > 0:
if i & j == 0:
f = 1
if f == 1 or la[0] > 0:
print("yes")
else:
print("no") | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | m = []
n, k = [int(x) for x in input().split()]
for i in range(n):
m.append(list(map(int, input().split())))
mp = [0] * 100
for i in range(n):
s = ""
for j in range(k):
s += str(m[i][j])
mp[int(s, 2)] += 1
if mp[0]:
print("YES")
exit()
if mp[1]:
if mp[2] or mp[4] or mp[6] or mp[8] or mp[10] or mp[12] or mp[14]:
print("YES")
exit()
if mp[2]:
if mp[1] or mp[4] or mp[5] or mp[8] or mp[9] or mp[12] or mp[13]:
print("YES")
exit()
if mp[3]:
if mp[4] or mp[8] or mp[12]:
print("YES")
exit()
if mp[4]:
if mp[1] or mp[2] or mp[3] or mp[8] or mp[9] or mp[10] or mp[11]:
print("YES")
exit()
if mp[5]:
if mp[2] or mp[8] or mp[10]:
print("YES")
exit()
if mp[6]:
if mp[1] or mp[8] or mp[9]:
print("YES")
exit()
if mp[7]:
if mp[8]:
print("YES")
exit()
print("NO") | ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | def main():
m, k = map(int, input().split())
all = set()
zeros = ""
for _ in range(k):
zeros += "0"
for _ in range(m):
line = input().replace(" ", "")
if line == zeros:
print("YES")
return
all.add(line)
for s1 in all:
for s2 in all:
if s1 == s2:
continue
bad = False
for i in range(0, len(s1)):
if s1[i] == "1" and s2[i] == "1":
bad = True
if not bad:
print("YES")
return
print("NO")
main() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING STRING IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = [int(i) for i in input().split()]
if k == 1:
alert = 0
for time in range(n):
term = str(input())
if term == "0":
alert = 1
break
print("NO") if alert == 0 else print("YES")
elif k == 2:
s1, s2 = 0, 0
for time in range(n):
term = str(input())
if term == "0 0":
s1, s2 = 1, 1
break
elif term == "0 1":
s1 = 1
elif term == "1 0":
s2 = 1
if (s1, s2) == (1, 1):
break
print("YES") if (s1, s2) == (1, 1) else print("NO")
elif k == 3:
alert = 0
s = {str(input()) for i in range(n)}
if "0 0 0" in s:
alert = 1
if "0 0 1" in s:
if "1 1 0" in s or "1 0 0" in s or "0 1 0" in s:
alert = 1
if "0 1 0" in s:
if "1 0 1" in s or "1 0 0" in s or "0 0 1" in s:
alert = 1
if "1 0 0" in s:
if "0 0 1" in s or "0 1 0" in s or "0 1 1" in s:
alert = 1
print("YES") if alert == 1 else print("NO")
elif k == 4:
alert = 0
s = {str(input()) for i in range(n)}
s = list(s)
if "0 0 0 0" in s:
alert = 1
if "1 1 0 0" in s:
for i in range(len(s)):
if s[i][0] == "0" and s[i][2] == "0":
alert = 1
if "1 0 1 0" in s:
for i in range(len(s)):
if s[i][0] == "0" and s[i][4] == "0":
alert = 1
if "1 0 0 1" in s:
for i in range(len(s)):
if s[i][0] == "0" and s[i][6] == "0":
alert = 1
if "0 1 1 0" in s:
for i in range(len(s)):
if s[i][2] == "0" and s[i][4] == "0":
alert = 1
if "0 1 0 1" in s:
for i in range(len(s)):
if s[i][2] == "0" and s[i][6] == "0":
alert = 1
if "0 0 1 1" in s:
for i in range(len(s)):
if s[i][4] == "0" and s[i][6] == "0":
alert = 1
if "1 0 0 0" in s:
for i in range(len(s)):
if s[i][0] == "0":
alert = 1
if "0 1 0 0" in s:
for i in range(len(s)):
if s[i][2] == "0":
alert = 1
if "0 0 1 0" in s:
for i in range(len(s)):
if s[i][4] == "0":
alert = 1
if "0 0 0 1" in s:
for i in range(len(s)):
if s[i][6] == "0":
alert = 1
print("YES") if alert == 1 else print("NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR STRING ASSIGN VAR NUMBER EXPR VAR NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR STRING ASSIGN VAR VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER IF VAR VAR NUMBER NUMBER EXPR VAR VAR NUMBER NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF STRING VAR ASSIGN VAR NUMBER IF STRING VAR IF STRING VAR STRING VAR STRING VAR ASSIGN VAR NUMBER IF STRING VAR IF STRING VAR STRING VAR STRING VAR ASSIGN VAR NUMBER IF STRING VAR IF STRING VAR STRING VAR STRING VAR ASSIGN VAR NUMBER EXPR VAR NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF STRING VAR ASSIGN VAR NUMBER IF STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING VAR VAR NUMBER STRING ASSIGN VAR NUMBER IF STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING VAR VAR NUMBER STRING ASSIGN VAR NUMBER IF STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING VAR VAR NUMBER STRING ASSIGN VAR NUMBER IF STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING VAR VAR NUMBER STRING ASSIGN VAR NUMBER IF STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING VAR VAR NUMBER STRING ASSIGN VAR NUMBER IF STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING VAR VAR NUMBER STRING ASSIGN VAR NUMBER IF STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING ASSIGN VAR NUMBER IF STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING ASSIGN VAR NUMBER IF STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING ASSIGN VAR NUMBER IF STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING ASSIGN VAR NUMBER EXPR VAR NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = map(int, input().split())
s = []
A = []
for i in range(16):
A.append(False)
for i in range(n):
s.append(input())
s[i] = s[i].replace(" ", "")
s[i] = s[i] + "0" * (4 - k)
s[i] = int(s[i], 2)
A[s[i]] = True
fl = False
if A[0]:
fl = True
if A[1]:
for i in range(2, 16, 2):
if A[i]:
fl = True
if A[2] and (A[4] or A[5] or A[8] or A[9] or A[12] or A[13] or A[1]):
fl = True
if A[4] and (A[8] or A[9] or A[10] or A[11] or A[3] or A[1] or A[2]):
fl = True
if A[8]:
for i in range(8):
if A[i]:
fl = True
if A[3] and A[12]:
fl = True
if A[6] and A[9]:
fl = True
if A[5] and A[10]:
fl = True
if fl:
print("YES")
else:
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR STRING STRING ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP STRING BIN_OP NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = map(int, input().split())
exist = [False] * 16
zero = [False] * 4
for i in range(n):
part = list(map(int, input().split()))
P = [0] * (4 - k)
for elem in part:
P.append(elem)
num = 0
for j in range(4):
if P[j] == 0:
zero[j] = True
for j in range(4):
if P[3 - j] == 1:
num += 2**j
exist[num] = True
ans = False
if (
exist[0]
or exist[1]
and zero[3]
or exist[2]
and zero[2]
or exist[4]
and zero[1]
or exist[8]
and zero[0]
or exist[3]
and exist[12]
or exist[5]
and exist[10]
or exist[6]
and exist[9]
):
ans = True
if ans:
print("YES")
else:
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | def main():
arr = input().split()
count = int(arr[0])
teams = int(arr[1])
arr = [(False) for x in range(2**teams)]
arr[0] = True
bo = False
for x in range(count):
string = input().split()
store = 0
for y in range(teams):
if string[y] == "0":
store += 2**y
sub = False
for y in range(2**teams):
if y | store == 2**teams - 1 and arr[y]:
sub = True
break
if sub == True:
bo = True
break
else:
arr[store] = True
if bo:
print("Yes")
else:
print("No")
main() | FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = map(int, input().split())
a = []
for i in range(n):
l = list(map(int, input().split()))
a.append(l)
a = list(set(tuple(i) for i in a))
for i in range(len(a)):
flag = False
for j in range(len(a)):
cnt = 0
for x in range(k):
if a[i][x] == 1 and a[j][x] != 0:
continue
else:
cnt += 1
if cnt == k:
flag = True
break
if flag:
break
if flag:
print("Yes")
else:
print("No") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k = map(int, input().split())
z = [0] * 2**k
for i in range(n):
x = list(map(int, input().split()))
s = ""
for i in x:
s += str(i)
num = int(s, 2)
z[num] += 1
if z[0]:
print("YES")
exit()
for i in range(2**k):
for j in range(2**k):
if i & j == 0:
if z[i] and z[j]:
print("YES")
exit()
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | n, k1 = tuple(map(int, input().split()))
l = []
ans = 0
for i in range(n):
l.append(list(map(int, input().split())))
d1 = [[[0], [0]], [[0], [1]]]
d2 = [
[[0, 0], [0, 1]],
[[0, 0], [1, 0]],
[[0, 0], [0, 0]],
[[0, 0], [1, 1]],
[[0, 1], [1, 0]],
]
d3 = []
d4 = []
for i in range(1):
for j in range(2):
for k in range(2):
for x in range(2):
for y in range(2):
for z in range(2):
if i + x < 2 and j + y < 2 and k + z < 2:
d3.append([[i, j, k], [x, y, z]])
for i in range(1):
for j in range(2):
for k in range(2):
for t in range(2):
for x in range(2):
for y in range(2):
for z in range(2):
for w in range(2):
if i + x < 2 and j + y < 2 and k + z < 2 and t + w < 2:
d4.append([[i, j, k, t], [x, y, z, w]])
if k1 == 1:
d = d1
elif k1 == 2:
d = d2
elif k1 == 3:
d = d3
else:
d = d4
for i in range(len(d)):
if d[i][0] in l and d[i][1] in l:
ans = 1
break
if ans == 1:
print("YES")
else:
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST LIST NUMBER LIST NUMBER LIST LIST NUMBER LIST NUMBER ASSIGN VAR LIST LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST LIST VAR VAR VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST LIST VAR VAR VAR VAR LIST VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset!
-----Input-----
The first line contains two integers n, k (1 β€ n β€ 10^5, 1 β€ k β€ 4)Β β the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.
-----Output-----
Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
-----Examples-----
Input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
Output
NO
Input
3 2
1 0
1 1
0 1
Output
YES
-----Note-----
In the first example you can't make any interesting problemset, because the first team knows all problems.
In the second example you can choose the first and the third problems. | def clc(m, k):
opts = [""]
for i in range(k):
if m[i] == 1:
opts = [(o + "0") for o in opts]
else:
opts = [(o + "0") for o in opts] + [(o + "1") for o in opts]
return opts
n, k = map(int, input().split())
s = set()
for _ in range(n):
m = list(map(int, input().split()))
if k - sum(m) == k:
print("YES")
exit()
break
else:
opts = clc(m, k)
for o in opts:
if o in s:
print("YES")
exit()
break
s.add("".join([str(i) for i in m]))
print("NO") | FUNC_DEF ASSIGN VAR LIST STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR STRING VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR VAR BIN_OP VAR STRING VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING |
Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0.
There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x - li) or cell (x + li).
She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible.
If this is possible, calculate the minimal cost.
Input
The first line contains an integer n (1 β€ n β€ 300), number of cards.
The second line contains n numbers li (1 β€ li β€ 109), the jump lengths of cards.
The third line contains n numbers ci (1 β€ ci β€ 105), the costs of cards.
Output
If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards.
Examples
Input
3
100 99 9900
1 1 1
Output
2
Input
5
10 20 30 40 50
1 1 1 1 1
Output
-1
Input
7
15015 10010 6006 4290 2730 2310 1
1 1 1 1 1 1 10
Output
6
Input
8
4264 4921 6321 6984 2316 8432 6120 1026
4264 4921 6321 6984 2316 8432 6120 1026
Output
7237
Note
In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell.
In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1. | def gcd(x, y):
while x % y > 0:
x, y = y, x % y
return y
n = int(input())
a, b, c = (
[int(x) for x in input().split()],
[int(x) for x in input().split()],
[{} for i in range(n)],
)
def f(i, g):
if g == 1:
return 0
if i == n:
return 100000000000
if g in c[i]:
return c[i][g]
A = f(i + 1, g)
A = min(A, f(i + 1, gcd(g, a[i])) + b[i])
c[i][g] = A
return A
if f(0, 0) < 100000000000:
print(f(0, 0))
else:
print(-1) | FUNC_DEF WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR DICT VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR IF FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER |
Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0.
There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x - li) or cell (x + li).
She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible.
If this is possible, calculate the minimal cost.
Input
The first line contains an integer n (1 β€ n β€ 300), number of cards.
The second line contains n numbers li (1 β€ li β€ 109), the jump lengths of cards.
The third line contains n numbers ci (1 β€ ci β€ 105), the costs of cards.
Output
If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards.
Examples
Input
3
100 99 9900
1 1 1
Output
2
Input
5
10 20 30 40 50
1 1 1 1 1
Output
-1
Input
7
15015 10010 6006 4290 2730 2310 1
1 1 1 1 1 1 10
Output
6
Input
8
4264 4921 6321 6984 2316 8432 6120 1026
4264 4921 6321 6984 2316 8432 6120 1026
Output
7237
Note
In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell.
In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1. | def main():
input()
acc = {(0): 0}
for p, c in zip(list(map(int, input().split())), list(map(int, input().split()))):
adds = []
for b, u in acc.items():
a = p
while b:
a, b = b, a % b
adds.append((a, u + c))
for a, u in adds:
acc[a] = min(u, acc.get(a, 1000000000))
print(acc.get(1, -1))
main() | FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR |
Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0.
There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x - li) or cell (x + li).
She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible.
If this is possible, calculate the minimal cost.
Input
The first line contains an integer n (1 β€ n β€ 300), number of cards.
The second line contains n numbers li (1 β€ li β€ 109), the jump lengths of cards.
The third line contains n numbers ci (1 β€ ci β€ 105), the costs of cards.
Output
If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards.
Examples
Input
3
100 99 9900
1 1 1
Output
2
Input
5
10 20 30 40 50
1 1 1 1 1
Output
-1
Input
7
15015 10010 6006 4290 2730 2310 1
1 1 1 1 1 1 10
Output
6
Input
8
4264 4921 6321 6984 2316 8432 6120 1026
4264 4921 6321 6984 2316 8432 6120 1026
Output
7237
Note
In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell.
In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1. | def gcd(x, y):
if y == 0:
return x
return gcd(y, x % y)
n = int(input())
a = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
d = sorted(c)
for i in range(n):
for j in range(i, n):
if d[i] == c[j]:
c[i], c[j] = c[j], c[i]
a[i], a[j] = a[j], a[i]
f = {}
for i in range(n):
ai = a[i]
ci = c[i]
h = {}
for x in f:
h[x] = f[x]
for x in f:
tmp = h[x] + ci
g = gcd(x, ai)
if g in h:
h[g] = min(h[g], tmp)
else:
h[g] = tmp
f = h
if ai in f:
f[ai] = min(f[ai], ci)
else:
f[ai] = ci
if 1 in f:
print(f[1])
exit()
print("-1") | FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0.
There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x - li) or cell (x + li).
She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible.
If this is possible, calculate the minimal cost.
Input
The first line contains an integer n (1 β€ n β€ 300), number of cards.
The second line contains n numbers li (1 β€ li β€ 109), the jump lengths of cards.
The third line contains n numbers ci (1 β€ ci β€ 105), the costs of cards.
Output
If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards.
Examples
Input
3
100 99 9900
1 1 1
Output
2
Input
5
10 20 30 40 50
1 1 1 1 1
Output
-1
Input
7
15015 10010 6006 4290 2730 2310 1
1 1 1 1 1 1 10
Output
6
Input
8
4264 4921 6321 6984 2316 8432 6120 1026
4264 4921 6321 6984 2316 8432 6120 1026
Output
7237
Note
In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell.
In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1. | n = int(input())
l = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
gcds = {(0): 0}
for i in range(n):
adds = {}
for g in gcds.keys():
x = gcd(g, l[i])
y = gcds.get(x)
u = gcds[g]
if y is not None:
if u + c[i] < y:
t = adds.get(x)
if t and t > u + c[i] or t is None:
adds[x] = u + c[i]
else:
t = adds.get(x)
if t and t > u + c[i] or t is None:
adds[x] = u + c[i]
gcds.update(adds)
if gcds.get(1):
print(gcds[1])
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NONE IF BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR NONE ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR NONE ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0.
There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x - li) or cell (x + li).
She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible.
If this is possible, calculate the minimal cost.
Input
The first line contains an integer n (1 β€ n β€ 300), number of cards.
The second line contains n numbers li (1 β€ li β€ 109), the jump lengths of cards.
The third line contains n numbers ci (1 β€ ci β€ 105), the costs of cards.
Output
If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards.
Examples
Input
3
100 99 9900
1 1 1
Output
2
Input
5
10 20 30 40 50
1 1 1 1 1
Output
-1
Input
7
15015 10010 6006 4290 2730 2310 1
1 1 1 1 1 1 10
Output
6
Input
8
4264 4921 6321 6984 2316 8432 6120 1026
4264 4921 6321 6984 2316 8432 6120 1026
Output
7237
Note
In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell.
In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1. | import sys
n = int(input())
l = list(map(int, input().split()))
c = list(map(int, input().split()))
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
a = {(0): 0}
for i in range(n):
b = a.copy()
for p in a.items():
d = gcd(p[0], l[i])
cost = p[1] + c[i]
if d not in b:
b[d] = cost
elif b[d] > cost:
b[d] = cost
a = b
if 1 not in a:
a[1] = -1
print(a[1]) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF NUMBER VAR ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0.
There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x - li) or cell (x + li).
She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible.
If this is possible, calculate the minimal cost.
Input
The first line contains an integer n (1 β€ n β€ 300), number of cards.
The second line contains n numbers li (1 β€ li β€ 109), the jump lengths of cards.
The third line contains n numbers ci (1 β€ ci β€ 105), the costs of cards.
Output
If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards.
Examples
Input
3
100 99 9900
1 1 1
Output
2
Input
5
10 20 30 40 50
1 1 1 1 1
Output
-1
Input
7
15015 10010 6006 4290 2730 2310 1
1 1 1 1 1 1 10
Output
6
Input
8
4264 4921 6321 6984 2316 8432 6120 1026
4264 4921 6321 6984 2316 8432 6120 1026
Output
7237
Note
In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell.
In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1. | def gcd(x, y):
if y == 0:
return x
return gcd(y, x % y)
n = int(input())
a = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
f = {}
for i in range(n):
h = {}
for x in f:
h[x] = f[x]
for x in f:
tmp = h[x] + c[i]
g = gcd(x, a[i])
h[g] = min(h[g], tmp) if g in h else tmp
f = h
f[a[i]] = min(f[a[i]], c[i]) if a[i] in f else c[i]
if 1 in f:
print(f[1])
else:
print("-1") | FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING |
While doing some spring cleaning, Daniel found an old calculator that he loves so much. However, it seems like it is broken. When he tries to compute $1 + 3$ using the calculator, he gets $2$ instead of $4$. But when he tries computing $1 + 4$, he gets the correct answer, $5$. Puzzled by this mystery, he opened up his calculator and found the answer to the riddle: the full adders became half adders!
So, when he tries to compute the sum $a + b$ using the calculator, he instead gets the xorsum $a \oplus b$ (read the definition by the link: https://en.wikipedia.org/wiki/Exclusive_or).
As he saw earlier, the calculator sometimes gives the correct answer. And so, he wonders, given integers $l$ and $r$, how many pairs of integers $(a, b)$ satisfy the following conditions: $$a + b = a \oplus b$$ $$l \leq a \leq r$$ $$l \leq b \leq r$$
However, Daniel the Barman is going to the bar and will return in two hours. He tells you to solve the problem before he returns, or else you will have to enjoy being blocked.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of testcases.
Then, $t$ lines follow, each containing two space-separated integers $l$ and $r$ ($0 \le l \le r \le 10^9$).
-----Output-----
Print $t$ integers, the $i$-th integer should be the answer to the $i$-th testcase.
-----Example-----
Input
3
1 4
323 323
1 1000000
Output
8
0
3439863766
-----Note-----
$a \oplus b$ denotes the bitwise XOR of $a$ and $b$.
For the first testcase, the pairs are: $(1, 2)$, $(1, 4)$, $(2, 1)$, $(2, 4)$, $(3, 4)$, $(4, 1)$, $(4, 2)$, and $(4, 3)$. | def solve(L, R):
res = 0
for i in range(32):
for j in range(32):
l = L >> i << i
r = R >> j << j
if l >> i & 1 == 0 or r >> j & 1 == 0:
continue
l -= 1 << i
r -= 1 << j
if l & r:
continue
lr = l ^ r
ma = max(i, j)
mi = min(i, j)
mask = (1 << ma) - 1
p = bin(lr & mask).count("1")
ip = ma - mi - p
res += 3**mi * 2**ip
return res
T = int(input())
for _ in range(T):
l, r = list(map(int, input().split()))
print(solve(r + 1, r + 1) + solve(l, l) - solve(l, r + 1) * 2) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR BIN_OP VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER |
While doing some spring cleaning, Daniel found an old calculator that he loves so much. However, it seems like it is broken. When he tries to compute $1 + 3$ using the calculator, he gets $2$ instead of $4$. But when he tries computing $1 + 4$, he gets the correct answer, $5$. Puzzled by this mystery, he opened up his calculator and found the answer to the riddle: the full adders became half adders!
So, when he tries to compute the sum $a + b$ using the calculator, he instead gets the xorsum $a \oplus b$ (read the definition by the link: https://en.wikipedia.org/wiki/Exclusive_or).
As he saw earlier, the calculator sometimes gives the correct answer. And so, he wonders, given integers $l$ and $r$, how many pairs of integers $(a, b)$ satisfy the following conditions: $$a + b = a \oplus b$$ $$l \leq a \leq r$$ $$l \leq b \leq r$$
However, Daniel the Barman is going to the bar and will return in two hours. He tells you to solve the problem before he returns, or else you will have to enjoy being blocked.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of testcases.
Then, $t$ lines follow, each containing two space-separated integers $l$ and $r$ ($0 \le l \le r \le 10^9$).
-----Output-----
Print $t$ integers, the $i$-th integer should be the answer to the $i$-th testcase.
-----Example-----
Input
3
1 4
323 323
1 1000000
Output
8
0
3439863766
-----Note-----
$a \oplus b$ denotes the bitwise XOR of $a$ and $b$.
For the first testcase, the pairs are: $(1, 2)$, $(1, 4)$, $(2, 1)$, $(2, 4)$, $(3, 4)$, $(4, 1)$, $(4, 2)$, and $(4, 3)$. | def g(a, b):
cur = 1
res = 0
ze = 0
while cur <= b:
if b & cur:
b ^= cur
if a & b == 0:
res += 1 << ze
if a & cur == 0:
ze = ze + 1
cur <<= 1
return res
def f(a, b):
res = 0
if a == b:
return 0
if a == 0:
return 2 * b - 1 + f(1, b)
if a & 1:
res = res + 2 * (g(a, b) - g(a, a))
a = a + 1
if b & 1:
res = res + 2 * (g(b - 1, b) - g(b - 1, a))
return 3 * f(a >> 1, b >> 1) + res
t = int(input())
while t > 0:
t = t - 1
l, r = map(int, input().split())
print(f(l, r + 1)) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
While doing some spring cleaning, Daniel found an old calculator that he loves so much. However, it seems like it is broken. When he tries to compute $1 + 3$ using the calculator, he gets $2$ instead of $4$. But when he tries computing $1 + 4$, he gets the correct answer, $5$. Puzzled by this mystery, he opened up his calculator and found the answer to the riddle: the full adders became half adders!
So, when he tries to compute the sum $a + b$ using the calculator, he instead gets the xorsum $a \oplus b$ (read the definition by the link: https://en.wikipedia.org/wiki/Exclusive_or).
As he saw earlier, the calculator sometimes gives the correct answer. And so, he wonders, given integers $l$ and $r$, how many pairs of integers $(a, b)$ satisfy the following conditions: $$a + b = a \oplus b$$ $$l \leq a \leq r$$ $$l \leq b \leq r$$
However, Daniel the Barman is going to the bar and will return in two hours. He tells you to solve the problem before he returns, or else you will have to enjoy being blocked.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of testcases.
Then, $t$ lines follow, each containing two space-separated integers $l$ and $r$ ($0 \le l \le r \le 10^9$).
-----Output-----
Print $t$ integers, the $i$-th integer should be the answer to the $i$-th testcase.
-----Example-----
Input
3
1 4
323 323
1 1000000
Output
8
0
3439863766
-----Note-----
$a \oplus b$ denotes the bitwise XOR of $a$ and $b$.
For the first testcase, the pairs are: $(1, 2)$, $(1, 4)$, $(2, 1)$, $(2, 4)$, $(3, 4)$, $(4, 1)$, $(4, 2)$, and $(4, 3)$. | def get_bin(a):
nums = []
for i in range(32):
if 1 << i & a:
nums.append(1)
else:
nums.append(0)
while len(nums) > 0 and nums[-1] == 0:
nums.pop()
return nums
dp = {}
def get_num(a, b):
nonlocal dp
if (a, b) in dp:
return dp[a, b]
if a < 0 or b < 0:
return 0
if a == 0 and b == 0:
return 1
a_bin = get_bin(a)
b_bin = get_bin(b)
if b > a:
a_bin, b_bin = b_bin, a_bin
a, b = b, a
if len(a_bin) > len(b_bin):
big_bit = 1 << len(a_bin) - 1
to_ret = get_num(big_bit - 1, b) + get_num(a - big_bit, b)
dp[a, b] = to_ret
return to_ret
if sum(a_bin) == len(a_bin) and sum(b_bin) == len(b_bin):
to_ret = pow(3, len(a_bin))
dp[a, b] = to_ret
return to_ret
big_bit = 1 << len(a_bin) - 1
to_ret = get_num(big_bit - 1, b - big_bit) + get_num(a, big_bit - 1)
dp[a, b] = to_ret
return to_ret
tc = int(input(""))
for i in range(int(tc)):
nums = input("").split(" ")
l = int(nums[0])
r = int(nums[1])
ans = get_num(r, r) - 2 * get_num(r, l - 1) + get_num(l - 1, l - 1)
print(ans) | FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR RETURN VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Many countries have such a New Year or Christmas tradition as writing a letter to Santa including a wish list for presents. Vasya is an ordinary programmer boy. Like all ordinary boys, he is going to write the letter to Santa on the New Year Eve (we Russians actually expect Santa for the New Year, not for Christmas).
Vasya has come up with an algorithm he will follow while writing a letter. First he chooses two strings, s1 anf s2, consisting of uppercase English letters. Then the boy makes string sk, using a recurrent equation sn = sn - 2 + sn - 1, operation '+' means a concatenation (that is, the sequential record) of strings in the given order. Then Vasya writes down string sk on a piece of paper, puts it in the envelope and sends in to Santa.
Vasya is absolutely sure that Santa will bring him the best present if the resulting string sk has exactly x occurrences of substring AC (the short-cut reminds him ΠΎf accepted problems). Besides, Vasya decided that string s1 should have length n, and string s2 should have length m. Vasya hasn't decided anything else.
At the moment Vasya's got urgent New Year business, so he asks you to choose two strings for him, s1 and s2 in the required manner. Help Vasya.
Input
The first line contains four integers k, x, n, m (3 β€ k β€ 50; 0 β€ x β€ 109; 1 β€ n, m β€ 100).
Output
In the first line print string s1, consisting of n uppercase English letters. In the second line print string s2, consisting of m uppercase English letters. If there are multiple valid strings, print any of them.
If the required pair of strings doesn't exist, print "Happy new year!" without the quotes.
Examples
Input
3 2 2 2
Output
AC
AC
Input
3 3 2 2
Output
Happy new year!
Input
3 0 2 2
Output
AA
AA
Input
4 3 2 1
Output
Happy new year!
Input
4 2 2 1
Output
Happy new year! | def F(a, b, c, d, e, f, k):
for _ in range(k - 2):
a, b, c, d, e, f = d, e, f, a, b + e + (c and d), f
return e
k, x, n, m = map(int, input().split())
for a in [True, False]:
for c in [True, False]:
for b in range((n - a - c) // 2 + 1):
for d in [True, False]:
for f in [True, False]:
for e in range((m - d - f) // 2 + 1):
if F(a, b, c, d, e, f, k) == x:
print(
["", "C"][a]
+ "AC" * b
+ "Z" * (n - a - b - b - c)
+ ["", "A"][c]
)
print(
["", "C"][d]
+ "AC" * e
+ "Z" * (m - d - e - e - f)
+ ["", "A"][f]
)
exit()
print("Happy new year!") | FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR LIST NUMBER NUMBER FOR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER FOR VAR LIST NUMBER NUMBER FOR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP LIST STRING STRING VAR BIN_OP STRING VAR BIN_OP STRING BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR LIST STRING STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP LIST STRING STRING VAR BIN_OP STRING VAR BIN_OP STRING BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR LIST STRING STRING VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
Many countries have such a New Year or Christmas tradition as writing a letter to Santa including a wish list for presents. Vasya is an ordinary programmer boy. Like all ordinary boys, he is going to write the letter to Santa on the New Year Eve (we Russians actually expect Santa for the New Year, not for Christmas).
Vasya has come up with an algorithm he will follow while writing a letter. First he chooses two strings, s1 anf s2, consisting of uppercase English letters. Then the boy makes string sk, using a recurrent equation sn = sn - 2 + sn - 1, operation '+' means a concatenation (that is, the sequential record) of strings in the given order. Then Vasya writes down string sk on a piece of paper, puts it in the envelope and sends in to Santa.
Vasya is absolutely sure that Santa will bring him the best present if the resulting string sk has exactly x occurrences of substring AC (the short-cut reminds him ΠΎf accepted problems). Besides, Vasya decided that string s1 should have length n, and string s2 should have length m. Vasya hasn't decided anything else.
At the moment Vasya's got urgent New Year business, so he asks you to choose two strings for him, s1 and s2 in the required manner. Help Vasya.
Input
The first line contains four integers k, x, n, m (3 β€ k β€ 50; 0 β€ x β€ 109; 1 β€ n, m β€ 100).
Output
In the first line print string s1, consisting of n uppercase English letters. In the second line print string s2, consisting of m uppercase English letters. If there are multiple valid strings, print any of them.
If the required pair of strings doesn't exist, print "Happy new year!" without the quotes.
Examples
Input
3 2 2 2
Output
AC
AC
Input
3 3 2 2
Output
Happy new year!
Input
3 0 2 2
Output
AA
AA
Input
4 3 2 1
Output
Happy new year!
Input
4 2 2 1
Output
Happy new year! | import sys
def make_string(k, k2string, s1, s2):
if k in k2string:
return k2string[k]
if k == 1:
k2string[k] = s1
elif k == 2:
k2string[k] = s2
else:
k2string[k] = make_string(k - 2, k2string, s1, s2) + make_string(
k - 1, k2string, s1, s2
)
return k2string[k]
def count_comb(k, k2comb):
if k in k2comb:
return k2comb[k]
if k == 1:
k2comb[k] = [0, 0, 0, 0, 1, 0], ("1", "1")
elif k == 2:
k2comb[k] = [0, 0, 0, 0, 0, 1], ("2", "2")
else:
r_2 = count_comb(k - 2, k2comb)
r_1 = count_comb(k - 1, k2comb)
r = [(x + y) for x, y in zip(r_2[0], r_1[0])], (r_2[1][0], r_1[1][1])
ss = r_2[1][1] + r_1[1][0]
if ss == "11":
r[0][0] += 1
elif ss == "12":
r[0][1] += 1
elif ss == "21":
r[0][2] += 1
elif ss == "22":
r[0][3] += 1
k2comb[k] = r
return k2comb[k]
def max_independent_ac(l, cStart, aEnd):
if l <= 1:
return 0
elif cStart and not aEnd or not cStart and aEnd:
return (l - 1) // 2
elif cStart and aEnd:
return (l - 2) // 2
else:
return l // 2
def print_sequence(l, ac, cStart, aEnd):
cStart = 1 if cStart else 0
aEnd = 1 if aEnd else 0
if cStart:
print("C", end="")
for i in range(ac):
print("AC", end="")
for i in range(l - 2 * ac - cStart - aEnd):
print("B", end="")
if aEnd:
print("A", end="")
print()
def run_test(k, x, n, m):
comb = count_comb(k, {})[0]
for mask in range(16):
v = [0, 0, 0, 0, 0, 0]
if mask & 1 and mask & 2:
v[0] = 1
if mask & 2 and mask & 4:
v[1] = 1
if mask & 1 and mask & 8:
v[2] = 1
if mask & 4 and mask & 8:
v[3] = 1
if n == 1 and v[0] or m == 1 and v[3]:
continue
max1 = max_independent_ac(n, mask & 1, mask & 2)
max2 = max_independent_ac(m, mask & 4, mask & 8)
for p in range(max1 + 1):
for q in range(max2 + 1):
v[4] = p
v[5] = q
if sum([(x * y) for x, y in zip(v, comb)]) == x:
print_sequence(n, p, mask & 1, mask & 2)
print_sequence(m, q, mask & 4, mask & 8)
return
print("Happy new year!")
k, x, n, m = (int(x) for x in sys.stdin.readline().split(" "))
run_test(k, x, n, m) | IMPORT FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING IF VAR NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR STRING VAR NUMBER NUMBER NUMBER IF VAR STRING VAR NUMBER NUMBER NUMBER IF VAR STRING VAR NUMBER NUMBER NUMBER IF VAR STRING VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR RETURN BIN_OP BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR STRING STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR STRING STRING IF VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR DICT NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR |
Many countries have such a New Year or Christmas tradition as writing a letter to Santa including a wish list for presents. Vasya is an ordinary programmer boy. Like all ordinary boys, he is going to write the letter to Santa on the New Year Eve (we Russians actually expect Santa for the New Year, not for Christmas).
Vasya has come up with an algorithm he will follow while writing a letter. First he chooses two strings, s1 anf s2, consisting of uppercase English letters. Then the boy makes string sk, using a recurrent equation sn = sn - 2 + sn - 1, operation '+' means a concatenation (that is, the sequential record) of strings in the given order. Then Vasya writes down string sk on a piece of paper, puts it in the envelope and sends in to Santa.
Vasya is absolutely sure that Santa will bring him the best present if the resulting string sk has exactly x occurrences of substring AC (the short-cut reminds him ΠΎf accepted problems). Besides, Vasya decided that string s1 should have length n, and string s2 should have length m. Vasya hasn't decided anything else.
At the moment Vasya's got urgent New Year business, so he asks you to choose two strings for him, s1 and s2 in the required manner. Help Vasya.
Input
The first line contains four integers k, x, n, m (3 β€ k β€ 50; 0 β€ x β€ 109; 1 β€ n, m β€ 100).
Output
In the first line print string s1, consisting of n uppercase English letters. In the second line print string s2, consisting of m uppercase English letters. If there are multiple valid strings, print any of them.
If the required pair of strings doesn't exist, print "Happy new year!" without the quotes.
Examples
Input
3 2 2 2
Output
AC
AC
Input
3 3 2 2
Output
Happy new year!
Input
3 0 2 2
Output
AA
AA
Input
4 3 2 1
Output
Happy new year!
Input
4 2 2 1
Output
Happy new year! | def main():
k, x, n, m = map(int, input().split())
def f(s, e, n, cnt):
ret = [""] * n
ret[0] = s
ret[-1] = e
sa = 0 if s == "A" else 1
for i in range(cnt):
ret[sa] = "A"
ret[sa + 1] = "C"
sa += 2
for j in range(sa, n - 1):
ret[j] = "B"
return "".join(ret)
for sa in "ABC":
for ea in "ABC":
if n == 1:
ea = sa
for sb in "ABC":
for eb in "ABC":
if m == 1:
eb = sb
N = max(0, n - (sa != "A") - (ea != "C"))
M = max(0, m - (sb != "A") - (eb != "C"))
for i in range(1 + N // 2):
for j in range(1 + M // 2):
A = sa + ea
B = sb + eb
a, b = i, j
for c in range(k - 2):
a, b = b, a + b
if A[1] == "A" and B[0] == "C":
b += 1
A, B = B, A[0] + B[1]
if b == x:
print(f(sa, ea, n, i))
print(f(sb, eb, m, j))
return 0
print("Happy new year!")
return 0
main() | FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING RETURN FUNC_CALL STRING VAR FOR VAR STRING FOR VAR STRING IF VAR NUMBER ASSIGN VAR VAR FOR VAR STRING FOR VAR STRING IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR STRING VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR STRING VAR STRING FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR STRING RETURN NUMBER EXPR FUNC_CALL VAR |
Many countries have such a New Year or Christmas tradition as writing a letter to Santa including a wish list for presents. Vasya is an ordinary programmer boy. Like all ordinary boys, he is going to write the letter to Santa on the New Year Eve (we Russians actually expect Santa for the New Year, not for Christmas).
Vasya has come up with an algorithm he will follow while writing a letter. First he chooses two strings, s1 anf s2, consisting of uppercase English letters. Then the boy makes string sk, using a recurrent equation sn = sn - 2 + sn - 1, operation '+' means a concatenation (that is, the sequential record) of strings in the given order. Then Vasya writes down string sk on a piece of paper, puts it in the envelope and sends in to Santa.
Vasya is absolutely sure that Santa will bring him the best present if the resulting string sk has exactly x occurrences of substring AC (the short-cut reminds him ΠΎf accepted problems). Besides, Vasya decided that string s1 should have length n, and string s2 should have length m. Vasya hasn't decided anything else.
At the moment Vasya's got urgent New Year business, so he asks you to choose two strings for him, s1 and s2 in the required manner. Help Vasya.
Input
The first line contains four integers k, x, n, m (3 β€ k β€ 50; 0 β€ x β€ 109; 1 β€ n, m β€ 100).
Output
In the first line print string s1, consisting of n uppercase English letters. In the second line print string s2, consisting of m uppercase English letters. If there are multiple valid strings, print any of them.
If the required pair of strings doesn't exist, print "Happy new year!" without the quotes.
Examples
Input
3 2 2 2
Output
AC
AC
Input
3 3 2 2
Output
Happy new year!
Input
3 0 2 2
Output
AA
AA
Input
4 3 2 1
Output
Happy new year!
Input
4 2 2 1
Output
Happy new year! | def f(k):
if k == 1:
return 1, 0, 0, 0, 0
if k == 2:
return 0, 1, 0, 0, 0
if k == 3:
return 1, 1, 1, 0, 0
a, b = 1, 1
for i in range(k - 3):
a, b = b, a + b
c = a
d = e = 0
for i in range(k - 3):
d, e = e, d + e + int(i % 2 == 0)
return a, b, c, d, e
def r(a, i, n):
if n == 1:
if a == "BB":
return "B"
if a == "BA":
return "A"
if a == "CB":
return "C"
return False
if i == 0:
if a == "BB":
return "B" * n
if a == "BA":
return "B" * (n - 1) + "A"
if a == "CB":
return "C" + "B" * (n - 1)
x = "AC" * i
n -= len(x)
if n < 0:
return False
if a[0] == "B":
if a[1] == "B":
return x + "C" * n
return x + "A" * n
else:
if a[1] == "B":
return "C" * n + x
n -= 1
if n < 1:
return False
return "C" + x + "A" * n
def g(a, b, i, j, n, m):
x, y = r(a, i, n), r(b, j, m)
if x and y:
print(x)
print(y)
return True
return False
def h():
k, x, n, m = map(int, input().split())
t = f(k)
for i in range(n // 2 + 1):
for j in range(m // 2 + 1):
if x == t[0] * i + t[1] * j:
print("AC" * i + "B" * (n - 2 * i))
print("AC" * j + "B" * (m - 2 * j))
return
for i in range((n + 1) // 2):
for j in range((m + 1) // 2):
y = x - (t[0] * i + t[1] * j)
if y == t[2] and g("BA", "CB", i, j, n, m):
return
if y == t[3] and g("BB", "CA", i, j, n, m):
return
if y == t[4] and g("CB", "BA", i, j, n, m):
return
if y == t[2] + t[3] and g("BA", "CA", i, j, n, m):
return
if y == t[3] + t[4] and g("CB", "BA", i, j, n, m):
return
if y == t[2] + t[3] + t[4] and g("CA", "CA", i, j, n, m):
return
print("Happy new year!")
h() | FUNC_DEF IF VAR NUMBER RETURN NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER RETURN NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER RETURN NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER IF VAR STRING RETURN STRING IF VAR STRING RETURN STRING IF VAR STRING RETURN STRING RETURN NUMBER IF VAR NUMBER IF VAR STRING RETURN BIN_OP STRING VAR IF VAR STRING RETURN BIN_OP BIN_OP STRING BIN_OP VAR NUMBER STRING IF VAR STRING RETURN BIN_OP STRING BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR BIN_OP STRING VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER STRING IF VAR NUMBER STRING RETURN BIN_OP VAR BIN_OP STRING VAR RETURN BIN_OP VAR BIN_OP STRING VAR IF VAR NUMBER STRING RETURN BIN_OP BIN_OP STRING VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING BIN_OP VAR BIN_OP NUMBER VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER FUNC_CALL VAR STRING STRING VAR VAR VAR VAR RETURN IF VAR VAR NUMBER FUNC_CALL VAR STRING STRING VAR VAR VAR VAR RETURN IF VAR VAR NUMBER FUNC_CALL VAR STRING STRING VAR VAR VAR VAR RETURN IF VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR STRING STRING VAR VAR VAR VAR RETURN IF VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR STRING STRING VAR VAR VAR VAR RETURN IF VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR STRING STRING VAR VAR VAR VAR RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR |
Many countries have such a New Year or Christmas tradition as writing a letter to Santa including a wish list for presents. Vasya is an ordinary programmer boy. Like all ordinary boys, he is going to write the letter to Santa on the New Year Eve (we Russians actually expect Santa for the New Year, not for Christmas).
Vasya has come up with an algorithm he will follow while writing a letter. First he chooses two strings, s1 anf s2, consisting of uppercase English letters. Then the boy makes string sk, using a recurrent equation sn = sn - 2 + sn - 1, operation '+' means a concatenation (that is, the sequential record) of strings in the given order. Then Vasya writes down string sk on a piece of paper, puts it in the envelope and sends in to Santa.
Vasya is absolutely sure that Santa will bring him the best present if the resulting string sk has exactly x occurrences of substring AC (the short-cut reminds him ΠΎf accepted problems). Besides, Vasya decided that string s1 should have length n, and string s2 should have length m. Vasya hasn't decided anything else.
At the moment Vasya's got urgent New Year business, so he asks you to choose two strings for him, s1 and s2 in the required manner. Help Vasya.
Input
The first line contains four integers k, x, n, m (3 β€ k β€ 50; 0 β€ x β€ 109; 1 β€ n, m β€ 100).
Output
In the first line print string s1, consisting of n uppercase English letters. In the second line print string s2, consisting of m uppercase English letters. If there are multiple valid strings, print any of them.
If the required pair of strings doesn't exist, print "Happy new year!" without the quotes.
Examples
Input
3 2 2 2
Output
AC
AC
Input
3 3 2 2
Output
Happy new year!
Input
3 0 2 2
Output
AA
AA
Input
4 3 2 1
Output
Happy new year!
Input
4 2 2 1
Output
Happy new year! | k, x, n, m = map(int, input().split())
def calc(k, a, b, op1, ed1, op2, ed2):
if k == 2:
return b
return calc(k - 1, b, a + b + (ed1 & op2), op2, ed2, op1, ed2)
def make(len, a, op, ed):
res, i = "", 0
if op:
res += "C"
i += 1
if ed:
len -= 1
while a:
res += "AC"
a -= 1
i += 2
while i < len:
res += "P"
i += 1
if ed:
res += "A"
return res
def check(a, b, op1, ed1, op2, ed2):
if 2 * a + op1 + ed1 > n or 2 * b + op2 + ed2 > m:
return 0
if calc(k, a, b, op1, ed1, op2, ed2) == x:
print(make(n, a, op1, ed1))
print(make(m, b, op2, ed2))
return 1
flag = 0
for i in range(0, (n >> 1) + 1):
for j in range(0, (m >> 1) + 1):
for l in range(0, 16):
if check(i, j, l & 1, l >> 1 & 1, l >> 2 & 1, l >> 3 & 1):
flag = 1
break
if flag:
break
if flag:
break
if not flag:
print("Happy new year!") | ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR STRING NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR NUMBER WHILE VAR VAR VAR STRING VAR NUMBER IF VAR VAR STRING RETURN VAR FUNC_DEF IF BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR IF VAR IF VAR EXPR FUNC_CALL VAR STRING |
Many countries have such a New Year or Christmas tradition as writing a letter to Santa including a wish list for presents. Vasya is an ordinary programmer boy. Like all ordinary boys, he is going to write the letter to Santa on the New Year Eve (we Russians actually expect Santa for the New Year, not for Christmas).
Vasya has come up with an algorithm he will follow while writing a letter. First he chooses two strings, s1 anf s2, consisting of uppercase English letters. Then the boy makes string sk, using a recurrent equation sn = sn - 2 + sn - 1, operation '+' means a concatenation (that is, the sequential record) of strings in the given order. Then Vasya writes down string sk on a piece of paper, puts it in the envelope and sends in to Santa.
Vasya is absolutely sure that Santa will bring him the best present if the resulting string sk has exactly x occurrences of substring AC (the short-cut reminds him ΠΎf accepted problems). Besides, Vasya decided that string s1 should have length n, and string s2 should have length m. Vasya hasn't decided anything else.
At the moment Vasya's got urgent New Year business, so he asks you to choose two strings for him, s1 and s2 in the required manner. Help Vasya.
Input
The first line contains four integers k, x, n, m (3 β€ k β€ 50; 0 β€ x β€ 109; 1 β€ n, m β€ 100).
Output
In the first line print string s1, consisting of n uppercase English letters. In the second line print string s2, consisting of m uppercase English letters. If there are multiple valid strings, print any of them.
If the required pair of strings doesn't exist, print "Happy new year!" without the quotes.
Examples
Input
3 2 2 2
Output
AC
AC
Input
3 3 2 2
Output
Happy new year!
Input
3 0 2 2
Output
AA
AA
Input
4 3 2 1
Output
Happy new year!
Input
4 2 2 1
Output
Happy new year! | import itertools
import sys
k, x, n, m = map(int, input().split())
def valid(inicio, fim, tamanho, num_ac):
if num_ac == 0:
if tamanho == 1:
return "" if inicio != fim else inicio
elif tamanho == 2:
return inicio + fim if inicio + fim != "AC" else ""
else:
return inicio + "B" * (tamanho - 2) + fim
s = "AC" * num_ac
if not s or s[0] != inicio:
s = inicio + s
if len(s) > tamanho or len(s) == tamanho and s[-1] != fim:
return ""
s += fim * (tamanho - len(s))
return s
a = [0] * 51
a[1] = 1
b = [0] * 51
b[2] = 1
for i in range(3, 51):
a[i] = a[i - 2] + a[i - 1]
b[i] = b[i - 2] + b[i - 1]
for letters in itertools.product(["A", "B", "C"], repeat=4):
first_s1 = letters[0]
last_s1 = letters[1]
first_s2 = letters[2]
last_s2 = letters[3]
c = [0] * 51
meio = last_s1 + first_s2
c[3] = 1 if meio == "AC" else 0
for i in range(4, 51):
meio = last_s2 + (first_s1 if i % 2 == 0 else first_s2)
c[i] = c[i - 2] + c[i - 1] + (1 if meio == "AC" else 0)
for occ1 in range(0, 51):
tmp = x - a[k] * occ1 - c[k]
if tmp % b[k] != 0:
continue
tmp = tmp // b[k]
if tmp < 0 or tmp > 50:
continue
s1 = valid(first_s1, last_s1, n, occ1)
s2 = valid(first_s2, last_s2, m, tmp)
if s1 and s2:
print(s1)
print(s2)
sys.exit(0)
print("Happy new year!") | IMPORT IMPORT ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER IF VAR NUMBER RETURN VAR VAR STRING VAR IF VAR NUMBER RETURN BIN_OP VAR VAR STRING BIN_OP VAR VAR STRING RETURN BIN_OP BIN_OP VAR BIN_OP STRING BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP STRING VAR IF VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR RETURN STRING VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR LIST STRING STRING STRING NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING |
Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0.
There are also n cards, each card has 2 attributes: length l_{i} and cost c_{i}. If she pays c_{i} dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length l_{i}, i. e. from cell x to cell (x - l_{i}) or cell (x + l_{i}).
She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible.
If this is possible, calculate the minimal cost.
-----Input-----
The first line contains an integer n (1 β€ n β€ 300), number of cards.
The second line contains n numbers l_{i} (1 β€ l_{i} β€ 10^9), the jump lengths of cards.
The third line contains n numbers c_{i} (1 β€ c_{i} β€ 10^5), the costs of cards.
-----Output-----
If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards.
-----Examples-----
Input
3
100 99 9900
1 1 1
Output
2
Input
5
10 20 30 40 50
1 1 1 1 1
Output
-1
Input
7
15015 10010 6006 4290 2730 2310 1
1 1 1 1 1 1 10
Output
6
Input
8
4264 4921 6321 6984 2316 8432 6120 1026
4264 4921 6321 6984 2316 8432 6120 1026
Output
7237
-----Note-----
In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell.
In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1. | def main():
input()
acc = {(0): 0}
for p, c in zip(list(map(int, input().split())), list(map(int, input().split()))):
adds = []
for b, u in acc.items():
a = p
while b:
a, b = b, a % b
adds.append((a, u + c))
for a, u in adds:
acc[a] = min(u, acc.get(a, 1000000000))
print(acc.get(1, -1))
def __starting_point():
main()
__starting_point() | FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
You are given a sequence of $n$ integers $a_1, \, a_2, \, \dots, \, a_n$.
Does there exist a sequence of $n$ integers $b_1, \, b_2, \, \dots, \, b_n$ such that the following property holds?
For each $1 \le i \le n$, there exist two (not necessarily distinct) indices $j$ and $k$ ($1 \le j, \, k \le n$) such that $a_i = b_j - b_k$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 20$) β 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 10$).
The second line of each test case contains the $n$ integers $a_1, \, \dots, \, a_n$ ($-10^5 \le a_i \le 10^5$).
-----Output-----
For each test case, output a line containing YES if a sequence $b_1, \, \dots, \, b_n$ satisfying the required property exists, and NO otherwise.
-----Examples-----
Input
5
5
4 -7 -1 5 10
1
0
3
1 10 100
4
-3 2 10 2
9
25 -171 250 174 152 242 100 -205 -258
Output
YES
YES
NO
YES
YES
-----Note-----
In the first test case, the sequence $b = [-9, \, 2, \, 1, \, 3, \, -2]$ satisfies the property. Indeed, the following holds:
$a_1 = 4 = 2 - (-2) = b_2 - b_5$;
$a_2 = -7 = -9 - (-2) = b_1 - b_5$;
$a_3 = -1 = 1 - 2 = b_3 - b_2$;
$a_4 = 5 = 3 - (-2) = b_4 - b_5$;
$a_5 = 10 = 1 - (-9) = b_3 - b_1$.
In the second test case, it is sufficient to choose $b = [0]$, since $a_1 = 0 = 0 - 0 = b_1 - b_1$.
In the third test case, it is possible to show that no sequence $b$ of length $3$ satisfies the property. | def f(l, i, s):
if s == 0:
return 1
if i >= len(l):
return 0
if s == -1:
return f(l, i + 1, l[i]) or f(l, i + 1, -1) or f(l, i + 1, -l[i])
else:
return f(l, i + 1, s + l[i]) or f(l, i + 1, s) or f(l, i + 1, s - l[i])
for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
print("YES" if f(l, 0, -1) else "NO") | FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER STRING STRING |
You are given a sequence of $n$ integers $a_1, \, a_2, \, \dots, \, a_n$.
Does there exist a sequence of $n$ integers $b_1, \, b_2, \, \dots, \, b_n$ such that the following property holds?
For each $1 \le i \le n$, there exist two (not necessarily distinct) indices $j$ and $k$ ($1 \le j, \, k \le n$) such that $a_i = b_j - b_k$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 20$) β 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 10$).
The second line of each test case contains the $n$ integers $a_1, \, \dots, \, a_n$ ($-10^5 \le a_i \le 10^5$).
-----Output-----
For each test case, output a line containing YES if a sequence $b_1, \, \dots, \, b_n$ satisfying the required property exists, and NO otherwise.
-----Examples-----
Input
5
5
4 -7 -1 5 10
1
0
3
1 10 100
4
-3 2 10 2
9
25 -171 250 174 152 242 100 -205 -258
Output
YES
YES
NO
YES
YES
-----Note-----
In the first test case, the sequence $b = [-9, \, 2, \, 1, \, 3, \, -2]$ satisfies the property. Indeed, the following holds:
$a_1 = 4 = 2 - (-2) = b_2 - b_5$;
$a_2 = -7 = -9 - (-2) = b_1 - b_5$;
$a_3 = -1 = 1 - 2 = b_3 - b_2$;
$a_4 = 5 = 3 - (-2) = b_4 - b_5$;
$a_5 = 10 = 1 - (-9) = b_3 - b_1$.
In the second test case, it is sufficient to choose $b = [0]$, since $a_1 = 0 = 0 - 0 = b_1 - b_1$.
In the third test case, it is possible to show that no sequence $b$ of length $3$ satisfies the property. | for _ in range(int(input())):
N = int(input())
A = list(map(int, input().split()))
found = False
for mask in range(1, 3**N):
sm = 0
for i in range(N):
rem = mask % 3
mask //= 3
if rem == 2:
rem = -1
sm += rem * A[i]
if sm == 0:
found = True
break
if found:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given a sequence of $n$ integers $a_1, \, a_2, \, \dots, \, a_n$.
Does there exist a sequence of $n$ integers $b_1, \, b_2, \, \dots, \, b_n$ such that the following property holds?
For each $1 \le i \le n$, there exist two (not necessarily distinct) indices $j$ and $k$ ($1 \le j, \, k \le n$) such that $a_i = b_j - b_k$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 20$) β 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 10$).
The second line of each test case contains the $n$ integers $a_1, \, \dots, \, a_n$ ($-10^5 \le a_i \le 10^5$).
-----Output-----
For each test case, output a line containing YES if a sequence $b_1, \, \dots, \, b_n$ satisfying the required property exists, and NO otherwise.
-----Examples-----
Input
5
5
4 -7 -1 5 10
1
0
3
1 10 100
4
-3 2 10 2
9
25 -171 250 174 152 242 100 -205 -258
Output
YES
YES
NO
YES
YES
-----Note-----
In the first test case, the sequence $b = [-9, \, 2, \, 1, \, 3, \, -2]$ satisfies the property. Indeed, the following holds:
$a_1 = 4 = 2 - (-2) = b_2 - b_5$;
$a_2 = -7 = -9 - (-2) = b_1 - b_5$;
$a_3 = -1 = 1 - 2 = b_3 - b_2$;
$a_4 = 5 = 3 - (-2) = b_4 - b_5$;
$a_5 = 10 = 1 - (-9) = b_3 - b_1$.
In the second test case, it is sufficient to choose $b = [0]$, since $a_1 = 0 = 0 - 0 = b_1 - b_1$.
In the third test case, it is possible to show that no sequence $b$ of length $3$ satisfies the property. | ans = []
for _ in range(int(input())):
n = int(input())
u = list(map(int, input().split()))
if 0 in u:
ans.append("YES")
continue
for i in range(n):
if u[i] < 0:
u[i] = -u[i]
u.sort()
for i in range(1, n):
if u[i] == u[i - 1]:
ans.append("YES")
break
else:
for msk1 in range(2**n):
cnt = 0
st = set()
for i in range(n):
if msk1 & 2**i:
cnt += 1
else:
st.add(u[i])
for msk2 in range(2**cnt):
sm = 0
cnt = 0
for i in range(n):
if msk1 & 2**i:
if msk2 & 2**cnt:
sm += u[i]
else:
sm -= u[i]
cnt += 1
if cnt > 1 and sm in st:
ans.append("YES")
break
else:
continue
break
else:
ans.append("NO")
print("\n".join(ans)) | ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF NUMBER VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You are given a sequence of $n$ integers $a_1, \, a_2, \, \dots, \, a_n$.
Does there exist a sequence of $n$ integers $b_1, \, b_2, \, \dots, \, b_n$ such that the following property holds?
For each $1 \le i \le n$, there exist two (not necessarily distinct) indices $j$ and $k$ ($1 \le j, \, k \le n$) such that $a_i = b_j - b_k$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 20$) β 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 10$).
The second line of each test case contains the $n$ integers $a_1, \, \dots, \, a_n$ ($-10^5 \le a_i \le 10^5$).
-----Output-----
For each test case, output a line containing YES if a sequence $b_1, \, \dots, \, b_n$ satisfying the required property exists, and NO otherwise.
-----Examples-----
Input
5
5
4 -7 -1 5 10
1
0
3
1 10 100
4
-3 2 10 2
9
25 -171 250 174 152 242 100 -205 -258
Output
YES
YES
NO
YES
YES
-----Note-----
In the first test case, the sequence $b = [-9, \, 2, \, 1, \, 3, \, -2]$ satisfies the property. Indeed, the following holds:
$a_1 = 4 = 2 - (-2) = b_2 - b_5$;
$a_2 = -7 = -9 - (-2) = b_1 - b_5$;
$a_3 = -1 = 1 - 2 = b_3 - b_2$;
$a_4 = 5 = 3 - (-2) = b_4 - b_5$;
$a_5 = 10 = 1 - (-9) = b_3 - b_1$.
In the second test case, it is sufficient to choose $b = [0]$, since $a_1 = 0 = 0 - 0 = b_1 - b_1$.
In the third test case, it is possible to show that no sequence $b$ of length $3$ satisfies the property. | from itertools import *
for s in [*open(0)][2::2]:
a = (*map(int, s.split()),)
n = len(a)
print(
"YNEOS"[
len({sum(x * y for x, y in zip(a, p)) for p in product((0, 1), repeat=n)})
>> n :: 2
]
) | FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR NUMBER |
You are given a sequence of $n$ integers $a_1, \, a_2, \, \dots, \, a_n$.
Does there exist a sequence of $n$ integers $b_1, \, b_2, \, \dots, \, b_n$ such that the following property holds?
For each $1 \le i \le n$, there exist two (not necessarily distinct) indices $j$ and $k$ ($1 \le j, \, k \le n$) such that $a_i = b_j - b_k$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 20$) β 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 10$).
The second line of each test case contains the $n$ integers $a_1, \, \dots, \, a_n$ ($-10^5 \le a_i \le 10^5$).
-----Output-----
For each test case, output a line containing YES if a sequence $b_1, \, \dots, \, b_n$ satisfying the required property exists, and NO otherwise.
-----Examples-----
Input
5
5
4 -7 -1 5 10
1
0
3
1 10 100
4
-3 2 10 2
9
25 -171 250 174 152 242 100 -205 -258
Output
YES
YES
NO
YES
YES
-----Note-----
In the first test case, the sequence $b = [-9, \, 2, \, 1, \, 3, \, -2]$ satisfies the property. Indeed, the following holds:
$a_1 = 4 = 2 - (-2) = b_2 - b_5$;
$a_2 = -7 = -9 - (-2) = b_1 - b_5$;
$a_3 = -1 = 1 - 2 = b_3 - b_2$;
$a_4 = 5 = 3 - (-2) = b_4 - b_5$;
$a_5 = 10 = 1 - (-9) = b_3 - b_1$.
In the second test case, it is sufficient to choose $b = [0]$, since $a_1 = 0 = 0 - 0 = b_1 - b_1$.
In the third test case, it is possible to show that no sequence $b$ of length $3$ satisfies the property. | def r(a, suma, cerca):
if suma == cerca:
return True
if not a:
return False
last = a.pop()
possible = r(a, suma + last, cerca) or r(a, suma - last, cerca) or r(a, suma, cerca)
a.append(last)
return possible
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
print("YES" if any(r(a[:i] + a[i + 1 :], 0, a[i]) for i in range(n)) else "NO") | FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR VAR STRING STRING |
You are given a sequence of $n$ integers $a_1, \, a_2, \, \dots, \, a_n$.
Does there exist a sequence of $n$ integers $b_1, \, b_2, \, \dots, \, b_n$ such that the following property holds?
For each $1 \le i \le n$, there exist two (not necessarily distinct) indices $j$ and $k$ ($1 \le j, \, k \le n$) such that $a_i = b_j - b_k$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 20$) β 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 10$).
The second line of each test case contains the $n$ integers $a_1, \, \dots, \, a_n$ ($-10^5 \le a_i \le 10^5$).
-----Output-----
For each test case, output a line containing YES if a sequence $b_1, \, \dots, \, b_n$ satisfying the required property exists, and NO otherwise.
-----Examples-----
Input
5
5
4 -7 -1 5 10
1
0
3
1 10 100
4
-3 2 10 2
9
25 -171 250 174 152 242 100 -205 -258
Output
YES
YES
NO
YES
YES
-----Note-----
In the first test case, the sequence $b = [-9, \, 2, \, 1, \, 3, \, -2]$ satisfies the property. Indeed, the following holds:
$a_1 = 4 = 2 - (-2) = b_2 - b_5$;
$a_2 = -7 = -9 - (-2) = b_1 - b_5$;
$a_3 = -1 = 1 - 2 = b_3 - b_2$;
$a_4 = 5 = 3 - (-2) = b_4 - b_5$;
$a_5 = 10 = 1 - (-9) = b_3 - b_1$.
In the second test case, it is sufficient to choose $b = [0]$, since $a_1 = 0 = 0 - 0 = b_1 - b_1$.
In the third test case, it is possible to show that no sequence $b$ of length $3$ satisfies the property. | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
x = 2**n - 1
d = []
an = "NO"
for i in range(x + 1):
c = sum([a[j] for j in range(n) if i & 1 << j])
if c in d:
an = "YES"
break
else:
d.append(c)
print(an) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a sequence of $n$ integers $a_1, \, a_2, \, \dots, \, a_n$.
Does there exist a sequence of $n$ integers $b_1, \, b_2, \, \dots, \, b_n$ such that the following property holds?
For each $1 \le i \le n$, there exist two (not necessarily distinct) indices $j$ and $k$ ($1 \le j, \, k \le n$) such that $a_i = b_j - b_k$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 20$) β 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 10$).
The second line of each test case contains the $n$ integers $a_1, \, \dots, \, a_n$ ($-10^5 \le a_i \le 10^5$).
-----Output-----
For each test case, output a line containing YES if a sequence $b_1, \, \dots, \, b_n$ satisfying the required property exists, and NO otherwise.
-----Examples-----
Input
5
5
4 -7 -1 5 10
1
0
3
1 10 100
4
-3 2 10 2
9
25 -171 250 174 152 242 100 -205 -258
Output
YES
YES
NO
YES
YES
-----Note-----
In the first test case, the sequence $b = [-9, \, 2, \, 1, \, 3, \, -2]$ satisfies the property. Indeed, the following holds:
$a_1 = 4 = 2 - (-2) = b_2 - b_5$;
$a_2 = -7 = -9 - (-2) = b_1 - b_5$;
$a_3 = -1 = 1 - 2 = b_3 - b_2$;
$a_4 = 5 = 3 - (-2) = b_4 - b_5$;
$a_5 = 10 = 1 - (-9) = b_3 - b_1$.
In the second test case, it is sufficient to choose $b = [0]$, since $a_1 = 0 = 0 - 0 = b_1 - b_1$.
In the third test case, it is possible to show that no sequence $b$ of length $3$ satisfies the property. | def main():
t = int(input())
for i in range(t):
c = int(input())
s = set()
k = 999999
for j in map(int, input().split()):
s.add(abs(j))
k = min(abs(j), k)
if len(s) < c or k == 0:
print("YES")
continue
m = list(s)
k = 1 << c
k += 1
flag = False
for j in range(k):
t = 0
L = 0
for h in range(c):
if 1 << h & j != 0:
t += m[h]
L += 1
if t in s and L > 1:
flag = True
break
s.add(t)
if flag:
print("YES")
else:
print("NO")
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR |
You are given a sequence of $n$ integers $a_1, \, a_2, \, \dots, \, a_n$.
Does there exist a sequence of $n$ integers $b_1, \, b_2, \, \dots, \, b_n$ such that the following property holds?
For each $1 \le i \le n$, there exist two (not necessarily distinct) indices $j$ and $k$ ($1 \le j, \, k \le n$) such that $a_i = b_j - b_k$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 20$) β 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 10$).
The second line of each test case contains the $n$ integers $a_1, \, \dots, \, a_n$ ($-10^5 \le a_i \le 10^5$).
-----Output-----
For each test case, output a line containing YES if a sequence $b_1, \, \dots, \, b_n$ satisfying the required property exists, and NO otherwise.
-----Examples-----
Input
5
5
4 -7 -1 5 10
1
0
3
1 10 100
4
-3 2 10 2
9
25 -171 250 174 152 242 100 -205 -258
Output
YES
YES
NO
YES
YES
-----Note-----
In the first test case, the sequence $b = [-9, \, 2, \, 1, \, 3, \, -2]$ satisfies the property. Indeed, the following holds:
$a_1 = 4 = 2 - (-2) = b_2 - b_5$;
$a_2 = -7 = -9 - (-2) = b_1 - b_5$;
$a_3 = -1 = 1 - 2 = b_3 - b_2$;
$a_4 = 5 = 3 - (-2) = b_4 - b_5$;
$a_5 = 10 = 1 - (-9) = b_3 - b_1$.
In the second test case, it is sufficient to choose $b = [0]$, since $a_1 = 0 = 0 - 0 = b_1 - b_1$.
In the third test case, it is possible to show that no sequence $b$ of length $3$ satisfies the property. | t = int(input())
for o in range(t):
n = int(input())
l = list(map(int, input().strip().split()))
zeros = 0
can = False
for i in l:
if i == 0:
zeros += 1
if zeros > 1:
print("YES")
continue
mx = 1
bit = 2**n
sums = {}
powers = []
k = 1
for i in range(n):
powers.append(k)
k *= 2
for i in range(bit):
ones = []
for j in range(n):
if i & powers[j]:
ones.append(j)
tot = 0
for ind in ones:
tot += l[ind]
if tot not in sums:
sums[tot] = 1
else:
sums[tot] += 1
if sums[tot] > mx:
print("YES")
can = True
break
if can == False:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING |
You are given a sequence of $n$ integers $a_1, \, a_2, \, \dots, \, a_n$.
Does there exist a sequence of $n$ integers $b_1, \, b_2, \, \dots, \, b_n$ such that the following property holds?
For each $1 \le i \le n$, there exist two (not necessarily distinct) indices $j$ and $k$ ($1 \le j, \, k \le n$) such that $a_i = b_j - b_k$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 20$) β 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 10$).
The second line of each test case contains the $n$ integers $a_1, \, \dots, \, a_n$ ($-10^5 \le a_i \le 10^5$).
-----Output-----
For each test case, output a line containing YES if a sequence $b_1, \, \dots, \, b_n$ satisfying the required property exists, and NO otherwise.
-----Examples-----
Input
5
5
4 -7 -1 5 10
1
0
3
1 10 100
4
-3 2 10 2
9
25 -171 250 174 152 242 100 -205 -258
Output
YES
YES
NO
YES
YES
-----Note-----
In the first test case, the sequence $b = [-9, \, 2, \, 1, \, 3, \, -2]$ satisfies the property. Indeed, the following holds:
$a_1 = 4 = 2 - (-2) = b_2 - b_5$;
$a_2 = -7 = -9 - (-2) = b_1 - b_5$;
$a_3 = -1 = 1 - 2 = b_3 - b_2$;
$a_4 = 5 = 3 - (-2) = b_4 - b_5$;
$a_5 = 10 = 1 - (-9) = b_3 - b_1$.
In the second test case, it is sufficient to choose $b = [0]$, since $a_1 = 0 = 0 - 0 = b_1 - b_1$.
In the third test case, it is possible to show that no sequence $b$ of length $3$ satisfies the property. | for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
def f(i, s, c):
if i == n:
if s == 0 and c != n:
return True
return False
if f(i + 1, s, c + 1) or f(i + 1, s + l[i], c) or f(i + 1, s - l[i], c):
return True
return False
if f(0, 0, 0):
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR IF VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given a sequence of $n$ integers $a_1, \, a_2, \, \dots, \, a_n$.
Does there exist a sequence of $n$ integers $b_1, \, b_2, \, \dots, \, b_n$ such that the following property holds?
For each $1 \le i \le n$, there exist two (not necessarily distinct) indices $j$ and $k$ ($1 \le j, \, k \le n$) such that $a_i = b_j - b_k$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 20$) β 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 10$).
The second line of each test case contains the $n$ integers $a_1, \, \dots, \, a_n$ ($-10^5 \le a_i \le 10^5$).
-----Output-----
For each test case, output a line containing YES if a sequence $b_1, \, \dots, \, b_n$ satisfying the required property exists, and NO otherwise.
-----Examples-----
Input
5
5
4 -7 -1 5 10
1
0
3
1 10 100
4
-3 2 10 2
9
25 -171 250 174 152 242 100 -205 -258
Output
YES
YES
NO
YES
YES
-----Note-----
In the first test case, the sequence $b = [-9, \, 2, \, 1, \, 3, \, -2]$ satisfies the property. Indeed, the following holds:
$a_1 = 4 = 2 - (-2) = b_2 - b_5$;
$a_2 = -7 = -9 - (-2) = b_1 - b_5$;
$a_3 = -1 = 1 - 2 = b_3 - b_2$;
$a_4 = 5 = 3 - (-2) = b_4 - b_5$;
$a_5 = 10 = 1 - (-9) = b_3 - b_1$.
In the second test case, it is sufficient to choose $b = [0]$, since $a_1 = 0 = 0 - 0 = b_1 - b_1$.
In the third test case, it is possible to show that no sequence $b$ of length $3$ satisfies the property. | import sys
t = int(sys.stdin.readline())
def f(k, su, w):
if w and su == 0:
return True
if k < n:
if f(k + 1, su + nums[k], True):
return True
if f(k + 1, su - nums[k], True):
return True
if w:
if f(k + 1, su, True):
return True
elif f(k + 1, su, False):
return True
for _ in range(t):
n = int(sys.stdin.readline())
nums = list(map(int, sys.stdin.readline().split()))
q = f(0, 0, False)
if q:
print("YES")
else:
print("NO") | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR NUMBER RETURN NUMBER IF VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER RETURN NUMBER IF VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given a sequence of $n$ integers $a_1, \, a_2, \, \dots, \, a_n$.
Does there exist a sequence of $n$ integers $b_1, \, b_2, \, \dots, \, b_n$ such that the following property holds?
For each $1 \le i \le n$, there exist two (not necessarily distinct) indices $j$ and $k$ ($1 \le j, \, k \le n$) such that $a_i = b_j - b_k$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 20$) β 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 10$).
The second line of each test case contains the $n$ integers $a_1, \, \dots, \, a_n$ ($-10^5 \le a_i \le 10^5$).
-----Output-----
For each test case, output a line containing YES if a sequence $b_1, \, \dots, \, b_n$ satisfying the required property exists, and NO otherwise.
-----Examples-----
Input
5
5
4 -7 -1 5 10
1
0
3
1 10 100
4
-3 2 10 2
9
25 -171 250 174 152 242 100 -205 -258
Output
YES
YES
NO
YES
YES
-----Note-----
In the first test case, the sequence $b = [-9, \, 2, \, 1, \, 3, \, -2]$ satisfies the property. Indeed, the following holds:
$a_1 = 4 = 2 - (-2) = b_2 - b_5$;
$a_2 = -7 = -9 - (-2) = b_1 - b_5$;
$a_3 = -1 = 1 - 2 = b_3 - b_2$;
$a_4 = 5 = 3 - (-2) = b_4 - b_5$;
$a_5 = 10 = 1 - (-9) = b_3 - b_1$.
In the second test case, it is sufficient to choose $b = [0]$, since $a_1 = 0 = 0 - 0 = b_1 - b_1$.
In the third test case, it is possible to show that no sequence $b$ of length $3$ satisfies the property. | T = int(input())
for _ in range(T):
N = int(input())
A = [abs(int(a)) for a in input().split()]
L = []
for i in range(1 << N):
s = 0
for j in range(N):
if i >> j & 1:
s += A[j]
L.append(s)
print("YES" if len(set(L)) < 1 << N else "NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR STRING STRING |
You are given a sequence of $n$ integers $a_1, \, a_2, \, \dots, \, a_n$.
Does there exist a sequence of $n$ integers $b_1, \, b_2, \, \dots, \, b_n$ such that the following property holds?
For each $1 \le i \le n$, there exist two (not necessarily distinct) indices $j$ and $k$ ($1 \le j, \, k \le n$) such that $a_i = b_j - b_k$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 20$) β 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 10$).
The second line of each test case contains the $n$ integers $a_1, \, \dots, \, a_n$ ($-10^5 \le a_i \le 10^5$).
-----Output-----
For each test case, output a line containing YES if a sequence $b_1, \, \dots, \, b_n$ satisfying the required property exists, and NO otherwise.
-----Examples-----
Input
5
5
4 -7 -1 5 10
1
0
3
1 10 100
4
-3 2 10 2
9
25 -171 250 174 152 242 100 -205 -258
Output
YES
YES
NO
YES
YES
-----Note-----
In the first test case, the sequence $b = [-9, \, 2, \, 1, \, 3, \, -2]$ satisfies the property. Indeed, the following holds:
$a_1 = 4 = 2 - (-2) = b_2 - b_5$;
$a_2 = -7 = -9 - (-2) = b_1 - b_5$;
$a_3 = -1 = 1 - 2 = b_3 - b_2$;
$a_4 = 5 = 3 - (-2) = b_4 - b_5$;
$a_5 = 10 = 1 - (-9) = b_3 - b_1$.
In the second test case, it is sufficient to choose $b = [0]$, since $a_1 = 0 = 0 - 0 = b_1 - b_1$.
In the third test case, it is possible to show that no sequence $b$ of length $3$ satisfies the property. | import sys
input = sys.stdin.readline
def rec(i, w, s, a, n):
if i == n:
for i in range(n):
if w[i] == 0 and a[i] == s:
return True
return False
w[i] = 1
if rec(i + 1, w, s + a[i], a, n):
return True
w[i] = 0
if rec(i + 1, w, s, a, n):
return True
w[i] = -1
if rec(i + 1, w, s - a[i], a, n):
return True
return False
def solve():
n = int(input())
a = list(map(int, input().split()))
w = [0] * n
if rec(0, w, 0, a, n):
print("YES")
else:
print("NO")
for i in range(int(input())):
solve() | IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF FUNC_CALL VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.