description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | a = int(input())
lister = input().split()
lister = [int(i) for i in lister]
ans = dict()
def findans(n):
if n in ans:
return ans[n]
mod = n % lister[n]
ok = True
if n + lister[n] >= a and n - lister[n] < 0:
ok = False
else:
for i in range(mod, a, lister[n]):
if i != n and lister[i] > lister[n]:
ok = ok and findans(i)
ok = not ok
ans[n] = ok
return ok
for i in range(len(lister)):
findans(i)
level = []
for i in range(a):
if ans[i] == True:
level.append("A")
else:
level.append("B")
print("".join(level)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n = int(input())
board = list(map(int, input().split(" ")))
index = list(range(0, n))
ascending = [x for _, x in sorted(zip(board, index))]
winners = n * [""]
for c in reversed(ascending):
if board[c] == n:
winners[c] = "B"
toCheck = c - board[c]
while toCheck >= 0:
if winners[toCheck] == "B":
winners[c] = "A"
toCheck = toCheck - board[c]
if winners[c] == "":
toCheck = c + board[c]
while toCheck < n:
if winners[toCheck] == "B":
winners[c] = "A"
toCheck = toCheck + board[c]
if winners[c] == "":
winners[c] = "B"
for i in range(n):
print(winners[i], end="")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR LIST STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | import sys
input = sys.stdin.readline
out = sys.stdout
n = int(input())
a = list(map(int, input().split()))
pos = [0] * n
for i in range(n):
a[i] -= 1
pos[a[i]] = i
win = ["?"] * n
for x in range(n - 1, -1, -1):
i = pos[x]
win[i] = "B"
j = i
while True:
j -= x + 1
if j < 0:
break
if win[j] == "B":
win[i] = "A"
break
j = i
while True:
j += x + 1
if j >= n:
break
if win[j] == "B":
win[i] = "A"
break
for k in win:
out.write(k) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR WHILE NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR WHILE NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n = int(input())
a = list(map(int, input().split()))
ans = [-1] * n
loc = {}
for i in range(n):
loc[a[i]] = i
for i in range(n):
if a[i] > i and a[i] >= n - i:
ans[i] = "B"
prev = 0
while ans.count(-1) != prev:
prev = ans.count(-1)
for i in range(n):
if ans[i] == -1:
for j in range(i % a[i], n, a[i]):
if a[j] > a[i] and ans[j] == "B":
ans[i] = "A"
break
while ans.count(-1) != 0:
for i in range(n):
if ans[i] == -1:
ans[i] = "B"
for j in range(i % a[i], n, a[i]):
if a[i] >= a[j]:
continue
if ans[j] == "B":
ans[i] = "A"
break
elif ans[j] == -1:
ans[i] = -1
break
for i in ans:
print(i, end="")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR NUMBER WHILE FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR STRING WHILE FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n = int(input())
a = list(map(int, input().split()))
p = [0] * (n + 2)
m = [0] * (n + 2)
for i in range(n):
p[a[i]] = i
for i in range(n, 0, -1):
j = p[i] % i
while j < n and m[i] == 0:
m[i] = i < a[j] and m[a[j]] == 0
j += i
for i in range(n):
print("BA"[m[a[i]]], end="") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING VAR VAR VAR STRING |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | import sys
cin = sys.stdin
n = int(next(cin))
a = list(map(int, next(cin).split()))
n2idx = {a[i]: i for i in range(n)}
f = [False] * (n + 1)
for i in range(n, 0, -1):
idx_lg = n2idx[i]
win_flag = False
for j in range(idx_lg % i, n, i):
if a[j] > i and not f[a[j]]:
win_flag = True
break
f[i] = win_flag
f = "".join([("A" if f[a_i] else "B") for a_i in a])
print(f, flush=True) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR STRING STRING VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n = int(input())
a = list(map(int, input().split()))
res = []
dp = [-1] * n
def helper(i):
if dp[i] != -1:
return dp[i]
cur = True
std = a[i]
while i + std < n:
if a[i] < a[i + std]:
cur = cur and helper(i + std)
std += a[i]
std = a[i]
while i - std >= 0:
if a[i] < a[i - std]:
cur = cur and helper(i - std)
std += a[i]
if cur:
dp[i] = 0
else:
dp[i] = 1
return dp[i]
for i in range(n):
if helper(i):
res.append("A")
else:
res.append("B")
print("".join(res)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n = int(input())
a = [int(x) for x in input().split()]
h = [-1] * n
b = [(a[i], i) for i in range(n)]
b.sort(reverse=True)
for e in b:
num, idx = e
flag = True
allNeg = True
foundLosing = False
foundWin = False
for i in range(idx % num, n, num):
if i == idx:
continue
if h[i] != -1:
allNeg = False
if h[i] == 0:
foundLosing = True
break
if h[i] == 1:
foundWin = False
if allNeg:
h[idx] = 0
elif foundLosing:
h[idx] = 1
else:
h[idx] = 0
for i in range(n):
if h[i] == 0:
print("B", end="")
else:
print("A", end="") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING STRING |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n = int(input())
x = list(map(int, input().split()))
dp = [0] * n
m = [0] * (n + 1)
for i in range(n):
m[x[i]] = i
for i in range(n, 0, -1):
for i2 in range(m[i] + i, n, i):
if i < x[i2] and dp[i2] == 0:
dp[m[i]] = 1
for i2 in range(m[i] - i, -1, -i):
if i < x[i2] and dp[i2] == 0:
dp[m[i]] = 1
for i in range(n):
if dp[i] == 0:
print("B", end="")
else:
print("A", end="") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING STRING |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n = int(input())
a = [int(x) for x in input().split()]
a_reverse = a.copy()
status = []
for i in range(n):
a_reverse[a[i] - 1] = i
status.append(None)
pos = a_reverse[n - 1]
status[pos] = False
fails = set()
fails.add(pos)
for i in range(n - 1, 0, -1):
i_ = i - 1
pos = a_reverse[i_]
for k in range((pos + 1) % i - 1, n, i):
if k == pos:
continue
if k in fails:
status[pos] = True
break
if not status[pos]:
status[pos] = False
fails.add(pos)
result = ""
for i in status:
if i == True:
result = result + "A"
else:
result = result + "B"
print(result) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NONE ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n = int(input())
a = [int(s) for s in input().split()]
ans = [None] * n
def get(p):
if ans[p] is not None:
return ans[p]
elif a[p] == 1:
ans[p] = "A"
elif a[p] == n:
ans[p] = "B"
else:
for i in range(p + a[p], n, a[p]):
if a[i] > a[p]:
if get(i) == "B":
ans[p] = "A"
return ans[p]
for i in range(p - a[p], -1, -a[p]):
if a[i] > a[p]:
if get(i) == "B":
ans[p] = "A"
return ans[p]
ans[p] = "B"
return ans[p]
if n == 1:
print("B")
else:
for i in range(n - 1, -1, -1):
get(i)
print("".join(ans)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR FUNC_DEF IF VAR VAR NONE RETURN VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR VAR ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR IF FUNC_CALL VAR VAR STRING ASSIGN VAR VAR STRING RETURN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR IF FUNC_CALL VAR VAR STRING ASSIGN VAR VAR STRING RETURN VAR VAR ASSIGN VAR VAR STRING RETURN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | i = m = n = int(input())
a = [*map(int, input().split())]
s = [0] * n
r = [range(i % a[i], n, a[i]) for i in range(n)]
while m:
i = (i - 1) % n
if s[i] == 0:
if all(a[j] <= a[i] or s[j] == "A" for j in r[i]):
s[i] = "B"
m -= 1
if any(a[j] > a[i] and s[j] == "B" for j in r[i]):
s[i] = "A"
m -= 1
print("".join(s)) | ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR STRING VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | import sys
n = int(input())
arr = list(map(int, input().split()))
if n == 1:
print("B")
sys.exit()
ind = [0] * (n + 1)
for i in range(n):
ind[arr[i]] = i
l = [False] * (n + 1)
l[1] = True
for i in range(n - 1, 1, -1):
f = ind[i] - i
trig = False
while f >= 0:
if arr[f] > i:
if l[arr[f]] == False:
l[i] = True
break
f -= i
f = ind[i] + i
while f <= n - 1:
if arr[f] > i:
if l[arr[f]] == False:
l[i] = True
break
f += i
s = ["B"] * n
for i in range(n):
if l[arr[i]]:
s[i] = "A"
print("".join(s)) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | import sys
input = sys.stdin.readline
n = int(input())
arr = [int(x) for x in input().split()]
both = []
for i in range(n):
both.append([arr[i], i])
both.sort(reverse=True)
color = [-1] * n
for val, index in both:
done = False
for i in range(index + val, n, val):
if done:
break
if color[i] == 1:
done = True
color[index] = 0
for i in range(index - val, -1, -val):
if done:
break
if color[i] == 1:
done = True
color[index] = 0
if not done:
color[index] = 1
ans = ""
for i in color:
if i:
ans += "B"
else:
ans += "A"
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR IF VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n = int(input())
a = [int(x) for x in input().split()]
ans = [""] * n
b = [0] * n
for i in range(n):
b[n - a[i]] = i
for i in range(len(b)):
t = b[i] % (n - i)
f = 0
for j in range(t, n, n - i):
if ans[j] == "B" and a[j] > n - i:
ans[b[i]] = "A"
f = 1
break
if f == 0:
ans[b[i]] = "B"
print("".join(ans)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR VAR STRING VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n = int(input())
a = [*map(int, input().split())]
s = [0] * n
t = set(range(n))
while t:
q = set()
for i in t:
x = a[i]
r = range(i % x, n, x)
if all(a[j] <= x or s[j] == "A" for j in r):
s[i] = "B"
q |= {i}
if any(a[j] > x and s[j] == "B" for j in r):
s[i] = "A"
q |= {i}
t -= q
print("".join(s)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR STRING VAR VAR ASSIGN VAR VAR STRING VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR STRING VAR VAR ASSIGN VAR VAR STRING VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
p = [[a[i], i] for i in range(n)]
p.sort(reverse=True)
lose = [0] * n
for v, x in p:
lose[x] = 1
for i in range(1, n):
y = x + i * a[x]
if y >= n:
break
if a[y] > a[x] and lose[y]:
lose[x] = 0
for i in range(1, n):
y = x - i * a[x]
if y < 0:
break
if a[y] > a[x] and lose[y]:
lose[x] = 0
ans = []
for i in range(n):
if lose[i]:
ans.append("B")
else:
ans.append("A")
print("".join(ans)) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | R = lambda: map(int, input().split())
n = int(input())
a = list(R())
arr = sorted(([x, i] for i, x in enumerate(a)), reverse=True)
res = [-1] * n
for x, i in arr:
res[i] = (
0
if any(res[j] == 1 for j in range(i + x, n, x) if a[j] > a[i])
or any(res[j] == 1 for j in range(i - x, -1, -x) if a[j] > a[i])
else 1
)
print("".join("B" if x else "A" for x in res)) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR STRING STRING VAR VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n = int(input())
board = list(map(int, input().strip().split()))
board.insert(0, 0)
hashed = [0] * (n + 1)
for i in range(n + 1):
hashed[board[i]] = i
answer = ["C"] * (n + 1)
for i in range(n, 0, -1):
flag = 0
k = hashed[i] - board[hashed[i]]
while k > 0:
if answer[k] == "B":
flag = 1
break
k -= board[hashed[i]]
k = hashed[i] + board[hashed[i]]
while k <= n and k != 0:
if answer[k] == "B":
flag = 1
break
k += board[hashed[i]]
if flag == 1:
answer[hashed[i]] = "A"
else:
answer[hashed[i]] = "B"
answer.pop(0)
print("".join(answer)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR WHILE VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR WHILE VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n = int(input())
p = [int(x) for x in input().split()]
dp = ["B" for _ in range(n)]
idx = sorted([[p[i], i] for i in range(n)], reverse=True)
idx = [x[1] for x in idx]
for i in idx:
for j in range(1, n):
ind = i + j * p[i]
ind1 = i - j * p[i]
if ind >= n and ind1 < 0:
break
if ind < n and p[ind] > p[i] and dp[ind] == "B":
dp[i] = "A"
break
if ind1 >= 0 and p[ind1] > p[i] and dp[ind1] == "B":
dp[i] = "A"
break
print("".join(dp)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR STRING IF VAR NUMBER VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n = int(input())
arr = [int(x) for x in input().split()]
pos = dict()
if n == 1:
print("B")
else:
for i in range(n):
pos[arr[i]] = i
ans = ["Q"] * n
ans[pos[1]] = "A"
ans[pos[n]] = "B"
for i in range(n - 1, 0, -1):
flag = 0
p = pos[i]
j = 1
while p + j * i < n:
if ans[p + j * i] == "B":
flag = 1
ans[pos[i]] = "A"
break
j += 1
if flag == 0:
j = 1
while p - j * i >= 0:
if ans[p - j * i] == "B":
flag = 1
ans[pos[i]] = "A"
break
j += 1
if flag == 0:
ans[pos[i]] = "B"
print("".join(ans)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR VAR NUMBER STRING ASSIGN VAR VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | def solve(n, a):
ainv = {}
for i in range(1, n + 1):
ainv[a[i - 1]] = i
sol = {}
for i in range(n, 0, -1):
ai = ainv[i]
j = (ai - 1) % i + 1
while j <= n:
if j in sol and sol[j] == "B":
sol[ai] = "A"
break
j += i
else:
sol[ai] = "B"
return "".join([sol[i] for i in range(1, n + 1)])
n = int(input().strip())
a = list(map(int, input().strip().split()))
print(solve(n, a)) | FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR STRING ASSIGN VAR VAR STRING VAR VAR ASSIGN VAR VAR STRING RETURN FUNC_CALL STRING VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | import sys
input = sys.stdin.readline
def find(x):
global dp
if len(adj[x]) == 0:
dp[x] = False
return False
else:
if dp[x] != -1:
return dp[x]
ans = False
for neighbor in adj[x]:
ans |= not find(neighbor)
dp[x] = ans
return dp[x]
n = int(input().strip())
arr = list(map(int, input().strip().split()))
adj = {}
for i in range(n):
lis = []
for j in range(i + arr[i], n, arr[i]):
if arr[j] > arr[i] and (j - i) % arr[i] == 0:
lis.append(arr[j])
for j in range(i - arr[i], -1, -arr[i]):
if arr[j] > arr[i] and (i - j) % arr[i] == 0:
lis.append(arr[j])
adj[arr[i]] = lis
dp = [(-1) for i in range(n + 1)]
for i in range(n):
dp[arr[i]] = find(arr[i])
for x in arr:
print("A" if dp[x] else "B", end="") | IMPORT ASSIGN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING STRING STRING |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n = int(input())
a = [int(i) for i in input().split()]
indx = [0] * n
winners = [""] * n
for i, ai in enumerate(a):
indx[ai - 1] = i
for ai in range(n, 0, -1):
i = indx[ai - 1]
can_win = False
for j in range(i + ai, n, ai):
if a[j] > ai and "B" == winners[j]:
can_win = True
break
if not can_win:
for j in range(i - ai, -1, -ai):
if a[j] > ai and "B" == winners[j]:
can_win = True
break
if can_win:
winners[i] = "A"
else:
winners[i] = "B"
print("".join(winners)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR STRING VAR VAR ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR STRING VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n = int(input())
arr = [0] + list(map(int, input().split()))
indx = [(0) for i in range(n + 1)]
for i in range(1, n + 1):
indx[arr[i]] = i
ans = [(-1) for i in range(n + 1)]
for i in range(n, 0, -1):
ind1 = indx[i] - i
count_zero, count_one, count_min_one = 0, 0, 0
while ind1 >= 1:
if ans[ind1] == -1:
count_min_one += 1
if ans[ind1] == 0:
count_zero += 1
if ans[ind1] == 1:
count_one += 1
ind1 -= i
ind2 = indx[i] + i
while ind2 <= n:
if ans[ind2] == -1:
count_min_one += 1
if ans[ind2] == 0:
count_zero += 1
if ans[ind2] == 1:
count_one += 1
ind2 += i
if count_zero >= 1:
ans[indx[i]] = 1
else:
ans[indx[i]] = 0
for i in range(1, n + 1):
if ans[i] == 1:
print("A", end="")
else:
print("B", end="") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING STRING |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n = int(input())
a = [*map(int, input().split())]
b = [0] * n
for i in range(n):
b[n - a[i]] = i
s = [0] * n
for i, j in enumerate(b):
i = n - i
s[j] = "BA"[any(a[k] > a[j] and s[k] == "B" for k in range(j % i, n, i))]
print("".join(s)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR STRING FUNC_CALL VAR VAR VAR VAR VAR VAR VAR STRING VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n = int(input())
arr = list(map(int, input().strip().split()))
dicti = {}
for i in range(n):
dicti[arr[i]] = i + 1
ans = [(-1) for i in range(n + 1)]
if n > 1:
ans[dicti[1]] = 1
for i in range(n, 1, -1):
for j in range(dicti[i] % i - 1, n, i):
if arr[j] > i:
if ans[j + 1] == 0:
ans[dicti[i]] = 1
break
else:
ans[dicti[i]] = 0
print("".join([("A" if i == 1 else "B") for i in ans[1:]])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER STRING STRING VAR VAR NUMBER |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n = int(input())
a = [int(i) for i in input().split()]
s = ["." for _ in range(n)]
v = 0
t = set(range(n))
while v < n:
for i in range(n):
if s[i] != ".":
continue
r = range(i % a[i], n, a[i])
if all(a[j] <= a[i] or s[j] == "A" for j in r):
s[i] = "B"
v += 1
if any(a[j] > a[i] and s[j] == "B" for j in r):
s[i] = "A"
v += 1
print("".join(s)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR STRING VAR VAR ASSIGN VAR VAR STRING VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR STRING VAR VAR ASSIGN VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n = int(input())
arr = list(map(int, input().split()))
memo = [(-1) for i in range(n + 1)]
def can_win(idx):
if memo[idx] != -1:
return memo[idx]
res = False
delta = arr[idx]
nidx = idx + delta
while nidx < n:
if arr[nidx] > arr[idx] and not can_win(nidx):
res = True
break
nidx += delta
nidx = idx - delta
while not res and nidx >= 0:
if arr[nidx] > arr[idx] and not can_win(nidx):
res = True
break
nidx -= delta
memo[idx] = res
return res
ans = [("A" if can_win(i) else "B") for i in range(n)]
print("".join(ans)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR NUMBER IF VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR STRING STRING VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int, input().split()))
PLACE = [None] * (n + 1)
for i in range(n):
PLACE[A[i]] = i
al = n
WINLIST = [None] * (n + 1)
def move(n, al):
place = PLACE[n]
for i in range(place, -1, -n):
if A[i] > n and WINLIST[A[i]] == "B":
WINLIST[n] = "A"
return
for i in range(place, al, n):
if A[i] > n and WINLIST[A[i]] == "B":
WINLIST[n] = "A"
return
else:
WINLIST[n] = "B"
return
for j in range(n, 0, -1):
move(j, al)
ANS = ""
for i in A:
ANS += WINLIST[i]
print(ANS) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR STRING RETURN FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR STRING RETURN ASSIGN VAR VAR STRING RETURN FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | def f(k):
if an[k] != -1:
return an[k]
z = a[k]
ans = "B"
for i in range(k, -1, -z):
if a[i] > z:
if f(i) == "B":
ans = "A"
for i in range(k, n, z):
if a[i] > z:
if f(i) == "B":
ans = "A"
an[k] = ans
return ans
n = int(input())
a = list(map(int, input().split()))
an = [-1] * n
for i in range(n):
f(i)
print("".join(an)) | FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR VAR IF FUNC_CALL VAR VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR IF FUNC_CALL VAR VAR STRING ASSIGN VAR STRING ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n = int(input())
a = list(map(int, input().split()))
b = sorted((x, i) for i, x in enumerate(a))
c = ["B"] * n
while b:
x, i = b.pop()
for j in range(i % x, n, x):
if a[j] > a[i] and "B" == c[j]:
c[i] = "A"
break
print("".join(c)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR STRING VAR VAR ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n, a = int(input()), list(map(int, input().split()))
mem, arr = [""] * (n + 1), [0] * n
for i in range(n):
arr[a[i] - 1] = i
if a[i] == n:
mem[i] = "B"
for i in arr[n - 2 :: -1]:
flag = -1
for j in range(i + a[i], n, a[i]):
if mem[j] == "B":
flag = j
break
if flag != -1:
mem[i] = "A"
else:
for j in range(i - a[i], -1, -a[i]):
if mem[j] == "B":
flag = j
break
if flag != -1:
mem[i] = "A"
else:
mem[i] = "B"
print("".join(mem)) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST STRING BIN_OP VAR NUMBER BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR STRING FOR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n = int(input())
arr = [int(x) for x in input().split()]
d = {x: i for i, x in enumerate(arr)}
ans = ["B"] * len(arr)
for x in reversed(range(1, n + 1)):
i, k = d[x], 1
while x * k <= n - 1:
y1 = i + k * x
y2 = i - k * x
if y1 <= n - 1:
if arr[y1] > x and ans[y1] == "B":
ans[i] = "A"
break
if y2 >= 0:
if arr[y2] > x and ans[y2] == "B":
ans[i] = "A"
break
k += 1
print("".join(ans)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER WHILE BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR STRING IF VAR NUMBER IF VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n = int(input())
line = list(map(int, input().split()))
a = [(0) for i in range(n)]
ans = [(0) for _ in range(n)]
ans[0] = 1
ans[-1] = -1
for i in range(n):
a[line[i] - 1] = i
for i in range(n - 2, 0, -1):
cur = -1
num = i + 1
k = 0
start = a[i] % num
while start + num * k < n:
temp = start + num * k
if line[temp] > num:
if ans[line[temp] - 1] == -1:
cur = 1
break
k += 1
ans[i] = cur
answer = ""
for i in line:
if ans[i - 1] == 1:
answer += "A"
else:
answer += "B"
print(answer) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR WHILE BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR STRING FOR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | from sys import stdin
input = stdin.readline
n = int(input())
a = [int(x) for x in input().split()]
b = [0] * (n + 1)
for i in range(n):
b[a[i]] = i
c = [False] * n
for i in range(n, 0, -1):
j = i
w = False
while b[i] + j < n and w == False:
w = c[b[i] + j]
j += i
j = i
while b[i] - j >= 0 and w == False:
w = c[b[i] - j]
j += i
c[b[i]] = not w
ans = []
for i in range(n):
ans.append(["A", "B"][c[i]])
print(*ans, sep="") | ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST STRING STRING VAR VAR EXPR FUNC_CALL VAR VAR STRING |
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$.
-----Output-----
Print $s$Β β a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B".
-----Examples-----
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
-----Note-----
In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. | n = int(input())
a = list(map(int, input().split(" ")))
if n == 1:
print("B")
exit()
indices = [(0) for i in range(n + 1)]
for i in range(1, n + 1):
indices[a[i - 1]] = i
steps = [(0) for i in range(n + 1)]
steps[1] = 1
existing = {}
existing[indices[n]] = steps[n]
existing[indices[1]] = steps[1]
for i in range(n - 1, 1, -1):
index = indices[i]
k = i
while True:
if index - k < 1 and index + k > n:
existing[index] = steps[i]
break
else:
if existing.get(index - k) is not None and existing[index - k] == 0:
steps[i] = 1
existing[index] = steps[i]
break
if existing.get(index + k) is not None and existing[index + k] == 0:
steps[i] = 1
existing[index] = steps[i]
break
k += i
ans = ["A" for i in range(n)]
for i in existing.keys():
if existing[i] == 0:
ans[i - 1] = "B"
print("".join(ans)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NONE VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NONE VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | dc = {}
def slv(n, prime=2):
if n == 1:
return 1
global dc
for i in range(2, 10000):
if i * i > prime:
continue
if prime % i == 0:
return slv(n, prime + 1)
if (n, prime) in dc:
return dc[n, prime]
ans = 10**50
for i in range(2, n + 1):
if n % i == 0:
ans = min(ans, prime ** (i - 1) * slv(n // i, prime + 1))
dc[n, prime] = ans
return ans
n = int(input())
print(slv(n)) | ASSIGN VAR DICT FUNC_DEF NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | import sys
input = sys.stdin.readline
n = int(input())
y = [2, 3, 5, 7, 11, 13, 17, 23, 29, 31]
def dfs(n, pos):
v = [1, 10**18][n > 1]
for i in range(2, n + 1):
if n % i == 0:
v = min(v, y[pos] ** (i - 1) * dfs(n // i, pos + 1))
return v
print(dfs(n, 0)) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST NUMBER BIN_OP NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | INF = 20**18
primes = []
a = [1] * 100
def crivo():
for i in range(2, 100):
if a[i]:
primes.append(i)
for j in range(i + i, 100, i):
a[j] = 0
def dp(div, idx):
div = int(div)
idx = int(idx)
if div == 1:
return 1
resp = INF
for i in range(1, div):
if div % (i + 1) == 0:
resp = min(resp, primes[idx] ** i * dp(div / (i + 1), idx + 1))
return resp
crivo()
n = int(input())
print(dp(n, 0)) | ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43]
def solve(n, pos):
ans = 10**18 if n > 1 else 1
for exp in range(2, n + 1):
if n % exp == 0:
ans = min(ans, p[pos] ** (exp - 1) * solve(n // exp, pos + 1))
return ans
inp = int(input())
print(solve(inp, 0)) | ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | from itertools import combinations
def prime_divisors(n):
a = []
i = 2
while i * i <= n:
if n % i == 0:
a.append(i)
n //= i
else:
i += 1
a.append(n)
a.sort(reverse=True)
return a
n = int(input())
npr = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
res = []
delit = prime_divisors(n)
for i in range(2, len(delit)):
data = list(combinations(delit, i))
for j in data:
jj = list(j)
temp = 1
for k in jj:
temp *= k
temp1 = n // temp
jj.append(temp1)
jj.sort(reverse=True)
if jj not in res:
res.append(jj)
ttemp = sorted([temp, temp1], reverse=True)
if ttemp not in res:
res.append(ttemp)
tt = [n, 1]
res.append(tt)
res.append(delit)
minc = int(1e18) + 1
for i in res:
j = 0
ss = 1
for ii in i:
ss *= npr[j] ** (ii - 1)
if ss > int(1e18) + 1:
ss = int(1e18) + 1
break
j += 1
minc = min(minc, ss)
print(minc) | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | n = int(input())
def get_factors(n):
result = []
factor = 2
while n != 1:
if n % factor == 0:
n = n // factor
result.append(factor)
else:
factor += 1
return result
factors = get_factors(n)
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
options = list(reversed(factors))
if options == [2, 2, 2]:
options = [4, 2]
answer = 1
for option, prime in zip(options, primes):
answer *= prime ** (option - 1)
print(answer) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | n = int(input())
pr = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61]
pprr = set(pr)
dp = [[(0) for i in range(9)] for j in range(1005)]
def it(n, pref):
global dp
if dp[n][pref] > 0:
return dp[n][pref]
if n in pprr or pref == 0:
dp[n][pref] = 2 ** (n - 1)
return dp[n][pref]
dp[n][pref] = it(n, pref - 1)
d = 2
while d * d <= n:
if n % d == 0:
dd = d
t = pr[pref] ** (dd - 1) * it(n // dd, pref - 1)
dp[n][pref] = min(t, dp[n][pref])
dd = n // d
t = pr[pref] ** (dd - 1) * it(n // dd, pref - 1)
dp[n][pref] = min(t, dp[n][pref])
d += 1
return dp[n][pref]
print(it(n, 8)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | def main():
def f(x, i, aa):
if x == 1:
return 1
bb = [q for q in aa if not x % q]
p = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43)[i]
return min(p ** (d - 1) * f(x // d, i + 1, bb) for d in bb)
n = int(input())
print(f(n, 0, [d for d in range(2, n + 1) if not n % d]))
main() | FUNC_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | prime = [0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
min_value = round(1e18)
n = int(input())
def search(value, total_factor, i):
global min_value
if total_factor == n:
min_value = min(value, min_value)
return
for k in range(1, 65):
value *= prime[i]
if value > min_value:
break
if total_factor * (k + 1) <= n:
search(value, total_factor * (k + 1), i + 1)
search(1, 1, 1)
print(min_value) | ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR IF VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | def prime(x):
for i in range(2, x):
if x % i == 0:
return False
return True
def Solve(i, Cnt):
if i == len(L):
ans = 1
for j in range(9):
ans *= P[j] ** Cnt[j]
Min[0] = min(Min[0], ans)
return
for j in range(9):
if Cnt[j] == 0:
Cnt[j] = L[i] - 1
Solve(i + 1, Cnt)
Cnt[j] = 0
break
e = Cnt[j]
t = Cnt[j] + 1
t *= L[i]
t -= 1
Cnt[j] = t
Solve(i + 1, Cnt)
Cnt[j] = e
return
n = int(input())
L = []
for i in range(2, n + 1):
if n % i == 0 and prime(i):
x = n
while x % i == 0:
x //= i
L.append(i)
L.sort(reverse=True)
P = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
Min = [10**18]
Solve(0, [0, 0, 0, 0, 0, 0, 0, 0, 0])
print(Min[0]) | FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | def isPrime(i) -> bool:
t = 2
while t * t <= i:
if i % t == 0:
return False
t += 1
return i > 1
MAX_ANS = 10**18
d = int(input())
bound = 25
p = [-1]
for i in range(100000):
if isPrime(i):
p.append(i)
if len(p) > bound:
break
dp = [([MAX_ANS + 1] * (1 + bound)) for _ in range(1 + d)]
for i in range(1 + bound):
dp[1][i] = 1
for i in range(2, 1 + d):
for k in range(1, 1 + bound):
dp[i][k] = dp[1][k - 1] * p[k] ** (i - 1)
t = 2
while t * t <= i:
if i % t == 0:
for k in range(1, 1 + bound):
dp[i][k] = min(
dp[i][k],
dp[i // t][k - 1] * p[k] ** (t - 1),
dp[t][k - 1] * p[k] ** (i // t - 1),
)
t += 1
print(min(dp[d])) | FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER VAR NUMBER RETURN VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | P = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
def power(a, b):
res = 1
while b:
if b & 1:
res *= a
a *= a
b >>= 1
return res
ans = []
ans.append(1e30)
def solve(pos, n, res):
if n == 1:
ans[0] = min(ans[0], res)
for i in range(2, 62):
if n % i == 0:
solve(pos + 1, n / i, res * power(P[pos], i - 1))
n = int(input())
solve(0, n, 1)
print(ans[0]) | ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | p = [2, 3, 5, 7, 11, 13, 17, 23, 29, 31]
b = int(input())
N = [([1] * 11) for i in range(b + 1)]
for j in range(10):
N[1][j] = 1
for i in range(2, b + 1):
for j in range(1, 11):
m = None
for k in range(i):
f = i % (k + 1)
if f == 0:
if j > 1:
t = N[i // (k + 1)][j - 1] * p[j - 1] ** k
else:
t = p[j - 1] ** (i - 1)
if m is None or t < m:
m = t
N[i][j] = m
print(N[b][10]) | ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NONE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
dp = [[(-1) for i in range(12)] for i in range(1100)]
def solve(divisors, index):
if divisors == 1:
return 1
if index == 11:
return 9999999999999999999999999999
if dp[divisors][index] != -1:
return dp[divisors][index]
ans = 9999999999999999999999999999
for i in range(1, divisors + 1):
if divisors % i == 0:
temp = solve(divisors // i, index + 1) * primes[index] ** (i - 1)
ans = min(ans, temp)
dp[divisors][index] = ans
return dp[divisors][index]
n = int(input())
print(solve(n, 0)) | ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
N = 0
ans = 1000000000000000000
def dfs(i, x, n):
global ans
if n > N:
return
if n == N and x < ans:
ans = x
for j in range(1, 61):
if ans <= x * p[i]:
return
x *= p[i]
dfs(i + 1, x, n * (j + 1))
N = int(input())
dfs(0, 1, 1)
print(ans) | ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR VAR RETURN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | pri = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
def deal(x, y):
if x == 1:
tmp = 1
else:
tmp = 10**18
for csx in range(2, x + 1):
if 0 == x % csx:
tmp = min(tmp, pri[y] ** (csx - 1) * deal(x // csx, y + 1))
return tmp
print(deal(int(input()), 0)) | ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | n = int(input())
p = []
f = []
def prime(x):
for i in range(2, x):
if x % i == 0:
return False
return True
def factorize(x):
i = 2
while i * i <= x:
while x % i == 0:
f.append(i)
x //= i
i = i + 1
if x > 1:
f.append(x)
f.reverse()
x = 2
while len(p) < 70:
if prime(x):
p.append(x)
x = x + 1
factorize(n)
if n == 1:
print(n)
else:
sum = f[0]
ans = p[0] ** (f[0] - 1)
for i in range(1, len(f)):
c1 = ans * p[i] ** (f[i] - 1)
c2 = ans // 2 ** (sum - 1) * 2 ** (sum * f[i] - 1)
if c1 < c2:
ans = c1
else:
ans = c2
sum *= f[i]
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | n = int(input())
prime = [(1) for i in range(1001)]
primes = []
for i in range(2, 1000 + 1):
if prime[i] == 1:
for j in range(i * i, 1000 + 1, i):
prime[j] = 0
primes.append(i)
def recurse(n, last):
if n == 1:
return 1
ans = 10**18
for i in range(2, n + 1):
if n % i == 0:
ans = min(ans, primes[last] ** (i - 1) * recurse(n // i, last + 1))
return ans
print(recurse(n, 0)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
n = int(input())
dp = []
for i in range(n + 1):
t = []
for j in range(len(p)):
t.append(0)
dp.append(t)
for i in range(10):
dp[1][i] = 1
for i in range(2, n + 1):
dp[i][0] = p[0] ** (i - 1)
for j in range(1, len(p)):
dp[i][j] = dp[i][j - 1]
for k in range(2, i + 1):
if i % k > 0:
continue
dp[i][j] = min(dp[i][j], dp[i // k][j - 1] * p[j] ** (k - 1))
A = dp[n][0]
for i in range(len(p)):
A = min(A, dp[n][i])
print(A) | ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | prime = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
dp = [[(10**18) for i in range(15)] for j in range(1006)]
inf = 10**18
for pw in range(64):
dp[pw + 1][0] = 2**pw
for idx in range(1, 13):
for i in range(1005):
for pw in range(64):
if i % (pw + 1) == 0:
if dp[i // (pw + 1)][idx - 1] > inf // prime[idx] ** pw:
break
dp[i][idx] = min(
dp[i][idx], dp[i // (pw + 1)][idx - 1] * prime[idx] ** pw
)
print(dp[int(input())][12]) | ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | n = int(input())
factors = [i for i in range(2, n + 1) if n % i == 0]
primes = []
is_composite = [False] * (n + 1)
for i in range(2, n + 1):
if not is_composite[i]:
primes.append(i)
for p in primes:
j = i * p
if j > n:
break
is_composite[j] = True
if i % p == 0:
break
def factorize(m=n, s=0):
if m == 1:
return [[]]
res = [[m]]
for i in range(s, len(factors)):
f = factors[i]
if f > m**0.5:
break
if m % f != 0:
continue
else:
for l in factorize(m // f, i):
res.append([f] + l)
return res
def rep_exp(a, b):
if b == 0:
return 1
x = rep_exp(a, b // 2)
return x * x * (a if b % 2 == 1 else 1)
m = float("inf")
for l in factorize():
prod = 1
for i in range(len(l)):
prod *= rep_exp(primes[len(l) - i - 1], l[i] - 1)
m = min(prod, m)
print(m) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER FUNC_DEF VAR NUMBER IF VAR NUMBER RETURN LIST LIST ASSIGN VAR LIST LIST VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | def fn(n, index, primes):
if n == 1:
return 1
ans = 10**19
for i in range(2, n + 1):
if n % i == 0:
ans = min(ans, primes[index] ** (i - 1) * fn(n // i, index + 1, primes))
return ans
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
n = int(input())
print(fn(n, 0, primes)) | FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | PRIMOS = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
def solucion(n, prim_i=0, numero=1, divisores=1):
if divisores == n:
return numero
sol = 10**18
primo = PRIMOS[prim_i]
for i in range(1, 64):
if numero * primo > sol:
break
if divisores * (i + 1) > n:
break
numero *= primo
aux = solucion(n, prim_i + 1, numero, divisores * (i + 1))
if aux < sol:
sol = aux
return sol
n = int(input())
if n == 1:
print(1)
else:
print(solucion(n)) | ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF NUMBER NUMBER NUMBER IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | n = int(input())
p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
ans = int(1e19)
def calc(i, cur, val):
global ans
if cur == n:
ans = min(ans, val)
return
if cur > n or i > 10:
return
t = p[i]
for j in range(1, 61):
if ans >= t * val:
calc(i + 1, cur * (j + 1), val * t)
t = t * p[i]
calc(0, 1, 1)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN IF VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | inf = 1 << 64
p = [1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
ans = inf
linf = int(1e18)
def dfs(x, n, m):
if n == 1:
global ans
ans = min(ans, m)
return
l, k = 1, p[x]
while l < n and k < linf:
if n % (l + 1) == 0:
if m * k > linf:
break
dfs(x + 1, n // (l + 1), m * k)
l += 1
k *= p[x]
n = int(input())
dfs(1, n, 1)
print(ans) | ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR VAR NUMBER VAR VAR WHILE VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | def fact(n):
p = {}
d = 2
while d * d <= n:
while n % d == 0:
p[d - 1] = p.get(d - 1, 0) + 1
n //= d
d += 1
if n > 1:
p[n - 1] = p.get(n - 1, 0) + 1
return p
def isPrime(n):
if n % 2 == 0:
return n == 2
d = 3
while d * d <= n and n % d != 0:
d += 2
return d * d > n
m = int(input())
g = fact(m)
z = []
for i, j in g.items():
z.extend([i] * j)
z.sort(reverse=True)
res = 1
pros = 2
for i in z:
res *= pros**i
pros += 1
while not isPrime(pros):
pros += 1
x = list(g)
if m > 1:
y = g[x[0]] - 3
if len(g) == 1 and x[0] == 1 and m > 4:
res = 24
kkk = 5
while y > 0:
res *= kkk
y -= 1
kkk += 2
print(res) | FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | n = int(input())
g = lambda n: [q for q in range(2, n + 1) if n % q == 0]
t = {k: g(k) for k in g(n)}
p = [2, 3, 5, 7, 11, 13, 17, 19, 23]
def f(i, k):
return min(f(i + 1, k // s) * p[i] ** (s - 1) for s in t[k]) if k > 1 else 1
print(f(0, n)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF RETURN VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | def prime_factors(n):
i = 2
factors = []
while i * i <= n:
if n % i:
i += 1
else:
n //= i
factors.append(i)
if n > 1:
factors.append(n)
return factors
num = int(input())
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41]
k = prime_factors(num)
k.reverse()
c = 1
for i in range(len(k)):
c *= primes[0] ** (k[i] - 1)
primes[0] = primes[0] ** k[i]
primes.sort()
print(c) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
Input
The first line of the input contains integer n (1 β€ n β€ 1000).
Output
Output the smallest positive integer with exactly n divisors.
Examples
Input
4
Output
6
Input
6
Output
12 | def is_prime(n):
i = 2
while i * i <= n:
if n % i == 0:
return False
i += 1
return True
m = {}
p = []
i = 2
while len(p) < 12:
if is_prime(i):
p.append(i)
i += 1
INF = 10**18
m = {}
ans = INF
def f(a, b, div):
global ans
if b >= len(p):
return
if div == n:
ans = min(ans, a)
return
for j in range(1, 60):
if a <= ans // p[b]:
a *= p[b]
f(a, b + 1, div * (j + 1))
else:
break
n = int(input())
f(1, 0, 1)
print(ans) | FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
A tree is an undirected connected graph without cycles.
You are given a tree of n vertices. Find the number of ways to choose exactly k vertices in this tree (i. e. a k-element subset of vertices) so that all pairwise distances between the selected vertices are equal (in other words, there exists an integer c such that for all u, v (u β v, u, v are in selected vertices) d_{u,v}=c, where d_{u,v} is the distance from u to v).
Since the answer may be very large, you need to output it modulo 10^9 + 7.
Input
The first line contains one integer t (1 β€ t β€ 10) β the number of test cases. Then t test cases follow.
Each test case is preceded by an empty line.
Each test case consists of several lines. The first line of the test case contains two integers n and k (2 β€ k β€ n β€ 100) β the number of vertices in the tree and the number of vertices to be selected, respectively. Then n - 1 lines follow, each of them contains two integers u and v (1 β€ u, v β€ n, u β v) which describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges.
Output
For each test case output in a separate line a single integer β the number of ways to select exactly k vertices so that for all pairs of selected vertices the distances between the vertices in the pairs are equal, modulo 10^9 + 7 (in other words, print the remainder when divided by 1000000007).
Example
Input
3
4 2
1 2
2 3
2 4
3 3
1 2
2 3
5 3
1 2
2 3
2 4
4 5
Output
6
0
1 | for _ in range(int(input())):
c = input()
n, K = map(int, input().split())
e = [[] for i in range(n)]
for i in range(n - 1):
t1, t2 = map(int, input().split())
t1 -= 1
t2 -= 1
e[t1].append(t2)
e[t2].append(t1)
bj = [1] * 101
ans = [0]
def dfs(x, depth):
bj[x] = 0
for i in e[x]:
if bj[i]:
dfs(i, depth + 1)
for j in range(n):
a[x][j] += a[i][j]
a[x][depth] += 1
if depth == 0:
for i in range(n):
f = [0] * (K + 1)
f[0] = 1
for j in e[x]:
if bj[j] and a[j][i]:
for k in range(K, 0, -1):
f[k] = (f[k] + f[k - 1] * a[j][i]) % 1000000007
ans[0] = (ans[0] + f[K]) % 1000000007
bj[x] = 1
for i in range(n):
a = [([0] * n) for j in range(n)]
dfs(i, 0)
if K == 2:
print(n * (n - 1) // 2)
else:
print(ans[0] % 1000000007) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER |
A tree is an undirected connected graph without cycles.
You are given a tree of n vertices. Find the number of ways to choose exactly k vertices in this tree (i. e. a k-element subset of vertices) so that all pairwise distances between the selected vertices are equal (in other words, there exists an integer c such that for all u, v (u β v, u, v are in selected vertices) d_{u,v}=c, where d_{u,v} is the distance from u to v).
Since the answer may be very large, you need to output it modulo 10^9 + 7.
Input
The first line contains one integer t (1 β€ t β€ 10) β the number of test cases. Then t test cases follow.
Each test case is preceded by an empty line.
Each test case consists of several lines. The first line of the test case contains two integers n and k (2 β€ k β€ n β€ 100) β the number of vertices in the tree and the number of vertices to be selected, respectively. Then n - 1 lines follow, each of them contains two integers u and v (1 β€ u, v β€ n, u β v) which describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges.
Output
For each test case output in a separate line a single integer β the number of ways to select exactly k vertices so that for all pairs of selected vertices the distances between the vertices in the pairs are equal, modulo 10^9 + 7 (in other words, print the remainder when divided by 1000000007).
Example
Input
3
4 2
1 2
2 3
2 4
3 3
1 2
2 3
5 3
1 2
2 3
2 4
4 5
Output
6
0
1 | import sys
input = sys.stdin.readline
F = [None] * 101
F[0] = 1
for i in range(1, len(F)):
F[i] = F[i - 1] * i % int(1000000000.0 + 7)
def rec(x, p, g, c, s):
c[s] += 1
for v in g[x]:
if v != p:
rec(v, x, g, c, s + 1)
def solve():
input()
n, k = map(int, input().split())
g = [[] for i in range(n + 1)]
for i in range(n - 1):
u, v = map(int, input().split())
g[u].append(v)
g[v].append(u)
MOD = int(1000000000.0 + 7)
if k == 2:
print(n * (n - 1) // 2 % MOD)
return
r = 0
for x in range(1, n + 1):
d = [([0] * n) for i in range(k + 1)]
d[0] = [1] * n
for v in g[x]:
c = [0] * n
rec(v, x, g, c, 0)
for i in range(k - 1, -1, -1):
for j in range(n):
d[i + 1][j] = (d[i + 1][j] + d[i][j] * c[j]) % MOD
for j in range(n):
r = (r + d[k][j]) % MOD
print(r)
for i in range(int(input())):
solve() | IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP LIST NUMBER VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
A tree is an undirected connected graph without cycles.
You are given a tree of n vertices. Find the number of ways to choose exactly k vertices in this tree (i. e. a k-element subset of vertices) so that all pairwise distances between the selected vertices are equal (in other words, there exists an integer c such that for all u, v (u β v, u, v are in selected vertices) d_{u,v}=c, where d_{u,v} is the distance from u to v).
Since the answer may be very large, you need to output it modulo 10^9 + 7.
Input
The first line contains one integer t (1 β€ t β€ 10) β the number of test cases. Then t test cases follow.
Each test case is preceded by an empty line.
Each test case consists of several lines. The first line of the test case contains two integers n and k (2 β€ k β€ n β€ 100) β the number of vertices in the tree and the number of vertices to be selected, respectively. Then n - 1 lines follow, each of them contains two integers u and v (1 β€ u, v β€ n, u β v) which describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges.
Output
For each test case output in a separate line a single integer β the number of ways to select exactly k vertices so that for all pairs of selected vertices the distances between the vertices in the pairs are equal, modulo 10^9 + 7 (in other words, print the remainder when divided by 1000000007).
Example
Input
3
4 2
1 2
2 3
2 4
3 3
1 2
2 3
5 3
1 2
2 3
2 4
4 5
Output
6
0
1 | import sys
input = sys.stdin.readline
t = int(input())
mod = 10**9 + 7
for _ in range(t):
tmp = input()
n, k = map(int, input().split())
g = [[] for i in range(n)]
for __ in range(n - 1):
u, v = map(int, input().split())
g[u - 1].append(v - 1)
g[v - 1].append(u - 1)
f = [1] * 101
ans = 0
def dfs(u, depth):
global ans
f[u] = 0
for v in g[u]:
if f[v]:
dfs(v, depth + 1)
for j in range(n):
dp[u][j] += dp[v][j]
dp[u][depth] += 1
if depth == 0:
for i in range(n):
c = [0] * (k + 1)
c[0] = 1
for to in g[u]:
if f[to] and dp[to][i]:
for kk in range(1, k + 1)[::-1]:
c[kk] += c[kk - 1] * dp[to][i] % mod
c[kk] %= mod
ans = (ans + c[k]) % mod
f[u] = 1
for i in range(n):
dp = [([0] * n) for i in range(n)]
dfs(i, 0)
if k == 2:
print(n * (n - 1) // 2)
else:
print(ans % mod) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
A tree is an undirected connected graph without cycles.
You are given a tree of n vertices. Find the number of ways to choose exactly k vertices in this tree (i. e. a k-element subset of vertices) so that all pairwise distances between the selected vertices are equal (in other words, there exists an integer c such that for all u, v (u β v, u, v are in selected vertices) d_{u,v}=c, where d_{u,v} is the distance from u to v).
Since the answer may be very large, you need to output it modulo 10^9 + 7.
Input
The first line contains one integer t (1 β€ t β€ 10) β the number of test cases. Then t test cases follow.
Each test case is preceded by an empty line.
Each test case consists of several lines. The first line of the test case contains two integers n and k (2 β€ k β€ n β€ 100) β the number of vertices in the tree and the number of vertices to be selected, respectively. Then n - 1 lines follow, each of them contains two integers u and v (1 β€ u, v β€ n, u β v) which describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges.
Output
For each test case output in a separate line a single integer β the number of ways to select exactly k vertices so that for all pairs of selected vertices the distances between the vertices in the pairs are equal, modulo 10^9 + 7 (in other words, print the remainder when divided by 1000000007).
Example
Input
3
4 2
1 2
2 3
2 4
3 3
1 2
2 3
5 3
1 2
2 3
2 4
4 5
Output
6
0
1 | md = int(1000000000.0) + 7
N = 110
def add(x, y):
x += y
if x >= md:
x -= md
if x < 0:
x += md
return x
def mul(x, y):
x *= y
x %= md
return x
depth = [0] * N
cnt = [0]
g = [[] for _ in range(N)]
def Dfs(v, pr):
for to in g[v]:
if to != pr:
depth[to] = depth[v] + 1
cnt[depth[to]] += 1
Dfs(to, v)
def solve(n, k, v):
dp = [([1] + k * [0]) for _ in range(n)]
depth[v] = 0
res = 0
for to in g[v]:
global cnt
cnt = [0] * n
depth[to] = 1
cnt[depth[to]] += 1
Dfs(to, v)
for dep in range(n):
for ct in reversed(range(k)):
dp[dep][ct + 1] = add(dp[dep][ct + 1], mul(dp[dep][ct], cnt[dep]))
for i in range(n):
res = add(res, dp[i][k])
return res
t = int(input())
for _ in range(t):
input()
n, k = map(int, input().split())
for i in range(n):
g[i].clear()
for i in range(n - 1):
u, v = map(int, input().split())
u -= 1
v -= 1
g[u].append(v)
g[v].append(u)
if k == 2:
print(int(n * (n - 1) / 2 % md))
continue
ans = 0
for i in range(n):
ans = add(ans, solve(n, k, i))
print(ans) | ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR LIST NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
A tree is an undirected connected graph without cycles.
You are given a tree of n vertices. Find the number of ways to choose exactly k vertices in this tree (i. e. a k-element subset of vertices) so that all pairwise distances between the selected vertices are equal (in other words, there exists an integer c such that for all u, v (u β v, u, v are in selected vertices) d_{u,v}=c, where d_{u,v} is the distance from u to v).
Since the answer may be very large, you need to output it modulo 10^9 + 7.
Input
The first line contains one integer t (1 β€ t β€ 10) β the number of test cases. Then t test cases follow.
Each test case is preceded by an empty line.
Each test case consists of several lines. The first line of the test case contains two integers n and k (2 β€ k β€ n β€ 100) β the number of vertices in the tree and the number of vertices to be selected, respectively. Then n - 1 lines follow, each of them contains two integers u and v (1 β€ u, v β€ n, u β v) which describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges.
Output
For each test case output in a separate line a single integer β the number of ways to select exactly k vertices so that for all pairs of selected vertices the distances between the vertices in the pairs are equal, modulo 10^9 + 7 (in other words, print the remainder when divided by 1000000007).
Example
Input
3
4 2
1 2
2 3
2 4
3 3
1 2
2 3
5 3
1 2
2 3
2 4
4 5
Output
6
0
1 | mod = 1000000007
def min(x, y):
if x < y:
return x
return y
def solve(m, x):
if m < k:
return 0
f = [[(0) for _ in range(k + 1)] for _ in range(m + 1)]
f[0][0] = 1
for i in range(1, m + 1):
for j in range(0, min(k + 1, i + 1)):
if j == 0:
f[i][j] = 1
else:
f[i][j] = (f[i - 1][j] + cnt[i] * f[i - 1][j - 1] % mod) % mod
return f[m][k]
def process(x):
check[x] = 1
res = 0
q = []
for i in range(len(a[x])):
v = a[x][i]
check[v] = 1
q.append([v, i + 1])
res = solve(len(a[x]), x)
while len(q):
cur = []
for i in range(len(q)):
u = q[i][0]
j = q[i][1]
cnt[j] -= 1
for v in a[u]:
if check[v] == 0:
cur.append([v, j])
check[v] = 1
cnt[j] += 1
q = cur
res += solve(len(a[x]), x)
res %= mod
return res
test = int(input())
for _ in range(test):
input()
n, k = map(int, input().split())
a = [[] for _ in range(n + 1)]
for _ in range(n - 1):
x, y = map(int, input().split())
a[x].append(y)
a[y].append(x)
ans = 0
if k == 2:
print(n * (n - 1) // 2)
continue
for x in range(1, n + 1):
check = [(0) for _ in range(n + 1)]
cnt = [(1) for _ in range(n + 1)]
ans = (ans + process(x)) % mod
print(ans) | ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR RETURN VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
A tree is an undirected connected graph without cycles.
You are given a tree of n vertices. Find the number of ways to choose exactly k vertices in this tree (i. e. a k-element subset of vertices) so that all pairwise distances between the selected vertices are equal (in other words, there exists an integer c such that for all u, v (u β v, u, v are in selected vertices) d_{u,v}=c, where d_{u,v} is the distance from u to v).
Since the answer may be very large, you need to output it modulo 10^9 + 7.
Input
The first line contains one integer t (1 β€ t β€ 10) β the number of test cases. Then t test cases follow.
Each test case is preceded by an empty line.
Each test case consists of several lines. The first line of the test case contains two integers n and k (2 β€ k β€ n β€ 100) β the number of vertices in the tree and the number of vertices to be selected, respectively. Then n - 1 lines follow, each of them contains two integers u and v (1 β€ u, v β€ n, u β v) which describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges.
Output
For each test case output in a separate line a single integer β the number of ways to select exactly k vertices so that for all pairs of selected vertices the distances between the vertices in the pairs are equal, modulo 10^9 + 7 (in other words, print the remainder when divided by 1000000007).
Example
Input
3
4 2
1 2
2 3
2 4
3 3
1 2
2 3
5 3
1 2
2 3
2 4
4 5
Output
6
0
1 | import sys
input = sys.stdin.readline
t = int(input())
mod = 10**9 + 7
cnb_max = 1000
kai = [1] * (cnb_max + 1)
rkai = [1] * (cnb_max + 1)
for i in range(cnb_max):
kai[i + 1] = kai[i] * (i + 1) % mod
rkai[cnb_max] = pow(kai[cnb_max], mod - 2, mod)
for i in range(cnb_max):
rkai[cnb_max - 1 - i] = rkai[cnb_max - i] * (cnb_max - i) % mod
def cnb(x, y):
if y > x:
return 0
if x < 0:
return 0
if y < 0:
return 0
return kai[x] * rkai[y] * rkai[x - y] % mod
for iii in range(t):
num = input() + "#"
n, k = map(int, input().split())
root = [[] for _ in range(n + 1)]
for i in range(n - 1):
u, v = map(int, input().split())
root[u].append(v)
root[v].append(u)
def tree_search(n, G, s, func1, func2, func3):
seen = [0] * (n + 1)
ind = [0] * (n + 1)
search = [s]
while search:
now = search[-1]
if seen[now] == 0 and func1 != 0:
func1(now)
seen[now] = 1
if len(G[now]) > ind[now]:
next = G[now][ind[now]]
ind[now] += 1
if seen[next] > 0:
continue
if func2 != 0:
func2(now, next)
search.append(next)
else:
if func3 != 0:
func3(now)
search.pop()
ans = 0
if k == 2:
print(cnb(n, 2))
continue
for i in range(1, n + 1):
dp = [0] * (n + 3) * (n + 3)
def _(i, j):
return i * (n + 1) + j
def f3(x):
dp[_(x, 1)] = 1
for d in range(2, n + 1):
for y in root[x]:
dp[_(x, d)] += dp[_(y, d - 1)]
dp[_(x, d)] %= mod
tree_search(n, root, i, 0, 0, f3)
for d in range(1, n + 1):
sub = []
for x in root[i]:
if dp[_(x, d)] > 0:
sub.append(dp[_(x, d)])
if len(sub) == 0:
continue
sub = [1] + sub
m = len(sub) - 1
f = [([0] * (m + 1)) for l in range(m + 1)]
f[0][0] = 1
for x in range(m + 1):
for y in range(m + 1):
if y == 0:
f[x][y] = 1
continue
f[x][y] = f[x - 1][y] + f[x - 1][y - 1] * sub[x]
f[x][y] %= mod
if k <= m:
ans += f[m][k]
ans %= mod
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
] | class Solution:
def generateParenthesis(self, n):
result = set()
if n == 0:
result = [""]
else:
for i in range(1, n + 1):
temp1 = {("(" + x + ")") for x in self.generateParenthesis(i - 1)}
temp2 = {x for x in self.generateParenthesis(n - i)}
temp = {(x + y) for x in temp1 for y in temp2} | {
(x + y) for x in temp2 for y in temp1
}
result = result | temp
result = sorted(list(result))
return result | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR LIST STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR |
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
] | class Solution:
def generateParenthesis(self, n):
ret = []
def backtrack(left=0, right=0, tmp=""):
if len(tmp) == 2 * n:
ret.append(tmp)
return
if left < n:
backtrack(left + 1, right, tmp + "(")
if left > right:
backtrack(left, right + 1, tmp + ")")
backtrack()
return ret | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF NUMBER NUMBER STRING IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR STRING EXPR FUNC_CALL VAR RETURN VAR |
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
] | class Solution:
def generateParenthesis(self, n):
if not n:
return []
cache = [["(", 1, n - 1]]
for _ in range(2 * n - 1):
for _ in range(len(cache)):
temp = cache.pop(0)
if temp[1]:
toAdd = temp.copy()
toAdd[0] += ")"
toAdd[1] -= 1
cache.append(toAdd)
if temp[2]:
temp[0] += "("
temp[1] += 1
temp[2] -= 1
cache.append(temp)
return [items[0] for items in cache] | CLASS_DEF FUNC_DEF IF VAR RETURN LIST ASSIGN VAR LIST LIST STRING NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER STRING VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER VAR VAR |
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
] | class Solution:
def generateParenthesis(self, n):
if n < 1 or type(n) != int:
return []
ret = []
tmp = []
tmp.append({"str": "(", "cnt": 1, "rcnt": 0})
while tmp:
cur = tmp.pop()
if n == cur["cnt"]:
ret.append(cur["str"] + ")" * (n - cur["rcnt"]))
else:
if cur["cnt"] > cur["rcnt"]:
tmp.append(
{
"str": cur["str"] + ")",
"cnt": cur["cnt"],
"rcnt": cur["rcnt"] + 1,
}
)
tmp.append(
{
"str": cur["str"] + "(",
"cnt": cur["cnt"] + 1,
"rcnt": cur["rcnt"],
}
)
return ret | CLASS_DEF FUNC_DEF IF VAR NUMBER FUNC_CALL VAR VAR VAR RETURN LIST ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR DICT STRING STRING STRING STRING NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR STRING BIN_OP STRING BIN_OP VAR VAR STRING IF VAR STRING VAR STRING EXPR FUNC_CALL VAR DICT STRING STRING STRING BIN_OP VAR STRING STRING VAR STRING BIN_OP VAR STRING NUMBER EXPR FUNC_CALL VAR DICT STRING STRING STRING BIN_OP VAR STRING STRING BIN_OP VAR STRING NUMBER VAR STRING RETURN VAR |
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
] | class Solution:
def generateParenthesis(self, n):
if n == 0:
return [""]
if n == 1:
return ["()"]
results = []
for i in range(1, 2 * n, 2):
first = self.generateParenthesis(int((i - 1) / 2))
second = self.generateParenthesis(int((2 * n - i - 1) / 2))
results += [("(" + u + ")" + v) for u in first for v in second]
return results | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST STRING IF VAR NUMBER RETURN LIST STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP STRING VAR STRING VAR VAR VAR VAR VAR RETURN VAR |
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
] | class Solution:
def generateParenthesis(self, n):
def balance(str):
lst = list(str)
bal = 0
for char in lst:
if char == "(":
bal += 1
else:
bal -= 1
return bal
def left(str):
lst = list(str)
sum = 0
for char in lst:
if char == "(":
sum += 1
return n - sum
def right(str):
lst = list(str)
sum = 0
for char in lst:
if char == ")":
sum += 1
return n - sum
prev = ["("]
length = 1
while length < 2 * n:
current = []
for item in prev:
if left(item) != 0:
current.append(item + "(")
if right(item) != 0 and balance(item) > 0:
current.append(item + ")")
length += 1
prev = current
return prev | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR LIST STRING ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR STRING IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR STRING VAR NUMBER ASSIGN VAR VAR RETURN VAR |
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
] | class Solution:
def parenthesis(self, n):
return "(" * n + ")" * n
def generateParenthesis(self, n):
if n == 0:
return []
elif n == 1:
return ["()"]
result = []
for i in range(1, n):
r1 = self.generateParenthesis(i)
r2 = self.generateParenthesis(n - i)
for r11 in r1:
for r21 in r2:
result.append(r11 + r21)
r3 = self.generateParenthesis(n - 1)
for r31 in r3:
result.append("({})".format(r31))
result.sort()
rr = []
last = None
for r in result:
if r == last:
continue
rr.append(r)
last = r
return rr | CLASS_DEF FUNC_DEF RETURN BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR FUNC_DEF IF VAR NUMBER RETURN LIST IF VAR NUMBER RETURN LIST STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NONE FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR |
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
] | class Solution:
hashMap = {}
outPut = []
def helper(self, n, string, length, opened, openUsed):
if length == n * 2:
if string not in self.outPut:
self.outPut.append(string)
return
if string in self.hashMap:
return
else:
self.hashMap[string] = 1
if opened == 0:
tempOpened = 1
tempOpenUsed = openUsed + 1
self.helper(n, string + "(", length + 1, tempOpened, tempOpenUsed)
if opened > 0:
tempOpened = opened - 1
self.helper(n, string + ")", length + 1, tempOpened, openUsed)
if openUsed < n:
tempOpened = opened + 1
tempOpenUsed = openUsed + 1
self.helper(n, string + "(", length + 1, tempOpened, tempOpenUsed)
def generateParenthesis(self, n):
self.hashMap = {}
self.outPut = []
self.helper(n, "", 0, 0, 0)
return self.outPut | CLASS_DEF ASSIGN VAR DICT ASSIGN VAR LIST FUNC_DEF IF VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR RETURN ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR STRING BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR STRING BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR STRING BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR STRING NUMBER NUMBER NUMBER RETURN VAR |
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
] | class Solution:
def generateParenthesis(self, n):
answers = []
for i in range(0, n + 1):
answers.append(Solution.catalanHelper(i, answers))
print((i, answers))
return list(set(answers[n]))
@staticmethod
def catalanHelper(n, answers):
if n == 0:
return [""]
elif n == 1:
return ["()"]
else:
output = []
for i in range(0, n - 1):
front_list = answers[i]
end_list = answers[n - i - 1]
for front in front_list:
for end in end_list:
output.append("(" + front + ")" + end)
output.append("(" + front + end + ")")
return output | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN LIST STRING IF VAR NUMBER RETURN LIST STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING VAR STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING VAR VAR STRING RETURN VAR VAR |
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
] | class Solution:
def generateParenthesis(self, n):
if n == 0:
return []
left = right = n
result = []
self.generate(left, right, result, "")
return result
def generate(self, left, right, result, string):
if left == 0 and right == 0:
result.append(string)
return
if left:
self.generate(left - 1, right, result, string + "(")
if left < right:
self.generate(left, right - 1, result, string + ")") | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST ASSIGN VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR STRING RETURN VAR FUNC_DEF IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING |
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
] | class Solution:
def generateParenthesis(self, n):
self.res = []
self.dfs(n, 0, 0, "")
return self.res
def dfs(self, n, left, right, path):
if left > n or right > n:
return
if n == 0:
self.res.append(path)
return
if left == 0:
self.dfs(n, left + 1, right, path + "(")
if left == right and left != 0:
self.dfs(n - left, 0, 0, path)
if left > right:
self.dfs(n, left + 1, right, path + "(")
self.dfs(n, left, right + 1, path + ")") | CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER NUMBER STRING RETURN VAR FUNC_DEF IF VAR VAR VAR VAR RETURN IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR STRING |
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
] | class Solution:
def generateParenthesis(self, n):
stack = []
s = ""
self.dfs(stack, 0, 0, s, n)
return stack
def dfs(self, stack, first, last, s, n):
if last == n:
stack.append(s)
else:
if first < n:
self.dfs(stack, first + 1, last, s + "(", n)
if last < first:
self.dfs(stack, first, last + 1, s + ")", n) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR STRING VAR |
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
] | class Solution:
def generateParenthesis(self, n):
rst = []
cake = ""
self.generateRecursion(rst, cake, n, n)
return rst
def generateRecursion(self, rst, cake, left, right):
if right == 0:
rst.append(cake)
if left > 0:
self.generateRecursion(rst, cake + "(", left - 1, right)
if right > left:
self.generateRecursion(rst, cake + ")", left, right - 1) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR STRING BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR STRING VAR BIN_OP VAR NUMBER |
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
] | class Solution:
def generateParenthesis(self, n):
results = []
def genparen(prefix, opens, closes):
if len(prefix) == 2 * n:
results.append(prefix)
return
if opens < n:
genparen(prefix + "(", opens + 1, closes)
if closes < opens:
genparen(prefix + ")", opens, closes + 1)
genparen("", 0, 0)
return results | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING NUMBER NUMBER RETURN VAR |
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
] | class Solution:
def generateParenthesis(self, n):
self.mem = {}
return self.top_down(n)
def merge(self, left, right):
comb = []
for i in left:
comb += [(i + j) for j in right]
return list(set(comb))
def top_down(self, n):
if n == 1:
return ["()"]
if not n - 1 in self.mem:
self.mem[n - 1] = self.top_down(n - 1)
rest = self.mem[n - 1]
comb = [("(" + i + ")") for i in rest]
for i in range(1, n):
if not i in self.mem:
self.mem[i] = self.top_down(i)
comb_left = self.mem[i]
if not n - i in self.mem:
self.mem[n - i] = self.top_down(n - i)
comb_right = self.mem[n - i]
comb += self.merge(comb_left, comb_right)
comb = list(set(comb))
return comb | CLASS_DEF FUNC_DEF ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN LIST STRING IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING VAR STRING VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR |
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
] | class Solution:
def generateParenthesis(self, n):
def gen(n, open_par, res, res_set):
print(res)
if len(res) == 2 * n:
res_set.append("".join(res))
return
if 2 * n - len(res) > open_par:
res.append("(")
gen(n, open_par + 1, res, res_set)
res.pop()
if open_par > 0:
res.append(")")
gen(n, open_par - 1, res, res_set)
res.pop()
res_set = []
gen(n, 0, [], res_set)
return res_set | CLASS_DEF FUNC_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN IF BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER LIST VAR RETURN VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | n, m = map(int, input().split())
a = []
for i in range(n):
a.append([])
for x in input():
if x == "0":
a[i].append(0)
else:
a[i].append(1)
ans = 0
for x1 in range(0, m):
for x2 in range(x1, m):
for y1 in range(0, n):
for y2 in range(y1, n):
z = 0
for k in range(y1, y2 + 1):
z += sum(a[k][x1 : x2 + 1])
if z == 0:
ans = max(ans, 2 * (x2 - x1 + 1 + y2 - y1 + 1))
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | nm = input().split()
n = int(nm[0])
m = int(nm[1])
room = []
for row in range(n):
room.append(input())
best = 0
for row in range(n):
for col in range(m):
if room[row][col] == "1":
continue
col2 = m
row2 = row
while row2 < n:
col3 = col
while col3 < col2:
if room[row2][col3] == "1":
break
col3 += 1
col2 = col3
row2 += 1
if col2 != col and row2 != row:
score = col2 - col + (row2 - row)
best = max(best, score)
print(2 * best) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | n, m = map(int, input().split())
a = [list(map(int, input())) for i in range(n)]
p = [([0] * (m + 3)) for i in range(n + 3)]
for i in range(n):
for j in range(m):
p[i][j] = p[i - 1][j] + p[i][j - 1] - p[i - 1][j - 1] + a[i][j]
ans = 0
for i1 in range(n):
for j1 in range(m):
for i2 in range(i1, n):
for j2 in range(j1, m):
cur = (i2 - i1 + 1) * 2 + (j2 - j1 + 1) * 2
Sum = p[i2][j2] - p[i1 - 1][j2] - p[i2][j1 - 1] + p[i1 - 1][j1 - 1]
if Sum == 0 and cur > ans:
ans = cur
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | import sys
dim_list = sys.stdin.readline().split()
n, m = int(dim_list[0]), int(dim_list[1])
T = [[[[(-1) for l in range(m)] for k in range(m)] for j in range(n)] for i in range(n)]
for i in range(n):
row = sys.stdin.readline()
for j in range(m):
T[i][i][j][j] = int(row[j])
max_per = 4
for k in range(3, n + m + 1):
found_k = 0
low = max(1, k - m)
high = min(n, k - 1)
for h in range(low, high + 1):
w = k - h
for x_m in range(n - h + 1):
for y_m in range(m - w + 1):
if h == 1:
if (
T[x_m][x_m][y_m][y_m + w - 2] == 0
and T[x_m][x_m][y_m + w - 1][y_m + w - 1] == 0
):
T[x_m][x_m + h - 1][y_m][y_m + w - 1] = 0
max_per = 2 * k
elif w == 1:
if (
T[x_m][x_m + h - 2][y_m][y_m] == 0
and T[x_m + h - 1][x_m + h - 1][y_m][y_m] == 0
):
T[x_m][x_m + h - 1][y_m][y_m + w - 1] = 0
max_per = 2 * k
elif (
T[x_m][x_m + h - 2][y_m][y_m + w - 1] == 0
and T[x_m][x_m + h - 1][y_m][y_m + w - 2] == 0
and T[x_m + h - 1][x_m + h - 1][y_m + w - 1][y_m + w - 1] == 0
):
T[x_m][x_m + h - 1][y_m][y_m + w - 1] = 0
max_per = 2 * k
print(max_per) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR 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 FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | n, m = map(int, input().split())
s = ""
for i in range(n):
s += input()
def is_possible(p, q, r):
for i in range(p, p + q):
for j in range(r):
if s[i + j * m] == "1":
return False
return True
ans = 1
for i in range(m * n):
max_cols = n - i // m
max_rows = m - i % m
for j in range(1, max_rows + 1):
for k in range(1, max_cols + 1):
perimeter = 2 * (j + k)
if perimeter > ans and is_possible(i, j, k):
ans = perimeter
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR BIN_OP VAR VAR STRING RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | graph = []
def perimeter(x0, y0, x1, y1):
return (x1 - x0 + 1) * 2 + (y1 - y0 + 1) * 2
def check(x0, y0, x1, y1):
for i in range(x0, x1 + 1):
for j in range(y0, y1 + 1):
if graph[i][j] == "1":
return False
return True
def solve(n, m, graph):
res = 4
for i in range(n):
for j in range(m):
for ii in range(i, n):
for jj in range(j, m):
if not check(i, j, ii, jj):
continue
res = max(res, perimeter(i, j, ii, jj))
return res
n, m = map(int, input().split())
for _ in range(n):
graph.append(list(input()))
print(solve(n, m, graph)) | ASSIGN VAR LIST FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | def func(a, b):
for i in range(min(a[0], b[0]), max(a[0], b[0]) + 1):
for j in range(min(a[1], b[1]), max(a[1], b[1]) + 1):
if matx[i][j] == 1:
return False
return True
n, m = map(int, input().split())
x = []
matx = [[int(X) for X in list(input().strip())] for i in range(n)]
for i in range(n):
for j in range(m):
x.append((i, j))
an = -1
for i in range(len(x)):
for j in range(len(x)):
if func(x[i], x[j]):
an = max(
an, 2 * abs(x[i][0] - x[j][0] + 1) + 2 * abs(x[i][1] - x[j][1] + 1)
)
print(an) | FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | n, m = map(int, input().split())
mat = []
for i in range(n):
mat.append(input())
dp = [
[[[(True) for i in range(m)] for i in range(n)] for i in range(m)] for i in range(n)
]
ans = 0
for x1 in range(n):
for y1 in range(m):
for x2 in range(x1, n):
for y2 in range(y1, m):
dp[x1][y1][x2][y2] = (
dp[x1][y1][x2 - 1][y2]
and dp[x1][y1][x2][y2 - 1]
and mat[x2][y2] == "0"
)
if dp[x1][y1][x2][y2]:
ans = max(ans, 2 * (x2 - x1 + y2 - y1 + 2))
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR STRING IF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | n, m = map(int, input().split())
n, m = n + 1, m + 1
dp = [[(0) for j in range(m)] for i in range(n)]
for i in range(1, n):
s = input()
for j in range(1, m):
dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + int(s[j - 1])
mx = 0
for i in range(1, n):
for j in range(1, m):
for k in range(i, n):
for l in range(j, m):
ans = dp[k][l] + dp[i - 1][j - 1] - dp[k][j - 1] - dp[i - 1][l]
if not ans:
mx = max(mx, k - i + 1 + l - j + 1)
print(mx << 1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | n, m = [int(i) for i in input().split()]
matriz = []
for i in range(n):
matriz.append(input())
ans = 4
for i in range(n):
for j in range(m):
for k in range(i, n):
for l in range(j, m):
block = False
for x in range(i, k + 1):
for y in range(j, l + 1):
if matriz[x][y] == "1":
block = True
if not block:
ans = max(ans, (k + 1 - i) * 2 + (l + 1 - j) * 2)
print(ans) | 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | n, m = map(int, input().split())
l = []
for i in range(n):
l.append(list(map(int, input())))
l1 = [[] for i in range(n)]
for i in range(n):
for j in range(m):
if l[i][j] == 1:
l1[i].append(j)
ans = 4
for p in range(n):
for q in range(m):
if l[p][q] == 0:
y = n
x = m
for i in range(p + 1, n):
if l[i][q] == 1:
y = i
break
for i in range(p, y):
for k in l1[i]:
if k > q:
x = min(x, k)
break
ans = max(ans, 2 * (x - q + i - p + 1))
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL 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 NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16 | n, m = map(int, input().split())
dp = [[(0) for j in range(m)] for i in range(n)]
mp = []
for i in range(n):
mp.append(input())
for j in range(m):
if mp[i][j] == "1":
continue
if not i or mp[i - 1][j] == "1":
dp[i][j] = 1
else:
dp[i][j] = dp[i - 1][j] + 1
ans = 0
for i in range(n):
for j in range(m):
for k in range(1, dp[i][j] + 1):
l = j
while l >= 0 and dp[i][l] >= k:
l -= 1
ans = max(ans, j - l + k)
print(ans << 1) | 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 ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING IF VAR VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.