description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | from itertools import dropwhile
n = [int(i) for i in input()]
l = len(n)
if len(n) == 1:
print(-1)
exit()
if n in [[2, 5], [7, 5], [5, 0]]:
print(0)
exit()
if n in [[5, 2], [5, 7]]:
print(1)
exit()
inf = 10**12
def calc1(x, y):
global n, l, inf
m = n[::-1]
if x not in n or y not in n:
return inf
ix, iy = m.index(x), m.index(y)
if max(ix, iy) < l - 1:
if iy < ix:
return iy + (ix - 1)
else:
return iy + (ix - 1) + 1
else:
ret = 0
if ix == l - 1:
for i in range(l - 2, -1, -1):
if m[i] != 0 and i != iy:
break
else:
return inf
if i < iy:
iy -= 1
ret += l - 1 - i
ix -= 1
ret += iy + (ix - 1)
return ret
else:
ret += iy
ret += l - 1 - i
ix -= 1
ret += ix - 1
return ret
else:
for i in range(l - 2, -1, -1):
if m[i] != 0 and i != ix:
break
else:
return inf
if i < ix:
iy -= 1
ret += l - 1 - i
ix -= 1
ret += iy + (ix - 1) + 1
return ret
else:
ret += iy
ret += l - 1 - i
ix -= 1
ret += ix - 1 + 1
return ret
def calc2(x, y):
global n, l, inf
if n.count(0) < 2:
return inf
m = [(l - 1 - i) for i in range(l) if n[i] == 0][::-1]
return m[0] + m[1] - 1
ans = min([calc1(2, 5), calc1(7, 5), calc1(5, 0), calc2(0, 0)])
print(-1 if ans == inf else ans) | ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR RETURN BIN_OP VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR RETURN VAR IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR RETURN VAR IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF IF FUNC_CALL VAR NUMBER NUMBER RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR LIST FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | l = [int(e) for e in input().strip()]
def lei00(l):
result = []
for i in range(len(l) - 1, -1, -1):
if l[i] == 0:
result.append(i)
if len(result) == 2:
break
if len(result) < 2:
return None
return 2 * len(l) - result[0] - result[1] - 3
def lei(l, x, y):
assert x != y
ix = None
iy = None
for i in range(len(l) - 1, -1, -1):
if l[i] == x and ix == None:
ix = i
elif l[i] == y and iy == None:
iy = i
if ix != None and iy != None:
break
if ix == None or iy == None:
return None
result = 2 * len(l) - ix - iy - 3
if ix > iy:
result += 1
if x == 7 or x == 2:
assert y == 5
if iy == 0 and l[1] == 0:
if len(l) >= 4:
iii = 1
while True:
iii += 1
if l[iii] != 0:
break
result += iii - 1
if len(l) == 3:
return None
return result
l = [lei00(l), lei(l, 5, 0), lei(l, 7, 5), lei(l, 2, 5)]
result = None
for e in l:
if e == None:
continue
if result == None or result > e:
result = e
if result == None:
print(-1)
else:
print(result) | ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NONE RETURN BIN_OP BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER NUMBER FUNC_DEF VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR NONE ASSIGN VAR VAR IF VAR VAR VAR VAR NONE ASSIGN VAR VAR IF VAR NONE VAR NONE IF VAR NONE VAR NONE RETURN NONE ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NONE RETURN VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NONE FOR VAR VAR IF VAR NONE IF VAR NONE VAR VAR ASSIGN VAR VAR IF VAR NONE EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | s = list(input())
n = len(s)
if n == 1:
print(-1)
exit(0)
ans = 2000000000.0
for i in range(n):
for j in range(i + 1, n):
s1 = s[:]
cnt = 0
i1, j1 = i + 1, j + 1
while j1 < n:
s1[j1 - 1], s1[j1] = s1[j1], s1[j1 - 1]
j1 += 1
cnt += 1
while i1 < n - 1:
s1[i1 - 1], s1[i1] = s1[i1], s1[i1 - 1]
i1 += 1
cnt += 1
k = 0
while k < n and s1[k] == "0":
k += 1
if int("".join(s1)) % 25 == 0:
ans = min(ans, cnt + k)
s1[n - 1], s1[n - 2] = s1[n - 2], s1[n - 1]
if int("".join(s1)) % 25 == 0:
ans = min(ans, cnt + k + 1)
if ans == 2000000000.0:
print(-1)
else:
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER IF BIN_OP FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | a = input()[::-1]
d = ["52", "05", "57"]
if len(a) < 2:
print(-1)
elif a[:2] in d:
print(0)
else:
ans = 99999999
if len([i for i in a if i == "0"]) >= 2:
x, y = -1, -1
for i in range(len(a)):
if a[i] == "0":
if x < 0:
x = i
elif y < 0:
y = i
else:
break
ans = x + y - 1
for p in d:
x, y = -1, -1
for i in range(len(a)):
if a[i] == p[0] and x < 0:
x = i
if a[i] == p[1] and y < 0:
y = i
if x < 0 or y < 0:
continue
c = 0
if x > y:
c += 1
x, y = y, x
if y == len(a) - 1 and a[-2] == "0" and x != len(a) - 2:
z = -1
for i in range(len(a) - 1):
if a[i] != "0" and i != x:
z = i
if z > 0:
c += len(a) - 2 - z
c += x + y - 1
ans = min(ans, c)
if ans == 99999999:
ans = -1
print(ans) | ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST STRING STRING STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR STRING NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | s = input()
globans = 10**9
for i_ in range(len(s)):
for j_ in range(len(s)):
i = i_
j = j_
if j == i:
continue
if i == len(s) - 1:
ans = abs(len(s) - j - 1)
else:
ans = abs(len(s) - i - 2) + abs(len(s) - j - 1) + int(j < i)
if i > j:
i, j = j, i
nw = s[:i] + s[i + 1 : j] + s[j + 1 :] + s[i_] + s[j_]
for x in range(len(s)):
if nw[x] == "0":
continue
ans += x
nw = nw[x] + nw[:x] + nw[x + 1 :]
break
if int(nw) % 25 == 0:
globans = min(globans, ans)
if globans == 10**9:
print(-1)
else:
print(globans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | n = list(input())[::-1]
INF = 10**9
if "".join(n) in ["52", "05", "57"]:
print(0)
exit(0)
if "".join(n) in ["25", "75"]:
print(1)
exit(0)
def solve(a, b):
if a not in n or b not in n:
return INF
nn = n[:]
ia = nn.index(a)
nn = [a] + nn[:ia] + nn[ia + 1 :]
ib = nn.index(b)
nn = [a, b] + nn[1:ib] + nn[ib + 1 :]
cnt = 0
for i in range(len(n) - 1, 1, -1):
if nn[i] != "0":
break
cnt += 1
else:
return INF
return ia + ib + cnt - 1
if n.count("0") >= 2:
ans = -1
one = False
for i in range(len(n)):
if n[i] == "0":
ans += i
if one:
break
one = True
else:
ans = INF
ans = min(ans, solve("5", "2"))
ans = min(ans, solve("0", "5"))
ans = min(ans, solve("5", "7"))
if ans == INF:
print(-1)
else:
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF FUNC_CALL STRING VAR LIST STRING STRING STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL STRING VAR LIST STRING STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER RETURN VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING STRING IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | s = input()
if int(s) % 25 == 0:
print(0)
else:
sols = []
d = {}
n = list(s)
nr = s[::-1]
for c in ("0", "2", "5", "7"):
p = nr.find(c)
d[c] = p
for c1, c2 in (("2", "5"), ("5", "0"), ("7", "5")):
if c1 not in n or c2 not in n:
continue
dc1 = d[c1]
dc2 = d[c2]
sol = 0
if dc1 == len(n) - 1 and len(n) > 1 and n[1] == "0":
i = 1
while i < len(n) and (n[i] == "0" or dc2 == i):
i += 1
if i != len(n):
i -= 1
sol = i
dc1 -= 1
if dc2 > len(n) - i - 1:
dc2 -= 1
if dc2 == len(n) - 1 and len(n) > 1 and n[1] == "0":
i = 1
while i < len(n) and (n[i] == "0" or dc1 == i):
i += 1
if i != len(n):
sol = i
dc2 -= 1
if dc1 > len(n) - i - 1:
dc1 -= 1
if dc1 == 0:
sols.append(dc2 + sol)
continue
if dc1 < dc2:
sols.append(dc2 + dc1 + sol)
else:
sols.append(dc2 + dc1 - 1 + sol)
z1 = nr.find("0")
if z1 != -1:
z2 = nr.find("0", z1 + 1)
if z2 != -1:
sols.append(z1 + z2 - 1)
if len(sols) == 0:
print(-1)
else:
print(min(sols)) | ASSIGN VAR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR STRING STRING STRING STRING STRING STRING IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | a = input()
k = 0
if a.rfind("5") == 0 and a[1] == "0" and len(a) > 2:
a = a[0] + a[2] + a[1] + a[3:]
k = a.count("0")
if a.count("0") >= 2:
a00 = -1 + len(a) - 1 - a.rfind("0") + (len(a) - 1 - a.rfind("0", 0, a.rfind("0")))
else:
a00 = 1000
if a.rfind("2") > a.rfind("5") and a.rfind("2") != -1 and a.rfind("5") != -1:
a25 = len(a) - 1 - a.rfind("2") + (len(a) - 1 - a.rfind("5", 0, a.rfind("2")))
elif a.rfind("2") != -1 and a.rfind("5") != -1:
a25 = len(a) - 2 - a.rfind("5") + (len(a) - 1 - a.rfind("2", 0, a.rfind("5")))
else:
a25 = 1000
if a.rfind("5") > a.rfind("0") and a.rfind("0") != -1 and a.rfind("5") != -1:
a50 = len(a) - 1 - a.rfind("5") + (len(a) - 1 - a.rfind("0", 0, a.rfind("5")))
elif a.rfind("0") != -1 and a.rfind("5") != -1:
a50 = len(a) - 2 - a.rfind("0") + (len(a) - 1 - a.rfind("5", 0, a.rfind("0")))
else:
a50 = 1000
if a.rfind("7") > a.rfind("5") and a.rfind("7") != -1 and a.rfind("5") != -1:
a75 = len(a) - 1 - a.rfind("7") + (len(a) - 1 - a.rfind("5", 0, a.rfind("7")))
elif a.rfind("7") != -1 and a.rfind("5") != -1:
a75 = len(a) - 2 - a.rfind("5") + (len(a) - 1 - a.rfind("7", 0, a.rfind("5")))
else:
a75 = 1000
if min(a00, a25, a50, a75) == 1000:
print(-1)
else:
print(min(a00, a25, a50, a75) + k) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR STRING NUMBER VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING IF FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING IF FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING IF FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | n = list(input())[::-1]
INF = 10**9
def solve(b, a):
a, b = str(a), str(b)
if a not in n or b not in n:
return INF
nn = n[:]
ia = nn.index(a)
nn = [a] + nn[:ia] + nn[ia + 1 :]
ib = nn.index(b)
nn = [a, b] + nn[1:ib] + nn[ib + 1 :]
if len(n) == 2:
return ia + ib - 1
cnt = 0
for i in range(len(n) - 1, 1, -1):
if nn[i] != "0":
break
cnt += 1
else:
return INF
return ia + ib + cnt - 1
if n.count("0") >= 2:
ans = -1
cnt = 0
for i in range(len(n)):
if n[i] == "0":
ans += i
cnt += 1
if cnt == 2:
break
else:
ans = INF
ans = min(ans, solve(2, 5), solve(5, 0), solve(7, 5))
if ans == INF:
print(-1)
else:
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER RETURN VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | s = input()
arr = []
for i in s:
arr.append(i)
ans = 100000000000
for i in range(len(s)):
for j in range(len(s)):
if i == j:
continue
t = arr[:]
curr = 0
for k in range(i, len(s) - 1):
tt = t[k]
t[k] = t[k + 1]
t[k + 1] = tt
curr += 1
tey = 0
if j > i:
tey += 1
for k in range(j - tey, len(s) - 2, 1):
tt = t[k]
t[k] = t[k + 1]
t[k + 1] = tt
curr += 1
pos = -1
for k in range(len(s)):
if t[k] != "0":
pos = k
break
for k in range(pos, 0, -1):
tt = t[k]
t[k] = t[k - 1]
t[k - 1] = tt
curr += 1
if int("".join(t)) % 25 == 0:
ans = min(ans, curr)
if ans == 100000000000:
ans = -1
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | import sys
k = input()
n = len(k)
Min = sys.maxsize
ck = ["50", "25", "75"]
for c in ck:
if c[0] in k and c[1] in k:
temp = 2 * n - 3 - k.rfind(c[0]) - k.rfind(c[1])
if c[1] == "5":
if k.rfind("5") == 0 and k[1] == "0" and n > 3:
add = 0
i = 1
while k[i] == "0":
add += 1
i += 1
temp += add
temp += k.rfind(c[0]) > k.rfind(c[1])
Min = min(temp, Min)
if k.count("0") > 1:
css = k.rfind("0")
temp = n - 1 - css
k = k[:css] + k[css + 1 :]
temp += len(k) - 1 - k.rfind("0")
Min = min(temp, Min)
print([Min, -1][Min == sys.maxsize]) | IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST STRING STRING STRING FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER STRING IF FUNC_CALL VAR STRING NUMBER VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | def is_valid(a):
i = len(a) - 1
cnt = 0
while i >= 0 and a[i] == 0:
cnt += 1
i -= 1
if i == -1 and cnt > 0:
return False, cnt
return True, cnt
def create(a):
b = [x for x in a]
return b
def solve(d, a):
min_ = float("inf")
for x, y in [[2, 5], [7, 5], [5, 0]]:
if x in d and y in d:
posX = d[x][0]
posY = d[y][0]
b = create(a)
b.remove(x)
b.remove(y)
num = posX + posY
if posY < posX:
num -= 1
flg, cnt = is_valid(b)
if flg == True:
min_ = min(min_, num + cnt)
if 0 in d and len(d[0]) >= 2:
min_ = min(min_, d[0][0] + d[0][1] - 1)
if min_ < float("inf"):
return min_
return -1
temp = int(input())
a = []
d = {}
while temp > 0:
a.append(temp % 10)
temp //= 10
for i, x in enumerate(a):
if x not in d:
d[x] = []
d[x].append(i)
print(solve(d, a)) | FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER VAR RETURN NUMBER VAR FUNC_DEF ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER IF VAR FUNC_CALL VAR STRING RETURN VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | def solve(n):
if int(n) % 25 == 0:
return 0
if n in {"52", "57"}:
return 1
ans = float("inf")
for x, y in ["00", "25", "50", "75"]:
for i in range(len(n)):
if n[i] == "0":
continue
for j in range(len(n)):
if i == j or n[j] != x:
continue
for k in range(len(n)):
if k in {i, j} or n[k] != y:
continue
ans = min(
ans, len(n) * 2 - 3 + i - j - k + (j > k) - (i > j) - (i > k)
)
return ans if ans < float("inf") else -1
print(solve(input())) | FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER IF VAR STRING STRING RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR LIST STRING STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | y = input()
fives = y.count("5")
zeroes = y.count("0")
twos = y.count("2")
sevens = y.count("7")
if fives == 0:
if zeroes < 2:
print(-1)
exit(0)
elif zeroes == 0 and twos == 0 and sevens == 0:
print(-1)
exit(0)
best = 5 * len(y)
y = y[::-1]
for xx in [["5", "2"], ["0", "0"], ["5", "7"], ["0", "5"]]:
z = y
count = 0
for x in xx:
if not x in z:
count = 5 * len(y)
break
f = z.index(x)
z = z[:f] + z[f + 1 :]
count += f
if len(z) > 2 and z[-1] == "0":
valid = False
for x in z[::-1]:
if x == "0":
count += 1
continue
else:
valid = True
break
if not valid:
count += 5 * len(y)
if count < best:
best = count
print(best) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR LIST LIST STRING STRING LIST STRING STRING LIST STRING STRING LIST STRING STRING ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | num = str(input())
n = len(num)
num_r = num[::-1]
s0, s5 = num_r.find("0"), num_r.find("5")
if s0 != -1:
newnum_r = num_r[:s0] + num_r[s0 + 1 :]
c = []
for a in ["0", "5"]:
if newnum_r.find(a) != -1:
c.append(newnum_r.find(a))
if c != []:
s = min(c)
s0 = s0 + s
else:
s0 = 999999999
else:
s0 = 999999999
if s5 != n - 1 or num.find("0") != 1:
if s5 != -1:
newnum_r = num_r[:s5] + num_r[s5 + 1 :]
c = []
for a in ["2", "7"]:
if newnum_r.find(a) != -1:
c.append(newnum_r.find(a))
if c != []:
s = min(c)
s5 = s5 + s
else:
s5 = 999999999
else:
s5 = 999999999
ssr = min(s0, s5)
if ssr != 999999999:
print(ssr)
else:
print(-1)
else:
if s5 != -1:
newnum_r = num_r[:s5] + num_r[s5 + 1 :]
c = []
for a in ["2", "7"]:
if newnum_r.find(a) != -1:
c.append(newnum_r.find(a))
if c != []:
s = min(c)
sk = 0
for i in range(1, n - 1):
if num[i] == "0":
sk = sk + 1
else:
break
s5 = s5 + s + sk
else:
s5 = 999999999
else:
s5 = 999999999
ssr = min(s0, s5)
if ssr != 999999999:
print(ssr)
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR LIST STRING STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR LIST STRING STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR LIST STRING STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | def idx(s, c, start=0):
try:
return s[::-1].index(c, start)
except:
return -1
def main():
s = input()
zero_cnt = 0
while zero_cnt < len(s) - 1 and s[zero_cnt + 1] == "0":
zero_cnt += 1
i01 = idx(s, "0")
i02 = idx(s, "0", i01 + 1)
i2 = idx(s, "2")
i5 = idx(s, "5")
i7 = idx(s, "7")
r = 666
if i01 != -1 and i02 != -1:
r = min(r, i01 + (i02 - 1))
if i2 != -1 and i5 != -1:
r = min(
r,
i2
+ i5
- int(i2 > i5)
+ int((i2 == len(s) - 1 or i5 == len(s) - 1) and zero_cnt),
)
if i5 != -1 and i01 != -1:
r = min(r, i5 + i01 - int(i5 > i01))
if i7 != -1 and i5 != -1:
r = min(
r,
i7
+ i5
- int(i7 > i5)
+ int((i7 == len(s) - 1 or i5 == len(s) - 1) and zero_cnt),
)
print(r if r != 666 else -1)
main() | FUNC_DEF NUMBER RETURN FUNC_CALL VAR NUMBER VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | n = input()
if "2" not in n or "5" not in n:
a = 10**18
else:
a = 0
i = len(n) - 1
while n[i] != "5":
i -= 1
a += 1
updated_n = n[0:i] + n[i + 1 :]
i = len(n) - 2
while updated_n[i] != "2":
i -= 1
a += 1
updated_n = updated_n[0:i] + updated_n[i + 1 :]
for i in range(len(updated_n)):
if updated_n[i] != "0":
break
a += i
if "7" not in n or "5" not in n:
b = 10**18
else:
b = 0
i = len(n) - 1
while n[i] != "5":
i -= 1
b += 1
updated_n = n[0:i] + n[i + 1 :]
i = len(n) - 2
while updated_n[i] != "7":
i -= 1
b += 1
updated_n = updated_n[0:i] + updated_n[i + 1 :]
for i in range(len(updated_n)):
if updated_n[i] != "0":
break
b += i
if "5" not in n or "0" not in n:
c = 10**18
else:
c = 0
i = len(n) - 1
while n[i] != "0":
i -= 1
c += 1
updated_n = n[0:i] + n[i + 1 :]
i = len(n) - 2
while updated_n[i] != "5":
i -= 1
c += 1
updated_n = updated_n[0:i] + updated_n[i + 1 :]
for i in range(len(updated_n)):
if updated_n[i] != "0":
break
c += i
if n.count("0") < 2:
d = 10**18
else:
d = 0
i = len(n) - 1
while n[i] != "0":
i -= 1
d += 1
updated_n = n[0:i] + n[i + 1 :]
i = len(n) - 2
while updated_n[i] != "0":
i -= 1
d += 1
updated_n = updated_n[0:i] + updated_n[i + 1 :]
for i in range(len(updated_n)):
if updated_n[i] != "0":
break
d += i
ans = min(a, b, c, d)
if ans == 10**18:
print(-1)
else:
print(ans) | ASSIGN VAR FUNC_CALL VAR IF STRING VAR STRING VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR IF STRING VAR STRING VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR IF STRING VAR STRING VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR IF FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | n = input()
n = list(n)
if not "5" in n and not "0" in n and not "7" in n and not "2" in n:
print(-1)
quit()
wkn = []
wkn[:] = n
ans = -1
for i in reversed(list(range(len(n)))):
if n[i] == "0":
wk1 = n[:i] + n[i + 1 :] + [n[i]]
ans = len(n) - i - 1
if wk1[0] != "0":
n[:] = wk1
break
else:
count = 0
f1 = True
for j in wk1:
count += 1
if j != "0":
f1 = False
break
if f1:
ans = -1
break
ans += count - 1
wk1 = [wk1[count - 1]] + wk1[: count - 1] + wk1[count:]
n[:] = wk1
break
if ans != -1:
f = True
for i in reversed(list(range(len(n) - 1))):
if n[i] == "0" or n[i] == "5":
wk1 = n[:i] + n[i + 1 : -1] + [n[i]]
ans += len(n) - i - 2
if wk1[0] != "0":
f = False
break
else:
count = 0
f1 = True
for j in wk1:
count += 1
if j != "0":
f1 = False
break
if f1:
ans = -1
break
ans += count - 1
f = False
break
if f:
ans = -1
wkans = ans
ans = -1
n = wkn
for i in reversed(list(range(len(n)))):
if n[i] == "5":
wk1 = n[:i] + n[i + 1 :] + [n[i]]
ans = len(n) - i - 1
if wk1[0] != "0":
n[:] = wk1
break
else:
count = 0
f1 = True
for j in wk1:
count += 1
if j != "0":
f1 = False
break
if f1:
ans = -1
break
ans += count - 1
wk1 = [wk1[count - 1]] + wk1[: count - 1] + wk1[count:]
n[:] = wk1
break
if ans != -1:
f = True
for i in reversed(list(range(len(n) - 1))):
if n[i] == "7" or n[i] == "2":
wk1 = n[:i] + n[i + 1 : -1] + [n[i]]
ans += len(n) - i - 2
if wk1[0] != "0":
f = False
break
else:
count = 0
f1 = True
for j in wk1:
count += 1
if j != "0":
f1 = False
break
if f1:
ans = -1
break
ans += count - 1
f = False
break
if f:
ans = -1
if wkans == -1:
print(ans)
quit()
if ans == -1:
print(wkans)
quit()
print(min(ans, wkans)) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF STRING VAR STRING VAR STRING VAR STRING VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER LIST VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER LIST VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER LIST VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER LIST VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | import sys
def sol(a, s):
s1 = s[::-1]
try:
ai = s1.index(a)
if a == "0":
s2 = a + s1[0:ai] + s1[ai + 1 :]
return [True, ai, s2[::-1]]
except:
return [False]
x = len(s) - 2
while x > -1 and s1[x] == "0" and ai == len(s) - 1 and len(s) != 1:
x -= 1
s = a + s1[0:ai] + s1[ai + 1 :]
if x == -1 and len(s) != 1:
return [False]
if s1[x + 1] == "0":
s = s[0 : x + 1] + s[x + 2 :] + s1[x]
return [True, ai + len(s) - 2 - x, s[::-1]]
s = input()
ans = 10**6
x = sol("0", s)
if x[0]:
y = sol("0", x[2][0:-1])
if y[0] and int(y[2] + "0") % 25 == 0:
ans = min(x[1] + y[1], ans)
x = sol("0", s)
if x[0]:
y = sol("5", x[2][0:-1])
if y[0] and int(y[2] + "0") % 25 == 0:
ans = min(x[1] + y[1], ans)
x = sol("5", s)
if x[0]:
y = sol("7", x[2][0:-1])
if y[0] and int(y[2] + "5") % 25 == 0:
ans = min(x[1] + y[1], ans)
x = sol("5", s)
if x[0]:
y = sol("2", x[2][0:-1])
if y[0] and int(y[2] + "5") % 25 == 0:
ans = min(x[1] + y[1], ans)
if s == "0":
print(s)
elif ans == 10**6:
print(-1)
else:
print(ans) | IMPORT FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN LIST NUMBER VAR VAR NUMBER RETURN LIST NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR VAR STRING VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN LIST NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN LIST NUMBER BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR NUMBER NUMBER NUMBER IF VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR NUMBER NUMBER NUMBER IF VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR NUMBER NUMBER NUMBER IF VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR NUMBER NUMBER NUMBER IF VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | s = input()[::-1]
ans = 10**9
mp = {"0": "05", "5": "27"}
for i in range(len(s)):
if s[i] not in mp:
continue
rem = s[:i] + s[i + 1 :]
for j in range(len(rem)):
if rem[j] not in mp[s[i]]:
continue
rem2 = rem[:j] + rem[j + 1 :]
if rem2:
lastnz = -1
while lastnz >= -len(rem2) and rem2[lastnz] == "0":
lastnz -= 1
if lastnz < -len(rem2):
continue
lastnz = -lastnz - 1
else:
lastnz = 0
ans = min(ans, i + j + lastnz)
print(-1 if ans == 10**9 else ans) | ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR DICT STRING STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER NUMBER VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | def swap(ar, ind1, ind2):
if ind2 > ind1:
for i in range(ind1, ind2):
temp = ar[i]
ar[i] = ar[i + 1]
ar[i + 1] = temp
else:
for i in range(ind1, ind2, -1):
temp = ar[i]
ar[i] = ar[i - 1]
ar[i - 1] = temp
n = list(input())
n2 = len(n)
ans = 100
for i in range(n2):
for j in range(n2):
nc = [0] * n2
for i2 in range(n2):
nc[i2] = n[i2]
swap(nc, i, n2 - 1)
swap(nc, j, n2 - 2)
num = 0
for z in range(n2):
if nc[z] != "0":
num = z
break
swap(nc, 0, num)
if int("".join(nc)) % 25 == 0:
ans = min(ans, abs(n2 - 2 - j) + abs(n2 - 1 - i) + num)
if ans == 100:
print(-1)
else:
print(ans) | FUNC_DEF IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | n = [int(x) for x in input()]
var = [[0, 0], [5, 2], [5, 7], [0, 5]]
res = 1000
ln = len(n)
for c in var:
num = n[:]
i1 = -1
i2 = -1
i0 = -1
for i in range(ln - 1, -1, -1):
if num[i] == c[0]:
i1 = i
break
if i1 == -1:
continue
tres = 0
idx = i1 + 1
while i1 != ln - 1:
num[idx], num[i1] = num[i1], num[idx]
idx = idx + 1
i1 = i1 + 1
tres = tres + 1
idx = i1 + 1
for i in range(ln - 1, -1, -1):
if i != i1 and num[i] == c[1]:
i2 = i
break
if i2 == -1:
continue
idx = i2 + 1
if idx == ln:
tres = tres + 1
num[i2 - 1], num[i2] = num[i2], num[i2 - 1]
else:
while i2 != ln - 2:
num[idx], num[i2] = num[i2], num[idx]
idx = idx + 1
i2 = i2 + 1
tres = tres + 1
for i in range(0, ln - 1):
if num[i] != 0:
i0 = i
break
if i0 == -1:
continue
idx = i0 - 1
while i0 != 0:
num[idx], num[i0] = num[i0], num[idx]
idx = idx - 1
i0 = i0 - 1
tres = tres + 1
res = min(res, tres)
if res < 100:
print(res)
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | import sys
n = int(input())
if n % 25 == 0:
print(0)
else:
tmp = n
digits = [0] * 10
while tmp:
digits[tmp % 10] += 1
tmp //= 10
s = str(n)
length = len(s)
leading_zero_inc = 0
for i in range(1, length):
if s[i] != "0":
leading_zero_inc = i
break
ans = sys.maxsize
if digits[0] >= 2:
i = s.rfind("0")
j = s.rfind("0", 0, i)
i = length - 1 - i
j = length - 2 - j
ans = min(ans, i + j)
if digits[2] >= 1 and digits[5] >= 1:
i = s.rfind("2")
j = s.rfind("5")
inc = False
if j < i:
inc = True
if i == 0:
i = length - 3 + leading_zero_inc
else:
i = length - 2 - i
if j == 0:
j = length - 2 + leading_zero_inc
else:
j = length - 1 - j
ans = min(ans, i + j + inc)
if digits[5] >= 1 and digits[0] >= 1:
i = s.rfind("5")
j = s.rfind("0")
inc = False
if j < i:
inc = True
if i == 0 and j != 1:
i = length - 3 + leading_zero_inc
else:
i = length - 2 - i
j = length - 1 - j
ans = min(ans, i + j + inc)
if digits[7] >= 1 and digits[5] >= 1:
i = s.rfind("7")
j = s.rfind("5")
inc = False
if j < i:
inc = True
if i == 0:
i = length - 3 + leading_zero_inc
else:
i = length - 2 - i
if j == 0:
j = length - 2 + leading_zero_inc
else:
j = length - 1 - j
ans = min(ans, i + j + inc)
if ans == sys.maxsize:
ans = -1
print(ans) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER WHILE VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | s = input()[::-1]
t = len(s)
def g(c, i=0):
return s.find(str(c), i) + 1 or 50
i, f = g(0), g(5)
j = min(g(0, i), f)
m = i + j + (j < i)
j = min(g(2), g(7))
l = f + j + (j < f)
if f == t:
i = t - 1
while s[i - 1] == "0":
l += 1
i -= 1
m = min(m, l)
print((-1, m - 3)[m < 40]) | ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | def digits(n):
return len(str(n))
def digit(n, i):
if i < digits(n):
return int(str(n)[digits(n) - i - 1])
return 0
def swap_digits(n, i, j):
return n + digit(n, i) * (10**j - 10**i) + digit(n, j) * (10**i - 10**j)
n = int(input())
if n % 25 == 0:
print(0)
exit()
ans = []
for i in range(digits(n)):
for j in range(digits(n)):
if i == j:
continue
x, y, cur, cur_ans = i, j, n, 0
if y > x:
x += 1
while y > 0:
cur = swap_digits(cur, y, y - 1)
y -= 1
cur_ans += 1
while x > 1:
cur = swap_digits(cur, x, x - 1)
x -= 1
cur_ans += 1
for k in range(digits(n), -1, -1):
if digit(cur, k) != 0:
cur_ans += digits(n) - 1 - k
break
if cur % 25 == 0:
ans.append(cur_ans)
print(min(ans) if len(ans) != 0 else -1) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | n = int(input())
l = list(str(n))[::-1]
g = []
def verif(l):
if l[-1] != "0":
return 0
else:
return 1 + verif(l[:-1])
def permut(l, c, c1):
i = l.copy()
k = 0
while True:
if i.index(c) == 0:
break
else:
j = i.index(c)
i[j], i[j - 1] = i[j - 1], i[j]
k += 1
m = i[1:]
while True:
if m.index(c1) == 0:
break
else:
j = m.index(c1)
m[j], m[j - 1] = m[j - 1], m[j]
k += 1
k += verif(i)
return k
if "2" in l and "5" in l:
g.append(permut(l, "5", "2"))
if "5" in l and "0" in l:
g.append(permut(l, "0", "5"))
if "7" in l and "5" in l:
g.append(permut(l, "5", "7"))
if l.count("0") > 1:
g.append(permut(l, "0", "0"))
if g == []:
print(-1)
else:
print(min(g)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER STRING RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR IF STRING VAR STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING STRING IF STRING VAR STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING STRING IF STRING VAR STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING STRING IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING STRING IF VAR LIST EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | A_str = input()
A = list(A_str)
B = [["2", "5"], ["5", "0"], ["7", "5"]]
result = 10**9
if int(A_str) % 100 == 0:
print(0)
else:
for a, b in B:
if a not in A or b not in A:
continue
cresult = 0
tmp_str = A_str[:]
i = tmp_str.rfind(b)
cresult += len(tmp_str) - 1 - i
tmp_str = "".join(A[:i] + A[i + 1 :] + [A[i]])
i = tmp_str.rfind(a)
cresult += len(tmp_str) - 2 - i
tmp_str = list(tmp_str)
tmp_str = "".join(
tmp_str[:i] + tmp_str[i + 1 : -1] + [tmp_str[i]] + [tmp_str[-1]]
)
for i in range(len(tmp_str)):
if tmp_str[i] != "0":
break
result = min([result, cresult + i])
if A.count("0") > 1:
cresult = 0
i = A_str.rfind("0")
cresult += len(A_str) - 1 - i
A_str = A_str[:i] + A_str[i + 1 :] + A[i]
i = A_str[:-1].rfind("0")
cresult += len(A_str) - 2 - i
result = min([result, cresult])
if result < 10**9:
print(result)
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST STRING STRING LIST STRING STRING LIST STRING STRING ASSIGN VAR BIN_OP NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL STRING BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER LIST VAR VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR IF FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER STRING VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | def swipe(s, i):
return s[:i] + s[i + 1] + s[i] + (s[i + 2 :] if i + 2 < len(s) else "")
def nazis(s):
m = float("inf")
for i in range(9):
if s.find(str(i + 1)) > -1:
m = min(s.find(str(i + 1)), m)
return m
n = input()
c = float("inf")
if (
n.count("0") >= 2
or "5" in n
and "0" in n
or "2" in n
and "5" in n
or "7" in n
and "5" in n
):
s = n
if n.count("0") >= 2:
a = n.rfind("0")
b = n.rfind("0", 0, a)
c = 2 * len(n) - a - b - 3
if "5" in n and "0" in n:
cc = 0
a = n.rfind("0")
while n[-1] != "0":
n = swipe(n, a)
a += 1
cc += 1
b = n.rfind("5")
while n[-2] != "5":
n = swipe(n, b)
b += 1
cc += 1
if n[0] == "0":
cc += nazis(n)
c = min(c, cc)
if "2" in n and "5" in n:
n = s
cc = 0
b = n.rfind("5")
while n[-1] != "5":
n = swipe(n, b)
b += 1
cc += 1
a = n.rfind("2")
while n[-2] != "2":
n = swipe(n, a)
a += 1
cc += 1
if n[0] == "0":
cc += nazis(n)
c = min(c, cc)
if "7" in n and "5" in n:
n = s
cc = 0
b = n.rfind("5")
while n[-1] != "5":
n = swipe(n, b)
b += 1
cc += 1
a = n.rfind("7")
while n[-2] != "7":
n = swipe(n, a)
a += 1
cc += 1
if n[0] == "0":
cc += nazis(n)
c = min(c, cc)
print(c)
else:
print(-1) | FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR STRING NUMBER STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR ASSIGN VAR VAR IF FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER IF STRING VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER STRING VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF STRING VAR STRING VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER STRING VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF STRING VAR STRING VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER STRING VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | def permutate(s):
answer = 1000
n = len(s)
z = 0
if s.count("2") >= 1 and s.count("5") >= 1:
k1 = s.rfind("2")
k2 = s.rfind("5")
s1 = s[:k1] + s[k1 + 1 :]
m = s1.rfind("5")
s2 = s1[:m] + s1[m + 1 :]
if len(s2) > 0 and s2[0] == "0":
for i in range(1, len(s2)):
if s2[i] != "0":
z = i
break
if k2 < k1:
answer = min(answer, 2 * n - k2 - k1 - 2 + z)
elif k2 > k1:
answer = min(answer, 2 * n - k2 - k1 - 3 + z)
z = 0
if s.count("5") >= 1 and s.count("0") >= 1:
k3 = s.rfind("5")
k4 = s.rfind("0")
s1 = s[:k3] + s[k3 + 1 :]
m = s1.rfind("0")
s2 = s1[:m] + s1[m + 1 :]
if len(s2) > 0 and s2[0] == "0":
for i in range(1, len(s2)):
if s2[i] != "0":
z = i
break
if k4 < k3:
answer = min(answer, 2 * n - k4 - k3 - 2 + z)
elif k4 > k3:
answer = min(answer, 2 * n - k4 - k3 - 3 + z)
z = 0
if s.count("7") >= 1 and s.count("5") >= 1:
k5 = s.rfind("7")
k6 = s.rfind("5")
s1 = s[:k5] + s[k5 + 1 :]
m = s1.rfind("5")
s2 = s1[:m] + s1[m + 1 :]
if len(s2) > 0 and s2[0] == "0":
for i in range(1, len(s2)):
if s2[i] != "0":
z = i
break
if k6 < k5:
answer = min(answer, 2 * n - k5 - k6 - 2 + z)
elif k6 > k5:
answer = min(answer, 2 * n - k6 - k5 - 3 + z)
z = 0
if s.count("0") > 1:
k7 = s.rfind("0")
k8 = s[: s.rfind("0")].rfind("0")
s1 = s[:k7] + s[k7 + 1 :]
m = s1.rfind("0")
s2 = s1[:m] + s1[m + 1 :]
if len(s2) > 0 and s2[0] == "0":
for i in range(1, len(s2)):
if s2[i] != "0":
z = i
break
answer = min(answer, 2 * n - 3 - k7 - k8 + z)
if answer == 1000:
return -1
return answer
l = input()
print(permutate(l)) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING STRING ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | from sys import stdin
def f(n, x, y):
ans = 0
cur = n.rfind(y)
if cur < 0:
return 10**18
ans += len(n) - 1 - cur
n = n[:cur] + n[cur + 1 :] + y
cur = n[:-1].rfind(x)
if cur < 0:
return 10**18
ans += len(n) - 2 - cur
n = n[:-1][:cur] + n[:-1][cur + 1 :] + x + y
cur = 0
has0 = False
while cur < len(n) - 2 and n[cur] == "0":
has0 = True
cur += 1
if has0 and cur >= len(n) - 2:
return 10**18
return ans + cur
def main():
for line in stdin.readlines():
n = line.strip()
ret = [f(n, "2", "5"), f(n, "7", "5"), f(n, "5", "0"), f(n, "0", "0")]
if min(ret) == 10**18:
print(-1)
else:
print(min(ret))
main() | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN BIN_OP NUMBER NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER RETURN BIN_OP NUMBER NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP NUMBER NUMBER RETURN BIN_OP VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR STRING STRING FUNC_CALL VAR VAR STRING STRING FUNC_CALL VAR VAR STRING STRING FUNC_CALL VAR VAR STRING STRING IF FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | k = input()
zeroes = [-1, -1]
five = -1
two = -1
seven = -1
for i in range(len(k)):
if k[i] == "0":
zeroes[zeroes.index(min(zeroes))] = i
elif k[i] == "2":
two = i
elif k[i] == "5":
five = i
elif k[i] == "7":
seven = i
if len(k) == 3 and k[:2] == "50" and k[-1] != "5" and k[-1] != "0":
print(2)
elif k == "5020" or k == "5070":
print(1)
elif k[-2:] == "25" or k[-2:] == "00" or k[-2:] == "50" or k[-2:] == "75":
print(0)
elif k[-2:] == "52" or k[-2:] == "05" or k[-2:] == "57":
print(1)
elif k == "505":
print(1)
elif k == "500":
print(0)
elif len(k) != 4 and (k[:3] == "502" and two == 2 or k[:3] == "507" and seven == 2):
print(len(k))
elif len(k) == 4 and (k[:3] == "502" or k[:3] == "507"):
print(4)
elif k[:2] == "50" and five == 0 and min(zeroes) == -1 and (seven != -1 or two != -1):
print(2 * len(k) - max(two, seven) - 1)
elif k[:2] == "50" and five == 0 and min(zeroes) != -1 and (seven != -1 or two != -1):
cnt0 = 0
for u in range(1, len(k)):
if k[u] == "0":
cnt0 += 1
print(min(len(k) * 2 - zeroes[0] - zeroes[1] - 3, cnt0 - 1 + len(k)))
else:
ch = []
fl = 0
if min(zeroes) > -1:
fl = 1
ch.append(len(k) * 2 - zeroes[0] - zeroes[1] - 3)
if five != -1 and max(zeroes) > -1:
fl = 1
if five < max(zeroes):
ch.append(2 * len(k) - max(zeroes) - five - 3)
else:
ch.append(2 * len(k) - max(zeroes) - five - 2)
if five != -1:
if two != -1:
fl = 1
if two < five:
ch.append(2 * len(k) - two - five - 3)
else:
ch.append(2 * len(k) - two - five - 2)
if seven != -1:
fl = 1
if seven < five:
ch.append(2 * len(k) - seven - five - 3)
else:
ch.append(2 * len(k) - seven - five - 2)
if fl:
print(min(ch))
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING VAR NUMBER VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER STRING VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER STRING VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | INF = 10000
n = input()
ans = INF
k = len(n)
for i in range(k):
for j in range(k):
if i == j:
continue
s = n
tmp = 0
for p in range(i, k - 1):
s = s[:p] + s[p + 1] + s[p] + s[p + 2 :]
tmp += 1
for p in range(j - (j > i), k - 2):
s = s[:p] + s[p + 1] + s[p] + s[p + 2 :]
tmp += 1
pos = -1
for p in range(k):
if s[p] != "0":
pos = p
break
for p in range(pos, 0, -1):
s = s[: p - 1] + s[p] + s[p - 1] + s[p + 1 :]
tmp += 1
if int(s) % 25 == 0:
ans = min(ans, tmp)
print(ans if ans != INF else -1) | ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | s = input()
n = len(s)
c = dict()
ans = 1e18
c["2"], c["5"], c["7"], c["0"] = 0, 0, 0, 0
for i in s:
if i not in c:
c[i] = 0
c[i] += 1
if c["0"] >= 2:
res = -1
for i in range(n - 1, -1, -1):
if s[i] == "0":
if res == -1:
res = n - 1 - i
else:
res += n - 2 - i
break
ans = min(ans, res)
if c["0"] and c["5"]:
res = 0
f = 0
for i in range(n - 1, -1, -1):
if s[i] == "0":
res += n - 1 - i
break
if s[i] == "5":
f = 1
for i in range(n - 1, -1, -1):
if s[i] == "5":
res += max(0, n - 2 - i + f)
break
ans = min(ans, res)
if c["2"] and c["5"]:
res = 0
f = 0
flag = 0
for i in range(n - 1, -1, -1):
if s[i] == "5":
res += n - 1 - i
break
if s[i] == "2":
f = 1
if i == 0 and s[1] == "0":
for i in range(1, n):
if s[i] != "0":
break
flag = i - 1
for i in range(n - 1, -1, -1):
if s[i] == "2":
res += max(0, n - 2 - i + f)
break
ans = min(ans, res + flag)
if c["7"] and c["5"]:
res = 0
f = 0
flag = 0
for i in range(n - 1, -1, -1):
if s[i] == "5":
res += n - 1 - i
break
if s[i] == "7":
f = 1
if i == 0 and s[1] == "0":
for i in range(1, n):
if s[i] != "0":
break
flag = i - 1
for i in range(n - 1, -1, -1):
if s[i] == "7":
res += max(0, n - 2 - i + f)
break
ans = min(ans, res + flag)
if ans == 1e18:
ans = -1
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING VAR STRING VAR STRING VAR STRING NUMBER NUMBER NUMBER NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR STRING VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR STRING VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR STRING VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | import sys
input = sys.stdin.readline
n = list(input().rstrip())
m = len(n)
inf = 114514
if m == 1:
ans = -1
elif m == 2:
x = 10 * int(n[0]) + int(n[1])
if x % 25 == 0:
ans = 0
elif x == 52 or x == 57:
ans = 1
else:
ans = -1
else:
ans = inf
for i in range(m):
if int(n[i]) % 5:
continue
n0 = list(n)
cnt = 0
f = 1
if i == 0:
f = 0
for j in range(1, m - 1):
if not int(n0[j]) == 0:
f = 1
cnt += j - 1
for k in range(j - 1, 0, -1):
n0[k], n0[k + 1] = n0[k + 1], n0[k]
break
if not f:
continue
for j in range(m - i - 1):
n0[i + j], n0[i + j + 1] = n0[i + j + 1], n0[i + j]
cnt += 1
if int(n0[0]) == 0:
continue
if not 10 * int(n0[-2]) + int(n0[-1]) % 25:
ans = min(ans, cnt)
continue
c = min(2, int(n0[-1]))
f = 0
for j in range(m - 2, 0, -1):
if int(n0[j]) % 5 == c:
f = 1
cnt += m - 2 - j
ans = min(ans, cnt)
break
if not f and int(n0[0]) % 5 == c:
cnt += m - 2
f = 0
for j in range(1, m - 1):
if int(n0[j]):
f = 1
cnt += j - 1
break
if f:
ans = min(ans, cnt)
if not ans ^ inf:
ans = -1
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | class Problem:
def solve(self, s):
ans = -1
n = len(s)
for i in range(n):
for j in range(n):
if i != j:
c1 = c2 = 0
t, c1 = self.move_end(s, i, j)
if t[0] == "0":
k = self.find_nonzero(t)
t, c2 = self.move_begin(t, k)
if self.ok(t):
ans = self.best(ans, c1 + c2)
return ans
def move_end(self, s, i, j):
if j < i:
i -= 1
t = s
t, c1 = self.move_char(t, j, len(t) - 1)
t, c2 = self.move_char(t, i, len(t) - 2)
return t, c1 + c2
def move_begin(self, s, i):
return self.move_char(s, i, 0)
def move_char(self, s, i: "index from", j: "index to"):
l = list(s)
count = 0
while i != j:
count += 1
if i < j:
l[i], l[i + 1] = l[i + 1], l[i]
i += 1
else:
l[i - 1], l[i] = l[i], l[i - 1]
i -= 1
return "".join(l), count
def ok(self, s):
return s[0] != "0" and int(s) % 25 == 0
def find_nonzero(self, s):
found = -1
for i in range(len(s)):
if s[i] != "0":
found = i
break
return found
def best(self, a, b):
if a == -1:
return b
if b == -1:
return a
return a if a < b else b
s = input()
ans = Problem().solve(s)
print(ans)
exit()
assert 0 == Problem().solve("100")
assert 1 == Problem().solve("52")
assert 4 == Problem().solve("5071")
assert 1 == Problem().solve("705")
assert -1 == Problem().solve("1241367")
assert 2 == Problem().solve("507")
assert 1 == Problem().solve("17010")
assert 6 == Problem().solve("52231")
assert 5 == Problem().solve("50267")
print("ok") | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF STRING STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN FUNC_CALL STRING VAR VAR FUNC_DEF RETURN VAR NUMBER STRING BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN VAR RETURN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL FUNC_CALL VAR STRING NUMBER FUNC_CALL FUNC_CALL VAR STRING NUMBER FUNC_CALL FUNC_CALL VAR STRING NUMBER FUNC_CALL FUNC_CALL VAR STRING NUMBER FUNC_CALL FUNC_CALL VAR STRING NUMBER FUNC_CALL FUNC_CALL VAR STRING NUMBER FUNC_CALL FUNC_CALL VAR STRING NUMBER FUNC_CALL FUNC_CALL VAR STRING NUMBER FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Let's call an undirected graph $G = (V, E)$ relatively prime if and only if for each edge $(v, u) \in E$ $GCD(v, u) = 1$ (the greatest common divisor of $v$ and $u$ is $1$). If there is no edge between some pair of vertices $v$ and $u$ then the value of $GCD(v, u)$ doesn't matter. The vertices are numbered from $1$ to $|V|$.
Construct a relatively prime graph with $n$ vertices and $m$ edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
-----Input-----
The only line contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of vertices and the number of edges.
-----Output-----
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The $i$-th of the next $m$ lines should contain the $i$-th edge $(v_i, u_i)$ of the resulting graph ($1 \le v_i, u_i \le n, v_i \neq u_i$). For each pair $(v, u)$ there can be no more pairs $(v, u)$ or $(u, v)$. The vertices are numbered from $1$ to $n$.
If there are multiple answers then print any of them.
-----Examples-----
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
-----Note-----
Here is the representation of the graph from the first example: [Image] | def gcd(a, b):
c = 0
while b > 0:
c = a % b
a = b
b = c
return a
arr = list(map(int, input().split()))
n = arr[0]
m = arr[1]
if m < n - 1:
print("Impossible")
exit()
vec = []
for i in range(n):
for j in range(i + 1, n):
if gcd(int(i + 1), int(j + 1)) == 1:
m -= 1
vec.append({i + 1, j + 1})
if m == 0:
break
if m == 0:
break
if m > 0:
print("Impossible")
exit()
print("Possible")
for x, y in vec:
print("%d %d" % (x, y)) | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR |
Let's call an undirected graph $G = (V, E)$ relatively prime if and only if for each edge $(v, u) \in E$ $GCD(v, u) = 1$ (the greatest common divisor of $v$ and $u$ is $1$). If there is no edge between some pair of vertices $v$ and $u$ then the value of $GCD(v, u)$ doesn't matter. The vertices are numbered from $1$ to $|V|$.
Construct a relatively prime graph with $n$ vertices and $m$ edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
-----Input-----
The only line contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of vertices and the number of edges.
-----Output-----
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The $i$-th of the next $m$ lines should contain the $i$-th edge $(v_i, u_i)$ of the resulting graph ($1 \le v_i, u_i \le n, v_i \neq u_i$). For each pair $(v, u)$ there can be no more pairs $(v, u)$ or $(u, v)$. The vertices are numbered from $1$ to $n$.
If there are multiple answers then print any of them.
-----Examples-----
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
-----Note-----
Here is the representation of the graph from the first example: [Image] | import sys
def make_prime_factors(n: int) -> list:
prime_factors = []
for k in range(2, int(n**0.5) + 1):
while n % k == 0:
prime_factors.append(k)
n = n // k
if n != 1:
prime_factors.append(n)
return set(prime_factors)
input = sys.stdin.readline
def solve(n, m):
ans = []
for num in range(1, n + 1):
set_ = make_prime_factors(num)
for i in range(num + 1, n + 1):
for div in set_:
if i % div == 0:
break
else:
ans.append((i, num))
m -= 1
if m == 0:
return ans
print("Impossible")
exit()
n, m = map(int, input().split())
if n - 1 > m:
print("Impossible")
exit()
ans = solve(n, m)
print("Possible")
for i in ans:
print(*i) | IMPORT FUNC_DEF VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR |
Let's call an undirected graph $G = (V, E)$ relatively prime if and only if for each edge $(v, u) \in E$ $GCD(v, u) = 1$ (the greatest common divisor of $v$ and $u$ is $1$). If there is no edge between some pair of vertices $v$ and $u$ then the value of $GCD(v, u)$ doesn't matter. The vertices are numbered from $1$ to $|V|$.
Construct a relatively prime graph with $n$ vertices and $m$ edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
-----Input-----
The only line contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of vertices and the number of edges.
-----Output-----
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The $i$-th of the next $m$ lines should contain the $i$-th edge $(v_i, u_i)$ of the resulting graph ($1 \le v_i, u_i \le n, v_i \neq u_i$). For each pair $(v, u)$ there can be no more pairs $(v, u)$ or $(u, v)$. The vertices are numbered from $1$ to $n$.
If there are multiple answers then print any of them.
-----Examples-----
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
-----Note-----
Here is the representation of the graph from the first example: [Image] | n, m = map(int, input().split())
def gcd(p, q):
if q == 0:
return p
else:
return gcd(q, p % q)
rec = []
num = 0
for i in range(1, n + 1):
if num == m:
break
for j in range(i, n + 1):
if num == m:
break
if j == i:
continue
if i == 1:
rec.append((1, j))
num += 1
elif gcd(i, j) == 1:
rec.append((i, j))
num += 1
if num < m or n - 1 > m:
print("Impossible")
else:
print("Possible")
for i in range(m):
print("{} {}".format(rec[i][0], rec[i][1])) | ASSIGN VAR 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 LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR NUMBER VAR VAR NUMBER |
Let's call an undirected graph $G = (V, E)$ relatively prime if and only if for each edge $(v, u) \in E$ $GCD(v, u) = 1$ (the greatest common divisor of $v$ and $u$ is $1$). If there is no edge between some pair of vertices $v$ and $u$ then the value of $GCD(v, u)$ doesn't matter. The vertices are numbered from $1$ to $|V|$.
Construct a relatively prime graph with $n$ vertices and $m$ edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
-----Input-----
The only line contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of vertices and the number of edges.
-----Output-----
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The $i$-th of the next $m$ lines should contain the $i$-th edge $(v_i, u_i)$ of the resulting graph ($1 \le v_i, u_i \le n, v_i \neq u_i$). For each pair $(v, u)$ there can be no more pairs $(v, u)$ or $(u, v)$. The vertices are numbered from $1$ to $n$.
If there are multiple answers then print any of them.
-----Examples-----
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
-----Note-----
Here is the representation of the graph from the first example: [Image] | def gcd(a, b):
return a if not b else gcd(b, a % b)
n, m = map(int, input().split())
ans, cnt = "Possible", 0
for i in range(1, n + 1):
for j in range(i + 1, n + 1):
if gcd(i, j) == 1:
cnt += 1
ans += "\n%d %d" % (i, j)
if cnt == m:
break
if cnt == m:
break
print(ans if cnt == m and m > n - 2 else "Impossible") | FUNC_DEF RETURN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR STRING NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP STRING VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR STRING |
Let's call an undirected graph $G = (V, E)$ relatively prime if and only if for each edge $(v, u) \in E$ $GCD(v, u) = 1$ (the greatest common divisor of $v$ and $u$ is $1$). If there is no edge between some pair of vertices $v$ and $u$ then the value of $GCD(v, u)$ doesn't matter. The vertices are numbered from $1$ to $|V|$.
Construct a relatively prime graph with $n$ vertices and $m$ edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
-----Input-----
The only line contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of vertices and the number of edges.
-----Output-----
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The $i$-th of the next $m$ lines should contain the $i$-th edge $(v_i, u_i)$ of the resulting graph ($1 \le v_i, u_i \le n, v_i \neq u_i$). For each pair $(v, u)$ there can be no more pairs $(v, u)$ or $(u, v)$. The vertices are numbered from $1$ to $n$.
If there are multiple answers then print any of them.
-----Examples-----
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
-----Note-----
Here is the representation of the graph from the first example: [Image] | def gcd_recursive(a, b):
if b == 0:
return a
else:
return gcd_recursive(b, a % b)
out = []
inp = [int(x) for x in input().split(" ")]
if inp[1] < inp[0] - 1:
print("Impossible")
else:
for a in range(1, inp[0]):
if len(out) == inp[1] - inp[0] + 1:
print("Possible")
for i in range(inp[0] - 1):
print(1, i + 2)
for elem in out:
print(elem[0], elem[1])
break
for b in range(a + 1, inp[0]):
gcd = gcd_recursive(a + 1, b + 1)
if gcd == 1:
out.append((a + 1, b + 1))
if len(out) == inp[1] - inp[0] + 1:
break
else:
print("Impossible") | FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING |
Let's call an undirected graph $G = (V, E)$ relatively prime if and only if for each edge $(v, u) \in E$ $GCD(v, u) = 1$ (the greatest common divisor of $v$ and $u$ is $1$). If there is no edge between some pair of vertices $v$ and $u$ then the value of $GCD(v, u)$ doesn't matter. The vertices are numbered from $1$ to $|V|$.
Construct a relatively prime graph with $n$ vertices and $m$ edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
-----Input-----
The only line contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of vertices and the number of edges.
-----Output-----
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The $i$-th of the next $m$ lines should contain the $i$-th edge $(v_i, u_i)$ of the resulting graph ($1 \le v_i, u_i \le n, v_i \neq u_i$). For each pair $(v, u)$ there can be no more pairs $(v, u)$ or $(u, v)$. The vertices are numbered from $1$ to $n$.
If there are multiple answers then print any of them.
-----Examples-----
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
-----Note-----
Here is the representation of the graph from the first example: [Image] | from itertools import islice
def get_all(n):
q = [(0, 1, 1, 1)]
while q:
a, b, c, d = q.pop()
e, f = a + c, b + d
if f <= n:
yield e, f
q.append((a, b, e, f))
q.append((e, f, c, d))
def solve(n, m):
x = list(islice(get_all(n), m))
if len(x) == m >= n - 1:
return x
res = solve(*map(int, input().split()))
if res is not None:
print("Possible")
for e in res:
print(*e)
else:
print("Impossible") | FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER WHILE VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NONE EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
Let's call an undirected graph $G = (V, E)$ relatively prime if and only if for each edge $(v, u) \in E$ $GCD(v, u) = 1$ (the greatest common divisor of $v$ and $u$ is $1$). If there is no edge between some pair of vertices $v$ and $u$ then the value of $GCD(v, u)$ doesn't matter. The vertices are numbered from $1$ to $|V|$.
Construct a relatively prime graph with $n$ vertices and $m$ edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
-----Input-----
The only line contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of vertices and the number of edges.
-----Output-----
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The $i$-th of the next $m$ lines should contain the $i$-th edge $(v_i, u_i)$ of the resulting graph ($1 \le v_i, u_i \le n, v_i \neq u_i$). For each pair $(v, u)$ there can be no more pairs $(v, u)$ or $(u, v)$. The vertices are numbered from $1$ to $n$.
If there are multiple answers then print any of them.
-----Examples-----
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
-----Note-----
Here is the representation of the graph from the first example: [Image] | def gcd(a, b):
while b != 0:
t = a
a = b
b = t % b
return a
n, m = map(int, input().split())
c = 0
ans = "Possible " + chr(10)
for i in range(1, n + 1):
for j in range(i + 1, n + 1):
if gcd(i, j) == 1:
ans += str(i) + " " + str(j) + chr(10)
c += 1
if c == m:
break
if c == m:
break
if c != m or n - 1 > m:
print("Impossible")
else:
print(ans) | FUNC_DEF WHILE VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP STRING FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
Let's call an undirected graph $G = (V, E)$ relatively prime if and only if for each edge $(v, u) \in E$ $GCD(v, u) = 1$ (the greatest common divisor of $v$ and $u$ is $1$). If there is no edge between some pair of vertices $v$ and $u$ then the value of $GCD(v, u)$ doesn't matter. The vertices are numbered from $1$ to $|V|$.
Construct a relatively prime graph with $n$ vertices and $m$ edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
-----Input-----
The only line contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of vertices and the number of edges.
-----Output-----
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The $i$-th of the next $m$ lines should contain the $i$-th edge $(v_i, u_i)$ of the resulting graph ($1 \le v_i, u_i \le n, v_i \neq u_i$). For each pair $(v, u)$ there can be no more pairs $(v, u)$ or $(u, v)$. The vertices are numbered from $1$ to $n$.
If there are multiple answers then print any of them.
-----Examples-----
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
-----Note-----
Here is the representation of the graph from the first example: [Image] | def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
nm = input().split()
n = int(nm[0])
m = int(nm[1])
if m < n - 1:
print("Impossible")
else:
l = []
cnt = 0
f = 0
for i in range(1, n):
for j in range(i + 1, n + 1):
if gcd(i, j) == 1:
l.append((i, j))
cnt += 1
if cnt == m:
f = 1
break
if f == 1:
break
if f == 0:
print("Impossible")
else:
print("Possible")
for x in l:
print(*x) | FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR |
Let's call an undirected graph $G = (V, E)$ relatively prime if and only if for each edge $(v, u) \in E$ $GCD(v, u) = 1$ (the greatest common divisor of $v$ and $u$ is $1$). If there is no edge between some pair of vertices $v$ and $u$ then the value of $GCD(v, u)$ doesn't matter. The vertices are numbered from $1$ to $|V|$.
Construct a relatively prime graph with $n$ vertices and $m$ edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
-----Input-----
The only line contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of vertices and the number of edges.
-----Output-----
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The $i$-th of the next $m$ lines should contain the $i$-th edge $(v_i, u_i)$ of the resulting graph ($1 \le v_i, u_i \le n, v_i \neq u_i$). For each pair $(v, u)$ there can be no more pairs $(v, u)$ or $(u, v)$. The vertices are numbered from $1$ to $n$.
If there are multiple answers then print any of them.
-----Examples-----
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
-----Note-----
Here is the representation of the graph from the first example: [Image] | def gcd(x, y):
while y:
x, y = y, x % y
return x
n, m = [int(i) for i in input().split()]
mi = n - 1
mx = n * (n - 1) // 2
if m < mi:
print("Impossible")
else:
l = []
c = 0
for i in range(1, n):
for j in range(i + 1, n + 1):
if gcd(i, j) == 1:
l.append([i, j])
c += 1
if c == m:
break
if c == m:
break
if c == m:
print("Possible")
for i in l:
print(*i)
else:
print("Impossible") | FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
Let's call an undirected graph $G = (V, E)$ relatively prime if and only if for each edge $(v, u) \in E$ $GCD(v, u) = 1$ (the greatest common divisor of $v$ and $u$ is $1$). If there is no edge between some pair of vertices $v$ and $u$ then the value of $GCD(v, u)$ doesn't matter. The vertices are numbered from $1$ to $|V|$.
Construct a relatively prime graph with $n$ vertices and $m$ edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
-----Input-----
The only line contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of vertices and the number of edges.
-----Output-----
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The $i$-th of the next $m$ lines should contain the $i$-th edge $(v_i, u_i)$ of the resulting graph ($1 \le v_i, u_i \le n, v_i \neq u_i$). For each pair $(v, u)$ there can be no more pairs $(v, u)$ or $(u, v)$. The vertices are numbered from $1$ to $n$.
If there are multiple answers then print any of them.
-----Examples-----
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
-----Note-----
Here is the representation of the graph from the first example: [Image] | [n, m] = [int(i) for i in input().split()]
gcd = lambda x, y: x if not y else gcd(y, x % y)
if m < n - 1:
print("Impossible")
else:
pairs = []
flag = 0
i = 0
while i < n and not flag:
j = i + 1
while j < n and not flag:
if len(pairs) == m:
flag = 1
elif gcd(i + 1, j + 1) == 1:
pairs.append((i + 1, j + 1))
j += 1
i += 1
if len(pairs) != m:
print("Impossible")
else:
print("Possible")
for p in pairs:
print(p[0], p[1]) | ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER |
Let's call an undirected graph $G = (V, E)$ relatively prime if and only if for each edge $(v, u) \in E$ $GCD(v, u) = 1$ (the greatest common divisor of $v$ and $u$ is $1$). If there is no edge between some pair of vertices $v$ and $u$ then the value of $GCD(v, u)$ doesn't matter. The vertices are numbered from $1$ to $|V|$.
Construct a relatively prime graph with $n$ vertices and $m$ edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
-----Input-----
The only line contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of vertices and the number of edges.
-----Output-----
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The $i$-th of the next $m$ lines should contain the $i$-th edge $(v_i, u_i)$ of the resulting graph ($1 \le v_i, u_i \le n, v_i \neq u_i$). For each pair $(v, u)$ there can be no more pairs $(v, u)$ or $(u, v)$. The vertices are numbered from $1$ to $n$.
If there are multiple answers then print any of them.
-----Examples-----
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
-----Note-----
Here is the representation of the graph from the first example: [Image] | def gcd(a, b):
while b:
a, b = b, a % b
return a
n, m = map(int, input().split())
if n - 1 > m:
exit(print("Impossible"))
ans = []
co = m
for i in range(1, n):
ans.append((i, i + 1))
co -= 1
for i in range(1, n + 1):
for j in range(i + 2, n + 1):
if co == 0:
break
if gcd(i, j) == 1:
ans.append((i, j))
co -= 1
if co == 0:
print("Possible")
for i in range(m):
print(*ans[i])
else:
print("Impossible") | FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING |
Let's call an undirected graph $G = (V, E)$ relatively prime if and only if for each edge $(v, u) \in E$ $GCD(v, u) = 1$ (the greatest common divisor of $v$ and $u$ is $1$). If there is no edge between some pair of vertices $v$ and $u$ then the value of $GCD(v, u)$ doesn't matter. The vertices are numbered from $1$ to $|V|$.
Construct a relatively prime graph with $n$ vertices and $m$ edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
-----Input-----
The only line contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of vertices and the number of edges.
-----Output-----
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The $i$-th of the next $m$ lines should contain the $i$-th edge $(v_i, u_i)$ of the resulting graph ($1 \le v_i, u_i \le n, v_i \neq u_i$). For each pair $(v, u)$ there can be no more pairs $(v, u)$ or $(u, v)$. The vertices are numbered from $1$ to $n$.
If there are multiple answers then print any of them.
-----Examples-----
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
-----Note-----
Here is the representation of the graph from the first example: [Image] | def gcd(a, b):
return b if a % b == 0 else gcd(b, a % b)
n, m = [int(x) for x in input().split()]
if m < n - 1:
print("Impossible")
elif m == n - 1:
print("Possible")
for i in range(1, n):
print(i, i + 1)
else:
ct = 0
pairs = []
done = False
for i in range(1, n):
if done:
break
for j in range(i + 1, n + 1):
if ct == m:
done = True
break
if i == 1:
pairs += [(i, j)]
ct += 1
elif gcd(i, j) == 1:
pairs += [(i, j)]
ct += 1
if len(pairs) < m:
print("Impossible")
else:
print("Possible")
for i, j in pairs:
print(i, j) | FUNC_DEF RETURN BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR LIST VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR LIST VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
Let's call an undirected graph $G = (V, E)$ relatively prime if and only if for each edge $(v, u) \in E$ $GCD(v, u) = 1$ (the greatest common divisor of $v$ and $u$ is $1$). If there is no edge between some pair of vertices $v$ and $u$ then the value of $GCD(v, u)$ doesn't matter. The vertices are numbered from $1$ to $|V|$.
Construct a relatively prime graph with $n$ vertices and $m$ edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
-----Input-----
The only line contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of vertices and the number of edges.
-----Output-----
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The $i$-th of the next $m$ lines should contain the $i$-th edge $(v_i, u_i)$ of the resulting graph ($1 \le v_i, u_i \le n, v_i \neq u_i$). For each pair $(v, u)$ there can be no more pairs $(v, u)$ or $(u, v)$. The vertices are numbered from $1$ to $n$.
If there are multiple answers then print any of them.
-----Examples-----
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
-----Note-----
Here is the representation of the graph from the first example: [Image] | def gcd(a, b):
if a % b == 0:
return b
else:
return gcd(b, a % b)
n, m = list(map(int, input().split()))
b = []
c = 0
e = 0
for i in range(1, n):
for j in range(i + 1, n + 1):
if gcd(i, j) == 1:
b.append([i, j])
c = c + 1
if c == m:
e = 1
break
if e == 1:
break
if e == 1 and m >= n - 1:
print("Possible")
for i in range(m):
print(*b[i])
else:
print("Impossible") | FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING |
Let's call an undirected graph $G = (V, E)$ relatively prime if and only if for each edge $(v, u) \in E$ $GCD(v, u) = 1$ (the greatest common divisor of $v$ and $u$ is $1$). If there is no edge between some pair of vertices $v$ and $u$ then the value of $GCD(v, u)$ doesn't matter. The vertices are numbered from $1$ to $|V|$.
Construct a relatively prime graph with $n$ vertices and $m$ edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
-----Input-----
The only line contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of vertices and the number of edges.
-----Output-----
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The $i$-th of the next $m$ lines should contain the $i$-th edge $(v_i, u_i)$ of the resulting graph ($1 \le v_i, u_i \le n, v_i \neq u_i$). For each pair $(v, u)$ there can be no more pairs $(v, u)$ or $(u, v)$. The vertices are numbered from $1$ to $n$.
If there are multiple answers then print any of them.
-----Examples-----
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
-----Note-----
Here is the representation of the graph from the first example: [Image] | def gcd(a, b):
while b:
a, b = b, a % b
return a
n, m = [int(_) for _ in input().split()]
edges = []
for i in range(1, n + 1):
for j in range(i + 1, n + 1):
if gcd(i, j) == 1:
edges.append((i, j))
if len(edges) == m:
break
if len(edges) == m:
break
if len(edges) == m and m >= n - 1:
print("Possible")
for edge in edges:
print("%d %d" % (edge[0], edge[1]))
else:
print("Impossible") | FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING |
Let's call an undirected graph $G = (V, E)$ relatively prime if and only if for each edge $(v, u) \in E$ $GCD(v, u) = 1$ (the greatest common divisor of $v$ and $u$ is $1$). If there is no edge between some pair of vertices $v$ and $u$ then the value of $GCD(v, u)$ doesn't matter. The vertices are numbered from $1$ to $|V|$.
Construct a relatively prime graph with $n$ vertices and $m$ edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
-----Input-----
The only line contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of vertices and the number of edges.
-----Output-----
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The $i$-th of the next $m$ lines should contain the $i$-th edge $(v_i, u_i)$ of the resulting graph ($1 \le v_i, u_i \le n, v_i \neq u_i$). For each pair $(v, u)$ there can be no more pairs $(v, u)$ or $(u, v)$. The vertices are numbered from $1$ to $n$.
If there are multiple answers then print any of them.
-----Examples-----
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
-----Note-----
Here is the representation of the graph from the first example: [Image] | def gcd(n1, n2):
if n2 > n1:
return gcd(n2, n1)
if n2 == 0:
return n1
return gcd(n2, n1 % n2)
def main():
n, m = map(int, input().split())
if m < n - 1:
print("Impossible")
return
ans = []
for i in range(1, n):
j = 2
curr = set()
while i * j <= n:
curr.add(i * j)
j += 1
if i == 1:
for j in curr:
ans.append((i, j))
if len(ans) == m:
break
else:
for j in range(i + 1, n + 1):
if j not in curr and gcd(i, j) == 1:
ans.append((i, j))
if len(ans) == m:
break
if len(ans) == m:
break
if len(ans) != m:
print("Impossible")
return
print("Possible")
for i in ans:
print(i[0], i[1])
main() | FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | def find():
n, m = map(int, input().split())
g = [list(input()) for _ in range(n)]
if m % 2 != 0:
ans = " ".join([str(j % 2 + 1) for j in range(m + 1)])
return "Yes\n" + ans
for j in range(n):
for k in range(j + 1, n):
if g[k][j] == g[j][k]:
res = j + 1, k + 1
ans = " ".join([str(res[p % 2]) for p in range(m + 1)])
return "Yes\n" + ans
for j in range(n):
last = []
for k in range(n):
last.append(g[k][j])
last_set = set(last)
for k in range(n):
if g[j][k] != "*" and g[j][k] in last_set:
x = last.index(g[j][k]) + 1
y = j + 1
z = k + 1
res = x, y, z, y
dif = 0 if m // 2 % 2 != 0 else 1
ans = " ".join([str(res[(p + dif) % 4]) for p in range(m + 1)])
return "Yes\n" + ans
return "No"
for i in range(int(input())):
print(find()) | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP STRING VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP STRING VAR RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | def arrIn():
return list(map(int, input().split()))
def mapIn():
return map(int, input().split())
for ii in range(int(input())):
n, k = mapIn()
arr = [([0] * n) for i in range(n)]
for i in range(n):
s = input()
for j in range(n):
arr[i][j] = s[j]
if k == 1:
print("YES")
print(1, 2)
continue
char = None
flg1 = False
flg2 = False
for i in range(n):
j2 = -1
for j in range(n):
if i != j:
if arr[i][j] == arr[j][i]:
char1 = arr[i][j]
flg1 = True
i1 = i + 1
j1 = j + 1
break
elif j2 != -1 and arr[i][j] != arr[i][j2 - 1]:
i1 = i + 1
j1 = j + 1
flg2 = True
break
elif j2 == -1:
i1 = i + 1
j2 = j + 1
if flg1 or flg2:
break
if flg1 == True:
print("YES")
for i in range(k + 1):
if i % 2 == 0:
print(i1, end=" ")
else:
print(j1, end=" ")
print()
elif flg2 == True:
print("YES")
if k % 2 == 1:
for i in range(k + 1):
if i % 2 == 0:
print(i1, end=" ")
else:
print(j2, end=" ")
print()
elif k // 2 % 2 == 1:
for i in range(k + 1):
if i % 4 == 0:
print(j1, end=" ")
elif i % 4 == 1:
print(i1, end=" ")
elif i % 4 == 2:
print(j2, end=" ")
else:
print(i1, end=" ")
print()
else:
for i in range(k + 1):
if i % 4 == 0:
print(i1, end=" ")
elif i % 4 == 1:
print(j1, end=" ")
elif i % 4 == 2:
print(i1, end=" ")
else:
print(j2, end=" ")
print()
elif k % 2 == 0:
print("NO")
else:
print("YES")
for i in range(k + 1):
if i % 2 == 0:
print(i1, end=" ")
else:
print(j2, end=" ")
print() | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
from itertools import permutations
input = lambda: sys.stdin.readline().rstrip()
def solve(n, m, graph):
for i in range(n):
for j in range(n):
if i != j and graph[i][j] == graph[j][i]:
print("YES")
ans = ([i + 1, j + 1] * m)[: m + 1]
print(*ans)
return
if m % 2:
print("YES")
ans = ([1, 2] * m)[: m + 1]
print(*ans)
return
if n == 2:
print("NO")
return
for c in permutations(range(3), 3):
i, j, k = c
if graph[i][j] == graph[j][k]:
break
print("YES")
ans = (
([i + 1, j + 1] * (m // 2))[: m // 2][::-1]
+ [j + 1]
+ ([k + 1, j + 1] * (m // 2))[: m // 2]
)
print(*ans)
return
t = int(input())
for i in range(t):
n, m = map(int, input().split())
graph = []
for j in range(n):
graph.append(input())
solve(n, m, graph) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER LIST BIN_OP VAR NUMBER BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | from itertools import cycle, islice
from sys import stdin
readline = stdin.readline
def read_ints():
return map(int, readline().split())
def read_string():
return readline()[:-1]
def f(a_seq, b_seq, c_seq):
assert len(a_seq) == len(b_seq)
n = len(a_seq)
colors_need = Counter(b for a, b in zip(a_seq, b_seq) if a != b)
colors_has = Counter(c_seq)
if any(colors_has[c] < required_n for c, required_n in colors_need.items()):
raise ValueError
indices_by_required_colors = defaultdict(list)
for i in range(n):
if b_seq[i] != a_seq[i]:
indices_by_required_colors[b_seq[i]].append(i)
if c_seq[-1] not in b_seq:
raise ValueError
if c_seq[-1] in indices_by_required_colors:
i_swap = indices_by_required_colors[c_seq[-1]][0]
else:
i_swap = b_seq.index(c_seq[-1])
for c in c_seq:
if c in indices_by_required_colors:
yield indices_by_required_colors[c].pop()
if not indices_by_required_colors[c]:
del indices_by_required_colors[c]
else:
yield i_swap
def f(graph, m):
assert n >= 2
assert m >= 1
for u in range(n):
for v in range(u + 1, n):
if graph[u][v] == graph[v][u]:
return list(islice(cycle([u, v]), m + 1))
if m % 2 != 0:
return list(islice(cycle([0, 1]), m + 1))
if n == 2:
raise ValueError
g = [("a" in row, "b" in row) for row in graph]
for u in range(n):
for v in range(n):
if u != v:
ch = graph[u][v]
if g[v][ord(ch) - ord("a")]:
w = graph[v].index(ch)
base = [u, v, w]
v_need = m + 1 - 3
if v_need == 0:
return base
v_at_half = v_need // 2
return (
list(islice(cycle([v, u]), v_at_half))[::-1]
+ base
+ list(islice(cycle([v, w]), v_at_half))
)
raise ValueError
(t_n,) = read_ints()
for i_t in range(t_n):
n, m = read_ints()
graph = [read_string() for i_row in range(n)]
try:
path = f(graph, m)
except ValueError:
print("NO")
else:
print("YES")
print(*[(v + 1) for v in path]) | ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR VAR FUNC_DEF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR LIST NUMBER NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR STRING VAR STRING VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, m = map(int, input().split())
G = [input()[:-1] for _ in range(n)]
if m % 2:
ans = [1, 2] * (m // 2 + 1)
print("YES")
print(*ans)
continue
flag = 0
for i in range(n):
for j in range(i + 1, n):
if G[i][j] == G[j][i]:
ans = [i + 1, j + 1] * (m // 2) + [i + 1]
flag = 1
print("YES")
print(*ans)
break
if flag:
break
else:
dic = [{} for _ in range(n)]
for i in range(n):
for j in range(n):
if i == j:
continue
dic[i][G[i][j]] = j
if len(dic[i]) == 2:
break
flag = 0
for i in range(n):
for j in range(n):
if i == j:
continue
if G[i][j] in dic[j]:
k = dic[j][G[i][j]]
m //= 2
if not m % 2:
ans = [j + 1] + [k + 1, j + 1, i + 1, j + 1] * (m // 2)
else:
ans = [i + 1] + [j + 1, k + 1, j + 1, i + 1] * (m // 2 + 1)
ans = ans[: 2 * m + 1]
print("YES")
print(*ans)
flag = 1
break
if flag:
break
else:
print("NO") | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR DICT VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
read = sys.stdin.readline
write = lambda x, end="\n": sys.stdout.write(x + end)
for _ in range(int(input())):
n, m = map(int, input().split())
g = [input()[:3] for _ in range(n)][:3]
if m & 1:
print("YES")
print(" ".join("1" if i & 1 else "2" for i in range(m + 1)))
elif g == ["*a", "b*"] or g == ["*b", "a*"]:
print("NO")
elif len(g) == 2:
print("YES")
print(" ".join("1" if i & 1 else "2" for i in range(m + 1)))
else:
db = False
for y in range(3):
for x in range(3):
if y != x and g[y][x] == g[x][y]:
print("YES")
print(
" ".join(
str(y + 1) if i & 1 else str(x + 1) for i in range(m + 1)
)
)
db = True
break
if db:
break
else:
if g[0][1] == g[1][2] == g[2][0]:
print("YES")
print(" ".join(str(i % 3 + 1) for i in range(m + 1)))
else:
x = 1 if g[0][1] != g[0][2] else 2 if g[1][0] != g[1][2] else 3
y, z = {1, 2, 3} - {x}
if m & 2:
print("YES")
print(
" ".join(
str(x if i & 1 == 0 else y if i & 2 == 0 else z)
for i in range(1, m + 2)
)
)
else:
print("YES")
print(
" ".join(
str(x if i & 1 == 0 else y if i & 2 == 0 else z)
for i in range(m + 1)
)
) | IMPORT ASSIGN VAR VAR ASSIGN VAR STRING FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER STRING STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR LIST STRING STRING VAR LIST STRING STRING EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER STRING STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER NUMBER NUMBER VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | from sys import gettrace, stdin
if gettrace():
def inputi():
return input()
else:
def input():
return next(stdin)[:-1]
def inputi():
return stdin.buffer.readline()
def solve():
n, m = map(int, input().split())
ll = []
for _ in range(n):
ll.append(input())
if m % 2 == 1:
print("YES")
print("1 2 " * ((m + 1) // 2))
return
if n == 2 and ll[0][1] != ll[1][0]:
print("NO")
return
if ll[0][1] == ll[1][0]:
print("YES")
print("1 2 " * (m // 2) + "1")
return
if ll[1][2] == ll[2][1]:
print("YES")
print("2 3 " * (m // 2) + "2")
return
if ll[2][0] == ll[0][2]:
print("YES")
print("3 1 " * (m // 2) + "3")
return
if ll[0][1] == ll[1][2]:
print("YES")
if m % 4 == 0:
print("2 1 " * (m // 4) + "2 3 " * (m // 4) + "2")
else:
print("1 " + "2 1 " * (m // 4) + "2 3 " * (m // 4) + "2 3")
return
if ll[1][2] == ll[2][0]:
print("YES")
if m % 4 == 0:
print("3 2 " * (m // 4) + "3 1 " * (m // 4) + "3")
else:
print("2 " + "3 2 " * (m // 4) + "3 1 " * (m // 4) + "3 1")
return
if ll[2][0] == ll[0][1]:
print("YES")
if m % 4 == 0:
print("1 3 " * (m // 4) + "1 2 " * (m // 4) + "1")
else:
print("3 " + "1 3 " * (m // 4) + "1 2 " * (m // 4) + "1 2")
return
def main():
t = int(input())
for _ in range(t):
solve()
main() | IF FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL 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 IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER RETURN IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR NUMBER STRING RETURN IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR NUMBER STRING RETURN IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR NUMBER STRING RETURN IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER STRING RETURN IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER STRING RETURN IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER STRING RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
for _ in range(int(input())):
n, t = [int(x) for x in input().split()]
arr = []
for i in range(n):
arr.append(input())
if t % 2:
print("YES")
ans = []
for i in range(t + 1):
if i % 2:
ans.append(2)
else:
ans.append(1)
print(*ans)
continue
f = False
for i in range(n):
for j in range(i + 1, n):
if arr[i][j] == arr[j][i]:
f = True
ans = [i + 1, j + 1]
if f:
print("YES")
print(*(ans * (t // 2) + [ans[0]]))
continue
for i in range(n):
ha, hb, va, vb = -1, -1, -1, -1
for k in range(n):
if arr[k][i] == "a":
va = k
if arr[k][i] == "b":
vb = k
if arr[i][k] == "a":
ha = k
if arr[i][k] == "b":
hb = k
if ha != -1 and va != -1:
f = True
if t % 4 == 0:
ans = [i + 1, ha + 1, i + 1, va + 1]
else:
ans = [ha + 1, i + 1, va + 1, i + 1]
break
if vb != -1 and hb != -1:
f = True
if t % 4 == 0:
ans = [i + 1, vb + 1, i + 1, 1 + hb]
else:
ans = [hb + 1, i + 1, vb + 1, i + 1]
break
if f and t % 4 == 0:
print("YES")
res = []
for i in range(t + 1):
res.append(ans[i % 4])
print(*res)
elif f:
print("YES")
res = []
for i in range(t + 1):
res.append(ans[i % 4])
print(*res)
else:
print("NO") | IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER LIST VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
input = sys.stdin.readline
for t in range(int(input())):
N, M = map(int, input().split())
S = [input() for i in range(N)]
F = 0
for i in range(N):
for j in range(N):
if i != j and S[i][j] == S[j][i]:
F = 1
P = [i + 1]
for k in range(M):
if k & 1:
P.append(i + 1)
else:
P.append(j + 1)
print("YES")
print(*P)
break
if F:
break
if F:
continue
X = [[[-1, -1], [-1, -1]] for i in range(N)]
for i in range(N):
for j in range(N):
if i != j:
if S[i][j] == "a":
X[i][0][1] = j
X[j][0][0] = i
else:
X[j][1][1] = j
X[i][1][0] = i
if M & 1:
P = [((i & 1) + 1) for i in range(M + 1)]
print("YES")
print(*P)
continue
F = 0
for i in range(N):
for j in range(2):
if min(X[i][j]) >= 0:
P = [0] * (M + 1)
P[M >> 1] = i + 1
for k in range(M >> 1):
if k & 1:
P[(M >> 1) - k - 1] = i + 1
P[(M >> 1) + k + 1] = i + 1
else:
P[(M >> 1) - k - 1] = X[i][j][0] + 1
P[(M >> 1) + k + 1] = X[i][j][1] + 1
print("YES")
print(*P)
F = 1
break
if F:
break
if F:
continue
print("NO") | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF VAR IF VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR EXPR FUNC_CALL VAR STRING |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | def get_best_edge(graph):
for i in range(len(graph) - 1):
for j in range(i + 1, len(graph)):
if graph[i][j] == graph[j][i]:
return True, i, j
return False, 0, 1, 2
def solve(graph, m):
x = get_best_edge(graph)
res = []
if x[0] or m % 2 == 1:
for i in range(m + 1):
if i % 2 == 0:
res.append(str(x[1] + 1))
else:
res.append(str(x[2] + 1))
return res
if len(graph) == 2:
return res
return solve_on_first3(graph, m)
def check_short_cycle(graph):
if graph[0][:3] == "*ba" and graph[1][:3] == "a*b":
return True
if graph[0][:3] == "*ab" and graph[1][:3] == "b*a":
return True
return False
def get_three_nodes(graph):
if graph[0][1] != graph[0][2]:
return 2, 0, 1
if graph[1][0] != graph[1][2]:
return 0, 1, 2
if graph[2][0] != graph[2][1]:
return 0, 2, 1
def solve_on_first3(graph, m):
res = []
if check_short_cycle(graph):
for i in range(m + 1):
res.append(str(i % 3 + 1))
return res
x, y, z = get_three_nodes(graph)
if m // 2 % 2 == 1:
for i in range(m + 1):
if i % 4 == 0:
res.append(str(x + 1))
elif i % 4 == 1:
res.append(str(y + 1))
elif i % 4 == 2:
res.append(str(z + 1))
elif i % 4 == 3:
res.append(str(y + 1))
else:
for i in range(m + 1):
if i % 4 == 0:
res.append(str(y + 1))
elif i % 4 == 1:
res.append(str(z + 1))
elif i % 4 == 2:
res.append(str(y + 1))
elif i % 4 == 3:
res.append(str(x + 1))
return res
T = int(input())
for _ in range(T):
n, m = tuple(map(int, input().split(" ")))
graph = []
for i in range(n):
graph.append(input())
answer = solve(graph, m)
if len(answer) > 0:
print("YES")
print(" ".join(answer))
else:
print("NO") | FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR RETURN NUMBER VAR VAR RETURN NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER NUMBER STRING VAR NUMBER NUMBER STRING RETURN NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER NUMBER STRING RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST IF FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
input = sys.stdin.readline
t = int(input())
for tests in range(t):
n, m = map(int, input().split())
S = [input().strip() for i in range(n)]
flag = 0
for i in range(n):
for j in range(i + 1, n):
if S[i][j] == S[j][i]:
ANSX = i, j
flag = 1
break
if flag == 1:
break
if flag == 1:
print("YES")
ANS = []
for i in range(m + 1):
ANS.append(ANSX[i % 2] + 1)
print(*ANS)
continue
if m % 2 == 1:
print("YES")
ANSX = 0, 1
ANS = []
for i in range(m + 1):
ANS.append(ANSX[i % 2] + 1)
print(*ANS)
continue
if n == 2:
print("NO")
continue
GO = [[0, 0] for i in range(n)]
for i in range(n):
for j in range(n):
if i == j:
continue
if S[i][j] == "a":
GO[i][0] = 1
else:
GO[i][1] = 1
for i in range(n):
if GO[i][0] == 1 and GO[i][1] == 1:
USE = i
break
UA = -1
UB = -1
for i in range(n):
if i == USE:
continue
if S[USE][i] == "a":
UA = i
else:
UB = i
m2 = m // 2
print("YES")
if m2 % 2 == 0:
ANS = []
ANSX = USE, UA
for i in range(m2 + 1):
ANS.append(ANSX[i % 2] + 1)
ANSX = UB, USE
for i in range(m2):
ANS.append(ANSX[i % 2] + 1)
print(*ANS)
continue
else:
ANS = []
ANSX = UA, USE
for i in range(m2 + 1):
ANS.append(ANSX[i % 2] + 1)
ANSX = UB, USE
for i in range(m2):
ANS.append(ANSX[i % 2] + 1)
print(*ANS)
continue | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
input = iter(sys.stdin.read().splitlines()).__next__
def solve():
n, m = map(int, input().split())
edges = [input() for _ in range(n)]
if m % 2:
path = [1, 2] * (m // 2 + 1)
return f"YES\n{' '.join(map(str, path))}"
for u in range(n):
for v in range(u + 1, n):
if edges[u][v] == edges[v][u]:
path = [u + 1, v + 1] * (m // 2 + 1)
return f"YES\n{' '.join(map(str, path[:m + 1]))}"
incoming = [set() for _ in range(n)]
outgoing = [set() for _ in range(n)]
for u in range(n):
for v in range(n):
if u == v:
continue
incoming[u].add(edges[v][u])
outgoing[u].add(edges[u][v])
for u in range(n):
in_and_out = incoming[u] & outgoing[u]
if not in_and_out:
continue
char_needed = in_and_out.pop()
for i in range(n):
if edges[i][u] == char_needed:
break
o = edges[u].index(char_needed)
needed_each_side = m // 2
if needed_each_side % 2:
path = [i, o] * (needed_each_side // 2)
path.append(i)
path.append(u)
path.extend([o, i] * (needed_each_side // 2))
path.append(o)
else:
left = [o, i] * (needed_each_side // 2)
right = left.copy()
path = left
path.append(u)
path.extend(right)
return f"YES\n{' '.join(map(lambda vertex: str(vertex + 1), path))}"
return "NO"
t = int(input())
output = []
for _ in range(t):
output.append(solve())
print(*output, sep="\n") | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER RETURN STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER RETURN STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN STRING FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
input = sys.stdin.readline
def find_aa_line(three):
for i in range(3):
for j in range(3):
if i == j:
continue
for k in range(3):
if j == k or i == k:
continue
if three[i][j] == three[j][k]:
return [i + 1, j + 1, k + 1]
return None
def find_aa_loop(three):
for i in range(3):
for j in range(3):
if i == j:
continue
if three[i][j] == three[j][i]:
return [i + 1, j + 1]
return None
t = int(input())
for _ in range(t):
n, m = map(int, input().split())
three = []
for i in range(n):
line = input().rstrip()
if i < 3:
three.append(line[:3])
if n == 2:
if m % 2 == 1:
ans = [(1 + item % 2) for item in range(m + 1)]
elif three[0][1] == three[1][0]:
ans = [(1 + item % 2) for item in range(m + 1)]
else:
ans = None
if n > 2:
if m % 2 == 1:
ans = [(1 + item % 2) for item in range(m + 1)]
else:
uv = find_aa_loop(three)
if uv is not None:
ans = [uv[item % 2] for item in range(m + 1)]
else:
uvw = find_aa_line(three)
if m % 4 == 0:
module = [uvw[1], uvw[2], uvw[1], uvw[0]]
ans = module * (m // 4) + [uvw[1]]
else:
module = [uvw[0], uvw[1], uvw[2], uvw[1]]
ans = module * (m // 4) + [uvw[0], uvw[1], uvw[2]]
if ans is not None:
print("YES")
print(*ans)
else:
print("NO") | IMPORT ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR RETURN LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN NONE FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR VAR VAR RETURN LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NONE IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER LIST VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | from sys import stdin, stdout
input = stdin.readline
def f1(i, j, k):
global all
if mat[i][j] == mat[j][k] and mat[j][i] == mat[k][j]:
all = []
for p in range(m // 2 + 1):
if p % 2 == 0:
all.append(i + 1)
else:
all.append(j + 1)
all.pop()
for p in range(m // 2 + 1):
if p % 2 == 0:
all.append(j + 1)
else:
all.append(k + 1)
return True
return False
def f2(i, j, k):
global all
if mat[i][j] == mat[j][k] and mat[j][i] == mat[k][j]:
all = []
for p in range(m // 2 + 1):
if p % 2 == 0:
all.append(j + 1)
else:
all.append(i + 1)
all.pop()
for p in range(m // 2 + 1):
if p % 2 == 0:
all.append(j + 1)
else:
all.append(k + 1)
return True
return False
t = int(input())
for _ in range(t):
n, m = map(int, input().split())
mat = [[(0) for i in range(n)] for j in range(n)]
for i in range(n):
s = input()[:-1]
for j in range(n):
mat[i][j] = s[j]
if m % 2:
print("YES")
for i in range(m + 1):
if i % 2:
stdout.write(str(1) + " ")
else:
stdout.write(str(2) + " ")
print()
continue
pivot = 0
for i in range(n):
for j in range(n):
if i == j:
continue
if mat[i][j] == mat[j][i]:
pivot = 1
a = i + 1
b = j + 1
print("YES")
for k in range(m + 1):
if k % 2:
stdout.write(str(a) + " ")
else:
stdout.write(str(b) + " ")
print()
break
if pivot:
break
if pivot:
continue
if n == 2:
print("no")
continue
print("yes")
if m // 2 % 2:
all = []
f1(0, 1, 2)
if len(all) > 0:
print(*all)
continue
f1(0, 2, 1)
if len(all) > 0:
print(*all)
continue
f1(1, 0, 2)
if len(all) > 0:
print(*all)
continue
f1(1, 2, 0)
if len(all) > 0:
print(*all)
continue
f1(2, 0, 1)
if len(all) > 0:
print(*all)
continue
f1(2, 1, 0)
if len(all) > 0:
print(*all)
continue
else:
all = []
f2(0, 1, 2)
if len(all) > 0:
print(*all)
continue
f2(0, 2, 1)
if len(all) > 0:
print(*all)
continue
f2(1, 0, 2)
if len(all) > 0:
print(*all)
continue
f2(1, 2, 0)
if len(all) > 0:
print(*all)
continue
f2(2, 0, 1)
if len(all) > 0:
print(*all)
continue
f2(2, 1, 0)
if len(all) > 0:
print(*all)
continue | ASSIGN VAR VAR FUNC_DEF IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF VAR IF VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | def repeat(l, count):
res = []
for i in range(count):
res.append(l[i % len(l)])
return res
def solve(graph, l):
if l % 2 == 1:
return repeat([0, 1], l + 1)
for i in range(len(graph)):
for j in range(len(graph)):
if i != j and graph[i][j] == graph[j][i]:
return repeat([i, j], l + 1)
if len(graph) == 2:
return []
for i in range(len(graph)):
if "a" not in graph[i] or "b" not in graph[i]:
continue
to_a = [x for x in range(len(graph)) if graph[i][x] == "a"][0]
to_b = [x for x in range(len(graph)) if graph[i][x] == "b"][0]
if l % 4 == 0:
return repeat([i, to_a, i, to_b], l + 1)
for ch in "ab":
prev = [x for x in range(len(graph)) if graph[x][i] == ch]
if not prev:
continue
prev = prev[0]
return (
[prev]
+ repeat([i, to_a, i, to_b], l - 1)
+ [to_a if ch == "a" else to_b]
)
return []
def main():
tests = int(input())
for _ in range(tests):
n, l = map(int, input().split())
graph = [input() for _ in range(n)]
res = solve(graph, l)
if not res:
print("NO")
else:
print("YES")
for x in res:
print(x + 1, end=" ")
print()
main() | FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR LIST NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF STRING VAR VAR STRING VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR STRING NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR STRING NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR LIST VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR ASSIGN VAR VAR NUMBER RETURN BIN_OP BIN_OP LIST VAR FUNC_CALL VAR LIST VAR VAR VAR VAR BIN_OP VAR NUMBER LIST VAR STRING VAR VAR RETURN LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
from itertools import permutations
input = sys.stdin.readline
def solution(n, m, mat):
sol = []
if m % 2 == 1:
print("YES")
for i in range(m + 1):
sol.append(i % 2 + 1)
print(*sol)
return
elif n == 2 and mat[0][1] != mat[1][0]:
print("NO")
return
elif n == 2:
print("YES")
for i in range(m + 1):
sol.append(i % 2 + 1)
print(*sol)
return
else:
for [u, v] in [[0, 1], [0, 2], [1, 2]]:
if mat[u][v] == mat[v][u]:
print("YES")
for i in range(m + 1):
sol.append([u, v][i % 2] + 1)
print(*sol)
return
for [u, v, w] in permutations([0, 1, 2]):
if mat[u][v] == mat[v][w]:
left = [
[v + 1, w + 1, v + 1, u + 1][i % 4] for i in range((m - 2) // 2)
]
left.reverse()
middle = [u + 1, v + 1, w + 1]
right = [
[v + 1, u + 1, v + 1, w + 1][i % 4] for i in range((m - 2) // 2)
]
sol += left
sol += middle
sol += right
print("YES")
print(*sol)
return
T = int(input())
for t in range(T):
[n, m] = list(map(int, input().split()))
mat = [input() for _ in range(n)]
solution(n, m, mat) | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR LIST VAR VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR LIST VAR VAR VAR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST 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 VAR VAR VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | from itertools import cycle, islice
def solve():
n, m = [int(t) for t in input().split()]
g = [input() for _ in range(n)]
for i in range(n):
for j in range(i + 1, n):
if g[i][j] == g[j][i]:
return repeat(i, j, m)
if m % 2 == 1:
return repeat(0, 1, m)
if n >= 3:
for x, y, z in ((0, 1, 2), (1, 2, 0), (2, 0, 1)):
if g[x][y] == g[y][z]:
if m // 2 % 2 == 1:
return repeat(x, y, m // 2 - 1) + repeat(y, z, m // 2)
else:
return repeat(y, x, m // 2 - 1) + repeat(y, z, m // 2)
return None
def repeat(i, j, k):
return list(islice(cycle([i + 1, j + 1]), k + 1))
for _ in range(int(input())):
res = solve()
if res is None:
print("NO")
else:
print("YES")
print(*res) | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER VAR IF VAR NUMBER FOR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN NONE FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NONE EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | def main():
num_cases = int(input())
for case_num in range(1, num_cases + 1):
solve(case_num)
def solve(case_num):
n, m = [int(x) for x in input().split()]
A = [input() for _ in range(n)]
if m % 2 != 0:
cycle = [1, 2]
print("YES")
print(" ".join(str(cycle[i % 2]) for i in range(m + 1)))
return
if n == 2:
if A[0][1] == A[1][0]:
cycle = [1, 2]
print("YES")
print(" ".join(str(cycle[i % 2]) for i in range(m + 1)))
else:
print("NO")
return
for i in range(3):
j = (i + 1) % 3
if A[i][j] == A[j][i]:
cycle = [i + 1, j + 1]
print("YES")
print(" ".join(str(cycle[i % 2]) for i in range(m + 1)))
return
if A[0][1] == A[1][2] == A[2][0]:
cycle = [1, 2, 3]
print("YES")
print(" ".join(str(cycle[i % 3]) for i in range(m + 1)))
return
for i in range(3):
j = (i + 1) % 3
k = (j + 1) % 3
if A[i][j] == A[j][k]:
if m % 4 == 0:
first = [j + 1, k + 1] * (m // 4)
mid = [j + 1]
second = [i + 1, j + 1] * (m // 4)
else:
first = [i + 1, j + 1] * ((m - 2) // 4)
mid = [i + 1, j + 1, k + 1]
second = [j + 1, k + 1] * ((m - 2) // 4)
path = first + mid + second
print("YES")
print(" ".join(str(x) for x in path))
return
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN IF VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR RETURN EXPR FUNC_CALL VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
sys.setrecursionlimit(10**5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.buffer.readline())
def MI():
return map(int, sys.stdin.buffer.readline().split())
def LI():
return list(map(int, sys.stdin.buffer.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def BI():
return sys.stdin.buffer.readline().rstrip()
def SI():
return sys.stdin.buffer.readline().rstrip().decode()
inf = 10**16
md = 10**9 + 7
def solve():
if m & 1:
print("YES")
ans = [((i & 1) + 1) for i in range(m + 1)]
print(*ans)
return
for i in range(n):
for j in range(i):
if ss[i][j] == ss[j][i]:
print("YES")
ans = [i + 1, j + 1] * (m // 2) + [i + 1]
print(*ans)
return
if n == 2:
print("NO")
return
for i in range(n):
j = ss[i].find("a")
k = ss[i].find("b")
if j == -1 or k == -1:
continue
if m % 4:
ans = [(i + 1 if c % 2 else j + 1) for c in range(m // 2 + 1)]
else:
ans = [(j + 1 if c % 2 else i + 1) for c in range(m // 2 + 1)]
ans += [(i + 1 if c % 2 else k + 1) for c in range(m // 2)]
print("YES")
print(*ans)
return
for _ in range(II()):
n, m = MI()
ss = [SI() for _ in range(n)]
solve() | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING IF VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
input = sys.stdin.readline
for f in range(int(input())):
n, m = map(int, input().split())
e = []
for i in range(n):
e.append(input())
if n == 2:
if m % 2:
print("YES")
print(*([1, 2] * (m // 2 + 1)))
elif e[0][1] == e[1][0]:
print("YES")
print(*([1, 2] * (m // 2) + [1]))
else:
print("NO")
elif m % 2:
print("YES")
print(*([1, 2] * (m // 2 + 1)))
else:
good = False
for i in range(2):
for j in range(i + 1, 3):
if e[i][j] == e[j][i]:
good = True
gi = i + 1
gj = j + 1
if good:
print("YES")
print(*([gi, gj] * (m // 2) + [gi]))
else:
for i in range(3):
for j in range(3):
if j != i:
for k in range(3):
if k != i and k != j and e[i][j] == e[j][k]:
gi = i + 1
gj = j + 1
gk = k + 1
print("YES")
if m % 4 == 2:
print(*([gi] + [gj, gk, gj, gi] * (m // 4) + [gj, gk]))
else:
print(*([gj, gk, gj, gi] * (m // 4) + [gj])) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP LIST NUMBER NUMBER BIN_OP VAR NUMBER LIST NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP LIST VAR VAR BIN_OP VAR NUMBER LIST VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP LIST VAR BIN_OP LIST VAR VAR VAR VAR BIN_OP VAR NUMBER LIST VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP LIST VAR VAR VAR VAR BIN_OP VAR NUMBER LIST VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
zz = 1
sys.setrecursionlimit(10**5)
if zz:
input = sys.stdin.readline
else:
sys.stdin = open("input.txt", "r")
sys.stdout = open("all.txt", "w")
di = [[-1, 0], [1, 0], [0, 1], [0, -1]]
def fori(n):
return [fi() for i in range(n)]
def inc(d, c, x=1):
d[c] = d[c] + x if c in d else x
def ii():
return input().rstrip()
def li():
return [int(xx) for xx in input().split()]
def fli():
return [float(x) for x in input().split()]
def dadd(d, p, val):
if p in d:
d[p].append(val)
else:
d[p] = [val]
def gi():
return [xx for xx in input().split()]
def gtc(tc, ans):
print("Case #" + str(tc) + ":", ans)
def cil(n, m):
return n // m + int(n % m > 0)
def fi():
return int(input())
def pro(a):
return reduce(lambda a, b: a * b, a)
def swap(a, i, j):
a[i], a[j] = a[j], a[i]
def si():
return list(input().rstrip())
def mi():
return map(int, input().split())
def gh():
sys.stdout.flush()
def isvalid(i, j, n, m):
return 0 <= i < n and 0 <= j < m
def bo(i):
return ord(i) - ord("a")
def graph(n, m):
for i in range(m):
x, y = mi()
a[x].append(y)
a[y].append(x)
t = fi()
uu = t
while t > 0:
t -= 1
n, m = mi()
a = []
for i in range(n):
a.append(si())
ide = [{} for i in range(n + 1)]
ode = [{} for i in range(n + 1)]
for i in range(n):
for j in range(n):
dadd(ode[i], a[i][j], j)
dadd(ide[j], a[i][j], i)
if m % 2:
print("YES")
ans = [1, 2] * ((m + 1) // 2)
print(*ans)
continue
flag = 0
for i in range(n):
for j in range(i + 1, n):
if a[i][j] == a[j][i]:
ans = [i + 1, j + 1] * (2 * m + 4)
flag = 1
break
if flag:
break
if flag:
print("YES")
print(*ans[: m + 1])
continue
ans = []
if m // 2 % 2:
ans = []
flag = 0
for i in range(n):
if flag:
break
for j in "ab":
if j in ide[i] and j in ode[i]:
for u in range(m // 2 + 1):
if u % 2 == 0:
ans.append(ide[i][j][0] + 1)
else:
ans.append(i + 1)
for u in range(m // 2):
if u % 2 == 0:
ans.append(ode[i][j][0] + 1)
else:
ans.append(i + 1)
flag = 1
break
if len(ans):
print("YES")
print(*ans)
else:
print("NO")
else:
flag = 0
for i in range(n):
if flag:
break
if "a" in ode[i] and "b" in ode[i]:
for u in range(m // 2 + 1):
if u % 2:
ans.append(ode[i]["a"][0] + 1)
else:
ans.append(i + 1)
for u in range(m // 2):
if u % 2 == 0:
ans.append(ode[i]["b"][0] + 1)
else:
ans.append(i + 1)
flag = 1
break
if len(ans):
print("YES")
print(*ans)
else:
print("NO") | IMPORT ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF RETURN NUMBER VAR VAR NUMBER VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST IF BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR FOR VAR STRING IF VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR IF STRING VAR VAR STRING VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
def soll(Var, m):
if m % 2 == 1:
answer = []
for k in range(m + 1):
if k % 2 == 0:
answer.append(Var[0] + 1)
else:
answer.append(Var[1] + 1)
print("YES")
print(*answer)
return
A = []
B = []
for i in range(m // 2 + 1):
if i % 2 == 0:
curA = 1
curB = 1
elif i % 4 == 1:
curA = 0
curB = 2
else:
curA = 2
curB = 0
A.append(Var[curA])
B.append(Var[curB])
answer = list(reversed(B)) + A[1:]
print("YES")
for elem in answer:
print(elem + 1, sep=" ")
return
def sol():
n, m = map(int, sys.stdin.readline().split())
if m % 2 == 1:
for i in range(n):
input()
answer = [(i % 2 + 1) for i in range(m + 1)]
print("YES")
print(*answer)
return
if n == 2:
x = sys.stdin.readline()[1]
y = sys.stdin.readline()[0]
if x != y:
print("NO")
return
else:
answer = [(i % 2 + 1) for i in range(m + 1)]
print("YES")
print(*answer)
return
G = [[], [], []]
for i in range(3):
G[i] = sys.stdin.readline()[:3]
for i in range(n - 3):
sys.stdin.readline()
for i in range(3):
for j in range(i):
if G[i][j] == G[j][i]:
answer = []
for k in range(m + 1):
if k % 2 == 0:
answer.append(i + 1)
else:
answer.append(j + 1)
print("YES")
print(*answer)
return
if G[0][1] == G[1][2]:
soll([0, 1, 2], m)
return
if G[2][0] == G[0][1]:
soll([2, 0, 1], m)
return
soll([1, 2, 0], m)
return
for iter in range(int(sys.stdin.readline())):
sol() | IMPORT FUNC_DEF IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING RETURN FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR LIST LIST LIST LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER VAR RETURN IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER VAR RETURN EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, m = map(int, input().split())
S = [input()[:-1] for _ in range(n)]
x, y = -1, -1
for i in range(n):
for j in range(i + 1, n):
if S[i][j] == S[j][i]:
x, y = i, j
if m % 2 == 1 and x == -1:
x, y = 0, 1
if x != -1:
ans = [x + 1]
for i in range(m):
if i % 2 == 0:
ans.append(y + 1)
else:
ans.append(x + 1)
print("YES")
print(*ans)
continue
x, y, z = -1, -1, -1
for i in range(n):
ax, bx, az, bz = -1, -1, -1, -1
for j in range(n):
if S[j][i] == "a":
ax = j
elif S[j][i] == "b":
bx = j
if S[i][j] == "a":
az = j
elif S[i][j] == "b":
bz = j
if ax != -1 and az != -1:
x, y, z = ax, i, az
break
elif bx != -1 and bz != -1:
x, y, z = bx, i, bz
break
if x != -1:
print("YES")
if m % 4 == 0:
print(*([y + 1] + [x + 1, y + 1, z + 1, y + 1] * (m // 4)))
else:
print(
*([z + 1, y + 1, x + 1] + [y + 1, z + 1, y + 1, x + 1] * ((m - 2) // 4))
)
else:
print("NO") | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | for _ in range(int(input())):
n, m = map(int, input().split())
if m % 2 == 1:
for i in range(n):
s = input()
print("YES")
for i in range(m + 1):
print(i % 2 + 1, end=" ")
print()
else:
A = []
for i in range(n):
B = input()
A.append(B)
k1, k2 = -1, -1
for i in range(n):
for j in range(i):
if A[i][j] == A[j][i]:
k1, k2 = i, j
break
if k1 != -1:
break
if k1 == -1:
if n == 2:
print("NO")
else:
x, y, z = -1, -1, -1
if A[0][1] == A[0][2]:
x = 1
if A[1][2] == A[0][1]:
y = 2
z = 3
else:
z = 2
y = 3
elif A[1][0] == A[1][2]:
x = 2
if A[0][2] == A[1][0]:
y = 1
z = 3
else:
z = 3
y = 1
else:
x = 3
if A[0][1] == A[2][0]:
y = 1
z = 2
else:
z = 2
y = 1
if m % 4 == 2:
print("YES")
print(x, end=" ")
for i in range(m // 4):
print(y, x, end=" ")
print(y, z, end=" ")
for i in range(m // 4):
print(y, z, end=" ")
print()
else:
print("YES")
print(y, x, end=" ")
for i in range(m // 4 - 1):
print(y, x, end=" ")
print(y, end=" ")
for i in range(m // 4):
print(z, y, end=" ")
print()
else:
print("YES")
for i in range(m + 1):
if i % 2 == 0:
print(k1 + 1, end=" ")
else:
print(k2 + 1, end=" ")
print() | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
def main():
t = int(input())
allAns = []
for _ in range(t):
n, m = readIntArr()
edges = []
for __ in range(n):
edges.append(input())
ok = False
if m % 2 == 1:
ok = True
ans = [0, 1] * ((m + 1) // 2)
else:
for i in range(n):
for j in range(n):
if i != j and edges[i][j] == edges[j][i]:
ok = True
ans = [i]
currIsJ = True
for __ in range(m):
if currIsJ:
ans.append(j)
else:
ans.append(i)
currIsJ = not currIsJ
break
if ok:
break
if not ok:
aOutIndexes = [[] for _ in range(n)]
for i in range(n):
for j in range(n):
if edges[i][j] == "a":
aOutIndexes[i].append(j)
for i in range(n):
for j in aOutIndexes[i]:
if len(aOutIndexes[j]) > 0:
vertex1 = i
vertex2 = j
vertex3 = aOutIndexes[j][0]
ok = True
break
if ok:
break
if ok:
if m // 2 % 2 == 0:
ans = [vertex2] + [vertex3, vertex2, vertex1, vertex2] * (
m // 4
)
else:
ans = [vertex1, vertex2, vertex3] + [
vertex2,
vertex1,
vertex2,
vertex3,
] * ((m - 2) // 4)
if ok:
allAns.append(["YES"])
for i in range(len(ans)):
ans[i] += 1
allAns.append(ans)
else:
allAns.append(["NO"])
multiLineArrayOfArraysPrint(allAns)
return
input = lambda: sys.stdin.readline().rstrip("\r\n")
def oneLineArrayPrint(arr):
print(" ".join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print("\n".join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print("\n".join([" ".join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
inf = float("inf")
MOD = 10**9 + 7
main() | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR IF VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP LIST VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR VAR BIN_OP LIST VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR LIST STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST STRING EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | def find():
n, m = map(int, input().split())
g = []
has = []
for j in range(n):
tmp = []
tmp1 = [-1] * 2
for p, k in enumerate(input()):
tmp.append(k)
if k != "*":
tmp1[ord(k) - ord("a")] = p
g.append(tmp)
has.append(tmp1)
if m % 2 != 0:
ans = " ".join([str(j % 2 + 1) for j in range(m + 1)])
return "Yes\n" + ans
for j in range(n):
for k in range(j + 1, n):
if g[k][j] == g[j][k]:
res = j + 1, k + 1
ans = " ".join([str(res[p % 2]) for p in range(m + 1)])
return "Yes\n" + ans
for j in range(n):
for k in range(n):
if g[j][k] != "*" and has[k][ord(g[j][k]) - ord("a")] != -1:
x = j + 1
y = k + 1
z = has[k][ord(g[j][k]) - ord("a")] + 1
res = x, y, z, y
dif = 0 if m // 2 % 2 != 0 else 1
ans = " ".join([str(res[(p + dif) % 4]) for p in range(m + 1)])
return "Yes\n" + ans
return "No"
for i in range(int(input())):
print(find()) | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP STRING VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP STRING VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP STRING VAR RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | for _ in range(int(input())):
n, m = map(int, input().split())
Edges = [input() for _ in range(n)]
if m & 1:
print("YES")
L = [(1 + (i & 1)) for i in range(m + 1)]
print(*L)
else:
flag = True
for i in range(n - 1):
for j in range(i + 1, n):
if Edges[i][j] == Edges[j][i]:
print("YES")
L = [i + 1, j + 1] * (m // 2) + [i + 1]
print(*L)
flag = False
break
if not flag:
break
if flag:
flag2 = False
for i in range(n):
S = set()
E = Edges[i]
a = ""
for j in range(n):
if i - j:
if not a:
a = E[j]
J = j + 1
elif E[j] != a:
I, K = i + 1, j + 1
flag2 = True
break
if flag2:
break
if flag2:
print("YES")
m //= 2
L0 = [J, I, K, I] * (m // 4) + [J, I, K, I][: m % 4]
L1 = [K, I, J, I] * (m // 4) + [K, I, J, I][: m % 4]
L = L0[::-1] + [I] + L1
print(*L)
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR IF VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR EXPR FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR VAR BIN_OP VAR NUMBER LIST VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR VAR BIN_OP VAR NUMBER LIST VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER LIST VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n, m = map(int, input().split())
s = [list(input().rstrip()) for _ in range(n)]
if n == 2:
if m % 2 or s[0][1] == s[1][0]:
print("YES")
ans = [(i % 2 + 1) for i in range(m + 1)]
print(*ans)
else:
print("NO")
continue
f = 0
ans = []
for i in range(n):
for j in range(i + 1, n):
if s[i][j] == s[j][i] or m % 2:
f = 1
ans = [(i + 1 if k % 2 else j + 1) for k in range(m + 1)]
break
if f:
break
if not f:
a, b, c = s[0][1], s[1][2], s[2][0]
if a == b == c:
ans = [(i % 3 + 1) for i in range(m + 1)]
elif a == b:
if not m % 4:
s = 2
c = 1
else:
s = 1
c = 1
ans = []
for i in range(m + 1):
ans.append(s)
s += c
if s == 3:
c = -1
if s == 1:
c = 1
elif b == c:
if not m % 4:
s = 3
c = 1
else:
s = 2
c = 1
ans = []
for i in range(m + 1):
ans.append(s if s <= 3 else s % 3)
s += c
if s == 4:
c = -1
if s == 2:
c = 1
else:
if not m % 4:
s = 4
c = 1
else:
s = 3
c = 1
ans = []
for i in range(m + 1):
ans.append(s if s <= 3 else s % 3)
s += c
if s == 5:
c = -1
if s == 3:
c = 1
print("YES")
print(*ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR IF VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | rn = lambda: int(input())
rns = lambda: map(int, input().split())
rl = lambda: list(map(int, input().split()))
rs = lambda: input()
YN = lambda x: print("YES") if x else print("NO")
pl = lambda l: print(" ".join(list(map(str, l))))
def d(a):
d = {}
for i in a:
if i in d:
d[i] += 1
else:
d[i] = 1
return d
for _ in range(rn()):
n, m = rns()
grid = []
for i in range(n):
grid.append(rs())
if m % 2 == 1:
print("YES")
print((m + 1) // 2 * "1 2 ")
else:
flag = False
pair = ""
for i in range(n):
for j in range(n):
if i != j and grid[i][j] == grid[j][i]:
flag = True
pair = str(i + 1) + " " + str(j + 1) + " "
break
if flag:
print("YES")
print(m // 2 * pair + pair.split()[0])
elif n == 2:
print("NO")
else:
triplet = []
for i in range(n):
if len(set(grid[i])) == 3:
triplet = [i + 1, grid[i].index("a") + 1, grid[i].index("b") + 1]
break
a = str(triplet[0]) + " " + str(triplet[1]) + " "
b = str(triplet[2]) + " " + str(triplet[0]) + " "
print("YES")
if m // 2 % 2 == 0:
print(m // 4 * a + str(triplet[0]) + " " + m // 4 * b)
else:
m -= 2
print(
str(triplet[1])
+ " "
+ m // 4 * a
+ str(triplet[0])
+ " "
+ m // 4 * b
+ str(triplet[2])
) | 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR BIN_OP VAR NUMBER STRING IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR STRING NUMBER BIN_OP FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR STRING IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER STRING BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER STRING BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | def solveTestCase():
nm = [int(i) for i in input().split()]
G = []
for i in range(nm[0]):
G.append(input())
if nm[1] % 2 == 1:
print("YES")
for i in range(nm[1] // 2 + 1):
print("1 2", end=" ")
print()
return
mypaire = 0, 0
for i in range(nm[0]):
if mypaire[0] != 0:
break
for j in range(nm[0]):
if i != j and G[i][j] == G[j][i]:
mypaire = i + 1, j + 1
break
if mypaire[0] != 0:
print("YES")
for i in range(nm[1] // 2):
print(str(mypaire[0]) + " " + str(mypaire[1]), end=" ")
print(str(mypaire[0]))
return
mytriple = 0, 0, 0
for i in range(nm[0]):
if mytriple[0] != 0:
break
for j in range(nm[0]):
if mytriple[0] != 0:
break
if i == j:
continue
for k in range(nm[0]):
if j == k or k == i:
continue
if G[i][j] == G[j][k]:
mytriple = i + 1, j + 1, k + 1
break
if mytriple[0] != 0:
print("YES")
if nm[1] % 4 == 0:
print(str(mytriple[1]), end=" ")
for i in range(nm[1] // 2 - 1):
print(mytriple[i % 2], end=" ")
else:
for i in range(nm[1] // 2):
print(mytriple[i % 2], end=" ")
print(str(mytriple[1]) + " " + str(mytriple[2]), end=" ")
for i in range(nm[1] // 2 - 1):
print(mytriple[i % 2 + 1], end=" ")
print()
else:
print("NO")
t = int(input().split()[0])
while t > 0:
t -= 1
solveTestCase() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR RETURN ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | t = int(input())
while t > 0:
t -= 1
n, m = [int(op) for op in input().split()]
mt = [input() for i in range(n)]
if m % 2 == 1:
print("YES")
for i in range(m + 1):
print(1, end=" ") if i % 2 == 0 else print(2, end=" ")
print()
else:
flag = False
f2 = False
for i in range(n):
j = i + 1
while j < n:
if mt[i][j] == mt[j][i]:
flag = True
a, b = i + 1, j + 1
break
j += 1
f2 = False
if not flag:
for i in range(n):
if "a" in mt[i] and "b" in mt[i]:
f2 = True
a, b, c = mt[i].find("a") + 1, mt[i].find("b") + 1, i + 1
break
if flag:
print("YES")
for i in range(m + 1):
print(a, end=" ") if i % 2 == 0 else print(b, end=" ")
print()
elif f2:
print("YES")
if int(m / 2) % 2 == 1:
for i in range(int(m / 2)):
print(a, end=" ") if i % 2 == 0 else print(c, end=" ")
for i in range(int(m / 2) + 1):
print(c, end=" ") if i % 2 == 0 else print(b, end=" ")
else:
for i in range(int(m / 2)):
print(c, end=" ") if i % 2 == 0 else print(b, end=" ")
for i in range(int(m / 2) + 1):
print(c, end=" ") if i % 2 == 0 else print(a, end=" ")
print()
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR NUMBER STRING FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR IF STRING VAR VAR STRING VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING NUMBER BIN_OP FUNC_CALL VAR VAR STRING NUMBER BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
input = sys.stdin.readline
ansl = []
for _ in range(int(input())):
n, k = map(int, input().split())
sl = []
for _ in range(n):
sl.append(list(input()))
if n == 2:
if k % 2 == 1 or sl[0][1] == sl[1][0]:
ansl.append([1, 2] * ((k - 1) // 2 + 1))
if k % 2 == 0:
ansl[-1].append(1)
else:
ansl.append([])
continue
ans = []
if k % 2 == 1:
ans.extend([1, 2] * (k // 2 + 1))
ansl.append(ans)
continue
if sl[0][1] == sl[1][0]:
ans.extend([1, 2] * (k // 2))
ans.append(1)
ansl.append(ans)
continue
if sl[1][2] == sl[2][1]:
ans.extend([2, 3] * (k // 2))
ans.append(2)
ansl.append(ans)
continue
if sl[2][0] == sl[0][2]:
ans.extend([3, 1] * (k // 2))
ans.append(3)
ansl.append(ans)
continue
if sl[0][1] == sl[1][2] and sl[1][2] == sl[2][0]:
for i in range(k + 1):
ans.append(i % 3 + 1)
ansl.append(ans)
continue
abi = -1
aai = -1
bbi = -1
for i in range(3):
if sl[i][(i + 1) % 3] == "a" and sl[i][(i + 2) % 3] == "a":
aai = i + 1
elif sl[i][(i + 1) % 3] == "b" and sl[i][(i + 2) % 3] == "b":
bbi = i + 1
else:
abi = i + 1
if k % 4 == 0:
ans.extend([abi, aai] * (k // 4))
ans.extend([abi, bbi] * (k // 4))
ans.append(abi)
else:
loop = k // 2 // 2 + 1
ans.extend([aai, abi] * loop)
ans.extend([bbi, abi] * loop)
ans = ans[:-1]
ansl.append(ans)
for row in ansl:
if row:
print("YES")
print(*row)
else:
print("NO") | IMPORT ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER STRING VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER STRING VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
N, M = map(int, input().split())
e = [list(input())[:-1] for _ in range(N)]
res = [0] * (M + 1)
for i in range(N):
for j in range(N):
if i == j:
continue
if e[i][j] == e[j][i]:
for k in range(M + 1):
if k % 2:
res[k] = i + 1
else:
res[k] = j + 1
break
else:
continue
break
if res[0]:
print("YES")
print(*res)
continue
if M % 2:
for k in range(M + 1):
if k % 2:
res[k] = 1
else:
res[k] = 2
print("YES")
print(*res)
continue
else:
for i in range(N):
a = -1
b = -1
for j in range(N):
if e[i][j] == "a":
a = j
if e[i][j] == "b":
b = j
if a >= 0 and b >= 0:
if M % 4 == 0:
for k in range(M + 1):
if k % 4 == 0:
res[k] = i + 1
elif k % 4 == 1:
res[k] = a + 1
elif k % 4 == 2:
res[k] = i + 1
elif k % 4 == 3:
res[k] = b + 1
else:
for k in range(M // 2 + 1):
if k % 2 == 0:
res[k] = a + 1
elif k % 2 == 1:
res[k] = i + 1
for k in range(M // 2 + 1, M + 1):
if k % 2 == 0:
res[k] = b + 1
elif k % 2 == 1:
res[k] = i + 1
print("YES")
print(*res)
break
else:
print("NO") | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
input = lambda: sys.stdin.readline().rstrip()
T = int(input())
for _ in range(T):
N, M = map(int, input().split())
X = [
[(0 if a == "a" else 1 if a == "b" else -1) for a in input()] for _ in range(N)
]
if M % 2:
print("YES")
print(*([1, 2] * (M // 2 + 1)))
continue
f = 0
for i in range(N):
for j in range(i):
if X[i][j] == X[j][i]:
print("YES")
print(*([i + 1] + [j + 1, i + 1] * (M // 2)))
f = 1
break
if f:
break
if f:
continue
if N == 2:
print("NO")
continue
if X[0][1] == X[1][2]:
i, j, k = 1, 2, 3
elif X[1][2] == X[2][0]:
i, j, k = 2, 3, 1
else:
i, j, k = 3, 1, 2
print("YES")
if M % 4:
print(*([i] + [j, i] * (M // 4) + [j, k] * (M // 4 + 1)))
else:
print(*([j, i] * (M // 4) + [j, k] * (M // 4) + [j])) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR STRING NUMBER VAR STRING NUMBER NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP LIST VAR BIN_OP LIST VAR VAR BIN_OP VAR NUMBER BIN_OP LIST VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP LIST VAR VAR BIN_OP VAR NUMBER BIN_OP LIST VAR VAR BIN_OP VAR NUMBER LIST VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | def print_ez(node1, node2, m):
print("YES")
p = []
for i in range(m + 1):
if i % 2:
p.append(node2)
else:
p.append(node1)
print(*p)
def print_hard(center, node1, node2, m):
print("YES")
p = [center]
for i in range(m // 2):
if i % 2:
p.append(center)
else:
p.append(node1)
for i in range(m // 2):
if i % 2:
p.append(center)
else:
p.append(node2)
print(*p)
def print_another(center, node1, node2, m):
print("YES")
p = []
for i in range(m // 2):
if i % 2:
p.append(center)
else:
p.append(node1)
p.append(center)
for i in range(m // 2):
if i % 2:
p.append(center)
else:
p.append(node2)
print(*p)
def main():
t = int(input())
for _ in range(t):
n, m = map(int, input().split())
l1 = input()
l2 = input()
if n != 2:
l3 = input()
for i in range(max(0, n - 3)):
s = input()
if n == 2:
if l1[1] == l2[0]:
print_ez(1, 2, m)
elif m % 2:
print_ez(1, 2, m)
else:
print("NO")
continue
if l1[1] == l2[0]:
print_ez(1, 2, m)
elif l1[2] == l3[0]:
print_ez(1, 3, m)
elif l2[2] == l3[1]:
print_ez(2, 3, m)
elif m % 2:
print_ez(1, 2, m)
elif m % 4:
if l1[:3] != "*aa" and l1[:3] != "*bb":
print_another(1, 2, 3, m)
elif l2[:3] != "a*a" and l2[:3] != "b*b":
print_another(2, 1, 3, m)
else:
print_another(3, 2, 1, m)
elif l1[:3] != "*aa" and l1[:3] != "*bb":
print_hard(1, 2, 3, m)
elif l2[:3] != "a*a" and l2[:3] != "b*b":
print_hard(2, 1, 3, m)
else:
print_hard(3, 1, 2, m)
main() | FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR IF BIN_OP VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR IF VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR IF VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR IF VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR EXPR FUNC_CALL VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | t = int(input())
for _ in range(t):
n, m = [int(x) for x in input().split()]
grid = [input() for _ in range(n)]
if m % 2 == 1:
print("YES")
print("1 2 " * ((m + 1) // 2))
continue
for i in range(n):
found = False
for j in range(n):
if j == i:
continue
for k in range(n):
if k == i:
continue
if grid[j][i] == grid[i][k]:
found = True
break
if found:
break
if found:
break
else:
print("NO")
continue
i += 1
j += 1
k += 1
if j == k:
print("YES")
print(i, f"{j} {i} " * (m // 2))
else:
print("YES")
if m // 2 % 2 == 0:
print(f"{k} {j} " * (m // 4) + f"{i} " + f"{k} {j} " * (m // 4))
else:
print(
j,
f"{k} {j} " * ((m - 2) // 4)
+ f"{i} "
+ f"{k} {j} " * ((m - 2) // 4)
+ f"{k}",
) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR EXPR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR STRING VAR STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR STRING VAR STRING BIN_OP VAR NUMBER VAR STRING BIN_OP VAR STRING VAR STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR STRING VAR STRING BIN_OP BIN_OP VAR NUMBER NUMBER VAR STRING BIN_OP VAR STRING VAR STRING BIN_OP BIN_OP VAR NUMBER NUMBER VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | from itertools import chain, cycle, islice
from sys import stdin
readline = stdin.readline
def read_ints():
return map(int, readline().split())
def read_string():
return readline()[:-1]
def f(graph, m):
assert n >= 2
assert m >= 1
for u in range(n):
for v in range(u + 1, n):
if graph[u][v] == graph[v][u]:
return islice(cycle([u, v]), m + 1)
if m % 2 != 0:
return islice(cycle([0, 1]), m + 1)
g = [("a" in row, "b" in row) for row in graph]
for u in range(n):
for v in range(n):
if u == v:
continue
ch = graph[u][v]
if g[v][ord(ch) - ord("a")]:
w = graph[v].index(ch)
base = [u, v, w]
v_need = m + 1 - 3
v_per_half = v_need // 2
fst_half = list(islice(cycle([v, u]), v_per_half))[::-1]
snd_half = islice(cycle([v, w]), v_per_half)
return chain(fst_half, base, snd_half)
raise ValueError
(t_n,) = read_ints()
for i_t in range(t_n):
n, m = read_ints()
graph = [read_string() for i_row in range(n)]
try:
path = tuple(f(graph, m))
except ValueError:
print("NO")
else:
print("YES")
print(*[(v + 1) for v in path]) | ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR LIST NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR STRING VAR STRING VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
def read_ints():
return [int(i) for i in sys.stdin.readline().strip().split()]
def read_int():
return int(sys.stdin.readline().strip())
def check_and_output(lines, nodes):
route = [lines[int(i) - 1][int(j) - 1] for i, j in zip(nodes[:-1], nodes[1:])]
in_reverse = list(reversed(route))
print("YES")
print(" ".join(nodes))
def nodes_for_3graph(length, start, middle, end):
return [
(start if (d + length // 2) % 2 else middle) for d in range(length // 2 + 1)
] + [(middle if d % 2 else end) for d in range(length // 2)]
def output_for_3graph(lines, length, start, middle, end):
nodes = nodes_for_3graph(length, start, middle, end)
check_and_output(lines, nodes)
def solve(n, length, lines):
if length % 2 == 1:
nodes = [("1" if d % 2 else "2") for d in range(length + 1)]
check_and_output(lines, nodes)
return
for i in range(n):
for j in range(n):
if i != j and lines[i][j] == lines[j][i]:
nodes = [str(i + 1 if d % 2 else j + 1) for d in range(length + 1)]
check_and_output(lines, nodes)
return
if n == 2:
print("NO")
return
if length == 2:
for i in range(n):
for j in range(n):
for k in range(n):
if i != j and j != k and i != k:
if lines[i][j] == lines[j][k]:
nodes = [str(i + 1), str(j + 1), str(k + 1)]
check_and_output(lines, nodes)
return
print("NO")
return
if lines[0][1] == lines[1][2] == lines[2][0]:
nodes = [str(d % 3 + 1) for d in range(length + 1)]
check_and_output(lines, nodes)
return
if lines[0][2] == lines[2][1] == lines[1][0]:
nodes = [str(3 - d % 3) for d in range(length + 1)]
check_and_output(lines, nodes)
return
if lines[0][1] == lines[2][0]:
output_for_3graph(lines, length, "2", "1", "3")
return
if lines[0][1] == lines[1][2]:
output_for_3graph(lines, length, "1", "2", "3")
return
if lines[0][2] == lines[2][1]:
output_for_3graph(lines, length, "1", "3", "2")
return
ncases = read_int()
lines = []
for i in range(ncases):
lines = []
n, length = read_ints()
skip = False
for j in range(n):
lines.append(sys.stdin.readline().strip())
solve(n, length, lines) | IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER STRING STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN EXPR FUNC_CALL VAR STRING RETURN IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR STRING STRING STRING RETURN IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR STRING STRING STRING RETURN IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR STRING STRING STRING RETURN ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | for nt in range(int(input())):
n, m = map(int, input().split())
edge = []
for i in range(n):
edge.append(list(input()))
found = 0
for i in range(n):
for j in range(n):
if i == j:
continue
if edge[i][j] == edge[j][i]:
found = [i, j]
if found:
print("YES")
j = 0
for i in range(m):
print(found[j] + 1, end=" ")
j = 1 - j
print(found[j] + 1)
continue
if m % 2:
print("YES")
for i in range(m + 1):
print(i % 2 + 1, end=" ")
print()
continue
if n == 2:
print("NO")
continue
print("YES")
if m == 2:
if edge[0][1] == edge[1][2]:
ans = [1, 2, 3]
elif edge[1][2] == edge[2][0]:
ans = [2, 3, 1]
elif edge[2][0] == edge[0][1]:
ans = [3, 1, 2]
print(*ans)
continue
if edge[0][1] == edge[1][2]:
i, j, k = 1, 2, 3
elif edge[1][2] == edge[2][0]:
i, j, k = 2, 3, 1
else:
i, j, k = 3, 1, 2
if m % 4:
print(*([i] + [j, i] * (m // 4) + [j, k] * (m // 4 + 1)))
else:
print(*([j, i] * (m // 4) + [j, k] * (m // 4) + [j])) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP LIST VAR BIN_OP LIST VAR VAR BIN_OP VAR NUMBER BIN_OP LIST VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP LIST VAR VAR BIN_OP VAR NUMBER BIN_OP LIST VAR VAR BIN_OP VAR NUMBER LIST VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
input = lambda: sys.stdin.readline().rstrip()
for _ in range(int(input())):
n, m = map(int, input().split())
s = [input() for i in range(n)]
same = False
u, t = -1, -1
for i in range(n):
for j in range(i + 1, n):
if s[i][j] == s[j][i]:
u, t = i, j
same = True
if same:
print("YES")
ans = [(0) for i in range(m + 1)]
for i in range(m + 1):
if i % 2 == 0:
ans[i] = u + 1
else:
ans[i] = t + 1
print(*ans)
continue
elif m % 2 == 1:
print("YES")
ans = [(0) for i in range(m + 1)]
for i in range(m + 1):
if i % 2 == 0:
ans[i] = 1
else:
ans[i] = 2
print(*ans)
continue
A = [(-1) for i in range(n)]
B = [(-1) for i in range(n)]
for i in range(n):
for j in range(n):
if s[i][j] == "a":
A[i] = j
elif s[i][j] == "b":
B[i] = j
u, t, mid = -1, -1, -1
for i in range(n):
for j in range(n):
if s[i][j] == "a" and A[j] != -1:
u, t, mid = i, A[j], j
elif s[i][j] == "b" and B[j] != -1:
u, t, mid = i, B[j], j
if u != -1:
print("YES")
k = m // 2
if (k - 1) % 2 == 1:
L = k // 2
ans = [mid + 1, u + 1] * L + [mid + 1] + [t + 1, mid + 1] * L
else:
L = k // 2
ans = [u + 1, mid + 1] * L + [u + 1, mid + 1, t + 1] + [mid + 1, t + 1] * L
print(*ans)
assert len(ans) == m + 1
continue
else:
print("NO") | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR STRING VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | for testcase in range(int(input())):
arr = [int(x) for x in input().split()]
n, m = arr[0], arr[1]
graph = []
for i in range(n):
graph.append(input())
if m & 1 == 1:
print("YES")
for i in range(m + 1):
if i & 1 == 1:
print(2, end=" ")
else:
print(1, end=" ")
print()
continue
flag = False
for i in range(n):
for j in range(n):
if i != j:
if graph[i][j] == graph[j][i]:
print("YES")
for k in range(m + 1):
if k & 1 == 1:
print(j + 1, end=" ")
else:
print(i + 1, end=" ")
print()
flag = True
break
if flag:
break
else:
flag = False
for i in range(n):
for j in range(n):
for k in range(n):
if i != j and j != k and k != i:
if graph[i][j] == graph[j][k]:
print("YES")
if m // 2 % 2 == 1:
r = m // 2
for z in range(r + 1):
if z & 1 == 1:
print(j + 1, end=" ")
else:
print(i + 1, end=" ")
for z in range(r):
if z & 1 == 1:
print(j + 1, end=" ")
else:
print(k + 1, end=" ")
else:
r = m // 2
for z in range(r + 1):
if z & 1 == 1:
print(k + 1, end=" ")
else:
print(j + 1, end=" ")
for z in range(r):
if z & 1 == 1:
print(j + 1, end=" ")
else:
print(i + 1, end=" ")
print()
flag = True
break
if flag:
break
if flag:
break
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR IF VAR EXPR FUNC_CALL VAR STRING |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
max_int = 2147483648
min_int = -max_int
t = int(input())
for _t in range(t):
n, m = map(int, sys.stdin.readline().split())
m += 1
M = []
s_out = []
for nn in range(n):
s = input()
M.append(s)
s_out.append(set(list(s)))
if not m % 2:
print("YES")
print(("1 2 " * (m // 2)).strip())
continue
has_to_break = False
same = ()
s_in = [set() for _ in range(n)]
for i in range(n):
for j in range(i + 1, n):
s_in[j].add(M[i][j])
s_in[i].add(M[j][i])
if M[i][j] == M[j][i]:
same = i, j
has_to_break = True
break
if has_to_break:
break
if same:
print("YES")
tmp = str(same[0] + 1) + " " + str(same[1] + 1) + " "
tmp *= m // 2
if m % 2:
tmp += str(same[0] + 1)
else:
tmp = tmp.strip()
print(tmp)
continue
has_to_break = False
for i in range(n):
for ch in ("a", "b"):
p1, p2, p3 = -1, -1, -1
if ch in s_in[i] and ch in s_out[i]:
p2 = i
else:
continue
for j in range(n):
if M[p2][j] == ch:
p3 = str(j + 1)
break
for j in range(n):
if M[j][p2] == ch:
p1 = str(j + 1)
break
if p1 == -1 or p3 == -1:
continue
p2 = str(p2 + 1)
if not m % 2:
out = (p1 + " " + p2 + " ") * (m // 2)
else:
h = m // 2
if h % 2:
out = (
(p1 + " " + p2 + " ") * (h // 2)
+ p1
+ " "
+ p2
+ " "
+ p3
+ (" " + p2 + " " + p3) * (h // 2)
)
else:
out = (
(p2 + " " + p1 + " ") * (h // 2)
+ p2
+ (" " + p3 + " " + p2) * (h // 2)
)
print("YES")
print(out.strip())
has_to_break = True
break
if has_to_break:
break
else:
print("NO") | IMPORT ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER STRING FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER STRING VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR STRING STRING ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR STRING VAR STRING BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR STRING VAR STRING BIN_OP VAR NUMBER VAR STRING VAR STRING VAR BIN_OP BIN_OP BIN_OP BIN_OP STRING VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR STRING VAR STRING BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP STRING VAR STRING VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
def input():
return sys.stdin.readline().rstrip()
def slv():
n, m = map(int, input().split())
G = [([-1] * n) for i in range(n)]
for i in range(n):
s = input()
for j in range(n):
if s[j] == "b":
G[i][j] = 1
elif s[j] == "a":
G[i][j] = 0
for i in range(n):
for j in range(n):
if G[i][j] == G[j][i] and G[i][j] >= 0:
print("YES")
ans = []
for _ in range(m + 1):
if _ % 2 == 0:
ans.append(i + 1)
else:
ans.append(j + 1)
print(*ans)
return
if m % 2 != 0:
ans = []
for _ in range(m + 1):
if _ % 2 == 0:
ans.append(1)
else:
ans.append(2)
print("YES")
print(*ans)
return
for y in range(n):
lnode0 = []
lnode1 = []
rnode0 = []
rnode1 = []
for x in range(n):
if G[x][y] == 0:
lnode0.append(x)
if G[x][y] == 1:
lnode1.append(x)
if G[y][x] == 0:
rnode0.append(x)
if G[y][x] == 1:
rnode1.append(x)
if lnode0 and rnode0:
X = lnode0[0] + 1
Y = y + 1
Z = rnode0[0] + 1
if m % 4 == 0:
print("YES")
ans = [Y, Z, Y, X] * (m // 4) + [Y]
print(*ans)
else:
print("YES")
ans = [X, Y, Z, Y] * (m // 4) + [X, Y, Z]
print(*ans)
return
elif lnode1 and rnode1:
X = lnode1[0] + 1
Y = y + 1
Z = rnode1[0] + 1
if m % 4 == 0:
print("YES")
ans = [Y, Z, Y, X] * (m // 4) + [Y]
print(*ans)
else:
print("YES")
ans = [X, Y, Z, Y] * (m // 4) + [X, Y, Z]
print(*ans)
return
print("NO")
return
def main():
t = int(input())
for i in range(t):
slv()
return
main() | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR VAR BIN_OP VAR NUMBER LIST VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR VAR BIN_OP VAR NUMBER LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR VAR BIN_OP VAR NUMBER LIST VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR VAR BIN_OP VAR NUMBER LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR STRING RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n, m = map(int, input().split())
s = [input() for _ in range(n)]
flg = 0
for i in range(n):
for j in range(i + 1, n):
if s[i][j] == s[j][i]:
print("YES")
res = [i + 1] * (m + 1)
for k in range(1, m + 1, 2):
res[k] = j + 1
print(*res)
flg = 1
break
if flg:
break
else:
if m % 2 == 1:
print("YES")
res = [1] * (m + 1)
for k in range(1, m + 1, 2):
res[k] = 2
print(*res)
else:
p = [-1] * n
q = [-1] * n
for i in range(n):
for j in range(n):
if s[i][j] == "a":
p[i] = j + 1
q[j] = i + 1
for i in range(n):
if p[i] > 0 and q[i] > 0:
print("YES")
res = [i + 1] * (m + 1)
if m % 4 == 2:
for k in range(0, m + 1, 4):
res[k] = q[i]
for k in range(2, m + 1, 4):
res[k] = p[i]
else:
for k in range(3, m + 1, 4):
res[k] = q[i]
for k in range(1, m + 1, 4):
res[k] = p[i]
print(*res)
break
else:
print("NO") | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR 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 VAR IF VAR VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | T = int(input())
for t in range(T):
n, m = tuple([int(x) for x in input().split()])
ss = []
for i in range(n):
ss.append(input().strip())
if n == 2:
if ss[0][1] != ss[1][0] and m % 2 == 0:
print("NO")
else:
print("YES")
print(" ".join([str(i % 2 + 1) for i in range(m + 1)]))
else:
print("YES")
two_nodes = None
for i in range(n):
for j in range(i + 1, n):
if ss[i][j] == ss[j][i]:
two_nodes = i, j
break
if two_nodes is not None:
break
if two_nodes is not None:
solution = [str(two_nodes[i % 2] + 1) for i in range(m + 1)]
print(" ".join(solution))
continue
same_label_pos = []
for i in range(3):
if ss[i][(i + 1) % 3] == ss[i][(i + 2) % 3]:
same_label_pos.append(i)
if len(same_label_pos) == 0:
solution = [str(i % 3 + 1) for i in range(m + 1)]
print(" ".join(solution))
continue
elif m % 2 == 1:
solution = [str(i % 2 + 1) for i in range(m + 1)]
print(" ".join(solution))
continue
else:
diff_label_pos = None
for i in range(3):
if i in same_label_pos:
continue
else:
diff_label_pos = i
break
solution = (
[str(same_label_pos[i % 2] + 1) for i in range(m // 2)]
+ [str(diff_label_pos + 1)]
+ [str(same_label_pos[(i + m // 2 % 2) % 2] + 1) for i in range(m // 2)]
)
print(" ".join(solution))
continue | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NONE IF VAR NONE ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | T = int(input())
for _ in range(T):
n, m = map(int, input().split())
A = [input() for i in range(n)]
if m % 2 != 0:
print("YES")
for i in range(m + 1):
if i % 2 == 0:
print(1, end=" ")
else:
print(2, end=" ")
print()
else:
x, y = -1, -1
for i in range(n):
for j in range(i + 1, n):
if i != j and A[i][j] == A[j][i]:
x, y = i + 1, j + 1
break
if x != -1:
print("YES")
for i in range(m + 1):
if i % 2 == 0:
print(x, end=" ")
else:
print(y, end=" ")
print()
else:
x, y, z = -1, -1, -1
for i in range(n):
G = {}
for j in range(n):
if i != j:
G[A[i][j]] = j + 1
cnt = 0
for j in G:
cnt = cnt + 1
if cnt == 2:
x = G["a"]
z = G["b"]
y = i + 1
break
if x == -1:
print("NO")
else:
print("YES")
if m % 4 == 0:
A1 = [z, y, x, y]
ans = [y]
v = m // 4
for i in range(v):
ans.extend(A1)
print(*ans)
else:
ans = [x, y, z]
if m == 2:
print(*ans)
else:
A1 = [y, x, y, z]
v = (m - 2) // 4
for i in range(v):
ans.extend(A1)
print(*ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST VAR VAR VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | for _ in range(int(input())):
n, m = map(int, input().split())
g = [input() for i in range(n)]
if m % 2 == 1:
print("YES")
print("1 2 " * (m // 2 + 1))
continue
ans = []
for v in range(n):
for u1 in range(n):
for u2 in range(n):
if (
v != u1
and v != u2
and g[v][u1] == g[u2][v]
and g[u1][v] == g[v][u2]
):
ans = (
([u1 + 1, v + 1] * m)[: m // 2][::-1]
+ [v + 1]
+ ([u2 + 1, v + 1] * m)[: m // 2]
)
break
else:
continue
break
else:
continue
break
if len(ans) > 0:
print("YES")
print(*ans)
continue
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER LIST BIN_OP VAR NUMBER BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.
You are given a complete directed graph with $n$ vertices without self-loops. In other words, you have $n$ vertices and each pair of vertices $u$ and $v$ ($u \neq v$) has both directed edges $(u, v)$ and $(v, u)$.
Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $(u, v)$ and $(v, u)$ may have different labels).
You are also given an integer $m > 0$. You should find a path of length $m$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.
You can visit the same vertex and the same directed edge any number of times.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 1000$; $1 \leq m \leq 10^{5}$) — the number of vertices in the graph and desirable length of the palindrome.
Each of the next $n$ lines contains $n$ characters. The $j$-th character of the $i$-th line describes the character on the edge that is going from node $i$ to node $j$.
Every character is either 'a' or 'b' if $i \neq j$, or '*' if $i = j$, since the graph doesn't contain self-loops.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $1000$ and the sum of $m$ doesn't exceed $10^5$.
-----Output-----
For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $m + 1$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them.
Otherwise, (if there is no answer) print "NO".
-----Examples-----
Input
5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*
Output
YES
1 2
YES
2 1 3 2
YES
1 3 1 3 1
YES
1 2 1 3 4 1 4
NO
-----Note-----
The graph from the first three test cases is shown below:
In the first test case, the answer sequence is $[1,2]$ which means that the path is:
$$1 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is b.
In the second test case, the answer sequence is $[2,1,3,2]$ which means that the path is:
$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$
So the string that is obtained by the given path is bab.
In the third test case, the answer sequence is $[1,3,1,3,1]$ which means that the path is:
$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$
So the string that is obtained by the given path is aaaa.
The string obtained in the fourth test case is abaaba. | from sys import stdin
t = int(stdin.readline())
def run():
L = [int(x) for x in stdin.readline().split(" ")]
n, m = L[0], L[1]
M = []
for i in range(n):
M.append(stdin.readline())
if m % 2 == 1:
res = []
for k in range(m):
res.append(1)
res.append(2)
res = res[: m + 1]
print("YES")
print(" ".join([str(x) for x in res]))
return
for i in range(n):
for j in range(i + 1, n):
if M[i][j] == M[j][i]:
res = []
for k in range(m):
res.append(i + 1)
res.append(j + 1)
res = res[: m + 1]
print("YES")
print(" ".join([str(x) for x in res]))
return
if n == 2:
print("NO")
return
fi, fj, fk = -1, -1, -1
for i in range(3):
for j in range(3):
for k in range(3):
if i != j and j != k and i != k:
if M[i][j] == M[j][k]:
fi = i
fj = j
fk = k
md = m // 2
if md % 2 == 0:
pattern = [fj, fi, fj, fk]
res = pattern * m
res = res[: m + 1]
else:
pattern = [fi, fj, fk, fj]
res = pattern * m
res = res[: m + 1]
print("YES")
print(" ".join([str(x + 1) for x in res]))
for _ in range(t):
run() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR RETURN IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones.
Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a.
You can apply the following operations any number of times:
* Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»);
* Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»).
You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring.
What is the minimum number of coins you need to spend to get a string consisting only of ones?
Input
The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring).
The second line contains the string a of length n, consisting of zeros and ones.
Output
Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations.
Examples
Input
5 1 10
01000
Output
11
Input
5 10 1
01000
Output
2
Input
7 2 3
1111111
Output
0
Note
In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5].
Then the string was changed as follows:
«01000» → «10000» → «11111».
The total cost of operations is 1 + 10 = 11.
In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5].
Then the string was changed as follows:
«01000» → «11000» → «11111».
The overall cost is 1 + 1 = 2.
In the third example, string already consists only of ones, so the answer is 0. | n, x, y = map(int, input().split())
s = input().replace("1", " ")
z = len(s.split())
print(z and (z - 1) * min(x, y) + y) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR |
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones.
Let's call a sequence of consecutive elements a_i, a_{i + 1}, …, a_j (1≤ i≤ j≤ n) a substring of string a.
You can apply the following operations any number of times:
* Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» → «0111001»);
* Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» → «0110001»).
You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring.
What is the minimum number of coins you need to spend to get a string consisting only of ones?
Input
The first line of input contains integers n, x and y (1 ≤ n ≤ 300 000, 0 ≤ x, y ≤ 10^9) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring).
The second line contains the string a of length n, consisting of zeros and ones.
Output
Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 0, if you do not need to perform any operations.
Examples
Input
5 1 10
01000
Output
11
Input
5 10 1
01000
Output
2
Input
7 2 3
1111111
Output
0
Note
In the first sample, at first you need to reverse substring [1 ... 2], and then you need to invert substring [2 ... 5].
Then the string was changed as follows:
«01000» → «10000» → «11111».
The total cost of operations is 1 + 10 = 11.
In the second sample, at first you need to invert substring [1 ... 1], and then you need to invert substring [3 ... 5].
Then the string was changed as follows:
«01000» → «11000» → «11111».
The overall cost is 1 + 1 = 2.
In the third example, string already consists only of ones, so the answer is 0. | n, x, y = map(int, input().split())
s = input()
nGroups = int(s[0] == "0")
for i in range(1, n):
if s[i - 1] != s[i] == "0":
nGroups += 1
if nGroups == 0:
print(0)
else:
print((nGroups - 1) * min(x, y) + y) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.